Debian Codegraph @

src/dr/evomodel/operators/TreeBitRandomWalkOperator.java

raw symbols SCIP
1 /*
2 * TreeBitRandomWalkOperator.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.evomodelxml.operators.TreeBitRandomWalkOperatorParser;
31 import dr.inference.operators.SimpleMCMCOperator;
32 import dr.math.MathUtils;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * A generic operator that randomly selects a 1-bit in the tree and choose a new
39 * location nearby, by doing k random steps on the tree with equal weights on the
40 * parent and two children for each random step. The 1 bit is swapped with the bit
41 * at the new location, optionally the associated variable values are swapped as well.
42 *
43 * @author Alexei Drummond
44 * @version $Id$
45 */
46 public class TreeBitRandomWalkOperator extends SimpleMCMCOperator {
47
48 public TreeBitRandomWalkOperator(TreeModel tree, String t1, String t2, double weight, int k, boolean swapTrait2) {
49 this.tree = tree;
50 this.indicatorTrait = t1;
51 this.trait2 = t2;
52
53 if (indicatorTrait == null) indicatorTrait = "trait";
54 this.k = k;
55
56 this.swapTrait2 = swapTrait2;
57
58 setWeight(weight);
59 }
60
61 /**
62 * Pick a parent-child node pair involving a single rate change and swap the rate change location
63 * and corresponding rate parameters.
64 */
65 public final double doOperation() {
66
67 // 1. collect nodes that form a pair with parent such that
68 // one of them has a one and one has a zero
69 List<NodeRef> candidates = new ArrayList<NodeRef>();
70
71 for (int i = 0; i < tree.getNodeCount(); i++) {
72 NodeRef node = tree.getNode(i);
73 if (tree.getNodeTrait(node, indicatorTrait) == 1.0) candidates.add(node);
74 }
75
76 if (candidates.size() == 0) throw new RuntimeException("No suitable bits!");
77
78 NodeRef node = candidates.get(MathUtils.nextInt(candidates.size()));
79
80 NodeRef newNode = node;
81
82 for (int i = 0; i < k; i++) {
83 int randomNode = MathUtils.nextInt(3);
84 if (randomNode < 2) {
85 if (!tree.isExternal(newNode)) {
86 newNode = tree.getChild(newNode, randomNode);
87 }
88 } else if (!tree.isRoot(newNode)) {
89 newNode = tree.getParent(newNode);
90 }
91 }
92
93 // this shortcut avoids unnecessary likelihood calculations
94 if (node == newNode) {
95 // this used to throw a OperatorFailedException which would have rejected
96 // the move. This has been deprecated to returning a logq of -Inf.
97 return Double.NEGATIVE_INFINITY;
98 }
99
100 double nodeTrait, newTrait;
101 double nodeRate, newRate;
102
103 nodeTrait = tree.getNodeTrait(node, indicatorTrait);
104 newTrait = tree.getNodeTrait(newNode, indicatorTrait);
105
106 tree.setNodeTrait(node, indicatorTrait, newTrait);
107 tree.setNodeTrait(newNode, indicatorTrait, nodeTrait);
108
109 if (swapTrait2) {
110 if (trait2 != null) {
111 nodeTrait = tree.getNodeTrait(node, trait2);
112 newTrait = tree.getNodeTrait(newNode, trait2);
113
114 tree.setNodeTrait(node, trait2, newTrait);
115 tree.setNodeTrait(newNode, trait2, nodeTrait);
116 } else {
117 nodeRate = tree.getNodeRate(node);
118 newRate = tree.getNodeRate(newNode);
119
120 tree.setNodeRate(node, newRate);
121 tree.setNodeRate(newNode, nodeRate);
122 }
123 }
124
125 return 0.0;
126 }
127
128 public final int rateChange(TreeModel tree, NodeRef node) {
129 return (int) Math.round(tree.getNodeTrait(node, indicatorTrait));
130 }
131
132 // Interface MCMCOperator
133 public final String getOperatorName() {
134 return TreeBitRandomWalkOperatorParser.BIT_RANDOM_WALK_OPERATOR + "(" + indicatorTrait + ")";
135 }
136
137 public final String getPerformanceSuggestion() {
138
139 return "no performance suggestion";
140 }
141
142 public String toString() {
143 return getOperatorName();
144 }
145
146 // Private instance variables
147
148 private TreeModel tree;
149 private String indicatorTrait;
150 private String trait2;
151 private int k;
152 private boolean swapTrait2 = true;
153 }