Debian Codegraph @

src/dr/inference/ml/MLOptimizer.java

raw symbols SCIP
1 /*
2 * MLOptimizer.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.ml;
27
28 import dr.inference.loggers.Logger;
29 import dr.inference.markovchain.MarkovChain;
30 import dr.inference.markovchain.MarkovChainListener;
31 import dr.inference.model.Likelihood;
32 import dr.inference.model.Model;
33 import dr.inference.operators.OperatorSchedule;
34 import dr.util.Identifiable;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 /**
39 * A heuristic optimizer that uses the MCMC framework.
40 *
41 * @author Andrew Rambaut
42 *
43 * @version $Id: MLOptimizer.java,v 1.5 2006/06/13 03:50:54 alexei Exp $
44 */
45 public class MLOptimizer implements Runnable, Identifiable {
46
47 /** the likelihood function */
48 private final Likelihood likelihood;
49
50 /** the operator schedule */
51 private OperatorSchedule schedule;
52
53 private final Logger[] loggers;
54
55 int chainLength;
56
57 private String id = null;
58
59 /**
60 * Constructor
61 * @param chainLength the chain length
62 * @param schedule operator schedule to be used in chain.
63 */
64 public MLOptimizer(String id,
65 int chainLength,
66 Likelihood likelihood,
67 OperatorSchedule schedule,
68 Logger[] loggers) {
69
70 this.id = id;
71
72 mc = new MarkovChain(likelihood, schedule, new GreatDelugeCriterion(0.2), 2000, 1, MarkovChain.EVALUATION_TEST_THRESHOLD, false);
73 //mc = new MarkovChain(null, likelihood, schedule, new HillClimbingCriterion(), false);
74
75 this.chainLength = chainLength;
76 this.likelihood = likelihood;
77 this.loggers = loggers;
78
79 setOperatorSchedule(schedule);
80
81 //initialize transients
82 currentState = 0;
83 }
84
85 public void run() {
86 chain();
87 }
88
89 /**
90 * This method actually intiates the MCMC analysis.
91 * the site patterns have been dropped from the site model
92 * to reduce the footprint of the sample.
93 */
94 public void chain() {
95 currentState = 0;
96
97 if (loggers != null) {
98 for (Logger logger : loggers) {
99 logger.startLogging();
100 }
101 }
102
103 timer.start();
104
105 mc.reset();
106 timer.start();
107
108 mc.addMarkovChainListener(chainListener);
109 mc.runChain(getChainLength(), true/*, 0*/);
110 mc.removeMarkovChainListener(chainListener);
111
112 timer.stop();
113 }
114
115 /** @return the likelihood function. */
116 public Likelihood getLikelihood() { return likelihood; }
117
118 /** @return the timer. */
119 public dr.util.Timer getTimer() { return timer; }
120
121 /** set the operator schedule used in this MCMC analysis.
122 * MUST be called before chain!
123 */
124 public void setOperatorSchedule(OperatorSchedule sched) {
125 this.schedule = sched;
126 }
127
128 /** @return the operator schedule used in this MCMC analysis. */
129 public OperatorSchedule getOperatorSchedule() { return schedule; }
130
131 /** @return the length of this analysis.*/
132 public final int getChainLength() { return chainLength; }
133
134 // TRANSIENT PUBLIC METHODS *****************************************
135
136 /** @return the current state of the MCMC analysis. */
137 public final long getCurrentState() { return currentState; }
138
139 /** @return the progress (0 to 1) of the MCMC analysis. */
140 public final double getProgress() {
141 return (double)currentState / chainLength;
142 }
143
144 private final MarkovChainListener chainListener = new MarkovChainListener() {
145 @Override
146 public void bestState(long state, MarkovChain markovChain, Model bestModel) {
147 currentState = state;
148 }
149
150 @Override
151 public void currentState(long state, MarkovChain markovChain, Model currentModel) {
152
153 currentState = state;
154
155 if (loggers != null) {
156 for (Logger logger : loggers) {
157 logger.log(state);
158 }
159 }
160 }
161
162 @Override
163 public void finished(long chainLength, MarkovChain markovChain) {
164 currentState = chainLength;
165
166 if (loggers != null) {
167 for (Logger logger : loggers) {
168 logger.log(currentState);
169 logger.stopLogging();
170 }
171 }
172
173 /* if (false) {
174 NumberFormatter formatter = new NumberFormatter(8);
175
176 System.out.println();
177 System.out.println("Operator analysis");
178 for (int i =0; i < schedule.getOperatorCount(); i++) {
179
180 MCMCOperator op = schedule.getOperator(i);
181 double acceptanceProb = MCMCOperator.Utils.getAcceptanceProbability(op);
182 System.out.println(formatter.formatToFieldWidth(op.getOperatorName(), 30) + "\t" + formatter.formatDecimal(op.getMeanDeviation(), 2) + "\t" + formatter.formatDecimal(acceptanceProb, 4));
183 }
184 System.out.println();
185 }
186 */
187 }
188 // for receiving
189 /** Called when a new new best likelihood state is found. */
190 public void bestLklModel(long state, Model bestModel) {
191 currentState = state;
192 }
193
194 };
195
196 /**
197 * Creates a DOM element that represents this MCMC analysis.
198 */
199 public Element createElement(Document d) {
200 throw new RuntimeException("Not implemented!");
201 }
202
203 public String getId() { return id; }
204
205 public void setId(String id) { this.id = id; }
206
207 // PRIVATE TRANSIENTS
208
209 private long currentState;
210
211 private final dr.util.Timer timer = new dr.util.Timer();
212
213 /** this markov chain does most of the work. */
214 private MarkovChain mc = null;
215 }
216