diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-23 18:05:13 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-23 18:05:13 +0000 |
commit | 0c288cc1952809294c8d70d86b9f41b04878ac2e (patch) | |
tree | ef9671b711fe665a3156594663c083595861a4e6 /src/jcgp/modules/ea | |
parent | d3527a63e12c0e5288f1e7d2e2dc18e61d16b760 (diff) |
Majorly refactored, node grid is fully implemented. About to attempt active path locking.
Diffstat (limited to 'src/jcgp/modules/ea')
-rw-r--r-- | src/jcgp/modules/ea/EvolutionaryAlgorithm.java | 6 | ||||
-rw-r--r-- | src/jcgp/modules/ea/StandardEA.java | 39 |
2 files changed, 33 insertions, 12 deletions
diff --git a/src/jcgp/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java index 8de8c87..d3fa709 100644 --- a/src/jcgp/modules/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java @@ -1,12 +1,14 @@ package jcgp.modules.ea; +import jcgp.CGP.Resources; +import jcgp.modules.Module; import jcgp.modules.mutator.Mutator; import jcgp.population.Chromosome; import jcgp.population.Population; -public interface EvolutionaryAlgorithm { +public interface EvolutionaryAlgorithm extends Module { - public abstract void evolve(Population population, Mutator mutator); + public abstract void evolve(Population population, Mutator mutator, Resources parameters); public abstract Chromosome getFittestChromosome(); diff --git a/src/jcgp/modules/ea/StandardEA.java b/src/jcgp/modules/ea/StandardEA.java index 2db8776..f2473f5 100644 --- a/src/jcgp/modules/ea/StandardEA.java +++ b/src/jcgp/modules/ea/StandardEA.java @@ -1,14 +1,16 @@ package jcgp.modules.ea; +import java.util.HashMap; + +import jcgp.CGP.Resources; import jcgp.modules.mutator.Mutator; -import jcgp.parameters.Parameters; import jcgp.parameters.IntegerParameter; -import jcgp.parameters.BooleanParameter; +import jcgp.parameters.Parameter; import jcgp.population.Chromosome; import jcgp.population.Population; /** - * (1 + λ) EA. + * (μ + λ) EA. * * * @author Eduardo Pedroni @@ -17,28 +19,38 @@ import jcgp.population.Population; public class StandardEA implements EvolutionaryAlgorithm { private Chromosome fittestChromosome; + + private IntegerParameter parents, offspring; + private HashMap<String, Parameter> localParameters; + + public StandardEA() { + parents = new IntegerParameter(1, "Parents"); + offspring = new IntegerParameter(4, "Offspring"); + + localParameters = new HashMap<String, Parameter>(); + + localParameters.put("mu", parents); + localParameters.put("lambda", offspring); + } @Override - public void evolve(Population population, Mutator mutator) { + public void evolve(Population population, Mutator mutator, Resources parameters) { // select fittest chromosome int fittest = 0; - for (int i = 1; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) { + for (int i = 1; i < (int) parameters.get("popSize"); 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++) { + for (int i = 0; i < (int) parameters.get("popSize"); i++) { if (i != fittest) { population.getChromosome(i).copyConnections(fc); - mutator.mutate(population.getChromosome(i)); + mutator.mutate(population.getChromosome(i), parameters); } } } @@ -47,4 +59,11 @@ public class StandardEA implements EvolutionaryAlgorithm { public Chromosome getFittestChromosome() { return fittestChromosome; } + + @Override + public HashMap<String, Parameter> activate(Resources parameters) { + parameters.setManagedParameter("popSize", true); + + return localParameters; + } } |