| 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.model.Variable; |
| 30 |
import dr.inference.operators.MCMCOperator; |
| 31 |
import dr.inference.operators.UniformIntegerOperator; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class UniformIntegerOperatorParser extends AbstractXMLObjectParser { |
| 37 |
|
| 38 |
public final static String UNIFORM_INTEGER_OPERATOR = "uniformIntegerOperator"; |
| 39 |
|
| 40 |
public String getParserName() { |
| 41 |
return UNIFORM_INTEGER_OPERATOR; |
| 42 |
} |
| 43 |
|
| 44 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 45 |
|
| 46 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 47 |
|
| 48 |
Variable parameter = (Variable) xo.getChild(Variable.class); |
| 49 |
|
| 50 |
int count = 1; |
| 51 |
if (xo.hasAttribute("count")) count = xo.getIntegerAttribute("count"); |
| 52 |
|
| 53 |
if (parameter instanceof Parameter) { |
| 54 |
int lower = (int) (double) ((Parameter) parameter).getBounds().getLowerLimit(0); |
| 55 |
if (xo.hasAttribute("lower")) lower = xo.getIntegerAttribute("lower"); |
| 56 |
|
| 57 |
int upper = (int) (double) ((Parameter) parameter).getBounds().getUpperLimit(0); |
| 58 |
if (xo.hasAttribute("upper")) upper = xo.getIntegerAttribute("upper"); |
| 59 |
|
| 60 |
if (upper == lower || lower == (int) Double.NEGATIVE_INFINITY || upper == (int) Double.POSITIVE_INFINITY) { |
| 61 |
throw new XMLParseException(this.getParserName() + " boundaries not found in parameter " |
| 62 |
+ parameter.getId() + " Use operator lower and upper !"); |
| 63 |
} |
| 64 |
|
| 65 |
return new UniformIntegerOperator((Parameter) parameter, lower, upper, weight, count); |
| 66 |
} else { |
| 67 |
return new UniformIntegerOperator(parameter, weight, count); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public String getParserDescription() { |
| 76 |
return "An operator that picks new parameter values uniformly at random."; |
| 77 |
} |
| 78 |
|
| 79 |
public Class getReturnType() { |
| 80 |
return UniformIntegerOperator.class; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 85 |
return rules; |
| 86 |
} |
| 87 |
|
| 88 |
private final XMLSyntaxRule[] rules = { |
| 89 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 90 |
AttributeRule.newDoubleRule("upper", true), |
| 91 |
AttributeRule.newDoubleRule("lower", true), |
| 92 |
AttributeRule.newDoubleRule("count", true), |
| 93 |
new ElementRule(Variable.class) |
| 94 |
}; |
| 95 |
} |