From 8f7874fa75c532bab994af8e6553d37afe42ec4c Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 30 Jan 2014 21:41:33 +0000 Subject: Chromosome data structure partially refactored, also added higher level classes. --- src/jcgp/CGP.java | 12 +++++ src/jcgp/chromosome/Chromosome.java | 66 ++++++++++++++++++++++++- src/jcgp/chromosome/Population.java | 16 ++++++ src/jcgp/chromosome/element/MutableElement.java | 7 --- src/jcgp/chromosome/element/Node.java | 18 +++---- src/jcgp/chromosome/element/Output.java | 14 ------ src/jcgp/mutator/Mutator.java | 15 ++++++ 7 files changed, 113 insertions(+), 35 deletions(-) create mode 100644 src/jcgp/CGP.java create mode 100644 src/jcgp/chromosome/Population.java create mode 100644 src/jcgp/mutator/Mutator.java diff --git a/src/jcgp/CGP.java b/src/jcgp/CGP.java new file mode 100644 index 0000000..d2cae80 --- /dev/null +++ b/src/jcgp/CGP.java @@ -0,0 +1,12 @@ +package jcgp; + +public class CGP { + + public static int ROWS, COLUMNS, INPUTS, OUTPUTS, MUTATION_RATE, GENERATIONS, RUNS; + + public CGP() { + + } + + +} diff --git a/src/jcgp/chromosome/Chromosome.java b/src/jcgp/chromosome/Chromosome.java index 19d7e77..cdf2e4b 100644 --- a/src/jcgp/chromosome/Chromosome.java +++ b/src/jcgp/chromosome/Chromosome.java @@ -2,16 +2,78 @@ package jcgp.chromosome; import java.util.ArrayList; -import jcgp.chromosome.element.ChromosomeElement; +import jcgp.CGP; +import jcgp.chromosome.element.Input; +import jcgp.chromosome.element.Node; +import jcgp.chromosome.element.Output; public class Chromosome { - private ArrayList> nodes; + private ArrayList inputs; + private ArrayList> nodes; + private ArrayList outputs; + /** + * Good citizen. + * + */ public Chromosome() { + inputs = new ArrayList(CGP.INPUTS); + for (int i = 0; i < CGP.INPUTS; i++) { + inputs.add(new Input()); + } + // rows first + nodes = new ArrayList>(CGP.ROWS); + for (int r = 0; r < CGP.ROWS; r++) { + nodes.add(new ArrayList(CGP.COLUMNS)); + for (int c = 0; c < CGP.COLUMNS; c++) { + nodes.get(r).add(new Node()); + } + } + outputs = new ArrayList(CGP.OUTPUTS); + for (int o = 0; o < CGP.OUTPUTS; o++) { + outputs.add(new Output()); + } + } + + public int getActiveNodeCount() { + return 0; + } + + /** + * @return the inputs + */ + public final ArrayList getInputs() { + return inputs; + } + + /** + * @return the nodes + */ + public final ArrayList> getNodes() { + return nodes; + } + + /** + * @return the outputs + */ + public final ArrayList 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); } } diff --git a/src/jcgp/chromosome/Population.java b/src/jcgp/chromosome/Population.java new file mode 100644 index 0000000..e720a24 --- /dev/null +++ b/src/jcgp/chromosome/Population.java @@ -0,0 +1,16 @@ +package jcgp.chromosome; + +import java.util.ArrayList; + +public class Population { + + private ArrayList population; + + public Population(int size) { + population = new ArrayList(size); + for (int c = 0; c < size; c++) { + population.add(new Chromosome()); + } + } + +} diff --git a/src/jcgp/chromosome/element/MutableElement.java b/src/jcgp/chromosome/element/MutableElement.java index ed7e23c..0122c69 100644 --- a/src/jcgp/chromosome/element/MutableElement.java +++ b/src/jcgp/chromosome/element/MutableElement.java @@ -1,12 +1,5 @@ package jcgp.chromosome.element; -import jcgp.chromosome.Chromosome; -import jcgp.chromosome.functions.FunctionSet; - public interface MutableElement { - public void initialise(Chromosome chromosome, FunctionSet functions); - - public void mutate(Chromosome chromosome, FunctionSet functions); - } diff --git a/src/jcgp/chromosome/element/Node.java b/src/jcgp/chromosome/element/Node.java index f97c680..761c3c9 100644 --- a/src/jcgp/chromosome/element/Node.java +++ b/src/jcgp/chromosome/element/Node.java @@ -1,22 +1,16 @@ package jcgp.chromosome.element; -import jcgp.chromosome.Chromosome; -import jcgp.chromosome.functions.FunctionSet; +import jcgp.chromosome.functions.Function; -public class Node extends ChromosomeElement implements MutableElement { - @Override - public void initialise(Chromosome chromosome, FunctionSet functions) { - // TODO Auto-generated method stub - - } +public class Node extends ChromosomeElement implements MutableElement { + + private Function function; - @Override - public void mutate(Chromosome chromosome, FunctionSet functions) { - // TODO Auto-generated method stub + public Node() { } - + @Override public int evaluate() { // TODO Auto-generated method stub diff --git a/src/jcgp/chromosome/element/Output.java b/src/jcgp/chromosome/element/Output.java index 261e77d..ee4d204 100644 --- a/src/jcgp/chromosome/element/Output.java +++ b/src/jcgp/chromosome/element/Output.java @@ -1,22 +1,8 @@ package jcgp.chromosome.element; -import jcgp.chromosome.Chromosome; -import jcgp.chromosome.functions.FunctionSet; public class Output extends ChromosomeElement implements MutableElement { - @Override - public void initialise(Chromosome chromosome, FunctionSet functions) { - // TODO Auto-generated method stub - - } - - @Override - public void mutate(Chromosome chromosome, FunctionSet functions) { - // TODO Auto-generated method stub - - } - @Override public int evaluate() { // TODO Auto-generated method stub diff --git a/src/jcgp/mutator/Mutator.java b/src/jcgp/mutator/Mutator.java new file mode 100644 index 0000000..577aa42 --- /dev/null +++ b/src/jcgp/mutator/Mutator.java @@ -0,0 +1,15 @@ +package jcgp.mutator; + +import jcgp.chromosome.Population; + +public class Mutator { + + public Mutator() { + + } + + public void mutate(Population population, int rate) { + + } + +} -- cgit v1.2.3