Debian Codegraph @

src/dr/inferencexml/operators/UniformIntegerOperatorParser.java

raw symbols SCIP
1 /*
2 * UniformIntegerOperatorParser.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.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 { // Variable<Integer>, Bounds.Staircase
67 return new UniformIntegerOperator(parameter, weight, count);
68 }
69 }
70
71 //************************************************************************
72 // AbstractXMLObjectParser implementation
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 }