From d9671c354080de20bba4f70438af9242c8ecd675 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 3 Feb 2014 23:23:55 +0000 Subject: Fixed FunctionSet issue, planned testbench evaluator interface, will implement tomorrow. --- src/jcgp/population/Chromosome.java | 98 +++++++++++++++++-------------------- src/jcgp/population/Population.java | 23 +++++---- 2 files changed, 56 insertions(+), 65 deletions(-) (limited to 'src/jcgp/population') 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 inputs = new ArrayList(); - private final ArrayList> nodes = new ArrayList>();; - private final ArrayList outputs = new ArrayList(); + 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(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 getInputs() { - return inputs; - } - - /** - * @return the nodes - */ - public ArrayList> getNodes() { - return nodes; - } - - /** - * @return the outputs - */ - public ArrayList 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 { +public class Population implements Iterable { - private ArrayList population; + private Chromosome[] population; public Population() { - population = new ArrayList(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 { @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 { @Override public Chromosome next() { - Chromosome next = population.get(index); + Chromosome next = population[index]; index++; return next; } @Override public void remove() { - // not allowed + // not allowed } }; -- cgit v1.2.3