From 6e7747e5b85f4ca93683ed5166f6e480cc58e6fa Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 10 Feb 2014 09:33:54 +0000 Subject: Refactored the resources mechanics, implemented a few of the chromosome tests --- src/jcgp/population/Chromosome.java | 26 ++++++++++++++++++-------- src/jcgp/population/Connection.java | 2 +- src/jcgp/population/Input.java | 2 +- src/jcgp/population/MutableElement.java | 2 ++ src/jcgp/population/Node.java | 18 ++++++++++++------ src/jcgp/population/Output.java | 14 ++++++++++++-- src/jcgp/population/Population.java | 2 +- 7 files changed, 47 insertions(+), 19 deletions(-) (limited to 'src/jcgp/population') diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index f060e14..328a608 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -1,7 +1,8 @@ package jcgp.population; -import jcgp.CGP.Parameters; -import jcgp.CGP.Utilities; +import jcgp.Parameters; +import jcgp.Utilities; +import jcgp.fitness.ParameterMismatchException; public class Chromosome { @@ -41,16 +42,16 @@ public class Chromosome { } // rows first - nodes = new Node[Parameters.getRows()][Parameters.getColumns()]; + nodes = new Node[rows][columns]; for (int r = 0; r < rows; r++) { //nodes[r] = new Node[Parameters.getColumns()]; for (int c = 0; c < columns; c++) { - nodes[r][c] = new Node(c); + nodes[r][c] = new Node(r, c); } } outputs = new Output[outputCount]; for (int o = 0; o < outputCount; o++) { - outputs[o] = new Output(); + outputs[o] = new Output(o); } } @@ -97,16 +98,25 @@ public class Chromosome { fitness = newFitness; } - public void setInputs(int ... values) { - // if the values provided dont match the specified number of inputs, the user should be warned + 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) { // set inputs for evaluation for (int i = 0; i < values.length; i++) { inputs[i].setValue(values[i]); } } else { - System.out.println("Input mismatch: chromosome has a different number of inputs than the truth table."); + throw new ParameterMismatchException(); } } + + public MutableElement getMutableElement(int row, int column) { + if (column < Parameters.getColumns() && column >= 0) { + return nodes[row][column]; + } else if (column == Parameters.getColumns()) { + return outputs[row]; + } + return null; + } } diff --git a/src/jcgp/population/Connection.java b/src/jcgp/population/Connection.java index fa02a22..4e69e99 100644 --- a/src/jcgp/population/Connection.java +++ b/src/jcgp/population/Connection.java @@ -2,5 +2,5 @@ package jcgp.population; public interface Connection { - public abstract int evaluate(); + public abstract int getValue(); } diff --git a/src/jcgp/population/Input.java b/src/jcgp/population/Input.java index b9c127f..e00573e 100644 --- a/src/jcgp/population/Input.java +++ b/src/jcgp/population/Input.java @@ -9,7 +9,7 @@ public class Input implements Connection { } @Override - public int evaluate() { + public int getValue() { return value; } diff --git a/src/jcgp/population/MutableElement.java b/src/jcgp/population/MutableElement.java index 8ac3724..c21ee0b 100644 --- a/src/jcgp/population/MutableElement.java +++ b/src/jcgp/population/MutableElement.java @@ -5,5 +5,7 @@ public interface MutableElement { public void setConnection(Connection newConnection); public int getColumn(); + + public int getRow(); } diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index fd0cd47..3dbabb2 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -1,7 +1,7 @@ package jcgp.population; -import jcgp.CGP.Parameters; -import jcgp.CGP.Utilities; +import jcgp.Parameters; +import jcgp.Utilities; import jcgp.function.Function; @@ -9,14 +9,15 @@ public class Node implements MutableElement, Connection { private Function function; private Connection[] connections; - private int column; + private int column, row; - public Node(int col) { - column = col; + public Node(int row, int column) { + this.column = column; + this.row = row; } @Override - public int evaluate() { + public int getValue() { return function.run(connections); } @@ -45,4 +46,9 @@ public class Node implements MutableElement, Connection { return column; } + + @Override + public int getRow() { + return row; + } } diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java index eeae743..ce4f776 100644 --- a/src/jcgp/population/Output.java +++ b/src/jcgp/population/Output.java @@ -1,14 +1,19 @@ package jcgp.population; -import jcgp.CGP.Parameters; +import jcgp.Parameters; public class Output implements MutableElement { private Connection source; + private int row; + + public Output(int row) { + this.row = row; + } public int calculate() { - return source.evaluate(); + return source.getValue(); } @Override @@ -22,4 +27,9 @@ public class Output implements MutableElement { return Parameters.getColumns(); } + @Override + public int getRow() { + return row; + } + } diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java index c4a0776..b56d992 100644 --- a/src/jcgp/population/Population.java +++ b/src/jcgp/population/Population.java @@ -2,7 +2,7 @@ package jcgp.population; import java.util.Iterator; -import jcgp.CGP.Parameters; +import jcgp.Parameters; public class Population implements Iterable { -- cgit v1.2.3