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.SubPopulation;
30  import p3j.pppm.parameters.ParameterAssignment;
31  import p3j.pppm.parameters.ParameterInstance;
32  import p3j.pppm.sets.SetType;
33  
34  /**
35   * This class is intended to represent the results of a single trial as stored
36   * in the database.
37   * 
38   * @author Christina Bohk
39   * @author Roland Ewald
40   */
41  public class ResultsOfTrial implements Serializable {
42  
43    /** Serialization ID. */
44    private static final long serialVersionUID = 2484910276641194217L;
45  
46    /** The ID of this trial. */
47    private int id = -1;
48  
49    /** The projection to which this trial belongs. */
50    private ProjectionModel projection;
51  
52    /**
53     * The assignment used to generate the trial: one parameter assignment per
54     * instance.
55     */
56    private Map<ParameterInstance, ParameterAssignment> assignment = new HashMap<>();
57  
58    /** Results of the calculations per sub-population & generation. */
59    private List<BasicResults> subPopulationResults = new ArrayList<>();
60  
61    /** The overall assignment probability. */
62    private double assignmentProbability;
63  
64    /**
65     * The probability that the given combination of sets was chosen. Dividing
66     * {@link ResultsOfTrial#assignmentProbability} by this value yields the
67     * probability of choosing the given assignments GIVEN the selected set
68     * combination.
69     */
70    private double setCombinationProbability;
71  
72    /**
73     * Empty constructor (for Bean compatibility).
74     */
75    public ResultsOfTrial() {
76    }
77  
78    /**
79     * Instantiates new results for a trial.
80     * 
81     * @param projectionModel
82     *          the current projection model
83     * @param execSummary
84     *          the execution summary
85     */
86    public ResultsOfTrial(IProjectionModel projectionModel,
87        ExecutionSummary execSummary) {
88      subPopulationResults.addAll(execSummary.getAllResults());
89      projection = (ProjectionModel) projectionModel;
90      assignment.putAll(execSummary.getParamAssignments());
91      calculateAssignmentProbability();
92    }
93  
94    /**
95     * Calculates the assignment's overall probability.
96     */
97    private void calculateAssignmentProbability() {
98  
99      double setCombProb = 1.0;
100     double assignmentCombProb = 1.0;
101 
102     Set<Integer> checkedSetTypeIDs = new HashSet<Integer>();
103     for (Entry<ParameterInstance, ParameterAssignment> entry : assignment
104         .entrySet()) {
105 
106       ParameterInstance paramInstance = entry.getKey();
107       ParameterAssignment paramAssignment = entry.getValue();
108       assignmentCombProb *= paramAssignment.getProbability();
109 
110       SetType currentSetType = projection.getInstanceSetTypes().get(
111           paramInstance);
112       if (!checkedSetTypeIDs.contains(currentSetType.getID())) {
113         setCombProb *= getSetProbability(paramInstance, paramAssignment,
114             currentSetType);
115         checkedSetTypeIDs.add(currentSetType.getID());
116       }
117     }
118 
119     setCombinationProbability = setCombProb;
120     assignmentProbability = setCombProb * assignmentCombProb;
121   }
122 
123   /**
124    * Gets the probability of the set.
125    * 
126    * @param paramInstance
127    *          a parameter instance belonging to the current Settype
128    * @param paramAssignment
129    *          a parameter assignment (belonging to the set in question)
130    * @param currentSetType
131    *          the current Settype
132    * @return the occurrence probability of the set
133    */
134   private double getSetProbability(ParameterInstance paramInstance,
135       ParameterAssignment paramAssignment, SetType currentSetType) {
136     List<p3j.pppm.sets.Set> currentSets = currentSetType.getSets();
137     for (p3j.pppm.sets.Set currentSet : currentSets) {
138       for (ParameterAssignment currentAssignment : currentSet
139           .getParameterAssignments(paramInstance).getAssignments()) {
140         if (currentAssignment.getID() == paramAssignment.getID()) {
141           return currentSet.getProbability();
142         }
143       }
144     }
145     return -1.0;
146   }
147 
148   public int getID() {
149     return id;
150   }
151 
152   public void setID(int id) {
153     this.id = id;
154   }
155 
156   public ProjectionModel getProjection() {
157     return projection;
158   }
159 
160   public void setProjection(ProjectionModel projection) {
161     this.projection = projection;
162   }
163 
164   public Map<ParameterInstance, ParameterAssignment> getAssignment() {
165     return assignment;
166   }
167 
168   public void setAssignment(
169       Map<ParameterInstance, ParameterAssignment> assignment) {
170     this.assignment = assignment;
171   }
172 
173   public double getAssignmentProbability() {
174     return assignmentProbability;
175   }
176 
177   public void setAssignmentProbability(double assignmentProbability) {
178     this.assignmentProbability = assignmentProbability;
179   }
180 
181   public double getSetCombinationProbability() {
182     return setCombinationProbability;
183   }
184 
185   public void setSetCombinationProbability(double setCombinationProbability) {
186     this.setCombinationProbability = setCombinationProbability;
187   }
188 
189   public List<BasicResults> getSubPopulationResults() {
190     return subPopulationResults;
191   }
192 
193   public void setSubPopulationResults(List<BasicResults> subPopulationResults) {
194     this.subPopulationResults = subPopulationResults;
195   }
196 
197   /**
198    * Retrieves all results for a specific sub-population.
199    * 
200    * @param subPop
201    *          the sub-population
202    * @return the list of results referring to this sub-population
203    */
204   public List<BasicResults> retrieveFor(SubPopulation subPop) {
205     List<BasicResults> subPopResults = new ArrayList<>();
206     for (BasicResults subPopResult : subPopulationResults)
207       if (subPopResult.getSubPopName().equals(subPop.getName()))
208         subPopResults.add(subPopResult);
209     return subPopResults;
210   }
211 
212 }