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.List;
20  
21  import p3j.misc.math.Matrix2D;
22  
23  /**
24   * This aggregator allows to add and subtract certain population, before summing
25   * over their ages and afterwards behaving like {@link SumOverAgesSelector}. All
26   * combinations of result/trial selectors for addition/subtraction will be
27   * chosen.
28   * 
29   * @see IAggregationSelector
30   * @see SumOverAgesSelector
31   * 
32   * @author Christina Bohk
33   * @author Roland Ewald
34   * 
35   */
36  public class MergeSubPopSumOverAgesSelector extends SumOverAgesSelector {
37  
38  	/** The aggregation selectors that point to added variables. */
39  	private final AbstractAggregationSelector[] addAggSelectors;
40  
41  	/** The aggregation selectors that point to subtracted variables. */
42  	private final AbstractAggregationSelector[] subtractAggSelectors;
43  
44  	/** The file name for result storage. */
45  	private final String fileName;
46  
47  	/**
48  	 * Instantiates aggregator that merges subpopulations.
49  	 * 
50  	 * @param selectorsForAddition
51  	 *          the selectors for addition
52  	 * @param selectorsForSubtraction
53  	 *          the selectors for subtraction
54  	 * @param customFileName
55  	 *          the custom file name for the aggregated results
56  	 */
57  	public MergeSubPopSumOverAgesSelector(
58  	    AbstractAggregationSelector[] selectorsForAddition,
59  	    AbstractAggregationSelector[] selectorsForSubtraction,
60  	    String customFileName) {
61  		// more than one selector is used - don't use super class here
62  		super(null, null, -1);
63  		addAggSelectors = getCopy(selectorsForAddition);
64  		subtractAggSelectors = getCopy(selectorsForSubtraction);
65  		fileName = customFileName;
66  	}
67  
68  	/**
69  	 * Instantiates a new merge sub pop sum over ages selector.
70  	 * 
71  	 * @param selectorsForAddition
72  	 *          the selectors for addition
73  	 * @param customFileName
74  	 *          the custom file name
75  	 */
76  	public MergeSubPopSumOverAgesSelector(
77  	    AbstractAggregationSelector[] selectorsForAddition, String customFileName) {
78  		this(selectorsForAddition, new AbstractAggregationSelector[0],
79  		    customFileName);
80  	}
81  
82  	/**
83  	 * Instantiates a new merge-sub-pop-sum-over-ages selector.
84  	 * 
85  	 * @param selectorsForAddition
86  	 *          the selectors for addition
87  	 * @param selectorsForSubtraction
88  	 *          the selectors for subtraction
89  	 * @param customFileName
90  	 *          the custom file name
91  	 */
92  	public MergeSubPopSumOverAgesSelector(
93  	    List<AbstractAggregationSelector> selectorsForAddition,
94  	    List<AbstractAggregationSelector> selectorsForSubtraction,
95  	    String customFileName) {
96  		this(selectorsForAddition != null ? selectorsForAddition
97  		    .toArray(new AbstractAggregationSelector[0]) : null,
98  		    selectorsForSubtraction != null ? selectorsForSubtraction
99  		        .toArray(new AbstractAggregationSelector[0]) : null, customFileName);
100 	}
101 
102 	/**
103 	 * Instantiates a new merge sub pop sum over ages selector.
104 	 * 
105 	 * @param selectorsForAddition
106 	 *          the selectors for addition
107 	 * @param customFileName
108 	 *          the custom file name
109 	 */
110 	public MergeSubPopSumOverAgesSelector(
111 	    List<AbstractAggregationSelector> selectorsForAddition,
112 	    String customFileName) {
113 		this(selectorsForAddition, null, customFileName);
114 	}
115 
116 	@Override
117 	public void consider(int trialCount, ResultsOfTrial result) {
118 		List<double[]> additionArrays = new ArrayList<double[]>();
119 		List<double[]> subtractionArrays = new ArrayList<double[]>();
120 
121 		for (AbstractAggregationSelector selector : getAddAggregationSelectors()) {
122 			additionArrays
123 			    .add(Matrix2D.sumRows(selector.select(result)).toArray()[0]);
124 		}
125 		for (AbstractAggregationSelector selector : getSubtractAggregationSelectors()) {
126 			subtractionArrays
127 			    .add(Matrix2D.sumRows(selector.select(result)).toArray()[0]);
128 		}
129 
130 		double[] rowSum = sumPerElement(additionArrays, subtractionArrays);
131 
132 		System.arraycopy(rowSum, 0, aggregation[trialCount], 0, rowSum.length);
133 	}
134 
135 	@Override
136 	protected String getFileName() {
137 		return fileName;
138 	}
139 
140 	public AbstractAggregationSelector[] getAddAggregationSelectors() {
141 		return addAggSelectors;
142 	}
143 
144 	public AbstractAggregationSelector[] getSubtractAggregationSelectors() {
145 		return subtractAggSelectors;
146 	}
147 
148 }