Debian Codegraph @

src/dr/inferencexml/operators/UpDownOperatorParser.java

raw symbols SCIP
1 /*
2 * UpDownOperatorParser.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.*;
30 import dr.xml.*;
31
32 /**
33 */
34 public class UpDownOperatorParser extends AbstractXMLObjectParser {
35
36 public static final String UP_DOWN_OPERATOR = "upDownOperator";
37 public static final String UP = "up";
38 public static final String DOWN = "down";
39
40 public static final String SCALE_FACTOR = ScaleOperatorParser.SCALE_FACTOR;
41
42 public String getParserName() {
43 return UP_DOWN_OPERATOR;
44 }
45
46 private Scalable[] getArgs(final XMLObject list) throws XMLParseException {
47 Scalable[] args = new Scalable[list.getChildCount()];
48 for (int k = 0; k < list.getChildCount(); ++k) {
49 final Object child = list.getChild(k);
50 if (child instanceof Parameter) {
51 args[k] = new Scalable.Default((Parameter) child);
52 } else if (child instanceof Scalable) {
53 args[k] = (Scalable) child;
54 } else {
55 XMLObject xo = (XMLObject) child;
56 if (xo.hasAttribute("count")) {
57 final int count = xo.getIntegerAttribute("count");
58
59 final Scalable s = (Scalable) xo.getChild(Scalable.class);
60 args[k] = new Scalable() {
61
62 public int scale(double factor, int nDims, boolean testBounds) {
63 return s.scale(factor, count, testBounds);
64 }
65
66 @Override
67 public boolean testBounds() {
68 return s.testBounds();
69 }
70
71 public String getName() {
72 return s.getName() + "(" + count + ")";
73 }
74 };
75 } else if (xo.hasAttribute("df")) {
76 final int df = xo.getIntegerAttribute("df");
77
78 final Scalable s = (Scalable) xo.getChild(Scalable.class);
79 args[k] = new Scalable() {
80
81 public int scale(double factor, int nDims, boolean testBounds) {
82 s.scale(factor, -1, testBounds);
83 return df;
84 }
85
86 @Override
87 public boolean testBounds() {
88 return s.testBounds();
89 }
90
91 public String getName() {
92 return s.getName() + "[df=" + df + "]";
93 }
94 };
95 }
96 }
97
98 }
99 return args;
100 }
101
102 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
103
104 final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
105
106 final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
107
108 final AdaptationMode mode = AdaptationMode.parseMode(xo);
109
110 final Scalable[] upArgs = getArgs(xo.getChild(UP));
111 final Scalable[] dnArgs = getArgs(xo.getChild(DOWN));
112
113 return new UpDownOperator(upArgs, dnArgs, scaleFactor, weight, mode);
114 }
115
116 public String getParserDescription() {
117 return "This element represents an operator that scales two parameters in different directions. " +
118 "Each operation involves selecting a scale uniformly at random between scaleFactor and 1/scaleFactor. " +
119 "The up parameter is multipled by this scale and the down parameter is divided by this scale.";
120 }
121
122 public Class getReturnType() {
123 return UpDownOperator.class;
124 }
125
126 public XMLSyntaxRule[] getSyntaxRules() {
127 return rules;
128 }
129
130 private final XMLSyntaxRule[] ee = {
131 new ElementRule(Scalable.class, true),
132 new ElementRule(Parameter.class, true),
133 new ElementRule("scale", new XMLSyntaxRule[]{
134 AttributeRule.newIntegerRule("count", true),
135 AttributeRule.newIntegerRule("df", true),
136 new ElementRule(Scalable.class),
137 }, true),
138 };
139
140 private final XMLSyntaxRule[] rules = {
141 AttributeRule.newDoubleRule(SCALE_FACTOR),
142 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
143 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
144
145 // Allow an arbitrary number of Parameters or Scalable in up or down
146 new ElementRule(UP, ee, 1, Integer.MAX_VALUE),
147 new ElementRule(DOWN, ee, 1, Integer.MAX_VALUE),
148 };
149
150 }