Debian Codegraph @

src/dr/inferencexml/operators/SimpleOperatorScheduleParser.java

raw symbols SCIP
1 /*
2 * SimpleOperatorScheduleParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 // AbstractXMLObjectParser implementation
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 }