Debian Codegraph @

src/dr/inference/multidimensionalscaling/mm/MMAlgorithm.java

raw symbols SCIP
1 /*
2 * MMAlgorithm.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.multidimensionalscaling.mm;
27
28 import dr.inference.multidimensionalscaling.MultiDimensionalScalingLikelihood;
29 import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood;
30 import dr.xml.*;
31
32 /**
33 * Created by msuchard on 12/15/15.
34 */
35 public abstract class MMAlgorithm {
36
37 public static final double DEFAULT_TOLERANCE = 1E-1;
38 public static final int DEFAULT_MAX_ITERATIONS = 1000;
39
40 public double[] findMode(final double[] startingValue) throws NotConvergedException {
41 return findMode(startingValue, DEFAULT_TOLERANCE, DEFAULT_MAX_ITERATIONS);
42 }
43
44 private void copyDifference(double[] dest, final double[] x, final double[] y) {
45 final int len = dest.length;
46 for (int i = 0; i < len; ++i) {
47 dest[i] = x[i] - y[i];
48 }
49 }
50
51 public double[] findMode(final double[] startingValue, final double tolerance,
52 final int maxIterations) throws NotConvergedException {
53
54 if (DEBUG) {
55 System.err.println("Starting findMode with " + tolerance + " " + maxIterations);
56 }
57
58 double[] buffer1 = new double[startingValue.length];
59 double[] buffer2 = new double[startingValue.length];
60
61 double[] previous = buffer1;
62 double[] x = buffer2;
63
64 System.arraycopy(startingValue, 0, x, 0, startingValue.length);
65 iteration = 0;
66
67 if (qnQ == 0) {
68 // Standard MM
69 do {
70 // Move x -> previous
71 double[] tmp = previous; previous = x; x = tmp;
72
73 if (DEBUG) {
74 System.err.println("Current: " + printArray(previous));
75 }
76
77 mmUpdate(previous, x);
78 ++iteration;
79
80 if (DEBUG) {
81 System.err.println("Finished iteration " + iteration);
82 }
83
84 } while (convergenceCriterion(x, previous) > tolerance && iteration < maxIterations);
85
86 } else {
87 // Quasi-Newton acceleration
88 final int J = startingValue.length;
89 double[][] secantsU = new double[1][J];
90 double[][] secantsV = new double[1][J];
91
92 double[] Fx = new double[J];
93 double[] C = new double[J];
94 double[] x0 = new double[J];
95
96 // Fill initial secants
97 int countU = 0;
98 int countV = 0;
99
100 for (int q = 0; q < qnQ; ++q) {
101 double[] tmp = previous; previous = x; x = tmp;
102 mmUpdate(previous, x);
103 ++iteration;
104
105 if (countU == 0) {
106 copyDifference(secantsU[countU], x, previous);
107 ++countU;
108 } else if (countV < qnQ - 1) {
109 copyDifference(secantsU[countU], x, previous);
110 System.arraycopy(secantsU[countU], 0, secantsV[countV], 0, J);
111 ++countU;
112 ++countV;
113 } else {
114 copyDifference(secantsV[countV], x, previous);
115 ++countV;
116 }
117 }
118
119 int newestSecant = qnQ - 1;
120 int previousSecant = newestSecant - 1;
121
122 boolean done = false;
123
124 while (!done) {
125
126 System.arraycopy(x, 0, x0, 0, J);
127
128 // 2 cycles for each QN step
129 double[] tmp = previous; previous = x; x = tmp;
130 mmUpdate(previous, x);
131 ++iteration;
132
133 copyDifference(secantsU[newestSecant], x, previous);
134 System.arraycopy(x, 0, Fx, 0, J); // TODO Remove Fx?
135
136 tmp = previous; previous = x; x = tmp;
137 mmUpdate(previous, x);
138 ++iteration;
139
140 copyDifference(secantsV[newestSecant], x, previous);
141
142 // Do QN approximation here
143 // auto M = secantsU.transpose() * (secantsU - secantsV);
144 // auto Minv = M.inverse();
145 // auto A = secantsU.transpose() * secantsU.col(newestSecant);
146 // auto B = Minv * A;
147 // auto C = secantsV * B;
148 // VectorXd xqn = Fx + C;
149
150 double M = 0;
151 for (int j = 0; j < J; ++j) {
152 M += secantsU[0][j] * (secantsU[0][j] - secantsV[0][j]);
153 }
154
155 double Minv = 1.0 / M;
156
157 double A = 0;
158 for (int j = 0; j < J; ++j) {
159 A += secantsU[0][j] * secantsU[newestSecant][j];
160 }
161
162 double B = Minv * A;
163 for (int j = 0; j < J; ++j) {
164 C[j] = secantsV[0][j] * B;
165 }
166
167 // New step
168 for (int j = 0; j < J; ++j) {
169 x[j] = Fx[j] + C[j];
170 }
171 x[1] = 0.0; // Fixed point
172
173 // Get ready for next secant-pair
174 previousSecant = newestSecant;
175 newestSecant = (newestSecant + 1) % qnQ;
176
177 done = (convergenceCriterion(x, x0) < tolerance || iteration > maxIterations);
178 // throw new RuntimeException("A");
179
180 }
181
182
183
184 }
185
186
187
188 System.err.println("Finished in " + iteration + " iterations.");
189
190 if (iteration >= maxIterations) {
191 throw new NotConvergedException();
192 }
193
194 if (DEBUG) {
195 System.err.println("Final : " + printArray(x));
196 }
197
198 // throw new RuntimeException("out");
199
200 return x;
201 }
202
203 static private String format = "%5.3e";
204
205 protected String printArray(double[] x) {
206 StringBuilder sb = new StringBuilder();
207 sb.append(String.format(format, x[0]));
208 for (int i = 1; i < x.length; ++i) {
209 sb.append(", ").append(String.format(format, x[i]));
210 }
211 return sb.toString();
212 }
213
214 protected abstract void mmUpdate(final double[] current, double[] next);
215
216 private double convergenceCriterion(final double[] current, final double[] previous) {
217 double norm = 0.0;
218
219 for (int i = 0; i < current.length; ++i) {
220 norm += (current[i] - previous[i]) * (current[i] - previous[i]);
221 }
222
223 double value = Math.sqrt(norm);
224
225 if (DEBUG) {
226 System.err.println("Convergence = " + value);
227 }
228
229 return value;
230 }
231
232
233 class NotConvergedException extends Exception {
234 // Nothing interesting
235 }
236
237 final int qnQ = 0;
238
239 int iteration;
240
241 private static final boolean DEBUG = true;
242 private static final boolean PROGRESS = true;
243 }