| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 74 |
} |
| 75 |
|
| 76 |
return logP; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
return ""; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
} |