Debian Codegraph @

src/dr/evomodel/operators/ExchangeOperator.java

raw symbols SCIP
1 /*
2 * ExchangeOperator.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.math.MathUtils;
31
32 /**
33 * Implements branch exchange operations. There is a NARROW and WIDE variety.
34 * The narrow exchange is very similar to a rooted-tree nearest-neighbour
35 * interchange but with the restriction that node height must remain consistent.
36 * <p/>
37 * KNOWN BUGS: WIDE operator cannot be used on trees with 4 or fewer tips!
38 */
39 public class ExchangeOperator extends AbstractTreeOperator {
40
41 public static final int NARROW = 0;
42 public static final int WIDE = 1;
43
44 private static final int MAX_TRIES = 100;
45
46 private int mode = NARROW;
47 private final TreeModel tree;
48
49 private double[] distances;
50
51 public ExchangeOperator(int mode, TreeModel tree, double weight) {
52 this.mode = mode;
53 this.tree = tree;
54 setWeight(weight);
55 }
56
57 public double doOperation() {
58
59 final int tipCount = tree.getExternalNodeCount();
60
61 double hastingsRatio;
62
63 switch( mode ) {
64 case NARROW:
65 hastingsRatio = (narrow() ? 0.0 : Double.NEGATIVE_INFINITY);
66 break;
67 case WIDE:
68 hastingsRatio = (wide() ? 0.0 : Double.NEGATIVE_INFINITY);
69 break;
70 default:
71 throw new IllegalArgumentException("Unknow Exchange Mode");
72 }
73
74 assert tree.getExternalNodeCount() == tipCount :
75 "Lost some tips in " + ((mode == NARROW) ? "NARROW mode." : "WIDE mode.");
76
77 return hastingsRatio;
78 }
79
80 /**
81 * WARNING: Assumes strictly bifurcating tree.
82 */
83 public boolean narrow() {
84 final int nNodes = tree.getNodeCount();
85 final NodeRef root = tree.getRoot();
86
87 NodeRef i = root;
88
89 while( root == i || tree.getParent(i) == root ) {
90 i = tree.getNode(MathUtils.nextInt(nNodes));
91 }
92
93 final NodeRef iParent = tree.getParent(i);
94 final NodeRef iGrandParent = tree.getParent(iParent);
95 NodeRef iUncle = tree.getChild(iGrandParent, 0);
96 if( iUncle == iParent ) {
97 iUncle = tree.getChild(iGrandParent, 1);
98 }
99 assert iUncle == getOtherChild(tree, iGrandParent, iParent);
100
101 assert tree.getNodeHeight(i) <= tree.getNodeHeight(iGrandParent);
102
103 if( tree.getNodeHeight(iUncle) < tree.getNodeHeight(iParent) ) {
104 exchangeNodes(tree, i, iUncle, iParent, iGrandParent);
105
106 // exchangeNodes generates the events
107 //tree.pushTreeChangedEvent(iParent);
108 //tree.pushTreeChangedEvent(iGrandParent);
109 return true;
110 }
111
112 return false;
113 }
114
115 /**
116 * WARNING: Assumes strictly bifurcating tree.
117 */
118 public boolean wide() {
119
120 final int nodeCount = tree.getNodeCount();
121 final NodeRef root = tree.getRoot();
122
123 NodeRef i = root;
124
125 while( root == i ) {
126 i = tree.getNode(MathUtils.nextInt(nodeCount));
127 }
128
129 NodeRef j = i;
130 while( j == i || j == root ) {
131 j = tree.getNode(MathUtils.nextInt(nodeCount));
132 }
133
134 final NodeRef iP = tree.getParent(i);
135 final NodeRef jP = tree.getParent(j);
136
137 if( (iP != jP) && (i != jP) && (j != iP)
138 && (tree.getNodeHeight(j) < tree.getNodeHeight(iP))
139 && (tree.getNodeHeight(i) < tree.getNodeHeight(jP)) ) {
140 exchangeNodes(tree, i, j, iP, jP);
141 // System.out.println("tries = " + tries+1);
142 return true;
143 }
144
145 return false;
146 }
147
148 /* why not use Arrays.asList(a).indexOf(n) ? */
149 private int indexOf(NodeRef[] a, NodeRef n) {
150
151 for(int i = 0; i < a.length; i++) {
152 if( a[i] == n ) {
153 return i;
154 }
155 }
156 return -1;
157 }
158
159 private double getWinningChance(int index) {
160
161 double sum = 0;
162 for( double distance : distances ) {
163 sum += (1.0 / distance);
164 }
165
166 return (1.0 / distances[index]) / sum;
167
168 }
169
170 private void calcDistances(NodeRef[] nodes, NodeRef ref) {
171 distances = new double[nodes.length];
172 for(int i = 0; i < nodes.length; i++) {
173 distances[i] = getNodeDistance(ref, nodes[i]) + 1;
174 }
175 }
176
177 private NodeRef getRandomNode(NodeRef[] nodes, NodeRef ref) {
178
179 calcDistances(nodes, ref);
180 double sum = 0;
181 for( double distance : distances ) {
182 sum += 1.0 / distance;
183 }
184
185 double randomValue = MathUtils.nextDouble() * sum;
186 NodeRef n = null;
187 for(int i = 0; i < distances.length; i++) {
188 randomValue -= 1.0 / distances[i];
189
190 if( randomValue <= 0 ) {
191 n = nodes[i];
192 break;
193 }
194 }
195 return n;
196 }
197
198 private int getNodeDistance(NodeRef i, NodeRef j) {
199 int count = 0;
200
201 while( i != j ) {
202 count++;
203 if( tree.getNodeHeight(i) < tree.getNodeHeight(j) ) {
204 i = tree.getParent(i);
205 } else {
206 j = tree.getParent(j);
207 }
208 }
209 return count;
210 }
211
212 public int getMode() {
213 return mode;
214 }
215
216 public String getOperatorName() {
217 return ((mode == NARROW) ? "Narrow" : "Wide") + " Exchange" + "(" + tree.getId() + ")";
218 }
219
220 public double getMinimumAcceptanceLevel() {
221 if( mode == NARROW ) {
222 return 0.05;
223 } else {
224 return 0.01;
225 }
226 }
227
228 public double getMinimumGoodAcceptanceLevel() {
229 if( mode == NARROW ) {
230 return 0.05;
231 } else {
232 return 0.01;
233 }
234 }
235
236 public String getPerformanceSuggestion() {
237 return "";
238
239 // if( MCMCOperator.Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel() ) {
240 // return "";
241 // } else if( MCMCOperator.Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel() ) {
242 // return "";
243 // } else {
244 // return "";
245 // }
246 }
247
248 // public static final String INTERMEDIATE_EXCHANGE = "intermediateExchange";
249 /* The INTERMEDIATE_EXCHANGE is deprecated for unknown reason, therefore comment out its parser to make other 2 parsers registered properly
250 public static XMLObjectParser INTERMEDIATE_EXCHANGE_PARSER = new AbstractXMLObjectParser() {
251
252 public String getParserName() {
253 return INTERMEDIATE_EXCHANGE;
254 }
255
256 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
257
258 final TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
259 final double weight = xo.getDoubleAttribute("weight");
260
261 return new ExchangeOperator(INTERMEDIATE, treeModel, weight);
262 }
263
264 // ************************************************************************
265 // AbstractXMLObjectParser implementation
266 // ************************************************************************
267
268 public String getParserDescription() {
269 return "This element represents a intermediate exchange operator. "
270 + "This operator swaps two random subtrees.";
271 }
272
273 public Class getReturnType() {
274 return ExchangeOperator.class;
275 }
276
277 public XMLSyntaxRule[] getSyntaxRules() {
278 return rules;
279 }
280
281 private final XMLSyntaxRule[] rules = {
282 AttributeRule.newDoubleRule("weight"),
283 new ElementRule(TreeModel.class)};
284 }; */
285 }