Debian Codegraph @

src/dr/inference/operators/TwoPhaseOperator.java

raw symbols SCIP
1 /*
2 * TwoPhaseOperator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.operators;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import dr.inference.model.Parameter;
32
33 /**
34 * This class allows to use two different sets of operators.
35 *
36 * @author Guy Baele
37 */
38
39 public class TwoPhaseOperator extends AbstractAdaptableOperator {
40
41 public static final boolean DEBUG = false;
42 public static final boolean PROVIDE_SAMPLES = false;
43
44 /*private AbstractAdaptableOperator[] phaseOneOperators;
45 private AbstractAdaptableOperator[] phaseTwoOperators;
46
47 double phaseOneTotalWeight, phaseTwoTotalWeight;*/
48
49 private List<AbstractAdaptableOperator> phaseOneOperators;
50 private List<AdaptableVarianceMultivariateNormalOperator> phaseTwoOperators;
51 //private List<AbstractAdaptableOperator> phaseTwoOperators;
52 private List<AbstractAdaptableOperator> currentOperators;
53
54 private SimpleOperatorSchedule phaseOneScheduler;
55 private SimpleOperatorSchedule phaseTwoScheduler;
56 private SimpleOperatorSchedule currentOperatorScheduler;
57
58 private List<Parameter> parameters;
59 private ArrayList<ArrayList<Double>> storedValues;
60
61 private int initial;
62 private int burnin;
63 private int numberOfCalls;
64 private int currentOperatorIndex;
65
66 private boolean switchOperators;
67
68 public TwoPhaseOperator(List<AbstractAdaptableOperator> phaseOneOperators, List<AdaptableVarianceMultivariateNormalOperator> phaseTwoOperators, List<Parameter> parameters, int initial, int burnin, double weight, AdaptationMode mode) {
69
70 super(mode);
71
72 /*this.phaseOneOperators = phaseOneOperators;
73 this.phaseTwoOperators = phaseTwoOperators;
74
75 this.phaseOneTotalWeight = 0.0;
76 for (int i = 0; i < phaseOneOperators.length; i++) {
77 this.phaseOneTotalWeight += phaseOneOperators[i].getWeight();
78 }
79 this.phaseTwoTotalWeight = 0.0;
80 for (int i = 0; i < phaseTwoOperators.length; i++) {
81 this.phaseTwoTotalWeight += phaseTwoOperators[i].getWeight();
82 }*/
83
84 if (DEBUG) {
85 System.err.println("\nConstructing TwoPhaseOperator");
86 }
87
88 setWeight(weight);
89
90 this.initial = initial;
91 this.burnin = burnin;
92 this.numberOfCalls = 0;
93
94 this.switchOperators = false;
95
96 this.phaseOneOperators = phaseOneOperators;
97 this.phaseTwoOperators = phaseTwoOperators;
98
99 this.parameters = parameters;
100 this.storedValues = new ArrayList<ArrayList<Double>>();
101 for (int i = 0; i < phaseOneOperators.size(); i++) {
102 this.storedValues.add(new ArrayList<Double>());
103 }
104
105 phaseOneScheduler = new SimpleOperatorSchedule();
106 for (MCMCOperator operator : phaseOneOperators) {
107 phaseOneScheduler.addOperator(operator);
108 }
109
110 if (DEBUG) {
111 System.err.println("Phase One Scheduler initiated with size: " + phaseOneScheduler.getOperatorCount());
112 }
113
114 phaseTwoScheduler = new SimpleOperatorSchedule();
115 for (MCMCOperator operator : phaseTwoOperators) {
116 phaseTwoScheduler.addOperator(operator);
117 }
118
119 if (DEBUG) {
120 System.err.println("Phase Two Scheduler initiated with size: " + phaseTwoScheduler.getOperatorCount());
121 }
122
123 currentOperatorScheduler = phaseOneScheduler;
124 currentOperators = phaseOneOperators;
125
126 }
127
128 public double doOperation() {
129
130 if (DEBUG) {
131 System.err.println("\nTwoPhaseOperator: doOperation() called");
132 }
133
134 numberOfCalls++;
135 if (DEBUG) {
136 System.err.println("Number of times called: " + numberOfCalls);
137 }
138
139 currentOperatorIndex = currentOperatorScheduler.getNextOperatorIndex();
140 if (DEBUG) {
141 System.err.println("current operator index: " + currentOperatorIndex);
142 }
143
144 //don't store anything in the first set of operators themselves
145 //store everything in this class to not clutter AVMVN operator with excessive code
146 if (numberOfCalls > burnin && !switchOperators) {
147 //assume a 1-on-1 relationship between the parameter list and the first phase of operators
148 //i.e. each parameter has 1 operator acting on it
149 //now we can use currentOperatorIndex to help with the bookkeeping
150
151 //first decide to which of the phase two operators the parameter value needs to be written to
152 int phaseTwoCounter = 0;
153
154 //at the same time decide where it actually came from in order to determine its actual value
155 //i.e. Parameter might be a CompoundParameter, which complicates things
156 int parameterIndex = currentOperatorIndex;
157 for (int i = 0; i < phaseTwoOperators.size(); i++) {
158 //TODO: this may rely on the AVMVN operator only having 1 CompoundParameter
159 if (currentOperatorIndex < phaseTwoOperators.get(i).getParameter().getSize()) {
160 break;
161 } else {
162 parameterIndex -= phaseTwoOperators.get(i).getParameter().getSize();
163 phaseTwoCounter++;
164 }
165 }
166 storedValues.get(currentOperatorIndex).add(parameters.get(phaseTwoCounter).getParameterValue(parameterIndex));
167
168 if (DEBUG) {
169 System.err.println("Storing values in TwoPhaseOperator");
170 System.err.println("currentOperatorIndex: " + currentOperatorIndex);
171 System.err.println("parameterIndex: " + parameterIndex);
172 System.err.print("storage dimensions: " + storedValues.size());
173 for (int i = 0; i < storedValues.size(); i++) {
174 System.err.print(" -> " + storedValues.get(i).size());
175 }
176 System.err.println();
177 }
178
179 /*if (DEBUG) {
180 System.err.println("Passing values to phase two operator(s)");
181 System.err.println("currentOperatorIndex: " + currentOperatorIndex);
182 System.err.println("AVMVN operator assigned: " + phaseTwoCounter);
183 System.err.println("parameterIndex: " + parameterIndex);
184 }
185 phaseTwoOperators.get(phaseTwoCounter).setSample(parameterIndex, parameters.get(phaseTwoCounter).getParameterValue(parameterIndex));
186 */
187 }
188
189 if (numberOfCalls > initial && !switchOperators) {
190 if (DEBUG) {
191 System.err.println("Switch from phase one scheduler to phase two scheduler");
192 }
193 currentOperatorScheduler = phaseTwoScheduler;
194 //TODO: fix Java type safety problem below
195 currentOperators = (List<AbstractAdaptableOperator>)(List<?>) phaseTwoOperators;
196 //an extra draw is needed here
197 currentOperatorIndex = currentOperatorScheduler.getNextOperatorIndex();
198
199 if (PROVIDE_SAMPLES) {
200 //call methods to calculate means and covariance matrix and pass them on to AVMVN operator(s)
201 //need to create the appropriate list of lists to pass on to AVMVN operator
202 for (int i = 0; i < phaseTwoOperators.size(); i++) {
203
204 int listSize = phaseTwoOperators.get(i).getParameter().getDimension();
205
206 ArrayList<ArrayList<Double>> temp = new ArrayList<ArrayList<Double>>();
207 for (int j = 0; j < listSize; j++) {
208 temp.add(new ArrayList<Double>());
209 temp.set(j, storedValues.get(i*phaseTwoOperators.get(i).getParameter().getSize()+j));
210 }
211
212 phaseTwoOperators.get(i).provideSamples(temp);
213 }
214 }
215
216 switchOperators = true;
217 }
218
219 double logJacobian = (currentOperators.get(currentOperatorIndex)).doOperation();
220
221 return logJacobian;
222 }
223
224 /*@Override
225 public void accept(double deviation) {
226 if (DEBUG) {
227 System.err.println("TwoPhaseOperator: accept(double deviation) called");
228 }
229 currentOperators.get(currentOperatorIndex).accept(deviation);
230 }
231
232 @Override
233 public void reject() {
234 if (DEBUG) {
235 System.err.println("TwoPhaseOperator: reject() called");
236 }
237 currentOperators.get(currentOperatorIndex).reject();
238 }
239
240 @Override
241 public void reset() {
242 if (DEBUG) {
243 System.err.println("TwoPhaseOperator: reset() called");
244 }
245 currentOperators.get(currentOperatorIndex).reset();
246 }*/
247
248 public double getAdaptableParameter() {
249 return currentOperators.get(currentOperatorIndex).getAdaptableParameter();
250 }
251
252 public void setAdaptableParameter(double value) {
253 currentOperators.get(currentOperatorIndex).setAdaptableParameter(value);
254 }
255
256 public double getRawParameter() {
257 return currentOperators.get(currentOperatorIndex).getRawParameter();
258 }
259
260 public String getAdaptableParameterName() {
261 return "windowSize";
262 }
263
264 // @Override
265 // public double getTargetAcceptanceProbability() {
266 // return currentOperators.get(currentOperatorIndex).getTargetAcceptanceProbability();
267 // }
268
269 public String getOperatorName() {
270 return "twoPhaseOperator(use at own risk)";
271 }
272
273 }