| 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.inference.operators; |
| 27 |
|
| 28 |
import dr.xml.*; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.math.MathUtils; |
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 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 |
|
| 65 |
return Double.NEGATIVE_INFINITY; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 140 |
private Parameter bitParameter = null; |
| 141 |
private Parameter rateParameter = null; |
| 142 |
final private int maxTries; |
| 143 |
} |