From 7f89d81e6f8a5ce82d42c3b852b5219edaa4b86c Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Wed, 7 May 2014 18:38:27 +0100 Subject: Removed reflection for now, added fitness orientation support --- src/jcgp/backend/modules/mutator/PointMutator.java | 6 +++--- src/jcgp/backend/modules/problem/BestFitness.java | 18 ++++++++++++++++++ .../modules/problem/DigitalCircuitProblem.java | 14 +------------- src/jcgp/backend/modules/problem/Problem.java | 2 +- .../modules/problem/SymbolicRegressionProblem.java | 16 +--------------- src/jcgp/backend/modules/problem/TestCaseProblem.java | 19 +++++++++++++++++++ 6 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 src/jcgp/backend/modules/problem/BestFitness.java (limited to 'src/jcgp/backend/modules') diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 9e421c2..5aba0d1 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -3,7 +3,7 @@ package jcgp.backend.modules.mutator; import jcgp.backend.parameters.BooleanParameter; import jcgp.backend.parameters.IntegerParameter; import jcgp.backend.population.Chromosome; -import jcgp.backend.population.MutableElement; +import jcgp.backend.population.Mutable; import jcgp.backend.population.Node; import jcgp.backend.population.Output; import jcgp.backend.resources.Resources; @@ -41,7 +41,7 @@ public abstract class PointMutator extends Mutator { // for however many genes must be mutated for (int i = 0; i < genesMutated.get(); i++) { - MutableElement m = chromosome.getRandomMutableElement(); + Mutable m = chromosome.getRandomMutable(); if (report.get()) getResources().report("[Mutator] Mutation " + i + " selected " + m + ", "); @@ -69,7 +69,7 @@ public abstract class PointMutator extends Mutator { if (report.get()) getResources().reportln("to " + ((Node) m).getFunction()); } else { // if we decided to mutate connection, subtract 1 from geneType so it fits into the arity range - geneType -= 1; + geneType--; if (report.get()) getResources().report("changed connection " + geneType + " from " + ((Node) m).getConnection(geneType) + " "); m.setConnection(geneType, chromosome.getRandomConnection(((Node) m).getColumn())); diff --git a/src/jcgp/backend/modules/problem/BestFitness.java b/src/jcgp/backend/modules/problem/BestFitness.java new file mode 100644 index 0000000..dce9ecd --- /dev/null +++ b/src/jcgp/backend/modules/problem/BestFitness.java @@ -0,0 +1,18 @@ +package jcgp.backend.modules.problem; + +/** + * Enum type to allow problems to indicate their fitness + * orientation. + *

+ * {@code BestFitness.HIGH} means high fitness values are + * better than low. Conversely, {@code BestFitness.LOW} + * signals that low fitness values indicate better fitness + * than high values. + * + * + * @author Eduardo Pedroni + * + */ +public enum BestFitness { + HIGH, LOW; +} diff --git a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java index 0071ed5..b615675 100644 --- a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java +++ b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java @@ -80,7 +80,7 @@ public class DigitalCircuitProblem extends TestCaseProblem { } @Override - public int perfectSolutionFound(Population population) { + public int hasPerfectSolution(Population population) { // higher fitness is better for (int i = 0; i < getResources().populationSize(); i++) { if (population.get(i).getFitness() >= maxFitness.get()) { @@ -89,16 +89,4 @@ public class DigitalCircuitProblem extends TestCaseProblem { } return -1; } - - @Override - public int hasImprovement(Population population) { - // higher fitness is better - 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; - } } diff --git a/src/jcgp/backend/modules/problem/Problem.java b/src/jcgp/backend/modules/problem/Problem.java index 5f194b5..2af2373 100644 --- a/src/jcgp/backend/modules/problem/Problem.java +++ b/src/jcgp/backend/modules/problem/Problem.java @@ -85,7 +85,7 @@ public abstract class Problem extends Module { * @param population the population to search through for a perfect chromosome. * @return the perfect solution index, if one exits, -1 if no perfect solution was found. */ - public abstract int perfectSolutionFound(Population population); + public abstract int hasPerfectSolution(Population population); /** * Used to assert whether a given population has a chromosome that is an improvement over diff --git a/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java b/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java index 24c61d6..3b5f539 100644 --- a/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java +++ b/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java @@ -126,7 +126,7 @@ public class SymbolicRegressionProblem extends TestCaseProblem { } @Override - public int perfectSolutionFound(Population population) { + public int hasPerfectSolution(Population population) { // higher fitness is better for (int i = 0; i < getResources().populationSize(); i++) { if (population.get(i).getFitness() >= maxFitness.get() - perfectionThreshold.get()) { @@ -135,18 +135,4 @@ public class SymbolicRegressionProblem extends TestCaseProblem { } return -1; } - - @Override - public int hasImprovement(Population population) { - // higher fitness is better - for (int i = 0; i < getResources().populationSize(); i++) { - System.out.println("checking for improvement"); - if (population.get(i).getFitness() > bestFitness.get()) { - System.out.println("found a better chr, " + i); - bestFitness.set(population.get(i).getFitness()); - return i; - } - } - return -1; - } } diff --git a/src/jcgp/backend/modules/problem/TestCaseProblem.java b/src/jcgp/backend/modules/problem/TestCaseProblem.java index 69c078d..188e236 100644 --- a/src/jcgp/backend/modules/problem/TestCaseProblem.java +++ b/src/jcgp/backend/modules/problem/TestCaseProblem.java @@ -5,6 +5,7 @@ import java.io.File; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import jcgp.backend.parsers.TestCaseParser; +import jcgp.backend.population.Population; import jcgp.backend.resources.ModifiableResources; import jcgp.backend.resources.Resources; @@ -162,6 +163,24 @@ public abstract class TestCaseProblem extends Problem { // use standard test case parser for this TestCaseParser.parse(file, this, resources); } + + @Override + public int hasImprovement(Population population) { + for (int i = 0; i < getResources().populationSize(); i++) { + if (getFitnessOrientation() == BestFitness.HIGH) { + if (population.get(i).getFitness() > bestFitness.get()) { + bestFitness.set(population.get(i).getFitness()); + return i; + } + } else { + if (population.get(i).getFitness() < bestFitness.get()) { + bestFitness.set(population.get(i).getFitness()); + return i; + } + } + } + return -1; + } } -- cgit v1.2.3