diff options
Diffstat (limited to 'src/jcgp/backend/modules')
-rw-r--r-- | src/jcgp/backend/modules/Module.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/modules/es/MuPlusLambda.java | 22 | ||||
-rw-r--r-- | src/jcgp/backend/modules/es/TournamentSelection.java | 10 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCaseProblem.java | 8 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/PointMutator.java | 16 |
5 files changed, 27 insertions, 31 deletions
diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java index 8a95737..a6b4d73 100644 --- a/src/jcgp/backend/modules/Module.java +++ b/src/jcgp/backend/modules/Module.java @@ -4,6 +4,6 @@ import jcgp.backend.resources.parameters.Parameter; public interface Module { - public abstract Parameter[] getLocalParameters(); + public abstract Parameter<?>[] getLocalParameters(); } diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java index 53fb932..67236f9 100644 --- a/src/jcgp/backend/modules/es/MuPlusLambda.java +++ b/src/jcgp/backend/modules/es/MuPlusLambda.java @@ -25,11 +25,11 @@ public class MuPlusLambda implements EvolutionaryStrategy { public MuPlusLambda(final Resources resources) { parents = new IntegerParameter(1, "Parents") { @Override - public void validate(int newValue) { - if (newValue + offspring.get() != resources.getInt("popSize")) { + public void validate(Number newValue) { + if (newValue.intValue() + offspring.get() != resources.populationSize()) { status = ParameterStatus.INVALID; status.setDetails("Parents + offspring must equal population size."); - } else if (newValue <= 0) { + } else if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("EA needs at least 1 parent."); } else { @@ -39,11 +39,11 @@ public class MuPlusLambda implements EvolutionaryStrategy { }; offspring = new IntegerParameter(4, "Offspring") { @Override - public void validate(int newValue) { - if (newValue + parents.get() != resources.getInt("popSize")) { + public void validate(Number newValue) { + if (newValue.intValue() + parents.get() != resources.populationSize()) { status = ParameterStatus.INVALID; status.setDetails("Parents + offspring must equal population size."); - } else if (newValue <= 0) { + } else if (newValue.intValue() <= 0) { status = ParameterStatus.INVALID; status.setDetails("EA needs at least 1 offspring."); } else { @@ -53,7 +53,7 @@ public class MuPlusLambda implements EvolutionaryStrategy { }; report = new BooleanParameter(false, "Report") { @Override - public void validate(boolean newValue) { + public void validate(Boolean newValue) { // nothing } }; @@ -65,7 +65,7 @@ public class MuPlusLambda implements EvolutionaryStrategy { fittestChromosome = 0; if (report.get()) resources.reportln("[ES] Selecting fittest chromosome..."); - for (int i = 0; i < resources.getInt("popSize"); i++) { + for (int i = 0; i < resources.populationSize(); i++) { if (report.get()) resources.reportln("[ES] Chromosome " + i + ", fitness: " + population.getChromosome(i).getFitness()); if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) { fittestChromosome = i; @@ -74,7 +74,7 @@ public class MuPlusLambda implements EvolutionaryStrategy { if (report.get()) resources.reportln("[ES] Selected chromosome: " + fittestChromosome); // create copies of fittest chromosome, mutate them - for (int i = 0; i < resources.getInt("popSize"); i++) { + for (int i = 0; i < resources.populationSize(); i++) { if (i != fittestChromosome) { if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i); population.copyChromosome(fittestChromosome, i); @@ -83,6 +83,8 @@ public class MuPlusLambda implements EvolutionaryStrategy { } } + if (report.get()) resources.reportln("[ES] Generation is complete."); + } @Override @@ -91,7 +93,7 @@ public class MuPlusLambda implements EvolutionaryStrategy { } @Override - public Parameter[] getLocalParameters() { + public Parameter<?>[] getLocalParameters() { return new Parameter[] {parents, offspring, report}; } diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java index 3954de8..8286101 100644 --- a/src/jcgp/backend/modules/es/TournamentSelection.java +++ b/src/jcgp/backend/modules/es/TournamentSelection.java @@ -1,7 +1,5 @@ package jcgp.backend.modules.es; -import java.util.HashMap; - import jcgp.backend.modules.mutator.Mutator; import jcgp.backend.population.Population; import jcgp.backend.resources.Resources; @@ -13,22 +11,18 @@ public class TournamentSelection implements EvolutionaryStrategy { private int fittestChromosome; private IntegerParameter tournament; - private HashMap<String, Parameter> localParameters; public TournamentSelection() { tournament = new IntegerParameter(1, "Tournament size") { @Override - public void validate(int newValue) { + public void validate(Number newValue) { // TODO this } }; - - localParameters = new HashMap<String, Parameter>(); - localParameters.put("tournament", tournament); } @Override - public Parameter[] getLocalParameters() { + public Parameter<?>[] getLocalParameters() { return new Parameter[] {tournament}; } diff --git a/src/jcgp/backend/modules/fitness/TestCaseProblem.java b/src/jcgp/backend/modules/fitness/TestCaseProblem.java index 4259285..7dd24af 100644 --- a/src/jcgp/backend/modules/fitness/TestCaseProblem.java +++ b/src/jcgp/backend/modules/fitness/TestCaseProblem.java @@ -56,7 +56,7 @@ public abstract class TestCaseProblem<U> extends Problem { maxFitness = new IntegerParameter(0, "Max fitness", true, false) { @Override - public void validate(int newValue) { + public void validate(Number newValue) { // blank } }; @@ -67,14 +67,14 @@ public abstract class TestCaseProblem<U> extends Problem { @Override public void evaluate(Population population, Resources resources) { // for every chromosome in the population - for (int i = 0; i < resources.getInt("popSize"); i++) { + for (int i = 0; i < resources.populationSize(); i++) { // assume an initial fitness of 0 int fitness = 0; // for each test case for (int t = 0; t < testCases.size(); t++) { population.getChromosome(i).setInputs(testCases.get(t).getInputs()); // check each output - for (int o = 0; o < resources.getInt("outputs"); o++) { + for (int o = 0; o < resources.outputs(); o++) { if (population.getChromosome(i).getOutput(o).calculate() == testCases.get(t).getOutput(o)) { fitness++; } @@ -86,7 +86,7 @@ public abstract class TestCaseProblem<U> extends Problem { } @Override - public Parameter[] getLocalParameters() { + public Parameter<?>[] getLocalParameters() { return new Parameter[]{maxFitness}; } diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 1f38cfe..ab8efad 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -18,11 +18,11 @@ public class PointMutator implements Mutator { public PointMutator(final Resources resources) { mutationRate = new DoubleParameter(50, "Percent mutation", false, false) { @Override - public void validate(double newValue) { - if (newValue <= 0 || newValue > 100) { + public void validate(Number newValue) { + if (newValue.doubleValue() <= 0 || newValue.doubleValue() > 100) { status = ParameterStatus.INVALID; status.setDetails("Mutation rate must be > 0 and <= 100"); - } else if ((int) ((newValue / 100) * resources.getDouble("nodes")) <= 0) { + } else if ((int) ((newValue.doubleValue() / 100) * (double) resources.nodes()) <= 0) { status = ParameterStatus.WARNING; status.setDetails("With mutation rate " + mutationRate.get() + ", 0 genes will be mutated."); } else { @@ -33,7 +33,7 @@ public class PointMutator implements Mutator { report = new BooleanParameter(false, "Report") { @Override - public void validate(boolean newValue) { + public void validate(Boolean newValue) { // blank } }; @@ -41,7 +41,7 @@ public class PointMutator implements Mutator { @Override public void mutate(Chromosome chromosome, Resources resources) { - int mutations = (int) ((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("outputs")))) / 100)); + int mutations = (int) ((mutationRate.get()) * (((((double) resources.nodes() + resources.outputs()))) / 100)); if (report.get()) resources.reportln("[Mutator] Number of mutations to be performed: " + mutations); for (int i = 0; i < mutations; i++) { MutableElement m = chromosome.getRandomMutableElement(); @@ -54,7 +54,7 @@ public class PointMutator implements Mutator { if (report.get()) resources.reportln("to " + ((Output) m).getSource().toString()); } else if (m instanceof Node) { - int geneType = resources.getRandomInt(1 + resources.getInt("arity")); + int geneType = resources.getRandomInt(1 + resources.arity()); if (geneType < 1) { if (report.get()) resources.report("changed function from " + ((Node) m).getFunction().getName() + " "); @@ -62,7 +62,7 @@ public class PointMutator implements Mutator { if (report.get()) resources.reportln("to " + ((Node) m).getFunction().getName()); } else { - int connection = resources.getRandomInt(resources.getInt("arity")); + int connection = resources.getRandomInt(resources.arity()); if (report.get()) resources.report("changed connection " + connection + " from " + ((Node) m).getConnection(connection) + " "); m.setConnection(connection, chromosome.getRandomConnection(((Node) m).getColumn())); @@ -74,7 +74,7 @@ public class PointMutator implements Mutator { } @Override - public Parameter[] getLocalParameters() { + public Parameter<?>[] getLocalParameters() { return new Parameter[] {mutationRate, report}; } |