aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/modules/problem/DigitalCircuitProblem.java')
-rw-r--r--src/jcgp/backend/modules/problem/DigitalCircuitProblem.java52
1 files changed, 26 insertions, 26 deletions
diff --git a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java
index 6654928..0071ed5 100644
--- a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java
+++ b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java
@@ -2,7 +2,6 @@ package jcgp.backend.modules.problem;
import jcgp.backend.function.DigitalCircuitFunctions;
import jcgp.backend.function.UnsignedInteger;
-import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Population;
import jcgp.backend.resources.Resources;
@@ -18,7 +17,7 @@ import jcgp.backend.resources.Resources;
*
*/
public class DigitalCircuitProblem extends TestCaseProblem<UnsignedInteger> {
-
+
/**
* Construct a new instance of DigitalCircuitProblem.
*
@@ -30,43 +29,38 @@ public class DigitalCircuitProblem extends TestCaseProblem<UnsignedInteger> {
setName("Digital circuit");
setFileExtension(".plu");
}
-
+
@Override
public void evaluate(Population population, Resources resources) {
// for every chromosome in the population
for (int i = 0; i < resources.populationSize(); i++) {
// assume an initial fitness of 0
int fitness = 0;
-
+
// iterate over every test case
for (int t = 0; t < testCases.size(); t++) {
- population.getChromosome(i).setInputs((Object[]) testCases.get(t).getInputs());
+ population.get(i).setInputs((Object[]) testCases.get(t).getInputs());
// check each output
for (int o = 0; o < resources.outputs(); o++) {
- Integer output = ((UnsignedInteger) population.getChromosome(i).getOutput(o).calculate()).get();
+ Integer output = ((UnsignedInteger) population.get(i).getOutput(o).calculate()).get();
Integer matches = ~(output ^ testCases.get(t).getOutput(o).get());
// check only the relevant bits
int bits = (int) Math.pow(2.0, (double) resources.inputs());
for (int b = 0; b < bits; b++) {
- if (((matches >>> b) & 1) == 1) {
- fitness++;
- }
+ fitness += (matches >>> b) & 1;
}
}
}
-
+
// assign the resulting fitness to the respective individual
- population.getChromosome(i).setFitness(fitness);
+ population.get(i).setFitness(fitness);
}
-
- // sort population
- population.sortAscending();
}
-
+
@Override
protected double getMaxFitness() {
// calculate the fitness by looking at inputs, not number of test cases
- double maxFitness = Math.pow(2.0, (double) resources.inputs()) * resources.outputs();
+ double maxFitness = Math.pow(2.0, (double) getResources().inputs()) * getResources().outputs();
return maxFitness;
}
@@ -81,24 +75,30 @@ public class DigitalCircuitProblem extends TestCaseProblem<UnsignedInteger> {
for (int o = 0; o < outputCases.length; o++) {
outputCases[o] = new UnsignedInteger(outputs[o]);
}
-
+
return new TestCase<UnsignedInteger>(inputCases, outputCases);
}
-
+
@Override
- public boolean isPerfectSolution(Chromosome fittest) {
+ public int perfectSolutionFound(Population population) {
// higher fitness is better
- return fittest.getFitness() >= maxFitness.get();
+ for (int i = 0; i < getResources().populationSize(); i++) {
+ if (population.get(i).getFitness() >= maxFitness.get()) {
+ return i;
+ }
+ }
+ return -1;
}
@Override
- public boolean isImprovement(Chromosome fittest) {
+ public int hasImprovement(Population population) {
// higher fitness is better
- if (fittest.getFitness() > bestFitness.get()) {
- bestFitness.set(fittest.getFitness());
- return true;
- } else {
- return false;
+ for (int i = 0; i < getResources().populationSize(); i++) {
+ if (population.get(i).getFitness() > bestFitness.get()) {
+ bestFitness.set(population.get(i).getFitness());
+ return i;
+ }
}
+ return -1;
}
}