diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-04 16:20:33 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-04 16:20:33 +0000 |
commit | 1fd678821cf133c7c431daea687f3467bb0be2dd (patch) | |
tree | c23e7fe6a4076c6748d03b2cee9d41ff6e9734b2 /src/jcgp/fitness | |
parent | d9671c354080de20bba4f70438af9242c8ecd675 (diff) |
Added fitness evaluation mechanism, though it might not be a very good design. Tests will be done to verify that.
Diffstat (limited to 'src/jcgp/fitness')
-rw-r--r-- | src/jcgp/fitness/FitnessEvaluator.java | 17 | ||||
-rw-r--r-- | src/jcgp/fitness/FitnessFunction.java | 4 | ||||
-rw-r--r-- | src/jcgp/fitness/ParameterMismatchException.java | 10 | ||||
-rw-r--r-- | src/jcgp/fitness/TestCase.java | 41 | ||||
-rw-r--r-- | src/jcgp/fitness/TestFitFunction.java | 17 | ||||
-rw-r--r-- | src/jcgp/fitness/TruthTableEvaluator.java | 26 |
6 files changed, 79 insertions, 36 deletions
diff --git a/src/jcgp/fitness/FitnessEvaluator.java b/src/jcgp/fitness/FitnessEvaluator.java deleted file mode 100644 index 5ecf679..0000000 --- a/src/jcgp/fitness/FitnessEvaluator.java +++ /dev/null @@ -1,17 +0,0 @@ -package jcgp.fitness; - -import jcgp.population.Population; - -public class FitnessEvaluator { - - private FitnessFunction fitnessFunction; - - public FitnessEvaluator(FitnessFunction fitnessFunction) { - this.fitnessFunction = fitnessFunction; - } - - public void evaluateFitness(Population population) { - - } - -} diff --git a/src/jcgp/fitness/FitnessFunction.java b/src/jcgp/fitness/FitnessFunction.java index 2155542..6fa63ef 100644 --- a/src/jcgp/fitness/FitnessFunction.java +++ b/src/jcgp/fitness/FitnessFunction.java @@ -3,7 +3,7 @@ package jcgp.fitness; import jcgp.population.Population; public interface FitnessFunction { - - public void evaluatePopulation(Population population); + + public void evaluate(Population population); } diff --git a/src/jcgp/fitness/ParameterMismatchException.java b/src/jcgp/fitness/ParameterMismatchException.java new file mode 100644 index 0000000..0ca69a7 --- /dev/null +++ b/src/jcgp/fitness/ParameterMismatchException.java @@ -0,0 +1,10 @@ +package jcgp.fitness; + +public class ParameterMismatchException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = -3161318886125868134L; + +} diff --git a/src/jcgp/fitness/TestCase.java b/src/jcgp/fitness/TestCase.java new file mode 100644 index 0000000..e2bd5ed --- /dev/null +++ b/src/jcgp/fitness/TestCase.java @@ -0,0 +1,41 @@ +package jcgp.fitness; + +import jcgp.CGP.Parameters; + +public class TestCase { + + private int[] inputs; + private int[] outputs; + + public TestCase(int[] inputs, int[] outputs) throws ParameterMismatchException { + if (inputs.length == Parameters.getInputs()) { + this.inputs = inputs; + } else { + throw new ParameterMismatchException(); + } + + if (outputs.length == Parameters.getOutputs()) { + this.outputs = outputs; + } else { + throw new ParameterMismatchException(); + } + + } + + public int getInput(int index) { + return inputs[index]; + } + + public int getOutput(int index) { + return outputs[index]; + } + + public int[] getInputs() { + return inputs; + } + + public int[] getOutputs() { + return outputs; + } + +} diff --git a/src/jcgp/fitness/TestFitFunction.java b/src/jcgp/fitness/TestFitFunction.java deleted file mode 100644 index 00ee833..0000000 --- a/src/jcgp/fitness/TestFitFunction.java +++ /dev/null @@ -1,17 +0,0 @@ -package jcgp.fitness; - -import jcgp.population.Chromosome; -import jcgp.population.Population; - -public class TestFitFunction implements FitnessFunction { - - @Override - public void evaluatePopulation(Population population) { - - for (Chromosome c : population) { - int i = c.getOutput(0).calculate(); - System.out.println(i); - c.setFitness(1); - } - } -} diff --git a/src/jcgp/fitness/TruthTableEvaluator.java b/src/jcgp/fitness/TruthTableEvaluator.java new file mode 100644 index 0000000..64ca209 --- /dev/null +++ b/src/jcgp/fitness/TruthTableEvaluator.java @@ -0,0 +1,26 @@ +package jcgp.fitness; + +import jcgp.CGP.Parameters; +import jcgp.CGP.TruthTable; +import jcgp.population.Chromosome; +import jcgp.population.Population; + +public class TruthTableEvaluator implements FitnessFunction { + + @Override + public void evaluate(Population population) { + for (Chromosome chromosome : population) { + for (int t = 0; t < TruthTable.getTestCaseCount(); t++) { + chromosome.setInputs(TruthTable.getTestCase(t).getInputs()); + int fitness = 0; + for (int o = 0; o < Parameters.getOutputs(); o++) { + if (chromosome.getOutput(o).calculate() == TruthTable.getTestCase(t).getOutput(o)) { + fitness++; + } + } + chromosome.setFitness(fitness); + } + } + } + +} |