aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/population
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-05-06 14:29:37 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-05-06 14:29:37 +0100
commit8189116ea4b5db4675e31dfd04a5687d55e29262 (patch)
treec1815021452a888f8838f1628d8fb4689777e73e /src/jcgp/backend/population
parentaa9e74e7f67789f6353fc26e02ee8e68e40609a2 (diff)
Added javadocs, made minor changes to the comments
Diffstat (limited to 'src/jcgp/backend/population')
-rw-r--r--src/jcgp/backend/population/Chromosome.java7
-rw-r--r--src/jcgp/backend/population/MutableElement.java2
-rw-r--r--src/jcgp/backend/population/Population.java64
3 files changed, 20 insertions, 53 deletions
diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java
index b99b817..0dfa801 100644
--- a/src/jcgp/backend/population/Chromosome.java
+++ b/src/jcgp/backend/population/Chromosome.java
@@ -159,7 +159,8 @@ public class Chromosome implements Comparable<Chromosome> {
* this instance. In practice, this iterates through the
* entire chromosome making equivalent connections and
* setting functions to the same values as those in the
- * specified chromosome.
+ * specified chromosome. It also sets the fitness of the
+ * copy to the same value as the original.
* <br>
* It is assumed that both chromosomes have the same
* topology; while this method will still run if that is not
@@ -205,6 +206,9 @@ public class Chromosome implements Comparable<Chromosome> {
System.out.println("Warning: Connection of subtype " + copyOutput.getClass().toString() + " is not explicitly handled by copy constructor.");
}
}
+
+ // copy fitness as well
+ this.fitness = clone.getFitness();
}
/**
@@ -264,7 +268,6 @@ public class Chromosome implements Comparable<Chromosome> {
* number of inputs exactly, an exception is thrown.
*
* @param values the values the input should take.
- * @throws ParameterMismatchException if the wrong number of values is received.
*/
public void setInputs(Object ... values) {
// if the values provided don't match the specified number of inputs, the user should be warned
diff --git a/src/jcgp/backend/population/MutableElement.java b/src/jcgp/backend/population/MutableElement.java
index 33f3890..5782a99 100644
--- a/src/jcgp/backend/population/MutableElement.java
+++ b/src/jcgp/backend/population/MutableElement.java
@@ -44,7 +44,7 @@ public interface MutableElement {
* <li>not reflexive: a.copyOf(a) returns false;</li>
* <li>not transitive: if a.copyOf(b) is true and b.copyOf(c) is true, a.copyOf(c) is
* not necessarily true since it is possible that a == c.</li>
- *
+ * </ul>
* @param element the mutable element to compare to.
* @return true if {@code element} is a copy of this element.
*/
diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java
index 5d549e9..70cfda8 100644
--- a/src/jcgp/backend/population/Population.java
+++ b/src/jcgp/backend/population/Population.java
@@ -3,6 +3,7 @@ package jcgp.backend.population;
import java.util.Arrays;
import java.util.Collections;
+import jcgp.backend.modules.problem.BestFitness;
import jcgp.backend.resources.Resources;
/**
@@ -19,16 +20,6 @@ import jcgp.backend.resources.Resources;
* experiment's specified seed. If an entirely random population
* is needed, {@code reinitialise()} should be used to randomise
* all chromosomes without creating a new instance of {@code Population}.
- * <br><br>
- * By convention the population's chromosomes should always be sorted in
- * order of fitness, from worst to best. This cannot be done automatically
- * since a higher fitness value does not necessarily mean better fitness;
- * some problem types might instead interpret fitness 0 as a perfect solution.
- * Sorting the population is done easily using {@code sortAscending()} and
- * {@code sortDescending()}, and should be done by the problem type as appropriate.
- * It is <b>critical</b> to sort the population after it is evaluated as
- * evolutionary strategies will obey the convention and assume the population
- * is sorted in order of fitness.
*
*
* @author Eduardo Pedroni
@@ -75,7 +66,7 @@ public class Population {
* @param index the chromosome to return.
* @return the indexed chromosome.
*/
- public Chromosome getChromosome(int index) {
+ public Chromosome get(int index) {
return chromosomes[index];
}
@@ -112,46 +103,19 @@ public class Population {
chromosomes[c].reinitialiseConnections();
}
}
-
- /**
- * The fittest chromosome, by convention, is the last one
- * in the array. Problem evaluation methods are expected to
- * sort the population into ascending order of fitness, such
- * that the best chromosome is in the last position (size - 1).
- * This method assumes that the population is sorted as such
- * and returns the last chromosome in the array.
- *
- * @return the fittest chromosome in the population.
- */
- public Chromosome getFittest() {
- return chromosomes[chromosomes.length - 1];
- }
-
- /**
- * The fittest chromosome, by convention, is the last one
- * in the array. Problem evaluation methods are expected to
- * sort the population into ascending order of fitness, such
- * that the best chromosome is in the last position (size - 1).
- * This method assumes that the population is sorted as such
- * and returns the last index in the array.
- *
- * @return the index of the fittest chromosome.
- */
- public int getFittestIndex() {
- return chromosomes.length - 1;
- }
-
- /**
- * Sort the population into ascending order of fitness.
- */
- public void sortAscending() {
- Arrays.sort(chromosomes);
- }
-
+
/**
- * Sort the population into descending order of fitness.
+ * Sorts the population in ascending order of fitness quality.
+ * What this means is that the best fitness chromosome will be
+ * in the last position, even though it might have the lowest
+ * fitness value. Fitness orientation as specified in the resources
+ * is respected.
*/
- public void sortDescending() {
- Arrays.sort(chromosomes, Collections.reverseOrder());
+ public void sort() {
+ if (resources.fitnessOrientation() == BestFitness.HIGH) {
+ Arrays.sort(chromosomes);
+ } else {
+ Arrays.sort(chromosomes, Collections.reverseOrder());
+ }
}
}