aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/modules/mutator/PointMutator.java
blob: b06d6787faf7469cd1e9ac9ffc9871630e7dfd33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package jcgp.modules.mutator;

import jcgp.Utilities;
import jcgp.parameters.IntegerParameter;
import jcgp.parameters.Parameters;
import jcgp.population.Chromosome;
import jcgp.population.MutableElement;
import jcgp.population.Node;
import jcgp.population.Output;

public class PointMutator implements Mutator {

	public PointMutator() {
		Parameters.add("mutRate", new IntegerParameter(10, "Mutation rate"));
	}
	
	@Override
	public void mutate(Chromosome chromosome) {
		int mutations = (int) (((int) Parameters.get("mutRate").getValue()) * ((((double) Parameters.get("nodes").getValue()) + ((double) Parameters.get("outputs").getValue())) / 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 + ((int) Parameters.get("Max arity").getValue()));
				if (geneType < 1) {
					((Node) m).setFunction(Utilities.getRandomFunction());
				} else {
					m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn()));
				}
			}
		}
	}
}