aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-02-15 21:43:35 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-02-15 21:45:21 +0000
commite9290a500b228e2561a8537adbc13e86f1356747 (patch)
tree5e76280ddefc5bf9dcf9cdb55e80ca4831998115 /src/jcgp/population
parentafa484021ba94d12e98da682a9ff69c3837d5dbb (diff)
Population and Chromosome classes refactored so that EAs are easier to implement.
Diffstat (limited to 'src/jcgp/population')
-rw-r--r--src/jcgp/population/Chromosome.java4
-rw-r--r--src/jcgp/population/Connection.java1
-rw-r--r--src/jcgp/population/Input.java1
-rw-r--r--src/jcgp/population/InsufficientConnectionsException.java10
-rw-r--r--src/jcgp/population/Node.java1
-rw-r--r--src/jcgp/population/Output.java4
-rw-r--r--src/jcgp/population/Population.java96
7 files changed, 64 insertions, 53 deletions
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<Chromosome> {
+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<Chromosome> iterator() {
- return new Iterator<Chromosome>() {
-
- 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;
}
}