diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-01 23:00:53 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-01 23:00:53 +0100 |
commit | 02fd2bc7059da416937beb1abe67e5ca60379030 (patch) | |
tree | 609341fe10aaa0f2dc45a1e72eba20bd24fb1281 /src/jcgp/backend/modules | |
parent | a757deacded0d7357a9f68462d3f2051e16004ee (diff) |
Settings pane now actually controls the parameters, not much left to do.
Diffstat (limited to 'src/jcgp/backend/modules')
-rw-r--r-- | src/jcgp/backend/modules/Module.java | 11 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ModuleStatus.java | 16 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java | 15 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/MuPlusLambda.java | 80 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/TournamentSelection.java | 55 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/FitnessFunction.java | 11 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCase.java | 29 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCaseEvaluator.java | 45 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/Mutator.java | 11 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/PointMutator.java | 73 |
10 files changed, 346 insertions, 0 deletions
diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java new file mode 100644 index 0000000..f9114de --- /dev/null +++ b/src/jcgp/backend/modules/Module.java @@ -0,0 +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 ModuleStatus getStatus(Resources resources); +} diff --git a/src/jcgp/backend/modules/ModuleStatus.java b/src/jcgp/backend/modules/ModuleStatus.java new file mode 100644 index 0000000..fec8490 --- /dev/null +++ b/src/jcgp/backend/modules/ModuleStatus.java @@ -0,0 +1,16 @@ +package jcgp.backend.modules; + +public enum ModuleStatus { + ERROR, WARNING, READY; + + private String details; + + public void setDetails(String details) { + this.details = details; + } + + public String getDetails() { + return details; + } + +} diff --git a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java new file mode 100644 index 0000000..ce457ef --- /dev/null +++ b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java @@ -0,0 +1,15 @@ +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(); + +} diff --git a/src/jcgp/backend/modules/ea/MuPlusLambda.java b/src/jcgp/backend/modules/ea/MuPlusLambda.java new file mode 100644 index 0000000..fcfba05 --- /dev/null +++ b/src/jcgp/backend/modules/ea/MuPlusLambda.java @@ -0,0 +1,80 @@ +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; + +/** + * (μ + λ) EA. + * + * + * @author Eduardo Pedroni + * + */ +public class MuPlusLambda implements EvolutionaryAlgorithm { + + private Chromosome 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); + } + + @Override + public void evolve(Population population, Mutator mutator, Resources parameters) { + // select fittest chromosome + int fittest = 0; + + for (int i = 1; i < parameters.getInt("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 < parameters.getInt("popSize"); i++) { + if (i != fittest) { + population.getChromosome(i).copyConnections(fc); + mutator.mutate(population.getChromosome(i), parameters); + } + } + } + + @Override + public Chromosome getFittestChromosome() { + return fittestChromosome; + } + + @Override + public HashMap<String, Parameter> getLocalParameters() { + return localParameters; + } + + @Override + public String toString() { + return "(μ + λ)"; + } + + @Override + public ModuleStatus getStatus(Resources resources) { + return ModuleStatus.READY; + } + +} diff --git a/src/jcgp/backend/modules/ea/TournamentSelection.java b/src/jcgp/backend/modules/ea/TournamentSelection.java new file mode 100644 index 0000000..6cbaa45 --- /dev/null +++ b/src/jcgp/backend/modules/ea/TournamentSelection.java @@ -0,0 +1,55 @@ +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; + +public class TournamentSelection implements EvolutionaryAlgorithm { + + private Chromosome fittestChromosome; + + private IntegerParameter tournament; + private HashMap<String, Parameter> localParameters; + + public TournamentSelection() { + tournament = new IntegerParameter(1, "Tournament size"); + + localParameters = new HashMap<String, Parameter>(); + localParameters.put("tournament", tournament); + } + + @Override + public HashMap<String, Parameter> getLocalParameters() { + return localParameters; + } + + @Override + public void evolve(Population population, Mutator mutator, + Resources parameters) { + tournament.set(tournament.get() + 1); + fittestChromosome = population.getChromosome(0); + // TODO implement this + + } + + @Override + public Chromosome getFittestChromosome() { + return fittestChromosome; + } + + @Override + public String toString() { + return "Tournament"; + } + + @Override + public ModuleStatus getStatus(Resources resources) { + return null; + } +} diff --git a/src/jcgp/backend/modules/fitness/FitnessFunction.java b/src/jcgp/backend/modules/fitness/FitnessFunction.java new file mode 100644 index 0000000..ffce9b7 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/FitnessFunction.java @@ -0,0 +1,11 @@ +package jcgp.backend.modules.fitness; + +import jcgp.JCGP.Resources; +import jcgp.backend.modules.Module; +import jcgp.backend.population.Population; + +public interface FitnessFunction extends Module { + + public void evaluate(Population population, Resources resources); + +} diff --git a/src/jcgp/backend/modules/fitness/TestCase.java b/src/jcgp/backend/modules/fitness/TestCase.java new file mode 100644 index 0000000..23f7ecf --- /dev/null +++ b/src/jcgp/backend/modules/fitness/TestCase.java @@ -0,0 +1,29 @@ +package jcgp.backend.modules.fitness; + +public class TestCase { + + private Object[] inputs; + private Object[] outputs; + + public TestCase(Object[] inputs, Object[] outputs) { + this.inputs = inputs; + this.outputs = outputs; + } + + public Object getInput(int index) { + return inputs[index]; + } + + public Object getOutput(int index) { + return outputs[index]; + } + + public Object[] getInputs() { + return inputs; + } + + public Object[] getOutputs() { + return outputs; + } + +} diff --git a/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java b/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java new file mode 100644 index 0000000..3b67f28 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java @@ -0,0 +1,45 @@ +package jcgp.backend.modules.fitness; + +import java.util.HashMap; + +import jcgp.JCGP.Resources; +import jcgp.backend.modules.ModuleStatus; +import jcgp.backend.parameters.Parameter; +import jcgp.backend.population.Population; + +public class TestCaseEvaluator implements FitnessFunction { + + @Override + 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; + // 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 < resources.getInt("outputs"); o++) { + if (population.getChromosome(i).getOutput(o).calculate() == resources.getTestCase(t).getOutput(o)) { + fitness++; + } + } + } + population.getChromosome(i).setFitness(fitness); + } + } + + @Override + public HashMap<String, Parameter> getLocalParameters() { + return null; + } + + @Override + public String toString() { + return "Test case"; + } + + @Override + public ModuleStatus getStatus(Resources resources) { + return null; + } +} diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java new file mode 100644 index 0000000..fd7cd27 --- /dev/null +++ b/src/jcgp/backend/modules/mutator/Mutator.java @@ -0,0 +1,11 @@ +package jcgp.backend.modules.mutator; + +import jcgp.JCGP.Resources; +import jcgp.backend.modules.Module; +import jcgp.backend.population.Chromosome; + +public interface Mutator extends Module { + + void mutate(Chromosome chromosome, Resources parameters); + +} diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java new file mode 100644 index 0000000..62d827d --- /dev/null +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -0,0 +1,73 @@ +package jcgp.backend.modules.mutator; + +import java.util.HashMap; + +import jcgp.backend.function.Function; +import jcgp.backend.modules.ModuleStatus; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.backend.parameters.Parameter; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.MutableElement; +import jcgp.backend.population.Node; +import jcgp.backend.population.Output; +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); + } + + @Override + public void mutate(Chromosome chromosome, Resources resources) { + 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 + resources.getInt("arity")); + if (geneType < 1) { + Function f = resources.getRandomFunction(); + ((Node) m).setFunction(f); + } else { + m.setConnection(resources.getRandomInt(resources.getInt("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); + } + } + } + } + + @Override + public HashMap<String, Parameter> getLocalParameters() { + return localParameters; + } + + @Override + public String toString() { + 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; + } +} |