| 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; |
| 27 |
|
| 28 |
import dr.inference.operators.MCMCOperator; |
| 29 |
import dr.inference.operators.OperatorSchedule; |
| 30 |
import dr.inference.operators.SimpleOperatorSchedule; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class SimpleOperatorScheduleParser extends AbstractXMLObjectParser { |
| 39 |
|
| 40 |
public static final String OPERATOR_SCHEDULE = "operators"; |
| 41 |
public static final String SEQUENTIAL = "sequential"; |
| 42 |
public static final String OPTIMIZATION_SCHEDULE = "optimizationSchedule"; |
| 43 |
|
| 44 |
public static final String ACCEPTANCE_THRESHOLD = "minAcceptance"; |
| 45 |
public static final String USE_THRESHOLD = "minUsage"; |
| 46 |
|
| 47 |
public String getParserName() { |
| 48 |
return OPERATOR_SCHEDULE; |
| 49 |
} |
| 50 |
|
| 51 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 52 |
|
| 53 |
int useThreshold = xo.getAttribute(USE_THRESHOLD, 1000); |
| 54 |
double acceptanceThreshold = xo.getAttribute(ACCEPTANCE_THRESHOLD, 0.0); |
| 55 |
|
| 56 |
SimpleOperatorSchedule schedule = new SimpleOperatorSchedule(useThreshold, acceptanceThreshold); |
| 57 |
|
| 58 |
if (xo.hasAttribute(SEQUENTIAL)) { |
| 59 |
schedule.setSequential(xo.getBooleanAttribute(SEQUENTIAL)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
if (xo.hasAttribute(OPTIMIZATION_SCHEDULE)) { |
| 64 |
String type = xo.getStringAttribute(OPTIMIZATION_SCHEDULE); |
| 65 |
Logger.getLogger("dr.inference").info("Optimization Schedule: " + type); |
| 66 |
|
| 67 |
try { |
| 68 |
schedule.setOptimizationSchedule(OperatorSchedule.OptimizationTransform.valueOf(type.toUpperCase())); |
| 69 |
} catch (IllegalArgumentException iae) { |
| 70 |
throw new RuntimeException("Unsupported optimization schedule"); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 75 |
Object child = xo.getChild(i); |
| 76 |
if (child instanceof MCMCOperator) { |
| 77 |
schedule.addOperator((MCMCOperator) child); |
| 78 |
} |
| 79 |
} |
| 80 |
return schedule; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 88 |
return rules; |
| 89 |
} |
| 90 |
|
| 91 |
private final XMLSyntaxRule[] rules = { |
| 92 |
AttributeRule.newBooleanRule(SEQUENTIAL, true), |
| 93 |
new ElementRule(MCMCOperator.class, 1, Integer.MAX_VALUE), |
| 94 |
AttributeRule.newStringRule(OPTIMIZATION_SCHEDULE, true), |
| 95 |
AttributeRule.newDoubleRule(ACCEPTANCE_THRESHOLD, true, "Acceptance rate below which an operator will be switched off"), |
| 96 |
AttributeRule.newIntegerRule(USE_THRESHOLD, true, "Minimum number of usage before testing acceptance threshold") |
| 97 |
}; |
| 98 |
|
| 99 |
public String getParserDescription() { |
| 100 |
return "A simple operator scheduler"; |
| 101 |
} |
| 102 |
|
| 103 |
public Class getReturnType() { |
| 104 |
return SimpleOperatorSchedule.class; |
| 105 |
} |
| 106 |
|
| 107 |
} |