Debian Codegraph @

src/dr/evomodelxml/coalescent/operators/GMRFSkyrideBlockUpdateOperatorParser.java

raw symbols SCIP
1 /*
2 * GMRFSkyrideBlockUpdateOperatorParser.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.evomodelxml.coalescent.operators;
27
28 import dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood;
29 import dr.evomodel.coalescent.GMRFSkyrideLikelihood;
30 import dr.evomodel.coalescent.operators.GMRFMultilocusSkyrideBlockUpdateOperator;
31 import dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator;
32 import dr.inference.operators.AdaptableMCMCOperator;
33 import dr.inference.operators.AdaptationMode;
34 import dr.inference.operators.MCMCOperator;
35 import dr.math.MathUtils;
36 import dr.xml.*;
37
38 import java.io.IOException;
39 import java.util.logging.*;
40
41 /**
42 *
43 */
44 public class GMRFSkyrideBlockUpdateOperatorParser extends AbstractXMLObjectParser {
45
46 public static final String BLOCK_UPDATE_OPERATOR = "gmrfBlockUpdateOperator";
47 public static final String GRID_BLOCK_UPDATE_OPERATOR = "gmrfGridBlockUpdateOperator";
48 public static final String SCALE_FACTOR = "scaleFactor";
49 public static final String MAX_ITERATIONS = "maxIterations";
50 public static final String STOP_VALUE = "stopValue";
51 public static final String KEEP_LOG_RECORD = "keepLogRecord";
52 public static final String OLD_SKYRIDE = "oldSkyride";
53
54 public String getParserName() {
55 return BLOCK_UPDATE_OPERATOR;
56 }
57
58 public String[] getParserNames() {
59 return new String[]{
60 getParserName(),
61 GRID_BLOCK_UPDATE_OPERATOR,
62 };
63 }
64
65 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
66
67 boolean logRecord = xo.getAttribute(KEEP_LOG_RECORD, false);
68
69 Handler gmrfHandler;
70 Logger gmrfLogger = Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator");
71 gmrfLogger.setUseParentHandlers(false);
72
73 if (logRecord) {
74 gmrfLogger.setLevel(Level.FINE);
75
76 try {
77 gmrfHandler = new FileHandler("GMRFBlockUpdate.log." + MathUtils.getSeed());
78 } catch (IOException e) {
79 throw new RuntimeException(e.getMessage());
80 }
81 gmrfHandler.setLevel(Level.FINE);
82
83 gmrfHandler.setFormatter(new XMLFormatter() {
84 public String format(LogRecord record) {
85 return "<record>\n \t<message>\n\t" + record.getMessage()
86 + "\n\t</message>\n<record>\n";
87 }
88 });
89
90 gmrfLogger.addHandler(gmrfHandler);
91 }
92
93 AdaptationMode mode = AdaptationMode.parseMode(xo);
94 if (mode == AdaptationMode.DEFAULT) mode = AdaptationMode.ADAPTATION_ON;
95
96 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
97 double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
98 if (scaleFactor == 1.0) {
99 mode = AdaptationMode.ADAPTATION_OFF;
100 }
101
102 // if (scaleFactor <= 0.0) {
103 // throw new XMLParseException("scaleFactor must be greater than 0.0");
104 if (scaleFactor < 1.0) {
105 throw new XMLParseException("scaleFactor must be greater than or equal to 1.0");
106 }
107
108 int maxIterations = xo.getAttribute(MAX_ITERATIONS, 200);
109
110 double stopValue = xo.getAttribute(STOP_VALUE, 0.01);
111
112
113 if (xo.getAttribute(OLD_SKYRIDE, true)
114 && !(xo.getName().compareTo(GRID_BLOCK_UPDATE_OPERATOR) == 0)
115 ) {
116 GMRFSkyrideLikelihood gmrfLikelihood = (GMRFSkyrideLikelihood) xo.getChild(GMRFSkyrideLikelihood.class);
117 return new GMRFSkyrideBlockUpdateOperator(gmrfLikelihood, weight, mode, scaleFactor,
118 maxIterations, stopValue);
119 } else {
120 GMRFMultilocusSkyrideLikelihood gmrfMultilocusLikelihood = (GMRFMultilocusSkyrideLikelihood) xo.getChild(GMRFMultilocusSkyrideLikelihood.class);
121 return new GMRFMultilocusSkyrideBlockUpdateOperator(gmrfMultilocusLikelihood, weight, mode, scaleFactor,
122 maxIterations, stopValue);
123 }
124
125 }
126
127 //************************************************************************
128 // AbstractXMLObjectParser implementation
129 //************************************************************************
130
131 public String getParserDescription() {
132 return "This element returns a GMRF block-update operator for the joint distribution of the population sizes and precision parameter.";
133 }
134
135 public Class getReturnType() {
136 return MCMCOperator.class;
137 }
138
139 public XMLSyntaxRule[] getSyntaxRules() {
140 return rules;
141 }
142
143 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
144 AttributeRule.newDoubleRule(SCALE_FACTOR),
145 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
146 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
147 AttributeRule.newDoubleRule(STOP_VALUE, true),
148 AttributeRule.newIntegerRule(MAX_ITERATIONS, true),
149 AttributeRule.newBooleanRule(OLD_SKYRIDE, true),
150 new ElementRule(GMRFSkyrideLikelihood.class)
151 };
152
153 }