diff options
Diffstat (limited to 'src/jcgp/population')
-rw-r--r-- | src/jcgp/population/Chromosome.java | 89 | ||||
-rw-r--r-- | src/jcgp/population/Connection.java | 6 | ||||
-rw-r--r-- | src/jcgp/population/Input.java | 16 | ||||
-rw-r--r-- | src/jcgp/population/MutableElement.java | 5 | ||||
-rw-r--r-- | src/jcgp/population/Node.java | 28 | ||||
-rw-r--r-- | src/jcgp/population/Output.java | 11 | ||||
-rw-r--r-- | src/jcgp/population/Population.java | 46 |
7 files changed, 201 insertions, 0 deletions
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java new file mode 100644 index 0000000..2e22cf9 --- /dev/null +++ b/src/jcgp/population/Chromosome.java @@ -0,0 +1,89 @@ +package jcgp.population; + +import java.util.ArrayList; + +public class Chromosome { + + private ArrayList<Input> inputs; + private ArrayList<ArrayList<Node>> nodes; + private ArrayList<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) { + + inputs = new ArrayList<Input>(inputCount); + for (int i = 0; i < inputCount; i++) { + inputs.add(new Input()); + } + + // rows first + nodes = new ArrayList<ArrayList<Node>>(rows); + for (int r = 0; r < rows; r++) { + nodes.add(new ArrayList<Node>(columns)); + for (int c = 0; c < columns; c++) { + nodes.get(r).add(new Node()); + } + } + + outputs = new ArrayList<Output>(outputCount); + for (int o = 0; o < outputCount; o++) { + outputs.add(new Output()); + } + + } + + public int getActiveNodeCount() { + return 0; + } + + /** + * @return the inputs + */ + public final ArrayList<Input> getInputs() { + return inputs; + } + + /** + * @return the nodes + */ + public final ArrayList<ArrayList<Node>> getNodes() { + return nodes; + } + + /** + * @return the outputs + */ + public final ArrayList<Output> getOutputs() { + return outputs; + } + + public final Node getNode(int row, int column) { + return nodes.get(row).get(column); + } + + public final Output getOutput(int index) { + return outputs.get(index); + } + + public final Input getInputs(int index) { + return inputs.get(index); + } + + public int getFitness() { + return fitness; + } + + public void setFitness(int newFitness) { + fitness = newFitness; + } + +} diff --git a/src/jcgp/population/Connection.java b/src/jcgp/population/Connection.java new file mode 100644 index 0000000..fa02a22 --- /dev/null +++ b/src/jcgp/population/Connection.java @@ -0,0 +1,6 @@ +package jcgp.population; + +public interface Connection { + + public abstract int evaluate(); +} diff --git a/src/jcgp/population/Input.java b/src/jcgp/population/Input.java new file mode 100644 index 0000000..b9c127f --- /dev/null +++ b/src/jcgp/population/Input.java @@ -0,0 +1,16 @@ +package jcgp.population; + +public class Input implements Connection { + + private int value = 0; + + public void setValue(int newValue) { + value = newValue; + } + + @Override + public int evaluate() { + return value; + } + +} diff --git a/src/jcgp/population/MutableElement.java b/src/jcgp/population/MutableElement.java new file mode 100644 index 0000000..4397e46 --- /dev/null +++ b/src/jcgp/population/MutableElement.java @@ -0,0 +1,5 @@ +package jcgp.population; + +public interface MutableElement { + +} diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java new file mode 100644 index 0000000..cce8dfd --- /dev/null +++ b/src/jcgp/population/Node.java @@ -0,0 +1,28 @@ +package jcgp.population; + +import jcgp.function.Function; + + +public class Node implements MutableElement, Connection { + + private Function function; + private Connection[] connections; + + public Node() { + + } + + @Override + public int evaluate() { + return function.run(connections[0], connections[1]); + } + + public void setFunction(Function newFunction) { + function = newFunction; + } + + public void setConnection(Connection newConnection) { + + } + +} diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java new file mode 100644 index 0000000..2f2df6e --- /dev/null +++ b/src/jcgp/population/Output.java @@ -0,0 +1,11 @@ +package jcgp.population; + + +public class Output implements MutableElement { + + public int calculate() { + // TODO Auto-generated method stub + return 0; + } + +} diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java new file mode 100644 index 0000000..e1a9a3c --- /dev/null +++ b/src/jcgp/population/Population.java @@ -0,0 +1,46 @@ +package jcgp.population; + +import java.util.ArrayList; +import java.util.Iterator; + +public final class Population implements Iterable<Chromosome> { + + private ArrayList<Chromosome> population; + + public Population(int inputs, int rows, int columns, int outputs, int size) { + population = new ArrayList<Chromosome>(size); + for (int c = 0; c < size; c++) { + population.add(new Chromosome(inputs, rows, columns, outputs)); + } + } + + @Override + public Iterator<Chromosome> iterator() { + return new Iterator<Chromosome>() { + + private int index = 0; + + @Override + public boolean hasNext() { + if (index < population.size()) { + return true; + } else { + return false; + } + } + + @Override + public Chromosome next() { + Chromosome next = population.get(index); + index++; + return next; + } + + @Override + public void remove() { + // not allowed + } + + }; + } +} |