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())); } } } } }