diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-11 08:14:09 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-11 08:14:09 +0000 |
commit | d8ab8a2424a689cff4c787b0448c893facaadf6f (patch) | |
tree | d6a459a8f4808b3042acdf33f108f485ce4ddf7c | |
parent | 6e7747e5b85f4ca93683ed5166f6e480cc58e6fa (diff) |
Worked on more tests, finished restructuring resources
-rw-r--r-- | src/jcgp/Utilities.java | 2 | ||||
-rw-r--r-- | src/jcgp/population/Chromosome.java | 51 | ||||
-rw-r--r-- | src/jcgp/tests/ChromosomeTests.java | 10 |
3 files changed, 34 insertions, 29 deletions
diff --git a/src/jcgp/Utilities.java b/src/jcgp/Utilities.java index 7de701a..76f996e 100644 --- a/src/jcgp/Utilities.java +++ b/src/jcgp/Utilities.java @@ -132,6 +132,8 @@ public class Utilities { * * It will pick outputs or nodes fairly. * + * TODO probably remove this + * * @param chromosome the chromosome to pick from * @return a random mutable element */ 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; } - + } diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index 123cbd1..75e3596 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -22,10 +22,8 @@ import org.junit.Test; * Tests which cover the behaviour specified for a chromosome. * * - The chromosome should be able to return a specified node, input or output. - * - It should be able to return a MutableElement indexed by row and column, - * where column NodeCount returns outputs. - * - It should be able to return a Connection indexed by row and column, where - * column 0 returns inputs. + * - It should be able to return a random MutableElement. + * - It should be able to return a random Connection * - It should contain a freely modifiable fitness value. * - It should be a good citizen - fully initialised upon instantiation. * - It should feature a clone constructor, which creates a deep copy of a @@ -91,8 +89,8 @@ public class ChromosomeTests { && chromosome.getOutput(2).calculate() == 4 && chromosome.getOutput(0) instanceof Output; assertTrue("Incorrect output returned.", outputReturn); - // get a mutable element, that that it is a Mutable - boolean mutableReturn = chromosome.getMutable() instanceof Output; + // get a mutable element, check that it is a Mutable + boolean mutableReturn = chromosome.getRandomMutableElement() instanceof Output; // set a fitness value, check if returned value is the same chromosome.setFitness(10); |