diff options
Diffstat (limited to 'src/jcgp/modules/fitness')
-rw-r--r-- | src/jcgp/modules/fitness/FitnessFunction.java | 6 | ||||
-rw-r--r-- | src/jcgp/modules/fitness/TestCase.java | 19 | ||||
-rw-r--r-- | src/jcgp/modules/fitness/TestCaseEvaluator.java | 34 | ||||
-rw-r--r-- | src/jcgp/modules/fitness/TruthTableEvaluator.java | 30 |
4 files changed, 41 insertions, 48 deletions
diff --git a/src/jcgp/modules/fitness/FitnessFunction.java b/src/jcgp/modules/fitness/FitnessFunction.java index 8ed1b56..509c230 100644 --- a/src/jcgp/modules/fitness/FitnessFunction.java +++ b/src/jcgp/modules/fitness/FitnessFunction.java @@ -1,9 +1,11 @@ package jcgp.modules.fitness; +import jcgp.CGP.Resources; +import jcgp.modules.Module; import jcgp.population.Population; -public interface FitnessFunction { +public interface FitnessFunction extends Module { - public void evaluate(Population population); + public void evaluate(Population population, Resources resources); } diff --git a/src/jcgp/modules/fitness/TestCase.java b/src/jcgp/modules/fitness/TestCase.java index 0cb09f1..081a257 100644 --- a/src/jcgp/modules/fitness/TestCase.java +++ b/src/jcgp/modules/fitness/TestCase.java @@ -1,26 +1,13 @@ 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 TestCase(Object[] inputs, Object[] outputs) { + this.inputs = inputs; + this.outputs = outputs; } public Object getInput(int index) { diff --git a/src/jcgp/modules/fitness/TestCaseEvaluator.java b/src/jcgp/modules/fitness/TestCaseEvaluator.java new file mode 100644 index 0000000..1dade74 --- /dev/null +++ b/src/jcgp/modules/fitness/TestCaseEvaluator.java @@ -0,0 +1,34 @@ +package jcgp.modules.fitness; + +import java.util.HashMap; + +import jcgp.CGP.Resources; +import jcgp.parameters.Parameter; +import jcgp.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 < (int) resources.get("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 < (int) resources.get("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> activate(Resources parameters) { + return new HashMap<String, Parameter>(); + } +} diff --git a/src/jcgp/modules/fitness/TruthTableEvaluator.java b/src/jcgp/modules/fitness/TruthTableEvaluator.java deleted file mode 100644 index a69de96..0000000 --- a/src/jcgp/modules/fitness/TruthTableEvaluator.java +++ /dev/null @@ -1,30 +0,0 @@ -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()); - } - } - } -} |