blob: e9be1533c72baeff79f5f72eab66d3a5c5522998 (
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
50
51
52
53
54
55
56
57
58
59
|
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 void activate(Resources parameters) {
// nothing
}
@Override
public HashMap<String, Parameter> getLocalParameters() {
return localParameters;
}
@Override
public String toString() {
return "Point mutation";
}
}
|