| 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.Bounds; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 31 |
import dr.inference.operators.AdaptationMode; |
| 32 |
import dr.inference.operators.MCMCOperator; |
| 33 |
import dr.inference.operators.RandomWalkOperator; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class RandomWalkOperatorParser extends AbstractXMLObjectParser { |
| 39 |
|
| 40 |
public static final String RANDOM_WALK_OPERATOR = "randomWalkOperator"; |
| 41 |
public static final String WINDOW_SIZE = "windowSize"; |
| 42 |
public static final String UPDATE_INDEX = "updateIndex"; |
| 43 |
public static final String UPPER = "upper"; |
| 44 |
public static final String LOWER = "lower"; |
| 45 |
|
| 46 |
public static final String BOUNDARY_CONDITION = "boundaryCondition"; |
| 47 |
|
| 48 |
public String getParserName() { |
| 49 |
return RANDOM_WALK_OPERATOR; |
| 50 |
} |
| 51 |
|
| 52 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 53 |
|
| 54 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 55 |
|
| 56 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 57 |
double windowSize = xo.getDoubleAttribute(WINDOW_SIZE); |
| 58 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 59 |
|
| 60 |
if (xo.hasAttribute(LOWER) || xo.hasAttribute(UPPER)) { |
| 61 |
throw new XMLParseException("Do not provide lower/upper bounds on for a RandomWalkOperator; set these values are parameter bounds"); |
| 62 |
} |
| 63 |
|
| 64 |
RandomWalkOperator.BoundaryCondition condition = RandomWalkOperator.BoundaryCondition.valueOf( |
| 65 |
xo.getAttribute(BOUNDARY_CONDITION, RandomWalkOperator.BoundaryCondition.reflecting.name())); |
| 66 |
|
| 67 |
|
| 68 |
final Bounds<Double> bounds = parameter.getBounds(); |
| 69 |
final int dim = parameter.getDimension(); |
| 70 |
|
| 71 |
boolean lowerBoundsSet = true; |
| 72 |
boolean upperBoundsSet = true; |
| 73 |
for (int i = 0; i < dim; ++i) { |
| 74 |
if (bounds.getLowerLimit(i) == null || Double.isInfinite(bounds.getLowerLimit(i))) { |
| 75 |
lowerBoundsSet = false; |
| 76 |
} |
| 77 |
if (bounds.getUpperLimit(i) == null || Double.isInfinite(bounds.getUpperLimit(i))) { |
| 78 |
upperBoundsSet = false; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (condition == RandomWalkOperator.BoundaryCondition.logit) { |
| 83 |
if (!lowerBoundsSet || !upperBoundsSet) { |
| 84 |
throw new XMLParseException("The logit transformed RandomWalkOperator cannot be used on a parameter without bounds."); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (condition == RandomWalkOperator.BoundaryCondition.log) { |
| 89 |
if (!lowerBoundsSet) { |
| 90 |
throw new XMLParseException("The log transformed RandomWalkOperator cannot be used on a parameter without lower bounds."); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (xo.hasChildNamed(UPDATE_INDEX)) { |
| 95 |
XMLObject cxo = xo.getChild(UPDATE_INDEX); |
| 96 |
Parameter updateIndex = (Parameter) cxo.getChild(Parameter.class); |
| 97 |
if (updateIndex.getDimension() != parameter.getDimension()) |
| 98 |
throw new RuntimeException("Parameter to update and missing indices must have the same dimension"); |
| 99 |
return new RandomWalkOperator(parameter, updateIndex, windowSize, condition, |
| 100 |
weight, mode); |
| 101 |
} |
| 102 |
|
| 103 |
return new RandomWalkOperator(parameter, null, windowSize, condition, weight, mode); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public String getParserDescription() { |
| 111 |
return "This element returns a random walk operator on a given parameter."; |
| 112 |
} |
| 113 |
|
| 114 |
public Class getReturnType() { |
| 115 |
return MCMCOperator.class; |
| 116 |
} |
| 117 |
|
| 118 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 119 |
return rules; |
| 120 |
} |
| 121 |
|
| 122 |
private final XMLSyntaxRule[] rules = { |
| 123 |
AttributeRule.newDoubleRule(WINDOW_SIZE), |
| 124 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 125 |
AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true), |
| 126 |
new ElementRule(UPDATE_INDEX, |
| 127 |
new XMLSyntaxRule[] { |
| 128 |
new ElementRule(Parameter.class), |
| 129 |
},true), |
| 130 |
new StringAttributeRule(BOUNDARY_CONDITION, null, RandomWalkOperator.BoundaryCondition.values(), true), |
| 131 |
new ElementRule(Parameter.class) |
| 132 |
}; |
| 133 |
} |