aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/modules/mutator/PointMutator.java
blob: 51a43903f7f9192d69260496d8e6cd6605c72d98 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
package jcgp.modules.mutator;

import java.util.HashMap;

import jcgp.parameters.DoubleParameter;
import jcgp.parameters.Parameter;
import jcgp.JCGP.Resources;
import jcgp.population.Chromosome;
import jcgp.population.MutableElement;
import jcgp.population.Node;
import jcgp.population.Output;

public class PointMutator implements Mutator {
	
	private Parameter mutationRate;
	private HashMap<String, Parameter> localParameters;
	
	public PointMutator() {
		mutationRate = new DoubleParameter(0.5, "Percent mutation");
		
		localParameters = new HashMap<String, Parameter>();
		localParameters.put("mutRate", mutationRate);
	}

	@Override
	public void mutate(Chromosome chromosome, Resources resources) {
		int mutations = (int) Math.ceil((((double) mutationRate.getValue()) * (((((Integer) resources.get("nodes")).doubleValue() + ((Integer) resources.get("outputs")).doubleValue())) / (double) 100)));
		for (int i = 0; i < mutations; i++) {
			MutableElement m = chromosome.getRandomMutableElement();
			
			if (m instanceof Output) {
				m.setConnection(0, chromosome.getRandomConnection());
			} else if (m instanceof Node) {
				int geneType = resources.getRandomInt(1 + ((int) resources.get("arity")));
				if (geneType < 1) {
					((Node) m).setFunction(resources.getRandomFunction());
				} else {
					m.setConnection(resources.getRandomInt((int) resources.get("arity")), chromosome.getRandomConnection(((Node) m).getColumn()));
				}
			}
		}
	}

	@Override
	public HashMap<String, Parameter> activate(Resources parameters) {
		return localParameters;
	}

}