    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.math.genetics;
    018
    019    /**
    020     * Stops after a fixed number of generations.  Each time
    021     * {@link #isSatisfied(Population)} is invoked, a generation counter is
    022     * incremented.  Once the counter reaches the configured
    023     * <code>maxGenerations</code> value, {@link #isSatisfied(Population)} returns
    024     * true.
    025     *
    026     * @version $Revision: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $
    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         */
    041        public FixedGenerationCount(int maxGenerations) {
    042            if (maxGenerations <= 0)
    043                throw new IllegalArgumentException("The number of generations has to be >= 0");
    044            this.maxGenerations = maxGenerations;
    045        }
    046
    047        /**
    048         * Determine whether or not the given number of generations have passed.
    049         * Increments the number of generations counter if the maximum has not
    050         * been reached.
    051         *
    052         * @param population ignored (no impact on result)
    053         * @return <code>true</code> IFF the maximum number of generations has been exceeded
    054         */
    055        public boolean isSatisfied(Population population) {
    056            if (this.numGenerations < this.maxGenerations) {
    057                numGenerations++;
    058                return false;
    059            }
    060            return true;
    061        }
    062
    063        /**
    064         * @return the number of generations that have passed
    065         */
    066        public int getNumGenerations() {
    067            return numGenerations;
    068        }
    069
    070    }