From c0269683bcc7fde0d437ae84cd89a93d9d8fd81b Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 9 Mar 2014 23:32:05 +0000 Subject: Started refactoring backend in preparation for integration with the GUI --- src/jcgp/modules/mutator/PointMutator.java | 36 +++++++++++++++++++++++++++ src/jcgp/modules/mutator/StandardMutator.java | 31 ----------------------- 2 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 src/jcgp/modules/mutator/PointMutator.java delete mode 100644 src/jcgp/modules/mutator/StandardMutator.java (limited to 'src/jcgp/modules/mutator') diff --git a/src/jcgp/modules/mutator/PointMutator.java b/src/jcgp/modules/mutator/PointMutator.java new file mode 100644 index 0000000..b06d678 --- /dev/null +++ b/src/jcgp/modules/mutator/PointMutator.java @@ -0,0 +1,36 @@ +package jcgp.modules.mutator; + +import jcgp.Utilities; +import jcgp.parameters.IntegerParameter; +import jcgp.parameters.Parameters; +import jcgp.population.Chromosome; +import jcgp.population.MutableElement; +import jcgp.population.Node; +import jcgp.population.Output; + +public class PointMutator implements Mutator { + + public PointMutator() { + Parameters.add("mutRate", new IntegerParameter(10, "Mutation rate")); + } + + @Override + public void mutate(Chromosome chromosome) { + int mutations = (int) (((int) Parameters.get("mutRate").getValue()) * ((((double) Parameters.get("nodes").getValue()) + ((double) Parameters.get("outputs").getValue())) / 100)); + + for (int i = 0; i < mutations; i++) { + MutableElement m = chromosome.getRandomMutableElement(); + + if (m instanceof Output) { + m.setConnection(chromosome.getRandomConnection()); + } else if (m instanceof Node) { + int geneType = Utilities.getRandomInt(1 + ((int) Parameters.get("Max arity").getValue())); + if (geneType < 1) { + ((Node) m).setFunction(Utilities.getRandomFunction()); + } else { + m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn())); + } + } + } + } +} diff --git a/src/jcgp/modules/mutator/StandardMutator.java b/src/jcgp/modules/mutator/StandardMutator.java deleted file mode 100644 index 17bd0be..0000000 --- a/src/jcgp/modules/mutator/StandardMutator.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.modules.mutator; - -import jcgp.Utilities; -import jcgp.parameters.Parameters; -import jcgp.population.Chromosome; -import jcgp.population.MutableElement; -import jcgp.population.Node; -import jcgp.population.Output; - -public class StandardMutator implements Mutator { - - @Override - public void mutate(Chromosome chromosome) { - int mutations = (int) (Parameters.getMutationRate() * (((double) Parameters.getNodeCount() + Parameters.getOutputs()) / 100)); - - for (int i = 0; i < mutations; i++) { - MutableElement m = chromosome.getRandomMutableElement(); - - if (m instanceof Output) { - m.setConnection(chromosome.getRandomConnection()); - } else if (m instanceof Node) { - int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity()); - if (geneType < 1) { - ((Node) m).setFunction(Utilities.getRandomFunction()); - } else { - m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn())); - } - } - } - } -} -- cgit v1.2.3