Debian Codegraph @

src/dr/inferencexml/hmc/AppendedPotentialDerivativeParser.java

raw symbols SCIP
1 /*
2 * AppendedPotentialDerivativeParser.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.hmc;
27
28 import dr.inference.distribution.DistributionLikelihood;
29 import dr.inference.distribution.MultivariateDistributionLikelihood;
30 import dr.inference.hmc.CompoundGradient;
31 import dr.inference.hmc.GradientWrtParameterProvider;
32 import dr.inference.model.*;
33 import dr.xml.*;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 /**
39 * @author Max Tolkoff
40 */
41 public class AppendedPotentialDerivativeParser extends AbstractXMLObjectParser {
42
43 public final static String SUM_DERIVATIVE = "appendedPotentialDerivative";
44 public static final String SUM_DERIVATIVE2 = "compoundGradient";
45
46 @Override
47 public String getParserName() {
48 return SUM_DERIVATIVE;
49 }
50
51 @Override
52 public String[] getParserNames() {
53 return new String[] { SUM_DERIVATIVE, SUM_DERIVATIVE2 };
54 }
55
56 @Override
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 List<GradientWrtParameterProvider> gradList = new ArrayList<GradientWrtParameterProvider>();
60 List<Likelihood> likelihoodList = new ArrayList<Likelihood>(); // TODO Remove?
61
62 for (int i = 0; i < xo.getChildCount(); ++i) {
63
64 Object obj = xo.getChild(i);
65
66 GradientWrtParameterProvider grad;
67 Likelihood likelihood;
68
69 if (obj instanceof DistributionLikelihood) {
70 DistributionLikelihood dl = (DistributionLikelihood) obj;
71 if (!(dl.getDistribution() instanceof GradientProvider)) {
72 throw new XMLParseException("Not a gradient provider");
73 }
74
75 throw new RuntimeException("Not yet implemented");
76
77 } else if (obj instanceof MultivariateDistributionLikelihood) {
78 final MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) obj;
79 if (!(mdl.getDistribution() instanceof GradientProvider)) {
80 throw new XMLParseException("Not a gradient provider");
81 }
82
83 final GradientProvider provider = (GradientProvider) mdl.getDistribution();
84 final Parameter parameter = mdl.getDataParameter();
85 likelihood = mdl;
86
87 grad = new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
88
89 } else if (obj instanceof GradientWrtParameterProvider) {
90 grad = (GradientWrtParameterProvider) obj;
91 likelihood = grad.getLikelihood();
92 } else {
93 throw new XMLParseException("Not a Gaussian process");
94 }
95
96 gradList.add(grad);
97 likelihoodList.add(likelihood);
98 }
99
100 return new CompoundGradient(gradList);
101 }
102
103 @Override
104 public XMLSyntaxRule[] getSyntaxRules() {
105 return rules;
106 }
107
108 private final XMLSyntaxRule[] rules = {
109 new ElementRule(GradientWrtParameterProvider.class, 1, Integer.MAX_VALUE),
110 };
111
112 @Override
113 public String getParserDescription() {
114 return null;
115 }
116
117 @Override
118 public Class getReturnType() {
119 return CompoundGradient.class;
120 }
121 }