diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-08 14:48:25 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-08 14:48:25 +0000 |
commit | d63d3145f0f2abcee1bb88457324f4aaf9b9320e (patch) | |
tree | dfb19082adfba8989c4fd95ef286b8c1e7a1f2b1 /src/jcgp/modules/mutator | |
parent | ef7a850a8f2f81ccaa07f25d9b7ad602e84d88c9 (diff) |
Slowly refactoring Parameters to fit the GUI a little better...
Diffstat (limited to 'src/jcgp/modules/mutator')
-rw-r--r-- | src/jcgp/modules/mutator/Mutator.java | 9 | ||||
-rw-r--r-- | src/jcgp/modules/mutator/StandardMutator.java | 31 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/jcgp/modules/mutator/Mutator.java b/src/jcgp/modules/mutator/Mutator.java new file mode 100644 index 0000000..10df8cd --- /dev/null +++ b/src/jcgp/modules/mutator/Mutator.java @@ -0,0 +1,9 @@ +package jcgp.modules.mutator; + +import jcgp.population.Chromosome; + +public interface Mutator { + + void mutate(Chromosome chromosome); + +} diff --git a/src/jcgp/modules/mutator/StandardMutator.java b/src/jcgp/modules/mutator/StandardMutator.java new file mode 100644 index 0000000..17bd0be --- /dev/null +++ b/src/jcgp/modules/mutator/StandardMutator.java @@ -0,0 +1,31 @@ +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())); + } + } + } + } +} |