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/DigitalCircuit.java | 17 ++++ .../backend/modules/fitness/FitnessFunction.java | 11 -- src/jcgp/backend/modules/fitness/Problem.java | 20 ++++ .../modules/fitness/SymbolicRegression.java | 17 ++++ .../backend/modules/fitness/TestCaseProblem.java | 112 +++++++++++++++++++++ .../fitness/testcase/TestCaseEvaluator.java | 87 ---------------- 6 files changed, 166 insertions(+), 98 deletions(-) create mode 100644 src/jcgp/backend/modules/fitness/DigitalCircuit.java delete mode 100644 src/jcgp/backend/modules/fitness/FitnessFunction.java create mode 100644 src/jcgp/backend/modules/fitness/Problem.java create mode 100644 src/jcgp/backend/modules/fitness/SymbolicRegression.java create mode 100644 src/jcgp/backend/modules/fitness/TestCaseProblem.java delete 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/DigitalCircuit.java b/src/jcgp/backend/modules/fitness/DigitalCircuit.java new file mode 100644 index 0000000..b01bdc5 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/DigitalCircuit.java @@ -0,0 +1,17 @@ +package jcgp.backend.modules.fitness; + +import jcgp.backend.function.BitwiseLogic; + +public class DigitalCircuit extends TestCaseProblem { + + public DigitalCircuit() { + super(); + functionSet = new BitwiseLogic(); + } + + @Override + public String toString() { + return "Digital circuit"; + } + +} diff --git a/src/jcgp/backend/modules/fitness/FitnessFunction.java b/src/jcgp/backend/modules/fitness/FitnessFunction.java deleted file mode 100644 index d3b76cb..0000000 --- a/src/jcgp/backend/modules/fitness/FitnessFunction.java +++ /dev/null @@ -1,11 +0,0 @@ -package jcgp.backend.modules.fitness; - -import jcgp.backend.modules.Module; -import jcgp.backend.population.Population; -import jcgp.backend.resources.Resources; - -public interface FitnessFunction extends Module { - - public void evaluate(Population population, Resources resources); - -} diff --git a/src/jcgp/backend/modules/fitness/Problem.java b/src/jcgp/backend/modules/fitness/Problem.java new file mode 100644 index 0000000..1e70c13 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/Problem.java @@ -0,0 +1,20 @@ +package jcgp.backend.modules.fitness; + +import jcgp.backend.function.FunctionSet; +import jcgp.backend.modules.Module; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Population; +import jcgp.backend.resources.Resources; + +public abstract class Problem implements Module { + + protected FunctionSet functionSet; + + public abstract void evaluate(Population population, Resources resources); + + public FunctionSet getFunctionSet() { + return functionSet; + } + + public abstract boolean isPerfectSolution(Chromosome fittest); +} diff --git a/src/jcgp/backend/modules/fitness/SymbolicRegression.java b/src/jcgp/backend/modules/fitness/SymbolicRegression.java new file mode 100644 index 0000000..da2e69e --- /dev/null +++ b/src/jcgp/backend/modules/fitness/SymbolicRegression.java @@ -0,0 +1,17 @@ +package jcgp.backend.modules.fitness; + +import jcgp.backend.function.IntegerArithmetic; + +public class SymbolicRegression extends TestCaseProblem { + + public SymbolicRegression() { + super(); + functionSet = new IntegerArithmetic(); + } + + @Override + public String toString() { + return "Symbolic regression"; + } + +} 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(); + } +} + diff --git a/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java b/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java deleted file mode 100644 index 4b0654c..0000000 --- a/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java +++ /dev/null @@ -1,87 +0,0 @@ -package jcgp.backend.modules.fitness.testcase; - -import java.util.ArrayList; -import java.util.Collections; - -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() { - int fitness = 0; - - for (TestCase tc : testCases) { - fitness += tc.getOutputs().length; - } - - return fitness; - } - - public void setTestCases(TestCase... testCases) { - this.testCases = new ArrayList>(); - Collections.addAll(this.testCases, testCases); - } -} - -- cgit v1.2.3