aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-04-07 10:04:05 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-04-07 10:04:05 +0100
commit67ace66f66ffaa00e1bd1495c0d406c801e59c5c (patch)
tree337da2ab9bbd5eb41a0a6eafbf783340240bcffe /src/jcgp/backend
parentefee474689b37f43897b8572cec4e3669874b0d4 (diff)
Refactored problem types
Diffstat (limited to 'src/jcgp/backend')
-rw-r--r--src/jcgp/backend/modules/fitness/DigitalCircuit.java17
-rw-r--r--src/jcgp/backend/modules/fitness/FitnessFunction.java11
-rw-r--r--src/jcgp/backend/modules/fitness/Problem.java20
-rw-r--r--src/jcgp/backend/modules/fitness/SymbolicRegression.java17
-rw-r--r--src/jcgp/backend/modules/fitness/TestCaseProblem.java (renamed from src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java)45
-rw-r--r--src/jcgp/backend/resources/ModifiableResources.java5
-rw-r--r--src/jcgp/backend/resources/Resources.java20
7 files changed, 95 insertions, 40 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();
}
}
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
@@ -267,13 +260,6 @@ public class Resources {
}
/**
- * @return the functionSets
- */
- public FunctionSet[] getFunctionSets() {
- return functionSets;
- }
-
- /**
* @return the functionSet
*/
public FunctionSet getFunctionSet() {