| 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.FixedHeightSubtreePruneRegraftOperator; |
| 29 |
import dr.evomodel.operators.SubtreeJumpOperator; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 32 |
import dr.inference.operators.AdaptationMode; |
| 33 |
import dr.inference.operators.MCMCOperator; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class SubtreeJumpOperatorParser extends AbstractXMLObjectParser { |
| 39 |
|
| 40 |
public static final String SUBTREE_JUMP = "subtreeJump"; |
| 41 |
|
| 42 |
public static final String SIZE = "size"; |
| 43 |
public static final String TARGET_ACCEPTANCE = "targetAcceptance"; |
| 44 |
public static final String UNIFORM = "uniform"; |
| 45 |
|
| 46 |
|
| 47 |
public String getParserName() { |
| 48 |
return SUBTREE_JUMP; |
| 49 |
} |
| 50 |
|
| 51 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 52 |
|
| 53 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 54 |
|
| 55 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 56 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 57 |
|
| 58 |
|
| 59 |
final double size = xo.getAttribute(SIZE, Double.NaN); |
| 60 |
final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234); |
| 61 |
final boolean uniform = xo.getAttribute(UNIFORM, false); |
| 62 |
|
| 63 |
if (size <= 0.0) { |
| 64 |
throw new XMLParseException("The SubTreeLeap size attribute must be positive and non-zero."); |
| 65 |
} |
| 66 |
|
| 67 |
if (Double.isInfinite(size)) { |
| 68 |
|
| 69 |
mode = AdaptationMode.ADAPTATION_OFF; |
| 70 |
} |
| 71 |
|
| 72 |
if (targetAcceptance <= 0.0 || targetAcceptance >= 1.0) { |
| 73 |
throw new XMLParseException("Target acceptance probability has to lie in (0, 1)"); |
| 74 |
} |
| 75 |
|
| 76 |
SubtreeJumpOperator operator = new SubtreeJumpOperator(treeModel, weight, size, targetAcceptance, uniform, mode); |
| 77 |
|
| 78 |
|
| 79 |
return operator; |
| 80 |
} |
| 81 |
|
| 82 |
public String getParserDescription() { |
| 83 |
return "An operator that jumps a subtree to another edge at the same height."; |
| 84 |
} |
| 85 |
|
| 86 |
public Class getReturnType() { |
| 87 |
return FixedHeightSubtreePruneRegraftOperator.class; |
| 88 |
} |
| 89 |
|
| 90 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 91 |
return rules; |
| 92 |
} |
| 93 |
|
| 94 |
private final XMLSyntaxRule[] rules = { |
| 95 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 96 |
AttributeRule.newDoubleRule(SIZE, true), |
| 97 |
AttributeRule.newDoubleRule(TARGET_ACCEPTANCE, true), |
| 98 |
AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true), |
| 99 |
new ElementRule(TreeModel.class) |
| 100 |
}; |
| 101 |
|
| 102 |
} |