View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.experiment.results;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  import java.util.Set;
26  
27  import p3j.pppm.IProjectionModel;
28  import p3j.pppm.ProjectionModel;
29  import p3j.pppm.parameters.ParameterAssignment;
30  import p3j.pppm.parameters.ParameterInstance;
31  import p3j.pppm.sets.SetType;
32  
33  /**
34   * This class is intended to represent the results of a single trial as stored
35   * in the database.
36   * 
37   * @author Christina Bohk
38   * @author Roland Ewald
39   */
40  public class ResultsOfTrial implements Serializable {
41  
42  	/** Serialization ID. */
43  	private static final long serialVersionUID = 2484910276641194217L;
44  
45  	/** The ID of this trial. */
46  	private int id = -1;
47  
48  	/** The projection to which this trial belongs. */
49  	private ProjectionModel projection;
50  
51  	/**
52  	 * The assignment used to generate the trial: one parameter assignment per
53  	 * instance.
54  	 */
55  	private Map<ParameterInstance, ParameterAssignment> assignment = new HashMap<ParameterInstance, ParameterAssignment>();
56  
57  	/**
58  	 * Results of native population calculation. This list has only a single
59  	 * element, but this improves database performance significantly.
60  	 */
61  	private List<BasicResults> nativeResults;
62  
63  	/** The results for the immigrant generations. */
64  	private List<BasicResults> immigrantResults = new ArrayList<BasicResults>();
65  
66  	/** The results for the emigrant generations. */
67  	private List<BasicResults> emigrantResults = new ArrayList<BasicResults>();
68  
69  	/** The overall assignment probability. */
70  	private double assignmentProbability;
71  
72  	/**
73  	 * The probability that the given combination of sets was chosen. Dividing
74  	 * {@link ResultsOfTrial#assignmentProbability} by this value yields the
75  	 * probability of choosing the given assignments GIVEN the selected set
76  	 * combination.
77  	 */
78  	private double setCombinationProbability;
79  
80  	/**
81  	 * Empty constructor (for Bean compatibility).
82  	 */
83  	public ResultsOfTrial() {
84  	}
85  
86  	/**
87  	 * Instantiates new results for a trial.
88  	 * 
89  	 * @param projectionModel
90  	 *          the current projection model
91  	 * @param execSummary
92  	 *          the execution summary
93  	 */
94  	public ResultsOfTrial(IProjectionModel projectionModel,
95  	    ExecutionSummary execSummary) {
96  		nativeResults = new ArrayList<BasicResults>();
97  		nativeResults.add(execSummary.getNativeResults());
98  		projection = (ProjectionModel) projectionModel;
99  		assignment.putAll(execSummary.getParamAssignments());
100 		immigrantResults.addAll(execSummary.getImmigrantResults());
101 		emigrantResults.addAll(execSummary.getEmigrantResults());
102 		calculateAssignmentProbability();
103 	}
104 
105 	/**
106 	 * Calculates the assignment's overall probability.
107 	 */
108 	private void calculateAssignmentProbability() {
109 
110 		double setCombProb = 1.0;
111 		double assignmentCombProb = 1.0;
112 
113 		Set<Integer> checkedSetTypeIDs = new HashSet<Integer>();
114 		for (Entry<ParameterInstance, ParameterAssignment> entry : assignment
115 		    .entrySet()) {
116 
117 			ParameterInstance paramInstance = entry.getKey();
118 			ParameterAssignment paramAssignment = entry.getValue();
119 			assignmentCombProb *= paramAssignment.getProbability();
120 
121 			SetType currentSetType = projection.getInstanceSetTypes().get(
122 			    paramInstance);
123 			if (!checkedSetTypeIDs.contains(currentSetType.getID())) {
124 				setCombProb *= getSetProbability(paramInstance, paramAssignment,
125 				    currentSetType);
126 				checkedSetTypeIDs.add(currentSetType.getID());
127 			}
128 		}
129 
130 		setCombinationProbability = setCombProb;
131 		assignmentProbability = setCombProb * assignmentCombProb;
132 	}
133 
134 	/**
135 	 * Gets the probability of the set.
136 	 * 
137 	 * @param paramInstance
138 	 *          a parameter instance belonging to the current Settype
139 	 * @param paramAssignment
140 	 *          a parameter assignment (belonging to the set in question)
141 	 * @param currentSetType
142 	 *          the current Settype
143 	 * @return the occurrence probability of the set
144 	 */
145 	private double getSetProbability(ParameterInstance paramInstance,
146 	    ParameterAssignment paramAssignment, SetType currentSetType) {
147 		List<p3j.pppm.sets.Set> currentSets = currentSetType.getSets();
148 		for (p3j.pppm.sets.Set currentSet : currentSets) {
149 			for (ParameterAssignment currentAssignment : currentSet
150 			    .getParameterAssignments(paramInstance).getAssignments()) {
151 				if (currentAssignment.getID() == paramAssignment.getID()) {
152 					return currentSet.getProbability();
153 				}
154 			}
155 		}
156 		return -1.0;
157 	}
158 
159 	public int getID() {
160 		return id;
161 	}
162 
163 	public void setID(int id) {
164 		this.id = id;
165 	}
166 
167 	public ProjectionModel getProjection() {
168 		return projection;
169 	}
170 
171 	public void setProjection(ProjectionModel projection) {
172 		this.projection = projection;
173 	}
174 
175 	public Map<ParameterInstance, ParameterAssignment> getAssignment() {
176 		return assignment;
177 	}
178 
179 	public void setAssignment(
180 	    Map<ParameterInstance, ParameterAssignment> assignment) {
181 		this.assignment = assignment;
182 	}
183 
184 	public List<BasicResults> getNativeResults() {
185 		return nativeResults;
186 	}
187 
188 	/**
189 	 * Gets the results of the natives. Checks that there is exactly one result
190 	 * set for the natives.
191 	 * 
192 	 * @return the results of the natives
193 	 */
194 	public BasicResults getNativesResults() {
195 		if (nativeResults.size() != 1) {
196 			throw new IllegalStateException(
197 			    "Number of native results does not match!");
198 		}
199 		return nativeResults.get(0);
200 	}
201 
202 	public void setNativeResults(List<BasicResults> nativeResults) {
203 		this.nativeResults = nativeResults;
204 	}
205 
206 	public double getAssignmentProbability() {
207 		return assignmentProbability;
208 	}
209 
210 	public void setAssignmentProbability(double assignmentProbability) {
211 		this.assignmentProbability = assignmentProbability;
212 	}
213 
214 	public List<BasicResults> getImmigrantResults() {
215 		return immigrantResults;
216 	}
217 
218 	public void setImmigrantResults(List<BasicResults> immigrantResults) {
219 		this.immigrantResults = immigrantResults;
220 	}
221 
222 	public List<BasicResults> getEmigrantResults() {
223 		return emigrantResults;
224 	}
225 
226 	public void setEmigrantResults(List<BasicResults> emigrantResults) {
227 		this.emigrantResults = emigrantResults;
228 	}
229 
230 	public double getSetCombinationProbability() {
231 		return setCombinationProbability;
232 	}
233 
234 	public void setSetCombinationProbability(double setCombinationProbability) {
235 		this.setCombinationProbability = setCombinationProbability;
236 	}
237 }