Debian Codegraph @

src/dr/inferencexml/MLOptimizerParser.java

raw symbols SCIP
1 /*
2 * MLOptimizerParser.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;
27
28 import dr.inference.loggers.Logger;
29 import dr.inference.ml.MLOptimizer;
30 import dr.inference.model.Likelihood;
31 import dr.inference.operators.OperatorSchedule;
32 import dr.xml.*;
33
34 import java.util.ArrayList;
35
36 /**
37 *
38 */
39 public class MLOptimizerParser extends AbstractXMLObjectParser {
40
41 public static final String CHAIN_LENGTH = "chainLength";
42 public static final String OPTIMIZER = "optimizer";
43
44 public String getParserName() { return OPTIMIZER; }
45
46 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
47
48 int chainLength = xo.getIntegerAttribute(CHAIN_LENGTH);
49
50 OperatorSchedule opsched = null;
51 dr.inference.model.Likelihood likelihood = null;
52 ArrayList<Logger> loggers = new ArrayList<Logger>();
53
54 for (int i = 0; i < xo.getChildCount(); i++) {
55 Object child = xo.getChild(i);
56 if (child instanceof dr.inference.model.Likelihood) {
57 likelihood = (dr.inference.model.Likelihood)child;
58 } else if (child instanceof OperatorSchedule) {
59 opsched = (OperatorSchedule)child;
60 } else if (child instanceof Logger) {
61 loggers.add((Logger)child);
62 } else {
63 throw new XMLParseException("Unrecognized element found in optimizer element:" + child);
64 }
65 }
66
67 Logger[] loggerArray = new Logger[loggers.size()];
68 loggers.toArray(loggerArray);
69
70 return new MLOptimizer("optimizer1", chainLength, likelihood, opsched, loggerArray);
71 }
72
73 //************************************************************************
74 // AbstractXMLObjectParser implementation
75 //************************************************************************
76
77
78 public String getParserDescription() {
79 return "This element returns a maximum likelihood heuristic optimizer and runs the optimization as a side effect.";
80 }
81
82 public Class getReturnType() { return MLOptimizer.class; }
83
84 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
85
86 private final XMLSyntaxRule[] rules = {
87 AttributeRule.newIntegerRule(CHAIN_LENGTH),
88 new ElementRule(OperatorSchedule.class ),
89 new ElementRule(Likelihood.class ),
90 new ElementRule(Logger.class, 1, Integer.MAX_VALUE )
91 };
92
93 }