Debian Codegraph @

src/dr/inference/smc/SMC.java

raw symbols SCIP
1 /*
2 * MCMC.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.inference.smc;
27
28 import dr.inference.loggers.LogColumn;
29 import dr.inference.loggers.Loggable;
30 import dr.inference.loggers.Logger;
31 import dr.inference.markovchain.MarkovChain;
32 import dr.inference.markovchain.MarkovChainListener;
33 import dr.inference.mcmc.MCMCCriterion;
34 import dr.inference.mcmc.MCMCOptions;
35 import dr.inference.model.Likelihood;
36 import dr.inference.model.Model;
37 import dr.inference.operators.*;
38 import dr.inference.state.Factory;
39 import dr.inference.state.StateLoader;
40 import dr.inference.state.StateLoaderSaver;
41 import dr.inference.state.StateSaver;
42 import dr.util.Identifiable;
43 import dr.util.NumberFormatter;
44 import dr.xml.Spawnable;
45
46 import java.io.File;
47 import java.io.FileOutputStream;
48 import java.io.IOException;
49 import java.io.PrintStream;
50 import java.util.ArrayList;
51 import java.util.List;
52
53 /**
54 * A class that runs short MCMC chains for each of a set of particles as
55 * part of a sequential Monte Carlo (SMC) sampler.
56 *
57 * @author Andrew Rambaut
58 * @version $Id:$
59 */
60 public class SMC implements Identifiable, Spawnable, Loggable {
61
62 public SMC(String id, List<StateLoaderSaver> particleStates) {
63 this.id = id;
64 this.particleStates.addAll(particleStates);
65 }
66
67 /**
68 * Must be called before calling chain.
69 *
70 * @param options the options for this SMC analysis
71 * @param schedule operator schedule to be used in chain.
72 * @param likelihood the likelihood for this SMC
73 * @param loggers an array of loggers to record output of this SMC run
74 */
75 public void init(
76 SMCOptions options,
77 Likelihood likelihood,
78 OperatorSchedule schedule,
79 Logger[] loggers) {
80
81 MCMCCriterion criterion = new MCMCCriterion();
82
83 // full evaluation tests and operator adaptation are off as these are multiple short runs.
84 // Operator tuning will have already been done.
85 mc = new MarkovChain(likelihood, schedule, criterion, 0, 0, 0, false);
86
87 this.options = options;
88 this.loggers = loggers;
89 this.schedule = schedule;
90
91 //initialize transients
92 currentState = 0;
93
94 // States are saved at the end of each particle's run
95 // if (Factory.INSTANCE != null) {
96 // for (MarkovChainListener listener : Factory.INSTANCE.getStateSaverChainListeners()) {
97 // mc.addMarkovChainListener(listener);
98 // }
99 // }
100
101 }
102
103 public MarkovChain getMarkovChain() {
104 return mc;
105 }
106
107 public Logger[] getLoggers() {
108 return loggers;
109 }
110
111 public OperatorSchedule getOperatorSchedule() {
112 return schedule;
113 }
114
115 public void run() {
116 chain();
117 }
118
119 /**
120 * This method actually initiates the MCMC analysis.
121 */
122 public void chain() {
123
124 currentState = 0;
125
126 timer.start();
127
128 if (loggers != null) {
129 for (Logger logger : loggers) {
130 logger.startLogging();
131 }
132 }
133
134 mc.addMarkovChainListener(chainListener);
135
136 for (StateLoaderSaver particleState : particleStates) {
137 // Don't need the savedLnL - it won't be there
138 particleState.loadState(mc, new double[1]);
139
140 // reset the current chain length to 0
141 mc.setCurrentLength(0);
142
143 mc.runChain(options.getChainLength(), true);
144
145 // Save state to file...
146 particleState.saveState(mc, mc.getCurrentLength(), mc.getCurrentScore());
147 }
148
149 mc.terminateChain();
150
151 mc.removeMarkovChainListener(chainListener);
152
153 timer.stop();
154 }
155
156 @Override
157 public LogColumn[] getColumns() {
158 return new LogColumn[] { new LogColumn() {
159 @Override
160 public void setLabel(String label) {
161 }
162
163 @Override
164 public String getLabel() {
165 return "time";
166 }
167
168 @Override
169 public void setMinimumWidth(int minimumWidth) {
170
171 }
172
173 @Override
174 public int getMinimumWidth() {
175 return 0;
176 }
177
178 @Override
179 public String getFormatted() {
180 return Double.toString(getTimer().toSeconds());
181 }
182 } };
183 }
184
185 private final MarkovChainListener chainListener = new MarkovChainListener() {
186
187 // MarkovChainListener interface *******************************************
188 // for receiving messages from subordinate MarkovChain
189
190 /**
191 * Called to update the current model keepEvery states.
192 */
193 @Override
194 public void currentState(long state, MarkovChain markovChain, Model currentModel) {
195
196 currentState = state;
197
198 if (loggers != null) {
199 for (Logger logger : loggers) {
200 logger.log(state);
201 }
202 }
203 }
204
205 /**
206 * Called when a new new best posterior state is found.
207 */
208 @Override
209 public void bestState(long state, MarkovChain markovChain, Model bestModel) { }
210
211 /**
212 * cleans up when the chain finishes (possibly early).
213 */
214 @Override
215 public void finished(long chainLength, MarkovChain markovChain) {
216 currentState = chainLength;
217
218 if (loggers != null) {
219 for (Logger logger : loggers) {
220 logger.log(currentState);
221 logger.stopLogging();
222 }
223 }
224 }
225
226 };
227
228 /**
229 * @return the likelihood function.
230 */
231 public Likelihood getLikelihood() {
232 return mc.getLikelihood();
233 }
234
235 /**
236 * @return the timer.
237 */
238 public dr.util.Timer getTimer() {
239 return timer;
240 }
241
242 // TRANSIENT PUBLIC METHODS *****************************************
243
244 /**
245 * @return the current state of the MCMC analysis.
246 */
247 public final long getCurrentState() {
248 return currentState;
249 }
250
251 /**
252 * @return the progress (0 to 1) of the MCMC analysis.
253 */
254 public final double getProgress() {
255 return (double) currentState / (double) options.getChainLength();
256 }
257
258 public boolean getSpawnable() {
259 return true;
260 }
261
262 private boolean spawnable = true;
263
264 //PRIVATE METHODS *****************************************
265
266 public String getId() {
267 return id;
268 }
269
270 public void setId(String id) {
271 this.id = id;
272 }
273
274 // PRIVATE TRANSIENTS
275
276 private final dr.util.Timer timer = new dr.util.Timer();
277 private long currentState = 0;
278 private final NumberFormatter formatter = new NumberFormatter(8);
279
280 /**
281 * this markov chain does most of the work.
282 */
283 private MarkovChain mc;
284
285 /**
286 * the options of this MCMC analysis
287 */
288 private SMCOptions options;
289
290 private final List<StateLoaderSaver> particleStates = new ArrayList<StateLoaderSaver>();
291 private Logger[] loggers;
292 private OperatorSchedule schedule;
293
294 private String id = null;
295 }