aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/problem/TestCaseProblem.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/modules/problem/TestCaseProblem.java')
-rw-r--r--src/jcgp/backend/modules/problem/TestCaseProblem.java86
1 files changed, 43 insertions, 43 deletions
diff --git a/src/jcgp/backend/modules/problem/TestCaseProblem.java b/src/jcgp/backend/modules/problem/TestCaseProblem.java
index ee72860..ff13c2e 100644
--- a/src/jcgp/backend/modules/problem/TestCaseProblem.java
+++ b/src/jcgp/backend/modules/problem/TestCaseProblem.java
@@ -1,11 +1,11 @@
package jcgp.backend.modules.problem;
+import java.io.File;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
-import jcgp.backend.population.Chromosome;
-import jcgp.backend.population.Population;
+import jcgp.backend.parser.TestCaseParser;
import jcgp.backend.resources.Resources;
import jcgp.backend.resources.parameters.IntegerParameter;
import jcgp.backend.resources.parameters.Parameter;
@@ -48,15 +48,16 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
}
}
- private ObservableList<TestCase<U>> testCases;
- private IntegerParameter maxFitness;
- private final int inputCount, outputCount;
+ protected ObservableList<TestCase<U>> testCases;
+ protected IntegerParameter maxFitness;
+ protected Resources resources;
+
+ protected TestCaseParser parser;
public TestCaseProblem(Resources resources) {
super();
- inputCount = resources.inputs();
- outputCount = resources.outputs();
+ this.resources = resources;
maxFitness = new IntegerParameter(0, "Max fitness", true, false) {
@Override
@@ -65,34 +66,26 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
}
};
testCases = FXCollections.observableArrayList();
+
+ parser = new TestCaseParser(this);
}
-
- @Override
- public void evaluate(Population population, Resources resources) {
- // set fittest to 0, change it whenever a fitter one is found
- population.setFittest(0);
+ public TestCaseProblem(Resources resources, File testCase) {
+ super();
- // for every chromosome in the population
- for (int i = 0; i < resources.populationSize(); 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.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);
- if (fitness >= population.getFittest().getFitness()) {
- population.setFittest(i);
+ this.resources = resources;
+
+ maxFitness = new IntegerParameter(0, "Max fitness", true, false) {
+ @Override
+ public void validate(Number newValue) {
+ // blank
}
- }
+ };
+ testCases = FXCollections.observableArrayList();
+
+ parser = new TestCaseParser(this);
+
+ parser.parse(testCase);
}
@Override
@@ -100,12 +93,7 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
return new Parameter[]{maxFitness};
}
- @Override
- public boolean isPerfectSolution(Chromosome fittest) {
- return fittest.getFitness() >= maxFitness.get();
- }
-
- private int getMaxFitness() {
+ protected int getMaxFitness() {
int fitness = 0;
for (TestCase<U> tc : testCases) {
@@ -125,13 +113,15 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
return testCases;
}
+ public abstract void addTestCase(String[] inputs, String[] outputs);
+
public void addTestCase(TestCase<U> testCase) {
- if (testCase.getInputs().length != inputCount) {
+ if (testCase.getInputs().length != resources.inputs()) {
throw new IllegalArgumentException("Received test case with " + testCase.getInputs().length +
- "inputs but need exactly " + inputCount);
- } else if (testCase.getOutputs().length != outputCount) {
+ " inputs but need exactly " + resources.inputs());
+ } else if (testCase.getOutputs().length != resources.outputs()) {
throw new IllegalArgumentException("Received test case with " + testCase.getOutputs().length +
- "outputs but need exactly " + outputCount);
+ " outputs but need exactly " + resources.outputs());
} else {
this.testCases.add(testCase);
maxFitness.set(getMaxFitness());
@@ -144,12 +134,22 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
}
public int getInputCount() {
- return inputCount;
+ return resources.inputs();
}
public int getOutputCount() {
- return outputCount;
+ return resources.outputs();
+ }
+
+ public void parse(File file) {
+ parser.parse(file);
}
+
+ public void clearTestCases() {
+ testCases.clear();
+ }
+
+ public abstract String getFileExtension();
}