aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population/Chromosome.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/population/Chromosome.java')
-rw-r--r--src/jcgp/population/Chromosome.java63
1 files changed, 52 insertions, 11 deletions
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java
index 08ff9b9..f61b780 100644
--- a/src/jcgp/population/Chromosome.java
+++ b/src/jcgp/population/Chromosome.java
@@ -11,8 +11,8 @@ public class Chromosome {
private Input[] inputs;
private Node[][] nodes;
private Output[] outputs;
-
- private ArrayList<Connection> activeNodes;
+
+ private ArrayList<Node> activeNodes;
private int fitness = 0;
private boolean recomputeActiveNodes = true;
@@ -117,7 +117,7 @@ public class Chromosome {
nodes[r][c].initialise(clone.getNode(r, c).getFunction(), connections);
}
}
-
+
// do the same to outputs
Connection copyOutput;
for (int o = 0; o < outputs.length; o++) {
@@ -153,6 +153,11 @@ public class Chromosome {
fitness = newFitness;
}
+ /**
+ *
+ * @param values
+ * @throws ParameterMismatchException
+ */
public void setInputs(int ... values) throws ParameterMismatchException {
// if the values provided don't match the specified number of inputs, the user should be warned
if (values.length == inputs.length) {
@@ -232,31 +237,67 @@ public class Chromosome {
return nodes[index / Parameters.getColumns()][index % Parameters.getColumns()];
}
}
-
+
/**
* This causes the list of active nodes to be recomputed lazily (once it is actually requested).
*/
public void recomputeActiveNodes() {
recomputeActiveNodes = true;
}
-
+
/**
- * This method computes a list of active nodes (if necessary) and returns it.
+ * This method computes a list of active connections (if necessary) and returns it.
*
* @return
*/
- public ArrayList<Connection> getActiveNodes() {
+ public ArrayList<Node> getActiveNodes() {
+ computeActiveNodes();
+ return activeNodes;
+ }
+
+ private void computeActiveNodes() {
// lazy recomputation has been triggered, do it
if (recomputeActiveNodes) {
recomputeActiveNodes = false;
- activeNodes = new ArrayList<Connection>();
-
+ activeNodes = new ArrayList<Node>();
+
for (Output output : outputs) {
output.getActiveNodes(activeNodes);
}
-
}
- return activeNodes;
+
}
+ public boolean compareTo(Chromosome chromosome) {
+ for (int r = 0; r < Parameters.getRows(); r++) {
+ for (int c = 0; c < Parameters.getColumns(); c++) {
+ if (!(nodes[r][c].copyOf(chromosome.getNode(r, c)))) {
+ return false;
+ }
+ }
+ }
+
+ for (int o = 0; o < Parameters.getOutputs(); o++) {
+ if (!(outputs[o].copyOf(chromosome.getOutput(o)))) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public boolean compareActiveTo(Chromosome chromosome) {
+ // update list if it is out of date
+ computeActiveNodes();
+
+ if (activeNodes.size() == chromosome.getActiveNodes().size()) {
+ for (int i = 0; i < activeNodes.size(); i++) {
+ if (!(activeNodes.get(i).copyOf(chromosome.getActiveNodes().get(i)))){
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
}