From 67ace66f66ffaa00e1bd1495c0d406c801e59c5c Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 7 Apr 2014 10:04:05 +0100 Subject: Refactored problem types --- .../backend/modules/fitness/TestCaseProblem.java | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/jcgp/backend/modules/fitness/TestCaseProblem.java (limited to 'src/jcgp/backend/modules/fitness/TestCaseProblem.java') diff --git a/src/jcgp/backend/modules/fitness/TestCaseProblem.java b/src/jcgp/backend/modules/fitness/TestCaseProblem.java new file mode 100644 index 0000000..7753e26 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/TestCaseProblem.java @@ -0,0 +1,112 @@ +package jcgp.backend.modules.fitness; + +import java.util.ArrayList; +import java.util.List; + +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Population; +import jcgp.backend.resources.Resources; +import jcgp.backend.resources.parameters.IntegerParameter; +import jcgp.backend.resources.parameters.Parameter; + +/** + * + * This fitness function module implements a simple test case evaluator. + * + * A TestCase object is a + * + * + * @author Eduardo Pedroni + * + */ +public abstract class TestCaseProblem extends Problem { + + 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; + } + } + + private ArrayList> testCases; + private IntegerParameter maxFitness; + + public TestCaseProblem() { + maxFitness = new IntegerParameter(0, "Max fitness", true, false) { + @Override + public void validate(int newValue) { + // blank + } + }; + testCases = new ArrayList>(); + } + + + @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); + } + } + + @Override + public Parameter[] getLocalParameters() { + return new Parameter[]{maxFitness}; + } + + private int getMaxFitness() { + int fitness = 0; + + for (TestCase tc : testCases) { + fitness += tc.getOutputs().length; + } + + return fitness; + } + + public void setTestCases(List> testCases) { + this.testCases.clear(); + this.testCases.addAll(testCases); + maxFitness.set(getMaxFitness()); + } + + @Override + public boolean isPerfectSolution(Chromosome fittest) { + return fittest.getFitness() >= maxFitness.get(); + } +} + -- cgit v1.2.3