blob: 1d27004390aca01483f728c8c0ea14f92a4ec24c (
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
|
package jcgp.modules.ea;
import java.util.HashMap;
import jcgp.JCGP.Resources;
import jcgp.modules.mutator.Mutator;
import jcgp.parameters.IntegerParameter;
import jcgp.parameters.Parameter;
import jcgp.population.Chromosome;
import jcgp.population.Population;
/**
* (μ + λ) EA.
*
*
* @author Eduardo Pedroni
*
*/
public class StandardEA implements EvolutionaryAlgorithm {
private Chromosome fittestChromosome;
private IntegerParameter parents, offspring;
private HashMap<String, Parameter> localParameters;
public StandardEA() {
parents = new IntegerParameter(1, "Parents");
offspring = new IntegerParameter(4, "Offspring");
localParameters = new HashMap<String, Parameter>();
localParameters.put("mu", parents);
localParameters.put("lambda", offspring);
}
@Override
public void evolve(Population population, Mutator mutator, Resources parameters) {
// select fittest chromosome
int fittest = 0;
for (int i = 1; i < (int) parameters.get("popSize"); i++) {
if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) {
fittest = i;
}
}
fittestChromosome = population.getChromosome(fittest);
population.setBestIndividual(fittest);
// create copies of fittest chromosome, mutate them
Chromosome fc = population.getChromosome(fittest);
for (int i = 0; i < (int) parameters.get("popSize"); i++) {
if (i != fittest) {
population.getChromosome(i).copyConnections(fc);
mutator.mutate(population.getChromosome(i), parameters);
}
}
}
@Override
public Chromosome getFittestChromosome() {
return fittestChromosome;
}
@Override
public void activate(Resources parameters) {
parameters.getParameter("popSize").setManaged(true);
}
@Override
public HashMap<String, Parameter> getLocalParameters() {
return localParameters;
}
@Override
public String toString() {
return "(μ + λ)";
}
}
|