Debian Codegraph @

src/dr/evomodelxml/treelikelihood/OptimizedBeagleTreeLikelihoodParser.java

raw symbols SCIP
1 /*
2 * OptimizedBeagleTreeLikelihoodParser.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.evomodelxml.treelikelihood;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32
33 import dr.evomodel.branchmodel.BranchModel;
34 import dr.evomodel.siteratemodel.GammaSiteRateModel;
35 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
36 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
37 import dr.evolution.alignment.PatternList;
38 import dr.evolution.alignment.Patterns;
39 import dr.evolution.alignment.SitePatterns;
40 import dr.evomodel.branchratemodel.BranchRateModel;
41 import dr.evomodel.tree.TreeModel;
42 import dr.evomodel.tipstatesmodel.TipStatesModel;
43 import dr.inference.model.*;
44 import dr.xml.AbstractXMLObjectParser;
45 import dr.xml.AttributeRule;
46 import dr.xml.ElementRule;
47 import dr.xml.XMLObject;
48 import dr.xml.XMLParseException;
49 import dr.xml.XMLSyntaxRule;
50
51 /**
52 * @author Guy Baele
53 */
54 public class OptimizedBeagleTreeLikelihoodParser extends AbstractXMLObjectParser {
55
56 public static final String OPTIMIZED_BEAGLE_TREE_LIKELIHOOD = "optimizedBeagleTreeLikelihood";
57
58 public static final String CALIBRATE = "calibrate";
59 public static final String RETRY = "retry";
60
61 public static final boolean DEBUG = false;
62
63 public String getParserName() {
64 return OPTIMIZED_BEAGLE_TREE_LIKELIHOOD;
65 }
66
67 protected BeagleTreeLikelihood createTreeLikelihood(PatternList patternList, TreeModel treeModel,
68 BranchModel branchModel,
69 GammaSiteRateModel siteRateModel,
70 BranchRateModel branchRateModel,
71 TipStatesModel tipStatesModel,
72 boolean useAmbiguities, PartialsRescalingScheme scalingScheme,
73 boolean delayRescalingUntilUnderflow,
74 Map<Set<String>, Parameter> partialsRestrictions,
75 XMLObject xo) throws XMLParseException {
76 return new BeagleTreeLikelihood(
77 patternList,
78 treeModel,
79 branchModel,
80 siteRateModel,
81 branchRateModel,
82 tipStatesModel,
83 useAmbiguities,
84 scalingScheme,
85 delayRescalingUntilUnderflow,
86 partialsRestrictions
87 );
88 }
89
90 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
91
92 //Default of 100 likelihood calculations for calibration
93 int calibrate = 100;
94 if (xo.hasAttribute(CALIBRATE)) {
95 calibrate = xo.getIntegerAttribute(CALIBRATE);
96 }
97
98 //Default: only try the next split up, unless a RETRY value is specified in the XML
99 int retry = 0;
100 if (xo.hasAttribute(RETRY)) {
101 retry = xo.getIntegerAttribute(RETRY);
102 }
103
104 int childCount = xo.getChildCount();
105 List<Likelihood> likelihoods = new ArrayList<Likelihood>();
106
107 //TEST
108 List<Likelihood> originalLikelihoods = new ArrayList<Likelihood>();
109
110 for (int i = 0; i < childCount; i++) {
111 likelihoods.add((Likelihood)xo.getChild(i));
112 originalLikelihoods.add((Likelihood)xo.getChild(i));
113 }
114
115 if (DEBUG) {
116 System.err.println("-----");
117 System.err.println(childCount + " BeagleTreeLikelihoods added.");
118 }
119
120 int[] instanceCounts = new int[childCount];
121 for (int i = 0; i < childCount; i++) {
122 instanceCounts[i] = 1;
123 }
124 int[] currentLocation = new int[childCount];
125 for (int i = 0; i < childCount; i++) {
126 currentLocation[i] = i;
127 }
128
129 int[] siteCounts = new int[childCount];
130 //store everything for later use
131 SitePatterns[] patterns = new SitePatterns[childCount];
132 TreeModel[] treeModels = new TreeModel[childCount];
133 BranchModel[] branchModels = new BranchModel[childCount];
134 GammaSiteRateModel[] siteRateModels = new GammaSiteRateModel[childCount];
135 BranchRateModel[] branchRateModels = new BranchRateModel[childCount];
136 boolean[] ambiguities = new boolean[childCount];
137 PartialsRescalingScheme[] rescalingSchemes = new PartialsRescalingScheme[childCount];
138 boolean[] isDelayRescalingUntilUnderflow = new boolean[childCount];
139 List<Map<Set<String>, Parameter>> partialsRestrictions = new ArrayList<Map<Set<String>, Parameter>>();
140 for (int i = 0; i < likelihoods.size(); i++) {
141 patterns[i] = (SitePatterns) ((BeagleTreeLikelihood) likelihoods.get(i)).getPatternsList();
142 siteCounts[i] = patterns[i].getPatternCount();
143 treeModels[i] = ((BeagleTreeLikelihood) likelihoods.get(i)).getTreeModel();
144 branchModels[i] = ((BeagleTreeLikelihood) likelihoods.get(i)).getBranchModel();
145 siteRateModels[i] = (GammaSiteRateModel) ((BeagleTreeLikelihood) likelihoods.get(i)).getSiteRateModel();
146 branchRateModels[i] = ((BeagleTreeLikelihood) likelihoods.get(i)).getBranchRateModel();
147 ambiguities[i] = ((BeagleTreeLikelihood) likelihoods.get(i)).useAmbiguities();
148 rescalingSchemes[i] = ((BeagleTreeLikelihood) likelihoods.get(i)).getRescalingScheme();
149 isDelayRescalingUntilUnderflow[i] = ((BeagleTreeLikelihood) likelihoods.get(i)).isDelayRescalingUntilUnderflow();
150 partialsRestrictions.add(i, ((BeagleTreeLikelihood) likelihoods.get(i)).getPartialsRestrictions());
151 }
152
153 if (DEBUG) {
154 System.err.println("Pattern counts: ");
155 for (int i = 0;i < siteCounts.length; i++) {
156 System.err.println(siteCounts[i] + " vs. " + patterns[i].getPatternCount());
157 }
158 System.err.println();
159 System.err.println("Instance counts: ");
160 for (int i = 0;i < instanceCounts.length; i++) {
161 System.err.print(instanceCounts[i] + " ");
162 }
163 System.err.println();
164 System.err.println("Current locations: ");
165 for (int i = 0;i < currentLocation.length; i++) {
166 System.err.print(currentLocation[i] + " ");
167 }
168 System.err.println();
169 }
170
171 TestThreadedCompoundLikelihood compound = new TestThreadedCompoundLikelihood(likelihoods);
172 //CompoundLikelihood compound = new CompoundLikelihood(likelihoods);
173 //ThreadedCompoundLikelihood compound = new ThreadedCompoundLikelihood(likelihoods);
174
175 if (DEBUG) {
176 System.err.println("Timing estimates for each of the " + calibrate + " likelihood calculations:");
177 }
178 double start = System.nanoTime();
179 for (int i = 0; i < calibrate; i++) {
180 if (DEBUG) {
181 //double debugStart = System.nanoTime();
182 compound.makeDirty();
183 compound.getLogLikelihood();
184 //double debugEnd = System.nanoTime();
185 //System.err.println(debugEnd - debugStart);
186 } else {
187 compound.makeDirty();
188 compound.getLogLikelihood();
189 }
190 }
191 double end = System.nanoTime();
192 double baseResult = end - start;
193
194 if (DEBUG) {
195 System.err.println("Starting evaluation took: " + baseResult);
196 }
197
198 int longestIndex = 0;
199 int longestSize = siteCounts[0];
200
201 //START TEST CODE
202 /*System.err.println("Detailed evaluation times: ");
203 long[] evaluationTimes = compound.getEvaluationTimes();
204 int[] evaluationCounts = compound.getEvaluationCounts();
205 long longest = evaluationTimes[0];
206 for (int i = 0; i < evaluationTimes.length; i++) {
207 System.err.println(i + ": time=" + evaluationTimes[i] + " count=" + evaluationCounts[i]);
208 if (evaluationTimes[i] > longest) {
209 longest = evaluationTimes[i];
210 }
211 }*/
212 //END TEST CODE
213
214 /*if (SPLIT_BY_PATTERN_COUNT) {
215
216 boolean notFinished = true;
217
218 while (notFinished) {
219
220 for (int i = 0; i < siteCounts.length; i++) {
221 if (siteCounts[i] > longestSize) {
222 longestIndex = i;
223 longestSize = siteCounts[longestIndex];
224 }
225 }
226 System.err.println("Split likelihood " + longestIndex + " with pattern count " + longestSize);
227
228 //split it in 2
229 int instanceCount = ++instanceCounts[longestIndex];
230
231 List<Likelihood> newList = new ArrayList<Likelihood>();
232 for (int i = 0; i < instanceCount; i++) {
233 Patterns subPatterns = new Patterns(patterns[longestIndex], 0, 0, 1, i, instanceCount);
234
235 BeagleTreeLikelihood treeLikelihood = createTreeLikelihood(
236 subPatterns, treeModels[longestIndex], branchModels[longestIndex], siteRateModels[longestIndex], branchRateModels[longestIndex],
237 null,
238 ambiguities[longestIndex], rescalingSchemes[longestIndex], partialsRestrictions.get(longestIndex),
239 xo);
240
241 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
242 newList.add(treeLikelihood);
243 }
244 for (int i = 0; i < newList.size()-1; i++) {
245 likelihoods.remove(currentLocation[longestIndex]);
246 }
247 //likelihoods.remove(longestIndex);
248 //likelihoods.add(longestIndex, new CompoundLikelihood(newList));
249 for (int i = 0; i < newList.size(); i++) {
250 likelihoods.add(currentLocation[longestIndex], newList.get(i));
251 }
252 for (int i = longestIndex+1; i < currentLocation.length; i++) {
253 currentLocation[i]++;
254 }
255 //compound = new ThreadedCompoundLikelihood(likelihoods);
256 compound = new CompoundLikelihood(likelihoods);
257 siteCounts[longestIndex] = (instanceCount-1)*siteCounts[longestIndex]/instanceCount;
258 longestSize = (instanceCount-1)*longestSize/instanceCount;
259
260 //check number of likelihoods
261 System.err.println("Number of BeagleTreeLikelihoods: " + compound.getLikelihoodCount());
262 System.err.println("Pattern counts: ");
263 for (int i = 0;i < siteCounts.length; i++) {
264 System.err.print(siteCounts[i] + " ");
265 }
266 System.err.println();
267 System.err.println("Instance counts: ");
268 for (int i = 0;i < instanceCounts.length; i++) {
269 System.err.print(instanceCounts[i] + " ");
270 }
271 System.err.println();
272 System.err.println("Current locations: ");
273 for (int i = 0;i < currentLocation.length; i++) {
274 System.err.print(currentLocation[i] + " ");
275 }
276 System.err.println();
277
278 //evaluate speed
279 start = System.nanoTime();
280 for (int i = 0; i < TEST_RUNS; i++) {
281 compound.makeDirty();
282 compound.getLogLikelihood();
283 }
284 end = System.nanoTime();
285 double newResult = end - start;
286 System.err.println("New evaluation took: " + newResult + " vs. old evaluation: " + baseResult);
287
288 if (newResult < baseResult) {
289 baseResult = newResult;
290 } else {
291 notFinished = false;
292
293 //remove 1 instanceCount
294 System.err.print("Removing 1 instance count: " + instanceCount);
295 instanceCount = --instanceCounts[longestIndex];
296 System.err.println(" -> " + instanceCount + " for likelihood " + longestIndex);
297 newList = new ArrayList<Likelihood>();
298 for (int i = 0; i < instanceCount; i++) {
299 Patterns subPatterns = new Patterns(patterns[longestIndex], 0, 0, 1, i, instanceCount);
300
301 BeagleTreeLikelihood treeLikelihood = createTreeLikelihood(
302 subPatterns, treeModels[longestIndex], branchModels[longestIndex], siteRateModels[longestIndex], branchRateModels[longestIndex],
303 null,
304 ambiguities[longestIndex], rescalingSchemes[longestIndex], partialsRestrictions.get(longestIndex),
305 xo);
306
307 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
308 newList.add(treeLikelihood);
309 }
310 for (int i = 0; i < newList.size()+1; i++) {
311 likelihoods.remove(currentLocation[longestIndex]);
312 }
313 for (int i = 0; i < newList.size(); i++) {
314 likelihoods.add(currentLocation[longestIndex], newList.get(i));
315 }
316 for (int i = longestIndex+1; i < currentLocation.length; i++) {
317 currentLocation[i]--;
318 }
319 //likelihoods.remove(longestIndex);
320 //likelihoods.add(longestIndex, new CompoundLikelihood(newList));
321
322 //compound = new ThreadedCompoundLikelihood(likelihoods);
323 compound = new CompoundLikelihood(likelihoods);
324 siteCounts[longestIndex] = (instanceCount+1)*siteCounts[longestIndex]/instanceCount;
325 longestSize = (instanceCount+1)*longestSize/instanceCount;
326
327 System.err.println("Pattern counts: ");
328 for (int i = 0;i < siteCounts.length; i++) {
329 System.err.print(siteCounts[i] + " ");
330 }
331 System.err.println();
332 System.err.println("Instance counts: ");
333 for (int i = 0;i < instanceCounts.length; i++) {
334 System.err.print(instanceCounts[i] + " ");
335 }
336 System.err.println();
337 System.err.println("Current locations: ");
338 for (int i = 0;i < currentLocation.length; i++) {
339 System.err.print(currentLocation[i] + " ");
340 }
341 System.err.println();
342
343 }
344
345 }
346
347 } else {*/
348
349 //Try splitting the same likelihood until no further improvement, then move on towards the next one
350 boolean notFinished = true;
351
352 //construct list with likelihoods to split up
353 List<Integer> splitList = new ArrayList<Integer>();
354 for (int i = 0; i < siteCounts.length; i++) {
355 int top = 0;
356 for (int j = 0; j < siteCounts.length; j++) {
357 if (siteCounts[j] > siteCounts[top]) {
358 top = j;
359 }
360 }
361 siteCounts[top] = 0;
362 splitList.add(top);
363 }
364 for (int i = 0; i < likelihoods.size(); i++) {
365 siteCounts[i] = patterns[i].getPatternCount();
366 if (DEBUG) {
367 System.err.println("Site count " + i + " = " + siteCounts[i]);
368 }
369 }
370
371 if (DEBUG) {
372 //print list
373 System.err.print("Ordered list of likelihoods to be evaluated: ");
374 for (int i = 0; i < splitList.size(); i++) {
375 System.err.print(splitList.get(i) + " ");
376 }
377 System.err.println();
378 }
379
380 int timesRetried = 0;
381
382 while (notFinished) {
383
384 //split it in 1 more piece
385 longestIndex = splitList.get(0);
386 int instanceCount = ++instanceCounts[longestIndex];
387
388 List<Likelihood> newList = new ArrayList<Likelihood>();
389 for (int i = 0; i < instanceCount; i++) {
390 Patterns subPatterns = new Patterns(patterns[longestIndex], 0, 0, 1, i, instanceCount);
391
392 BeagleTreeLikelihood treeLikelihood = createTreeLikelihood(
393 subPatterns, treeModels[longestIndex], branchModels[longestIndex], siteRateModels[longestIndex], branchRateModels[longestIndex],
394 null,
395 ambiguities[longestIndex], rescalingSchemes[longestIndex], isDelayRescalingUntilUnderflow[longestIndex],
396 partialsRestrictions.get(longestIndex),
397 xo);
398
399 treeLikelihood.setId(xo.getId() + "_" + longestIndex + "_" + i);
400 System.err.println(treeLikelihood.getId() + " created.");
401 newList.add(treeLikelihood);
402 }
403 for (int i = 0; i < newList.size()-1; i++) {
404 likelihoods.remove(currentLocation[longestIndex]);
405 }
406 //likelihoods.remove(longestIndex);
407 //likelihoods.add(longestIndex, new CompoundLikelihood(newList));
408 for (int i = 0; i < newList.size(); i++) {
409 likelihoods.add(currentLocation[longestIndex], newList.get(i));
410 }
411 for (int i = longestIndex+1; i < currentLocation.length; i++) {
412 currentLocation[i]++;
413 }
414 compound = new TestThreadedCompoundLikelihood(likelihoods);
415 //compound = new CompoundLikelihood(likelihoods);
416 //compound = new ThreadedCompoundLikelihood(likelihoods);
417 siteCounts[longestIndex] = (instanceCount-1)*siteCounts[longestIndex]/instanceCount;
418 longestSize = (instanceCount-1)*longestSize/instanceCount;
419
420 if (DEBUG) {
421 //check number of likelihoods
422 System.err.println("Number of BeagleTreeLikelihoods: " + compound.getLikelihoodCount());
423 System.err.println("Pattern counts: ");
424 for (int i = 0;i < siteCounts.length; i++) {
425 System.err.print(siteCounts[i] + " ");
426 }
427 System.err.println();
428 System.err.println("Instance counts: ");
429 for (int i = 0;i < instanceCounts.length; i++) {
430 System.err.print(instanceCounts[i] + " ");
431 }
432 System.err.println();
433 System.err.println("Current locations: ");
434 for (int i = 0;i < currentLocation.length; i++) {
435 System.err.print(currentLocation[i] + " ");
436 }
437 System.err.println();
438 }
439
440 //evaluate speed
441 if (DEBUG) {
442 System.err.println("Timing estimates for each of the " + calibrate + " likelihood calculations:");
443 }
444 start = System.nanoTime();
445 for (int i = 0; i < calibrate; i++) {
446 if (DEBUG) {
447 //double debugStart = System.nanoTime();
448 compound.makeDirty();
449 compound.getLogLikelihood();
450 //double debugEnd = System.nanoTime();
451 //System.err.println(debugEnd - debugStart);
452 } else {
453 compound.makeDirty();
454 compound.getLogLikelihood();
455 }
456 }
457 end = System.nanoTime();
458 double newResult = end - start;
459
460 if (DEBUG) {
461 System.err.println("New evaluation took: " + newResult + " vs. old evaluation: " + baseResult);
462 }
463
464 //START TEST CODE
465 /*evaluationTimes = compound.getEvaluationTimes();
466 evaluationCounts = compound.getEvaluationCounts();
467 longest = evaluationTimes[0];
468 for (int i = 0; i < evaluationTimes.length; i++) {
469 System.err.println(i + ": time=" + evaluationTimes[i] + " count=" + evaluationCounts[i]);
470 if (evaluationTimes[i] > longest) {
471 longest = evaluationTimes[i];
472 }
473 }*/
474 //END TEST CODE
475
476 if (newResult < baseResult) {
477 //new partitioning is faster, so partition further
478 baseResult = newResult;
479 //reorder split list
480 if (DEBUG) {
481 System.err.print("Current split list: ");
482 for (int i = 0; i < splitList.size(); i++) {
483 System.err.print(splitList.get(i) + " ");
484 }
485 System.err.println();
486 System.err.print("Current pattern counts: ");
487 for (int i = 0; i < splitList.size(); i++) {
488 System.err.print(siteCounts[splitList.get(i)] + " ");
489 }
490 System.err.println();
491 }
492 int currentPatternCount = siteCounts[longestIndex];
493 int findIndex = 0;
494 for (int i = 0; i < splitList.size(); i++) {
495 if (siteCounts[splitList.get(i)] > currentPatternCount) {
496 findIndex = i;
497 }
498 }
499 if (DEBUG) {
500 System.err.println("Current pattern count: " + currentPatternCount);
501 System.err.println("Index found: " + findIndex + " with pattern count: " + siteCounts[findIndex]);
502 System.err.println("Moving 0 to " + findIndex);
503 }
504 for (int i = 0; i < findIndex; i++) {
505 int temp = splitList.get(i);
506 splitList.set(i, splitList.get(i+1));
507 splitList.set(i+1, temp);
508 }
509 if (DEBUG) {
510 System.err.print("New split list: ");
511 for (int i = 0; i < splitList.size(); i++) {
512 System.err.print(splitList.get(i) + " ");
513 }
514 System.err.println();
515 System.err.print("New pattern counts: ");
516 for (int i = 0; i < splitList.size(); i++) {
517 System.err.print(siteCounts[splitList.get(i)] + " ");
518 }
519 System.err.println();
520 }
521 timesRetried = 0;
522 } else {
523 if (DEBUG) {
524 System.err.println("timesRetried = " + timesRetried + " vs. retry = " + retry);
525 }
526 //new partitioning is slower, so reinstate previous state unless RETRY is specified
527 if (timesRetried < retry) {
528 //try splitting further any way
529 //do not set baseResult
530 timesRetried++;
531 if (DEBUG) {
532 System.err.println("RETRY number " + timesRetried);
533 }
534 } else {
535
536 splitList.remove(0);
537 if (splitList.size() == 0) {
538 notFinished = false;
539 }
540
541 //remove timesTried instanceCount(s)
542 if (DEBUG) {
543 System.err.print("Removing " + (timesRetried+1) + " instance count(s): " + instanceCount);
544 }
545 //instanceCount = --instanceCounts[longestIndex];
546 instanceCounts[longestIndex] = instanceCounts[longestIndex] - (timesRetried+1);
547 instanceCount = instanceCounts[longestIndex];
548 if (DEBUG) {
549 System.err.println(" -> " + instanceCount + " for likelihood " + longestIndex);
550 }
551 newList = new ArrayList<Likelihood>();
552 for (int i = 0; i < instanceCount; i++) {
553 Patterns subPatterns = new Patterns(patterns[longestIndex], 0, 0, 1, i, instanceCount);
554
555 BeagleTreeLikelihood treeLikelihood = createTreeLikelihood(
556 subPatterns, treeModels[longestIndex], branchModels[longestIndex], siteRateModels[longestIndex], branchRateModels[longestIndex],
557 null,
558 ambiguities[longestIndex], rescalingSchemes[longestIndex], isDelayRescalingUntilUnderflow[longestIndex],
559 partialsRestrictions.get(longestIndex),
560 xo);
561
562 treeLikelihood.setId(xo.getId() + "_" + longestIndex + "_" + i);
563 System.err.println(treeLikelihood.getId() + " created.");
564 newList.add(treeLikelihood);
565 }
566 /*for (int i = 0; i < newList.size()+1; i++) {
567 likelihoods.remove(currentLocation[longestIndex]);
568 }*/
569 for (int i = 0; i < newList.size()+timesRetried+1; i++) {
570 //TEST CODE START
571 unregisterAllModels((BeagleTreeLikelihood)likelihoods.get(currentLocation[longestIndex]));
572 //TEST CODE END
573 likelihoods.remove(currentLocation[longestIndex]);
574 }
575 for (int i = 0; i < newList.size(); i++) {
576 likelihoods.add(currentLocation[longestIndex], newList.get(i));
577 }
578 for (int i = longestIndex+1; i < currentLocation.length; i++) {
579 currentLocation[i] -= (timesRetried+1);
580 }
581 //likelihoods.remove(longestIndex);
582 //likelihoods.add(longestIndex, new CompoundLikelihood(newList));
583
584 compound = new TestThreadedCompoundLikelihood(likelihoods);
585 //compound = new CompoundLikelihood(likelihoods);
586 //compound = new ThreadedCompoundLikelihood(likelihoods);
587 siteCounts[longestIndex] = (instanceCount+timesRetried+1)*siteCounts[longestIndex]/instanceCount;
588 longestSize = (instanceCount+timesRetried+1)*longestSize/instanceCount;
589
590 if (DEBUG) {
591 System.err.println("Pattern counts: ");
592 for (int i = 0;i < siteCounts.length; i++) {
593 System.err.print(siteCounts[i] + " ");
594 }
595 System.err.println();
596 System.err.println("Instance counts: ");
597 for (int i = 0;i < instanceCounts.length; i++) {
598 System.err.print(instanceCounts[i] + " ");
599 }
600 System.err.println();
601 System.err.println("Current locations: ");
602 for (int i = 0;i < currentLocation.length; i++) {
603 System.err.print(currentLocation[i] + " ");
604 }
605 System.err.println();
606 }
607
608 timesRetried = 0;
609
610 }
611
612 }
613
614 }
615
616 //}
617
618 /*CompoundLikelihood compound = new CompoundLikelihood(likelihoods);
619
620 double start = System.nanoTime();
621 for (int i = 0; i < TEST_RUNS; i++) {
622 compound.makeDirty();
623 compound.getLogLikelihood();
624 }
625 double end = System.nanoTime();
626 double baseResult = end - start;
627 System.err.println("Starting evaluation took: " + baseResult);
628
629 System.err.println("Detailed evaluation times: ");
630 long[] evaluationTimes = compound.getEvaluationTimes();
631 int[] evaluationCounts = compound.getEvaluationCounts();
632 long longest = evaluationTimes[0];
633 int longestIndex = 0;
634 ArrayList<Integer> list = null;
635
636 if (TRY_ALL_LIKELIHOODS) {
637
638 for (int i = 0; i < evaluationTimes.length; i++) {
639 System.err.println(i + ": time=" + evaluationTimes[i] + " count=" + evaluationCounts[i]);
640 if (evaluationTimes[i] > longest) {
641 longest = evaluationTimes[i];
642 longestIndex = i;
643 }
644 }
645 System.err.println("Longest likelihood calculation: " + longestIndex + " took " + longest);
646
647 long[] evalCopy = new long[evaluationTimes.length];
648 System.arraycopy(evaluationTimes, 0, evalCopy, 0, evalCopy.length);
649 list = new ArrayList<Integer>();
650 for (int i = 0; i < evalCopy.length; i++) {
651 int top = 0;
652 for (int j = 0; j < evalCopy.length; j++) {
653 if (evalCopy[j] > evalCopy[top]) {
654 top = j;
655 }
656 }
657 evalCopy[top] = 0;
658 list.add(top);
659 }
660 //print list
661 System.err.print("Ordered list of evaluation times: ");
662 for (int i = 0; i < list.size(); i++) {
663 System.err.print(list.get(i) + " ");
664 }
665 System.err.println();
666
667 }
668
669 boolean notFinished = true;
670
671 while (notFinished) {
672
673 if (TRY_ALL_LIKELIHOODS) {
674
675 //pick next one from the list
676 longestIndex = list.get(0);
677
678 } else {
679
680 longest = evaluationTimes[0];
681 for (int i = 0; i < evaluationTimes.length; i++) {
682 System.err.println(i + ": time=" + evaluationTimes[i] + " count=" + evaluationCounts[i]);
683 if (evaluationTimes[i] > longest) {
684 longest = evaluationTimes[i];
685 longestIndex = i;
686 }
687 }
688 System.err.println("Longest likelihood calculation: " + longestIndex + " took " + longest);
689
690 }
691
692 System.err.println("Split up likelihood " + longestIndex);
693
694 //split it in 2
695 int instanceCount = ++instanceCounts[longestIndex];
696
697 List<Likelihood> newList = new ArrayList<Likelihood>();
698 for (int i = 0; i < instanceCount; i++) {
699 Patterns subPatterns = new Patterns(patterns[longestIndex], 0, 0, 1, i, instanceCount);
700
701 BeagleTreeLikelihood treeLikelihood = createTreeLikelihood(
702 subPatterns, treeModels[longestIndex], branchModels[longestIndex], siteRateModels[longestIndex], branchRateModels[longestIndex],
703 null,
704 ambiguities[longestIndex], rescalingSchemes[longestIndex], partialsRestrictions.get(longestIndex),
705 xo);
706
707 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
708 newList.add(treeLikelihood);
709 }
710 likelihoods.remove(longestIndex);
711 likelihoods.add(longestIndex, new CompoundLikelihood(instanceCount, newList));
712 //likelihoods.add(longestIndex, new CompoundLikelihood(newList));
713 //compound = new CompoundLikelihood(totalInstances,likelihoods);
714 compound = new CompoundLikelihood(likelihoods);
715 siteCounts[longestIndex] /= 2;
716
717 //check number of likelihoods
718 System.err.println("Number of BeagleTreeLikelihoods: " + likelihoods.size());
719 System.err.println("Pattern counts: ");
720 for (int i = 0;i < siteCounts.length; i++) {
721 System.err.print(siteCounts[i] + " ");
722 }
723 System.err.println();
724 System.err.println("Instance counts: ");
725 for (int i = 0;i < instanceCounts.length; i++) {
726 System.err.print(instanceCounts[i] + " ");
727 }
728 System.err.println();
729
730 //evaluate speed
731 start = System.nanoTime();
732 for (int i = 0; i < TEST_RUNS; i++) {
733 compound.makeDirty();
734 //this does not help
735 //compound.resetEvaluationTimes();
736 compound.getLogLikelihood();
737 }
738 end = System.nanoTime();
739 double newResult = end - start;
740 System.err.println("New evaluation took: " + newResult + " vs. old evaluation: " + baseResult);
741
742 System.err.println("Detailed evaluation times: ");
743 evaluationTimes = compound.getEvaluationTimes();
744 evaluationCounts = compound.getEvaluationCounts();
745 //when there are multiple instances, get the maximum of each individual thread?
746 //work on this later, when a machine is available
747 //longest = evaluationTimes[0];
748 //longestIndex = 0;
749 for (int i = 0; i < evaluationTimes.length; i++) {
750 System.err.println(i + ": time=" + evaluationTimes[i] + " count=" + evaluationCounts[i]);
751 //if (evaluationTimes[i] > longest) {
752 // longest = evaluationTimes[i];
753 // longestIndex = i;
754 //}
755 if (instanceCounts[i] > 1) {
756 long[] tempTimes = ((CompoundLikelihood)likelihoods.get(i)).getEvaluationTimes();
757 int[] tempCounts = ((CompoundLikelihood)likelihoods.get(i)).getEvaluationCounts();
758 for (int j = 0; j < tempCounts.length; j++) {
759 System.err.println(" time=" + tempTimes[j] + " count=" + tempCounts[j]);
760 }
761 System.err.println(" Resetting times and counts.");
762 ((CompoundLikelihood)likelihoods.get(i)).resetEvaluationTimes();
763 }
764 }
765 //System.err.println("Longest likelihood calculation: " + longestIndex + " took " + longest);
766
767 if (TRY_ALL_LIKELIHOODS) {
768
769 if (newResult < baseResult) {
770 baseResult = newResult;
771 } else {
772 list.remove(0);
773 System.err.print("Ordered list of evaluation times: ");
774 for (int i = 0; i < list.size(); i++) {
775 System.err.print(list.get(i) + " ");
776 }
777 System.err.println();
778 //remove 1 instanceCount
779 System.err.print("Removing 1 instance count: " + instanceCount);
780 instanceCount = --instanceCounts[longestIndex];
781 System.err.println(" -> " + instanceCount + " for likelihood " + longestIndex);
782 newList = new ArrayList<Likelihood>();
783 for (int i = 0; i < instanceCount; i++) {
784 Patterns subPatterns = new Patterns(patterns[longestIndex], 0, 0, 1, i, instanceCount);
785
786 BeagleTreeLikelihood treeLikelihood = createTreeLikelihood(
787 subPatterns, treeModels[longestIndex], branchModels[longestIndex], siteRateModels[longestIndex], branchRateModels[longestIndex],
788 null,
789 ambiguities[longestIndex], rescalingSchemes[longestIndex], partialsRestrictions.get(longestIndex),
790 xo);
791
792 treeLikelihood.setId(xo.getId() + "_" + instanceCount);
793 newList.add(treeLikelihood);
794 }
795 likelihoods.remove(longestIndex);
796 likelihoods.add(longestIndex, new CompoundLikelihood(instanceCount, newList));
797 //likelihoods.add(longestIndex, new CompoundLikelihood(newList));
798 //compound = new CompoundLikelihood(totalInstances, likelihoods);
799 compound = new CompoundLikelihood(likelihoods);
800 siteCounts[longestIndex] *= 2;
801 }
802 if (list.isEmpty()) {
803 notFinished = false;
804 }
805
806 } else {
807
808 if (newResult < baseResult) {
809 baseResult = newResult;
810 } else {
811 notFinished = false;
812 }
813 }
814
815 }*/
816
817 /*for (int i = 0; i < originalLikelihoods.size(); i++) {
818 ((BeagleTreeLikelihood)originalLikelihoods.get(i)).removeModel(((BeagleTreeLikelihood)originalLikelihoods.get(i)).getBranchModel());
819 ((BeagleTreeLikelihood)originalLikelihoods.get(i)).removeModel(((BeagleTreeLikelihood)originalLikelihoods.get(i)).getBranchRateModel());
820 ((BeagleTreeLikelihood)originalLikelihoods.get(i)).removeModel(((BeagleTreeLikelihood)originalLikelihoods.get(i)).getSiteRateModel());
821 ((BeagleTreeLikelihood)originalLikelihoods.get(i)).removeModel(((BeagleTreeLikelihood)originalLikelihoods.get(i)).getTreeModel());
822 if (((BeagleTreeLikelihood)originalLikelihoods.get(i)).getTipStatesModel() != null) {
823 ((BeagleTreeLikelihood) originalLikelihoods.get(i)).removeModel(((BeagleTreeLikelihood) originalLikelihoods.get(i)).getTipStatesModel());
824 }
825 }*/
826
827 for (int i = 0; i < originalLikelihoods.size(); i++) {
828 unregisterAllModels((BeagleTreeLikelihood)originalLikelihoods.get(i));
829 }
830
831 return compound;
832
833 }
834
835 private void unregisterAllModels(BeagleTreeLikelihood btl) {
836 btl.removeModel(btl.getTreeModel());
837 btl.removeModel(btl.getBranchRateModel());
838 btl.removeModel(btl.getBranchModel());
839 btl.removeModel(btl.getSiteRateModel());
840 if (btl.getTipStatesModel() != null) {
841 btl.removeModel(btl.getTipStatesModel());
842 }
843 }
844
845 //************************************************************************
846 // AbstractXMLObjectParser implementation
847 //************************************************************************
848
849 public String getParserDescription() {
850 return "Parses a collection of BeagleTreeLikelihoods and determines the number of partitions.";
851 }
852
853 public Class getReturnType() {
854 return Likelihood.class;
855 }
856
857 public static final XMLSyntaxRule[] rules = {
858 new ElementRule(BeagleTreeLikelihood.class, 1, Integer.MAX_VALUE),
859 AttributeRule.newIntegerRule(CALIBRATE, true),
860 AttributeRule.newIntegerRule(RETRY, true)
861 };
862
863 public XMLSyntaxRule[] getSyntaxRules() {
864 return rules;
865 }
866 }