From b0c0698e5503c2506217117bf144fde31e6f6601 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 25 Apr 2014 19:38:16 +0100 Subject: Commented lots of packages. --- src/jcgp/backend/population/Chromosome.java | 20 ++++++++++++----- src/jcgp/backend/population/Population.java | 35 ++++++++++++++++------------- 2 files changed, 34 insertions(+), 21 deletions(-) (limited to 'src/jcgp/backend/population') 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 { 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()); } } -- cgit v1.2.3