Index: GeneticAlgorithm.java =================================================================== --- GeneticAlgorithm.java (revision 746844) +++ GeneticAlgorithm.java (working copy) @@ -23,22 +23,47 @@ * @version $Revision$ $Date$ */ public class GeneticAlgorithm { + /** the crossover policy used by the algorithm. */ - private CrossoverPolicy crossoverPolicy; + protected final CrossoverPolicy crossoverPolicy; /** the rate of crossover for the algorithm. */ - private double crossoverRate; + protected final double crossoverRate; /** the mutation policy used by the algorithm. */ - private MutationPolicy mutationPolicy; + protected final MutationPolicy mutationPolicy; /** the rate of mutation for the algorithm. */ - private double mutationRate; + protected final double mutationRate; /** the selection policy used by the algorithm. */ - private SelectionPolicy selectionPolicy; - + protected final SelectionPolicy selectionPolicy; + /** + * @param crossoverPolicy The {@link CrossoverPolicy} + * @param crossoverRate The crossover rate as a percentage (0-1 inclusive) + * @param mutationPolicy The {@link MutationPolicy} + * @param mutationRate The mutation rate as a percentage (0-1 inclusive) + * @param selectionPolicy The {@link selectionPolicy} + */ + public GeneticAlgorithm( + CrossoverPolicy crossoverPolicy, double crossoverRate, + MutationPolicy mutationPolicy, double mutationRate, + SelectionPolicy selectionPolicy) { + if (crossoverRate < 0 || crossoverRate > 1) { + throw new IllegalArgumentException("crossoverRate must be between 0 and 1"); + } + if (mutationRate < 0 || mutationRate > 1) { + throw new IllegalArgumentException("mutationRate must be between 0 and 1"); + } + this.crossoverPolicy = crossoverPolicy; + this.crossoverRate = crossoverRate; + this.mutationPolicy = mutationPolicy; + this.mutationRate = mutationRate; + this.selectionPolicy = selectionPolicy; + } + + /** * Evolve the given population. Evolution stops when the stopping condition * is satisfied. * @@ -55,152 +80,76 @@ } /** - * Access the crossover policy. + * Evolve the given population into the next generation. * - * @return the crossover policy. + * @return the population for the next generation */ - private CrossoverPolicy getCrossoverPolicy() { - return crossoverPolicy; - } - - /** - * Access the crossover rate. - * - * @return the crossover rate. - */ - private double getCrossoverRate() { - return crossoverRate; - } - - /** - * Access the mutation policy. - * - * @return the mutation policy. - */ - private MutationPolicy getMutationPolicy() { - return mutationPolicy; - } - - /** - * Access the mutation rate. - * - * @return the mutation rate. - */ - private double getMutationRate() { - return mutationRate; - } - - /** - * Access the selection policy. - * - * @return the selection policy. - */ - private SelectionPolicy getSelectionPolicy() { - return selectionPolicy; - } - - /** - *

Evolve the given population into the next generation.

- *

    - *
  1. Get nextGeneration population to fill from current - * generation, using its nextGeneration method
  2. - *
  3. Loop until new generation is filled:
  4. - * - *
  5. Return nextGeneration
  6. - *
- *

- * - * - * @param current the current population. - * @return the population for the next generation. - */ - private Population nextGeneration(Population current) { + public Population nextGeneration(Population current) { Population nextGeneration = current.nextGeneration(); - while (nextGeneration.getPopulationSize() < nextGeneration - .getPopulationLimit()) { + while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { // select parent chromosomes ChromosomePair pair = getSelectionPolicy().select(current); // crossover? if (Math.random() < getCrossoverRate()) { // apply crossover policy to create two offspring - pair = getCrossoverPolicy().crossover(pair.getFirst(), - pair.getSecond()); + pair = getCrossoverPolicy().crossover(pair.getFirst(), pair.getSecond()); } // mutation? if (Math.random() < getMutationRate()) { // apply mutation policy to the chromosomes pair = new ChromosomePair( - getMutationPolicy().mutate(pair.getFirst()), - getMutationPolicy().mutate(pair.getSecond()) - ); + getMutationPolicy().mutate(pair.getFirst()), + getMutationPolicy().mutate(pair.getSecond())); } // add the first chromosome to the population nextGeneration.addChromosome(pair.getFirst()); // is there still a place for the second chromosome? - if (nextGeneration.getPopulationSize() < nextGeneration - .getPopulationLimit()) { + if (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { // add the second chromosome to the population nextGeneration.addChromosome(pair.getSecond()); } } return nextGeneration; - } - + } + /** - * Modify the crossover policy. - * - * @param value the new crossover policy. + * Returns the crossover policy. */ - public void setCrossoverPolicy(CrossoverPolicy value) { - this.crossoverPolicy = value; + public CrossoverPolicy getCrossoverPolicy() { + return crossoverPolicy; } /** - * Modify the crossover rate. - * - * @param value the new crossover rate. + * Returns the crossover rate. */ - public void setCrossoverRate(double value) { - this.crossoverRate = value; + public double getCrossoverRate() { + return crossoverRate; } /** - * Modify the mutation policy. - * - * @param value the new mutation policy. + * Returns the mutation policy. */ - public void setMutationPolicy(MutationPolicy value) { - this.mutationPolicy = value; + public MutationPolicy getMutationPolicy() { + return mutationPolicy; } /** - * Modify the mutation rate. - * - * @param value the new mutation rate. + * Returns the mutation rate. */ - public void setMutationRate(double value) { - this.mutationRate = value; + public double getMutationRate() { + return mutationRate; } /** - * Modify the selection policy. - * - * @param value the new selection policy. + * Returns the selection policy. */ - public void setSelectionPolicy(SelectionPolicy value) { - this.selectionPolicy = value; + public SelectionPolicy getSelectionPolicy() { + return selectionPolicy; } + }