| 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.evomodel.continuous.IntegratedMultivariateTraitLikelihood; |
| 29 |
import dr.inference.operators.SimpleMCMCOperator; |
| 30 |
import dr.math.MathUtils; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class TipTraitSwapOperator extends SimpleMCMCOperator { |
| 37 |
|
| 38 |
public static final String TIP_TRAIT_SWAP_OPERATOR = "tipTraitSwapOperator"; |
| 39 |
|
| 40 |
public TipTraitSwapOperator(String traitName, IntegratedMultivariateTraitLikelihood traitLikelihood, double weight) { |
| 41 |
this.traitLikelihood = traitLikelihood; |
| 42 |
this.traitName = traitName; |
| 43 |
setWeight(weight); |
| 44 |
} |
| 45 |
|
| 46 |
private int index1; |
| 47 |
private int index2; |
| 48 |
|
| 49 |
public double doOperation() { |
| 50 |
|
| 51 |
int tipCount = traitLikelihood.getTreeModel().getExternalNodeCount(); |
| 52 |
|
| 53 |
|
| 54 |
index1 = MathUtils.nextInt(tipCount); |
| 55 |
index2 = index1; |
| 56 |
while (index2 == index1) |
| 57 |
index2 = MathUtils.nextInt(tipCount); |
| 58 |
|
| 59 |
swap(index1, index2); |
| 60 |
|
| 61 |
traitLikelihood.makeDirty(); |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
private void swap(int i, int j) { |
| 67 |
double[] trait1 = traitLikelihood.getTipDataValues(i); |
| 68 |
double[] trait2 = traitLikelihood.getTipDataValues(j); |
| 69 |
|
| 70 |
traitLikelihood.setTipDataValuesForNode(j, trait1); |
| 71 |
traitLikelihood.setTipDataValuesForNode(i, trait2); |
| 72 |
} |
| 73 |
|
| 74 |
public void reject() { |
| 75 |
super.reject(); |
| 76 |
|
| 77 |
swap(index1, index2); |
| 78 |
} |
| 79 |
|
| 80 |
public String getOperatorName() { |
| 81 |
return TIP_TRAIT_SWAP_OPERATOR; |
| 82 |
} |
| 83 |
|
| 84 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 85 |
|
| 86 |
public String getParserName() { |
| 87 |
return TIP_TRAIT_SWAP_OPERATOR; |
| 88 |
} |
| 89 |
|
| 90 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 91 |
|
| 92 |
IntegratedMultivariateTraitLikelihood traitLikelihood = |
| 93 |
(IntegratedMultivariateTraitLikelihood) xo.getChild(IntegratedMultivariateTraitLikelihood.class); |
| 94 |
final String traitName = xo.getStringAttribute("trait"); |
| 95 |
final double weight = xo.getDoubleAttribute("weight"); |
| 96 |
return new TipTraitSwapOperator(traitName, traitLikelihood, weight); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public String getParserDescription() { |
| 104 |
return "This element represents an operator to swap tip traits between two random tips."; |
| 105 |
} |
| 106 |
|
| 107 |
public Class getReturnType() { |
| 108 |
return ExchangeOperator.class; |
| 109 |
} |
| 110 |
|
| 111 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 112 |
return rules; |
| 113 |
} |
| 114 |
|
| 115 |
private final XMLSyntaxRule[] rules = { |
| 116 |
AttributeRule.newStringRule("trait"), |
| 117 |
AttributeRule.newDoubleRule("weight"), |
| 118 |
new ElementRule(IntegratedMultivariateTraitLikelihood.class), |
| 119 |
}; |
| 120 |
|
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
private final IntegratedMultivariateTraitLikelihood traitLikelihood; |
| 125 |
private final String traitName; |
| 126 |
} |