aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/population
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-04-25 19:38:16 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-04-25 19:38:16 +0100
commitb0c0698e5503c2506217117bf144fde31e6f6601 (patch)
tree11a6e20fb565f1e75fb25852e757e4a37e4c313b /src/jcgp/backend/population
parent9ac2848be66c39acdc291dc3b48b91178acc1a05 (diff)
Commented lots of packages.
Diffstat (limited to 'src/jcgp/backend/population')
-rw-r--r--src/jcgp/backend/population/Chromosome.java20
-rw-r--r--src/jcgp/backend/population/Population.java35
2 files changed, 34 insertions, 21 deletions
diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java
index f8830fa..9e53f85 100644
--- a/src/jcgp/backend/population/Chromosome.java
+++ b/src/jcgp/backend/population/Chromosome.java
@@ -5,7 +5,7 @@ import java.util.ArrayList;
import jcgp.backend.exceptions.ParameterMismatchException;
import jcgp.backend.resources.Resources;
-public class Chromosome {
+public class Chromosome implements Comparable<Chromosome> {
private Resources resources;
@@ -276,7 +276,7 @@ public class Chromosome {
}
}
- public boolean compareTo(Chromosome chromosome) {
+ public boolean compareGenesTo(Chromosome chromosome) {
for (int r = 0; r < resources.rows(); r++) {
for (int c = 0; c < resources.columns(); c++) {
if (!(nodes[r][c].copyOf(chromosome.getNode(r, c)))) {
@@ -294,7 +294,7 @@ public class Chromosome {
return true;
}
- public boolean compareActiveTo(Chromosome chromosome) {
+ public boolean compareActiveGenesTo(Chromosome chromosome) {
// update list if it is out of date
computeActiveNodes();
@@ -310,7 +310,6 @@ public class Chromosome {
}
public void printNodes() {
- // TODO make this proper
int arity = resources.arity();
for (int r = 0; r < resources.rows(); r++) {
@@ -320,7 +319,7 @@ public class Chromosome {
for (int i = 0; i < arity; i++) {
System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).toString() + ") ");
}
- System.out.print("F: " + nodes[r][c].getFunction().getName() + "\t");
+ System.out.print("F: " + nodes[r][c].getFunction() + "\t");
}
System.out.print("\n");
}
@@ -335,4 +334,15 @@ public class Chromosome {
public Resources getResources() {
return resources;
}
+
+ @Override
+ public int compareTo(Chromosome o) {
+ if (fitness < o.getFitness()) {
+ return -1;
+ } else if (fitness > o.getFitness()) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
}
diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java
index b6dd055..d99c64e 100644
--- a/src/jcgp/backend/population/Population.java
+++ b/src/jcgp/backend/population/Population.java
@@ -1,12 +1,14 @@
package jcgp.backend.population;
+import java.util.Arrays;
+import java.util.Collections;
+
import jcgp.backend.resources.Resources;
public class Population {
private final Chromosome[] chromosomes;
private final Resources resources;
- private int fittest = 0;
/**
* Initialise a random population according to the parameters specified
@@ -77,25 +79,26 @@ public class Population {
chromosomes[c].reinitialiseConnections();
}
}
-
- public void setFittest(int fittest) {
- this.fittest = fittest;
- }
-
- public void setFittest(Chromosome fittest) {
- for (int i = 0; i < chromosomes.length; i++) {
- if (chromosomes[i] == fittest) {
- this.fittest = i;
- return;
- }
- }
- }
public Chromosome getFittest() {
- return chromosomes[fittest];
+ return chromosomes[chromosomes.length - 1];
}
public int getFittestIndex() {
- return fittest;
+ 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.
+ */
+ public void sortDescending() {
+ Arrays.sort(chromosomes, Collections.reverseOrder());
}
}