diff options
Diffstat (limited to 'src/jcgp/modules')
-rw-r--r-- | src/jcgp/modules/Module.java | 5 | ||||
-rw-r--r-- | src/jcgp/modules/ea/StandardEA.java | 79 | ||||
-rw-r--r-- | src/jcgp/modules/fitness/TestCaseEvaluator.java | 17 | ||||
-rw-r--r-- | src/jcgp/modules/mutator/PointMutator.java | 30 |
4 files changed, 31 insertions, 100 deletions
diff --git a/src/jcgp/modules/Module.java b/src/jcgp/modules/Module.java index 7cf0b8d..9e0b267 100644 --- a/src/jcgp/modules/Module.java +++ b/src/jcgp/modules/Module.java @@ -6,9 +6,6 @@ import jcgp.JCGP.Resources; import jcgp.parameters.Parameter; public interface Module { - - public void activate(Resources parameters); - public HashMap<String, Parameter> getLocalParameters(); - + public ModuleStatus getStatus(Resources resources); } diff --git a/src/jcgp/modules/ea/StandardEA.java b/src/jcgp/modules/ea/StandardEA.java deleted file mode 100644 index 1d27004..0000000 --- a/src/jcgp/modules/ea/StandardEA.java +++ /dev/null @@ -1,79 +0,0 @@ -package jcgp.modules.ea; - -import java.util.HashMap; - -import jcgp.JCGP.Resources; -import jcgp.modules.mutator.Mutator; -import jcgp.parameters.IntegerParameter; -import jcgp.parameters.Parameter; -import jcgp.population.Chromosome; -import jcgp.population.Population; - -/** - * (μ + λ) EA. - * - * - * @author Eduardo Pedroni - * - */ -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, Resources parameters) { - // select fittest chromosome - int fittest = 0; - - 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); - // create copies of fittest chromosome, mutate them - Chromosome fc = population.getChromosome(fittest); - for (int i = 0; i < (int) parameters.get("popSize"); i++) { - if (i != fittest) { - population.getChromosome(i).copyConnections(fc); - mutator.mutate(population.getChromosome(i), parameters); - } - } - } - - @Override - public Chromosome getFittestChromosome() { - return fittestChromosome; - } - - @Override - public void activate(Resources parameters) { - parameters.getParameter("popSize").setManaged(true); - } - - @Override - public HashMap<String, Parameter> getLocalParameters() { - return localParameters; - } - - @Override - public String toString() { - return "(μ + λ)"; - } - - -} diff --git a/src/jcgp/modules/fitness/TestCaseEvaluator.java b/src/jcgp/modules/fitness/TestCaseEvaluator.java index 77b282c..d8c1255 100644 --- a/src/jcgp/modules/fitness/TestCaseEvaluator.java +++ b/src/jcgp/modules/fitness/TestCaseEvaluator.java @@ -3,6 +3,7 @@ package jcgp.modules.fitness; import java.util.HashMap; import jcgp.JCGP.Resources; +import jcgp.modules.ModuleStatus; import jcgp.parameters.Parameter; import jcgp.population.Population; @@ -11,13 +12,13 @@ public class TestCaseEvaluator implements FitnessFunction { @Override public void evaluate(Population population, Resources resources) { // for every chromosome in the population - for (int i = 0; i < (int) resources.get("popSize"); i++) { + for (int i = 0; i < resources.getInt("popSize"); i++) { int fitness = 0; // for every test case for (int t = 0; t < resources.getTestCaseCount(); t++) { population.getChromosome(i).setInputs(resources.getTestCase(t).getInputs()); // check every output - for (int o = 0; o < (int) resources.get("outputs"); o++) { + for (int o = 0; o < resources.getInt("outputs"); o++) { if (population.getChromosome(i).getOutput(o).calculate() == resources.getTestCase(t).getOutput(o)) { fitness++; } @@ -28,17 +29,17 @@ public class TestCaseEvaluator implements FitnessFunction { } @Override - public void activate(Resources parameters) { - // nothing - } - - @Override public HashMap<String, Parameter> getLocalParameters() { - return new HashMap<String, Parameter>(); + return null; } @Override public String toString() { return "Test case"; } + + @Override + public ModuleStatus getStatus(Resources resources) { + return null; + } } diff --git a/src/jcgp/modules/mutator/PointMutator.java b/src/jcgp/modules/mutator/PointMutator.java index e9be153..2298368 100644 --- a/src/jcgp/modules/mutator/PointMutator.java +++ b/src/jcgp/modules/mutator/PointMutator.java @@ -2,6 +2,7 @@ package jcgp.modules.mutator; import java.util.HashMap; +import jcgp.modules.ModuleStatus; import jcgp.parameters.DoubleParameter; import jcgp.parameters.Parameter; import jcgp.JCGP.Resources; @@ -12,9 +13,11 @@ import jcgp.population.Output; public class PointMutator implements Mutator { - private Parameter mutationRate; + private DoubleParameter mutationRate; private HashMap<String, Parameter> localParameters; + private ModuleStatus status = ModuleStatus.READY; + public PointMutator() { mutationRate = new DoubleParameter(0.5, "Percent mutation"); @@ -24,29 +27,24 @@ public class PointMutator implements Mutator { @Override public void mutate(Chromosome chromosome, Resources resources) { - int mutations = (int) Math.ceil((((double) mutationRate.getValue()) * (((((Integer) resources.get("nodes")).doubleValue() + ((Integer) resources.get("outputs")).doubleValue())) / (double) 100))); + int mutations = (int) Math.ceil(((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("outputs")))) / (double) 100))); for (int i = 0; i < mutations; i++) { MutableElement m = chromosome.getRandomMutableElement(); if (m instanceof Output) { m.setConnection(0, chromosome.getRandomConnection()); } else if (m instanceof Node) { - int geneType = resources.getRandomInt(1 + ((int) resources.get("arity"))); + int geneType = resources.getRandomInt(1 + resources.getInt("arity")); if (geneType < 1) { ((Node) m).setFunction(resources.getRandomFunction()); } else { - m.setConnection(resources.getRandomInt((int) resources.get("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); + m.setConnection(resources.getRandomInt(resources.getInt("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); } } } } @Override - public void activate(Resources parameters) { - // nothing - } - - @Override public HashMap<String, Parameter> getLocalParameters() { return localParameters; } @@ -56,4 +54,18 @@ public class PointMutator implements Mutator { return "Point mutation"; } + @Override + public ModuleStatus getStatus(Resources resources) { + if (mutationRate.get() <= 0 || mutationRate.get() > 100) { + status = ModuleStatus.ERROR; + status.setDetails("Mutation rate must be > 0 and <= 100"); + } else if ((int) ((mutationRate.get() / 100) * resources.getDouble("nodes")) > 0) { + status = ModuleStatus.WARNING; + status.setDetails("With mutation rate " + mutationRate.get() + ", no mutations will occur."); + } else { + status = ModuleStatus.READY; + status.setDetails(""); + } + return status; + } } |