aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/mutator/PointMutator.java
blob: 44c453a830459dbdae7a5c14f71e0dd3ceb09907 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package jcgp.backend.modules.mutator;

import jcgp.backend.population.Chromosome;
import jcgp.backend.population.MutableElement;
import jcgp.backend.population.Node;
import jcgp.backend.population.Output;
import jcgp.backend.resources.Resources;
import jcgp.backend.resources.parameters.BooleanParameter;
import jcgp.backend.resources.parameters.DoubleParameter;
import jcgp.backend.resources.parameters.IntegerParameter;
import jcgp.backend.resources.parameters.Parameter;
import jcgp.backend.resources.parameters.ParameterStatus;

public class PointMutator implements Mutator {
	
	private DoubleParameter mutationRate;
	private IntegerParameter nodesMutated;
	private BooleanParameter report;

	public PointMutator(final Resources resources) {
		mutationRate = new DoubleParameter(50, "Percent mutation", false, false) {
			@Override
			public void validate(Number newValue) {
				nodesMutated.set((int) ((newValue.intValue()) * (((((double) resources.nodes() + resources.outputs()))) / 100)));
				if (newValue.doubleValue() <= 0 || newValue.doubleValue() > 100) {
					status = ParameterStatus.INVALID;
					status.setDetails("Mutation rate must be > 0 and <= 100");
				} else if ((int) ((newValue.doubleValue() / 100) * (double) resources.nodes()) <=  0) {
					status = ParameterStatus.WARNING;
					status.setDetails("With mutation rate " + mutationRate.get() + ", 0 genes will be mutated.");
				} else {
					status = ParameterStatus.VALID;
				}
			}
		};
		nodesMutated = new IntegerParameter(0, "Genes mutated", true, false) {
			@Override
			public void validate(Number newValue) {
				// blank
			}
		};
		report = new BooleanParameter(false, "Report") {
			@Override
			public void validate(Boolean newValue) {
				// blank
			}
		};
	}

	@Override
	public void mutate(Chromosome chromosome, Resources resources) {
		if (report.get()) resources.reportln("[Mutator] Number of mutations to be performed: " + nodesMutated.get());
		for (int i = 0; i < nodesMutated.get(); i++) {
			MutableElement m = chromosome.getRandomMutableElement();
			if (report.get()) resources.report("[Mutator] Mutation " + i + " selected " + m.toString() + ", ");
			
			if (m instanceof Output) {
				if (report.get()) resources.report("changed source from " + ((Output) m).getSource().toString() + " ");
				
				m.setConnection(0, chromosome.getRandomConnection());
				
				if (report.get()) resources.reportln("to " + ((Output) m).getSource().toString());
			} else if (m instanceof Node) {
				int geneType = resources.getRandomInt(1 + resources.arity());
				if (geneType < 1) {
					if (report.get()) resources.report("changed function from " + ((Node) m).getFunction().getName() + " ");
					
					((Node) m).setFunction(resources.getRandomFunction());
					
					if (report.get()) resources.reportln("to " + ((Node) m).getFunction().getName());
				} else {
					int connection = resources.getRandomInt(resources.arity());
					if (report.get()) resources.report("changed connection " + connection + " from " + ((Node) m).getConnection(connection) + " ");
					
					m.setConnection(connection, chromosome.getRandomConnection(((Node) m).getColumn()));
					
					if (report.get()) resources.reportln("to " + ((Node) m).getConnection(connection));
				}
			}
		}
	}

	@Override
	public Parameter<?>[] getLocalParameters() {
		return new Parameter[] {mutationRate, nodesMutated, report};
	}

	@Override
	public String toString() {
		return "Point mutation";
	}

}