diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-03 23:23:55 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-03 23:23:55 +0000 |
commit | d9671c354080de20bba4f70438af9242c8ecd675 (patch) | |
tree | f76736a4cda70950cb54aff56054095bbc9b5c22 /src/jcgp/population | |
parent | 2343cc0e456e0306711c0a7218d3027f17cffee7 (diff) |
Fixed FunctionSet issue, planned testbench evaluator interface, will implement tomorrow.
Diffstat (limited to 'src/jcgp/population')
-rw-r--r-- | src/jcgp/population/Chromosome.java | 98 | ||||
-rw-r--r-- | src/jcgp/population/Population.java | 23 |
2 files changed, 56 insertions, 65 deletions
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 76667e5..70e8836 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -1,14 +1,13 @@ package jcgp.population; -import java.util.ArrayList; - +import jcgp.CGP.Parameters; import jcgp.CGP.Utilities; public class Chromosome { - private final ArrayList<Input> inputs = new ArrayList<Input>(); - private final ArrayList<ArrayList<Node>> nodes = new ArrayList<ArrayList<Node>>();; - private final ArrayList<Output> outputs = new ArrayList<Output>(); + private Input[] inputs; + private Node[][] nodes; + private Output[] outputs; private int fitness = 0; @@ -29,25 +28,6 @@ public class Chromosome { } - private void initialiseConnections() { - - // initialise nodes - for (int r = 0; r < nodes.size(); r++) { - for (int c = 0; c < nodes.size(); c++) { - Connection[] connections = new Connection[Utilities.getMaxArity()]; - for (int i = 0; i < connections.length; i++) { - connections[i] = Utilities.getRandomConnection(this, c); - } - nodes.get(r).get(c).initialise(Utilities.getRandomFunction(), connections); - } - } - - for (Output output : outputs) { - output.setConnection(Utilities.getRandomNode(this)); - } - - } - /** * @param inputCount * @param rows @@ -55,58 +35,58 @@ public class Chromosome { * @param outputCount */ private void instantiateElements(int inputCount, int rows, int columns, int outputCount) { + inputs = new Input[inputCount]; for (int i = 0; i < inputCount; i++) { - inputs.add(new Input()); + inputs[i] = new Input(); } // rows first + nodes = new Node[Parameters.getRows()][Parameters.getColumns()]; for (int r = 0; r < rows; r++) { - nodes.add(new ArrayList<Node>(columns)); + //nodes[r] = new Node[Parameters.getColumns()]; for (int c = 0; c < columns; c++) { - nodes.get(r).add(new Node()); + nodes[r][c] = new Node(); } } - + outputs = new Output[outputCount]; for (int o = 0; o < outputCount; o++) { - outputs.add(new Output()); + outputs[o] = new Output(); + } + } + + private void initialiseConnections() { + + // initialise nodes - [rows][columns] + for (int r = 0; r < nodes.length; r++) { + for (int c = 0; c < nodes.length; c++) { + Connection[] connections = new Connection[Utilities.getMaxArity()]; + for (int i = 0; i < connections.length; i++) { + connections[i] = Utilities.getRandomConnection(this, c); + } + nodes[r][c].initialise(Utilities.getRandomFunction(), connections); + } + } + + for (Output output : outputs) { + output.setConnection(Utilities.getRandomNode(this)); } + } public int getActiveNodeCount() { return 0; } - /** - * @return the inputs - */ - public ArrayList<Input> getInputs() { - return inputs; - } - - /** - * @return the nodes - */ - public ArrayList<ArrayList<Node>> getNodes() { - return nodes; - } - - /** - * @return the outputs - */ - public ArrayList<Output> getOutputs() { - return outputs; - } - public Node getNode(int row, int column) { - return nodes.get(row).get(column); + return nodes[row][column]; } public Output getOutput(int index) { - return outputs.get(index); + return outputs[index]; } public Input getInput(int index) { - return inputs.get(index); + return inputs[index]; } public int getFitness() { @@ -117,4 +97,16 @@ 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 + 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."); + } + } + } diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java index 67b6695..55f756a 100644 --- a/src/jcgp/population/Population.java +++ b/src/jcgp/population/Population.java @@ -1,21 +1,20 @@ package jcgp.population; -import java.util.ArrayList; import java.util.Iterator; import jcgp.CGP.Parameters; -public final class Population implements Iterable<Chromosome> { +public class Population implements Iterable<Chromosome> { - private ArrayList<Chromosome> population; + private Chromosome[] population; public Population() { - population = new ArrayList<Chromosome>(Parameters.getPopulationSize()); - for (int c = 0; c < Parameters.getPopulationSize(); c++) { - population.add(new Chromosome(Parameters.getInputs(), - Parameters.getRows(), - Parameters.getColumns(), - Parameters.getOutputs())); + population = new Chromosome[Parameters.getPopulationSize()]; + for (int c = 0; c < population.length; c++) { + population[c] = new Chromosome(Parameters.getInputs(), + Parameters.getRows(), + Parameters.getColumns(), + Parameters.getOutputs()); } } @@ -27,7 +26,7 @@ public final class Population implements Iterable<Chromosome> { @Override public boolean hasNext() { - if (index < population.size()) { + if (index < population.length) { return true; } else { return false; @@ -36,14 +35,14 @@ public final class Population implements Iterable<Chromosome> { @Override public Chromosome next() { - Chromosome next = population.get(index); + Chromosome next = population[index]; index++; return next; } @Override public void remove() { - // not allowed + // not allowed } }; |