aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-03-09 23:32:05 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-03-09 23:32:05 +0000
commitc0269683bcc7fde0d437ae84cd89a93d9d8fd81b (patch)
tree62ef738e29ae310dff513cc44193c5169c4ea4ca /src/jcgp/population
parentd63d3145f0f2abcee1bb88457324f4aaf9b9320e (diff)
Started refactoring backend in preparation for integration with the GUI
Diffstat (limited to 'src/jcgp/population')
-rw-r--r--src/jcgp/population/Chromosome.java42
-rw-r--r--src/jcgp/population/Node.java4
-rw-r--r--src/jcgp/population/Output.java5
-rw-r--r--src/jcgp/population/Population.java50
4 files changed, 34 insertions, 67 deletions
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java
index df2a9d4..1f0b312 100644
--- a/src/jcgp/population/Chromosome.java
+++ b/src/jcgp/population/Chromosome.java
@@ -52,20 +52,20 @@ public class Chromosome {
*
*/
private void instantiateElements() {
- inputs = new Input[Parameters.getInputs()];
- for (int i = 0; i < Parameters.getInputs(); i++) {
+ inputs = new Input[((int) Parameters.get("inputs").getValue())];
+ for (int i = 0; i < ((int) Parameters.get("inputs").getValue()); i++) {
inputs[i] = new Input(i);
}
// rows first
- nodes = new Node[Parameters.getRows()][Parameters.getColumns()];
- for (int r = 0; r < Parameters.getRows(); r++) {
- for (int c = 0; c < Parameters.getColumns(); c++) {
+ nodes = new Node[((int) Parameters.get("rows").getValue())][((int) Parameters.get("columns").getValue())];
+ for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) {
+ for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) {
nodes[r][c] = new Node(this, r, c);
}
}
- outputs = new Output[Parameters.getOutputs()];
- for (int o = 0; o < Parameters.getOutputs(); o++) {
+ outputs = new Output[((int) Parameters.get("outputs").getValue())];
+ for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) {
outputs[o] = new Output(this, o);
}
}
@@ -78,7 +78,7 @@ public class Chromosome {
// initialise nodes - [rows][columns]
for (int r = 0; r < nodes.length; r++) {
for (int c = 0; c < nodes[r].length; c++) {
- Connection[] connections = new Connection[Parameters.getMaxArity()];
+ Connection[] connections = new Connection[((int) Parameters.get("maxArity").getValue())];
for (int i = 0; i < connections.length; i++) {
connections[i] = getRandomConnection(c);
}
@@ -100,7 +100,7 @@ public class Chromosome {
for (int r = 0; r < nodes.length; r++) {
for (int c = 0; c < nodes[r].length; c++) {
// make array of connections to initialise with
- Connection[] connections = new Connection[Parameters.getMaxArity()];
+ Connection[] connections = new Connection[((int) Parameters.get("maxArity").getValue())];
// populate with connections equivalent to clone
Connection copyConnection;
for (int i = 0; i < connections.length; i++) {
@@ -177,7 +177,7 @@ public class Chromosome {
*/
public MutableElement getRandomMutableElement() {
// choose output or node
- int index = Utilities.getRandomInt(outputs.length + Parameters.getNodeCount());
+ int index = Utilities.getRandomInt(outputs.length + ((int) Parameters.get("rows").getValue()) * ((int) Parameters.get("columns").getValue()));
if (index < outputs.length) {
// outputs
@@ -185,7 +185,7 @@ public class Chromosome {
} else {
// node
index -= outputs.length;
- return nodes[index / Parameters.getColumns()][index % Parameters.getColumns()];
+ return nodes[index / ((int) Parameters.get("columns").getValue())][index % ((int) Parameters.get("columns").getValue())];
}
}
@@ -199,7 +199,7 @@ public class Chromosome {
*/
public Connection getRandomConnection(int column) {
// work out the allowed range obeying levels back
- int allowedColumns = ((column >= Parameters.getLevelsBack()) ? Parameters.getLevelsBack() : column);
+ int allowedColumns = ((column >= ((int) Parameters.get("levelsBack").getValue())) ? ((int) Parameters.get("levelsBack").getValue()) : column);
int offset = ((column - allowedColumns) * nodes.length) - inputs.length;
// choose input or allowed node
@@ -226,7 +226,7 @@ public class Chromosome {
*/
public Connection getRandomConnection() {
// choose output or node
- int index = Utilities.getRandomInt(inputs.length + Parameters.getNodeCount());
+ int index = Utilities.getRandomInt(inputs.length + ((int) Parameters.get("columns").getValue()) * ((int) Parameters.get("rows").getValue()));
if (index < inputs.length) {
// outputs
@@ -234,7 +234,7 @@ public class Chromosome {
} else {
// node
index -= inputs.length;
- return nodes[index / Parameters.getColumns()][index % Parameters.getColumns()];
+ return nodes[index / ((int) Parameters.get("columns").getValue())][index % ((int) Parameters.get("columns").getValue())];
}
}
@@ -269,15 +269,15 @@ public class Chromosome {
}
public boolean compareTo(Chromosome chromosome) {
- for (int r = 0; r < Parameters.getRows(); r++) {
- for (int c = 0; c < Parameters.getColumns(); c++) {
+ for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) {
+ for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) {
if (!(nodes[r][c].copyOf(chromosome.getNode(r, c)))) {
return false;
}
}
}
- for (int o = 0; o < Parameters.getOutputs(); o++) {
+ for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) {
if (!(outputs[o].copyOf(chromosome.getOutput(o)))) {
return false;
}
@@ -302,11 +302,11 @@ public class Chromosome {
}
public void printNodes() {
- for (int r = 0; r < Parameters.getRows(); r++) {
+ for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) {
System.out.print("r: " + r + "\t");
- for (int c = 0; c < Parameters.getColumns(); c++) {
+ for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) {
System.out.print("N: (" + r + ", " + c + ") ");
- for (int i = 0; i < Parameters.getMaxArity(); i++) {
+ for (int i = 0; i < ((int) Parameters.get("maxArity").getValue()); i++) {
System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).getDescription() + ") ");
}
System.out.print("F: " + nodes[r][c].getFunction().toString() + "\t");
@@ -314,7 +314,7 @@ public class Chromosome {
System.out.print("\n");
}
- for (int o = 0; o < Parameters.getOutputs(); o++) {
+ for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) {
System.out.print("o: " + o + " (" + outputs[o].getSource().getDescription() + ")\t");
}
}
diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java
index 4203f25..9fafe32 100644
--- a/src/jcgp/population/Node.java
+++ b/src/jcgp/population/Node.java
@@ -20,7 +20,7 @@ public class Node implements MutableElement, Connection {
this.chromosome = chromosome;
this.column = column;
this.row = row;
- this.connections = new Connection[Parameters.getMaxArity()];
+ this.connections = new Connection[((int) Parameters.get("maxArity").getValue())];
}
@Override
@@ -44,7 +44,7 @@ public class Node implements MutableElement, Connection {
function = newFunction;
- if (newConnections.length >= Parameters.getMaxArity()) {
+ if (newConnections.length >= ((int) Parameters.get("maxArity").getValue())) {
connections = newConnections;
} else {
throw new InsufficientConnectionsException();
diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java
index 89ca13c..ccecae0 100644
--- a/src/jcgp/population/Output.java
+++ b/src/jcgp/population/Output.java
@@ -2,8 +2,6 @@ package jcgp.population;
import java.util.ArrayList;
-import jcgp.parameters.Parameters;
-
public class Output implements MutableElement {
private Connection source;
@@ -17,9 +15,6 @@ public class Output implements MutableElement {
public Object calculate() {
Object result = source.getValue();
- if (Parameters.getDebug()) {
- System.out.println("Output " + index + ": " + result);
- }
return result;
}
diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java
index 3e7b590..5718515 100644
--- a/src/jcgp/population/Population.java
+++ b/src/jcgp/population/Population.java
@@ -4,46 +4,26 @@ import jcgp.parameters.Parameters;
public class Population {
- private Chromosome[] parents;
- private Chromosome[] offspring;
+ private Chromosome[] chromosomes;
private Chromosome bestIndividual;
public Population(Chromosome parent) {
- parents = new Chromosome[Parameters.getParentCount()];
+ chromosomes = new Chromosome[((int) Parameters.get("population").getValue())];
// 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();
+ this.chromosomes[0] = new Chromosome(parent);
+ // generate the rest of the individuals
+ for (int c = 1; c < chromosomes.length; c++) {
+ chromosomes[c] = new Chromosome(chromosomes[0]);
}
}
public Population() {
- parents = new Chromosome[Parameters.getParentCount()];
- for (int c = 0; c < parents.length; c++) {
- parents[c] = new Chromosome();
- }
-
- offspring = new Chromosome[Parameters.getOffspringCount()];
- for (int c = 0; c < offspring.length; c++) {
- offspring[c] = new Chromosome();
+ chromosomes = new Chromosome[((int) Parameters.get("population").getValue())];
+ for (int c = 0; c < chromosomes.length; c++) {
+ chromosomes[c] = new Chromosome();
}
}
- public Chromosome getOffspring(int index) {
- return offspring[index];
- }
-
- public Chromosome getParent(int index) {
- return parents[index];
- }
-
/**
* Returns all chromosomes, parents first, then offspring.
*
@@ -51,19 +31,11 @@ public class Population {
* @return
*/
public Chromosome getChromosome(int index) {
- if (index < parents.length) {
- return parents[index];
- } else {
- return offspring[index - parents.length];
- }
+ return chromosomes[index];
}
public void setBestIndividual(int index) {
- if (index < parents.length) {
- bestIndividual = parents[index];
- } else {
- bestIndividual = offspring[index - parents.length];
- }
+ bestIndividual = chromosomes[index];
}
public Chromosome getBestIndividual() {