source: java/ecj/cecj/problems/TestBasedProblemCachingDecorator.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1package cecj.problems;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9
10import cecj.interaction.InteractionResult;
11import cecj.utils.Pair;
12
13import ec.EvolutionState;
14import ec.Individual;
15import ec.util.Parameter;
16
17public class TestBasedProblemCachingDecorator extends TestBasedProblem {
18        public static final String P_CACHE_SIZE = "cache-size";
19        public static final String P_INNER_PROBLEM = "inner-problem";
20
21        public static final int UNBOUNDED_CACHE = Integer.MAX_VALUE;
22
23        private Map<Pair<Individual>, Pair<? extends InteractionResult>> cache;
24        private Map<Pair<Individual>, Integer> LRUtimer;
25
26        private TestBasedProblem problem;
27        private int cacheSizeLimit;
28
29        @Override
30        public void setup(EvolutionState state, Parameter base) {
31                super.setup(state, base);
32               
33                Parameter innerProblemParam = base.push(P_INNER_PROBLEM);
34                problem = (TestBasedProblem) state.parameters
35                        .getInstanceForParameter(innerProblemParam, null, TestBasedProblem.class);
36                problem.setup(state, base);
37
38                Parameter cacheSizeParam = base.push(P_CACHE_SIZE);
39                cacheSizeLimit = state.parameters.getIntWithDefault(cacheSizeParam, null, UNBOUNDED_CACHE);
40
41                cache = new HashMap<Pair<Individual>, Pair<? extends InteractionResult>>();
42                LRUtimer = new HashMap<Pair<Individual>, Integer>();
43        }
44
45        public TestBasedProblem getProblem() {
46                return problem;
47        }
48       
49        @Override
50        public Pair<? extends InteractionResult> test(EvolutionState state, Individual candidate,
51                        Individual test) {
52
53                Pair<Individual> key = new Pair<Individual>(candidate, test);
54                Pair<? extends InteractionResult> result = cache.get(key);
55                if (result == null) {
56                        result = problem.test(state, candidate, test);
57                        cache.put(key, result);
58                }
59
60                LRUtimer.put(key, state.generation);
61                if (cache.size() > cacheSizeLimit) {
62                        clearLeastRecentlyUsed();
63                }
64
65                return result;
66        }
67
68        /**
69         * Clears least recently used pairs of individuals. Sorts the list of stored pairs of
70         * individuals according to time of last use. Removes a half of cache storage.
71         */
72        private void clearLeastRecentlyUsed() {
73                List<Pair<Individual>> list = new ArrayList<Pair<Individual>>(LRUtimer.keySet());
74                Collections.sort(list, new Comparator<Pair<Individual>>() {
75                        public int compare(Pair<Individual> o1, Pair<Individual> o2) {
76                                if (LRUtimer.get(o1) > LRUtimer.get(o2)) {
77                                        return -1;
78                                } else if (LRUtimer.get(o1) < LRUtimer.get(o2)) {
79                                        return 1;
80                                } else {
81                                        return 0;
82                                }
83                        }
84                });
85
86                for (int i = cacheSizeLimit / 2; i < list.size(); i++) {
87                        cache.remove(list.get(i));
88                        LRUtimer.remove(list.get(i));
89                }
90        }
91}
Note: See TracBrowser for help on using the repository browser.