[28] | 1 | package cecj.archive; |
---|
| 2 | |
---|
| 3 | import java.util.Collections; |
---|
| 4 | import java.util.Comparator; |
---|
| 5 | import java.util.HashMap; |
---|
| 6 | import java.util.List; |
---|
| 7 | import java.util.Map; |
---|
| 8 | |
---|
| 9 | import cecj.interaction.InteractionResult; |
---|
| 10 | import cecj.utils.EquivalenceComparator; |
---|
| 11 | |
---|
| 12 | import ec.EvolutionState; |
---|
| 13 | import ec.Individual; |
---|
| 14 | import ec.util.Parameter; |
---|
| 15 | |
---|
| 16 | public class MaxSolveArchive extends CandidateTestArchive { |
---|
| 17 | |
---|
| 18 | private static final String P_ARCHIVE_SIZE = "archive-size"; |
---|
| 19 | |
---|
| 20 | private int archiveSize; |
---|
| 21 | |
---|
| 22 | @Override |
---|
| 23 | public void setup(EvolutionState state, Parameter base) { |
---|
| 24 | super.setup(state, base); |
---|
| 25 | |
---|
| 26 | Parameter archiveSizeParameter = base.push(P_ARCHIVE_SIZE); |
---|
| 27 | archiveSize = state.parameters.getInt(archiveSizeParameter, null, 1); |
---|
| 28 | if (archiveSize <= 0) { |
---|
| 29 | state.output.fatal("Archive size must be > 0\n"); |
---|
| 30 | } |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | @Override |
---|
| 34 | protected void submit(EvolutionState state, List<Individual> candidates, |
---|
| 35 | List<Individual> cArchive, List<Individual> tests, List<Individual> tArchive) { |
---|
| 36 | cArchive.addAll(candidates); |
---|
| 37 | tArchive.addAll(tests); |
---|
| 38 | |
---|
| 39 | eliminateDuplicates(cArchive, new CandidateEquivalenceComparator(state, tArchive)); |
---|
| 40 | Map<Individual, Integer> numSolved = countSolved(state, cArchive, tArchive); |
---|
| 41 | Collections.sort(cArchive, new NumberSolvedComparator(numSolved)); |
---|
| 42 | |
---|
| 43 | if (cArchive.size() > archiveSize) { |
---|
| 44 | cArchive.subList(archiveSize, cArchive.size()).clear(); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | eliminateUnsolvedTests(state, cArchive, tArchive); |
---|
| 48 | eliminateDuplicates(tArchive, new TestEquivalenceComparator(state, cArchive)); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | private void eliminateDuplicates(List<Individual> archive, |
---|
| 52 | EquivalenceComparator<Individual> comparator) { |
---|
| 53 | for (int ind1 = 0; ind1 < archive.size(); ind1++) { |
---|
| 54 | for (int ind2 = archive.size(); ind2 > ind1; ind2--) { |
---|
| 55 | if (comparator.equal(archive.get(ind1), archive.get(ind2))) { |
---|
| 56 | archive.remove(ind2); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | private void eliminateUnsolvedTests(EvolutionState state, List<Individual> cArchive, |
---|
| 63 | List<Individual> tArchive) { |
---|
| 64 | for (int test = 0; test < tArchive.size(); test++) { |
---|
| 65 | if (!isSolved(state, tArchive.get(test), cArchive)) { |
---|
| 66 | tArchive.remove(test); |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | private boolean isSolved(EvolutionState state, Individual test, List<Individual> candidates) { |
---|
| 72 | for (Individual candidate : candidates) { |
---|
| 73 | if (problem.solves(state, candidate, test)) { |
---|
| 74 | return true; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | return false; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | private Map<Individual, Integer> countSolved(EvolutionState state, List<Individual> candidates, |
---|
| 81 | List<Individual> tests) { |
---|
| 82 | Map<Individual, Integer> result = new HashMap<Individual, Integer>(); |
---|
| 83 | for (Individual candidate : candidates) { |
---|
| 84 | int countTest = 0; |
---|
| 85 | for (Individual test : tests) { |
---|
| 86 | if (problem.solves(state, candidate, test)) { |
---|
| 87 | countTest++; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | result.put(candidate, countTest); |
---|
| 91 | } |
---|
| 92 | return result; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | private class NumberSolvedComparator implements Comparator<Individual> { |
---|
| 96 | private Map<Individual, Integer> solved; |
---|
| 97 | |
---|
| 98 | public NumberSolvedComparator(Map<Individual, Integer> solved) { |
---|
| 99 | this.solved = solved; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | public int compare(Individual o1, Individual o2) { |
---|
| 103 | if (solved.get(o1) > solved.get(o2)) { |
---|
| 104 | return -1; |
---|
| 105 | } else if (solved.get(o1) > solved.get(o2)) { |
---|
| 106 | return 1; |
---|
| 107 | } else { |
---|
| 108 | return 0; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | private class CandidateEquivalenceComparator implements EquivalenceComparator<Individual> { |
---|
| 114 | private EvolutionState state; |
---|
| 115 | private List<Individual> tests; |
---|
| 116 | |
---|
| 117 | public CandidateEquivalenceComparator(EvolutionState state, List<Individual> tests) { |
---|
| 118 | this.state = state; |
---|
| 119 | this.tests = tests; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | public boolean equal(Individual o1, Individual o2) { |
---|
| 123 | for (Individual test : tests) { |
---|
| 124 | InteractionResult result1 = problem.test(state, o1, test).first; |
---|
| 125 | InteractionResult result2 = problem.test(state, o2, test).first; |
---|
| 126 | if (!result1.equals(result2)) { |
---|
| 127 | return false; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | private class TestEquivalenceComparator implements EquivalenceComparator<Individual> { |
---|
| 135 | private EvolutionState state; |
---|
| 136 | private List<Individual> candidates; |
---|
| 137 | |
---|
| 138 | public TestEquivalenceComparator(EvolutionState state, List<Individual> candidates) { |
---|
| 139 | this.state = state; |
---|
| 140 | this.candidates = candidates; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | public boolean equal(Individual o1, Individual o2) { |
---|
| 144 | for (Individual candidate : candidates) { |
---|
| 145 | InteractionResult result1 = problem.test(state, candidate, o2).second; |
---|
| 146 | InteractionResult result2 = problem.test(state, candidate, o2).second; |
---|
| 147 | if (!result1.equals(result2)) { |
---|
| 148 | return false; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | return false; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|