| 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.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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |