Debian Codegraph @

src/dr/inferencexml/operators/TransformedRandomWalkOperatorParser.java

raw symbols SCIP
1 /*
2 * TransformedRandomWalkOperatorParser.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.operators.AdaptableMCMCOperator;
30 import dr.inference.operators.AdaptationMode;
31 import dr.inference.operators.MCMCOperator;
32 import dr.inference.operators.TransformedRandomWalkOperator;
33 import dr.util.Transform;
34 import dr.xml.AbstractXMLObjectParser;
35 import dr.xml.AttributeRule;
36 import dr.xml.ElementRule;
37 import dr.xml.StringAttributeRule;
38 import dr.xml.XMLObject;
39 import dr.xml.XMLParseException;
40 import dr.xml.XMLSyntaxRule;
41
42 /**
43 * Parser for the TransformedRandomWalkOperator.
44 *
45 * @author Guy Baele
46 */
47 public class TransformedRandomWalkOperatorParser extends AbstractXMLObjectParser {
48
49 public static final String TRANSFORMED_RANDOM_WALK_OPERATOR = "transformedRandomWalkOperator";
50 public static final String WINDOW_SIZE = "windowSize";
51 public static final String UPDATE_INDEX = "updateIndex";
52 public static final String UPPER = "upper";
53 public static final String LOWER = "lower";
54
55 public static final String BOUNDARY_CONDITION = "boundaryCondition";
56
57 public String getParserName() {
58 return TRANSFORMED_RANDOM_WALK_OPERATOR;
59 }
60
61 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
62
63 AdaptationMode mode = AdaptationMode.parseMode(xo);
64
65 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
66 double windowSize = xo.getDoubleAttribute(WINDOW_SIZE);
67 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
68
69 int dim = parameter.getDimension();
70 Transform[] transformations = new Transform[dim];
71 for (int i = 0; i < dim; i++) {
72 transformations[i] = Transform.NONE;
73 }
74
75 for (int i = 0; i < xo.getChildCount(); i++) {
76 Object child = xo.getChild(i);
77 if (child instanceof Transform.ParsedTransform) {
78 Transform.ParsedTransform thisObject = (Transform.ParsedTransform) child;
79
80 System.err.println("Transformations:");
81 for (int j = thisObject.start; j < thisObject.end; ++j) {
82 transformations[j] = thisObject.transform;
83 System.err.print(transformations[j].getTransformName() + " ");
84 }
85 System.err.println();
86 }
87 }
88
89 Double lower = null;
90 Double upper = null;
91
92 if (xo.hasAttribute(LOWER)) {
93 lower = xo.getDoubleAttribute(LOWER);
94 }
95
96 if (xo.hasAttribute(UPPER)) {
97 upper = xo.getDoubleAttribute(UPPER);
98 }
99
100 TransformedRandomWalkOperator.BoundaryCondition condition = TransformedRandomWalkOperator.BoundaryCondition.valueOf(
101 xo.getAttribute(BOUNDARY_CONDITION, TransformedRandomWalkOperator.BoundaryCondition.reflecting.name()));
102
103 if (xo.hasChildNamed(UPDATE_INDEX)) {
104 XMLObject cxo = xo.getChild(UPDATE_INDEX);
105 Parameter updateIndex = (Parameter) cxo.getChild(Parameter.class);
106 if (updateIndex.getDimension() != parameter.getDimension())
107 throw new RuntimeException("Parameter to update and missing indices must have the same dimension");
108 return new TransformedRandomWalkOperator(parameter, transformations, updateIndex, windowSize, condition,
109 weight, mode, lower, upper);
110 }
111
112 return new TransformedRandomWalkOperator(parameter, transformations, null, windowSize, condition, weight, mode, lower, upper);
113 }
114
115 //************************************************************************
116 // AbstractXMLObjectParser implementation
117 //************************************************************************
118
119 public String getParserDescription() {
120 return "This element returns a transformed random walk operator on a given parameter.";
121 }
122
123 public Class getReturnType() {
124 return MCMCOperator.class;
125 }
126
127 public XMLSyntaxRule[] getSyntaxRules() {
128 return rules;
129 }
130
131 private final XMLSyntaxRule[] rules = {
132 AttributeRule.newDoubleRule(WINDOW_SIZE),
133 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
134 AttributeRule.newDoubleRule(LOWER, true),
135 AttributeRule.newDoubleRule(UPPER, true),
136 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
137 new ElementRule(UPDATE_INDEX,
138 new XMLSyntaxRule[] {
139 new ElementRule(Parameter.class),
140 },true),
141 new StringAttributeRule(BOUNDARY_CONDITION, null, TransformedRandomWalkOperator.BoundaryCondition.values(), true),
142 new ElementRule(Parameter.class),
143 new ElementRule(Transform.ParsedTransform.class, 0, Integer.MAX_VALUE)
144 };
145 }