package jcgp.population; import jcgp.Parameters; import jcgp.Utilities; import jcgp.fitness.ParameterMismatchException; public class Chromosome { private Input[] inputs; private Node[][] nodes; private Output[] outputs; private int fitness = 0; /** * Good citizen. * * @param outputs * @param columns * @param rows * @param inputs * */ public Chromosome(int inputCount, int rows, int columns, int outputCount) { instantiateElements(inputCount, rows, columns, outputCount); initialiseConnections(); } /** * @param inputCount * @param rows * @param columns * @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[i] = new Input(); } // rows first 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(r, c); } } outputs = new Output[outputCount]; for (int o = 0; o < outputCount; o++) { 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++) { Connection[] connections = new Connection[Parameters.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; } 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) { // set inputs for evaluation for (int i = 0; i < values.length; i++) { inputs[i].setValue(values[i]); } } else { 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; } }