From d8ab8a2424a689cff4c787b0448c893facaadf6f Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 11 Feb 2014 08:14:09 +0000 Subject: Worked on more tests, finished restructuring resources --- src/jcgp/population/Chromosome.java | 51 ++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'src/jcgp/population') diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 328a608..195b95c 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -9,9 +9,9 @@ public class Chromosome { private Input[] inputs; private Node[][] nodes; private Output[] outputs; - + private int fitness = 0; - + /** * Good citizen. * @@ -22,11 +22,11 @@ public class Chromosome { * */ public Chromosome(int inputCount, int rows, int columns, int outputCount) { - + instantiateElements(inputCount, rows, columns, outputCount); - + initialiseConnections(); - + } /** @@ -40,7 +40,7 @@ public class Chromosome { for (int i = 0; i < inputCount; i++) { inputs[i] = new Input(); } - + // rows first nodes = new Node[rows][columns]; for (int r = 0; r < rows; r++) { @@ -54,9 +54,9 @@ public class Chromosome { outputs[o] = new Output(o); } } - + private void initialiseConnections() { - + // initialise nodes - [rows][columns] for (int r = 0; r < nodes.length; r++) { for (int c = 0; c < nodes.length; c++) { @@ -67,13 +67,13 @@ public class Chromosome { nodes[r][c].initialise(Utilities.getRandomFunction(), connections); } } - + for (Output output : outputs) { output.setConnection(Utilities.getRandomNode(this)); } - + } - + public int getActiveNodeCount() { return 0; } @@ -81,23 +81,23 @@ public class Chromosome { public Node getNode(int row, int column) { return nodes[row][column]; } - + public Output getOutput(int index) { return outputs[index]; } - + public Input getInput(int index) { return inputs[index]; } - + public int getFitness() { return fitness; } - + public void setFitness(int newFitness) { fitness = newFitness; } - + 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) { @@ -110,13 +110,18 @@ public class Chromosome { } } - 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]; + public MutableElement getRandomMutableElement() { + // choose output or node + int index = Utilities.getRandomInt(outputs.length + Parameters.getNodeCount()); + + if (index < outputs.length) { + // outputs + return outputs[index]; + } else { + // node + index -= outputs.length; + return nodes[index / Parameters.getColumns()][index % Parameters.getColumns()]; } - return null; } - + } -- cgit v1.2.3