diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-05-06 14:29:37 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-05-06 14:29:37 +0100 |
commit | 8189116ea4b5db4675e31dfd04a5687d55e29262 (patch) | |
tree | c1815021452a888f8838f1628d8fb4689777e73e /src/jcgp/backend/modules/mutator | |
parent | aa9e74e7f67789f6353fc26e02ee8e68e40609a2 (diff) |
Added javadocs, made minor changes to the comments
Diffstat (limited to 'src/jcgp/backend/modules/mutator')
5 files changed, 50 insertions, 36 deletions
diff --git a/src/jcgp/backend/modules/mutator/FixedPointMutator.java b/src/jcgp/backend/modules/mutator/FixedPointMutator.java index 5d40c57..1efdd2f 100644 --- a/src/jcgp/backend/modules/mutator/FixedPointMutator.java +++ b/src/jcgp/backend/modules/mutator/FixedPointMutator.java @@ -25,7 +25,7 @@ public class FixedPointMutator extends PointMutator { * @param resources a reference to the experiment's resources. */ public FixedPointMutator(final Resources resources) { - super(); + super(resources); genesMutated = new IntegerParameter(5, "Genes mutated", false, false) { @Override public void validate(Number newValue) { diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java index 02fd70a..56692ef 100644 --- a/src/jcgp/backend/modules/mutator/Mutator.java +++ b/src/jcgp/backend/modules/mutator/Mutator.java @@ -25,14 +25,22 @@ import jcgp.backend.resources.Resources; * */ public abstract class Mutator extends Module { + + /** + * For internal use only, initialises the resources field. + * + * @param resources the experiment's resources. + */ + protected Mutator(Resources resources) { + super(resources); + } /** * Applies mutations to the specified chromosome according * to the parameter values. * * @param chromosome the chromosome to mutate. - * @param resources parameters and utilities for optional reference. */ - public abstract void mutate(Chromosome chromosome, Resources resources); + public abstract void mutate(Chromosome chromosome); } diff --git a/src/jcgp/backend/modules/mutator/PercentPointMutator.java b/src/jcgp/backend/modules/mutator/PercentPointMutator.java index 31a7739..015edf4 100644 --- a/src/jcgp/backend/modules/mutator/PercentPointMutator.java +++ b/src/jcgp/backend/modules/mutator/PercentPointMutator.java @@ -24,12 +24,12 @@ public class PercentPointMutator extends PointMutator { private DoubleParameter mutationRate; /** - * Creates a new instance of PointMutator. + * Creates a new instance of PercentPointMutator. * * @param resources a reference to the experiment's resources. */ public PercentPointMutator(final Resources resources) { - super(); + super(resources); mutationRate = new DoubleParameter(10, "Percent mutation", false, false) { @Override public void validate(Number newValue) { diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 0223a37..9e421c2 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -24,48 +24,57 @@ public abstract class PointMutator extends Mutator { protected IntegerParameter genesMutated; protected BooleanParameter report; + + /** + * For internal use only, initialises the resources field. + * + * @param resources the experiment's resources. + */ + protected PointMutator(Resources resources) { + super(resources); + } @Override - public void mutate(Chromosome chromosome, Resources resources) { - if (report.get()) resources.reportln("[Mutator] Number of mutations to be performed: " + genesMutated.get()); + public void mutate(Chromosome chromosome) { + if (report.get()) getResources().reportln("[Mutator] Number of mutations to be performed: " + genesMutated.get()); // for however many genes must be mutated for (int i = 0; i < genesMutated.get(); i++) { MutableElement m = chromosome.getRandomMutableElement(); - if (report.get()) resources.report("[Mutator] Mutation " + i + " selected " + m + ", "); + if (report.get()) getResources().report("[Mutator] Mutation " + i + " selected " + m + ", "); // outputs and nodes are mutated differently if (m instanceof Output) { - if (report.get()) resources.report("changed source from " + ((Output) m).getSource() + " "); + 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()) resources.reportln("to " + ((Output) m).getSource()); + 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 = resources.getRandomInt(1 + resources.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()) resources.report("changed function from " + ((Node) m).getFunction() + " "); + if (report.get()) getResources().report("changed function from " + ((Node) m).getFunction() + " "); - ((Node) m).setFunction(resources.getRandomFunction()); + ((Node) m).setFunction(getResources().getRandomFunction()); - if (report.get()) resources.reportln("to " + ((Node) m).getFunction()); + 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 -= 1; - if (report.get()) resources.report("changed connection " + geneType + " from " + ((Node) m).getConnection(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()) resources.reportln("to " + ((Node) m).getConnection(geneType)); + if (report.get()) getResources().reportln("to " + ((Node) m).getConnection(geneType)); } } } diff --git a/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java b/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java index c65fc22..9273558 100644 --- a/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java +++ b/src/jcgp/backend/modules/mutator/ProbabilisticMutator.java @@ -25,17 +25,14 @@ public class ProbabilisticMutator extends Mutator { private DoubleParameter mutationProbability; private BooleanParameter report; - - private Resources resources; - + /** * Creates a new instance of ProbabilisticMutator. * * @param resources a reference to the experiment's resources. */ public ProbabilisticMutator(Resources resources) { - super(); - this.resources = resources; + super(resources); mutationProbability = new DoubleParameter(10, "Mutation probability", false, false) { @Override @@ -56,53 +53,53 @@ public class ProbabilisticMutator extends Mutator { } @Override - public void mutate(Chromosome chromosome, Resources resources) { - if (report.get()) resources.reportln("[Mutator] Starting mutations"); + public void mutate(Chromosome chromosome) { + if (report.get()) getResources().reportln("[Mutator] Starting mutations"); // go through nodes - [rows][columns] - for (int r = 0; r < resources.rows(); r++) { - for (int c = 0; c < resources.columns(); c++) { + for (int r = 0; r < getResources().rows(); r++) { + for (int c = 0; c < getResources().columns(); c++) { // go through all connections - for (int a = 0; a < resources.arity(); a++) { + for (int a = 0; a < getResources().arity(); a++) { if (mutateGene()) { Node n = chromosome.getNode(r, c); - if (report.get()) resources.report("[Mutator] Mutating " + n + + if (report.get()) getResources().report("[Mutator] Mutating " + n + ", changed connection " + a + " from " + n.getConnection(a) + " "); n.setConnection(a, chromosome.getRandomConnection(c)); - if (report.get()) resources.reportln("to " + n.getConnection(a)); + if (report.get()) getResources().reportln("to " + n.getConnection(a)); } } // deal with node function next if (mutateGene()) { Node n = chromosome.getNode(r, c); - if (report.get()) resources.report("[Mutator] Mutating " + n + + if (report.get()) getResources().report("[Mutator] Mutating " + n + ", changed function from " + n.getFunction()); - n.setFunction(resources.getRandomFunction()); + n.setFunction(getResources().getRandomFunction()); - if (report.get()) resources.reportln(" to " + n.getFunction()); + if (report.get()) getResources().reportln(" to " + n.getFunction()); } } } // finally, mutate outputs - for (int o = 0; o < resources.outputs(); o++) { + for (int o = 0; o < getResources().outputs(); o++) { if (mutateGene()) { Output out = chromosome.getOutput(o); - if (report.get()) resources.report("[Mutator] Mutating " + out + + if (report.get()) getResources().report("[Mutator] Mutating " + out + ", changed source from " + out.getSource()); out.setConnection(0, chromosome.getRandomConnection()); - if (report.get()) resources.reportln("to " + out.getSource()); + if (report.get()) getResources().reportln("to " + out.getSource()); } } - if (report.get()) resources.reportln("[Mutator] Mutation finished "); + if (report.get()) getResources().reportln("[Mutator] Mutation finished "); } @@ -115,6 +112,6 @@ public class ProbabilisticMutator extends Mutator { * @return true if a mutation should be performed, false if otherwise. */ private boolean mutateGene() { - return resources.getRandomDouble(100) < mutationProbability.get(); + return getResources().getRandomDouble(100) < mutationProbability.get(); } } |