diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-06 21:58:53 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-06 21:58:53 +0100 |
commit | e6dd7711c7dad5e000445208eb5845801f4ccffc (patch) | |
tree | 1454bd20a8dd7069b1283184c42f4def6d5f7e6f /src/jcgp/backend/modules | |
parent | c7969623b44f375e30fa3f15dcd7581609276a0f (diff) |
About to make big changes to the way fitness works, committing just in case
Diffstat (limited to 'src/jcgp/backend/modules')
-rw-r--r-- | src/jcgp/backend/modules/Module.java | 4 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ModuleStatus.java | 16 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/MuPlusLambda.java | 20 | ||||
-rw-r--r-- | src/jcgp/backend/modules/ea/TournamentSelection.java | 12 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/FitnessFunction.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCase.java | 29 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCaseEvaluator.java | 43 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java | 75 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/Mutator.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/PointMutator.java | 40 |
11 files changed, 105 insertions, 140 deletions
diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java index 82c3d80..f2b91b0 100644 --- a/src/jcgp/backend/modules/Module.java +++ b/src/jcgp/backend/modules/Module.java @@ -1,11 +1,9 @@ package jcgp.backend.modules; -import jcgp.JCGP.Resources; -import jcgp.backend.parameters.Parameter; +import jcgp.backend.resources.parameters.Parameter; public interface Module { public Parameter[] getLocalParameters(); - public ModuleStatus getStatus(Resources resources); } diff --git a/src/jcgp/backend/modules/ModuleStatus.java b/src/jcgp/backend/modules/ModuleStatus.java deleted file mode 100644 index fec8490..0000000 --- a/src/jcgp/backend/modules/ModuleStatus.java +++ /dev/null @@ -1,16 +0,0 @@ -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 index 7719111..3aca104 100644 --- a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java @@ -1,9 +1,9 @@ package jcgp.backend.modules.ea; -import jcgp.JCGP.Resources; 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 { diff --git a/src/jcgp/backend/modules/ea/MuPlusLambda.java b/src/jcgp/backend/modules/ea/MuPlusLambda.java index ad8c5d8..0d16111 100644 --- a/src/jcgp/backend/modules/ea/MuPlusLambda.java +++ b/src/jcgp/backend/modules/ea/MuPlusLambda.java @@ -1,13 +1,12 @@ package jcgp.backend.modules.ea; -import jcgp.JCGP.Resources; -import jcgp.backend.modules.ModuleStatus; import jcgp.backend.modules.mutator.Mutator; -import jcgp.backend.parameters.BooleanParameter; -import jcgp.backend.parameters.IntegerParameter; -import jcgp.backend.parameters.Parameter; -import jcgp.backend.parameters.ParameterStatus; 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. @@ -62,8 +61,7 @@ public class MuPlusLambda implements EvolutionaryAlgorithm { @Override public void evolve(Population population, Mutator mutator, Resources resources) { - // TODO actually use parents and offspring - // select fittest chromosome + // select fittest chromosomes fittestChromosome = 0; for (int i = 1; i < resources.getInt("popSize"); i++) { @@ -79,6 +77,7 @@ public class MuPlusLambda implements EvolutionaryAlgorithm { mutator.mutate(population.getChromosome(i), resources); } } + } @Override @@ -95,10 +94,5 @@ public class MuPlusLambda implements EvolutionaryAlgorithm { 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 index 32ac54d..baf6704 100644 --- a/src/jcgp/backend/modules/ea/TournamentSelection.java +++ b/src/jcgp/backend/modules/ea/TournamentSelection.java @@ -2,12 +2,11 @@ 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.Population; +import jcgp.backend.resources.Resources; +import jcgp.backend.resources.parameters.IntegerParameter; +import jcgp.backend.resources.parameters.Parameter; public class TournamentSelection implements EvolutionaryAlgorithm { @@ -51,9 +50,4 @@ public class TournamentSelection implements EvolutionaryAlgorithm { 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 index ffce9b7..d3b76cb 100644 --- a/src/jcgp/backend/modules/fitness/FitnessFunction.java +++ b/src/jcgp/backend/modules/fitness/FitnessFunction.java @@ -1,8 +1,8 @@ package jcgp.backend.modules.fitness; -import jcgp.JCGP.Resources; import jcgp.backend.modules.Module; import jcgp.backend.population.Population; +import jcgp.backend.resources.Resources; public interface FitnessFunction extends Module { diff --git a/src/jcgp/backend/modules/fitness/TestCase.java b/src/jcgp/backend/modules/fitness/TestCase.java deleted file mode 100644 index 23f7ecf..0000000 --- a/src/jcgp/backend/modules/fitness/TestCase.java +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index 5ff6973..0000000 --- a/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java +++ /dev/null @@ -1,43 +0,0 @@ -package jcgp.backend.modules.fitness; - -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 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/fitness/testcase/TestCaseEvaluator.java b/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java new file mode 100644 index 0000000..51676e3 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java @@ -0,0 +1,75 @@ +package jcgp.backend.modules.fitness.testcase; + +import java.util.ArrayList; + +import jcgp.backend.modules.fitness.FitnessFunction; +import jcgp.backend.population.Population; +import jcgp.backend.resources.Resources; + +/** + * + * This fitness function module implements a simple test case evaluator. + * + * A TestCase object is a + * + * + * @author Eduardo Pedroni + * + */ +public abstract class TestCaseEvaluator implements FitnessFunction { + + public static class TestCase<T> { + + private T[] inputs; + private T[] outputs; + + public TestCase(T[] inputs, T[] outputs) { + this.inputs = inputs; + this.outputs = outputs; + } + + public T getInput(int index) { + return inputs[index]; + } + + public T getOutput(int index) { + return outputs[index]; + } + + public T[] getInputs() { + return inputs; + } + + public T[] getOutputs() { + return outputs; + } + } + + protected ArrayList<TestCase> testCases; + + @Override + public void evaluate(Population population, Resources resources) { + // for every chromosome in the population + for (int i = 0; i < resources.getInt("popSize"); 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++) { + if (population.getChromosome(i).getOutput(o).calculate() == testCases.get(t).getOutput(o)) { + fitness++; + } + } + } + // assign the resulting fitness to the respective individual + population.getChromosome(i).setFitness(fitness); + } + } + + public int getMaxFitness() { + return 0; + } +} + diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java index fd7cd27..ffdd35c 100644 --- a/src/jcgp/backend/modules/mutator/Mutator.java +++ b/src/jcgp/backend/modules/mutator/Mutator.java @@ -1,8 +1,8 @@ package jcgp.backend.modules.mutator; -import jcgp.JCGP.Resources; import jcgp.backend.modules.Module; import jcgp.backend.population.Chromosome; +import jcgp.backend.resources.Resources; public interface Mutator extends Module { diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index cdac8bb..54d5f3d 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -1,33 +1,39 @@ package jcgp.backend.modules.mutator; 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; +import jcgp.backend.resources.Resources; +import jcgp.backend.resources.parameters.DoubleParameter; +import jcgp.backend.resources.parameters.Parameter; +import jcgp.backend.resources.parameters.ParameterStatus; public class PointMutator implements Mutator { private DoubleParameter mutationRate; - private ModuleStatus status = ModuleStatus.READY; - - public PointMutator() { - mutationRate = new DoubleParameter(0.5, "Percent mutation", false, false) { + public PointMutator(final Resources resources) { + mutationRate = new DoubleParameter(50, "Percent mutation", false, false) { @Override public void validate(double newValue) { - // TODO this + if (newValue <= 0 || newValue > 100) { + status = ParameterStatus.INVALID; + status.setDetails("Mutation rate must be > 0 and <= 100"); + } else if ((int) ((newValue / 100) * resources.getDouble("nodes")) <= 0) { + status = ParameterStatus.WARNING; + status.setDetails("With mutation rate " + mutationRate.get() + ", 0 genes will be mutated."); + } else { + status = ParameterStatus.VALID; + } } }; } @Override public void mutate(Chromosome chromosome, Resources resources) { - int mutations = (int) Math.ceil(((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("outputs")))) / (double) 100))); + int mutations = (int) ((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("outputs")))) / 100)); for (int i = 0; i < mutations; i++) { MutableElement m = chromosome.getRandomMutableElement(); @@ -55,18 +61,4 @@ public class PointMutator implements Mutator { 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; - } } |