Debian Codegraph @

src/dr/inferencexml/operators/DeltaExchangeOperatorParser.java

raw symbols SCIP
1 /*
2 * DeltaExchangeOperatorParser.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.DeltaExchangeOperator;
32 import dr.inference.operators.MCMCOperator;
33 import dr.xml.*;
34
35 /**
36 */
37 public class DeltaExchangeOperatorParser extends AbstractXMLObjectParser {
38
39 public static final String DELTA_EXCHANGE = "deltaExchange";
40 public static final String DELTA = "delta";
41 public static final String INTEGER_OPERATOR = "integer";
42 public static final String PARAMETER_WEIGHTS = "parameterWeights";
43
44 public String getParserName() {
45 return DELTA_EXCHANGE;
46 }
47
48 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
49
50 AdaptationMode mode = AdaptationMode.parseMode(xo);
51
52 final boolean isIntegerOperator = xo.getAttribute(INTEGER_OPERATOR, false);
53
54 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
55 double delta = xo.getDoubleAttribute(DELTA);
56
57 if (delta <= 0.0) {
58 throw new XMLParseException("delta must be greater than 0.0");
59 }
60
61 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
62
63
64 int[] parameterWeights;
65 if (xo.hasAttribute(PARAMETER_WEIGHTS)) {
66 parameterWeights = xo.getIntegerArrayAttribute(PARAMETER_WEIGHTS);
67 System.out.print("Parameter weights for delta exchange are: ");
68 for (int parameterWeight : parameterWeights) {
69 System.out.print(parameterWeight + "\t");
70 }
71 System.out.println();
72
73 } else {
74 parameterWeights = new int[parameter.getDimension()];
75 for (int i = 0; i < parameterWeights.length; i++) {
76 parameterWeights[i] = 1;
77 }
78 }
79
80 if (parameterWeights.length != parameter.getDimension()) {
81 throw new XMLParseException("parameter weights have the same length as parameter");
82 }
83
84
85 return new DeltaExchangeOperator(parameter, parameterWeights, delta, weight, isIntegerOperator, mode);
86 }
87
88 //************************************************************************
89 // AbstractXMLObjectParser implementation
90 //************************************************************************
91
92 public String getParserDescription() {
93 return "This element returns a delta exchange operator on a given parameter.";
94 }
95
96 public Class getReturnType() {
97 return MCMCOperator.class;
98 }
99
100 public XMLSyntaxRule[] getSyntaxRules() {
101 return rules;
102 }
103
104 private final XMLSyntaxRule[] rules = {
105 AttributeRule.newDoubleRule(DELTA),
106 AttributeRule.newIntegerArrayRule(PARAMETER_WEIGHTS, true),
107 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
108 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
109 AttributeRule.newBooleanRule(INTEGER_OPERATOR, true),
110 new ElementRule(Parameter.class)
111 };
112 }