diff options
Diffstat (limited to 'src/jcgp/ea')
-rw-r--r-- | src/jcgp/ea/EvolutionaryAlgorithm.java | 8 | ||||
-rw-r--r-- | src/jcgp/ea/StandardEA.java | 41 | ||||
-rw-r--r-- | src/jcgp/ea/StandardMutator.java | 2 |
3 files changed, 37 insertions, 14 deletions
diff --git a/src/jcgp/ea/EvolutionaryAlgorithm.java b/src/jcgp/ea/EvolutionaryAlgorithm.java index 9e72a1b..e084da0 100644 --- a/src/jcgp/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/ea/EvolutionaryAlgorithm.java @@ -4,12 +4,8 @@ import jcgp.population.Population; public interface EvolutionaryAlgorithm { -// protected Mutator mutator; -// -// public EvolutionaryAlgorithm(Mutator mutator) { -// this.mutator = mutator; -// } - public abstract void evolve(Population population, Mutator mutator); + public abstract int getBestFitness(); + } diff --git a/src/jcgp/ea/StandardEA.java b/src/jcgp/ea/StandardEA.java index b34c421..901333b 100644 --- a/src/jcgp/ea/StandardEA.java +++ b/src/jcgp/ea/StandardEA.java @@ -1,18 +1,45 @@ package jcgp.ea; +import jcgp.Parameters; import jcgp.population.Chromosome; import jcgp.population.Population; +/** + * (1 + λ) EA. + * + * + * @author Eduardo Pedroni + * + */ public class StandardEA implements EvolutionaryAlgorithm { + + private int bestFitness = 0; @Override - public void evolve(Population population, Mutator mutator) { - Chromosome select; - for (Chromosome chromosome : population) { - - - mutator.mutate(chromosome); + public void evolve(Population population, Mutator mutator) { + // select fittest chromosome + int fittest = 0; + + for (int i = 1; i < Parameters.getPopulationSize(); i++) { + if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) { + fittest = i; + } + } + bestFitness = population.getChromosome(fittest).getFitness(); + population.setBestIndividual(fittest); + System.out.println("Best fitness: " + bestFitness); + // create copies of fittest chromosome, mutate them + Chromosome fc = population.getChromosome(fittest); + for (int i = 0; i < Parameters.getPopulationSize(); i++) { + if (i != fittest) { + population.getChromosome(i).copyConnections(fc); + mutator.mutate(population.getChromosome(i)); + } } } - + + @Override + public int getBestFitness() { + return bestFitness; + } } diff --git a/src/jcgp/ea/StandardMutator.java b/src/jcgp/ea/StandardMutator.java index 447338a..b9c0a4e 100644 --- a/src/jcgp/ea/StandardMutator.java +++ b/src/jcgp/ea/StandardMutator.java @@ -11,7 +11,7 @@ public class StandardMutator implements Mutator { @Override public void mutate(Chromosome chromosome) { - int mutations = (int) (Parameters.getMutationRate() * ((double) Parameters.getNodeCount() / 100)); + int mutations = (int) (Parameters.getMutationRate() * (((double) Parameters.getNodeCount() + Parameters.getOutputs()) / 100)); for (int i = 0; i < mutations; i++) { MutableElement m = chromosome.getRandomMutableElement(); |