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.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.Map;
24
25 import p3j.pppm.parameters.ParameterAssignment;
26 import p3j.pppm.parameters.ParameterInstance;
27 import p3j.pppm.sets.Set;
28 import p3j.pppm.sets.SetType;
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class SetTypeManager {
43
44
45 private List<SetManager> setManagers = new ArrayList<SetManager>();
46
47
48 private SetManagerComparator smComp = new SetManagerComparator();
49
50
51
52
53
54
55
56 private List<SetAssignment> history = new ArrayList<SetAssignment>();
57
58
59
60
61
62
63
64 public SetTypeManager(SetType setType) {
65 if (setType.getSets().isEmpty()) {
66 throw new IllegalArgumentException("Fatal error: Settype '"
67 + setType.getName() + "' has no sets!");
68 }
69
70 for (Set set : setType.getSets()) {
71 if (set.isValid()) {
72 setManagers.add(new SetManager(set, setType));
73 }
74 }
75
76 Collections.sort(setManagers, smComp);
77 createAssignments(1);
78 }
79
80
81
82
83
84
85
86
87
88 public double getProbability(int assignmentIndex) {
89
90 if (assignmentIndex < history.size()) {
91 return history.get(assignmentIndex).getProbability();
92 }
93
94 if (!createAssignments(assignmentIndex + 1)) {
95 return -1;
96 }
97 return history.get(assignmentIndex).getProbability();
98 }
99
100
101
102
103
104
105
106
107
108 public Map<ParameterInstance, ParameterAssignment> getAssignment(
109 int assignmentIndex) {
110 if (assignmentIndex < history.size()) {
111 return history.get(assignmentIndex).getAssignment();
112 }
113
114 if (!createAssignments(assignmentIndex + 1)) {
115 return null;
116 }
117 return history.get(assignmentIndex).getAssignment();
118 }
119
120
121
122
123
124
125
126
127
128 public boolean hasAssignment(int assignmentIndex) {
129 if (assignmentIndex < history.size()) {
130 return true;
131 }
132 return createAssignments(assignmentIndex + 1);
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146 protected final boolean createAssignments(int desiredSize) {
147
148 while (history.size() < desiredSize && !setManagers.isEmpty()) {
149
150
151 history.add(new SetAssignment(setManagers.get(0).getCurrentMapping(),
152 setManagers.get(0).calcSetAssignmentProbability()));
153
154
155 boolean hasNext = setManagers.get(0).nextAssignment();
156
157
158 if (!hasNext) {
159 setManagers.remove(0);
160 continue;
161 }
162 Collections.sort(setManagers, smComp);
163 }
164 return (history.size() == desiredSize);
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179 class SetAssignment {
180
181
182 private final Map<ParameterInstance, ParameterAssignment> assignment;
183
184
185 private final double probability;
186
187
188
189
190
191
192
193
194
195 SetAssignment(Map<ParameterInstance, ParameterAssignment> assignm, Double prob) {
196 assignment = assignm;
197 probability = prob;
198 }
199
200 public Map<ParameterInstance, ParameterAssignment> getAssignment() {
201 return assignment;
202 }
203
204 public double getProbability() {
205 return probability;
206 }
207
208 }
209
210
211
212
213
214
215
216
217
218
219
220 class SetManagerComparator implements Comparator<SetManager>, Serializable {
221
222
223 private static final long serialVersionUID = -7821654659763832183L;
224
225 @Override
226 public int compare(SetManager man1, SetManager man2) {
227 return Double.compare(man2.calcSetAssignmentProbability(),
228 man1.calcSetAssignmentProbability());
229 }
230
231 }