From e6dd7711c7dad5e000445208eb5845801f4ccffc Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 6 Apr 2014 21:58:53 +0100 Subject: About to make big changes to the way fitness works, committing just in case --- .../fitness/testcase/TestCaseEvaluator.java | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java (limited to 'src/jcgp/backend/modules/fitness/testcase') 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 { + + 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 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; + } +} + -- cgit v1.2.3