| 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.inferencexml.operators; |
| 27 |
|
| 28 |
import dr.inference.model.Parameter; |
| 29 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 30 |
import dr.inference.operators.AdaptationMode; |
| 31 |
import dr.inference.operators.DeltaExchangeOperator; |
| 32 |
import dr.inference.operators.MCMCOperator; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class DeltaExchangeOperatorParser extends AbstractXMLObjectParser { |
| 38 |
|
| 39 |
public static final String DELTA_EXCHANGE = "deltaExchange"; |
| 40 |
public static final String DELTA = "delta"; |
| 41 |
public static final String INTEGER_OPERATOR = "integer"; |
| 42 |
public static final String PARAMETER_WEIGHTS = "parameterWeights"; |
| 43 |
|
| 44 |
public String getParserName() { |
| 45 |
return DELTA_EXCHANGE; |
| 46 |
} |
| 47 |
|
| 48 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 49 |
|
| 50 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 51 |
|
| 52 |
final boolean isIntegerOperator = xo.getAttribute(INTEGER_OPERATOR, false); |
| 53 |
|
| 54 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 55 |
double delta = xo.getDoubleAttribute(DELTA); |
| 56 |
|
| 57 |
if (delta <= 0.0) { |
| 58 |
throw new XMLParseException("delta must be greater than 0.0"); |
| 59 |
} |
| 60 |
|
| 61 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 62 |
|
| 63 |
|
| 64 |
int[] parameterWeights; |
| 65 |
if (xo.hasAttribute(PARAMETER_WEIGHTS)) { |
| 66 |
parameterWeights = xo.getIntegerArrayAttribute(PARAMETER_WEIGHTS); |
| 67 |
System.out.print("Parameter weights for delta exchange are: "); |
| 68 |
for (int parameterWeight : parameterWeights) { |
| 69 |
System.out.print(parameterWeight + "\t"); |
| 70 |
} |
| 71 |
System.out.println(); |
| 72 |
|
| 73 |
} else { |
| 74 |
parameterWeights = new int[parameter.getDimension()]; |
| 75 |
for (int i = 0; i < parameterWeights.length; i++) { |
| 76 |
parameterWeights[i] = 1; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (parameterWeights.length != parameter.getDimension()) { |
| 81 |
throw new XMLParseException("parameter weights have the same length as parameter"); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
return new DeltaExchangeOperator(parameter, parameterWeights, delta, weight, isIntegerOperator, mode); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public String getParserDescription() { |
| 93 |
return "This element returns a delta exchange operator on a given parameter."; |
| 94 |
} |
| 95 |
|
| 96 |
public Class getReturnType() { |
| 97 |
return MCMCOperator.class; |
| 98 |
} |
| 99 |
|
| 100 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 101 |
return rules; |
| 102 |
} |
| 103 |
|
| 104 |
private final XMLSyntaxRule[] rules = { |
| 105 |
AttributeRule.newDoubleRule(DELTA), |
| 106 |
AttributeRule.newIntegerArrayRule(PARAMETER_WEIGHTS, true), |
| 107 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 108 |
AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true), |
| 109 |
AttributeRule.newBooleanRule(INTEGER_OPERATOR, true), |
| 110 |
new ElementRule(Parameter.class) |
| 111 |
}; |
| 112 |
} |