| 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.treelikelihood.AncestralStateBeagleTreeLikelihood; |
| 29 |
import dr.inference.operators.SimpleMCMCOperator; |
| 30 |
import dr.math.MathUtils; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class TipStateSwapOperator extends SimpleMCMCOperator { |
| 36 |
|
| 37 |
public static final String TIP_STATE_OPERATOR = "tipStateSwapOperator"; |
| 38 |
|
| 39 |
public TipStateSwapOperator(AncestralStateBeagleTreeLikelihood treeLikelihood, double weight) { |
| 40 |
this.treeLikelihood = treeLikelihood; |
| 41 |
setWeight(weight); |
| 42 |
int patternCount = treeLikelihood.getPatternCount(); |
| 43 |
states1 = new int[patternCount]; |
| 44 |
states2 = new int[patternCount]; |
| 45 |
} |
| 46 |
|
| 47 |
private final int[] states1; |
| 48 |
private final int[] states2; |
| 49 |
private int index1; |
| 50 |
private int index2; |
| 51 |
|
| 52 |
public double doOperation() { |
| 53 |
|
| 54 |
int tipCount = treeLikelihood.getTreeModel().getExternalNodeCount(); |
| 55 |
|
| 56 |
|
| 57 |
index1 = MathUtils.nextInt(tipCount); |
| 58 |
index2 = index1; |
| 59 |
while (index2 == index1) |
| 60 |
index2 = MathUtils.nextInt(tipCount); |
| 61 |
|
| 62 |
swap(index1, index2); |
| 63 |
|
| 64 |
treeLikelihood.makeDirty(); |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
private void swap(int i, int j) { |
| 70 |
|
| 71 |
treeLikelihood.getTipStates(i, states1); |
| 72 |
treeLikelihood.getTipStates(j, states2); |
| 73 |
|
| 74 |
treeLikelihood.setTipStates(j, states1); |
| 75 |
treeLikelihood.setTipStates(i, states2); |
| 76 |
} |
| 77 |
|
| 78 |
public void reject() { |
| 79 |
super.reject(); |
| 80 |
|
| 81 |
swap(index1, index2); |
| 82 |
} |
| 83 |
|
| 84 |
public String getOperatorName() { |
| 85 |
return TIP_STATE_OPERATOR; |
| 86 |
} |
| 87 |
|
| 88 |
private final AncestralStateBeagleTreeLikelihood treeLikelihood; |
| 89 |
} |