| 1 | /* |
| 2 | * MultivariateMinimum.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.math; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * abstract base class for minimisation of a multivariate function |
| 31 | * |
| 32 | * @author Korbinian Strimmer |
| 33 | */ |
| 34 | public abstract class MultivariateMinimum |
| 35 | { |
| 36 | // |
| 37 | // Public stuff |
| 38 | // |
| 39 | |
| 40 | /** total number of function evaluations necessary */ |
| 41 | public int numFun; |
| 42 | |
| 43 | /** |
| 44 | * maxFun is the maximum number of calls to fun allowed. |
| 45 | * the default value of 0 indicates no limit on the number |
| 46 | * of calls. |
| 47 | */ |
| 48 | public int maxFun = 0; |
| 49 | |
| 50 | /** |
| 51 | * numFuncStops is the number of consecutive positive |
| 52 | * evaluations of the stop criterion based on function evaluation |
| 53 | * necessary to cause the abortion of the optimization |
| 54 | * (default is 4) |
| 55 | */ |
| 56 | public int numFuncStops = 4; |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Find minimum close to vector x |
| 61 | * |
| 62 | * @param f multivariate function |
| 63 | * @param xvec initial guesses for the minimum |
| 64 | * (contains the location of the minimum on return) |
| 65 | * |
| 66 | * @return minimal function value |
| 67 | */ |
| 68 | public double findMinimum(MultivariateFunction f, double[] xvec) |
| 69 | { |
| 70 | optimize(f, xvec, MachineAccuracy.EPSILON, MachineAccuracy.EPSILON); |
| 71 | |
| 72 | return f.evaluate(xvec); |
| 73 | } |
| 74 | /** |
| 75 | * Find minimum close to vector x |
| 76 | * (desired fractional digits for each parameter is specified) |
| 77 | * |
| 78 | * @param f multivariate function |
| 79 | * @param xvec initial guesses for the minimum |
| 80 | * (contains the location of the minimum on return) |
| 81 | * @param fxFracDigits desired fractional digits in the function value |
| 82 | * @param xFracDigits desired fractional digits in parameters x |
| 83 | * |
| 84 | * @return minimal function value |
| 85 | */ |
| 86 | public double findMinimum(MultivariateFunction f, double[] xvec, |
| 87 | int fxFracDigits, int xFracDigits) |
| 88 | { |
| 89 | return findMinimum(f,xvec,fxFracDigits,xFracDigits,null); |
| 90 | } |
| 91 | /** |
| 92 | * Find minimum close to vector x |
| 93 | * (desired fractional digits for each parameter is specified) |
| 94 | * |
| 95 | * @param f multivariate function |
| 96 | * @param xvec initial guesses for the minimum |
| 97 | * (contains the location of the minimum on return) |
| 98 | * @param fxFracDigits desired fractional digits in the function value |
| 99 | * @param xFracDigits desired fractional digits in parameters x |
| 100 | * |
| 101 | * @return minimal function value |
| 102 | */ |
| 103 | public double findMinimum(MultivariateFunction f, double[] xvec, |
| 104 | int fxFracDigits, int xFracDigits, MinimiserMonitor monitor) |
| 105 | { |
| 106 | double tolfx = Math.pow(10, -1-fxFracDigits); |
| 107 | double tolx = Math.pow(10, -1-xFracDigits); |
| 108 | |
| 109 | optimize(f, xvec, tolfx, tolx,monitor); |
| 110 | |
| 111 | // trim x |
| 112 | double m = Math.pow(10, xFracDigits); |
| 113 | for (int i = 0; i < xvec.length; i++) |
| 114 | { |
| 115 | xvec[i] = Math.round(xvec[i]*m)/m; |
| 116 | } |
| 117 | // trim fx |
| 118 | return Math.round(f.evaluate(xvec)*m)/m; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * The actual optimization routine |
| 123 | * (needs to be implemented in a subclass of MultivariateMinimum). |
| 124 | * It finds a minimum close to vector x when the |
| 125 | * absolute tolerance for each parameter is specified. |
| 126 | * |
| 127 | * @param f multivariate function |
| 128 | * @param xvec initial guesses for the minimum |
| 129 | * (contains the location of the minimum on return) |
| 130 | * @param tolfx absolute tolerance of function value |
| 131 | * @param tolx absolute tolerance of each parameter |
| 132 | */ |
| 133 | public abstract void optimize(MultivariateFunction f, double[] xvec, double tolfx, double tolx); |
| 134 | |
| 135 | /** |
| 136 | * The actual optimization routine |
| 137 | * |
| 138 | * It finds a minimum close to vector x when the |
| 139 | * absolute tolerance for each parameter is specified. |
| 140 | * |
| 141 | * @param f multivariate function |
| 142 | * @param xvec initial guesses for the minimum |
| 143 | * (contains the location of the minimum on return) |
| 144 | * @param tolfx absolute tolerance of function value |
| 145 | * @param tolx absolute tolerance of each parameter |
| 146 | * @param monitor A monitor object that receives information about the minimising process (for display purposes) |
| 147 | * @note The default implementation just calls the optimize function with out the Monitor! |
| 148 | */ |
| 149 | |
| 150 | public void optimize(MultivariateFunction f, double[] xvec, double tolfx, double tolx, MinimiserMonitor monitor) { |
| 151 | optimize(f,xvec,tolfx,tolx); |
| 152 | } |
| 153 | /** |
| 154 | * Checks whether optimization should stop |
| 155 | * |
| 156 | * @param fx current function value |
| 157 | * @param x current values of function parameters |
| 158 | * @param tolfx absolute tolerance of function value |
| 159 | * @param tolx absolute tolerance of each parameter |
| 160 | * @param firstCall needs to be set to true when this routine is first called |
| 161 | * otherwise it should be set to false |
| 162 | * |
| 163 | * @return true if either x and its previous value are sufficiently similar |
| 164 | * or if fx and its previous values are sufficiently similar |
| 165 | * (test on function value has to be succesful numFuncStops consecutive |
| 166 | * times) |
| 167 | */ |
| 168 | public boolean stopCondition(double fx, double[] x, double tolfx, |
| 169 | double tolx, boolean firstCall) |
| 170 | { |
| 171 | boolean stop = false; |
| 172 | |
| 173 | if (firstCall) |
| 174 | { |
| 175 | countFuncStops = 0; |
| 176 | fxold = fx; |
| 177 | xold = new double[x.length]; |
| 178 | copy(xold, x); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | if (xStop(x, xold, tolx)) |
| 183 | { |
| 184 | stop = true; |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | if (fxStop(fx, fxold, tolfx)) |
| 189 | { |
| 190 | countFuncStops++; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | countFuncStops = 0; |
| 195 | } |
| 196 | |
| 197 | if (countFuncStops >= numFuncStops) |
| 198 | { |
| 199 | stop = true; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (!stop) |
| 205 | { |
| 206 | fxold = fx; |
| 207 | copy(xold, x); |
| 208 | } |
| 209 | |
| 210 | return stop; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Copy source vector into target vector |
| 216 | * |
| 217 | * @param target parameter array |
| 218 | * @param source parameter array |
| 219 | */ |
| 220 | public void copy(double[] target, double[] source) |
| 221 | { |
| 222 | for (int i = 0; i < source.length; i++) |
| 223 | { |
| 224 | target[i] = source[i]; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // |
| 229 | // Private stuff |
| 230 | // |
| 231 | |
| 232 | // number of fStops |
| 233 | private int countFuncStops; |
| 234 | |
| 235 | // old function and parameter values |
| 236 | private double fxold; |
| 237 | private double[] xold; |
| 238 | |
| 239 | private boolean xStop(double[] x, double[] xold, double tolx) |
| 240 | { |
| 241 | boolean stop = true; |
| 242 | |
| 243 | for (int i = 0; i < x.length && stop == true; i++) |
| 244 | { |
| 245 | if (Math.abs(x[i]-xold[i]) > tolx) |
| 246 | { |
| 247 | stop = false; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return stop; |
| 252 | } |
| 253 | |
| 254 | private boolean fxStop(double fx, double fxold, double tolfx) |
| 255 | { |
| 256 | if (Math.abs(fx-fxold) > tolfx) |
| 257 | { |
| 258 | return false; |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | } |