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 james.SimSystem;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.logging.Level;
24  
25  import p3j.misc.math.Matrix2D;
26  
27  /**
28   * Selector to sum over ages, i.e. the result is a single number per year,
29   * giving the overall amount of people in the selected population(s).
30   * 
31   * @author Christina Bohk
32   * @author Roland Ewald
33   * 
34   */
35  public class SumOverAgesSelector extends AbstractAggregationSelector {
36  
37  	/**
38  	 * Instantiates a new sum over ages selector.
39  	 * 
40  	 * @param resultsSelector
41  	 *          the results selector
42  	 * @param trialSelector
43  	 *          the trial selector
44  	 * @param generationForSelection
45  	 *          the generation for selection
46  	 */
47  	public SumOverAgesSelector(IOutputVariableSelector resultsSelector,
48  	    ISubPopulationSelector trialSelector, int generationForSelection) {
49  		super(resultsSelector, trialSelector, generationForSelection);
50  	}
51  
52  	@Override
53  	public void init(int numOfTrials, int numOfYears, int numOfAgeClasses) {
54  		aggregation = new double[numOfTrials][numOfYears];
55  	}
56  
57  	@Override
58  	public void consider(int trialCount, ResultsOfTrial result) {
59  		double[] rowSum = Matrix2D.sumRows(select(result)).toArray()[0];
60  		System.arraycopy(rowSum, 0, aggregation[trialCount], 0, rowSum.length);
61  	}
62  
63  	@Override
64  	public void finish(File destinationDir, List<Integer> indexOrdering,
65  	    ResultExport resultExport) throws IOException {
66  
67  		double[][] aggregatedData = cutOffUnused(aggregation, indexOrdering.size());
68  
69  		double[][] filteredResults = resultExport.filter(aggregatedData);
70  		double[][] quantiles = resultExport.calcQuantiles(aggregatedData);
71  
72  		SimSystem.report(Level.INFO, "Writing aggregated data...");
73  		resultExport.writeResult(destinationDir,
74  		    reorderResults(indexOrdering, filteredResults), getFileName()
75  		        + "_sum.csv");
76  		resultExport.writeResult(destinationDir, quantiles, getFileName()
77  		    + "_quantiles.csv");
78  		SimSystem.report(Level.INFO, "\t\tdone.");
79  	}
80  
81  }