| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 42 |
|
| 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 |
} |