From c35a6806df01481c1b169cd0fc47660ea1cc10fb Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Wed, 14 May 2014 01:32:51 +0100 Subject: Refactored Mutable, commented a little bit of the GUI package --- src/jcgp/backend/modules/mutator/PointMutator.java | 42 +++------------------- .../modules/mutator/ProbabilisticMutator.java | 4 +-- 2 files changed, 7 insertions(+), 39 deletions(-) (limited to 'src/jcgp/backend/modules/mutator') diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 5aba0d1..6ba3e10 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -4,8 +4,6 @@ import jcgp.backend.parameters.BooleanParameter; import jcgp.backend.parameters.IntegerParameter; import jcgp.backend.population.Chromosome; import jcgp.backend.population.Mutable; -import jcgp.backend.population.Node; -import jcgp.backend.population.Output; import jcgp.backend.resources.Resources; /** @@ -40,43 +38,13 @@ public abstract class PointMutator extends Mutator { // for however many genes must be mutated for (int i = 0; i < genesMutated.get(); i++) { + // choose a random mutable + Mutable mutable = chromosome.getRandomMutable(); - Mutable m = chromosome.getRandomMutable(); + if (report.get()) getResources().report("[Mutator] Mutation " + i + " selected " + mutable); - if (report.get()) getResources().report("[Mutator] Mutation " + i + " selected " + m + ", "); - - // outputs and nodes are mutated differently - if (m instanceof Output) { - if (report.get()) getResources().report("changed source from " + ((Output) m).getSource() + " "); - - // outputs are easy, simply set to a different random connection, any will do - m.setConnection(0, chromosome.getRandomConnection()); - - if (report.get()) getResources().reportln("to " + ((Output) m).getSource()); - } else if (m instanceof Node) { - /* nodes are more complicated, first we must decide whether to mutate the function - * or a connection - * we do this by generating a random int between 0 and 1 + arity - */ - int geneType = getResources().getRandomInt(1 + getResources().arity()); - - // if the int is less than 1, mutate function, else mutate connections - if (geneType < 1) { - if (report.get()) getResources().report("changed function from " + ((Node) m).getFunction() + " "); - - ((Node) m).setFunction(getResources().getRandomFunction()); - - if (report.get()) getResources().reportln("to " + ((Node) m).getFunction()); - } else { - // if we decided to mutate connection, subtract 1 from geneType so it fits into the arity range - geneType--; - if (report.get()) getResources().report("changed connection " + geneType + " from " + ((Node) m).getConnection(geneType) + " "); - - m.setConnection(geneType, chromosome.getRandomConnection(((Node) m).getColumn())); - - if (report.get()) getResources().reportln("to " + ((Node) m).getConnection(geneType)); - } - } + // mutate a random gene + mutable.mutate(); } } diff --git a/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java b/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java index 9273558..1ea1373 100644 --- a/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java +++ b/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java @@ -93,13 +93,13 @@ public class ProbabilisticMutator extends Mutator { if (report.get()) getResources().report("[Mutator] Mutating " + out + ", changed source from " + out.getSource()); - out.setConnection(0, chromosome.getRandomConnection()); + out.setSource(chromosome.getRandomConnection()); if (report.get()) getResources().reportln("to " + out.getSource()); } } - if (report.get()) getResources().reportln("[Mutator] Mutation finished "); + if (report.get()) getResources().reportln("[Mutator] Mutation finished"); } -- cgit v1.2.3