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

import jcgp.backend.parameters.BooleanParameter;
import jcgp.backend.parameters.DoubleParameter;
import jcgp.backend.parameters.ParameterStatus;
import jcgp.backend.parameters.monitors.IntegerMonitor;
import jcgp.backend.resources.Resources;

/**
 * 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;

	/**
	 * Creates a new instance of PointMutator. 
	 * 
	 * @param resources a reference to the experiment's resources.
	 */
	public PercentPointMutator(final Resources resources) {
		super();
		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 IntegerMonitor(0, "Genes mutated");
		report = new BooleanParameter(false, "Report");
		
		setName("Percent point mutation");
		registerParameters(mutationRate, genesMutated, report);
	}
}