From 226365bd6a0f56143ffbce7379857ff3e126fbaf Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 7 Apr 2014 15:58:30 +0100 Subject: Refactoring resources for (hopefully) the last time --- src/jcgp/backend/modules/mutator/Mutator.java | 2 +- src/jcgp/backend/modules/mutator/PointMutator.java | 32 ++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'src/jcgp/backend/modules/mutator') diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java index ffdd35c..1d5b99a 100644 --- a/src/jcgp/backend/modules/mutator/Mutator.java +++ b/src/jcgp/backend/modules/mutator/Mutator.java @@ -6,6 +6,6 @@ import jcgp.backend.resources.Resources; public interface Mutator extends Module { - void mutate(Chromosome chromosome, Resources parameters); + void mutate(Chromosome chromosome, Resources resources); } diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 54d5f3d..1f38cfe 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -1,11 +1,11 @@ package jcgp.backend.modules.mutator; -import jcgp.backend.function.Function; import jcgp.backend.population.Chromosome; import jcgp.backend.population.MutableElement; import jcgp.backend.population.Node; import jcgp.backend.population.Output; import jcgp.backend.resources.Resources; +import jcgp.backend.resources.parameters.BooleanParameter; import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.Parameter; import jcgp.backend.resources.parameters.ParameterStatus; @@ -13,6 +13,7 @@ import jcgp.backend.resources.parameters.ParameterStatus; public class PointMutator implements Mutator { private DoubleParameter mutationRate; + private BooleanParameter report; public PointMutator(final Resources resources) { mutationRate = new DoubleParameter(50, "Percent mutation", false, false) { @@ -29,23 +30,44 @@ public class PointMutator implements Mutator { } } }; + + report = new BooleanParameter(false, "Report") { + @Override + public void validate(boolean newValue) { + // blank + } + }; } @Override public void mutate(Chromosome chromosome, Resources resources) { int mutations = (int) ((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("outputs")))) / 100)); + if (report.get()) resources.reportln("[Mutator] Number of mutations to be performed: " + mutations); for (int i = 0; i < mutations; i++) { MutableElement m = chromosome.getRandomMutableElement(); + if (report.get()) resources.report("[Mutator] Mutation " + i + " selected " + m.toString() + ", "); if (m instanceof Output) { + if (report.get()) resources.report("changed source from " + ((Output) m).getSource().toString() + " "); + m.setConnection(0, chromosome.getRandomConnection()); + + if (report.get()) resources.reportln("to " + ((Output) m).getSource().toString()); } else if (m instanceof Node) { int geneType = resources.getRandomInt(1 + resources.getInt("arity")); if (geneType < 1) { - Function f = resources.getRandomFunction(); - ((Node) m).setFunction(f); + if (report.get()) resources.report("changed function from " + ((Node) m).getFunction().getName() + " "); + + ((Node) m).setFunction(resources.getRandomFunction()); + + if (report.get()) resources.reportln("to " + ((Node) m).getFunction().getName()); } else { - m.setConnection(resources.getRandomInt(resources.getInt("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); + int connection = resources.getRandomInt(resources.getInt("arity")); + if (report.get()) resources.report("changed connection " + connection + " from " + ((Node) m).getConnection(connection) + " "); + + m.setConnection(connection, chromosome.getRandomConnection(((Node) m).getColumn())); + + if (report.get()) resources.reportln("to " + ((Node) m).getConnection(connection)); } } } @@ -53,7 +75,7 @@ public class PointMutator implements Mutator { @Override public Parameter[] getLocalParameters() { - return new Parameter[] {mutationRate}; + return new Parameter[] {mutationRate, report}; } @Override -- cgit v1.2.3