1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.simulation;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 import org.jamesii.SimSystem;
25 import org.jamesii.core.math.random.generators.IRandom;
26 import org.jamesii.core.util.misc.Pair;
27
28 import p3j.database.IP3MDatabase;
29 import p3j.experiment.results.ExecutionSummary;
30 import p3j.experiment.results.ResultsOfTrial;
31 import p3j.misc.errors.GeneratorError;
32 import p3j.misc.gui.GUI;
33 import p3j.misc.math.Matrix2D;
34 import p3j.pppm.IProjectionModel;
35 import p3j.pppm.SubPopulation;
36 import p3j.pppm.parameters.ParameterAssignment;
37 import p3j.pppm.parameters.ParameterInstance;
38 import p3j.pppm.parameters.ParameterType;
39 import p3j.simulation.assignments.plugintype.IParamAssignmentGenerator;
40 import p3j.simulation.calculation.deterministic.InFlowDescendantPopulation;
41 import p3j.simulation.calculation.deterministic.InFlowPopulation;
42 import p3j.simulation.calculation.deterministic.JumpOffPopulation;
43 import p3j.simulation.calculation.deterministic.parameters.BasicParameters;
44 import p3j.simulation.calculation.deterministic.parameters.InFlowDescendantParameters;
45 import p3j.simulation.calculation.deterministic.parameters.InFlowParameters;
46 import p3j.simulation.calculation.deterministic.parameters.JumpOffParameters;
47
48
49
50
51
52
53
54
55
56
57
58 public class SingleExecution {
59
60
61
62
63
64 private Map<String, ParameterAssignment> genIndepParameters;
65
66
67
68
69
70
71 private List<Map<String, ParameterAssignment>> genDepParameters;
72
73
74 private final IProjectionModel projection;
75
76
77 private final List<SubPopulation> jumpOffPopulations;
78
79
80 private final List<SubPopulation> inFlowPopulations;
81
82
83 private final IP3MDatabase database;
84
85
86 private final IRandom random;
87
88
89
90
91
92
93
94
95
96 public SingleExecution(IProjectionModel proj, IP3MDatabase dataBase) {
97 projection = proj;
98 jumpOffPopulations = projection.getSubPopulationModel()
99 .getJumpOffPopulations();
100 inFlowPopulations = projection.getSubPopulationModel()
101 .getInFlowPopulations();
102 database = dataBase;
103 random = SimSystem.getRNGGenerator().getNextRNG();
104 }
105
106
107
108
109
110
111
112
113 public Pair<ExecutionSummary, List<GeneratorError>> execute(
114 IParamAssignmentGenerator generator) {
115
116 Pair<ExecutionSummary, List<GeneratorError>> result = null;
117
118 try {
119 Pair<Map<ParameterInstance, ParameterAssignment>, List<GeneratorError>> assignment = chooseAssignment(
120 generator, random);
121
122
123 int years = projection.getYears();
124 ExecutionSummary executionSummary = new ExecutionSummary(projection
125 .getSubPopulationModel().getSubPopulations(),
126 assignment.getFirstValue());
127
128 for (SubPopulation jumpOffPopulation : jumpOffPopulations) {
129 JumpOffParameters jumpOffParameters = setupBasicJumpOffParameters(
130 years, jumpOffPopulation);
131 JumpOffPopulation nativePopulation = new JumpOffPopulation();
132 executionSummary.setJumpOffParameters(jumpOffPopulation,
133 jumpOffParameters);
134 executionSummary.addResults(jumpOffPopulation, 0, nativePopulation
135 .calculatePopulation(jumpOffPopulation.getName(), 0,
136 jumpOffParameters));
137
138 if (jumpOffPopulation.isConsistingOfDescendantGenerations())
139 throw new UnsupportedOperationException();
140 }
141
142 for (SubPopulation inFlowPopulation : inFlowPopulations) {
143 calculateFirstInFlowPopulation(executionSummary, inFlowPopulation,
144 years);
145 if (inFlowPopulation.isConsistingOfDescendantGenerations())
146 for (int i = 1; i < projection.getGenerations(); i++) {
147 calculateInFlowChildPopulation(executionSummary, inFlowPopulation,
148 i, years);
149 }
150 }
151 storeResultsToDB(executionSummary);
152 result = new Pair<ExecutionSummary, List<GeneratorError>>(
153 executionSummary, assignment.getSecondValue());
154 } catch (Throwable t) {
155 GUI.printErrorMessage("Execution failed", t);
156 }
157 return result;
158 }
159
160
161
162
163
164
165
166 private void storeResultsToDB(ExecutionSummary executionSummary) {
167 ResultsOfTrial results = new ResultsOfTrial(projection, executionSummary);
168 database.saveTrialResults(results);
169 }
170
171
172
173
174
175
176
177
178
179
180
181 protected Pair<Map<ParameterInstance, ParameterAssignment>, List<GeneratorError>> chooseAssignment(
182 IParamAssignmentGenerator generator, IRandom rng) {
183
184 Pair<Map<ParameterInstance, ParameterAssignment>, List<GeneratorError>> generatorResults = generator
185 .chooseParamAssignments(rng);
186
187
188 Map<ParameterInstance, ParameterAssignment> assignments = generatorResults
189 .getFirstValue();
190
191
192
193 genIndepParameters = new HashMap<String, ParameterAssignment>();
194 genDepParameters = new ArrayList<Map<String, ParameterAssignment>>();
195 for (int i = 0; i < projection.getGenerations(); i++) {
196 genDepParameters.add(new HashMap<String, ParameterAssignment>());
197 }
198
199 for (Entry<ParameterInstance, ParameterAssignment> assignmentEntry : assignments
200 .entrySet()) {
201 if (assignmentEntry.getKey().getParameter().isGenerationDependent()) {
202 genDepParameters.get(assignmentEntry.getKey().getGeneration()).put(
203 assignmentEntry.getKey().getParameter().getName(),
204 assignmentEntry.getValue());
205 } else {
206 genIndepParameters.put(assignmentEntry.getKey().getParameter()
207 .getName(), assignmentEntry.getValue());
208 }
209 }
210 return generatorResults;
211 }
212
213
214
215
216
217
218
219
220 protected Matrix2D getGenIndepParameter(String parameterName) {
221 ParameterAssignment parameterAssignment = genIndepParameters
222 .get(parameterName);
223 return DeviationCalculator.calculateAssignmentDeviation(
224 parameterAssignment, random);
225 }
226
227
228
229
230
231
232
233
234
235
236 protected Matrix2D getGenDepParameter(String parameterName, int generation) {
237 ParameterAssignment parameterAssignment = genDepParameters.get(generation)
238 .get(parameterName);
239 return DeviationCalculator.calculateAssignmentDeviation(
240 parameterAssignment, random);
241 }
242
243
244
245
246
247
248
249
250
251
252
253 void calculateFirstInFlowPopulation(ExecutionSummary executionSummary,
254 SubPopulation subPopulation, int years) {
255 InFlowParameters parameters = new InFlowParameters(years,
256 projection.getMaximumAge());
257 parameters.setMigrantsXm(getGenIndepParameter(ParameterType.MIGRATION
258 .getMaleLabelFor(subPopulation)));
259 parameters.setMigrantsXf(getGenIndepParameter(ParameterType.MIGRATION
260 .getFemaleLabelFor(subPopulation)));
261 setupBasicInFlowPopulationParameters(parameters, subPopulation, 0);
262 InFlowPopulation migPopulation = new InFlowPopulation();
263 executionSummary.setInFlowParameters(subPopulation, parameters);
264 executionSummary.addResults(subPopulation, 0, migPopulation
265 .calculatePopulation(subPopulation.getName(), 0, parameters));
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280 void calculateInFlowChildPopulation(ExecutionSummary executionSummary,
281 SubPopulation subPopulation, int generation, int years) {
282 InFlowDescendantParameters parameters = new InFlowDescendantParameters(
283 years, projection.getMaximumAge());
284 parameters.setOldFertX(executionSummary.getParameters(subPopulation,
285 generation - 1).getFertX());
286 parameters.setOldMeanXf(executionSummary.getResults(subPopulation,
287 generation - 1).getMeanXf());
288 setupBasicInFlowPopulationParameters(parameters, subPopulation, generation);
289 InFlowDescendantPopulation migChildPopulation = new InFlowDescendantPopulation();
290 executionSummary.setDescendantParameters(subPopulation, generation,
291 parameters);
292 executionSummary.addResults(subPopulation, generation, migChildPopulation
293 .calculatePopulation(subPopulation.getName(), generation, parameters));
294 }
295
296
297
298
299
300
301
302
303
304 JumpOffParameters setupBasicJumpOffParameters(int years,
305 SubPopulation jumpOffPopulation) {
306
307 JumpOffParameters nativeParameters = new JumpOffParameters(years,
308 projection.getMaximumAge());
309
310 nativeParameters.setPEndSYm(getGenIndepParameter(ParameterType.JUMP_OFF
311 .getMaleLabelFor(jumpOffPopulation)));
312 nativeParameters.setPEndSYf(getGenIndepParameter(ParameterType.JUMP_OFF
313 .getFemaleLabelFor(jumpOffPopulation)));
314
315 nativeParameters.setMortXm(getGenIndepParameter(ParameterType.MORTALITY
316 .getMaleLabelFor(jumpOffPopulation)));
317 nativeParameters.setMortXf(getGenIndepParameter(ParameterType.MORTALITY
318 .getFemaleLabelFor(jumpOffPopulation)));
319
320 nativeParameters
321 .setDeathProbInfant1halfMale(getGenIndepParameter(ParameterType.PROP_INF_DEATHS_FIRST_6M
322 .getMaleLabelFor(jumpOffPopulation)));
323 nativeParameters
324 .setDeathProbInfant1halfFemale(getGenIndepParameter(ParameterType.PROP_INF_DEATHS_FIRST_6M
325 .getFemaleLabelFor(jumpOffPopulation)));
326
327 nativeParameters
328 .setSurviveProbO100m(getGenIndepParameter(ParameterType.SURV_PROB_OPEN_END
329 .getMaleLabelFor(jumpOffPopulation)));
330 nativeParameters
331 .setSurviveProbO100f(getGenIndepParameter(ParameterType.SURV_PROB_OPEN_END
332 .getFemaleLabelFor(jumpOffPopulation)));
333
334 nativeParameters
335 .setMaleRateLiveBirth(getGenIndepParameter(ParameterType.PROP_MALE_LIVE_BIRTHS
336 .getLabelFor(jumpOffPopulation)));
337
338 nativeParameters.setFertX(getGenIndepParameter(ParameterType.FERTILITY
339 .getLabelFor(jumpOffPopulation)));
340
341 return nativeParameters;
342 }
343
344
345
346
347
348
349
350
351
352
353
354 void setupBasicInFlowPopulationParameters(BasicParameters parameters,
355 SubPopulation subPopulation, int generation) {
356
357 parameters.setDeathProbInfant1halfMale(getGenDepParameter(
358 ParameterType.PROP_INF_DEATHS_FIRST_6M.getMaleLabelFor(subPopulation),
359 generation));
360 parameters
361 .setDeathProbInfant1halfFemale(getGenDepParameter(
362 ParameterType.PROP_INF_DEATHS_FIRST_6M
363 .getFemaleLabelFor(subPopulation), generation));
364
365 parameters.setMortXm(getGenDepParameter(
366 ParameterType.MORTALITY.getMaleLabelFor(subPopulation), generation));
367 parameters.setMortXf(getGenDepParameter(
368 ParameterType.MORTALITY.getFemaleLabelFor(subPopulation), generation));
369
370 parameters.setSurviveProbO100m(getGenDepParameter(
371 ParameterType.SURV_PROB_OPEN_END.getMaleLabelFor(subPopulation),
372 generation));
373 parameters.setSurviveProbO100f(getGenDepParameter(
374 ParameterType.SURV_PROB_OPEN_END.getFemaleLabelFor(subPopulation),
375 generation));
376
377 parameters.setFertX(getGenDepParameter(
378 ParameterType.FERTILITY.getLabelFor(subPopulation), generation));
379
380 parameters.setMaleRateLiveBirth(getGenDepParameter(
381 ParameterType.PROP_MALE_LIVE_BIRTHS.getLabelFor(subPopulation),
382 generation));
383 }
384 }