diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-08 14:48:25 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-08 14:48:25 +0000 |
commit | d63d3145f0f2abcee1bb88457324f4aaf9b9320e (patch) | |
tree | dfb19082adfba8989c4fd95ef286b8c1e7a1f2b1 /src/jcgp/modules/ea | |
parent | ef7a850a8f2f81ccaa07f25d9b7ad602e84d88c9 (diff) |
Slowly refactoring Parameters to fit the GUI a little better...
Diffstat (limited to 'src/jcgp/modules/ea')
-rw-r--r-- | src/jcgp/modules/ea/EvolutionaryAlgorithm.java | 13 | ||||
-rw-r--r-- | src/jcgp/modules/ea/StandardEA.java | 50 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/jcgp/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java new file mode 100644 index 0000000..8de8c87 --- /dev/null +++ b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java @@ -0,0 +1,13 @@ +package jcgp.modules.ea; + +import jcgp.modules.mutator.Mutator; +import jcgp.population.Chromosome; +import jcgp.population.Population; + +public interface EvolutionaryAlgorithm { + + public abstract void evolve(Population population, Mutator mutator); + + public abstract Chromosome getFittestChromosome(); + +} diff --git a/src/jcgp/modules/ea/StandardEA.java b/src/jcgp/modules/ea/StandardEA.java new file mode 100644 index 0000000..2db8776 --- /dev/null +++ b/src/jcgp/modules/ea/StandardEA.java @@ -0,0 +1,50 @@ +package jcgp.modules.ea; + +import jcgp.modules.mutator.Mutator; +import jcgp.parameters.Parameters; +import jcgp.parameters.IntegerParameter; +import jcgp.parameters.BooleanParameter; +import jcgp.population.Chromosome; +import jcgp.population.Population; + +/** + * (1 + λ) EA. + * + * + * @author Eduardo Pedroni + * + */ +public class StandardEA implements EvolutionaryAlgorithm { + + private Chromosome fittestChromosome; + + @Override + public void evolve(Population population, Mutator mutator) { + // select fittest chromosome + int fittest = 0; + + for (int i = 1; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) { + if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) { + fittest = i; + } + } + fittestChromosome = population.getChromosome(fittest); + population.setBestIndividual(fittest); + if (((BooleanParameter) Parameters.get("debug")).getValue()) { + System.out.println("Best fitness: " + fittestChromosome.getFitness()); + } + // create copies of fittest chromosome, mutate them + Chromosome fc = population.getChromosome(fittest); + for (int i = 0; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) { + if (i != fittest) { + population.getChromosome(i).copyConnections(fc); + mutator.mutate(population.getChromosome(i)); + } + } + } + + @Override + public Chromosome getFittestChromosome() { + return fittestChromosome; + } +} |