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 --- .../backend/modules/fitness/FitnessFunction.java | 2 +- src/jcgp/backend/modules/fitness/TestCase.java | 29 --------- .../backend/modules/fitness/TestCaseEvaluator.java | 43 ------------- .../fitness/testcase/TestCaseEvaluator.java | 75 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 73 deletions(-) delete mode 100644 src/jcgp/backend/modules/fitness/TestCase.java delete mode 100644 src/jcgp/backend/modules/fitness/TestCaseEvaluator.java create mode 100644 src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java (limited to 'src/jcgp/backend/modules/fitness') 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 { + + 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