Debian Codegraph @

src/dr/evomodel/operators/TreeUniform.java

raw symbols SCIP
1 /*
2 * TreeUniform.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.MutableTree;
29 import dr.evolution.tree.NodeRef;
30 import dr.evomodel.tree.TreeModel;
31 import dr.evomodelxml.operators.TreeUniformParser;
32 import dr.math.MathUtils;
33
34 /**
35 * Uniform height change of several related tree nodes at once.
36 *
37 * Currently supported - a parent-son pair and parent-two sons triplet.
38 *
39 * @author Joseph Heled
40 */
41 // Cleaning out untouched stuff. Can be resurrected if needed
42 @Deprecated
43 public class TreeUniform extends AbstractTreeOperator {
44
45 private final int nodesToMove;
46
47 //private static final int MAX_TRIES = 100;
48 private final TreeModel tree;
49
50 public TreeUniform(int n, TreeModel tree, double weight) {
51 assert n == 2 || n == 3;
52
53 this.tree = tree;
54 this.nodesToMove = n;
55 setWeight(weight);
56 }
57
58 public double doOperation() {
59 if( tree.getInternalNodeCount() < 2 ) {
60 throw new RuntimeException("no node found");
61 }
62
63 tree.beginTreeEdit();
64
65 switch( nodesToMove ) {
66 case 2: move2(); break;
67 case 3: move3(); break;
68 }
69
70 tree.endTreeEdit();
71
72 // AR not sure whether this check is needed...
73 try {
74 tree.checkTreeIsValid();
75 } catch( MutableTree.InvalidTreeException ite ) {
76 throw new RuntimeException(ite.toString());
77 }
78
79 return 0;
80 }
81
82
83 // insure two elements are sorted in reverse order
84 private void sorted(double[] h, int i, int j) {
85 if( h[i] < h[j] ) {
86 exch(h, i, j);
87 }
88 }
89
90 private void exch(double[] h, int i, int j) {
91 final double t = h[i];
92 h[i] = h[j];
93 h[j] = t;
94 }
95
96 private void move3() /*throws OperatorFailedException*/ {
97 final int nInternalNodes = tree.getInternalNodeCount();
98
99 final NodeRef root = tree.getRoot();
100
101 // find an internal node with 2 internal children
102 NodeRef i = root;
103 int nTries = 0;
104
105 while( root == i ) {
106 i = tree.getInternalNode(MathUtils.nextInt(nInternalNodes));
107 }
108 boolean ise0 = tree.isExternal(tree.getChild(i, 0));
109 boolean ise1 = tree.isExternal(tree.getChild(i, 1));
110
111 if( ise0 || ise1 ) {
112 if( ise0 != ise1 ) {
113 doMove2(tree.getChild(i, ise0 ? 1 : 0) );
114 } else {
115 doMove1(i);
116 }
117 return;
118 }
119
120 final NodeRef iParent = tree.getParent(i);
121
122 final NodeRef child0 = tree.getChild(i, 0);
123 final NodeRef child1 = tree.getChild(i, 1);
124
125 final double hMin0 = getMaxChildHeight(child0);
126 final double hMin1 = getMaxChildHeight(child1);
127 // upper limit for i
128 final double hMax = tree.getNodeHeight(iParent);
129
130 final double mx = Math.max(hMin0, hMin1);
131 final double mn = Math.min(hMin0, hMin1);
132
133 final double d1 = hMax - mx;
134
135 // factor of d1^2 dropped
136 final double
137 a0 = Math.abs(hMin0 - hMin1) / 2,
138 a1 = d1 / 3;
139
140 final double th = a0 / (a0 + a1);
141
142 double[] h = new double[3];
143
144 final int il = (hMin0 < hMin1) ? 0 : 1;
145
146 for(int k = 0; k < 3; ++k) {
147 h[k] = MathUtils.uniform(mx, hMax);
148 }
149
150 if( MathUtils.nextDouble() < th ) {
151 h[1+il] = MathUtils.uniform(mn,mx);
152 sorted(h, 0, 2-il);
153 } else {
154 int iMax = h[0] > h[1] ? 0 : 1;
155 iMax = h[iMax] < h[2] ? 2 : iMax;
156 exch(h, 0, iMax);
157 }
158
159 assert hMax > h[0] && h[0] > h[1] && h[0] > h[2] && h[1] > hMin0 && h[2] > hMin1;
160
161 NodeRef[] nodes = {i, child0, child1};
162 for(int k = 0; k < nodes.length; ++k) {
163 tree.setNodeHeight(nodes[k], h[k]);
164 tree.pushTreeChangedEvent(nodes[k]);
165 }
166 }
167
168 private void move2() {
169 final int nInternalNodes = tree.getInternalNodeCount();
170
171 final NodeRef root = tree.getRoot();
172
173 NodeRef i = root;
174
175 while( root == i ) {
176 i = tree.getInternalNode(MathUtils.nextInt(nInternalNodes)) ;
177 }
178
179 doMove2(i);
180 // return true;
181 }
182
183 private void doMove2(NodeRef i) {
184 final NodeRef iParent = tree.getParent(i);
185
186 if( iParent == tree.getRoot() ) {
187 doMove1(i);
188 return;
189 }
190
191 final NodeRef iGrandParent = tree.getParent(iParent);
192
193 // lower limit for node (max height of children)
194 final double hMin = getMaxChildHeight(i);
195 // upper limit for parent (it's parent height)
196 final double hMax = tree.getNodeHeight(iGrandParent);
197 // lower bound for parent
198 final double hLim0 = Math.max(hMin, tree.getNodeHeight(getOtherChild(iParent, i)));
199
200 final double d1 = hMax - hLim0;
201 // factor of d1 dropped
202 final double a0 = (hLim0 - hMin),
203 a1 = d1/2;
204
205 final double th = a0 / (a0 + a1);
206 double[] h = new double[2];
207
208 if( MathUtils.nextDouble() < th ) {
209 h[0] = MathUtils.uniform(hMin, hLim0);
210 h[1] = MathUtils.uniform(hLim0, hMax);
211 } else {
212 for(int k = 0; k < 2; ++k) {
213 h[k] = MathUtils.uniform(hLim0, hMax);
214 }
215 // sorted(h, 1, 0) ??
216 if( h[0] > h[1] ) {
217 double t = h[0];
218 h[0] = h[1];
219 h[1] = t;
220 }
221 }
222
223 tree.setNodeHeight(iParent, h[1]);
224 tree.setNodeHeight(i, h[0]);
225
226 tree.pushTreeChangedEvent(iParent);
227 tree.pushTreeChangedEvent(i);
228 }
229
230 private void doMove1(NodeRef i) {
231 final NodeRef iParent = tree.getParent(i);
232 final double hMax = tree.getNodeHeight(iParent);
233
234 // lower limit for node (max height of children)
235 final double hMin = getMaxChildHeight(i);
236 double h = MathUtils.uniform(hMin, hMax);
237 tree.setNodeHeight(i, h);
238
239 tree.pushTreeChangedEvent(i);
240 }
241
242 private NodeRef getOtherChild(NodeRef n, NodeRef c) {
243 for(int nc = 0; nc < tree.getChildCount(n); ++nc ) {
244 final NodeRef child = tree.getChild(n, nc);
245 if( child != c ) {
246 return child;
247 }
248 }
249 assert true;
250 return null;
251 }
252
253 private double getMaxChildHeight(NodeRef n) {
254 double hMax = -1;
255 for(int nc = 0; nc < tree.getChildCount(n); ++nc ) {
256 hMax = Math.max(hMax, tree.getNodeHeight(tree.getChild(n, nc) ));
257 }
258 return hMax;
259 }
260
261
262 public String getPerformanceSuggestion() {
263 return "";
264 }
265
266 public String getOperatorName() {
267 return TreeUniformParser.TREE_UNIFORM + "(" + nodesToMove + "," + tree.getId() + ")";
268 }
269
270 }