FixedGenerationCount.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.genetics;

  18. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;

  19. /**
  20.  * Stops after a fixed number of generations.
  21.  * <p>
  22.  * Each time {@link #isSatisfied(Population)} is invoked, a generation counter
  23.  * is incremented. Once the counter reaches the configured
  24.  * {@code maxGenerations} value, {@link #isSatisfied(Population)} returns true.
  25.  *
  26.  * @since 2.0
  27.  */
  28. public class FixedGenerationCount implements StoppingCondition {
  29.     /** Number of generations that have passed. */
  30.     private int numGenerations;

  31.     /** Maximum number of generations (stopping criteria). */
  32.     private final int maxGenerations;

  33.     /**
  34.      * Create a new FixedGenerationCount instance.
  35.      *
  36.      * @param maxGenerations number of generations to evolve
  37.      * @throws NumberIsTooSmallException if the number of generations is &lt; 1
  38.      */
  39.     public FixedGenerationCount(final int maxGenerations) throws NumberIsTooSmallException {
  40.         if (maxGenerations <= 0) {
  41.             throw new NumberIsTooSmallException(maxGenerations, 1, true);
  42.         }
  43.         this.maxGenerations = maxGenerations;
  44.     }

  45.     /**
  46.      * Determine whether or not the given number of generations have passed. Increments the number of generations
  47.      * counter if the maximum has not been reached.
  48.      *
  49.      * @param population ignored (no impact on result)
  50.      * @return <code>true</code> IFF the maximum number of generations has been exceeded
  51.      */
  52.     @Override
  53.     public boolean isSatisfied(final Population population) {
  54.         if (this.numGenerations < this.maxGenerations) {
  55.             numGenerations++;
  56.             return false;
  57.         }
  58.         return true;
  59.     }

  60.     /**
  61.      * Returns the number of generations that have already passed.
  62.      * @return the number of generations that have passed
  63.      */
  64.     public int getNumGenerations() {
  65.         return numGenerations;
  66.     }
  67. }