Debian Codegraph @

src/dr/inference/loggers/MLLogger.java

raw symbols SCIP
1 /*
2 * MLLogger.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.loggers;
27
28 import dr.inference.model.Likelihood;
29
30 /**
31 * A logger that stores maximum likelihood states.
32 *
33 * @author Andrew Rambaut
34 * @author Alexei Drummond
35 * @version $Id: MLLogger.java,v 1.21 2005/07/27 22:09:21 rambaut Exp $
36 */
37 public class MLLogger extends MCLogger {
38
39 private final Likelihood likelihood;
40 private double bestLikelihood;
41 private long bestState;
42 private String[] bestValues = null;
43 private long logEvery = 0;
44
45 public MLLogger(Likelihood likelihood, LogFormatter formatter, long logEvery) {
46
47 super(formatter, logEvery, false);
48
49 this.likelihood = likelihood;
50 }
51
52 public void startLogging() {
53 bestLikelihood = Double.NEGATIVE_INFINITY;
54 bestState = 0;
55 bestValues = new String[getColumnCount()];
56
57 if (logEvery > 0) {
58 String[] labels = new String[getColumnCount() + 1];
59
60 labels[0] = "state";
61
62 for (int i = 0; i < getColumnCount(); i++) {
63 labels[i + 1] = getColumnLabel(i);
64 }
65
66 logLabels(labels);
67 }
68
69 super.startLogging();
70 }
71
72 public void log(long state) {
73
74 double lik;
75
76 lik = likelihood.getLogLikelihood();
77
78 if (lik > bestLikelihood) {
79
80 for (int i = 0; i < getColumnCount(); i++) {
81 bestValues[i] = getColumnFormatted(i);
82 }
83
84 bestState = state;
85 bestLikelihood = lik;
86
87 if (logEvery == 1) {
88
89 String[] values = new String[getColumnCount() + 1];
90
91 values[0] = Long.toString(bestState);
92
93 System.arraycopy(bestValues, 0, values, 1, getColumnCount());
94
95 logValues(values);
96 }
97 }
98
99 if (logEvery > 1 && (state % logEvery == 0)) {
100
101 String[] values = new String[getColumnCount() + 1];
102
103 values[0] = Long.toString(bestState);
104
105 System.arraycopy(bestValues, 0, values, 1, getColumnCount());
106
107 logValues(values);
108 }
109 }
110
111 public void stopLogging() {
112 final int columnCount = getColumnCount();
113 String[] values = new String[columnCount + 2];
114
115 values[0] = Long.toString(bestState);
116 values[1] = Double.toString(bestLikelihood);
117
118 System.arraycopy(bestValues, 0, values, 2, columnCount);
119
120 if (logEvery > 0) {
121 logValues(values);
122 } else {
123 String[] labels = new String[columnCount + 2];
124
125 labels[0] = "state";
126 labels[1] = "ML";
127
128 for (int i = 0; i < columnCount; i++) {
129 labels[i + 2] = getColumnLabel(i);
130 }
131
132 logLabels(labels);
133 logValues(values);
134 }
135
136 super.stopLogging();
137 }
138 }