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 ---------------- .../backend/resources/ModifiableResources.java | 5 +- src/jcgp/backend/resources/Resources.java | 20 +--- 8 files changed, 172 insertions(+), 117 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') 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); - } -} - diff --git a/src/jcgp/backend/resources/ModifiableResources.java b/src/jcgp/backend/resources/ModifiableResources.java index 3e6b55e..90c2f03 100644 --- a/src/jcgp/backend/resources/ModifiableResources.java +++ b/src/jcgp/backend/resources/ModifiableResources.java @@ -1,5 +1,6 @@ package jcgp.backend.resources; +import jcgp.backend.function.FunctionSet; import jcgp.backend.resources.parameters.BooleanParameter; import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.IntegerParameter; @@ -30,8 +31,8 @@ public class ModifiableResources extends Resources { } } - public void setFunctionSet(int index) { - functionSet = functionSets[index]; + public void setFunctionSet(FunctionSet functionSet) { + this.functionSet = functionSet; set("arity", functionSet.getMaxArity()); } diff --git a/src/jcgp/backend/resources/Resources.java b/src/jcgp/backend/resources/Resources.java index c1c3e4c..d1f396a 100644 --- a/src/jcgp/backend/resources/Resources.java +++ b/src/jcgp/backend/resources/Resources.java @@ -5,11 +5,8 @@ import java.util.Random; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; -import jcgp.backend.function.BitwiseLogic; -import jcgp.backend.function.BooleanLogic; import jcgp.backend.function.Function; import jcgp.backend.function.FunctionSet; -import jcgp.backend.function.IntegerArithmetic; import jcgp.backend.resources.parameters.BooleanParameter; import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.IntegerParameter; @@ -30,12 +27,7 @@ public class Resources { protected Random numberGenerator = new Random(); - // function sets - protected FunctionSet[] functionSets = new FunctionSet[] { - new IntegerArithmetic(), - new BitwiseLogic(), - new BooleanLogic() }; - protected FunctionSet functionSet = functionSets[0]; + protected FunctionSet functionSet; // GUI console protected Console console; @@ -202,7 +194,8 @@ public class Resources { } }); - parameters.put("arity", new IntegerParameter(functionSet.getMaxArity(), "Max arity", true, false) { + //parameters.put("arity", new IntegerParameter(functionSet.getMaxArity(), "Max arity", true, false) { + parameters.put("arity", new IntegerParameter(0, "Max arity", true, false) { @Override public void validate(int newValue) { // blank @@ -266,13 +259,6 @@ public class Resources { return functionSet.getAllowedFunction(index); } - /** - * @return the functionSets - */ - public FunctionSet[] getFunctionSets() { - return functionSets; - } - /** * @return the functionSet */ -- cgit v1.2.3