From e9290a500b228e2561a8537adbc13e86f1356747 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sat, 15 Feb 2014 21:43:35 +0000 Subject: Population and Chromosome classes refactored so that EAs are easier to implement. --- src/jcgp/population/Chromosome.java | 4 +- src/jcgp/population/Connection.java | 1 + src/jcgp/population/Input.java | 1 - .../InsufficientConnectionsException.java | 10 --- src/jcgp/population/Node.java | 1 + src/jcgp/population/Output.java | 4 +- src/jcgp/population/Population.java | 96 +++++++++++++--------- 7 files changed, 64 insertions(+), 53 deletions(-) delete mode 100644 src/jcgp/population/InsufficientConnectionsException.java (limited to 'src/jcgp/population') diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 12a8978..3e61a10 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import jcgp.Parameters; import jcgp.Utilities; -import jcgp.fitness.ParameterMismatchException; +import jcgp.exceptions.ParameterMismatchException; public class Chromosome { @@ -95,7 +95,7 @@ public class Chromosome { /** * @param clone */ - private void copyConnections(Chromosome clone) { + public void copyConnections(Chromosome clone) { // copy nodes - [rows][columns] for (int r = 0; r < nodes.length; r++) { for (int c = 0; c < nodes[r].length; c++) { diff --git a/src/jcgp/population/Connection.java b/src/jcgp/population/Connection.java index ff11d7c..751fe10 100644 --- a/src/jcgp/population/Connection.java +++ b/src/jcgp/population/Connection.java @@ -3,5 +3,6 @@ package jcgp.population; public interface Connection { public Object getValue(); + } diff --git a/src/jcgp/population/Input.java b/src/jcgp/population/Input.java index 2154dd9..e793bf6 100644 --- a/src/jcgp/population/Input.java +++ b/src/jcgp/population/Input.java @@ -21,5 +21,4 @@ public class Input implements Connection { public int getIndex() { return index; } - } diff --git a/src/jcgp/population/InsufficientConnectionsException.java b/src/jcgp/population/InsufficientConnectionsException.java deleted file mode 100644 index 807ec53..0000000 --- a/src/jcgp/population/InsufficientConnectionsException.java +++ /dev/null @@ -1,10 +0,0 @@ -package jcgp.population; - -public class InsufficientConnectionsException extends RuntimeException { - - /** - * - */ - private static final long serialVersionUID = 660740514800883541L; - -} diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index e58c1a4..35d33cb 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -5,6 +5,7 @@ import java.util.Arrays; import jcgp.Parameters; import jcgp.Utilities; +import jcgp.exceptions.InsufficientConnectionsException; import jcgp.function.Function; diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java index dae7278..b3ce74f 100644 --- a/src/jcgp/population/Output.java +++ b/src/jcgp/population/Output.java @@ -14,7 +14,9 @@ public class Output implements MutableElement { } public Object calculate() { - return source.getValue(); + Object result = source.getValue(); + System.out.println("Output " + index + ": " + result); + return result; } @Override diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java index 4153e0f..9cd7430 100644 --- a/src/jcgp/population/Population.java +++ b/src/jcgp/population/Population.java @@ -1,54 +1,72 @@ package jcgp.population; -import java.util.Iterator; - import jcgp.Parameters; -public class Population implements Iterable { +public class Population { - private Chromosome[] population; + private Chromosome[] parents; + private Chromosome[] offspring; + private Chromosome bestIndividual; - public Population(Chromosome chromosome) { - population = new Chromosome[Parameters.getPopulationSize()]; - for (int c = 0; c < population.length; c++) { - population[c] = new Chromosome(chromosome); + public Population(Chromosome parent) { + parents = new Chromosome[Parameters.getParentCount()]; + // make a clone for safety + this.parents[0] = new Chromosome(parent); + // generate the rest of parents + for (int c = 1; c < parents.length; c++) { + parents[c] = new Chromosome(); + } + + offspring = new Chromosome[Parameters.getOffspringCount()]; + for (int c = 0; c < offspring.length; c++) { + // completely random offspring? depending on EA, this means the given parent won't be selected + offspring[c] = new Chromosome(); } } public Population() { - population = new Chromosome[Parameters.getPopulationSize()]; - for (int c = 0; c < population.length; c++) { - population[c] = new Chromosome(); + parents = new Chromosome[Parameters.getParentCount()]; + for (int c = 0; c < parents.length; c++) { + parents[c] = new Chromosome(); } - } - - @Override - public Iterator iterator() { - return new Iterator() { - - private int index = 0; - - @Override - public boolean hasNext() { - if (index < population.length) { - return true; - } else { - return false; - } - } - @Override - public Chromosome next() { - Chromosome next = population[index]; - index++; - return next; - } + offspring = new Chromosome[Parameters.getOffspringCount()]; + for (int c = 0; c < offspring.length; c++) { + offspring[c] = new Chromosome(); + } + } - @Override - public void remove() { - // not allowed - throw new UnsupportedOperationException("Removing chromosomes from the population is not allowed. Instead, re-instantiate the chromosome."); - } - }; + public Chromosome getOffspring(int index) { + return offspring[index]; + } + + public Chromosome getParent(int index) { + return parents[index]; + } + + /** + * Returns all chromosomes, parents first, then offspring. + * + * @param index + * @return + */ + public Chromosome getChromosome(int index) { + if (index < parents.length) { + return parents[index]; + } else { + return offspring[index - parents.length]; + } + } + + public void setBestIndividual(int index) { + if (index < parents.length) { + bestIndividual = parents[index]; + } else { + bestIndividual = offspring[index - parents.length]; + } + } + + public Chromosome getBestIndividual() { + return bestIndividual; } } -- cgit v1.2.3