Debian Codegraph @

src/dr/inference/model/TestThreadedCompoundLikelihood.java

raw symbols SCIP
1 /*
2 * TestThreadedCompoundLikelihood.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.model;
27
28 import dr.util.NumberFormatter;
29
30 import java.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.concurrent.locks.Condition;
35 import java.util.concurrent.locks.ReentrantLock;
36
37 /**
38 * A likelihood function which is simply the product of a set of likelihood functions.
39 *
40 * @author Marc Suchard
41 * @author Andrew Rambaut
42 * @version $Id: CompoundLikelihood.java,v 1.19 2005/05/25 09:14:36 rambaut Exp $
43 */
44 public class TestThreadedCompoundLikelihood implements Likelihood {
45
46 public static final boolean DEBUG = false;
47
48 public TestThreadedCompoundLikelihood() {
49 }
50
51 public TestThreadedCompoundLikelihood(List<Likelihood> likelihoods) {
52 for (Likelihood likelihood : likelihoods) {
53 addLikelihood(likelihood);
54 }
55 }
56
57 public void addLikelihood(Likelihood likelihood) {
58
59 if (!likelihoods.contains(likelihood)) {
60
61 likelihoods.add(likelihood);
62 if (likelihood.getModel() != null) {
63 compoundModel.addModel(likelihood.getModel());
64 }
65
66 likelihoodCallers.add(new LikelihoodCaller(likelihood));
67 }
68 }
69
70 public int getLikelihoodCount() {
71 return likelihoods.size();
72 }
73
74 public final Likelihood getLikelihood(int i) {
75 return likelihoods.get(i);
76 }
77
78 @Override
79 public Set<Likelihood> getLikelihoodSet() {
80 Set<Likelihood> set = new HashSet<Likelihood>();
81 for (Likelihood l : likelihoods) {
82 set.add(l);
83 set.addAll(l.getLikelihoodSet());
84 }
85 return set;
86 }
87
88
89 // **************************************************************
90 // Likelihood IMPLEMENTATION
91 // **************************************************************
92
93 public Model getModel() {
94 return compoundModel;
95 }
96
97 public double getLogLikelihood() {
98
99 double logLikelihood = 0.0;
100
101 if (threads == null) {
102 // first call so setup a thread for each likelihood...
103 threads = new LikelihoodThread[likelihoodCallers.size()];
104 for (int i = 0; i < threads.length; i++) {
105 // and start them running...
106 threads[i] = new LikelihoodThread();
107 threads[i].start();
108 }
109 }
110
111 for (int i = 0; i < threads.length; i++) {
112 // set the caller which will be called in each thread
113 LikelihoodCaller caller = likelihoodCallers.get(i);
114 if (caller.isLikelihoodKnown()) {
115 threads[i].setReturnValue(caller.call());
116 } else {
117 threads[i].setCaller(caller);
118 }
119 }
120
121 for (LikelihoodThread thread : threads) {
122 // now wait for the results to be set...
123 Double result = thread.getResult();
124 while (result == null) {
125 result = thread.getResult();
126 }
127 logLikelihood += result;
128 }
129
130 return logLikelihood; // * weightFactor;
131 }
132
133 public boolean evaluateEarly() {
134 return false;
135 }
136
137 public void makeDirty() {
138
139 for (Likelihood likelihood : likelihoods) {
140 likelihood.makeDirty();
141 }
142 }
143
144 public String prettyName() {
145 return Abstract.getPrettyName(this);
146 }
147
148 public String getDiagnosis() {
149 String message = "";
150 boolean first = true;
151
152 for (Likelihood lik : likelihoods) {
153
154 if (!first) {
155 message += ", ";
156 } else {
157 first = false;
158 }
159
160 String id = lik.getId();
161 if (id == null || id.trim().length() == 0) {
162 String[] parts = lik.getClass().getName().split("\\.");
163 id = parts[parts.length - 1];
164 }
165
166 message += id + "=";
167
168
169 if (lik instanceof TestThreadedCompoundLikelihood) {
170 String d = ((TestThreadedCompoundLikelihood) lik).getDiagnosis();
171 if (d != null && d.length() > 0) {
172 message += "(" + d + ")";
173 }
174 } else {
175
176 if (lik.getLogLikelihood() == Double.NEGATIVE_INFINITY) {
177 message += "-Inf";
178 } else if (Double.isNaN(lik.getLogLikelihood())) {
179 message += "NaN";
180 } else {
181 NumberFormatter nf = new NumberFormatter(6);
182 message += nf.formatDecimal(lik.getLogLikelihood(), 4);
183 }
184 }
185 }
186
187 return message;
188 }
189
190 public String toString() {
191
192 return Double.toString(getLogLikelihood());
193
194 }
195
196 public void setWeightFactor(double w) {
197 weightFactor = w;
198 }
199
200 public double getWeightFactor() {
201 return weightFactor;
202 }
203
204 // **************************************************************
205 // Loggable IMPLEMENTATION
206 // **************************************************************
207
208 /**
209 * @return the log columns.
210 */
211 public dr.inference.loggers.LogColumn[] getColumns() {
212 return new dr.inference.loggers.LogColumn[]{
213 new LikelihoodColumn(getId())
214 };
215 }
216
217 private class LikelihoodColumn extends dr.inference.loggers.NumberColumn {
218 public LikelihoodColumn(String label) {
219 super(label);
220 }
221
222 public double getDoubleValue() {
223 return getLogLikelihood();
224 }
225 }
226
227 // **************************************************************
228 // Identifiable IMPLEMENTATION
229 // **************************************************************
230
231 private String id = null;
232
233 public void setId(String id) {
234 this.id = id;
235 }
236
237 public String getId() {
238 return id;
239 }
240
241 private LikelihoodThread[] threads;
242
243 private final ArrayList<Likelihood> likelihoods = new ArrayList<Likelihood>();
244 private final CompoundModel compoundModel = new CompoundModel("compoundModel");
245
246 private final List<LikelihoodCaller> likelihoodCallers = new ArrayList<LikelihoodCaller>();
247
248 private double weightFactor = 1.0;
249
250 class LikelihoodCaller {
251
252 public LikelihoodCaller(Likelihood likelihood) {
253 this.likelihood = likelihood;
254 }
255
256 public double call() {
257 return likelihood.getLogLikelihood();
258 }
259
260 private final Likelihood likelihood;
261
262 public boolean isLikelihoodKnown() {
263 return ((likelihood instanceof ThreadAwareLikelihood) &&
264 ((ThreadAwareLikelihood) likelihood).isLikelihoodKnown());
265 }
266 }
267
268 class LikelihoodThread extends Thread {
269
270 public LikelihoodThread() {
271 }
272
273 public void setCaller(LikelihoodCaller caller) {
274 lock.lock();
275 resultAvailable = false;
276 try {
277 this.caller = caller;
278 condition.signal();
279 } finally {
280 lock.unlock();
281 }
282 }
283
284 /**
285 * Main run loop
286 */
287 public void run() {
288 while (true) {
289 lock.lock();
290 try {
291 while (caller == null)
292 condition.await();
293 result = caller.call(); // SLOW
294 resultAvailable = true;
295 caller = null;
296 } catch (InterruptedException e) {
297
298 } finally {
299 lock.unlock();
300 }
301 }
302 }
303
304 public Double getResult() {
305 Double returnValue = null;
306 if (!lock.isLocked() && resultAvailable) { // thread is not busy and completed
307 resultAvailable = false; // TODO need to lock before changing resultAvailable?
308 returnValue = result;
309 }
310 return returnValue;
311 }
312
313 private LikelihoodCaller caller = null;
314 private Double result = Double.NaN;
315 private boolean resultAvailable = false;
316 private final ReentrantLock lock = new ReentrantLock();
317 private final Condition condition = lock.newCondition();
318
319 public void setReturnValue(double logLikelihood) {
320 result = logLikelihood;
321 resultAvailable = true;
322 }
323 }
324
325 public boolean isUsed() {
326 return isUsed;
327 }
328
329 public void setUsed() {
330 isUsed = true;
331
332 for (Likelihood l : likelihoods) {
333 l.setUsed();
334 }
335 }
336
337 private boolean isUsed = false;
338
339 }