aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/ea/StandardMutator.java
blob: 8af3f5ce15cce57e9a52038323a574b2b84a6664 (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
package jcgp.ea;

import jcgp.Parameters;
import jcgp.Utilities;
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() / 100));
		
		for (int i = 0; i < mutations; i++) {
			MutableElement m = Utilities.getRandomMutable(chromosome);
			
			if (m instanceof Output) {
				m.setConnection(Utilities.getRandomConnection(chromosome, m.getColumn()));
			} else if (m instanceof Node) {
				int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity());
				if (geneType < 1) {
					((Node) m).setFunction(Utilities.getRandomFunction());
				} else {
					m.setConnection(Utilities.getRandomConnection(chromosome, m.getColumn()));
				}
			}
		}
	}
}