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  /**
26   * Selectors of this type select data from a particular year across all trials.
27   * The leave the age groups untouched; the aggregated data can, e.g., be used to
28   * create population pyramids.
29   * 
30   * @author Christina Bohk
31   * @author Roland Ewald
32   * 
33   */
34  public class ChooseAgesForSingleYearSelector extends
35      AbstractAggregationSelector {
36  
37  	/**
38  	 * The year of interest. Represents column index, i.e. 0 selects the first
39  	 * year and so on.
40  	 */
41  	private final int year;
42  
43  	/** The number of age classes. */
44  	private int numberOfAgeClasses = -1;
45  
46  	/**
47  	 * Instantiates a new choose ages for single year selector.
48  	 * 
49  	 * @param resultsSelector
50  	 *          the results selector
51  	 * @param trialSelector
52  	 *          the trial selector
53  	 * @param generationForSelection
54  	 *          the generation for selection
55  	 * @param yearForSelection
56  	 *          the year for selection
57  	 */
58  	public ChooseAgesForSingleYearSelector(
59  	    IOutputVariableSelector resultsSelector,
60  	    ISubPopulationSelector trialSelector, int generationForSelection,
61  	    int yearForSelection) {
62  		super(resultsSelector, trialSelector, generationForSelection);
63  		year = yearForSelection;
64  	}
65  
66  	@Override
67  	public void init(int numOfTrials, int numOfYears, int numOfAgeClasses) {
68  		aggregation = new double[numOfTrials][numOfAgeClasses];
69  		setNumberOfAgeClasses(numOfAgeClasses);
70  	}
71  
72  	@Override
73  	public void consider(int trialCount, ResultsOfTrial result) {
74  		double[][] currentData = select(result).toArray();
75  		for (int j = 0; j < getNumberOfAgeClasses(); j++) {
76  			aggregation[trialCount][j] = currentData[j][getYear()];
77  		}
78  	}
79  
80  	@Override
81  	public void finish(File destinationDir, List<Integer> indexOrdering,
82  	    ResultExport resultExport) throws IOException {
83  		double[][] aggregatedData = cutOffUnused(aggregation, indexOrdering.size());
84  		double[][] quantiles = resultExport.calcQuantiles(aggregatedData);
85  		SimSystem.report(Level.INFO, "Writing aggregated data...");
86  		resultExport.writeResult(destinationDir, quantiles, getFileName()
87  		    + "_quantiles.csv");
88  		SimSystem.report(Level.INFO, "\t\tdone.");
89  	}
90  
91  	@Override
92  	protected String getFileName() {
93  		return getPrefix() + super.getFileName();
94  	}
95  
96  	protected final String getPrefix() {
97  		return "year_" + getYear() + "_";
98  	}
99  
100 	public int getNumberOfAgeClasses() {
101 		return numberOfAgeClasses;
102 	}
103 
104 	public void setNumberOfAgeClasses(int numberOfAgeClasses) {
105 		this.numberOfAgeClasses = numberOfAgeClasses;
106 	}
107 
108 	public int getYear() {
109 		return year;
110 	}
111 
112 }