From 0c288cc1952809294c8d70d86b9f41b04878ac2e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 23 Mar 2014 18:05:13 +0000 Subject: Majorly refactored, node grid is fully implemented. About to attempt active path locking. --- src/jcgp/modules/ea/EvolutionaryAlgorithm.java | 6 ++-- src/jcgp/modules/ea/StandardEA.java | 39 +++++++++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) (limited to 'src/jcgp/modules/ea') 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 localParameters; + + public StandardEA() { + parents = new IntegerParameter(1, "Parents"); + offspring = new IntegerParameter(4, "Offspring"); + + localParameters = new HashMap(); + + 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 activate(Resources parameters) { + parameters.setManagedParameter("popSize", true); + + return localParameters; + } } -- cgit v1.2.3