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.ArrayList;
23  import java.util.List;
24  import java.util.logging.Level;
25  
26  /**
27   * Aggregates a trial x age matrix for a given year.
28   * 
29   * @author Christina Bohk
30   * @author Roland Ewald
31   * 
32   */
33  public class YearlyAgeTrialMatrixSelector extends AbstractAggregationSelector {
34  
35  	/** The list of selectors to be added. */
36  	private final AbstractAggregationSelector[] addSelectors;
37  
38  	/** The list of selectors to be subtracted. */
39  	private final AbstractAggregationSelector[] subSelectors;
40  
41  	/** The file name for the results. */
42  	private final String fileName;
43  
44  	/** The year of interest. */
45  	private final int year;
46  
47  	/**
48  	 * Creates a age x trial matrix selector for a single year.
49  	 * 
50  	 * @param addSels
51  	 *          the list of selected results to be added
52  	 * @param subSels
53  	 *          the list of selected results to be subtracted
54  	 * @param customFileName
55  	 *          a custom file name
56  	 * @param yearOfInterest
57  	 *          the year of interest
58  	 */
59  	public YearlyAgeTrialMatrixSelector(AbstractAggregationSelector[] addSels,
60  	    AbstractAggregationSelector[] subSels, String customFileName,
61  	    int yearOfInterest) {
62  		super(null, null, -1);
63  		addSelectors = getCopy(addSels);
64  		subSelectors = getCopy(subSels);
65  		year = yearOfInterest;
66  		fileName = customFileName + year + ".csv";
67  	}
68  
69  	/**
70  	 * Creates a age x trial matrix selector for a single year.
71  	 * 
72  	 * @param addSels
73  	 *          the list of selected results to be added
74  	 * @param subSels
75  	 *          the list of selected results to be subtracted
76  	 * @param customFileName
77  	 *          a custom file name
78  	 * @param yearOfInterest
79  	 *          the year of interest
80  	 */
81  	public YearlyAgeTrialMatrixSelector(
82  	    List<AbstractAggregationSelector> selectorsForAddition,
83  	    List<AbstractAggregationSelector> selectorsForSubtraction,
84  	    String customFileName, int yearOfInterest) {
85  		this(selectorsForAddition != null ? selectorsForAddition
86  		    .toArray(new AbstractAggregationSelector[0]) : null,
87  		    selectorsForSubtraction != null ? selectorsForSubtraction
88  		        .toArray(new AbstractAggregationSelector[0]) : null,
89  		    customFileName, yearOfInterest);
90  	}
91  
92  	@Override
93  	public void init(int numOfTrials, int numOfYears, int numOfAgeClasses) {
94  		aggregation = new double[numOfTrials][numOfAgeClasses];
95  	}
96  
97  	@Override
98  	public void consider(int trialCount, ResultsOfTrial result) {
99  		List<double[]> additionArrays = new ArrayList<double[]>();
100 		List<double[]> subtractionArrays = new ArrayList<double[]>();
101 
102 		for (AbstractAggregationSelector selector : addSelectors) {
103 			additionArrays.add(selector.select(result).viewColumn(year).toArray());
104 		}
105 
106 		for (AbstractAggregationSelector selector : subSelectors) {
107 			subtractionArrays.add(selector.select(result).viewColumn(year).toArray());
108 		}
109 
110 		double[] ageStructure = sumPerElement(additionArrays, subtractionArrays);
111 
112 		System.arraycopy(ageStructure, 0, aggregation[trialCount], 0,
113 		    ageStructure.length);
114 	}
115 
116 	@Override
117 	public void finish(File destinationDir, List<Integer> indexOrdering,
118 	    ResultExport resultExport) throws IOException {
119 		double[][] aggregatedData = cutOffUnused(aggregation, indexOrdering.size());
120 		SimSystem.report(Level.INFO, "Writing aggregated data...");
121 		resultExport.writeResult(destinationDir, aggregatedData, fileName);
122 		SimSystem.report(Level.INFO, "\t\tdone.");
123 	}
124 
125 }