| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 63 |
|
| 64 |
|
| 65 |
public final double doOperation() { |
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 94 |
if (node == newNode) { |
| 95 |
|
| 96 |
|
| 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 |
|
| 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 |
|
| 147 |
|
| 148 |
private TreeModel tree; |
| 149 |
private String indicatorTrait; |
| 150 |
private String trait2; |
| 151 |
private int k; |
| 152 |
private boolean swapTrait2 = true; |
| 153 |
} |