From 226365bd6a0f56143ffbce7379857ff3e126fbaf Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 7 Apr 2014 15:58:30 +0100 Subject: Refactoring resources for (hopefully) the last time --- src/jcgp/backend/modules/Module.java | 2 +- .../backend/modules/ea/EvolutionaryAlgorithm.java | 14 --- src/jcgp/backend/modules/ea/MuPlusLambda.java | 98 -------------------- .../backend/modules/ea/TournamentSelection.java | 53 ----------- .../backend/modules/es/EvolutionaryStrategy.java | 14 +++ src/jcgp/backend/modules/es/MuPlusLambda.java | 103 +++++++++++++++++++++ .../backend/modules/es/TournamentSelection.java | 53 +++++++++++ .../backend/modules/fitness/DigitalCircuit.java | 5 +- .../modules/fitness/SymbolicRegression.java | 5 +- .../backend/modules/fitness/TestCaseProblem.java | 4 +- src/jcgp/backend/modules/mutator/Mutator.java | 2 +- src/jcgp/backend/modules/mutator/PointMutator.java | 32 ++++++- 12 files changed, 208 insertions(+), 177 deletions(-) delete mode 100644 src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java delete mode 100644 src/jcgp/backend/modules/ea/MuPlusLambda.java delete mode 100644 src/jcgp/backend/modules/ea/TournamentSelection.java create mode 100644 src/jcgp/backend/modules/es/EvolutionaryStrategy.java create mode 100644 src/jcgp/backend/modules/es/MuPlusLambda.java create mode 100644 src/jcgp/backend/modules/es/TournamentSelection.java (limited to 'src/jcgp/backend/modules') diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java index f2b91b0..8a95737 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 Parameter[] getLocalParameters(); + public abstract Parameter[] getLocalParameters(); } diff --git a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java deleted file mode 100644 index 3aca104..0000000 --- a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java +++ /dev/null @@ -1,14 +0,0 @@ -package jcgp.backend.modules.ea; - -import jcgp.backend.modules.Module; -import jcgp.backend.modules.mutator.Mutator; -import jcgp.backend.population.Population; -import jcgp.backend.resources.Resources; - -public interface EvolutionaryAlgorithm extends Module { - - public abstract void evolve(Population population, Mutator mutator, Resources parameters); - - public abstract int getFittestChromosome(); - -} diff --git a/src/jcgp/backend/modules/ea/MuPlusLambda.java b/src/jcgp/backend/modules/ea/MuPlusLambda.java deleted file mode 100644 index 0d16111..0000000 --- a/src/jcgp/backend/modules/ea/MuPlusLambda.java +++ /dev/null @@ -1,98 +0,0 @@ -package jcgp.backend.modules.ea; - -import jcgp.backend.modules.mutator.Mutator; -import jcgp.backend.population.Population; -import jcgp.backend.resources.Resources; -import jcgp.backend.resources.parameters.BooleanParameter; -import jcgp.backend.resources.parameters.IntegerParameter; -import jcgp.backend.resources.parameters.Parameter; -import jcgp.backend.resources.parameters.ParameterStatus; - -/** - * (μ + λ) EA. - * - * - * @author Eduardo Pedroni - * - */ -public class MuPlusLambda implements EvolutionaryAlgorithm { - - private int fittestChromosome; - - private IntegerParameter parents, offspring; - private BooleanParameter report; - - public MuPlusLambda(final Resources resources) { - parents = new IntegerParameter(1, "Parents") { - @Override - public void validate(int newValue) { - if (newValue + offspring.get() != resources.getInt("popSize")) { - status = ParameterStatus.INVALID; - status.setDetails("Parents + offspring must equal population size."); - } else if (newValue <= 0) { - status = ParameterStatus.INVALID; - status.setDetails("EA needs at least 1 parent."); - } else { - status = ParameterStatus.VALID; - } - } - }; - offspring = new IntegerParameter(4, "Offspring") { - @Override - public void validate(int newValue) { - if (newValue + parents.get() != resources.getInt("popSize")) { - status = ParameterStatus.INVALID; - status.setDetails("Parents + offspring must equal population size."); - } else if (newValue <= 0) { - status = ParameterStatus.INVALID; - status.setDetails("EA needs at least 1 offspring."); - } else { - status = ParameterStatus.VALID; - } - } - }; - report = new BooleanParameter(false, "Report") { - @Override - public void validate(boolean newValue) { - // nothing - } - }; - } - - @Override - public void evolve(Population population, Mutator mutator, Resources resources) { - // select fittest chromosomes - fittestChromosome = 0; - - for (int i = 1; i < resources.getInt("popSize"); i++) { - if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) { - fittestChromosome = i; - } - } - - // create copies of fittest chromosome, mutate them - for (int i = 0; i < resources.getInt("popSize"); i++) { - if (i != fittestChromosome) { - population.copyChromosome(fittestChromosome, i); - mutator.mutate(population.getChromosome(i), resources); - } - } - - } - - @Override - public int getFittestChromosome() { - return fittestChromosome; - } - - @Override - public Parameter[] getLocalParameters() { - return new Parameter[] {parents, offspring, report}; - } - - @Override - public String toString() { - return "(μ + λ)"; - } - -} diff --git a/src/jcgp/backend/modules/ea/TournamentSelection.java b/src/jcgp/backend/modules/ea/TournamentSelection.java deleted file mode 100644 index baf6704..0000000 --- a/src/jcgp/backend/modules/ea/TournamentSelection.java +++ /dev/null @@ -1,53 +0,0 @@ -package jcgp.backend.modules.ea; - -import java.util.HashMap; - -import jcgp.backend.modules.mutator.Mutator; -import jcgp.backend.population.Population; -import jcgp.backend.resources.Resources; -import jcgp.backend.resources.parameters.IntegerParameter; -import jcgp.backend.resources.parameters.Parameter; - -public class TournamentSelection implements EvolutionaryAlgorithm { - - private int fittestChromosome; - - private IntegerParameter tournament; - private HashMap localParameters; - - public TournamentSelection() { - tournament = new IntegerParameter(1, "Tournament size") { - @Override - public void validate(int newValue) { - // TODO this - } - }; - - localParameters = new HashMap(); - localParameters.put("tournament", tournament); - } - - @Override - public Parameter[] getLocalParameters() { - return new Parameter[] {tournament}; - } - - @Override - public void evolve(Population population, Mutator mutator, - Resources parameters) { - tournament.set(tournament.get() + 1); - fittestChromosome = 0; - // TODO implement this - - } - - @Override - public int getFittestChromosome() { - return fittestChromosome; - } - - @Override - public String toString() { - return "Tournament"; - } -} diff --git a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java new file mode 100644 index 0000000..1117e99 --- /dev/null +++ b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java @@ -0,0 +1,14 @@ +package jcgp.backend.modules.es; + +import jcgp.backend.modules.Module; +import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.population.Population; +import jcgp.backend.resources.Resources; + +public interface EvolutionaryStrategy extends Module { + + public abstract void evolve(Population population, Mutator mutator, Resources resources); + + public abstract int getFittestChromosome(); + +} diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java new file mode 100644 index 0000000..53fb932 --- /dev/null +++ b/src/jcgp/backend/modules/es/MuPlusLambda.java @@ -0,0 +1,103 @@ +package jcgp.backend.modules.es; + +import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.population.Population; +import jcgp.backend.resources.Resources; +import jcgp.backend.resources.parameters.BooleanParameter; +import jcgp.backend.resources.parameters.IntegerParameter; +import jcgp.backend.resources.parameters.Parameter; +import jcgp.backend.resources.parameters.ParameterStatus; + +/** + * (μ + λ) EA. + * + * + * @author Eduardo Pedroni + * + */ +public class MuPlusLambda implements EvolutionaryStrategy { + + private int fittestChromosome; + + private IntegerParameter parents, offspring; + private BooleanParameter report; + + public MuPlusLambda(final Resources resources) { + parents = new IntegerParameter(1, "Parents") { + @Override + public void validate(int newValue) { + if (newValue + offspring.get() != resources.getInt("popSize")) { + status = ParameterStatus.INVALID; + status.setDetails("Parents + offspring must equal population size."); + } else if (newValue <= 0) { + status = ParameterStatus.INVALID; + status.setDetails("EA needs at least 1 parent."); + } else { + status = ParameterStatus.VALID; + } + } + }; + offspring = new IntegerParameter(4, "Offspring") { + @Override + public void validate(int newValue) { + if (newValue + parents.get() != resources.getInt("popSize")) { + status = ParameterStatus.INVALID; + status.setDetails("Parents + offspring must equal population size."); + } else if (newValue <= 0) { + status = ParameterStatus.INVALID; + status.setDetails("EA needs at least 1 offspring."); + } else { + status = ParameterStatus.VALID; + } + } + }; + report = new BooleanParameter(false, "Report") { + @Override + public void validate(boolean newValue) { + // nothing + } + }; + } + + @Override + public void evolve(Population population, Mutator mutator, Resources resources) { + // select fittest chromosomes + fittestChromosome = 0; + + if (report.get()) resources.reportln("[ES] Selecting fittest chromosome..."); + for (int i = 0; i < resources.getInt("popSize"); 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; + } + } + 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++) { + if (i != fittestChromosome) { + if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i); + population.copyChromosome(fittestChromosome, i); + if (report.get()) resources.reportln("[ES] Mutating copied chromosome"); + mutator.mutate(population.getChromosome(i), resources); + } + } + + } + + @Override + public int getFittestChromosome() { + return fittestChromosome; + } + + @Override + public Parameter[] getLocalParameters() { + return new Parameter[] {parents, offspring, report}; + } + + @Override + public String toString() { + return "(μ + λ)"; + } + +} diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java new file mode 100644 index 0000000..3954de8 --- /dev/null +++ b/src/jcgp/backend/modules/es/TournamentSelection.java @@ -0,0 +1,53 @@ +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; +import jcgp.backend.resources.parameters.IntegerParameter; +import jcgp.backend.resources.parameters.Parameter; + +public class TournamentSelection implements EvolutionaryStrategy { + + private int fittestChromosome; + + private IntegerParameter tournament; + private HashMap localParameters; + + public TournamentSelection() { + tournament = new IntegerParameter(1, "Tournament size") { + @Override + public void validate(int newValue) { + // TODO this + } + }; + + localParameters = new HashMap(); + localParameters.put("tournament", tournament); + } + + @Override + public Parameter[] getLocalParameters() { + return new Parameter[] {tournament}; + } + + @Override + public void evolve(Population population, Mutator mutator, + Resources parameters) { + tournament.set(tournament.get() + 1); + fittestChromosome = 0; + // TODO implement this + + } + + @Override + public int getFittestChromosome() { + return fittestChromosome; + } + + @Override + public String toString() { + return "Tournament"; + } +} diff --git a/src/jcgp/backend/modules/fitness/DigitalCircuit.java b/src/jcgp/backend/modules/fitness/DigitalCircuit.java index b01bdc5..8677d5f 100644 --- a/src/jcgp/backend/modules/fitness/DigitalCircuit.java +++ b/src/jcgp/backend/modules/fitness/DigitalCircuit.java @@ -1,11 +1,12 @@ package jcgp.backend.modules.fitness; import jcgp.backend.function.BitwiseLogic; +import jcgp.backend.resources.Resources; public class DigitalCircuit extends TestCaseProblem { - public DigitalCircuit() { - super(); + public DigitalCircuit(Resources resources) { + super(resources); functionSet = new BitwiseLogic(); } diff --git a/src/jcgp/backend/modules/fitness/SymbolicRegression.java b/src/jcgp/backend/modules/fitness/SymbolicRegression.java index da2e69e..cb9d1a7 100644 --- a/src/jcgp/backend/modules/fitness/SymbolicRegression.java +++ b/src/jcgp/backend/modules/fitness/SymbolicRegression.java @@ -1,11 +1,12 @@ package jcgp.backend.modules.fitness; import jcgp.backend.function.IntegerArithmetic; +import jcgp.backend.resources.Resources; public class SymbolicRegression extends TestCaseProblem { - public SymbolicRegression() { - super(); + public SymbolicRegression(Resources resources) { + super(resources); functionSet = new IntegerArithmetic(); } diff --git a/src/jcgp/backend/modules/fitness/TestCaseProblem.java b/src/jcgp/backend/modules/fitness/TestCaseProblem.java index 7753e26..4259285 100644 --- a/src/jcgp/backend/modules/fitness/TestCaseProblem.java +++ b/src/jcgp/backend/modules/fitness/TestCaseProblem.java @@ -51,7 +51,9 @@ public abstract class TestCaseProblem extends Problem { private ArrayList> testCases; private IntegerParameter maxFitness; - public TestCaseProblem() { + public TestCaseProblem(Resources resources) { + super(); + maxFitness = new IntegerParameter(0, "Max fitness", true, false) { @Override public void validate(int newValue) { diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java index ffdd35c..1d5b99a 100644 --- a/src/jcgp/backend/modules/mutator/Mutator.java +++ b/src/jcgp/backend/modules/mutator/Mutator.java @@ -6,6 +6,6 @@ import jcgp.backend.resources.Resources; public interface Mutator extends Module { - void mutate(Chromosome chromosome, Resources parameters); + void mutate(Chromosome chromosome, Resources resources); } diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 54d5f3d..1f38cfe 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -1,11 +1,11 @@ package jcgp.backend.modules.mutator; -import jcgp.backend.function.Function; import jcgp.backend.population.Chromosome; import jcgp.backend.population.MutableElement; import jcgp.backend.population.Node; import jcgp.backend.population.Output; import jcgp.backend.resources.Resources; +import jcgp.backend.resources.parameters.BooleanParameter; import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.Parameter; import jcgp.backend.resources.parameters.ParameterStatus; @@ -13,6 +13,7 @@ import jcgp.backend.resources.parameters.ParameterStatus; public class PointMutator implements Mutator { private DoubleParameter mutationRate; + private BooleanParameter report; public PointMutator(final Resources resources) { mutationRate = new DoubleParameter(50, "Percent mutation", false, false) { @@ -29,23 +30,44 @@ public class PointMutator implements Mutator { } } }; + + report = new BooleanParameter(false, "Report") { + @Override + public void validate(boolean newValue) { + // blank + } + }; } @Override public void mutate(Chromosome chromosome, Resources resources) { int mutations = (int) ((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("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(); + if (report.get()) resources.report("[Mutator] Mutation " + i + " selected " + m.toString() + ", "); if (m instanceof Output) { + if (report.get()) resources.report("changed source from " + ((Output) m).getSource().toString() + " "); + m.setConnection(0, chromosome.getRandomConnection()); + + if (report.get()) resources.reportln("to " + ((Output) m).getSource().toString()); } else if (m instanceof Node) { int geneType = resources.getRandomInt(1 + resources.getInt("arity")); if (geneType < 1) { - Function f = resources.getRandomFunction(); - ((Node) m).setFunction(f); + if (report.get()) resources.report("changed function from " + ((Node) m).getFunction().getName() + " "); + + ((Node) m).setFunction(resources.getRandomFunction()); + + if (report.get()) resources.reportln("to " + ((Node) m).getFunction().getName()); } else { - m.setConnection(resources.getRandomInt(resources.getInt("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); + int connection = resources.getRandomInt(resources.getInt("arity")); + if (report.get()) resources.report("changed connection " + connection + " from " + ((Node) m).getConnection(connection) + " "); + + m.setConnection(connection, chromosome.getRandomConnection(((Node) m).getColumn())); + + if (report.get()) resources.reportln("to " + ((Node) m).getConnection(connection)); } } } @@ -53,7 +75,7 @@ public class PointMutator implements Mutator { @Override public Parameter[] getLocalParameters() { - return new Parameter[] {mutationRate}; + return new Parameter[] {mutationRate, report}; } @Override -- cgit v1.2.3