Debian Codegraph @

src/dr/inferencexml/operators/hmc/BouncyParticleOperatorParser.java

raw symbols SCIP
1 /*
2 * HamiltonianMonteCarloOperatorParser.java
3 *
4 * Copyright (c) 2002-2017 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.hmc;
27
28 import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
29 import dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate;
30 import dr.inference.hmc.GradientWrtParameterProvider;
31 import dr.inference.model.Parameter;
32 import dr.inference.operators.AdaptableMCMCOperator;
33 import dr.inference.operators.AdaptationMode;
34 import dr.inference.operators.MCMCOperator;
35 import dr.inference.operators.hmc.HamiltonianMonteCarloOperator;
36 import dr.inference.operators.hmc.NewBouncyParticleOperator;
37 import dr.util.Transform;
38 import dr.xml.*;
39
40 /**
41 * @author Zhenyu Zhang
42 * @author Marc A. Suchard
43 */
44
45 public class BouncyParticleOperatorParser extends AbstractXMLObjectParser {
46 public final static String BPO_OPERATOR = "bouncyParticleOperator";
47
48 public final static String DRAW_VARIANCE = "drawVariance";
49 public static final String MODE = "mode";
50
51 @Override
52 public String getParserName() {
53 return BPO_OPERATOR;
54 }
55
56 @Override
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 System.err.println("HERE?");
60 System.exit(-1);
61 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
62 double drawVariance = xo.getDoubleAttribute(DRAW_VARIANCE);
63
64 AdaptationMode adaptationMode = AdaptationMode.parseMode(xo);
65
66 GradientWrtParameterProvider derivative =
67 (GradientWrtParameterProvider) xo.getChild(GradientWrtParameterProvider.class);
68
69 TreeDataLikelihood treeDataLikelihood =
70 (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
71
72 ContinuousDataLikelihoodDelegate likelihoodDelegate =
73 (ContinuousDataLikelihoodDelegate) xo.getChild(ContinuousDataLikelihoodDelegate.class);
74
75 String traitName = (String) xo.getChild(String.class);
76
77 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
78
79 Transform transform = (Transform.MultivariableTransformWithParameter)
80 xo.getChild(Transform.MultivariableTransformWithParameter.class);
81
82 if (derivative.getDimension() != parameter.getDimension()) {
83 throw new XMLParseException("Gradient (" + derivative.getDimension() +
84 ") must be the same dimensions as the parameter (" + parameter.getDimension() + ")");
85 }
86
87
88 return new NewBouncyParticleOperator(adaptationMode, weight, treeDataLikelihood, likelihoodDelegate, traitName,
89 parameter, drawVariance);
90
91 }
92
93 @Override
94 public XMLSyntaxRule[] getSyntaxRules() {
95 return rules;
96 }
97
98 private final XMLSyntaxRule[] rules = {
99 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
100 AttributeRule.newDoubleRule(DRAW_VARIANCE),
101 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
102 AttributeRule.newStringRule(MODE, true),
103 new ElementRule(Parameter.class),
104 new ElementRule(Transform.MultivariableTransformWithParameter.class, true),
105 new ElementRule(GradientWrtParameterProvider.class),
106 };
107
108 @Override
109 public String getParserDescription() {
110 return "Returns a Hamiltonian Monte Carlo transition kernel";
111 }
112
113 @Override
114 public Class getReturnType() {
115 return HamiltonianMonteCarloOperator.class;
116 }
117 }