    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.math3.genetics;
    018
    019    import org.apache.commons.math3.exception.NumberIsTooSmallException;
    020
    021    /**
    022     * Stops after a fixed number of generations. Each time {@link #isSatisfied(Population)} is invoked, a generation
    023     * counter is incremented. Once the counter reaches the configured <code>maxGenerations</code> value,
    024     * {@link #isSatisfied(Population)} returns true.
    025     *
    026     * @version $Id: FixedGenerationCount.java 1416643 2012-12-03 19:37:14Z tn $
    027     * @since 2.0
    028     */
    029    public class FixedGenerationCount implements StoppingCondition {
    030        /** Number of generations that have passed */
    031        private int numGenerations = 0;
    032
    033        /** Maximum number of generations (stopping criteria) */
    034        private final int maxGenerations;
    035
    036        /**
    037         * Create a new FixedGenerationCount instance.
    038         *
    039         * @param maxGenerations number of generations to evolve
    040         * @throws NumberIsTooSmallException if the number of generations is &lt; 1
    041         */
    042        public FixedGenerationCount(final int maxGenerations) throws NumberIsTooSmallException {
    043            if (maxGenerations <= 0) {
    044                throw new NumberIsTooSmallException(maxGenerations, 1, true);
    045            }
    046            this.maxGenerations = maxGenerations;
    047        }
    048
    049        /**
    050         * Determine whether or not the given number of generations have passed. Increments the number of generations
    051         * counter if the maximum has not been reached.
    052         *
    053         * @param population ignored (no impact on result)
    054         * @return <code>true</code> IFF the maximum number of generations has been exceeded
    055         */
    056        public boolean isSatisfied(final Population population) {
    057            if (this.numGenerations < this.maxGenerations) {
    058                numGenerations++;
    059                return false;
    060            }
    061            return true;
    062        }
    063
    064        /**
    065         * Returns the number of generations that have already passed.
    066         * @return the number of generations that have passed
    067         */
    068        public int getNumGenerations() {
    069            return numGenerations;
    070        }
    071
    072    }