Debian Codegraph @

src/dr/evomodel/operators/TipStateSwapOperator.java

raw symbols SCIP
1 /*
2 * TipStateSwapOperator.java
3 *
4 * Copyright (c) 2002-2016 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.evomodel.treelikelihood.AncestralStateBeagleTreeLikelihood;
29 import dr.inference.operators.SimpleMCMCOperator;
30 import dr.math.MathUtils;
31
32 /**
33 * @author Marc A. Suchard
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 // Choose two tips to swap
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 // There is currently no restore functions for tip states, so manually adjust state
81 swap(index1, index2);
82 }
83
84 public String getOperatorName() {
85 return TIP_STATE_OPERATOR;
86 }
87
88 private final AncestralStateBeagleTreeLikelihood treeLikelihood;
89 }