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.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import p3j.misc.math.Matrix2D;
25  import p3j.pppm.SubPopulation;
26  import p3j.pppm.parameters.ParameterAssignment;
27  import p3j.pppm.parameters.ParameterInstance;
28  import p3j.simulation.calculation.deterministic.parameters.BasicParameters;
29  import p3j.simulation.calculation.deterministic.parameters.InFlowDescendantParameters;
30  import p3j.simulation.calculation.deterministic.parameters.InFlowParameters;
31  import p3j.simulation.calculation.deterministic.parameters.JumpOffParameters;
32  
33  /**
34   * Summary of a single PPPM execution.
35   * 
36   * Created on February 20, 2007
37   * 
38   * @author Christina Bohk
39   * @author Roland Ewald
40   * 
41   */
42  public class ExecutionSummary {
43  
44    /** The parameter assignments. */
45    private final Map<ParameterInstance, ParameterAssignment> paramAssignments;
46  
47    /** The sub-populations. */
48    private final List<SubPopulation> subPopulations;
49  
50    /** Parameters for calculating the jump-off populations. */
51    private Map<SubPopulation, JumpOffParameters> jumpOffParameters = new HashMap<>();
52  
53    /** Parameters to calculate (the first generation of) in-flow sub-populations. */
54    private Map<SubPopulation, InFlowParameters> inFlowParameters = new HashMap<>();
55  
56    /** Parameters to calculate descendant generations of in-flow sub-populations. */
57    private Map<SubPopulation, List<InFlowDescendantParameters>> inFlowDescParameters = new HashMap<>();
58  
59    /** Stores the results per sub-population. */
60    private Map<SubPopulation, List<BasicResults>> results = new HashMap<>();
61  
62    /**
63     * Instantiates a new execution summary.
64     * 
65     * @param subPopulations
66     *          the existing sub populations
67     * @param assignment
68     *          the overall assignment used: parameter instance -> assignment
69     */
70    public ExecutionSummary(List<SubPopulation> subPopulations,
71        Map<ParameterInstance, ParameterAssignment> assignment) {
72      paramAssignments = Collections.unmodifiableMap(assignment);
73      this.subPopulations = subPopulations;
74    }
75  
76    /**
77     * Calculates total end population.
78     * 
79     * @return total end population
80     */
81    public Matrix2D getTotalEndPopulation() {
82      List<Matrix2D> addList = new ArrayList<Matrix2D>();
83      List<Matrix2D> subList = new ArrayList<Matrix2D>();
84      for (SubPopulation subPop : subPopulations)
85        if (subPop.isAdditive())
86          addTotalEndPopulation(subPop, addList);
87        else
88          addTotalEndPopulation(subPop, subList);
89      return Matrix2D.add(addList).sub(subList);
90    }
91  
92    /**
93     * Adds the total end population matrices to the matrix list passed as second
94     * argument.
95     * 
96     * @param subPop
97     *          the sub pop
98     * @param matrixList
99     *          the matrix list
100    */
101   private void addTotalEndPopulation(SubPopulation subPop,
102       List<Matrix2D> matrixList) {
103     for (BasicResults result : results.get(subPop)) {
104       matrixList.add(result.getEndXm());
105       matrixList.add(result.getEndXf());
106     }
107   }
108 
109   public Map<ParameterInstance, ParameterAssignment> getParamAssignments() {
110     return paramAssignments;
111   }
112 
113   /**
114    * Gets all results.
115    * 
116    * @return the results
117    */
118   public List<BasicResults> getAllResults() {
119     List<BasicResults> allResults = new ArrayList<>();
120     for (SubPopulation subPop : subPopulations)
121       allResults.addAll(results.get(subPop));
122     return allResults;
123   }
124 
125   public void setJumpOffParameters(SubPopulation jumpOffPopulation,
126       JumpOffParameters jumpOffParams) {
127     jumpOffParameters.put(jumpOffPopulation, jumpOffParams);
128   }
129 
130   public void addResults(SubPopulation jumpOffPopulation, int generation,
131       BasicResults popResults) {
132     List<BasicResults> resultList = results.get(jumpOffPopulation);
133     if (resultList == null) {
134       resultList = new ArrayList<>();
135       results.put(jumpOffPopulation, resultList);
136     }
137     if (resultList.size() != generation)
138       throw new IllegalArgumentException("Can't add result for generation "
139           + generation + ", there are " + resultList.size()
140           + " elements in result list.");
141     resultList.add(generation, popResults);
142   }
143 
144   public void setInFlowParameters(SubPopulation subPopulation,
145       InFlowParameters parameters) {
146     inFlowParameters.put(subPopulation, parameters);
147   }
148 
149   public BasicParameters getParameters(SubPopulation subPopulation,
150       int generation) {
151     if (!subPopulation.isConsistingOfDescendantGenerations())
152       throw new IllegalArgumentException("Sub-population '"
153           + subPopulation.getName()
154           + "' does not have separate descendant generations.");
155 
156     if (generation == 0)
157       return inFlowParameters.get(subPopulation);
158 
159     return inFlowDescParameters.get(subPopulation).get(generation - 1);
160   }
161 
162   public void setDescendantParameters(SubPopulation subPopulation,
163       int generation, InFlowDescendantParameters parameters) {
164     if (generation <= 0 || !subPopulation.isConsistingOfDescendantGenerations())
165       throw new IllegalArgumentException("There is no generation '"
166           + generation + "' for sub-population '" + subPopulation.getName()
167           + "'.");
168     List<InFlowDescendantParameters> paramList = inFlowDescParameters
169         .get(subPopulation);
170     if (paramList == null) {
171       paramList = new ArrayList<>();
172       inFlowDescParameters.put(subPopulation, paramList);
173     }
174     if (paramList.size() != generation - 1)
175       throw new IllegalArgumentException("Can't add parameters for generation "
176           + generation + ", there are " + paramList.size()
177           + " elements in the parameter list for descendant populations.");
178     paramList.add(parameters);
179   }
180 
181   public BasicResults getResults(SubPopulation subPopulation, int generation) {
182     return results.get(subPopulation).get(generation);
183   }
184 }