diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-07 10:04:05 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-07 10:04:05 +0100 |
commit | 67ace66f66ffaa00e1bd1495c0d406c801e59c5c (patch) | |
tree | 337da2ab9bbd5eb41a0a6eafbf783340240bcffe /src/jcgp/backend/modules | |
parent | efee474689b37f43897b8572cec4e3669874b0d4 (diff) |
Refactored problem types
Diffstat (limited to 'src/jcgp/backend/modules')
-rw-r--r-- | src/jcgp/backend/modules/fitness/DigitalCircuit.java | 17 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/FitnessFunction.java | 11 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/Problem.java | 20 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/SymbolicRegression.java | 17 | ||||
-rw-r--r-- | src/jcgp/backend/modules/fitness/TestCaseProblem.java (renamed from src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java) | 45 |
5 files changed, 89 insertions, 21 deletions
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<Integer> { + + 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<Integer> { + + public SymbolicRegression() { + super(); + functionSet = new IntegerArithmetic(); + } + + @Override + public String toString() { + return "Symbolic regression"; + } + +} diff --git a/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java b/src/jcgp/backend/modules/fitness/TestCaseProblem.java index 4b0654c..7753e26 100644 --- a/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java +++ b/src/jcgp/backend/modules/fitness/TestCaseProblem.java @@ -1,11 +1,13 @@ -package jcgp.backend.modules.fitness.testcase; +package jcgp.backend.modules.fitness; import java.util.ArrayList; -import java.util.Collections; +import java.util.List; -import jcgp.backend.modules.fitness.FitnessFunction; +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; /** * @@ -17,7 +19,7 @@ import jcgp.backend.resources.Resources; * @author Eduardo Pedroni * */ -public abstract class TestCaseEvaluator implements FitnessFunction { +public abstract class TestCaseProblem<U> extends Problem { public static class TestCase<T> { @@ -46,7 +48,19 @@ public abstract class TestCaseEvaluator implements FitnessFunction { } } - protected ArrayList<TestCase<?>> testCases; + private ArrayList<TestCase<U>> testCases; + private IntegerParameter maxFitness; + + public TestCaseProblem() { + maxFitness = new IntegerParameter(0, "Max fitness", true, false) { + @Override + public void validate(int newValue) { + // blank + } + }; + testCases = new ArrayList<TestCase<U>>(); + } + @Override public void evaluate(Population population, Resources resources) { @@ -69,19 +83,30 @@ public abstract class TestCaseEvaluator implements FitnessFunction { } } - public int getMaxFitness() { + @Override + public Parameter[] getLocalParameters() { + return new Parameter[]{maxFitness}; + } + + private int getMaxFitness() { int fitness = 0; - for (TestCase<?> tc : testCases) { + for (TestCase<U> tc : testCases) { fitness += tc.getOutputs().length; } return fitness; } - public void setTestCases(TestCase<?>... testCases) { - this.testCases = new ArrayList<TestCase<?>>(); - Collections.addAll(this.testCases, testCases); + public void setTestCases(List<TestCase<U>> testCases) { + this.testCases.clear(); + this.testCases.addAll(testCases); + maxFitness.set(getMaxFitness()); + } + + @Override + public boolean isPerfectSolution(Chromosome fittest) { + return fittest.getFitness() >= maxFitness.get(); } } |