aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/fitness
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/modules/fitness')
-rw-r--r--src/jcgp/backend/modules/fitness/FitnessFunction.java2
-rw-r--r--src/jcgp/backend/modules/fitness/TestCase.java29
-rw-r--r--src/jcgp/backend/modules/fitness/TestCaseEvaluator.java43
-rw-r--r--src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java75
4 files changed, 76 insertions, 73 deletions
diff --git a/src/jcgp/backend/modules/fitness/FitnessFunction.java b/src/jcgp/backend/modules/fitness/FitnessFunction.java
index ffce9b7..d3b76cb 100644
--- a/src/jcgp/backend/modules/fitness/FitnessFunction.java
+++ b/src/jcgp/backend/modules/fitness/FitnessFunction.java
@@ -1,8 +1,8 @@
package jcgp.backend.modules.fitness;
-import jcgp.JCGP.Resources;
import jcgp.backend.modules.Module;
import jcgp.backend.population.Population;
+import jcgp.backend.resources.Resources;
public interface FitnessFunction extends Module {
diff --git a/src/jcgp/backend/modules/fitness/TestCase.java b/src/jcgp/backend/modules/fitness/TestCase.java
deleted file mode 100644
index 23f7ecf..0000000
--- a/src/jcgp/backend/modules/fitness/TestCase.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package jcgp.backend.modules.fitness;
-
-public class TestCase {
-
- private Object[] inputs;
- private Object[] outputs;
-
- public TestCase(Object[] inputs, Object[] outputs) {
- this.inputs = inputs;
- this.outputs = outputs;
- }
-
- public Object getInput(int index) {
- return inputs[index];
- }
-
- public Object getOutput(int index) {
- return outputs[index];
- }
-
- public Object[] getInputs() {
- return inputs;
- }
-
- public Object[] getOutputs() {
- return outputs;
- }
-
-}
diff --git a/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java b/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java
deleted file mode 100644
index 5ff6973..0000000
--- a/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package jcgp.backend.modules.fitness;
-
-import jcgp.JCGP.Resources;
-import jcgp.backend.modules.ModuleStatus;
-import jcgp.backend.parameters.Parameter;
-import jcgp.backend.population.Population;
-
-public class TestCaseEvaluator implements FitnessFunction {
-
- @Override
- public void evaluate(Population population, Resources resources) {
- // for every chromosome in the population
- for (int i = 0; i < resources.getInt("popSize"); i++) {
- int fitness = 0;
- // for every test case
- for (int t = 0; t < resources.getTestCaseCount(); t++) {
- population.getChromosome(i).setInputs(resources.getTestCase(t).getInputs());
- // check every output
- for (int o = 0; o < resources.getInt("outputs"); o++) {
- if (population.getChromosome(i).getOutput(o).calculate() == resources.getTestCase(t).getOutput(o)) {
- fitness++;
- }
- }
- }
- population.getChromosome(i).setFitness(fitness);
- }
- }
-
- @Override
- public Parameter[] getLocalParameters() {
- return null;
- }
-
- @Override
- public String toString() {
- return "Test case";
- }
-
- @Override
- public ModuleStatus getStatus(Resources resources) {
- return null;
- }
-}
diff --git a/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java b/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java
new file mode 100644
index 0000000..51676e3
--- /dev/null
+++ b/src/jcgp/backend/modules/fitness/testcase/TestCaseEvaluator.java
@@ -0,0 +1,75 @@
+package jcgp.backend.modules.fitness.testcase;
+
+import java.util.ArrayList;
+
+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<T> {
+
+ 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<TestCase> 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() {
+ return 0;
+ }
+}
+