| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 69 |
do { |
| 70 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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); |
| 135 |
|
| 136 |
tmp = previous; previous = x; x = tmp; |
| 137 |
mmUpdate(previous, x); |
| 138 |
++iteration; |
| 139 |
|
| 140 |
copyDifference(secantsV[newestSecant], x, previous); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 168 |
for (int j = 0; j < J; ++j) { |
| 169 |
x[j] = Fx[j] + C[j]; |
| 170 |
} |
| 171 |
x[1] = 0.0; |
| 172 |
|
| 173 |
|
| 174 |
previousSecant = newestSecant; |
| 175 |
newestSecant = (newestSecant + 1) % qnQ; |
| 176 |
|
| 177 |
done = (convergenceCriterion(x, x0) < tolerance || iteration > maxIterations); |
| 178 |
|
| 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 |
|
| 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 |
|
| 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 |
} |