1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.simulation.assignments.exhaustive;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.jamesii.core.math.random.generators.IRandom;
25 import org.jamesii.core.util.misc.Pair;
26
27 import p3j.misc.errors.GeneratorError;
28 import p3j.pppm.IProjectionModel;
29 import p3j.pppm.ProjectionModel;
30 import p3j.pppm.parameters.ParameterAssignment;
31 import p3j.pppm.parameters.ParameterInstance;
32 import p3j.pppm.sets.Set;
33 import p3j.pppm.sets.SetType;
34 import p3j.simulation.assignments.plugintype.IParamAssignmentGenerator;
35
36
37
38
39
40
41
42
43
44
45
46
47 public class ExhaustiveAssignmentGenerator implements IParamAssignmentGenerator {
48
49
50 private ExhaustiveSimParameters parameters;
51
52
53 private long overallCombinations;
54
55
56 private int currentRun;
57
58
59
60
61
62 private boolean probStopCriterionFulfilled;
63
64
65
66
67 private double overallProbabilitySum;
68
69
70
71
72
73 private List<SetTypeManager> stManagers = new ArrayList<SetTypeManager>();
74
75
76 private AssignmentEnumerator assignmentEnumerator;
77
78 @Override
79 public void init(IProjectionModel proj) {
80 overallCombinations = calculateNumOfCombinations(proj);
81
82
83 List<Integer> maxProbIndices = new ArrayList<Integer>();
84 for (SetType setType : proj.getAllSetTypes()) {
85 stManagers.add(new SetTypeManager(setType));
86 maxProbIndices.add(0);
87 }
88
89
90 assignmentEnumerator = new AssignmentEnumerator(new Assignment(
91 maxProbIndices, calcAssignmentProbability(maxProbIndices)));
92 }
93
94 @Override
95 public Pair<Map<ParameterInstance, ParameterAssignment>, List<GeneratorError>> chooseParamAssignments(
96 IRandom random) {
97
98 List<GeneratorError> errorLog = new ArrayList<GeneratorError>();
99 Pair<Map<ParameterInstance, ParameterAssignment>, Double> assignment = nextAssignment();
100 double assignmentProb = assignment.getSecondValue();
101
102 currentRun++;
103
104
105 overallProbabilitySum += assignmentProb;
106 if ((parameters.getDesiredOverallProbability() > 0 && overallProbabilitySum >= parameters
107 .getDesiredOverallProbability())
108 || (assignmentProb < parameters.getCutOffProbability())) {
109 probStopCriterionFulfilled = true;
110 }
111
112 return new Pair<Map<ParameterInstance, ParameterAssignment>, List<GeneratorError>>(
113 assignment.getFirstValue(), errorLog);
114 }
115
116
117
118
119
120
121
122
123 protected Pair<Map<ParameterInstance, ParameterAssignment>, Double> nextAssignment() {
124
125 if (assignmentEnumerator.isEmpty()) {
126 return null;
127 }
128
129
130 Assignment top = assignmentEnumerator.removeMostProbable();
131 assignmentEnumerator.add(getChildren(top));
132
133
134 Map<ParameterInstance, ParameterAssignment> overallAssignment = createMapping(top
135 .getAssignmentIndices());
136 return new Pair<Map<ParameterInstance, ParameterAssignment>, Double>(
137 overallAssignment, top.getProbability());
138 }
139
140
141
142
143
144
145 protected Assignment peek() {
146 return assignmentEnumerator.isEmpty() ? null : assignmentEnumerator
147 .getMostProbable();
148 }
149
150
151
152
153
154
155
156
157
158 protected java.util.Set<Assignment> getChildren(Assignment assignment) {
159 java.util.Set<Assignment> childs = new HashSet<Assignment>();
160 final List<Integer> indices = assignment.getAssignmentIndices();
161 for (int i = 0; i < stManagers.size(); i++) {
162 int currentIndex = indices.get(i);
163 if (!stManagers.get(i).hasAssignment(currentIndex + 1)) {
164 continue;
165 }
166 List<Integer> childIndices = new ArrayList<Integer>(indices);
167 childIndices.set(i, currentIndex + 1);
168 childs.add(new Assignment(childIndices,
169 calcAssignmentProbability(childIndices)));
170 }
171 return childs;
172 }
173
174 @Override
175 public long assignmentsLeft() {
176
177 if (probStopCriterionFulfilled) {
178 return 0;
179 }
180
181 if (parameters.getMaxNumRuns() > 0) {
182 if (currentRun >= parameters.getMaxNumRuns()) {
183 return 0;
184 }
185 return (long) parameters.getMaxNumRuns() - currentRun;
186 }
187
188 return (long) Math.ceil(overallCombinations
189 * parameters.getMaxRunFraction())
190 - currentRun;
191 }
192
193
194
195
196
197
198
199
200
201 protected long calculateNumOfCombinations(IProjectionModel proj) {
202
203 long num = 1;
204 for (SetType defType : proj.getAllSetTypes()) {
205 long numSetTypeCombinations = 0;
206 for (Set set : defType.getSets()) {
207 long numSetCombinations = 1;
208 for (ParameterInstance instance : defType.getDefinedParameters()) {
209 numSetCombinations *= set.getNumberOfAssignments(instance);
210 }
211 numSetTypeCombinations += numSetCombinations;
212 }
213 num *= numSetTypeCombinations;
214 }
215
216 return num;
217 }
218
219
220
221
222
223
224
225
226
227 protected long calculateNumOfSetCombinations(ProjectionModel proj) {
228 long num = 1;
229 int setTypes = proj.getNumOfSetTypes();
230 for (int i = 0; i < setTypes; i++) {
231 num *= proj.getSetType(i).getNumOfSets();
232 }
233 return num;
234 }
235
236
237
238
239
240
241
242
243
244
245 protected double calcAssignmentProbability(List<Integer> indices) {
246 double prob = 1;
247 for (int i = 0; i < indices.size(); i++) {
248 prob *= stManagers.get(i).getProbability(indices.get(i));
249 }
250 return prob;
251 }
252
253
254
255
256
257
258
259
260
261 protected Map<ParameterInstance, ParameterAssignment> createMapping(
262 List<Integer> indices) {
263 Map<ParameterInstance, ParameterAssignment> overallMap = new HashMap<ParameterInstance, ParameterAssignment>();
264 for (int i = 0; i < indices.size(); i++) {
265 overallMap.putAll(stManagers.get(i).getAssignment(indices.get(i)));
266 }
267 return overallMap;
268 }
269
270
271
272
273
274
275 public ExhaustiveSimParameters getParameters() {
276 return parameters;
277 }
278
279
280
281
282
283
284
285 public void setParameters(ExhaustiveSimParameters parameters) {
286 this.parameters = parameters;
287 }
288
289 }