diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-07 15:58:30 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-07 15:58:30 +0100 |
commit | 226365bd6a0f56143ffbce7379857ff3e126fbaf (patch) | |
tree | 4582b16a5519c94b121a369104157859dc97cba1 /src/jcgp/backend/modules | |
parent | 67ace66f66ffaa00e1bd1495c0d406c801e59c5c (diff) |
Refactoring resources for (hopefully) the last time
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/EvolutionaryStrategy.java (renamed from src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java) | 6 | ||||
-rw-r--r-- | src/jcgp/backend/modules/es/MuPlusLambda.java (renamed from src/jcgp/backend/modules/ea/MuPlusLambda.java) | 13 | ||||
-rw-r--r-- | src/jcgp/backend/modules/es/TournamentSelection.java (renamed from src/jcgp/backend/modules/ea/TournamentSelection.java) | 4 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/DigitalCircuit.java | 5 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/SymbolicRegression.java | 5 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCaseProblem.java | 4 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/Mutator.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/PointMutator.java | 32 |
9 files changed, 52 insertions, 21 deletions
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/es/EvolutionaryStrategy.java index 3aca104..1117e99 100644 --- a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java @@ -1,13 +1,13 @@ -package jcgp.backend.modules.ea; +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 EvolutionaryAlgorithm extends Module { +public interface EvolutionaryStrategy extends Module { - public abstract void evolve(Population population, Mutator mutator, Resources parameters); + public abstract void evolve(Population population, Mutator mutator, Resources resources); public abstract int getFittestChromosome(); diff --git a/src/jcgp/backend/modules/ea/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java index 0d16111..53fb932 100644 --- a/src/jcgp/backend/modules/ea/MuPlusLambda.java +++ b/src/jcgp/backend/modules/es/MuPlusLambda.java @@ -1,4 +1,4 @@ -package jcgp.backend.modules.ea; +package jcgp.backend.modules.es; import jcgp.backend.modules.mutator.Mutator; import jcgp.backend.population.Population; @@ -15,7 +15,7 @@ import jcgp.backend.resources.parameters.ParameterStatus; * @author Eduardo Pedroni * */ -public class MuPlusLambda implements EvolutionaryAlgorithm { +public class MuPlusLambda implements EvolutionaryStrategy { private int fittestChromosome; @@ -58,22 +58,27 @@ public class MuPlusLambda implements EvolutionaryAlgorithm { } }; } - + @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 (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); } } diff --git a/src/jcgp/backend/modules/ea/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java index baf6704..3954de8 100644 --- a/src/jcgp/backend/modules/ea/TournamentSelection.java +++ b/src/jcgp/backend/modules/es/TournamentSelection.java @@ -1,4 +1,4 @@ -package jcgp.backend.modules.ea; +package jcgp.backend.modules.es; import java.util.HashMap; @@ -8,7 +8,7 @@ import jcgp.backend.resources.Resources; import jcgp.backend.resources.parameters.IntegerParameter; import jcgp.backend.resources.parameters.Parameter; -public class TournamentSelection implements EvolutionaryAlgorithm { +public class TournamentSelection implements EvolutionaryStrategy { private int fittestChromosome; 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<Integer> { - 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<Integer> { - 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<U> extends Problem { private ArrayList<TestCase<U>> 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 |