| 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.evomodel.operators; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.evomodelxml.operators.SubtreeLeapOperatorParser; |
| 32 |
import dr.inference.operators.*; |
| 33 |
import dr.math.MathUtils; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.List; |
| 38 |
import java.util.Map; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class SubtreeLeapOperator extends AbstractTreeOperator implements AdaptableMCMCOperator { |
| 55 |
|
| 56 |
private double size = 1.0; |
| 57 |
|
| 58 |
private final TreeModel tree; |
| 59 |
private final AdaptationMode mode; |
| 60 |
private final double targetAcceptance; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public SubtreeLeapOperator(TreeModel tree, double weight, double size, double targetAcceptance, AdaptationMode mode) { |
| 71 |
this.tree = tree; |
| 72 |
setWeight(weight); |
| 73 |
this.size = size; |
| 74 |
this.targetAcceptance = targetAcceptance; |
| 75 |
this.mode = mode; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public double doOperation() { |
| 86 |
double logq; |
| 87 |
|
| 88 |
final double delta = getDelta(); |
| 89 |
|
| 90 |
final NodeRef root = tree.getRoot(); |
| 91 |
|
| 92 |
NodeRef node; |
| 93 |
|
| 94 |
do { |
| 95 |
|
| 96 |
node = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 97 |
|
| 98 |
} while (node == root); |
| 99 |
|
| 100 |
|
| 101 |
final NodeRef parent = tree.getParent(node); |
| 102 |
|
| 103 |
|
| 104 |
final NodeRef sibling = getOtherChild(tree, parent, node); |
| 105 |
|
| 106 |
|
| 107 |
final NodeRef grandParent = tree.getParent(parent); |
| 108 |
|
| 109 |
final Map<NodeRef, Double> destinations = getDestinations(node, parent, sibling, delta); |
| 110 |
final List<NodeRef> destinationNodes = new ArrayList<NodeRef>(destinations.keySet()); |
| 111 |
|
| 112 |
|
| 113 |
int r = MathUtils.nextInt(destinations.size()); |
| 114 |
|
| 115 |
double forwardProbability = 1.0 / destinations.size(); |
| 116 |
|
| 117 |
final NodeRef j = destinationNodes.get(r); |
| 118 |
final double newHeight = destinations.get(j); |
| 119 |
|
| 120 |
final NodeRef jParent = tree.getParent(j); |
| 121 |
|
| 122 |
if (jParent != null && newHeight > tree.getNodeHeight(jParent)) { |
| 123 |
throw new IllegalArgumentException("height error"); |
| 124 |
} |
| 125 |
|
| 126 |
if (newHeight < tree.getNodeHeight(j)) { |
| 127 |
throw new IllegalArgumentException("height error"); |
| 128 |
} |
| 129 |
|
| 130 |
tree.beginTreeEdit(); |
| 131 |
|
| 132 |
if (j == parent || jParent == parent) { |
| 133 |
|
| 134 |
} else { |
| 135 |
if (grandParent == null) { |
| 136 |
|
| 137 |
|
| 138 |
tree.removeChild(parent, sibling); |
| 139 |
tree.setRoot(sibling); |
| 140 |
|
| 141 |
} else { |
| 142 |
|
| 143 |
tree.removeChild(parent, sibling); |
| 144 |
tree.removeChild(grandParent, parent); |
| 145 |
tree.addChild(grandParent, sibling); |
| 146 |
} |
| 147 |
|
| 148 |
if (jParent == null) { |
| 149 |
|
| 150 |
tree.addChild(parent, j); |
| 151 |
tree.setRoot(parent); |
| 152 |
} else { |
| 153 |
|
| 154 |
tree.removeChild(jParent, j); |
| 155 |
|
| 156 |
|
| 157 |
tree.addChild(parent, j); |
| 158 |
|
| 159 |
|
| 160 |
tree.addChild(jParent, parent); |
| 161 |
} |
| 162 |
} |
| 163 |
tree.endTreeEdit(); |
| 164 |
|
| 165 |
tree.setNodeHeight(parent, newHeight); |
| 166 |
|
| 167 |
if (tree.getParent(parent) != null && newHeight > tree.getNodeHeight(tree.getParent(parent))) { |
| 168 |
throw new IllegalArgumentException("height error"); |
| 169 |
} |
| 170 |
|
| 171 |
if (newHeight < tree.getNodeHeight(node)) { |
| 172 |
throw new IllegalArgumentException("height error"); |
| 173 |
} |
| 174 |
|
| 175 |
if (newHeight < tree.getNodeHeight(getOtherChild(tree, parent, node))) { |
| 176 |
throw new IllegalArgumentException("height error"); |
| 177 |
} |
| 178 |
|
| 179 |
final Map<NodeRef, Double> reverseDestinations = getDestinations(node, parent, getOtherChild(tree, parent, node), delta); |
| 180 |
double reverseProbability = 1.0 / reverseDestinations.size(); |
| 181 |
|
| 182 |
|
| 183 |
logq = Math.log(reverseProbability) - Math.log(forwardProbability); |
| 184 |
return logq; |
| 185 |
} |
| 186 |
|
| 187 |
private Map<NodeRef, Double> getDestinations(NodeRef node, NodeRef parent, NodeRef sibling, double delta) { |
| 188 |
|
| 189 |
final Map<NodeRef, Double> destinations = new HashMap<NodeRef, Double>(); |
| 190 |
|
| 191 |
|
| 192 |
final double height = tree.getNodeHeight(parent); |
| 193 |
|
| 194 |
final double heightBelow = height - delta; |
| 195 |
|
| 196 |
if (heightBelow > tree.getNodeHeight(node)) { |
| 197 |
|
| 198 |
|
| 199 |
final List<NodeRef> edges = new ArrayList<NodeRef>(); |
| 200 |
|
| 201 |
getIntersectingEdges(tree, sibling, heightBelow, edges); |
| 202 |
|
| 203 |
|
| 204 |
for (NodeRef n : edges) { |
| 205 |
destinations.put(n, heightBelow); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
final double heightAbove = height + delta; |
| 210 |
|
| 211 |
NodeRef node1 = parent; |
| 212 |
|
| 213 |
|
| 214 |
boolean done = false; |
| 215 |
while (!done) { |
| 216 |
NodeRef parent1 = tree.getParent(node1); |
| 217 |
|
| 218 |
if (parent1 != null) { |
| 219 |
final double height1 = tree.getNodeHeight(parent1); |
| 220 |
if (height1 < heightAbove) { |
| 221 |
|
| 222 |
|
| 223 |
NodeRef sibling1 = getOtherChild(tree, parent1, node1); |
| 224 |
|
| 225 |
double heightBelow1 = height1 - (heightAbove - height1); |
| 226 |
|
| 227 |
if (heightBelow1 > tree.getNodeHeight(node)) { |
| 228 |
|
| 229 |
final List<NodeRef> edges = new ArrayList<NodeRef>(); |
| 230 |
|
| 231 |
getIntersectingEdges(tree, sibling1, heightBelow1, edges); |
| 232 |
|
| 233 |
|
| 234 |
for (NodeRef n : edges) { |
| 235 |
destinations.put(n, heightBelow1); |
| 236 |
} |
| 237 |
} |
| 238 |
} else { |
| 239 |
|
| 240 |
destinations.put(node1, heightAbove); |
| 241 |
done = true; |
| 242 |
} |
| 243 |
|
| 244 |
node1 = parent1; |
| 245 |
} else { |
| 246 |
|
| 247 |
destinations.put(node1, heightAbove); |
| 248 |
done = true; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return destinations; |
| 253 |
} |
| 254 |
|
| 255 |
private double getDelta() { |
| 256 |
return Math.abs(MathUtils.nextGaussian() * size); |
| 257 |
} |
| 258 |
|
| 259 |
private int getIntersectingEdges(Tree tree, NodeRef node, double height, List<NodeRef> edges) { |
| 260 |
|
| 261 |
final NodeRef parent = tree.getParent(node); |
| 262 |
|
| 263 |
if (tree.getNodeHeight(parent) < height) return 0; |
| 264 |
|
| 265 |
if (tree.getNodeHeight(node) < height) { |
| 266 |
edges.add(node); |
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
int count = 0; |
| 271 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 272 |
count += getIntersectingEdges(tree, tree.getChild(node, i), height, edges); |
| 273 |
} |
| 274 |
return count; |
| 275 |
} |
| 276 |
|
| 277 |
public double getSize() { |
| 278 |
return size; |
| 279 |
} |
| 280 |
|
| 281 |
public void setSize(double size) { |
| 282 |
this.size = size; |
| 283 |
} |
| 284 |
|
| 285 |
public double getAdaptableParameter() { |
| 286 |
return Math.log(getSize()); |
| 287 |
} |
| 288 |
|
| 289 |
public void setAdaptableParameter(double value) { |
| 290 |
setSize(Math.exp(value)); |
| 291 |
} |
| 292 |
|
| 293 |
public double getRawParameter() { |
| 294 |
return getSize(); |
| 295 |
} |
| 296 |
|
| 297 |
public AdaptationMode getMode() { |
| 298 |
return mode; |
| 299 |
} |
| 300 |
|
| 301 |
public double getMinimumAcceptanceLevel() { |
| 302 |
return 0.1; |
| 303 |
} |
| 304 |
|
| 305 |
public double getMaximumAcceptanceLevel() { |
| 306 |
return 0.4; |
| 307 |
} |
| 308 |
|
| 309 |
public double getMinimumGoodAcceptanceLevel() { |
| 310 |
return 0.20; |
| 311 |
} |
| 312 |
|
| 313 |
public double getMaximumGoodAcceptanceLevel() { |
| 314 |
return 0.30; |
| 315 |
} |
| 316 |
|
| 317 |
public final String getPerformanceSuggestion() { |
| 318 |
return null; |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
public double getTargetAcceptanceProbability() { |
| 323 |
return targetAcceptance; |
| 324 |
} |
| 325 |
|
| 326 |
public String getAdaptableParameterName() { |
| 327 |
return "size"; |
| 328 |
} |
| 329 |
|
| 330 |
public String getOperatorName() { |
| 331 |
return SubtreeLeapOperatorParser.SUBTREE_LEAP + "(" + tree.getId() + ")"; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
} |