From 02fd2bc7059da416937beb1abe67e5ca60379030 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 1 Apr 2014 23:00:53 +0100 Subject: Settings pane now actually controls the parameters, not much left to do. --- .../backend/modules/fitness/FitnessFunction.java | 11 ++++++ src/jcgp/backend/modules/fitness/TestCase.java | 29 ++++++++++++++ .../backend/modules/fitness/TestCaseEvaluator.java | 45 ++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/jcgp/backend/modules/fitness/FitnessFunction.java create mode 100644 src/jcgp/backend/modules/fitness/TestCase.java create mode 100644 src/jcgp/backend/modules/fitness/TestCaseEvaluator.java (limited to 'src/jcgp/backend/modules/fitness') diff --git a/src/jcgp/backend/modules/fitness/FitnessFunction.java b/src/jcgp/backend/modules/fitness/FitnessFunction.java new file mode 100644 index 0000000..ffce9b7 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/FitnessFunction.java @@ -0,0 +1,11 @@ +package jcgp.backend.modules.fitness; + +import jcgp.JCGP.Resources; +import jcgp.backend.modules.Module; +import jcgp.backend.population.Population; + +public interface FitnessFunction extends Module { + + public void evaluate(Population population, Resources resources); + +} diff --git a/src/jcgp/backend/modules/fitness/TestCase.java b/src/jcgp/backend/modules/fitness/TestCase.java new file mode 100644 index 0000000..23f7ecf --- /dev/null +++ b/src/jcgp/backend/modules/fitness/TestCase.java @@ -0,0 +1,29 @@ +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 new file mode 100644 index 0000000..3b67f28 --- /dev/null +++ b/src/jcgp/backend/modules/fitness/TestCaseEvaluator.java @@ -0,0 +1,45 @@ +package jcgp.backend.modules.fitness; + +import java.util.HashMap; + +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 HashMap getLocalParameters() { + return null; + } + + @Override + public String toString() { + return "Test case"; + } + + @Override + public ModuleStatus getStatus(Resources resources) { + return null; + } +} -- cgit v1.2.3