aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/problem/Problem.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/modules/problem/Problem.java')
-rw-r--r--src/jcgp/backend/modules/problem/Problem.java57
1 files changed, 36 insertions, 21 deletions
diff --git a/src/jcgp/backend/modules/problem/Problem.java b/src/jcgp/backend/modules/problem/Problem.java
index 721b9b3..5f194b5 100644
--- a/src/jcgp/backend/modules/problem/Problem.java
+++ b/src/jcgp/backend/modules/problem/Problem.java
@@ -6,23 +6,21 @@ import jcgp.backend.function.FunctionSet;
import jcgp.backend.modules.Module;
import jcgp.backend.parameters.DoubleParameter;
import jcgp.backend.parameters.monitors.DoubleMonitor;
-import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Population;
import jcgp.backend.resources.ModifiableResources;
import jcgp.backend.resources.Resources;
/**
- * Defines the general behaviour of a CGP problem. The primary function of Problem
+ * Defines the general behaviour of a CGP problem. The primary function of {@code Problem}
* is to evaluate a population and assign a fitness value to each chromosome.
* <br>
- * By convention, the population should be sorted into ascending order of fitness. The
- * reason for this is because high fitness is not necessarily better - some problem types
- * might treat 0 as the best fitness. In order for the evolutionary strategy to be able to
- * pick chromosomes by fitness, the safest way is to sort them such that the last chromosome
- * is the fittest.
+ * Problems are free to define whether better fitness means a higher or lower fitness value.
+ * In some problem types, it is more convenient to treat fitness 0 as the best possible value.
+ * This can be done by changing the fitness orientation to {@code BestFitness.HIGH} or {@code BestFitness.LOW} as appropriate.
+ * Fitness orientation is set to high by default.
* <br><br>
- * When extending this class, the constructor should call a couple methods in order to
- * properly construct the problem type: {@code setFunctionSet()} and {@code setFileExtension()},
+ * When extending this class, the constructor should call a few methods in order to
+ * properly construct the problem type: {@code setFunctionSet()}, {@code setFileExtension()} and {@code setFitnessOrientation()},
* with the respective arguments. As with all subclasses of {@code Module}, {@code setName()} and
* {@code registerParameters()} should be used where appropriate as well.
* <br><br>
@@ -41,12 +39,18 @@ public abstract class Problem extends Module {
private FunctionSet functionSet;
private String fileExtension = ".*";
+ private BestFitness fitnessOrientation = BestFitness.HIGH;
+
protected DoubleParameter maxFitness, bestFitness;
/**
* Initialises the two problem-wide parameters, maxFitness and bestFitness.
+ *
+ * @param resources a reference to the experiment's resources.
*/
- public Problem() {
+ public Problem(Resources resources) {
+ super(resources);
+
maxFitness = new DoubleMonitor(0, "Max fitness");
bestFitness = new DoubleMonitor(0, "Best fitness");
registerParameters(maxFitness, bestFitness);
@@ -73,29 +77,26 @@ public abstract class Problem extends Module {
public abstract void evaluate(Population population, Resources resources);
/**
- * Used to assert whether a given chromosome is a perfect solution
+ * Used to assert whether a given population contains a perfect solution
* to the problem. It is up to the problem to define what qualifies
* a perfect solution, as some problems (subject ones such as music and
* art evolution, for example) might not have perfect solutions at all.
- * <br><br>
- * Note that if this method returns true, the experiment will move on
- * to the next run, or finish if no more runs are left.
*
- * @param candidate the potentially perfect chromosome.
- * @return true if the argument is a perfect solution.
+ * @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 boolean isPerfectSolution(Chromosome candidate);
+ public abstract int perfectSolutionFound(Population population);
/**
- * Used to assert whether a given chromosome is an improvement over
+ * Used to assert whether a given population has a chromosome that is an improvement over
* the current best chromosome. A typical implementation of this method
* will simply compare chromosome fitness values, though the problem type
* is free to implement this in any way.
*
- * @param candidate the potentially fitter chromosome.
- * @return true if the argument is fitter than the currently fittest chromosome.
+ * @param population the population potentially containing a fitter chromosome.
+ * @return the index of the first chromosome in the population that is an improvement, -1 if none is found.
*/
- public abstract boolean isImprovement(Chromosome candidate);
+ public abstract int hasImprovement(Population population);
/**
* Parses the specified file and uses the parsed data to
@@ -149,6 +150,20 @@ public abstract class Problem extends Module {
}
/**
+ * @param newOrientation the new fitness orientation to set.
+ */
+ protected void setFitnessOrientation(BestFitness newOrientation) {
+ this.fitnessOrientation = newOrientation;
+ }
+
+ /**
+ * @return the fitness orientation of this particular problem.
+ */
+ public BestFitness getFitnessOrientation() {
+ return fitnessOrientation;
+ }
+
+ /**
* @return the current best fitness, in other words, the fitness
* value of the fittest chromosome in the current generation.
*/