Debian Codegraph @

src/dr/evomodel/operators/TreeBitMoveOperator.java

raw symbols SCIP
1 /*
2 * TreeBitMoveOperator.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
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37 * A generic operator swaps a randomly selected rate change from parent to offspring or vice versa.
38 *
39 * @author Alexei Drummond
40 * @version $Id$
41 */
42 public class TreeBitMoveOperator extends SimpleMCMCOperator {
43
44 public TreeBitMoveOperator(TreeModel tree, String t1, String t2, double weight) {
45 this.tree = tree;
46 this.indicatorTrait = t1;
47 this.trait2 = t2;
48
49 if (indicatorTrait == null) indicatorTrait = "trait";
50
51 setWeight(weight);
52 }
53
54 /**
55 * Pick a parent-child node pair involving a single rate change and swap the rate change location
56 * and corresponding rate parameters.
57 */
58 public final double doOperation() {
59
60 NodeRef root = tree.getRoot();
61
62 // 1. collect nodes that form a pair with parent such that
63 // one of them has a one and one has a zero
64 List<NodeRef> candidates = new ArrayList<NodeRef>();
65
66 for (int i = 0; i < tree.getNodeCount(); i++) {
67
68 NodeRef node = tree.getNode(i);
69 if (node != root && tree.getParent(node) != root) {
70
71 NodeRef parent = tree.getParent(node);
72
73 int sum = rateChange(tree, node) + rateChange(tree, parent);
74
75 if (sum == 1) candidates.add(node);
76 }
77 }
78
79 if (candidates.size() == 0) throw new RuntimeException("No suitable pairs!");
80
81 NodeRef node = candidates.get(MathUtils.nextInt(candidates.size()));
82 NodeRef parent = tree.getParent(node);
83
84 double nodeTrait, parentTrait;
85 double nodeRate, parentRate;
86
87 nodeTrait = tree.getNodeTrait(node, indicatorTrait);
88 parentTrait = tree.getNodeTrait(parent, indicatorTrait);
89
90 tree.setNodeTrait(node, indicatorTrait, parentTrait);
91 tree.setNodeTrait(parent, indicatorTrait, nodeTrait);
92
93 if (trait2 != null) {
94 nodeTrait = tree.getNodeTrait(node, trait2);
95 parentTrait = tree.getNodeTrait(parent, trait2);
96
97 tree.setNodeTrait(node, trait2, parentTrait);
98 tree.setNodeTrait(parent, trait2, nodeTrait);
99 } else {
100 nodeRate = tree.getNodeRate(node);
101 parentRate = tree.getNodeRate(parent);
102
103 tree.setNodeRate(node, parentRate);
104 tree.setNodeRate(parent, nodeRate);
105 }
106
107 return 0.0;
108 }
109
110 public final int rateChange(TreeModel tree, NodeRef node) {
111 return (int) Math.round(tree.getNodeTrait(node, indicatorTrait));
112 }
113
114 // Interface MCMCOperator
115 public final String getOperatorName() {
116 return "treeBitMove()";
117 }
118
119 public final String getPerformanceSuggestion() {
120
121 return "no performance suggestion";
122 }
123
124 public String toString() {
125 return getOperatorName();
126 }
127 // Private instance variables
128
129 private TreeModel tree;
130 private String indicatorTrait;
131 private String trait2;
132 }