Debian Codegraph @

src/dr/inference/operators/RateBitExchangeOperator.java

raw symbols SCIP
1 /*
2 * RateBitExchangeOperator.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.inference.operators;
27
28 import dr.xml.*;
29 import dr.inference.model.Parameter;
30 import dr.math.MathUtils;
31
32 /**
33 * @author Marc Suchard
34 */
35 public class RateBitExchangeOperator extends SimpleMCMCOperator {
36
37 public static final String OPERATOR_NAME = "rateBitExchangeOperator";
38 public static final String BITS = "bits";
39 public static final String RATES = "rates";
40 public static final String MAX_TRIES = "maxTries";
41 private static final int MAX_TRIES_MAGIC_NUMBER = 1000;
42
43 RateBitExchangeOperator(Parameter rateParameter, Parameter bitParameter, double weight,
44 int maxTries) {
45 this.rateParameter = rateParameter;
46 this.bitParameter = bitParameter;
47 setWeight(weight);
48 this.maxTries = maxTries;
49 }
50
51 public double doOperation() {
52 int dim = rateParameter.getDimension() / 2;
53 int tries = 0;
54
55 // Find a pair-set in which at least one bit is non-zero
56 int index = -1;
57 do {
58 index = MathUtils.nextInt(dim);
59 ++tries;
60 } while( (bitParameter.getParameterValue(index) + bitParameter.getParameterValue(index+dim) < 1) &&
61 (tries < maxTries) );
62
63 if (tries >= maxTries) {
64 // throw new RuntimeException("Too many attempts");
65 return Double.NEGATIVE_INFINITY;
66 }
67
68 // Swap (bit,rate) values
69 double tmpBit = bitParameter.getParameterValue(index);
70 double tmpRate = rateParameter.getParameterValue(index);
71 bitParameter.setParameterValue(index, bitParameter.getParameterValue(index+dim));
72 rateParameter.setParameterValue(index, rateParameter.getParameterValue(index+dim));
73 bitParameter.setParameterValue(index+dim,tmpBit);
74 rateParameter.setParameterValue(index+dim,tmpRate);
75
76 return 0;
77 }
78
79 // Interface MCMCOperator
80 public final String getOperatorName() {
81 return OPERATOR_NAME + "(" + bitParameter.getParameterName() +"," + rateParameter.getParameterName() + ")";
82 }
83
84 public final String getPerformanceSuggestion() {
85 return "No performance suggestion";
86 }
87
88 public String toString() {
89 return getOperatorName();
90 }
91
92 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
93
94 public String getParserName() {
95 return OPERATOR_NAME;
96 }
97
98 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
99
100 double weight = xo.getDoubleAttribute(WEIGHT);
101
102 Parameter ratesParameter = (Parameter) ((XMLObject)xo.getChild(RATES)).getChild(Parameter.class);
103 Parameter bitsParameter = (Parameter) ((XMLObject)xo.getChild(BITS)).getChild(Parameter.class);
104
105 int maxTries = xo.getAttribute(MAX_TRIES, MAX_TRIES_MAGIC_NUMBER);
106
107 return new RateBitExchangeOperator(ratesParameter,bitsParameter, weight, maxTries);
108 }
109
110 //************************************************************************
111 // AbstractXMLObjectParser implementation
112 //************************************************************************
113
114 public String getParserDescription() {
115 return "This element returns a bit-flip operator on a given parameter.";
116 }
117
118 public Class getReturnType() {
119 return MCMCOperator.class;
120 }
121
122 public XMLSyntaxRule[] getSyntaxRules() {
123 return rules;
124 }
125
126 private final XMLSyntaxRule[] rules = {
127 AttributeRule.newDoubleRule(WEIGHT),
128 new ElementRule(BITS, new XMLSyntaxRule[] {
129 new ElementRule(Parameter.class)
130 }),
131 new ElementRule(RATES, new XMLSyntaxRule[] {
132 new ElementRule(Parameter.class)
133 }),
134 AttributeRule.newIntegerRule(MAX_TRIES, true),
135 };
136
137 };
138
139 // Private instance variables
140 private Parameter bitParameter = null;
141 private Parameter rateParameter = null;
142 final private int maxTries;
143 }