diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-03 15:29:24 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-03 15:29:24 +0100 |
commit | 0dbf126fc524bc029d9f5803d849b7c8f43fe389 (patch) | |
tree | 35d7c23b371614388cbb7a4bc247374759b95a33 /src/jcgp/backend | |
parent | 02fd2bc7059da416937beb1abe67e5ca60379030 (diff) |
Visual feedback for parameters implemented.
Diffstat (limited to 'src/jcgp/backend')
-rw-r--r-- | src/jcgp/backend/modules/Module.java | 6 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java | 3 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/MuPlusLambda.java | 57 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/TournamentSelection.java | 18 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCaseEvaluator.java | 6 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/PointMutator.java | 19 | ||||
-rw-r--r-- | src/jcgp/backend/parameters/BooleanParameter.java | 10 | ||||
-rw-r--r-- | src/jcgp/backend/parameters/DoubleParameter.java | 10 | ||||
-rw-r--r-- | src/jcgp/backend/parameters/IntegerParameter.java | 13 | ||||
-rw-r--r-- | src/jcgp/backend/parameters/Parameter.java | 16 | ||||
-rw-r--r-- | src/jcgp/backend/parameters/ParameterStatus.java | 16 | ||||
-rw-r--r-- | src/jcgp/backend/population/Chromosome.java | 10 | ||||
-rw-r--r-- | src/jcgp/backend/population/Population.java | 53 |
13 files changed, 147 insertions, 90 deletions
diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java index f9114de..82c3d80 100644 --- a/src/jcgp/backend/modules/Module.java +++ b/src/jcgp/backend/modules/Module.java @@ -1,11 +1,11 @@ package jcgp.backend.modules; -import java.util.HashMap; - import jcgp.JCGP.Resources; import jcgp.backend.parameters.Parameter; public interface Module { - public HashMap<String, Parameter> getLocalParameters(); + + public Parameter[] getLocalParameters(); + public ModuleStatus getStatus(Resources resources); } diff --git a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java index ce457ef..7719111 100644 --- a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java @@ -3,13 +3,12 @@ package jcgp.backend.modules.ea; import jcgp.JCGP.Resources; import jcgp.backend.modules.Module; import jcgp.backend.modules.mutator.Mutator; -import jcgp.backend.population.Chromosome; import jcgp.backend.population.Population; public interface EvolutionaryAlgorithm extends Module { public abstract void evolve(Population population, Mutator mutator, Resources parameters); - public abstract Chromosome getFittestChromosome(); + public abstract int getFittestChromosome(); } diff --git a/src/jcgp/backend/modules/ea/MuPlusLambda.java b/src/jcgp/backend/modules/ea/MuPlusLambda.java index fcfba05..22a0876 100644 --- a/src/jcgp/backend/modules/ea/MuPlusLambda.java +++ b/src/jcgp/backend/modules/ea/MuPlusLambda.java @@ -1,13 +1,10 @@ package jcgp.backend.modules.ea; -import java.util.HashMap; - import jcgp.JCGP.Resources; import jcgp.backend.modules.ModuleStatus; import jcgp.backend.modules.mutator.Mutator; import jcgp.backend.parameters.IntegerParameter; import jcgp.backend.parameters.Parameter; -import jcgp.backend.population.Chromosome; import jcgp.backend.population.Population; /** @@ -19,52 +16,54 @@ import jcgp.backend.population.Population; */ public class MuPlusLambda implements EvolutionaryAlgorithm { - private Chromosome fittestChromosome; + private int fittestChromosome; private IntegerParameter parents, offspring; - - private HashMap<String, Parameter> localParameters; - - public MuPlusLambda() { - parents = new IntegerParameter(1, "Parents"); - offspring = new IntegerParameter(4, "Offspring"); - - localParameters = new HashMap<String, Parameter>(); - localParameters.put("mu", parents); - localParameters.put("lambda", offspring); + public MuPlusLambda() { + parents = new IntegerParameter(1, "Parents") { + @Override + protected void validate(int newValue) { + + } + }; + offspring = new IntegerParameter(4, "Offspring") { + @Override + protected void validate(int newValue) { + + } + }; } @Override - public void evolve(Population population, Mutator mutator, Resources parameters) { + public void evolve(Population population, Mutator mutator, Resources resources) { + // TODO actually use parents and offspring // select fittest chromosome - int fittest = 0; + fittestChromosome = 0; - for (int i = 1; i < parameters.getInt("popSize"); i++) { - if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) { - fittest = i; + for (int i = 1; i < resources.getInt("popSize"); i++) { + if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) { + fittestChromosome = 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 < parameters.getInt("popSize"); i++) { - if (i != fittest) { - population.getChromosome(i).copyConnections(fc); - mutator.mutate(population.getChromosome(i), parameters); + for (int i = 0; i < resources.getInt("popSize"); i++) { + if (i != fittestChromosome) { + population.copyChromosome(fittestChromosome, i); + mutator.mutate(population.getChromosome(i), resources); } } } @Override - public Chromosome getFittestChromosome() { + public int getFittestChromosome() { return fittestChromosome; } @Override - public HashMap<String, Parameter> getLocalParameters() { - return localParameters; + public Parameter[] getLocalParameters() { + return new Parameter[] {parents, offspring}; } @Override diff --git a/src/jcgp/backend/modules/ea/TournamentSelection.java b/src/jcgp/backend/modules/ea/TournamentSelection.java index 6cbaa45..3c4a539 100644 --- a/src/jcgp/backend/modules/ea/TournamentSelection.java +++ b/src/jcgp/backend/modules/ea/TournamentSelection.java @@ -7,39 +7,43 @@ import jcgp.backend.modules.ModuleStatus; import jcgp.backend.modules.mutator.Mutator; import jcgp.backend.parameters.IntegerParameter; import jcgp.backend.parameters.Parameter; -import jcgp.backend.population.Chromosome; import jcgp.backend.population.Population; public class TournamentSelection implements EvolutionaryAlgorithm { - private Chromosome fittestChromosome; + private int fittestChromosome; private IntegerParameter tournament; private HashMap<String, Parameter> localParameters; public TournamentSelection() { - tournament = new IntegerParameter(1, "Tournament size"); + tournament = new IntegerParameter(1, "Tournament size") { + @Override + protected void validate(int newValue) { + + } + }; localParameters = new HashMap<String, Parameter>(); localParameters.put("tournament", tournament); } @Override - public HashMap<String, Parameter> getLocalParameters() { - return localParameters; + public Parameter[] getLocalParameters() { + return new Parameter[] {tournament}; } @Override public void evolve(Population population, Mutator mutator, Resources parameters) { tournament.set(tournament.get() + 1); - fittestChromosome = population.getChromosome(0); + fittestChromosome = 0; // TODO implement this } @Override - public Chromosome getFittestChromosome() { + public int getFittestChromosome() { return fittestChromosome; } diff --git a/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java b/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java index 3b67f28..5ff6973 100644 --- a/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java +++ b/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java @@ -1,7 +1,5 @@ package jcgp.backend.modules.fitness; -import java.util.HashMap; - import jcgp.JCGP.Resources; import jcgp.backend.modules.ModuleStatus; import jcgp.backend.parameters.Parameter; @@ -10,7 +8,7 @@ import jcgp.backend.population.Population; public class TestCaseEvaluator implements FitnessFunction { @Override - public void evaluate(Population population, Resources resources) { + public void evaluate(Population population, Resources resources) { // for every chromosome in the population for (int i = 0; i < resources.getInt("popSize"); i++) { int fitness = 0; @@ -29,7 +27,7 @@ public class TestCaseEvaluator implements FitnessFunction { } @Override - public HashMap<String, Parameter> getLocalParameters() { + public Parameter[] getLocalParameters() { return null; } diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 62d827d..cdac8bb 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -1,7 +1,5 @@ package jcgp.backend.modules.mutator; -import java.util.HashMap; - import jcgp.backend.function.Function; import jcgp.backend.modules.ModuleStatus; import jcgp.backend.parameters.DoubleParameter; @@ -15,15 +13,16 @@ import jcgp.JCGP.Resources; public class PointMutator implements Mutator { private DoubleParameter mutationRate; - private HashMap<String, Parameter> localParameters; - + private ModuleStatus status = ModuleStatus.READY; public PointMutator() { - mutationRate = new DoubleParameter(0.5, "Percent mutation", false, false, false); - - localParameters = new HashMap<String, Parameter>(); - localParameters.put("mutRate", mutationRate); + mutationRate = new DoubleParameter(0.5, "Percent mutation", false, false) { + @Override + public void validate(double newValue) { + // TODO this + } + }; } @Override @@ -47,8 +46,8 @@ public class PointMutator implements Mutator { } @Override - public HashMap<String, Parameter> getLocalParameters() { - return localParameters; + public Parameter[] getLocalParameters() { + return new Parameter[] {mutationRate}; } @Override diff --git a/src/jcgp/backend/parameters/BooleanParameter.java b/src/jcgp/backend/parameters/BooleanParameter.java index d5904cd..db78419 100644 --- a/src/jcgp/backend/parameters/BooleanParameter.java +++ b/src/jcgp/backend/parameters/BooleanParameter.java @@ -2,17 +2,17 @@ package jcgp.backend.parameters; import javafx.beans.property.SimpleBooleanProperty; -public class BooleanParameter extends Parameter { +public abstract class BooleanParameter extends Parameter { private SimpleBooleanProperty value; - public BooleanParameter(boolean value, String name, boolean monitor, boolean hidden, boolean critical) { - super(name, monitor, hidden, critical); + public BooleanParameter(boolean value, String name, boolean monitor, boolean critical) { + super(name, monitor, critical); this.value = new SimpleBooleanProperty(value); } public BooleanParameter(boolean value, String name) { - super(name, false, false, true); + super(name, false, false); this.value = new SimpleBooleanProperty(value); } @@ -26,6 +26,8 @@ public class BooleanParameter extends Parameter { } } + protected abstract void validate(boolean newValue); + public SimpleBooleanProperty valueProperty() { return value; } diff --git a/src/jcgp/backend/parameters/DoubleParameter.java b/src/jcgp/backend/parameters/DoubleParameter.java index 26031be..2b98991 100644 --- a/src/jcgp/backend/parameters/DoubleParameter.java +++ b/src/jcgp/backend/parameters/DoubleParameter.java @@ -2,17 +2,17 @@ package jcgp.backend.parameters; import javafx.beans.property.SimpleDoubleProperty; -public class DoubleParameter extends Parameter { +public abstract class DoubleParameter extends Parameter { protected SimpleDoubleProperty value; - public DoubleParameter(double value, String name, boolean monitor, boolean hidden, boolean critical) { - super(name, monitor, hidden, critical); + public DoubleParameter(double value, String name, boolean monitor, boolean critical) { + super(name, monitor, critical); this.value = new SimpleDoubleProperty(value); } public DoubleParameter(double value, String name) { - super(name, false, false, true); + super(name, false, false); this.value = new SimpleDoubleProperty(value); } @@ -30,4 +30,6 @@ public class DoubleParameter extends Parameter { return value; } + protected abstract void validate(double newValue); + } diff --git a/src/jcgp/backend/parameters/IntegerParameter.java b/src/jcgp/backend/parameters/IntegerParameter.java index dbfa5c5..1127817 100644 --- a/src/jcgp/backend/parameters/IntegerParameter.java +++ b/src/jcgp/backend/parameters/IntegerParameter.java @@ -2,17 +2,17 @@ package jcgp.backend.parameters; import javafx.beans.property.SimpleIntegerProperty; -public class IntegerParameter extends Parameter { +public abstract class IntegerParameter extends Parameter { - protected SimpleIntegerProperty value; + private SimpleIntegerProperty value; - public IntegerParameter(int value, String name, boolean monitor, boolean hidden, boolean critical) { - super(name, monitor, hidden, critical); + public IntegerParameter(int value, String name, boolean monitor, boolean critical) { + super(name, monitor, critical); this.value = new SimpleIntegerProperty(value); } public IntegerParameter(int value, String name) { - super(name, false, false, true); + super(name, false, false); this.value = new SimpleIntegerProperty(value); } @@ -22,6 +22,7 @@ public class IntegerParameter extends Parameter { public void set(int newValue) { if (!value.isBound()) { + validate(newValue); value.set(newValue); } } @@ -30,4 +31,6 @@ public class IntegerParameter extends Parameter { return value; } + protected abstract void validate(int newValue); + } diff --git a/src/jcgp/backend/parameters/Parameter.java b/src/jcgp/backend/parameters/Parameter.java index 2f584a4..26bc8f1 100644 --- a/src/jcgp/backend/parameters/Parameter.java +++ b/src/jcgp/backend/parameters/Parameter.java @@ -4,21 +4,18 @@ import javafx.beans.property.Property; public abstract class Parameter { - protected boolean hidden, monitor, critical; + protected boolean monitor, critical; + + protected ParameterStatus status = ParameterStatus.VALID; protected String name; - public Parameter(String name, boolean monitor, boolean hidden, boolean critical) { + public Parameter(String name, boolean monitor, boolean critical) { this.name = name; - this.hidden = hidden; this.monitor = monitor; this.critical = critical; } - public boolean isHidden() { - return hidden; - } - public boolean isMonitor() { return monitor; } @@ -31,5 +28,10 @@ public abstract class Parameter { return name; } + public ParameterStatus getStatus() { + return status; + } + public abstract Property<?> valueProperty(); + } diff --git a/src/jcgp/backend/parameters/ParameterStatus.java b/src/jcgp/backend/parameters/ParameterStatus.java new file mode 100644 index 0000000..ed235d4 --- /dev/null +++ b/src/jcgp/backend/parameters/ParameterStatus.java @@ -0,0 +1,16 @@ +package jcgp.backend.parameters; + +public enum ParameterStatus { + INVALID, WARNING, VALID; + + private String details; + + public void setDetails(String details) { + this.details = details; + } + + public String getDetails() { + return details; + } + +} diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java index 18ae9bb..41ba06e 100644 --- a/src/jcgp/backend/population/Chromosome.java +++ b/src/jcgp/backend/population/Chromosome.java @@ -43,12 +43,12 @@ public class Chromosome { */ public Chromosome(Chromosome clone) { // store a reference to the parameters - this.resources = clone.getParameters(); + this.resources = clone.getResources(); // allocate memory for all elements of the chromosome instantiateElements(); // initialise all connections based on argument - copyConnections(clone); + copyGenes(clone); } /** @@ -102,7 +102,7 @@ public class Chromosome { /** * @param clone */ - public void copyConnections(Chromosome clone) { + public void copyGenes(Chromosome clone) { int arity = resources.getInt("arity"); // copy nodes - [rows][columns] @@ -119,7 +119,7 @@ public class Chromosome { } else if (copyConnection instanceof Node) { connections[i] = nodes[((Node) copyConnection).getRow()][((Node) copyConnection).getColumn()]; } else { - System.out.println("Warning: Connection of subtype " + copyConnection.getClass().toString() + " is not explicitly handled by copy constructor."); + System.out.println("Error: Connection of subtype " + copyConnection.getClass().toString() + " is not explicitly handled by copy method."); } } // initialise with copied arguments @@ -329,7 +329,7 @@ public class Chromosome { } } - public Resources getParameters() { + public Resources getResources() { return resources; } } diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java index 7049d79..7b62d27 100644 --- a/src/jcgp/backend/population/Population.java +++ b/src/jcgp/backend/population/Population.java @@ -6,27 +6,41 @@ import jcgp.JCGP.Resources; public class Population { private Chromosome[] chromosomes; - private int fittest; + private Resources resources; + /** + * Initialise a random population according to the parameters specified + * in the resources. + * + * @param resources the CGP resources + */ public Population(Resources resources) { + this.resources = resources; + chromosomes = new Chromosome[(resources.getInt("popSize"))]; for (int c = 0; c < chromosomes.length; c++) { chromosomes[c] = new Chromosome(resources); } } + /** + * Initialise a population of copies of the given chromosome. + * + * @param parent + * @param resources + */ public Population(Chromosome parent, Resources resources) { + this.resources = resources; + chromosomes = new Chromosome[(resources.getInt("popSize"))]; - // make a clone for safety - this.chromosomes[0] = new Chromosome(parent); // generate the rest of the individuals - for (int c = 1; c < chromosomes.length; c++) { - chromosomes[c] = new Chromosome(chromosomes[0]); + for (int c = 0; c < chromosomes.length; c++) { + chromosomes[c] = new Chromosome(parent); } } /** - * Returns all chromosomes, parents first, then offspring. + * Returns the indexed chromosome. * * @param index * @return @@ -35,11 +49,30 @@ public class Population { return chromosomes[index]; } - public void setBestIndividual(int index) { - fittest = index; + /** + * @return a random chromosome from this population. + */ + public Chromosome getRandomChromosome() { + return chromosomes[resources.getRandomInt(chromosomes.length)]; } + - public Chromosome getBestIndividual() { - return chromosomes[fittest]; + /** + * Copy a chromosome into a different position. + * After this returns, the target chromosome has + * identical connections and functions to the source + * one, though they are separate instances. + * + * This method does nothing if source == target. + * + * @param source + * @param target + */ + public void copyChromosome(int source, int target) { + if (source != target) { + chromosomes[target].copyGenes(chromosomes[source]); + } } + + } |