Debian Codegraph @

src/dr/inferencexml/operators/ScaleOperatorParser.java

raw symbols SCIP
1 /*
2 * ScaleOperatorParser.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.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.ScaleOperator;
34 import dr.xml.*;
35
36 /**
37 */
38 public class ScaleOperatorParser extends AbstractXMLObjectParser {
39 public static final String SCALE_OPERATOR = "scaleOperator";
40 public static final String SCALE_ALL = "scaleAll";
41 public static final String SCALE_ALL_IND = "scaleAllIndependently";
42 public static final String SCALE_FACTOR = "scaleFactor";
43 public static final String DEGREES_OF_FREEDOM = "df";
44 public static final String INDICATORS = "indicators";
45 public static final String PICKONEPROB = "pickoneprob";
46 public static final String IGNORE_BOUNDS = "ignoreBounds";
47
48 public String getParserName() {
49 return SCALE_OPERATOR;
50 }
51
52 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
53
54 final boolean scaleAll = xo.getAttribute(SCALE_ALL, false);
55 final boolean scaleAllInd = xo.getAttribute(SCALE_ALL_IND, false);
56 final int degreesOfFreedom = xo.getAttribute(DEGREES_OF_FREEDOM, 0);
57 final boolean ignoreBounds = xo.getAttribute(IGNORE_BOUNDS, false);
58
59 final AdaptationMode mode = AdaptationMode.parseMode(xo);
60
61 final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
62 final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
63
64 if (scaleFactor <= 0.0 || scaleFactor >= 1.0) {
65 throw new XMLParseException("scaleFactor must be between 0.0 and 1.0");
66 }
67
68 final Parameter parameter = (Parameter) xo.getChild(Parameter.class);
69 Bounds<Double> bounds = parameter.getBounds();
70
71 Boolean negativeSpace = null;
72
73 for (int dim = 0; dim < parameter.getDimension(); dim++) {
74
75 if (parameter.getParameterValue(dim) < 0.0) {
76 // the parameter is in negative space
77
78 if (negativeSpace != null && !negativeSpace) {
79 throw new XMLParseException("Scale operator can only be used on parameters where all elements are strictly positive or negative (" + parameter.getId() + ")");
80 }
81
82 negativeSpace = true;
83
84 if (bounds.getUpperLimit(dim) > 0.0) {
85 throw new XMLParseException("Scale operator can only be used on parameters constrained to be strictly positive or negative (" + parameter.getId() + ")");
86 }
87 if (!ignoreBounds && !Double.isInfinite(bounds.getLowerLimit(dim))) {
88 throw new XMLParseException("Scale operator can only be used on parameters with an infinite upper or lower bound (use a RandomWalk) (" + parameter.getId() + ")");
89 }
90 } else if (parameter.getParameterValue(dim) > 0.0) {
91 if (negativeSpace != null && negativeSpace) {
92 throw new XMLParseException("Scale operator can only be used on parameters where all elements are strictly positive or negative (" + parameter.getId() + ")");
93 }
94
95 negativeSpace = false;
96
97 if (bounds.getLowerLimit(dim) < 0.0) {
98 throw new XMLParseException("Scale operator can only be used on parameters constrained to be strictly positive or negative (" + parameter.getId() + ")");
99 }
100 if (!ignoreBounds && !Double.isInfinite(bounds.getUpperLimit(dim))) {
101 throw new XMLParseException("Scale operator can only be used on parameters with an infinite upper or lower bound (use a RandomWalk) (" + parameter.getId() + ")");
102 }
103 } else {
104 // disallow zero starting values
105 throw new XMLParseException("Scale operator can only be used on parameters where all elements are strictly positive or negative (" + parameter.getId() + ")");
106 }
107 }
108
109 Parameter indicator = null;
110 double indicatorOnProb = 1.0;
111 final XMLObject inds = xo.getChild(INDICATORS);
112
113 if (inds != null) {
114 indicator = (Parameter) inds.getChild(Parameter.class);
115 if (inds.hasAttribute(PICKONEPROB)) {
116 indicatorOnProb = inds.getDoubleAttribute(PICKONEPROB);
117 if (!(0 <= indicatorOnProb && indicatorOnProb <= 1)) {
118 throw new XMLParseException("pickoneprob must be between 0.0 and 1.0");
119 }
120 }
121 }
122 ScaleOperator operator = new ScaleOperator(parameter, scaleAll,
123 degreesOfFreedom, scaleFactor,
124 mode, indicator, indicatorOnProb,
125 scaleAllInd);
126 operator.setWeight(weight);
127 return operator;
128 }
129
130 //************************************************************************
131 // AbstractXMLObjectParser implementation
132 //************************************************************************
133
134 public String getParserDescription() {
135 return "This element returns a scale operator on a given parameter.";
136 }
137
138 public Class getReturnType() {
139 return ScaleOperator.class;
140 }
141
142 public XMLSyntaxRule[] getSyntaxRules() {
143 return rules;
144 }
145
146 private final XMLSyntaxRule[] rules = {
147 AttributeRule.newDoubleRule(SCALE_FACTOR),
148 AttributeRule.newBooleanRule(SCALE_ALL, true),
149 AttributeRule.newBooleanRule(SCALE_ALL_IND, true),
150 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
151 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
152 AttributeRule.newIntegerRule(DEGREES_OF_FREEDOM, true),
153 AttributeRule.newBooleanRule(IGNORE_BOUNDS, true),
154
155 new ElementRule(Parameter.class),
156 new ElementRule(INDICATORS,
157 new XMLSyntaxRule[]{
158 AttributeRule.newDoubleRule(PICKONEPROB, true),
159 new ElementRule(Parameter.class)}, true),
160 };
161 }