From 36f4393bcc9e55afa2334baa33e603ce839741a1 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 1 May 2014 13:05:27 +0100 Subject: Did more commenting, implemented reflection and statistics --- .../backend/modules/mutator/FixedPointMutator.java | 35 ++++++------------- src/jcgp/backend/modules/mutator/Mutator.java | 10 +++--- .../modules/mutator/PercentPointMutator.java | 40 ++++++---------------- src/jcgp/backend/modules/mutator/PointMutator.java | 6 ++-- .../modules/mutator/ProbabilisticMutator.java | 33 ++++++------------ 5 files changed, 40 insertions(+), 84 deletions(-) (limited to 'src/jcgp/backend/modules/mutator') diff --git a/src/jcgp/backend/modules/mutator/FixedPointMutator.java b/src/jcgp/backend/modules/mutator/FixedPointMutator.java index 4088918..5d40c57 100644 --- a/src/jcgp/backend/modules/mutator/FixedPointMutator.java +++ b/src/jcgp/backend/modules/mutator/FixedPointMutator.java @@ -1,10 +1,9 @@ package jcgp.backend.modules.mutator; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.ParameterStatus; import jcgp.backend.resources.Resources; -import jcgp.backend.resources.parameters.BooleanParameter; -import jcgp.backend.resources.parameters.IntegerParameter; -import jcgp.backend.resources.parameters.Parameter; -import jcgp.backend.resources.parameters.ParameterStatus; /** * Fixed point mutator @@ -20,16 +19,14 @@ import jcgp.backend.resources.parameters.ParameterStatus; */ public class FixedPointMutator extends PointMutator { - private IntegerParameter geneMutated; - private BooleanParameter report; - /** * Creates a new instance of FixedPointMutator. * * @param resources a reference to the experiment's resources. */ public FixedPointMutator(final Resources resources) { - geneMutated = new IntegerParameter(5, "Genes mutated", false, false) { + super(); + genesMutated = new IntegerParameter(5, "Genes mutated", false, false) { @Override public void validate(Number newValue) { if (newValue.intValue() <= 0) { @@ -43,22 +40,10 @@ public class FixedPointMutator extends PointMutator { } } }; - report = new BooleanParameter(false, "Report") { - @Override - public void validate(Boolean newValue) { - // blank - } - }; + + report = new BooleanParameter(false, "Report"); + + setName("Fixed point mutation"); + registerParameters(genesMutated, report); } - - @Override - public Parameter[] getLocalParameters() { - return new Parameter[] {geneMutated, report}; - } - - @Override - public String toString() { - return "Fixed point mutation"; - } - } diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java index 7435cc1..02fd70a 100644 --- a/src/jcgp/backend/modules/mutator/Mutator.java +++ b/src/jcgp/backend/modules/mutator/Mutator.java @@ -5,13 +5,13 @@ import jcgp.backend.population.Chromosome; import jcgp.backend.resources.Resources; /** - * This interface specifies the required behaviour of a mutation operator. Its job is + * This class specifies the basic characteristics of a mutation operator. Its job is * to modify the connections and functions of the chromosome according to the operator's * parameters. *

* Parameters may be specified to control the implemented mutation. Any parameters - * returned by {@code getLocalParameters()} should be displayed by the user interface, - * if it is being used. See {@link Parameter} for more information. + * registered with {@code registerParameters()} should be displayed by the user interface, + * if it is being used. See {@link Module} for more information. *

* It is advisable to use {@code Resources.reportln()} and {@code Resources.report()} * to print any relevant information. Note that reportln() and report() are affected @@ -24,7 +24,7 @@ import jcgp.backend.resources.Resources; * @author Eduardo Pedroni * */ -public interface Mutator extends Module { +public abstract class Mutator extends Module { /** * Applies mutations to the specified chromosome according @@ -33,6 +33,6 @@ public interface Mutator extends Module { * @param chromosome the chromosome to mutate. * @param resources parameters and utilities for optional reference. */ - void mutate(Chromosome chromosome, Resources resources); + public abstract void mutate(Chromosome chromosome, Resources resources); } diff --git a/src/jcgp/backend/modules/mutator/PercentPointMutator.java b/src/jcgp/backend/modules/mutator/PercentPointMutator.java index 4057027..31a7739 100644 --- a/src/jcgp/backend/modules/mutator/PercentPointMutator.java +++ b/src/jcgp/backend/modules/mutator/PercentPointMutator.java @@ -1,11 +1,10 @@ package jcgp.backend.modules.mutator; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.backend.parameters.ParameterStatus; +import jcgp.backend.parameters.monitors.IntegerMonitor; import jcgp.backend.resources.Resources; -import jcgp.backend.resources.parameters.BooleanParameter; -import jcgp.backend.resources.parameters.DoubleParameter; -import jcgp.backend.resources.parameters.IntegerParameter; -import jcgp.backend.resources.parameters.Parameter; -import jcgp.backend.resources.parameters.ParameterStatus; /** * Percent point mutator @@ -23,8 +22,6 @@ import jcgp.backend.resources.parameters.ParameterStatus; public class PercentPointMutator extends PointMutator { private DoubleParameter mutationRate; - private IntegerParameter genesMutated; - private BooleanParameter report; /** * Creates a new instance of PointMutator. @@ -32,6 +29,7 @@ public class PercentPointMutator extends PointMutator { * @param resources a reference to the experiment's resources. */ public PercentPointMutator(final Resources resources) { + super(); mutationRate = new DoubleParameter(10, "Percent mutation", false, false) { @Override public void validate(Number newValue) { @@ -47,27 +45,11 @@ public class PercentPointMutator extends PointMutator { } } }; - genesMutated = new IntegerParameter(0, "Genes mutated", true, false) { - @Override - public void validate(Number newValue) { - // blank - } - }; - report = new BooleanParameter(false, "Report") { - @Override - public void validate(Boolean newValue) { - // blank - } - }; - } - - @Override - public Parameter[] getLocalParameters() { - return new Parameter[] {mutationRate, genesMutated, report}; - } - - @Override - public String toString() { - return "Percent point mutation"; + + genesMutated = new IntegerMonitor(0, "Genes mutated"); + report = new BooleanParameter(false, "Report"); + + setName("Percent point mutation"); + registerParameters(mutationRate, genesMutated, report); } } diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 17c6996..0223a37 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -1,12 +1,12 @@ package jcgp.backend.modules.mutator; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.IntegerParameter; 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.IntegerParameter; /** * Point mutator @@ -20,7 +20,7 @@ import jcgp.backend.resources.parameters.IntegerParameter; * @author Eduardo Pedroni * */ -public abstract class PointMutator implements Mutator { +public abstract class PointMutator extends Mutator { protected IntegerParameter genesMutated; protected BooleanParameter report; diff --git a/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java b/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java index cacb451..c65fc22 100644 --- a/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java +++ b/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java @@ -1,13 +1,12 @@ package jcgp.backend.modules.mutator; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.backend.parameters.ParameterStatus; import jcgp.backend.population.Chromosome; 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; /** * Probabilistic mutator @@ -22,7 +21,7 @@ import jcgp.backend.resources.parameters.ParameterStatus; * @author Eduardo Pedroni * */ -public class ProbabilisticMutator implements Mutator { +public class ProbabilisticMutator extends Mutator { private DoubleParameter mutationProbability; private BooleanParameter report; @@ -35,6 +34,7 @@ public class ProbabilisticMutator implements Mutator { * @param resources a reference to the experiment's resources. */ public ProbabilisticMutator(Resources resources) { + super(); this.resources = resources; mutationProbability = new DoubleParameter(10, "Mutation probability", false, false) { @@ -48,17 +48,11 @@ public class ProbabilisticMutator implements Mutator { } } }; - report = new BooleanParameter(false, "Report") { - @Override - public void validate(Boolean newValue) { - // blank - } - }; - } - - @Override - public Parameter[] getLocalParameters() { - return new Parameter[] {mutationProbability, report}; + + report = new BooleanParameter(false, "Report"); + + setName("Probabilisic mutation"); + registerParameters(mutationProbability, report); } @Override @@ -113,7 +107,7 @@ public class ProbabilisticMutator implements Mutator { } /** - * This method offers a shorthand to decide whether a mutation should occur or not. + * This method provides a shorthand to decide whether a mutation should occur or not. * A random double is generated in the range 0 <= x < 100 and compared with the * mutation probability parameter. If the generated number is less than the mutation * probability, this returns true meaning a mutation should occur. @@ -123,9 +117,4 @@ public class ProbabilisticMutator implements Mutator { private boolean mutateGene() { return resources.getRandomDouble(100) < mutationProbability.get(); } - - @Override - public String toString() { - return "Probabilistic mutation"; - } } -- cgit v1.2.3