001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojavax.ga;
023
024/**
025 * Used by a <code>GeneticAlgorithm.run()</code> method
026 * to determine when the algorithm should stop</p>
027 * 
028 * @author Mark Schreiber
029 * @version 1.0
030 * @since 1.5
031 */
032
033public interface GAStoppingCriteria {
034
035  /**
036   * Determines if an Algorithm should stop spawning new generations
037   * @param ga the Algorithm to test
038   * @return true if it should stop, false otherwise.
039   */
040  public boolean stop(GeneticAlgorithm ga);
041
042  /**
043   * Simple Implementation of GAStoppingCriteria, signals
044   * a <code>GeneticAlgorithm</code> to stop after n generations</p>
045   *
046   * Useful for pausing and seeing what the
047   * state of the algorithm is at any particular time and possibly changing
048   * parameters etc before calling the run() method of the
049   * <code>GeneticAlgorithm</code> again.
050   * 
051   * @author Mark Schreiber
052   * @version 1.0
053   */
054  public class MaximumGeneration implements GAStoppingCriteria {
055    int maxGenerations;
056
057    /**
058     * Public Constructer
059     * @param maxGenerations the number of generations to stop after
060     */
061    public MaximumGeneration(int maxGenerations){
062      this.maxGenerations = maxGenerations;
063    }
064
065    /**
066     * Stops the Algorithm if the iterations are >= <code>maxGenerations</code>
067     * @param ga the genetic algorithm to assess for stopping
068     * @return true if the <code>ga</code> should stop
069     */
070    public boolean stop(GeneticAlgorithm ga){
071      return(ga.getGeneration() >= maxGenerations);
072    }
073
074    public int getMaxGenerations(){
075      return maxGenerations;
076    }
077  }
078
079}