Debian Codegraph @

src/dr/inferencexml/operators/TwoPhaseOperatorParser.java

raw symbols SCIP
1 /*
2 * TwoPhaseOperatorParser.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 java.util.ArrayList;
29 import java.util.List;
30
31 import dr.inference.model.Parameter;
32 import dr.inference.operators.*;
33 import dr.inference.operators.AdaptableMCMCOperator;
34 import dr.xml.AbstractXMLObjectParser;
35 import dr.xml.AttributeRule;
36 import dr.xml.ElementRule;
37 import dr.xml.XMLObject;
38 import dr.xml.XMLParseException;
39 import dr.xml.XMLSyntaxRule;
40
41 /**
42 * Parser for the TwoPhaseOperator.
43 *
44 * @author Guy Baele
45 */
46 public class TwoPhaseOperatorParser extends AbstractXMLObjectParser {
47
48 public static final boolean DEBUG = false;
49
50 public static final String TWO_PHASE_OPERATOR = "twoPhaseOperator";
51 public static final String WEIGHT = "weight";
52 public static final String INITIAL = "initial";
53 public static final String BURNIN = "burnin";
54 public static final String PHASE_ONE = "phaseOne";
55 public static final String PHASE_TWO = "phaseTwo";
56
57 public String getParserName() {
58 return TWO_PHASE_OPERATOR;
59 }
60
61 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
62
63 if (DEBUG) {
64 System.err.println("\nParsing TwoPhaseOperator");
65 }
66
67 final AdaptationMode mode = AdaptationMode.parseMode(xo);
68 final double weight = xo.getDoubleAttribute(WEIGHT);
69 final int initial = xo.getIntegerAttribute(INITIAL);
70 final int burnin = xo.getIntegerAttribute(BURNIN);
71
72 /*final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
73 if (scaleFactor <= 0.0 || scaleFactor >= 1.0) {
74 throw new XMLParseException("scaleFactor must be between 0.0 and 1.0");
75 }*/
76
77 //final Parameter parameter = (Parameter) xo.getChild(Parameter.class);
78
79 if (DEBUG) {
80 System.err.println("child count: " + xo.getChildCount());
81
82 System.err.println(xo.getChild(PHASE_ONE));
83 System.err.println(xo.getChild(PHASE_ONE).getChildCount());
84
85 System.err.println(xo.getChild(PHASE_TWO));
86 System.err.println(xo.getChild(PHASE_TWO).getChildCount());
87 }
88
89 List<AbstractAdaptableOperator> phaseOneOperators = new ArrayList<AbstractAdaptableOperator>();
90 int phaseOneCount = xo.getChild(PHASE_ONE).getChildCount();
91 for (int i = 0; i < phaseOneCount; i++) {
92 phaseOneOperators.add((AbstractAdaptableOperator)xo.getChild(PHASE_ONE).getChild(i));
93 }
94
95 if (DEBUG) {
96 System.err.println("arrayList one size: " + phaseOneOperators.size());
97 for (int i = 0; i < phaseOneOperators.size(); i++) {
98 System.err.println(" " + phaseOneOperators.get(i));
99 }
100 }
101
102 /*List<AbstractAdaptableOperator> phaseTwoOperators = new ArrayList<AbstractAdaptableOperator>();
103 int phaseTwoCount = xo.getChild(PHASE_TWO).getChildCount();
104 for (int i = 0; i < phaseTwoCount; i++) {
105 phaseTwoOperators.add((AbstractAdaptableOperator)xo.getChild(PHASE_TWO).getChild(i));
106 }*/
107
108 List<AdaptableVarianceMultivariateNormalOperator> phaseTwoOperators = new ArrayList<AdaptableVarianceMultivariateNormalOperator>();
109 int phaseTwoCount = xo.getChild(PHASE_TWO).getChildCount();
110 for (int i = 0; i < phaseTwoCount; i++) {
111 phaseTwoOperators.add((AdaptableVarianceMultivariateNormalOperator)xo.getChild(PHASE_TWO).getChild(i));
112 }
113
114 if (DEBUG) {
115 System.err.println("arrayList two size: " + phaseTwoOperators.size());
116 for (int i = 0; i < phaseTwoOperators.size(); i++) {
117 System.err.println(" " + phaseTwoOperators.get(i));
118 }
119 }
120
121 //keep track of the parameters of phase one here, as apparently we can't get to them afterwards
122 //let's just get them from phase two, as there I can implement whatever I want
123 List<Parameter> parameters = new ArrayList<Parameter>();
124 for (int i = 0; i < phaseTwoCount; i++) {
125 parameters.add(phaseTwoOperators.get(i).getParameter());
126 }
127
128 if (DEBUG) {
129 System.err.println("parameter list size: " + parameters.size());
130 for (int i = 0; i < parameters.size(); i++) {
131 System.err.println(" " + parameters.get(i));
132 }
133 }
134
135 return new TwoPhaseOperator(phaseOneOperators, phaseTwoOperators, parameters, initial, burnin, weight, mode);
136 }
137
138 //************************************************************************
139 // AbstractXMLObjectParser implementation
140 //************************************************************************
141
142 public String getParserDescription() {
143 return "This element returns a two phase operator.";
144 }
145
146 public Class getReturnType() {
147 return TwoPhaseOperator.class;
148 }
149
150 public XMLSyntaxRule[] getSyntaxRules() {
151 return rules;
152 }
153
154 private final XMLSyntaxRule[] rules = {
155 AttributeRule.newDoubleRule(WEIGHT),
156 AttributeRule.newIntegerRule(INITIAL),
157 AttributeRule.newIntegerRule(BURNIN),
158 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
159 new ElementRule(PHASE_ONE,
160 new XMLSyntaxRule[]{new ElementRule(AbstractAdaptableOperator.class, 1, Integer.MAX_VALUE)}),
161 new ElementRule(PHASE_TWO,
162 new XMLSyntaxRule[]{new ElementRule(AdaptableVarianceMultivariateNormalOperator.class, 1, Integer.MAX_VALUE)})
163 /*new ElementRule(PHASE_TWO,
164 new XMLSyntaxRule[]{new ElementRule(AbstractAdaptableOperator.class, 1, Integer.MAX_VALUE)})*/
165 };
166 }