aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/es/MuPlusLambda.java
blob: 67236f97e438fd2344edc37f187c8bcb5dbe8b35 (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
94
95
96
97
98
99
100
101
102
103
104
105
package jcgp.backend.modules.es;

import jcgp.backend.modules.mutator.Mutator;
import jcgp.backend.population.Population;
import jcgp.backend.resources.Resources;
import jcgp.backend.resources.parameters.BooleanParameter;
import jcgp.backend.resources.parameters.IntegerParameter;
import jcgp.backend.resources.parameters.Parameter;
import jcgp.backend.resources.parameters.ParameterStatus;

/**
 * (μ + λ) EA.
 * 
 * 
 * @author Eduardo Pedroni
 *
 */
public class MuPlusLambda implements EvolutionaryStrategy {
	
	private int fittestChromosome;
	
	private IntegerParameter parents, offspring;
	private BooleanParameter report;
		
	public MuPlusLambda(final Resources resources) {
		parents = new IntegerParameter(1, "Parents") {
			@Override
			public void validate(Number newValue) {
				if (newValue.intValue() + offspring.get() != resources.populationSize()) {
					status = ParameterStatus.INVALID;
					status.setDetails("Parents + offspring must equal population size.");
				} else if (newValue.intValue() <= 0) {
					status = ParameterStatus.INVALID;
					status.setDetails("EA needs at least 1 parent.");
				} else {
					status = ParameterStatus.VALID;
				}
			}
		};
		offspring = new IntegerParameter(4, "Offspring") {
			@Override
			public void validate(Number newValue) {
				if (newValue.intValue() + parents.get() != resources.populationSize()) {
					status = ParameterStatus.INVALID;
					status.setDetails("Parents + offspring must equal population size.");
				} else if (newValue.intValue() <= 0) {
					status = ParameterStatus.INVALID;
					status.setDetails("EA needs at least 1 offspring.");
				} else {
					status = ParameterStatus.VALID;
				}
			}
		};
		report = new BooleanParameter(false, "Report") {
			@Override
			public void validate(Boolean newValue) {
				// nothing
			}
		};
	}
	
	@Override
	public void evolve(Population population, Mutator mutator, Resources resources) {
		// select fittest chromosomes
		fittestChromosome = 0;
		
		if (report.get()) resources.reportln("[ES] Selecting fittest chromosome...");
		for (int i = 0; i < resources.populationSize(); i++) {
			if (report.get()) resources.reportln("[ES] Chromosome " + i + ", fitness: " + population.getChromosome(i).getFitness());
			if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) {
				fittestChromosome = i;
			}
		}
		if (report.get()) resources.reportln("[ES] Selected chromosome: " + fittestChromosome);
		
		// create copies of fittest chromosome, mutate them
		for (int i = 0; i < resources.populationSize(); i++) {
			if (i != fittestChromosome) {
				if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i);
				population.copyChromosome(fittestChromosome, i);
				if (report.get()) resources.reportln("[ES] Mutating copied chromosome");
				mutator.mutate(population.getChromosome(i), resources);
			}
		}
		
		if (report.get()) resources.reportln("[ES] Generation is complete.");
		
	}

	@Override
	public int getFittestChromosome() {
		return fittestChromosome;
	}

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

	@Override
	public String toString() {
		return "(μ + λ)";
	}
	
}