Debian Codegraph @

src/dr/evomodel/operators/RLCNarrowExchangeOperator.java

raw symbols SCIP
1 /*
2 * RLCNarrowExchangeOperator.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.evomodel.tree.TreeModel;
30 import dr.inference.operators.SimpleMCMCOperator;
31 import dr.math.MathUtils;
32 import dr.xml.*;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * Implements narrow exchange operations while also changing the rates.
39 * The narrow exchange is very similar to a rooted-tree
40 * nearest-neighbour interchange but with the restriction
41 * that node height must remain consistent.
42 */
43 public class RLCNarrowExchangeOperator extends SimpleMCMCOperator {
44
45 public static final String NARROW_EXCHANGE = "narrowExchangeRLC";
46
47 private static final int MAX_TRIES = 10000;
48
49 private final TreeModel tree;
50
51 public RLCNarrowExchangeOperator(TreeModel tree, double weight) {
52 this.tree = tree;
53 setWeight(weight);
54 }
55
56 public double doOperation() {
57
58 int tipCount = tree.getExternalNodeCount();
59
60 narrow();
61
62 if (tree.getExternalNodeCount() != tipCount) {
63 throw new RuntimeException("Lost some tips in narrow exchange RLC");
64 }
65
66 return 0.0;
67 }
68
69 /**
70 * WARNING: Assumes strictly bifurcating tree.
71 */
72 public void narrow() {
73 final int nNodes = tree.getNodeCount();
74 final NodeRef root = tree.getRoot();
75
76 for (int tries = 0; tries < MAX_TRIES; ++tries) {
77 NodeRef i = tree.getNode(MathUtils.nextInt(nNodes));
78
79 while (root == i || tree.getParent(i) == root) {
80 i = tree.getNode(MathUtils.nextInt(nNodes));
81 }
82
83 final NodeRef iParent = tree.getParent(i);
84 final NodeRef iGrandParent = tree.getParent(iParent);
85 NodeRef iUncle = tree.getChild(iGrandParent, 0);
86 if (iUncle == iParent) {
87 iUncle = tree.getChild(iGrandParent, 1);
88 }
89
90 assert tree.getNodeHeight(i) < tree.getNodeHeight(iGrandParent);
91
92 if (tree.getNodeHeight(iUncle) < tree.getNodeHeight(iParent)) {
93
94 NodeRef iSister = tree.getChild(iParent, 0);
95 if (iSister == i) {
96 iSister = tree.getChild(iParent, 1);
97 }
98
99 eupdate(i, iUncle, iParent, iGrandParent, iSister);
100
101 tree.pushTreeChangedEvent(iParent);
102 tree.pushTreeChangedEvent(iGrandParent);
103 return;
104 }
105 }
106 //System.out.println("tries = " + tries);
107
108 throw new RuntimeException("Couldn't find valid narrow move on this tree!!");
109 }
110
111 public String getOperatorName() {
112 return "Narrow Exchange RLC";
113 }
114
115 /* exchange subtrees whose root are i and j */
116 private void eupdate(NodeRef i, NodeRef j, NodeRef iP, NodeRef jP, NodeRef iS) {
117
118 tree.beginTreeEdit();
119 tree.removeChild(iP, i);
120 tree.removeChild(jP, j);
121 tree.addChild(jP, i);
122 tree.addChild(iP, j);
123 tree.endTreeEdit();
124
125 List<NodeRef> nodes = new ArrayList<NodeRef>();
126 nodes.add(i);
127 nodes.add(iP);
128 nodes.add(j);
129 nodes.add(jP);
130 nodes.add(iS);
131
132 NodeRef a = nodes.remove(MathUtils.nextInt(nodes.size()));
133 NodeRef b = nodes.get(MathUtils.nextInt(nodes.size()));
134
135 //swap traits in these two nodes
136 double changedA = tree.getNodeTrait(a, "trait");
137 double changedB = tree.getNodeTrait(a, "trait");
138
139 tree.setNodeTrait(a, "trait", changedB);
140 tree.setNodeTrait(b, "trait", changedA);
141 }
142
143 public static XMLObjectParser NARROW_EXCHANGE_PARSER = new AbstractXMLObjectParser() {
144
145 public String getParserName() {
146 return NARROW_EXCHANGE;
147 }
148
149 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
150
151 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
152 double weight = xo.getDoubleAttribute("weight");
153
154 return new RLCNarrowExchangeOperator(treeModel, weight);
155 }
156
157 //************************************************************************
158 // AbstractXMLObjectParser implementation
159 //************************************************************************
160
161 public String getParserDescription() {
162 return "This element represents a narrow exchange operator. " +
163 "This operator swaps a random subtree with its uncle.";
164 }
165
166 public Class getReturnType() {
167 return RLCNarrowExchangeOperator.class;
168 }
169
170 public XMLSyntaxRule[] getSyntaxRules() {
171 return rules;
172 }
173
174 private final XMLSyntaxRule[] rules = {
175 AttributeRule.newDoubleRule("weight"),
176 new ElementRule(TreeModel.class)
177 };
178
179 };
180 }