Debian Codegraph @

src/dr/evomodel/operators/FixedHeightSubtreePruneRegraftOperator.java

raw symbols SCIP
1 /*
2 * FixedHeightSubtreePruneRegraftOperator.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.evomodel.operators;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeUtils;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodelxml.operators.SubtreeJumpOperatorParser;
33 import dr.inference.operators.*;
34 import dr.math.distributions.NormalDistribution;
35 import dr.math.MathUtils;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40 * Implements the fixed height subtree prune regraft move. Described by Sebastian Hoehna et al
41 *
42 * @author Andrew Rambaut
43 * @version $Id$
44 */
45 public class FixedHeightSubtreePruneRegraftOperator extends AbstractTreeOperator {
46
47 private final TreeModel tree;
48
49
50 /**
51 * Constructor
52 * @param tree
53 * @param weight
54 */
55 public FixedHeightSubtreePruneRegraftOperator(TreeModel tree, double weight) {
56 this.tree = tree;
57 setWeight(weight);
58
59 }
60 /**
61 * Do a subtree jump move.
62 *
63 * @return the log-transformed hastings ratio
64 */
65 public double doOperation() {
66
67 final NodeRef root = tree.getRoot();
68
69 // double maxHeight = tree.getNodeHeight(root);
70
71 NodeRef i;
72 NodeRef iP = null;
73 NodeRef CiP = null;
74 NodeRef PiP = null;
75 List<NodeRef> destinations = null;
76
77 do {
78 // 1. choose a random node avoiding root or child of root
79 i = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
80
81 } while (root == i || tree.getParent(i) == root);
82
83 iP = tree.getParent(i);
84 CiP = getOtherChild(tree, iP, i);
85 PiP = tree.getParent(iP);
86
87 // get the height of the parent
88 double parentHeight = tree.getNodeHeight(iP);
89
90 // get a list of all edges that intersect this height
91 destinations = getIntersectingEdges(tree, parentHeight);
92
93 if (destinations.size() == 0) {
94 // if there are no destinations available then reject the move
95 return Double.NEGATIVE_INFINITY;
96 }
97
98 int r = MathUtils.nextInt(destinations.size());
99
100 // remove the target node and its sibling (shouldn't be there because their parent's height is exactly equal to the target height).
101 destinations.remove(i);
102 destinations.remove(CiP);
103
104 final NodeRef j = destinations.get(r);
105 final NodeRef jP = tree.getParent(j);
106
107 tree.beginTreeEdit();
108
109 // remove the parent of i by connecting its sibling to its grandparent.
110 tree.removeChild(iP, CiP);
111 tree.removeChild(PiP, iP);
112 tree.addChild(PiP, CiP);
113
114 // remove destination edge j from its parent
115 tree.removeChild(jP, j);
116
117 // add destination edge to the parent of i
118 tree.addChild(iP, j);
119
120 // and add the parent of i as a child of the former parent of j
121 tree.addChild(jP, iP);
122
123 tree.endTreeEdit();
124
125 return 0.0;
126 }
127
128 /**
129 * Gets a list of edges that subtend the given height
130 * @param tree
131 * @param height
132 * @return
133 */
134 private List<NodeRef> getIntersectingEdges(Tree tree, double height) {
135
136 List<NodeRef> intersectingEdges = new ArrayList<NodeRef>();
137
138 for (int i = 0; i < tree.getNodeCount(); i++) {
139 final NodeRef node = tree.getNode(i);
140 final NodeRef parent = tree.getParent(node);
141
142 // The original node and its sibling will not be included because their height is exactly equal to the target height
143 if (parent != null && tree.getNodeHeight(node) < height && tree.getNodeHeight(parent) > height) {
144 intersectingEdges.add(node);
145 }
146 }
147 return intersectingEdges;
148 }
149
150 public String getPerformanceSuggestion() {
151 return null;
152 }
153
154
155 public String getOperatorName() {
156 return SubtreeJumpOperatorParser.SUBTREE_JUMP + "(" + tree.getId() + ")";
157 }
158 }