Debian Codegraph @

src/dr/inference/model/ParameterIntegerParser.java

raw symbols SCIP
1 /*
2 * ParameterIntegerParser.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.inference.model;
27
28 import dr.xml.AttributeRule;
29 import dr.xml.XMLObject;
30 import dr.xml.XMLParseException;
31 import dr.xml.XMLSyntaxRule;
32
33 /**
34 * Parses a multi-dimensional continuous parameter.
35 *
36 * @author Alexei Drummond
37 * @author Andrew Rambaut
38 * @author Walter Xie
39 *
40 * @version $Id: ParameterParser.java,v 1.12 2005/05/24 20:26:00 rambaut Exp $
41 */
42 public class ParameterIntegerParser extends dr.xml.AbstractXMLObjectParser {
43
44 // public static final String UPPER = "upper";
45 // public static final String LOWER = "lower";
46 public static final String DIMENSION = "dimension";
47 public static final String VALUE = "value";
48 public static final String PARAMETER = "integerParameter";
49 // public static final String RANDOMIZE = "randomize";
50
51 public String getParserName() {
52 return PARAMETER;
53 }
54
55 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
56
57 int[] values = null;
58 // double[] uppers;
59 // double[] lowers;
60
61 // if( xo.hasAttribute(DIMENSION) ) {
62 // values = new int[xo.getIntegerAttribute(DIMENSION)];
63 // }
64
65 if( xo.hasAttribute(VALUE) ) {
66 if( values == null ) {
67 values = xo.getIntegerArrayAttribute(VALUE);
68 } else {
69 int[] v = xo.getIntegerArrayAttribute(VALUE);
70 if( v.length == values.length ) {
71 System.arraycopy(v, 0, values, 0, v.length);
72 } else if( v.length == 1 ) {
73 for(int i = 0; i < values.length; i++) {
74 values[i] = v[0];
75 }
76 } else {
77 throw new XMLParseException("value string must have 1 value or dimension values");
78 }
79 }
80 } else {
81 if( xo.hasAttribute(DIMENSION) ) {
82 values = new int[xo.getIntegerAttribute(DIMENSION)];
83 } else {
84 // parameter dimension will get set correctly by TreeModel presumably.
85 // if (!xo.hasChildNamed(RANDOMIZE)) {
86 // return new Parameter.Default(1);
87 // }
88 values = new int[1];
89 values[0] = 0;
90 }
91 }
92
93 // uppers = new double[values.length];
94 // for(int i = 0; i < values.length; i++) {
95 // uppers[i] = Double.POSITIVE_INFINITY;
96 // }
97 //
98 // lowers = new double[values.length];
99 // for(int i = 0; i < values.length; i++) {
100 // lowers[i] = Double.NEGATIVE_INFINITY;
101 // }
102
103 // if( xo.hasAttribute(UPPER) ) {
104 // double[] v = xo.getDoubleArrayAttribute(UPPER);
105 // if( v.length == uppers.length ) {
106 // System.arraycopy(v, 0, uppers, 0, v.length);
107 // } else if( v.length == 1 ) {
108 // for(int i = 0; i < uppers.length; i++) {
109 // uppers[i] = v[0];
110 // }
111 // } else {
112 // throw new XMLParseException("uppers string must have 1 value or dimension values");
113 // }
114 // }
115 //
116 // if( xo.hasAttribute(LOWER) ) {
117 // double[] v = xo.getDoubleArrayAttribute(LOWER);
118 // if( v.length == lowers.length ) {
119 // System.arraycopy(v, 0, lowers, 0, v.length);
120 // } else if( v.length == 1 ) {
121 // for(int i = 0; i < lowers.length; i++) {
122 // lowers[i] = v[0];
123 // }
124 // } else {
125 // throw new XMLParseException("lowers string must have 1 value or dimension values");
126 // }
127 // }
128
129 // assert uppers != null && lowers != null;
130
131 // if( (uppers.length != values.length) ) {
132 // throw new XMLParseException("value and upper limit strings have different dimension, in parameter");
133 // }
134 //
135 // if( (lowers.length != values.length) ) {
136 // throw new XMLParseException("value and lower limit strings have different dimension, in parameter");
137 // }
138 //
139 // // check if uppers and lowers are consistent
140 // for(int i = 0; i < values.length; i++) {
141 // if( uppers[i] < lowers[i] ) {
142 // throw new XMLParseException("upper is lower than lower, in parameter");
143 // }
144 // }
145 //
146 // if (xo.hasChildNamed(RANDOMIZE)) {
147 //
148 // Distribution distribution = (Distribution) xo.getChild(RANDOMIZE).getChild(Distribution.class);
149 // for (int i = 0; i < values.length; i++) {
150 // do {
151 // // Not an efficient way to draw random variables, but this is currently the only general interface
152 // values[i] = distribution.quantile(MathUtils.nextDouble());
153 // } while (values[i] < lowers[i] || values[i] > uppers[i]);
154 // }
155 //
156 // } else {
157 //
158 // // make values consistent with bounds
159 // for(int i = 0; i < values.length; i++) {
160 // if( uppers[i] < values[i] ) values[i] = uppers[i];
161 // }
162 //
163 // for(int i = 0; i < values.length; i++) {
164 // if (lowers[i] > values[i]) values[i] = lowers[i];
165 // }
166 // }
167
168 Variable<Integer> param = new Variable.I(values);
169
170 param.addBounds(new Bounds.Staircase(param));
171 return param;
172 }
173
174 public XMLSyntaxRule[] getSyntaxRules() {
175 return rules;
176 }
177
178 private final XMLSyntaxRule[] rules = {
179 AttributeRule.newIntegerArrayRule(VALUE, true),
180 AttributeRule.newIntegerRule(DIMENSION, true),
181 // AttributeRule.newDoubleArrayRule(UPPER, true),
182 // AttributeRule.newDoubleArrayRule(LOWER, true),
183 // new ElementRule(RANDOMIZE, new XMLSyntaxRule[] {
184 // new ElementRule(Distribution.class),
185 // },true),
186 };
187
188
189 public String getParserDescription() {
190 return "An integer-valued parameter only for staircase bound.";
191 }
192
193 public Class getReturnType() {
194 return Variable.class;
195 }
196
197 // static public void replaceParameter(XMLObject xo, Parameter newParam) throws XMLParseException {
198 //
199 // for (int i = 0; i < xo.getChildCount(); i++) {
200 //
201 // if (xo.getChild(i) instanceof Parameter) {
202 //
203 // XMLObject rxo;
204 // Object obj = xo.getRawChild(i);
205 //
206 // if (obj instanceof Reference ) {
207 // rxo = ((Reference) obj).getReferenceObject();
208 // } else if (obj instanceof XMLObject) {
209 // rxo = (XMLObject) obj;
210 // } else {
211 // throw new XMLParseException("object reference not available");
212 // }
213 //
214 // if (rxo.getChildCount() > 0) {
215 // throw new XMLParseException("No child elements allowed in parameter element.");
216 // }
217 //
218 // if (rxo.hasAttribute(XMLParser.IDREF)) {
219 // throw new XMLParseException("References to " + xo.getName() + " parameters are not allowed in treeModel.");
220 // }
221 //
222 // if (rxo.hasAttribute(VALUE)) {
223 // throw new XMLParseException("Parameters in " + xo.getName() + " have values set automatically.");
224 // }
225 //
226 //// if (rxo.hasAttribute(UPPER)) {
227 //// throw new XMLParseException("Parameters in " + xo.getName() + " have bounds set automatically.");
228 //// }
229 ////
230 //// if (rxo.hasAttribute(LOWER)) {
231 //// throw new XMLParseException("Parameters in " + xo.getName() + " have bounds set automatically.");
232 //// }
233 //
234 // if (rxo.hasAttribute(XMLParser.ID)) {
235 // newParam.setId(rxo.getStringAttribute(XMLParser.ID));
236 // }
237 //
238 // rxo.setNativeObject(newParam);
239 //
240 // return;
241 // }
242 // }
243 // }
244
245 // static public Parameter getParameter(XMLObject xo) throws XMLParseException {
246 //
247 // int paramCount = 0;
248 // Parameter param = null;
249 // for (int i = 0; i < xo.getChildCount(); i++) {
250 // if (xo.getChild(i) instanceof Parameter) {
251 // param = (Parameter) xo.getChild(i);
252 // paramCount += 1;
253 // }
254 // }
255 //
256 // if (paramCount == 0) {
257 // throw new XMLParseException("no parameter element in treeModel " + xo.getName() + " element");
258 // } else if (paramCount > 1) {
259 // throw new XMLParseException("More than one parameter element in treeModel " + xo.getName() + " element");
260 // }
261 //
262 // return param;
263 // }
264 }