Debian Codegraph @

src/dr/evomodel/arg/operators/ARGExchangeOperator.java

raw symbols SCIP
1 /*
2 * ARGExchangeOperator.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.arg.operators;
27
28 import dr.evolution.tree.MutableTree;
29 import dr.evolution.tree.NodeRef;
30 import dr.evomodel.arg.ARGModel;
31 import dr.evomodel.operators.ExchangeOperator;
32 import dr.inference.operators.MCMCOperator;
33 import dr.inference.operators.SimpleMCMCOperator;
34 import dr.math.MathUtils;
35 import dr.xml.*;
36
37 import java.util.ArrayList;
38
39 /**
40 * Implements branch exchange operations.
41 * There is a NARROW and WIDE variety.
42 * The narrow exchange is very similar to a rooted-tree
43 * nearest-neighbour interchange but with the restriction
44 * that node height must remain consistent.
45 * <p/>
46 * KNOWN BUGS: WIDE operator cannot be used on trees with 4 or less tips!
47 */
48 public class ARGExchangeOperator extends SimpleMCMCOperator {
49
50 public static final String NARROW_EXCHANGE = "argNarrowExchange";
51 public static final String WIDE_EXCHANGE = "argWideExchange";
52
53 public static final int NARROW = 0;
54 public static final int WIDE = 1;
55
56 private static final int MAX_TRIES = 10000;
57
58 private int mode = NARROW;
59 private ARGModel tree;
60
61 public ARGExchangeOperator(int mode, ARGModel tree, int weight) {
62 this.mode = mode;
63 this.tree = tree;
64 setWeight(weight);
65 }
66
67 public double doOperation() {
68
69 double logHastings = 0.0;
70 int tipCount = tree.getExternalNodeCount();
71
72 if (mode == NARROW) {
73 if (tree.getReassortmentNodeCount() < 2)
74 logHastings = narrow();
75 else
76 return 0.0;
77 } else {
78 logHastings = wide();
79 }
80
81
82 if (tree.getExternalNodeCount() != tipCount) {
83 throw new RuntimeException("Lost some tips in " + ((mode == NARROW) ? "NARROW mode." : "WIDE mode."));
84 }
85
86 assert !Double.isInfinite(logHastings) && !Double.isNaN(logHastings);
87
88 return logHastings;
89 }
90
91 public int getAllValidNarrowMoves() {
92 NodeRef iP = null, j = null, jP = null;
93 ArrayList<NodeRef> nodes = new ArrayList<NodeRef>(tree.getNodeCount());
94 ArrayList<NarrowMove> moves = new ArrayList<NarrowMove>(tree.getNodeCount());
95
96 for (int k = 0, n = tree.getNodeCount(); k < n; k++) {
97 NodeRef x = tree.getNode(k);
98 if (!tree.isRoot(x) && !tree.isRoot(tree.getParent(x, 0))
99 && !tree.isRoot(tree.getParent(x, 1))) {
100 nodes.add(x);
101 }
102 }
103 NarrowMove a;
104 for (NodeRef i : nodes) {
105 for (int k = 0; k < 2; k++) {
106 iP = tree.getParent(i, k);
107 for (int m = 0; m < 2; m++) {
108 jP = tree.getParent(iP, m);
109 j = tree.getOtherChild(jP, iP);
110 a = new NarrowMove(i, iP, j, jP);
111 if (validMove(a) && !moves.contains(a))
112 moves.add(a);
113 }
114 }
115 }
116 assert moves.size() > 0;
117 return moves.size();
118 }
119
120 private boolean validMove(NarrowMove move) {
121 if (move.j != move.iP && move.i != move.j &&
122 (tree.getNodeHeight(move.j) < tree.getNodeHeight(move.iP)) &&
123 (tree.getNodeHeight(move.i) < tree.getNodeHeight(move.jP))) {
124 return true;
125
126 }
127 return false;
128 }
129
130 private class NarrowMove {
131 public NodeRef i;
132 public NodeRef j;
133 public NodeRef iP;
134 public NodeRef jP;
135
136 public NarrowMove(NodeRef i, NodeRef iP, NodeRef j, NodeRef jP) {
137 this.i = i;
138 this.j = j;
139 this.iP = iP;
140 this.jP = jP;
141
142 }
143
144 public boolean equals(Object o) {
145 if (!(o instanceof NarrowMove)) {
146 return false;
147 }
148
149 NarrowMove move = (NarrowMove) o;
150
151 if (this.i == move.i && this.j == move.j &&
152 this.iP == move.iP && this.jP == move.jP) {
153 return true;
154 }
155 if (this.i == move.j && this.j == move.i &&
156 this.iP == move.jP && this.jP == move.iP) {
157 return true;
158 }
159
160 return false;
161 }
162
163 public String toString() {
164 return "(" + i.toString() + ", " + iP.toString() +
165 ", " + jP.toString() + ", " + j.toString() + ")";
166 }
167
168 }
169
170 /**
171 * WARNING: Assumes strictly bifurcating tree.
172 */
173 public double narrow() {
174
175 NodeRef i = null, iP = null, j = null, jP = null;
176 int tries = 0;
177
178 //Echoose
179
180 int beforeMoves = getAllValidNarrowMoves();
181
182 while (tries < MAX_TRIES) {
183 i = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
184 while (tree.getRoot() == i || tree.getParent(i, 0) == tree.getRoot() || tree.getParent(i, 1) == tree.getRoot()) {
185 i = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
186 }
187
188 iP = tree.getParent(i, 0);
189 if (tree.isReassortment(i) && MathUtils.nextBoolean())
190 iP = tree.getParent(i, 1);
191
192 jP = tree.getParent(iP, 0);
193 if (tree.isReassortment(iP) && MathUtils.nextBoolean())
194 jP = tree.getParent(iP, 1);
195
196 j = tree.getChild(jP, 0);
197 if (j == iP) {
198 j = tree.getChild(jP, 1);
199 }
200
201 if (j != iP && i != j && // can still occur if i is child of doubly-linked reassortment
202 (tree.getNodeHeight(j) < tree.getNodeHeight(iP)) && (tree.getNodeHeight(i) < tree.getNodeHeight(jP))) {
203 // todo fix height check for cases where i and j get switched
204 break;
205 }
206 tries += 1;
207 }
208 //System.out.println("tries = " + tries);
209
210 //Eupdate
211 if (tries < MAX_TRIES) {
212 try {
213 eupdateARG(i, j, iP, jP);
214 } catch (ARGOperatorFailedException e) {
215 return Double.NEGATIVE_INFINITY;
216 }
217
218 tree.pushTreeChangedEvent(iP);
219 tree.pushTreeChangedEvent(jP);
220 } else {
221 //throw new ARGOperatorFailedException("Couldn't find valid narrow move on this tree!!");
222 return Double.NEGATIVE_INFINITY;
223 }
224
225 return Math.log((double) beforeMoves / getAllValidNarrowMoves());
226 }
227
228 /**
229 * WARNING: Assumes strictly bifurcating tree.
230 */
231 public double wide() {
232
233 NodeRef i = null, iP = null, j = null, jP = null;
234 int tries = 0;
235
236 //Echoose
237
238 while (tries < MAX_TRIES) {
239 i = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
240 while (tree.getRoot() == i) {
241 i = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
242 }
243
244 j = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
245 while (j == i || j == tree.getRoot()) {
246 j = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
247 }
248
249 iP = tree.getParent(i);
250 jP = tree.getParent(j);
251
252 if ((iP != jP) && (i != jP) && (j != iP) &&
253 (tree.getNodeHeight(j) < tree.getNodeHeight(iP)) &&
254 (tree.getNodeHeight(i) < tree.getNodeHeight(jP))) {
255 break;
256 }
257 tries += 1;
258 }
259 //System.out.println("tries = " + tries);
260
261 //Eupdate
262 if (tries < MAX_TRIES) {
263 try {
264 eupdateARG(i, j, iP, jP);
265 } catch (ARGOperatorFailedException e) {
266 return Double.NEGATIVE_INFINITY;
267 }
268 } else {
269 // throw new ARGOperatorFailedException("Couldn't find valid wide move on this tree!");
270 return Double.NEGATIVE_INFINITY;
271 }
272 return 0.0;
273 }
274
275 public int getMode() {
276 return mode;
277 }
278
279 public String getOperatorName() {
280 return ((mode == NARROW) ? "Narrow" : "Wide") + " Exchange";
281 }
282
283 private void eupdateARG(NodeRef i, NodeRef j, NodeRef iP, NodeRef jP) throws ARGOperatorFailedException {
284
285 // There are three different cases:
286 // 1) neither i nor j are reassortments, 2) either i or j are reassortments, 3) both i and j are reassortments
287
288 tree.beginTreeEdit();
289
290 boolean iBifurcation = tree.isBifurcation(i);
291 boolean jBifurcation = tree.isBifurcation(j);
292
293 if (iBifurcation && jBifurcation) {
294 tree.removeChild(iP, i);
295 tree.removeChild(jP, j);
296 tree.addChild(jP, i);
297 tree.addChild(iP, j);
298 } else if (!iBifurcation && !jBifurcation) {
299 // tree.singleRemoveChild(iP, i);
300 // tree.singleRemoveChild(jP, j);
301 // tree.singleAddChild(jP, i);
302 // tree.singleAddChild(iP, j);
303 } else {
304 if (jBifurcation) {
305 // one reassortment; force i to be bifurcation and j to be reassortment
306 NodeRef t = i;
307 NodeRef tP = iP;
308 i = j;
309 iP = jP;
310 j = t;
311 jP = tP;
312 }
313 // System.err.println(tree.toGraphString());
314 // ARGModel.Node iNode = (ARGModel.Node) i;
315 // ARGModel.Node jNode = (ARGModel.Node) j;
316 // System.err.println("i = "+iNode.number+" : j = "+jNode.number);
317 /* tree.removeChild(iP, i);
318 tree.singleRemoveChild(jP, j);
319 tree.addChild(jP, i);
320 tree.singleAddChild(iP, j);*/
321 }
322
323 tree.endTreeEdit();
324 try {
325 tree.checkTreeIsValid();
326 } catch (MutableTree.InvalidTreeException ite) {
327 throw new ARGOperatorFailedException(ite.toString());
328 }
329 }
330
331 public double getMinimumAcceptanceLevel() {
332 if (mode == NARROW) return 0.05;
333 else return 0.01;
334 }
335
336 public double getMinimumGoodAcceptanceLevel() {
337 if (mode == NARROW) return 0.05;
338 else return 0.01;
339 }
340
341 public static XMLObjectParser NARROW_EXCHANGE_PARSER = new AbstractXMLObjectParser() {
342
343 public String getParserName() {
344 return NARROW_EXCHANGE;
345 }
346
347 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
348
349 ARGModel treeModel = (ARGModel) xo.getChild(ARGModel.class);
350 int weight = xo.getIntegerAttribute("weight");
351
352 return new ARGExchangeOperator(NARROW, treeModel, weight);
353 }
354
355 //************************************************************************
356 // AbstractXMLObjectParser implementation
357 //************************************************************************
358
359 public String getParserDescription() {
360 return "This element represents a narrow exchange operator. " +
361 "This operator swaps a random subtree with its uncle.";
362 }
363
364 public Class getReturnType() {
365 return ExchangeOperator.class;
366 }
367
368 public XMLSyntaxRule[] getSyntaxRules() {
369 return rules;
370 }
371
372 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
373 AttributeRule.newIntegerRule("weight"),
374 new ElementRule(ARGModel.class)
375 };
376
377 };
378
379 public static XMLObjectParser WIDE_EXCHANGE_PARSER = new AbstractXMLObjectParser() {
380
381 public String getParserName() {
382 return WIDE_EXCHANGE;
383 }
384
385 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
386
387 ARGModel treeModel = (ARGModel) xo.getChild(ARGModel.class);
388 int weight = xo.getIntegerAttribute("weight");
389
390 return new ARGExchangeOperator(WIDE, treeModel, weight);
391 }
392
393 //************************************************************************
394 // AbstractXMLObjectParser implementation
395 //************************************************************************
396
397 public String getParserDescription() {
398 return "This element represents a wide exchange operator. " +
399 "This operator swaps two random subtrees.";
400 }
401
402 public Class getReturnType() {
403 return ExchangeOperator.class;
404 }
405
406 public XMLSyntaxRule[] getSyntaxRules() {
407 return rules;
408 }
409
410 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
411 AttributeRule.newIntegerRule("weight"),
412 new ElementRule(ARGModel.class)
413 };
414
415 };
416 }