Debian Codegraph @

src/dr/inference/operators/ModeFindOperator.java

raw symbols SCIP
1 /*
2 * ModeFindOperator.java
3 *
4 * Copyright (c) 2002-2016 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.inference.operators;
27
28 import dr.inference.model.MatrixParameterInterface;
29 import dr.inference.multidimensionalscaling.mm.MMAlgorithm;
30 import dr.inference.multidimensionalscaling.mm.MultiDimensionalScalingMM;
31 import dr.xml.*;
32
33
34 /**
35 * @author Marc Suchard
36 */
37 public class ModeFindOperator extends AbstractAdaptableOperator {
38
39 public static final String OPERATOR = "modeFindOperator";
40 public static final String MAX_TIMES = "maxTimes";
41 public static final String SCALE_FACTOR = "sd";
42
43 private double scaleFactor;
44
45 private final int maxTimes;
46 private final int maxSteps;
47 private int executeTimes = 0;
48 private final MultiDimensionalScalingMM mm;
49
50 public ModeFindOperator(MultiDimensionalScalingMM mm, int maxTimes, double weight,
51 AdaptationMode mode, double scaleFactor) {
52 this(mm, maxTimes, weight, 1000, mode, scaleFactor);
53 }
54
55 public ModeFindOperator(MultiDimensionalScalingMM mm, int maxTimes, double weight,
56 int maxModeSteps, AdaptationMode mode, double scaleFactor) {
57 super(mode);
58 setWeight(weight);
59
60 this.maxTimes = maxTimes;
61 this.mm = mm;
62 this.maxSteps = maxModeSteps;
63 this.scaleFactor = scaleFactor;
64 }
65
66 public double doOperation() {
67
68 if (executeTimes < maxTimes) {
69
70 MatrixParameterInterface parameter = mm.getLikelihood().getMatrixParameter();
71
72
73 boolean sample = mode == AdaptationMode.ADAPTATION_ON || mode == AdaptationMode.DEFAULT;
74
75 // double[] original = null;
76 //
77 // if (sample) {
78 // original = parameter.getParameterValues();
79 // }
80
81 // System.err.println("START");
82 mm.run(maxSteps);
83 // System.err.println("END");
84 ++executeTimes;
85
86 double logHR = Double.POSITIVE_INFINITY; // Always accept, breaks target distribution if called infinitely often
87
88 // System.err.println("HERE");
89
90 if (sample) {
91 System.err.println("Adaptive sampling: " + scaleFactor);
92 // double[] center = parameter.getParameterValues();
93 //
94 // double balance = 0.0;
95 // for (int i = 0; i < center.length; ++i) {
96 // final double epsilon = scaleFactor * MathUtils.nextGaussian();
97 // double x = center[i] + epsilon;
98 // final double old = original[i] - center[i];
99 // balance += epsilon * epsilon - old * old;
100 // }
101 //
102 // logHR = 0.5 * balance / (scaleFactor * scaleFactor);
103 logHR = 0.0; // TODO Detailed balance is only met when mode finder run to convergence
104 }
105
106 return logHR;
107
108 } else {
109 throw new RuntimeException("Finished max times");
110 }
111 }
112
113 //MCMCOperator INTERFACE
114 public final String getOperatorName() {
115 return OPERATOR;
116 }
117
118 @Override
119 public double getAdaptableParameter() {
120 return Math.log(scaleFactor);
121 }
122
123 @Override
124 public void setAdaptableParameter(double value) {
125 scaleFactor = Math.exp(value);
126 }
127
128 @Override
129 public double getRawParameter() {
130 return scaleFactor;
131 }
132
133 public String getAdaptableParameterName() {
134 return "scaleFactor";
135 }
136
137 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
138
139 public String getParserName() {
140 return OPERATOR;
141 }
142
143 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
144
145 AdaptationMode mode = AdaptationMode.parseMode(xo);
146 double scaleFactor = xo.getAttribute(SCALE_FACTOR, 1.0);
147
148 System.err.println(mode);
149 // System.exit(-1);
150
151 double weight = xo.getDoubleAttribute(WEIGHT);
152
153 int maxTimes = xo.getAttribute(MAX_TIMES, Integer.MAX_VALUE);
154
155 MultiDimensionalScalingMM mm = (MultiDimensionalScalingMM) xo.getChild(MMAlgorithm.class);
156
157 return new ModeFindOperator(mm, maxTimes, weight, mode, scaleFactor);
158 }
159
160 //************************************************************************
161 // AbstractXMLObjectParser implementation
162 //************************************************************************
163
164 public String getParserDescription() {
165 return "This element returns a mode finder that always accepts.";
166 }
167
168 public Class getReturnType() {
169 return ModeFindOperator.class;
170 }
171
172 public XMLSyntaxRule[] getSyntaxRules() {
173 return rules;
174 }
175
176 private final XMLSyntaxRule[] rules = {
177 AttributeRule.newIntegerRule(MAX_TIMES, true),
178 AttributeRule.newDoubleRule(WEIGHT),
179 AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true),
180 AttributeRule.newDoubleRule(SCALE_FACTOR, true),
181 new ElementRule(MultiDimensionalScalingMM.class),
182 };
183
184 };
185 }