Debian Codegraph @

src/dr/inference/operators/JointOperator.java

raw symbols SCIP
1 /*
2 * JointOperator.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 dr.math.MathUtils;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31
32 import java.util.ArrayList;
33
34 /**
35 * @author Marc A. Suchard
36 */
37 public class JointOperator extends SimpleMCMCOperator implements AdaptableMCMCOperator {
38
39 private final ArrayList<SimpleMCMCOperator> operatorList;
40 private final ArrayList<Integer> operatorToOptimizeList;
41
42 private int currentOptimizedOperator;
43 private final double targetProbability;
44
45 public JointOperator(double weight, double targetProb) {
46
47 operatorList = new ArrayList<SimpleMCMCOperator>();
48 operatorToOptimizeList = new ArrayList<Integer>();
49 targetProbability = targetProb;
50
51 setWeight(weight);
52 }
53
54 public void addOperator(SimpleMCMCOperator operation) {
55
56 operatorList.add(operation);
57 if (operation instanceof AdaptableMCMCOperator) {
58
59 if (((AdaptableMCMCOperator) operation).getMode() == AdaptationMode.ADAPTATION_ON)
60
61 operatorToOptimizeList.add(operatorList.size() - 1);
62
63 }
64 }
65
66 public final double doOperation() {
67
68 double logP = 0;
69
70 for (SimpleMCMCOperator operation : operatorList) {
71
72 logP += operation.doOperation();
73 // todo After a failure, should not have to complete remaining operations, need to fake their operate();
74 }
75
76 return logP;
77 }
78
79 // private double old;
80
81 public double getAdaptableParameter() {
82 if (operatorToOptimizeList.size() > 0) {
83 currentOptimizedOperator = operatorToOptimizeList.get(MathUtils.nextInt(operatorToOptimizeList.size()));
84 return ((AdaptableMCMCOperator) operatorList.get(currentOptimizedOperator)).getAdaptableParameter();
85 }
86 throw new IllegalArgumentException();
87 }
88
89 public void setAdaptableParameter(double value) {
90 if (operatorToOptimizeList.size() > 0) {
91 ((AdaptableMCMCOperator) operatorList.get(currentOptimizedOperator)).setAdaptableParameter(value);
92 return;
93 }
94 throw new IllegalArgumentException();
95 }
96
97
98 public int getNumberOfSubOperators() {
99 return operatorList.size();
100 }
101
102 public double getRawParamter(int i) {
103 if (i < 0 || i >= operatorList.size())
104 throw new IllegalArgumentException();
105 return ((AdaptableMCMCOperator) operatorList.get(i)).getRawParameter();
106 }
107
108
109 public double getRawParameter() {
110
111 throw new RuntimeException("More than one raw parameter for a joint operator");
112 }
113
114 public AdaptationMode getMode() {
115 if (operatorToOptimizeList.size() > 0)
116 return AdaptationMode.ADAPTATION_ON;
117 return AdaptationMode.ADAPTATION_OFF;
118 }
119
120 public MCMCOperator getSubOperator(int i) {
121 return operatorList.get(i);
122 }
123
124 public AdaptationMode getSubOperatorMode(int i) {
125 if (i < 0 || i >= operatorList.size())
126 throw new IllegalArgumentException();
127 if (operatorList.get(i) instanceof AdaptableMCMCOperator)
128 return ((AdaptableMCMCOperator) operatorList.get(i)).getMode();
129 return AdaptationMode.ADAPTATION_OFF;
130 }
131
132 public String getSubOperatorName(int i) {
133 if (i < 0 || i >= operatorList.size())
134 throw new IllegalArgumentException();
135 return "Joint." + operatorList.get(i).getOperatorName();
136 }
137
138 public String getOperatorName() {
139 // StringBuffer sb = new StringBuffer("Joint(\n");
140 // for(SimpleMCMCOperator operation : operatorList)
141 // sb.append("\t"+operation.getOperatorName()+"\n");
142 // sb.append(") opt = "+optimizedOperator.getOperatorName());
143 // return sb.toString();
144 return "JointOperator";
145 }
146
147 public Element createOperatorElement(Document d) {
148 throw new RuntimeException("not implemented");
149 }
150
151 public String getAdaptableParameterName() {
152 return "";
153 }
154
155 public double getTargetAcceptanceProbability() {
156 return targetProbability;
157 }
158
159 public double getMinimumAcceptanceLevel() {
160 double min = targetProbability - 0.2;
161 if (min < 0)
162 min = 0.01;
163 return min;
164 }
165
166 public double getMaximumAcceptanceLevel() {
167 double max = targetProbability + 0.2;
168 if (max > 1)
169 max = 0.9;
170 return max;
171 }
172
173 public double getMinimumGoodAcceptanceLevel() {
174 double min = targetProbability - 0.1;
175 if (min < 0)
176 min = 0.01;
177 return min;
178 }
179
180 public double getMaximumGoodAcceptanceLevel() {
181 double max = targetProbability + 0.2;
182 if (max > 1)
183 max = 0.9;
184 return max;
185 }
186
187 public final String getPerformanceSuggestion() {
188
189 // double prob = MCMCOperator.Utils.getAcceptanceProbability(this);
190 // double targetProb = getTargetAcceptanceProbability();
191 // dr.util.NumberFormatter formatter = new dr.util.NumberFormatter(5);
192 // double sf = OperatorUtils.optimizeScaleFactor(scaleFactor, prob, targetProb);
193 // if (prob < getMinimumGoodAcceptanceLevel()) {
194 // return "Try setting scaleFactor to about " + formatter.format(sf);
195 // } else if (prob > getMaximumGoodAcceptanceLevel()) {
196 // return "Try setting scaleFactor to about " + formatter.format(sf);
197 // } else return "";
198 return "";
199 }
200
201
202 }