Debian Codegraph @

src/dr/inferencexml/operators/DeltaMixOperatorParser.java

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