use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.
the class SubtreeLeapOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
AdaptationMode mode = AdaptationMode.parseMode(xo);
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
// size attribute is mandatory
final double size = xo.getAttribute(SIZE, Double.NaN);
final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
SubtreeLeapOperator.DistanceKernelType distanceKernel = SubtreeLeapOperator.DistanceKernelType.NORMAL;
if (xo.hasAttribute(DISTANCE_KERNEL)) {
try {
distanceKernel = SubtreeLeapOperator.DistanceKernelType.valueOf(xo.getStringAttribute(DISTANCE_KERNEL).trim().toUpperCase());
} catch (IllegalArgumentException iae) {
throw new XMLParseException("Unrecognised distanceKernel attribute: " + xo.getStringAttribute(DISTANCE_KERNEL));
}
}
if (size <= 0.0) {
throw new XMLParseException("The SubTreeLeap size attribute must be positive and non-zero.");
}
if (targetAcceptance <= 0.0 || targetAcceptance >= 1.0) {
throw new XMLParseException("Target acceptance probability has to lie in (0, 1)");
}
final boolean slideOnly = xo.getAttribute(SLIDE_ONLY, false);
return new SubtreeLeapOperator(treeModel, weight, size, distanceKernel, slideOnly, mode, targetAcceptance);
}
use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.
the class TipLeapOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
AdaptationMode mode = AdaptationMode.parseMode(xo);
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
Taxa taxa = (Taxa) xo.getChild(Taxa.class);
List<NodeRef> tips = new ArrayList<NodeRef>();
for (Taxon taxon : taxa) {
boolean found = false;
for (int i = 0; i < treeModel.getExternalNodeCount(); i++) {
NodeRef tip = treeModel.getExternalNode(i);
if (treeModel.getNodeTaxon(tip).equals(taxon)) {
tips.add(tip);
found = true;
break;
}
}
if (!found) {
throw new XMLParseException("Error constructing " + TIP_LEAP + ": " + taxon.getId() + ", not found in tree with id " + treeModel.getId());
}
}
// size attribute is mandatory
final double size = xo.getAttribute(SIZE, Double.NaN);
final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
final SubtreeLeapOperator.DistanceKernelType distanceKernel = SubtreeLeapOperator.DistanceKernelType.NORMAL;
if (xo.hasAttribute(DISTANCE_KERNEL)) {
try {
SubtreeLeapOperator.DistanceKernelType.valueOf(xo.getStringAttribute(DISTANCE_KERNEL).trim().toUpperCase());
} catch (IllegalArgumentException iae) {
throw new XMLParseException("Unrecognised distanceKernel attribute: " + xo.getStringAttribute(DISTANCE_KERNEL));
}
}
if (size <= 0.0) {
throw new XMLParseException("The TipLeap size attribute must be positive and non-zero.");
}
if (targetAcceptance <= 0.0 || targetAcceptance >= 1.0) {
throw new XMLParseException("Target acceptance probability has to lie in (0, 1)");
}
return new SubtreeLeapOperator(treeModel, taxa, weight, size, distanceKernel, mode, targetAcceptance);
}
use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.
the class FunkyPriorMixerOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
AdaptationMode mode = AdaptationMode.parseMode(xo);
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
double windowSize = xo.getDoubleAttribute(WINDOW_SIZE);
Parameter parameter = (Parameter) xo.getChild(Parameter.class);
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
RandomWalkOperator.BoundaryCondition condition = RandomWalkOperator.BoundaryCondition.valueOf(xo.getAttribute(BOUNDARY_CONDITION, RandomWalkOperator.BoundaryCondition.reflecting.name()));
return new FunkyPriorMixerOperator(treeModel, parameter, windowSize, condition, weight, mode);
}
use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.
the class GMRFSkyrideBlockUpdateOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
boolean logRecord = xo.getAttribute(KEEP_LOG_RECORD, false);
Handler gmrfHandler;
Logger gmrfLogger = Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator");
gmrfLogger.setUseParentHandlers(false);
if (logRecord) {
gmrfLogger.setLevel(Level.FINE);
try {
gmrfHandler = new FileHandler("GMRFBlockUpdate.log." + MathUtils.getSeed());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
gmrfHandler.setLevel(Level.FINE);
gmrfHandler.setFormatter(new XMLFormatter() {
public String format(LogRecord record) {
return "<record>\n \t<message>\n\t" + record.getMessage() + "\n\t</message>\n<record>\n";
}
});
gmrfLogger.addHandler(gmrfHandler);
}
AdaptationMode mode = AdaptationMode.parseMode(xo);
if (mode == AdaptationMode.DEFAULT)
mode = AdaptationMode.ADAPTATION_ON;
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
if (scaleFactor == 1.0) {
mode = AdaptationMode.ADAPTATION_OFF;
}
// throw new XMLParseException("scaleFactor must be greater than 0.0");
if (scaleFactor < 1.0) {
throw new XMLParseException("scaleFactor must be greater than or equal to 1.0");
}
int maxIterations = xo.getAttribute(MAX_ITERATIONS, 200);
double stopValue = xo.getAttribute(STOP_VALUE, 0.01);
if (xo.getAttribute(OLD_SKYRIDE, true) && !(xo.getName().compareTo(GRID_BLOCK_UPDATE_OPERATOR) == 0)) {
OldGMRFSkyrideLikelihood gmrfLikelihood = (OldGMRFSkyrideLikelihood) xo.getChild(OldGMRFSkyrideLikelihood.class);
return new GMRFSkyrideBlockUpdateOperator(gmrfLikelihood, weight, mode, scaleFactor, maxIterations, stopValue);
} else {
GMRFMultilocusSkyrideLikelihood gmrfMultilocusLikelihood = (GMRFMultilocusSkyrideLikelihood) xo.getChild(GMRFMultilocusSkyrideLikelihood.class);
return new GMRFMultilocusSkyrideBlockUpdateOperator(gmrfMultilocusLikelihood, weight, mode, scaleFactor, maxIterations, stopValue);
}
}
use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.
the class NodeHeightOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
OperatorType operatorType = OperatorType.UNIFORM;
if (xo.hasAttribute(OPERATOR_TYPE)) {
try {
operatorType = OperatorType.valueOf(xo.getStringAttribute(OPERATOR_TYPE).trim().toUpperCase());
} catch (IllegalArgumentException iae) {
throw new XMLParseException("Unrecognised operator type attribute: " + xo.getStringAttribute(OPERATOR_TYPE));
}
}
if (operatorType != OperatorType.UNIFORM) {
AdaptationMode mode = AdaptationMode.parseMode(xo);
double tuningParameter = 0.75;
if (xo.hasAttribute(SIZE)) {
tuningParameter = xo.getDoubleAttribute(SIZE);
if (tuningParameter <= 0.0) {
throw new XMLParseException("The UniformNodeHeightOperator size attribute must be positive and non-zero.");
}
}
if (xo.hasAttribute(SCALE_FACTOR)) {
tuningParameter = xo.getDoubleAttribute(SCALE_FACTOR);
if (tuningParameter <= 0.0 || tuningParameter >= 1.0) {
throw new XMLParseException("The UniformNodeHeightOperator scaleFactor attribute must be between 0 and 1.");
}
}
final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
if (targetAcceptance <= 0.0 || targetAcceptance >= 1.0) {
throw new XMLParseException("Target acceptance probability has to lie in (0, 1)");
}
switch(operatorType) {
case RANDOMWALK:
return new RandomWalkNodeHeightOperator(treeModel, weight, tuningParameter, mode, targetAcceptance);
case SCALEROOT:
case SCALEALL:
return new ScaleNodeHeightOperator(treeModel, weight, tuningParameter, operatorType, mode, targetAcceptance);
default:
throw new IllegalArgumentException("Unknown operator type");
}
}
if (xo.hasAttribute(SIZE) || xo.hasAttribute(SCALE_FACTOR) || xo.hasAttribute(TARGET_ACCEPTANCE) || xo.hasAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) {
throw new XMLParseException("Uniform node height operator does not take adaptable operator arguments");
}
return new UniformNodeHeightOperator(treeModel, weight);
}
Aggregations