Debian Codegraph @

src/dr/evomodel/treelikelihood/AbstractTreeLikelihood.java

raw symbols SCIP
1 /*
2 * AbstractTreeLikelihood.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.evomodel.treelikelihood;
27
28 import dr.evolution.alignment.PatternList;
29 import dr.evolution.datatype.DataType;
30 import dr.evolution.tree.NodeRef;
31 import dr.evomodel.tree.TreeModel;
32 import dr.inference.model.*;
33 import dr.xml.Reportable;
34
35 /**
36 * AbstractTreeLikelihood - a base class for likelihood calculators of sites on a tree.
37 *
38 * @author Andrew Rambaut
39 * @author Marc Suchard
40 * @version $Id: AbstractTreeLikelihood.java,v 1.16 2005/06/07 16:27:39 alexei Exp $
41 */
42
43 @Deprecated // Switching to TreeDataLikelihood
44 public abstract class AbstractTreeLikelihood extends AbstractModelLikelihood implements Reportable {
45
46 protected static final boolean COUNT_TOTAL_OPERATIONS = true;
47
48 public AbstractTreeLikelihood(String name, TreeModel treeModel) {
49
50 super(name);
51
52 this.treeModel = treeModel;
53 addModel(treeModel);
54
55 nodeCount = treeModel.getNodeCount();
56
57 updateNode = new boolean[nodeCount];
58 for (int i = 0; i < nodeCount; i++) {
59 updateNode[i] = true;
60 }
61
62 likelihoodKnown = false;
63
64 hasInitialized = true;
65 }
66
67
68 /**
69 * Set update flag for a node only
70 */
71 protected void updateNode(NodeRef node) {
72 if (COUNT_TOTAL_OPERATIONS)
73 totalRateUpdateSingleCount++;
74
75 updateNode[node.getNumber()] = true;
76 likelihoodKnown = false;
77 }
78
79 /**
80 * Set update flag for a node and its direct children
81 */
82 protected void updateNodeAndChildren(NodeRef node) {
83 if (COUNT_TOTAL_OPERATIONS)
84 totalRateUpdateSingleCount++;
85
86 updateNode[node.getNumber()] = true;
87
88 for (int i = 0; i < treeModel.getChildCount(node); i++) {
89 if (COUNT_TOTAL_OPERATIONS)
90 totalRateUpdateSingleCount++;
91
92 NodeRef child = treeModel.getChild(node, i);
93 updateNode[child.getNumber()] = true;
94 }
95 likelihoodKnown = false;
96 }
97
98 /**
99 * Set update flag for a node and all its descendents
100 */
101 protected void updateNodeAndDescendents(NodeRef node) {
102 if (COUNT_TOTAL_OPERATIONS)
103 totalRateUpdateSingleCount++;
104
105 updateNode[node.getNumber()] = true;
106
107 for (int i = 0; i < treeModel.getChildCount(node); i++) {
108 NodeRef child = treeModel.getChild(node, i);
109 updateNodeAndDescendents(child);
110 }
111
112 likelihoodKnown = false;
113 }
114
115 /**
116 * Set update flag for all nodes
117 */
118 protected void updateAllNodes() {
119 if (COUNT_TOTAL_OPERATIONS)
120 totalRateUpdateAllCount++;
121
122 for (int i = 0; i < nodeCount; i++) {
123 updateNode[i] = true;
124 }
125 likelihoodKnown = false;
126 }
127
128 // **************************************************************
129 // VariableListener IMPLEMENTATION
130 // **************************************************************
131
132 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
133 // do nothing
134 }
135
136 // **************************************************************
137 // Model IMPLEMENTATION
138 // **************************************************************
139
140 protected void handleModelChangedEvent(Model model, Object object, int index) {
141 if (COUNT_TOTAL_OPERATIONS)
142 totalModelChangedCount++;
143 likelihoodKnown = false;
144 }
145
146 /**
147 * Stores the additional state other than model components
148 */
149 protected void storeState() {
150
151 storedLikelihoodKnown = likelihoodKnown;
152 storedLogLikelihood = logLikelihood;
153 }
154
155 /**
156 * Restore the additional stored state
157 */
158 protected void restoreState() {
159
160 likelihoodKnown = storedLikelihoodKnown;
161 logLikelihood = storedLogLikelihood;
162 }
163
164 protected void acceptState() {
165 } // nothing to do
166
167 // **************************************************************
168 // Likelihood IMPLEMENTATION
169 // **************************************************************
170
171 public final Model getModel() {
172 return this;
173 }
174
175 public final double getLogLikelihood() {
176 if (COUNT_TOTAL_OPERATIONS)
177 totalGetLogLikelihoodCount++;
178 if (CompoundLikelihood.DEBUG_PARALLEL_EVALUATION) {
179 System.err.println((likelihoodKnown ? "lazy" : "evaluate"));
180 }
181 if (!likelihoodKnown) {
182 if (COUNT_TOTAL_OPERATIONS)
183 totalCalculateLikelihoodCount++;
184 logLikelihood = calculateLogLikelihood();
185 likelihoodKnown = true;
186 }
187 return logLikelihood;
188 }
189
190 /**
191 * Forces a complete recalculation of the likelihood next time getLikelihood is called
192 */
193 public void makeDirty() {
194 if (COUNT_TOTAL_OPERATIONS)
195 totalMakeDirtyCount++;
196 likelihoodKnown = false;
197 updateAllNodes();
198 }
199
200 public boolean isLikelihoodKnown() {
201 return likelihoodKnown;
202 }
203
204 protected abstract double calculateLogLikelihood();
205
206 public String getReport() {
207 if (hasInitialized) {
208 String rtnValue = getClass().getName() + "(" + getLogLikelihood() + ")";
209 if (COUNT_TOTAL_OPERATIONS)
210 rtnValue += "\n total operations = " + totalOperationCount +
211 "\n matrix updates = " + totalMatrixUpdateCount +
212 "\n model changes = " + totalModelChangedCount +
213 "\n make dirties = " + totalMakeDirtyCount +
214 "\n calculate likelihoods = " + totalCalculateLikelihoodCount +
215 "\n get likelihoods = " + totalGetLogLikelihoodCount +
216 "\n all rate updates = " + totalRateUpdateAllCount +
217 "\n partial rate updates = " + totalRateUpdateSingleCount;
218 return rtnValue;
219 } else {
220 return getClass().getName() + "(uninitialized)";
221 }
222 }
223
224 // **************************************************************
225 // INSTANCE VARIABLES
226 // **************************************************************
227
228 /**
229 * the tree
230 */
231 protected TreeModel treeModel = null;
232
233 /**
234 * the number of nodes in the tree
235 */
236 protected int nodeCount;
237
238 /**
239 * Flags to specify which nodes are to be updated
240 */
241 protected boolean[] updateNode;
242
243 private double logLikelihood;
244 private double storedLogLikelihood;
245 protected boolean likelihoodKnown = false;
246 private boolean storedLikelihoodKnown = false;
247
248 protected boolean hasInitialized = false;
249
250 protected int totalOperationCount = 0;
251 protected int totalMatrixUpdateCount = 0;
252 protected int totalGetLogLikelihoodCount = 0;
253 protected int totalModelChangedCount = 0;
254 protected int totalMakeDirtyCount = 0;
255 protected int totalCalculateLikelihoodCount = 0;
256 protected int totalRateUpdateAllCount = 0;
257 protected int totalRateUpdateSingleCount = 0;
258
259 }