Debian Codegraph @

src/dr/evomodel/operators/TipTraitSwapOperator.java

raw symbols SCIP
1 /*
2 * TipTraitSwapOperator.java
3 *
4 * Copyright (c) 2002-2015 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.continuous.IntegratedMultivariateTraitLikelihood;
29 import dr.inference.operators.SimpleMCMCOperator;
30 import dr.math.MathUtils;
31 import dr.xml.*;
32
33 /**
34 * @author Marc A. Suchard
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 // Choose two tips to swap
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 // There is currently no restore functions for tip states, so manually adjust state
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 // AbstractXMLObjectParser implementation
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 }