1 | package cecj.archive; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.HashSet; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Set; |
---|
7 | |
---|
8 | import ec.EvolutionState; |
---|
9 | import ec.Individual; |
---|
10 | import ec.util.Parameter; |
---|
11 | |
---|
12 | public class LAPCArchive extends ParetoCoevolutionArchive { |
---|
13 | |
---|
14 | private static final String P_NUM_LAYERS = "num-layers"; |
---|
15 | |
---|
16 | private int numLayers; |
---|
17 | |
---|
18 | private List<List<Individual>> layers; |
---|
19 | |
---|
20 | @Override |
---|
21 | public void setup(EvolutionState state, Parameter base) { |
---|
22 | super.setup(state, base); |
---|
23 | |
---|
24 | Parameter numLayersParameter = base.push(P_NUM_LAYERS); |
---|
25 | numLayers = state.parameters.getInt(numLayersParameter, null, 1); |
---|
26 | if (numLayers <= 0) { |
---|
27 | state.output.fatal("Number of LAPCA layers must be > 0.\n"); |
---|
28 | } |
---|
29 | |
---|
30 | layers = new ArrayList<List<Individual>>(numLayers); |
---|
31 | } |
---|
32 | |
---|
33 | /* |
---|
34 | * It is implemented in a IPCA-like way. Another method is to extend both existing archives by |
---|
35 | * new individuals, then find first n layers of candidates with respect to all tests in the |
---|
36 | * archive and in the population and finally select necessary tests making distinctions between |
---|
37 | * layers. |
---|
38 | */ |
---|
39 | @Override |
---|
40 | protected void submit(EvolutionState state, List<Individual> candidates, |
---|
41 | List<Individual> cArchive, List<Individual> tests, List<Individual> tArchive) { |
---|
42 | List<Individual> testsCopy = new ArrayList<Individual>(tests); |
---|
43 | List<Individual> usefulTests; |
---|
44 | |
---|
45 | for (Individual candidate : candidates) { |
---|
46 | if (isUseful(state, candidate, cArchive, tArchive, testsCopy)) { |
---|
47 | usefulTests = findUsefulTests(state, candidate, cArchive, tArchive, testsCopy); |
---|
48 | |
---|
49 | cArchive.add(candidate); |
---|
50 | tArchive.addAll(usefulTests); |
---|
51 | testsCopy.removeAll(usefulTests); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | maintainLayers(state, cArchive, tArchive); |
---|
56 | updateTestArchive(state, tArchive); |
---|
57 | } |
---|
58 | |
---|
59 | private void updateTestArchive(EvolutionState state, List<Individual> tArchive) { |
---|
60 | Set<Individual> tset = new HashSet<Individual>(); |
---|
61 | tset.addAll(findDistinguishingTests(state, layers.get(0), layers.get(0), tArchive)); |
---|
62 | for (int l = 1; l < numLayers; l++) { |
---|
63 | tset.addAll(findDistinguishingTests(state, layers.get(l - 1), layers.get(l), tArchive)); |
---|
64 | } |
---|
65 | |
---|
66 | tArchive.clear(); |
---|
67 | tArchive.addAll(tset); |
---|
68 | } |
---|
69 | |
---|
70 | private List<Individual> findDistinguishingTests(EvolutionState state, List<Individual> layer1, |
---|
71 | List<Individual> layer2, List<Individual> tests) { |
---|
72 | List<Individual> distinguishingTests = new ArrayList<Individual>(); |
---|
73 | for (Individual candidate1 : layer1) { |
---|
74 | for (Individual candidate2 : layer2) { |
---|
75 | if (candidate1.equals(candidate2)) |
---|
76 | continue; |
---|
77 | Individual test = findUsefulTest(state, candidate1, candidate2, tests); |
---|
78 | if ((test != null) && (!distinguishingTests.contains(test))) { |
---|
79 | distinguishingTests.add(test); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | return distinguishingTests; |
---|
84 | } |
---|
85 | |
---|
86 | private void maintainLayers(EvolutionState state, List<Individual> cArchive, |
---|
87 | List<Individual> tArchive) { |
---|
88 | List<Individual> cArchiveCopy = new ArrayList<Individual>(cArchive); |
---|
89 | for (int layer = 0; layer < numLayers; layer++) { |
---|
90 | List<Individual> frontPareto = findNonDominatedCandidates(state, cArchiveCopy, tArchive); |
---|
91 | layers.set(layer, frontPareto); |
---|
92 | cArchiveCopy.removeAll(frontPareto); |
---|
93 | } |
---|
94 | cArchive.removeAll(cArchiveCopy); |
---|
95 | } |
---|
96 | |
---|
97 | private List<Individual> findNonDominatedCandidates(EvolutionState state, |
---|
98 | List<Individual> cArchive, List<Individual> tArchive) { |
---|
99 | List<Individual> result = new ArrayList<Individual>(); |
---|
100 | for (Individual candidate : cArchive) { |
---|
101 | if (!isDominated(state, candidate, cArchive, tArchive)) { |
---|
102 | result.add(candidate); |
---|
103 | } |
---|
104 | } |
---|
105 | return result; |
---|
106 | } |
---|
107 | |
---|
108 | } |
---|