1 | /* |
---|
2 | Portions copyright 2010 by Sean Luke, Robert Hubley, and George Mason University |
---|
3 | Licensed under the Academic Free License version 3.0 |
---|
4 | See the file "LICENSE" for more information |
---|
5 | */ |
---|
6 | |
---|
7 | package ec.multiobjective.spea2; |
---|
8 | |
---|
9 | import ec.*; |
---|
10 | import ec.util.*; |
---|
11 | import ec.multiobjective.*; |
---|
12 | import ec.simple.*; |
---|
13 | import java.util.*; |
---|
14 | |
---|
15 | /* |
---|
16 | * SPEA2Breeder.java |
---|
17 | * |
---|
18 | * Created: Sat Oct 16 11:24:43 EDT 2010 |
---|
19 | * By: Faisal Abidi and Sean Luke |
---|
20 | * Replaces earlier class by: Robert Hubley, with revisions by Gabriel Balan and Keith Sullivan |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * This subclass of SimpleBreeder overrides the loadElites method to build an archive in the top elites[subpopnum] |
---|
25 | * of each subpopulation. It computes the sparsity metric, then constructs the archive. |
---|
26 | */ |
---|
27 | |
---|
28 | public class SPEA2Breeder extends SimpleBreeder |
---|
29 | { |
---|
30 | protected void loadElites(EvolutionState state, Population newpop) |
---|
31 | { |
---|
32 | // are our elites small enough? |
---|
33 | for(int x=0;x<state.population.subpops.length;x++) |
---|
34 | if (elite[x]>state.population.subpops[x].individuals.length) |
---|
35 | state.output.error("The number of elites for subpopulation " + x + " exceeds the actual size of the subpopulation", new Parameter(EvolutionState.P_BREEDER).push(P_ELITE).push(""+x)); |
---|
36 | state.output.exitIfErrors(); |
---|
37 | |
---|
38 | // do it |
---|
39 | for (int sub = 0; sub < state.population.subpops.length; sub++) |
---|
40 | { |
---|
41 | Individual[] newInds = newpop.subpops[sub].individuals; // The new population after we are done picking the elites |
---|
42 | Individual[] oldInds = state.population.subpops[sub].individuals; // The old population from which to pick elites |
---|
43 | |
---|
44 | buildArchive(state, oldInds, newInds, elite[sub]); |
---|
45 | } |
---|
46 | |
---|
47 | // optionally force reevaluation |
---|
48 | unmarkElitesEvaluated(newpop); |
---|
49 | } |
---|
50 | |
---|
51 | public double[] calculateDistancesFromIndividual(Individual ind, Individual[] inds) |
---|
52 | { |
---|
53 | double[] d = new double[inds.length]; |
---|
54 | for(int i = 0; i < inds.length; i++) |
---|
55 | d[i] = ((SPEA2MultiObjectiveFitness)ind.fitness).sumSquaredObjectiveDistance((SPEA2MultiObjectiveFitness)inds[i].fitness); |
---|
56 | // now sort |
---|
57 | Arrays.sort(d); |
---|
58 | return d; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | public void buildArchive(EvolutionState state, Individual[] oldInds, Individual[] newInds, int archiveSize) |
---|
63 | { |
---|
64 | Individual[] dummy = new Individual[0]; |
---|
65 | |
---|
66 | // step 1: load the archive with the pareto-nondominated front |
---|
67 | ArrayList archive = new ArrayList(); |
---|
68 | ArrayList nonFront = new ArrayList(); |
---|
69 | MultiObjectiveFitness.partitionIntoParetoFront(oldInds, archive, nonFront); |
---|
70 | int currentArchiveSize = archive.size(); |
---|
71 | |
---|
72 | // step 2: if the archive isn't full, load the remainder with the fittest individuals (using customFitnessMetric) that aren't in the archive yet |
---|
73 | if (currentArchiveSize < archiveSize) |
---|
74 | { |
---|
75 | Collections.sort(nonFront); // the fitter individuals will be earlier |
---|
76 | int len = (archiveSize - currentArchiveSize); |
---|
77 | for(int i = 0; i < len; i++) |
---|
78 | { |
---|
79 | archive.add(nonFront.get(i)); |
---|
80 | currentArchiveSize++; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | // step 3: if the archive is OVERFULL, iterate as follows: |
---|
86 | // step 3a: remove the k-closest individual in the archive |
---|
87 | SPEA2Evaluator evaluator = ((SPEA2Evaluator)(state.evaluator)); |
---|
88 | Individual[] inds = (Individual[])(archive.toArray(dummy)); |
---|
89 | |
---|
90 | while(currentArchiveSize > archiveSize) |
---|
91 | { |
---|
92 | Individual closest = (Individual)(archive.get(0)); |
---|
93 | int closestIndex = 0; |
---|
94 | double[] closestD = calculateDistancesFromIndividual(closest, oldInds); |
---|
95 | |
---|
96 | for(int i = 1; i < currentArchiveSize; i++) |
---|
97 | { |
---|
98 | Individual competitor = (Individual)(archive.get(i)); |
---|
99 | double[] competitorD = calculateDistancesFromIndividual(competitor, oldInds); |
---|
100 | |
---|
101 | for(int k = 0; k < oldInds.length; k++) |
---|
102 | { |
---|
103 | if (closestD[i] > competitorD[i]) |
---|
104 | { closest = competitor ; closestD = competitorD; closestIndex = k; break; } |
---|
105 | else if (closestD[i] < competitorD[i]) |
---|
106 | { break; } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | // remove him destructively -- put the top guy in his place and remove the top guy. This is O(1) |
---|
111 | archive.set(closestIndex, archive.get(archive.size()-1)); |
---|
112 | archive.remove(archive.size()-1); |
---|
113 | |
---|
114 | currentArchiveSize--; |
---|
115 | } |
---|
116 | |
---|
117 | // step 4: put clones of the archive in the new individuals |
---|
118 | Object[] obj = archive.toArray(); |
---|
119 | for(int i = 0; i < archiveSize; i++) |
---|
120 | newInds[newInds.length - archiveSize + i] = (Individual)(((Individual)obj[i]).clone()); |
---|
121 | } |
---|
122 | } |
---|