diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-08 14:48:25 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-08 14:48:25 +0000 |
commit | d63d3145f0f2abcee1bb88457324f4aaf9b9320e (patch) | |
tree | dfb19082adfba8989c4fd95ef286b8c1e7a1f2b1 /src/jcgp/modules/fitness | |
parent | ef7a850a8f2f81ccaa07f25d9b7ad602e84d88c9 (diff) |
Slowly refactoring Parameters to fit the GUI a little better...
Diffstat (limited to 'src/jcgp/modules/fitness')
-rw-r--r-- | src/jcgp/modules/fitness/FitnessFunction.java | 9 | ||||
-rw-r--r-- | src/jcgp/modules/fitness/TestCase.java | 42 | ||||
-rw-r--r-- | src/jcgp/modules/fitness/TruthTableEvaluator.java | 30 |
3 files changed, 81 insertions, 0 deletions
diff --git a/src/jcgp/modules/fitness/FitnessFunction.java b/src/jcgp/modules/fitness/FitnessFunction.java new file mode 100644 index 0000000..8ed1b56 --- /dev/null +++ b/src/jcgp/modules/fitness/FitnessFunction.java @@ -0,0 +1,9 @@ +package jcgp.modules.fitness; + +import jcgp.population.Population; + +public interface FitnessFunction { + + public void evaluate(Population population); + +} diff --git a/src/jcgp/modules/fitness/TestCase.java b/src/jcgp/modules/fitness/TestCase.java new file mode 100644 index 0000000..0cb09f1 --- /dev/null +++ b/src/jcgp/modules/fitness/TestCase.java @@ -0,0 +1,42 @@ +package jcgp.modules.fitness; + +import jcgp.exceptions.ParameterMismatchException; +import jcgp.parameters.Parameters; + +public class TestCase { + + private Object[] inputs; + private Object[] outputs; + + public TestCase(Object[] inputs, Object[] 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 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/modules/fitness/TruthTableEvaluator.java b/src/jcgp/modules/fitness/TruthTableEvaluator.java new file mode 100644 index 0000000..a69de96 --- /dev/null +++ b/src/jcgp/modules/fitness/TruthTableEvaluator.java @@ -0,0 +1,30 @@ +package jcgp.modules.fitness; + +import jcgp.TruthTable; +import jcgp.parameters.Parameters; +import jcgp.population.Population; + +public class TruthTableEvaluator implements FitnessFunction { + + @Override + public void evaluate(Population population) { + // for every chromosome in the population + for (int i = 0; i < Parameters.getPopulationSize(); i++) { + int fitness = 0; + // for every test case + for (int t = 0; t < TruthTable.getTestCaseCount(); t++) { + population.getChromosome(i).setInputs(TruthTable.getTestCase(t).getInputs()); + // check every output + for (int o = 0; o < Parameters.getOutputs(); o++) { + if (population.getChromosome(i).getOutput(o).calculate() == TruthTable.getTestCase(t).getOutput(o)) { + fitness++; + } + } + } + population.getChromosome(i).setFitness(fitness); + if (Parameters.getDebug()) { + System.out.println("active nodes: " + population.getChromosome(i).getActiveNodes().size()); + } + } + } +} |