aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/mutator/PercentPointMutator.java
blob: 4057027c52d79aef17c1a866f191db2b537f5c3d (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
package jcgp.backend.modules.mutator;

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;

/**
 * Percent point mutator
 * <br><br>
 * This operator calculates how many genes to mutate based on the mutation rate
 * parameter. The total number of genes is computed from the number of nodes, 
 * the arity and the number of outputs. It then uses the point mutation 
 * algorithm to perform the required number of mutations.
 * 
 * 
 * @see PointMutator
 * @author Eduardo Pedroni
 *
 */
public class PercentPointMutator extends PointMutator {
	
	private DoubleParameter mutationRate;
	private IntegerParameter genesMutated;
	private BooleanParameter report;

	/**
	 * Creates a new instance of PointMutator. 
	 * 
	 * @param resources a reference to the experiment's resources.
	 */
	public PercentPointMutator(final Resources resources) {
		mutationRate = new DoubleParameter(10, "Percent mutation", false, false) {
			@Override
			public void validate(Number newValue) {
				genesMutated.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 (genesMutated.get() <=  0) {
					status = ParameterStatus.WARNING;
					status.setDetails("With mutation rate " + mutationRate.get() + ", 0 genes will be mutated.");
				} else {
					status = ParameterStatus.VALID;
				}
			}
		};
		genesMutated = 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 Parameter<?>[] getLocalParameters() {
		return new Parameter[] {mutationRate, genesMutated, report};
	}

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