aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/population
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/population')
-rw-r--r--src/jcgp/backend/population/Chromosome.java52
-rw-r--r--src/jcgp/backend/population/Node.java2
-rw-r--r--src/jcgp/backend/population/Population.java14
3 files changed, 36 insertions, 32 deletions
diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java
index bbada14..71e19ec 100644
--- a/src/jcgp/backend/population/Chromosome.java
+++ b/src/jcgp/backend/population/Chromosome.java
@@ -31,7 +31,7 @@ public class Chromosome {
// allocate memory for all elements of the chromosome
instantiateElements();
// set random connections so that the chromosome can be evaluated
- initialiseConnections();
+ reinitialiseConnections();
}
/**
@@ -55,22 +55,22 @@ public class Chromosome {
*
*/
private void instantiateElements() {
- inputs = new Input[(resources.getInt("inputs"))];
- for (int i = 0; i < (resources.getInt("inputs")); i++) {
+ inputs = new Input[(resources.inputs())];
+ for (int i = 0; i < inputs.length; i++) {
inputs[i] = new Input(i);
}
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
// rows first
- nodes = new Node[(resources.getInt("rows"))][(resources.getInt("columns"))];
- for (int r = 0; r < (resources.getInt("rows")); r++) {
- for (int c = 0; c < (resources.getInt("columns")); c++) {
+ nodes = new Node[(resources.rows())][(resources.columns())];
+ for (int r = 0; r < nodes.length; r++) {
+ for (int c = 0; c < nodes[r].length; c++) {
nodes[r][c] = new Node(this, r, c, arity);
}
}
- outputs = new Output[(resources.getInt("outputs"))];
- for (int o = 0; o < (resources.getInt("outputs")); o++) {
+ outputs = new Output[resources.outputs()];
+ for (int o = 0; o < outputs.length; o++) {
outputs[o] = new Output(this, o);
}
}
@@ -78,9 +78,9 @@ public class Chromosome {
/**
*
*/
- private void initialiseConnections() {
+ public void reinitialiseConnections() {
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
// initialise nodes - [rows][columns]
for (int r = 0; r < nodes.length; r++) {
@@ -103,7 +103,7 @@ public class Chromosome {
* @param clone
*/
public void copyGenes(Chromosome clone) {
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
// copy nodes - [rows][columns]
for (int r = 0; r < nodes.length; r++) {
@@ -164,6 +164,7 @@ public class Chromosome {
/**
*
+ *
* @param values
* @throws ParameterMismatchException
*/
@@ -186,7 +187,7 @@ public class Chromosome {
*/
public MutableElement getRandomMutableElement() {
// choose output or node
- int index = resources.getRandomInt(outputs.length + (resources.getInt("rows")) * (resources.getInt("columns")));
+ int index = resources.getRandomInt(outputs.length + (resources.rows() * resources.columns()));
if (index < outputs.length) {
// outputs
@@ -194,7 +195,7 @@ public class Chromosome {
} else {
// node
index -= outputs.length;
- return nodes[index / (resources.getInt("columns"))][index % (resources.getInt("columns"))];
+ return nodes[index / resources.columns()][index % resources.columns()];
}
}
@@ -208,7 +209,7 @@ public class Chromosome {
*/
public Connection getRandomConnection(int column) {
// work out the allowed range obeying levels back
- int allowedColumns = ((column >= (resources.getInt("levelsBack"))) ? (resources.getInt("levelsBack")) : column);
+ int allowedColumns = column >= resources.levelsBack() ? resources.levelsBack() : column;
int offset = ((column - allowedColumns) * nodes.length) - inputs.length;
// choose input or allowed node
@@ -235,14 +236,14 @@ public class Chromosome {
*/
public Connection getRandomConnection() {
// choose output or node
- int index = resources.getRandomInt(inputs.length + (resources.getInt("columns")) * (resources.getInt("rows")));
+ int index = resources.getRandomInt(inputs.length + (resources.columns() * resources.rows()));
if (index < inputs.length) {
// outputs
return inputs[index];
} else {
// node
index -= inputs.length;
- return nodes[index / (resources.getInt("columns"))][index % (resources.getInt("columns"))];
+ return nodes[index / resources.columns()][index % resources.columns()];
}
}
@@ -273,19 +274,18 @@ public class Chromosome {
output.getActiveNodes(activeNodes);
}
}
-
}
public boolean compareTo(Chromosome chromosome) {
- for (int r = 0; r < (resources.getInt("rows")); r++) {
- for (int c = 0; c < (resources.getInt("columns")); c++) {
+ 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)))) {
return false;
}
}
}
- for (int o = 0; o < (resources.getInt("outputs")); o++) {
+ for (int o = 0; o < resources.outputs(); o++) {
if (!(outputs[o].copyOf(chromosome.getOutput(o)))) {
return false;
}
@@ -311,11 +311,11 @@ public class Chromosome {
public void printNodes() {
// TODO make this proper
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
- for (int r = 0; r < (resources.getInt("rows")); r++) {
+ for (int r = 0; r < resources.rows(); r++) {
System.out.print("r: " + r + "\t");
- for (int c = 0; c < (resources.getInt("columns")); c++) {
+ for (int c = 0; c < resources.columns(); c++) {
System.out.print("N: (" + r + ", " + c + ") ");
for (int i = 0; i < arity; i++) {
System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).toString() + ") ");
@@ -325,9 +325,11 @@ public class Chromosome {
System.out.print("\n");
}
- for (int o = 0; o < (resources.getInt("outputs")); o++) {
+ for (int o = 0; o < resources.outputs(); o++) {
System.out.print("o: " + o + " (" + outputs[o].getSource().toString() + ")\t");
}
+
+ System.out.println();
}
public Resources getResources() {
diff --git a/src/jcgp/backend/population/Node.java b/src/jcgp/backend/population/Node.java
index 87a2f99..6a558a4 100644
--- a/src/jcgp/backend/population/Node.java
+++ b/src/jcgp/backend/population/Node.java
@@ -37,7 +37,7 @@ public class Node extends Gene implements MutableElement, Connection {
public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException {
function = newFunction;
- if (newConnections.length == chromosome.getResources().getInt("arity")) {
+ if (newConnections.length == chromosome.getResources().arity()) {
connections = newConnections;
} else {
throw new InsufficientConnectionsException();
diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java
index d2e6058..fdeec82 100644
--- a/src/jcgp/backend/population/Population.java
+++ b/src/jcgp/backend/population/Population.java
@@ -5,7 +5,7 @@ import jcgp.backend.resources.Resources;
public class Population {
private Chromosome[] chromosomes;
- private Resources resources;
+ private final Resources resources;
/**
* Initialise a random population according to the parameters specified
@@ -16,7 +16,7 @@ public class Population {
public Population(Resources resources) {
this.resources = resources;
- chromosomes = new Chromosome[(resources.getInt("popSize"))];
+ chromosomes = new Chromosome[resources.populationSize()];
for (int c = 0; c < chromosomes.length; c++) {
chromosomes[c] = new Chromosome(resources);
}
@@ -31,8 +31,7 @@ public class Population {
public Population(Chromosome parent, Resources resources) {
this.resources = resources;
- chromosomes = new Chromosome[(resources.getInt("popSize"))];
- // generate the rest of the individuals
+ chromosomes = new Chromosome[resources.populationSize()];
for (int c = 0; c < chromosomes.length; c++) {
chromosomes[c] = new Chromosome(parent);
}
@@ -72,7 +71,10 @@ public class Population {
}
}
-
-
+ public void reinitialise() {
+ for (int c = 0; c < chromosomes.length; c++) {
+ chromosomes[c].reinitialiseConnections();
+ }
+ }
}