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.Collections;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25
26 import p3j.pppm.parameters.ParameterAssignment;
27 import p3j.pppm.parameters.ParameterInstance;
28 import p3j.pppm.sets.Set;
29 import p3j.pppm.sets.SetType;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class SetManager {
49
50
51 private Set set;
52
53
54 private List<ParameterInstance> instances;
55
56
57 private List<List<ParameterAssignment>> assumptions = new ArrayList<List<ParameterAssignment>>();
58
59
60 private AssignmentEnumerator assignmentEnumerator;
61
62
63 private Assignment currentAssignment;
64
65
66
67
68
69
70
71
72
73 public SetManager(Set s, SetType setType) {
74 set = s;
75 instances = setType.getDefinedParameters();
76 List<Integer> maxProbCombination = new ArrayList<Integer>();
77 for (ParameterInstance instance : instances) {
78 List<ParameterAssignment> assignments = new ArrayList<ParameterAssignment>(
79 set.getParameterAssignments(instance).getAssignments());
80
81
82 Collections.sort(assignments, new Comparator<ParameterAssignment>() {
83 @Override
84 public int compare(ParameterAssignment a1, ParameterAssignment a2) {
85 return Double.compare(a2.getProbability(), a1.getProbability());
86 }
87 });
88
89 assumptions.add(assignments);
90 maxProbCombination.add(0);
91 }
92
93
94 assignmentEnumerator = new AssignmentEnumerator(new Assignment(
95 maxProbCombination, calcAssignmentProbability(maxProbCombination)));
96 nextAssignment();
97 }
98
99
100
101
102
103
104
105
106
107 protected double calcSetAssignmentProbability() {
108 if (currentAssignment == null) {
109 return -1;
110 }
111 return set.getProbability() * currentAssignment.getProbability();
112 }
113
114
115
116
117
118
119
120
121 private double calcAssignmentProbability(List<Integer> assumptionIndices) {
122 double prob = 1;
123 for (int i = 0; i < assumptionIndices.size(); i++) {
124 Integer index = assumptionIndices.get(i);
125 if (index >= assumptions.get(i).size()) {
126 return 0;
127 }
128 prob *= assumptions.get(i).get(index).getProbability();
129 }
130 return prob;
131 }
132
133
134
135
136
137
138 protected final boolean nextAssignment() {
139
140 if (assignmentEnumerator.isEmpty()) {
141 return false;
142 }
143
144 currentAssignment = assignmentEnumerator.removeMostProbable();
145 java.util.Set<Assignment> childs = createChildAssignments(currentAssignment);
146 assignmentEnumerator.add(childs);
147 return true;
148 }
149
150
151
152
153
154
155
156
157 private java.util.Set<Assignment> createChildAssignments(Assignment assignment) {
158 java.util.Set<Assignment> childs = new HashSet<Assignment>();
159 final List<Integer> indices = assignment.getAssignmentIndices();
160 for (int i = 0; i < instances.size(); i++) {
161 int currentIndex = indices.get(i);
162 if (currentIndex >= assumptions.get(i).size() - 1) {
163 continue;
164 }
165 List<Integer> childIndices = new ArrayList<Integer>(indices);
166 childIndices.set(i, currentIndex + 1);
167 childs.add(new Assignment(childIndices,
168 calcAssignmentProbability(childIndices)));
169 }
170 return childs;
171 }
172
173
174
175
176
177
178 protected double getNextAssignmentProb() {
179 return assignmentEnumerator.getMostProbable().getProbability();
180 }
181
182
183
184
185
186
187 protected double getCurrentAssignmentProb() {
188 return currentAssignment.getProbability();
189 }
190
191
192
193
194
195
196
197
198 protected Map<ParameterInstance, ParameterAssignment> getCurrentMapping() {
199 Map<ParameterInstance, ParameterAssignment> mapping = new HashMap<ParameterInstance, ParameterAssignment>();
200 List<Integer> assumptionIndices = currentAssignment.getAssignmentIndices();
201 for (int i = 0; i < instances.size(); i++) {
202 mapping.put(instances.get(i),
203 assumptions.get(i).get(assumptionIndices.get(i)));
204 }
205 return mapping;
206 }
207 }