| 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.evomodelxml.operators; |
| 27 |
|
| 28 |
import dr.evomodel.operators.SubtreeSlideOperator; |
| 29 |
import dr.evomodel.tree.TreeModel; |
| 30 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 31 |
import dr.inference.operators.AdaptationMode; |
| 32 |
import dr.inference.operators.MCMCOperator; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SubtreeSlideOperatorParser extends AbstractXMLObjectParser { |
| 38 |
|
| 39 |
public static final String SUBTREE_SLIDE = "subtreeSlide"; |
| 40 |
public static final String SWAP_RATES = "swapInRandomRate"; |
| 41 |
public static final String SWAP_TRAITS = "swapInRandomTrait"; |
| 42 |
public static final String DIRICHLET_BRANCHES = "branchesAreScaledDirichlet"; |
| 43 |
public static final String TARGET_ACCEPTANCE = "targetAcceptance"; |
| 44 |
|
| 45 |
public static final String TRAIT = "trait"; |
| 46 |
|
| 47 |
public String getParserName() { |
| 48 |
return SUBTREE_SLIDE; |
| 49 |
} |
| 50 |
|
| 51 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 52 |
|
| 53 |
boolean swapRates = xo.getAttribute(SWAP_RATES, false); |
| 54 |
boolean swapTraits = xo.getAttribute(SWAP_TRAITS, false); |
| 55 |
boolean scaledDirichletBranches = xo.getAttribute(DIRICHLET_BRANCHES, false); |
| 56 |
|
| 57 |
AdaptationMode mode = AdaptationMode.DEFAULT; |
| 58 |
if (xo.hasAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) { |
| 59 |
if (xo.getBooleanAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) { |
| 60 |
mode = AdaptationMode.ADAPTATION_ON; |
| 61 |
} else { |
| 62 |
mode = AdaptationMode.ADAPTATION_OFF; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 67 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 68 |
|
| 69 |
final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234); |
| 70 |
|
| 71 |
final double size = xo.getAttribute("size", 1.0); |
| 72 |
|
| 73 |
if (Double.isInfinite(size) || size <= 0.0) { |
| 74 |
throw new XMLParseException("size attribute must be positive and not infinite. was " + size + |
| 75 |
" for tree " + treeModel.getId() ); |
| 76 |
} |
| 77 |
|
| 78 |
final boolean gaussian = xo.getBooleanAttribute("gaussian"); |
| 79 |
SubtreeSlideOperator operator = new SubtreeSlideOperator(treeModel, weight, size, gaussian, |
| 80 |
swapRates, swapTraits, scaledDirichletBranches, mode, targetAcceptance); |
| 81 |
|
| 82 |
return operator; |
| 83 |
} |
| 84 |
|
| 85 |
public String getParserDescription() { |
| 86 |
return "An operator that slides a subtree."; |
| 87 |
} |
| 88 |
|
| 89 |
public Class getReturnType() { |
| 90 |
return SubtreeSlideOperator.class; |
| 91 |
} |
| 92 |
|
| 93 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 94 |
return rules; |
| 95 |
} |
| 96 |
|
| 97 |
private final XMLSyntaxRule[] rules = { |
| 98 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 99 |
|
| 100 |
AttributeRule.newDoubleRule("size", true), |
| 101 |
AttributeRule.newDoubleRule(TARGET_ACCEPTANCE, true), |
| 102 |
AttributeRule.newBooleanRule("gaussian"), |
| 103 |
AttributeRule.newBooleanRule(SWAP_RATES, true), |
| 104 |
AttributeRule.newBooleanRule(SWAP_TRAITS, true), |
| 105 |
AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true), |
| 106 |
new ElementRule(TreeModel.class) |
| 107 |
}; |
| 108 |
|
| 109 |
} |