| 1 |
|
| /* |
| 2 |
|
| * Limited memory BFGS (L-BFGS). |
| 3 |
|
| * |
| 4 |
|
| * Copyright (c) 1990, Jorge Nocedal |
| 5 |
|
| * Copyright (c) 2007, Naoaki Okazaki |
| 6 |
|
| * All rights reserved. |
| 7 |
|
| * |
| 8 |
|
| * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
|
| * of this software and associated documentation files (the "Software"), to deal |
| 10 |
|
| * in the Software without restriction, including without limitation the rights |
| 11 |
|
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
|
| * copies of the Software, and to permit persons to whom the Software is |
| 13 |
|
| * furnished to do so, subject to the following conditions: |
| 14 |
|
| * |
| 15 |
|
| * The above copyright notice and this permission notice shall be included in |
| 16 |
|
| * all copies or substantial portions of the Software. |
| 17 |
|
| * |
| 18 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
|
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
|
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
|
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
|
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
|
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
|
| * THE SOFTWARE. |
| 25 |
|
| */ |
| 26 |
|
| |
| 27 |
|
| /* $Id: lbfgs.c 56 2007-12-16 08:01:47Z naoaki $ */ |
| 28 |
|
| |
| 29 |
|
| /* |
| 30 |
|
| This library is a C port of the FORTRAN implementation of Limited-memory |
| 31 |
|
| Broyden-Fletcher-Goldfarb-Shanno (L-BFGS) method written by Jorge Nocedal. |
| 32 |
|
| The original FORTRAN source code is available at: |
| 33 |
|
| http://www.ece.northwestern.edu/~nocedal/lbfgs.html |
| 34 |
|
| |
| 35 |
|
| The L-BFGS algorithm is described in: |
| 36 |
|
| - Jorge Nocedal. |
| 37 |
|
| Updating Quasi-Newton Matrices with Limited Storage. |
| 38 |
|
| <i>Mathematics of Computation</i>, Vol. 35, No. 151, pp. 773--782, 1980. |
| 39 |
|
| - Dong C. Liu and Jorge Nocedal. |
| 40 |
|
| On the limited memory BFGS method for large scale optimization. |
| 41 |
|
| <i>Mathematical Programming</i> B, Vol. 45, No. 3, pp. 503-528, 1989. |
| 42 |
|
| |
| 43 |
|
| The line search algorithms used in this implementation are described in: |
| 44 |
|
| - John E. Dennis and Robert B. Schnabel. |
| 45 |
|
| <i>Numerical Methods for Unconstrained Optimization and Nonlinear |
| 46 |
|
| Equations</i>, Englewood Cliffs, 1983. |
| 47 |
|
| - Jorge J. More and David J. Thuente. |
| 48 |
|
| Line search algorithm with guaranteed sufficient decrease. |
| 49 |
|
| <i>ACM Transactions on Mathematical Software (TOMS)</i>, Vol. 20, No. 3, |
| 50 |
|
| pp. 286-307, 1994. |
| 51 |
|
| |
| 52 |
|
| This library also implements Orthant-Wise Limited-memory Quasi-Newton (OW-LQN) |
| 53 |
|
| method presented in: |
| 54 |
|
| - Galen Andrew and Jianfeng Gao. |
| 55 |
|
| Scalable training of L1-regularized log-linear models. |
| 56 |
|
| In <i>Proceedings of the 24th International Conference on Machine |
| 57 |
|
| Learning (ICML 2007)</i>, pp. 33-40, 2007. |
| 58 |
|
| |
| 59 |
|
| I would like to thank the original author, Jorge Nocedal, who has been |
| 60 |
|
| distributing the effieicnt and explanatory implementation in an open source |
| 61 |
|
| licence. |
| 62 |
|
| */ |
| 63 |
|
| |
| 64 |
|
| #ifdef HAVE_CONFIG_H |
| 65 |
|
| #include <config.h> |
| 66 |
|
| #endif/*HAVE_CONFIG_H*/ |
| 67 |
|
| |
| 68 |
|
| #include <stdio.h> |
| 69 |
|
| #include <stdlib.h> |
| 70 |
|
| #include <math.h> |
| 71 |
|
| |
| 72 |
|
| #include <lbfgs.h> |
| 73 |
|
| |
| 74 |
|
| #ifdef _MSC_VER |
| 75 |
|
| #define inline __inline |
| 76 |
|
| typedef unsigned int uint32_t; |
| 77 |
|
| #endif/*_MSC_VER*/ |
| 78 |
|
| |
| 79 |
|
| /* this block is added by laye */ |
| 80 |
|
| #ifndef _MSC_VER |
| 81 |
|
| #include <stdint.h> |
| 82 |
|
| #endif/*_MSC_VER*/ |
| 83 |
|
| |
| 84 |
|
| #if defined(USE_SSE) && defined(__SSE__) && LBFGS_FLOAT == 32 |
| 85 |
|
| /* Use SSE optimization for 32bit float precision. */ |
| 86 |
|
| #include "arithmetic_sse_float.h" |
| 87 |
|
| |
| 88 |
|
| #elif defined(USE_SSE) && defined(__SSE__) && LBFGS_FLOAT == 64 |
| 89 |
|
| /* Use SSE2 optimization for 64bit double precision. */ |
| 90 |
|
| #include "arithmetic_sse_double.h" |
| 91 |
|
| |
| 92 |
|
| #else |
| 93 |
|
| /* No CPU specific optimization. */ |
| 94 |
|
| #include "arithmetic_ansi.h" |
| 95 |
|
| |
| 96 |
|
| #endif |
| 97 |
|
| |
| 98 |
|
| #define min2(a, b) ((a) <= (b) ? (a) : (b)) |
| 99 |
|
| #define max2(a, b) ((a) >= (b) ? (a) : (b)) |
| 100 |
|
| #define max3(a, b, c) max2(max2((a), (b)), (c)); |
| 101 |
|
| |
| 102 |
|
| struct tag_iteration_data { |
| 103 |
|
| lbfgsfloatval_t alpha; |
| 104 |
|
| lbfgsfloatval_t *s; /* [n] */ |
| 105 |
|
| lbfgsfloatval_t *y; /* [n] */ |
| 106 |
|
| lbfgsfloatval_t ys; /* vecdot(y, s) */ |
| 107 |
|
| }; |
| 108 |
|
| typedef struct tag_iteration_data iteration_data_t; |
| 109 |
|
| |
| 110 |
|
| static const lbfgs_parameter_t _defparam = { |
| 111 |
|
| 6, 1e-5, 0, 20, |
| 112 |
|
| 1e-20, 1e20, 1e-4, 0.9, 1.0e-16, |
| 113 |
|
| 0.0, |
| 114 |
|
| }; |
| 115 |
|
| |
| 116 |
|
| /* Forward function declarations. */ |
| 117 |
|
| |
| 118 |
|
| static int line_search_backtracking( |
| 119 |
|
| int n, |
| 120 |
|
| lbfgsfloatval_t *x, |
| 121 |
|
| lbfgsfloatval_t *f, |
| 122 |
|
| lbfgsfloatval_t *g, |
| 123 |
|
| lbfgsfloatval_t *s, |
| 124 |
|
| lbfgsfloatval_t *stp, |
| 125 |
|
| lbfgsfloatval_t *wa, |
| 126 |
|
| lbfgs_evaluate_t proc_evaluate, |
| 127 |
|
| void *instance, |
| 128 |
|
| const lbfgs_parameter_t *param |
| 129 |
|
| ); |
| 130 |
|
| |
| 131 |
|
| static int line_search( |
| 132 |
|
| int n, |
| 133 |
|
| lbfgsfloatval_t *x, |
| 134 |
|
| lbfgsfloatval_t *f, |
| 135 |
|
| lbfgsfloatval_t *g, |
| 136 |
|
| lbfgsfloatval_t *s, |
| 137 |
|
| lbfgsfloatval_t *stp, |
| 138 |
|
| lbfgsfloatval_t *wa, |
| 139 |
|
| lbfgs_evaluate_t proc_evaluate, |
| 140 |
|
| void *instance, |
| 141 |
|
| const lbfgs_parameter_t *param |
| 142 |
|
| ); |
| 143 |
|
| |
| 144 |
|
| static int update_trial_interval( |
| 145 |
|
| lbfgsfloatval_t *x, |
| 146 |
|
| lbfgsfloatval_t *fx, |
| 147 |
|
| lbfgsfloatval_t *dx, |
| 148 |
|
| lbfgsfloatval_t *y, |
| 149 |
|
| lbfgsfloatval_t *fy, |
| 150 |
|
| lbfgsfloatval_t *dy, |
| 151 |
|
| lbfgsfloatval_t *t, |
| 152 |
|
| lbfgsfloatval_t *ft, |
| 153 |
|
| lbfgsfloatval_t *dt, |
| 154 |
|
| const lbfgsfloatval_t tmin, |
| 155 |
|
| const lbfgsfloatval_t tmax, |
| 156 |
|
| int *brackt |
| 157 |
|
| ); |
| 158 |
|
| |
| 159 |
|
| |
| 160 |
|
| |
| 161 |
|
| |
| 162 |
4 |
| void lbfgs_parameter_init(lbfgs_parameter_t *param) |
| 163 |
|
| { |
| 164 |
4 |
| memcpy(param, &_defparam, sizeof(*param)); |
| 165 |
4 |
| } |
| 166 |
|
| |
| 167 |
4 |
| int lbfgs( |
| 168 |
|
| const int n, |
| 169 |
|
| lbfgsfloatval_t *x, |
| 170 |
|
| lbfgsfloatval_t *ptr_fx, |
| 171 |
|
| lbfgs_evaluate_t proc_evaluate, |
| 172 |
|
| lbfgs_progress_t proc_progress, |
| 173 |
|
| void *instance, |
| 174 |
|
| lbfgs_parameter_t *_param |
| 175 |
|
| ) |
| 176 |
|
| { |
| 177 |
|
| int ret; |
| 178 |
|
| int i, j, k, ls, end, bound; |
| 179 |
|
| lbfgsfloatval_t step; |
| 180 |
|
| |
| 181 |
|
| /* Constant parameters and their default values. */ |
| 182 |
4 |
▶ | const lbfgs_parameter_t* param = (_param != NULL) ? _param : &_defparam; |
Branch: 1 branch
| Branch | True | False | Total |
| const lbfgs_parameter_t* param = (_param != NULL) ? _param : &_defparam; |
4 |
0 |
4 |
|
| 183 |
4 |
| const int m = param->m; |
| 184 |
|
| |
| 185 |
4 |
| lbfgsfloatval_t *xp = NULL, *g = NULL, *gp = NULL, *d = NULL, *w = NULL; |
| 186 |
4 |
| iteration_data_t *lm = NULL, *it = NULL; |
| 187 |
|
| lbfgsfloatval_t ys, yy; |
| 188 |
|
| lbfgsfloatval_t norm, xnorm, gnorm, beta; |
| 189 |
4 |
| lbfgsfloatval_t fx = 0.; |
| 190 |
|
| |
| 191 |
|
| /* Check the input parameters for errors. */ |
| 192 |
4 |
▶ | if (n <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) { |
0 |
4 |
4 |
|
| 193 |
0 |
| return LBFGSERR_INVALID_N; |
| 194 |
|
| } |
| 195 |
|
| #if defined(USE_SSE) && defined(__SSE__) |
| 196 |
|
| if (n % 8 != 0) { |
| 197 |
|
| return LBFGSERR_INVALID_N_SSE; |
| 198 |
|
| } |
| 199 |
|
| #endif/*defined(__SSE__)*/ |
| 200 |
4 |
▶ | if (param->min_step < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->min_step < 0.) { |
0 |
4 |
4 |
|
| 201 |
0 |
| return LBFGSERR_INVALID_MINSTEP; |
| 202 |
|
| } |
| 203 |
4 |
▶ | if (param->max_step < param->min_step) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->max_step < param->min_step) { |
0 |
4 |
4 |
|
| 204 |
0 |
| return LBFGSERR_INVALID_MAXSTEP; |
| 205 |
|
| } |
| 206 |
4 |
▶ | if (param->ftol < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->ftol < 0.) { |
0 |
4 |
4 |
|
| 207 |
0 |
| return LBFGSERR_INVALID_FTOL; |
| 208 |
|
| } |
| 209 |
4 |
▶ | if (param->gtol < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->gtol < 0.) { |
0 |
4 |
4 |
|
| 210 |
0 |
| return LBFGSERR_INVALID_GTOL; |
| 211 |
|
| } |
| 212 |
4 |
▶ | if (param->xtol < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->xtol < 0.) { |
0 |
4 |
4 |
|
| 213 |
0 |
| return LBFGSERR_INVALID_XTOL; |
| 214 |
|
| } |
| 215 |
4 |
▶ | if (param->max_linesearch <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->max_linesearch <= 0) { |
0 |
4 |
4 |
|
| 216 |
0 |
| return LBFGSERR_INVALID_MAXLINESEARCH; |
| 217 |
|
| } |
| 218 |
4 |
▶ | if (param->orthantwise_c < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c < 0.) { |
0 |
4 |
4 |
|
| 219 |
0 |
| return LBFGSERR_INVALID_ORTHANTWISE; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| /* Allocate working space. */ |
| 223 |
4 |
| xp = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); |
| 224 |
4 |
| g = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); |
| 225 |
4 |
| gp = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); |
| 226 |
4 |
| d = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); |
| 227 |
4 |
| w = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); |
| 228 |
4 |
▶ | if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) { |
Branch: 5 branches
| Branch | True | False | Total |
| if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) { |
4 |
0 |
4 |
| if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) { |
4 |
0 |
4 |
| if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) { |
4 |
0 |
4 |
| if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) { |
4 |
0 |
4 |
| if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) { |
0 |
4 |
4 |
|
| 229 |
0 |
| ret = LBFGSERR_OUTOFMEMORY; |
| 230 |
0 |
| goto lbfgs_exit; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| /* Allocate limited memory storage. */ |
| 234 |
4 |
| lm = (iteration_data_t*)vecalloc(m * sizeof(iteration_data_t)); |
| 235 |
4 |
▶ | if (lm == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lm == NULL) { |
0 |
4 |
4 |
|
| 236 |
0 |
| ret = LBFGSERR_OUTOFMEMORY; |
| 237 |
0 |
| goto lbfgs_exit; |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| /* Initialize the limited memory. */ |
| 241 |
28 |
▶ | for (i = 0;i < m;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < m;++i) { |
24 |
4 |
28 |
|
| 242 |
24 |
| it = &lm[i]; |
| 243 |
24 |
| it->alpha = 0; |
| 244 |
24 |
| it->ys = 0; |
| 245 |
24 |
| it->s = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); |
| 246 |
24 |
| it->y = (lbfgsfloatval_t*)vecalloc(n * sizeof(lbfgsfloatval_t)); |
| 247 |
24 |
▶ | if (it->s == NULL || it->y == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (it->s == NULL || it->y == NULL) { |
24 |
0 |
24 |
| if (it->s == NULL || it->y == NULL) { |
0 |
24 |
24 |
|
| 248 |
0 |
| ret = LBFGSERR_OUTOFMEMORY; |
| 249 |
0 |
| goto lbfgs_exit; |
| 250 |
|
| } |
| 251 |
|
| } |
| 252 |
|
| |
| 253 |
|
| /* Evaluate the function value and its gradient. */ |
| 254 |
4 |
| fx = proc_evaluate(instance, x, g, n, 0); |
| 255 |
4 |
▶ | if (0. < param->orthantwise_c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0. < param->orthantwise_c) { |
0 |
4 |
4 |
|
| 256 |
|
| /* Compute L1-regularization factor and add it to the object value. */ |
| 257 |
0 |
| norm = 0.; |
| 258 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 259 |
0 |
| norm += fabs(x[i]); |
| 260 |
|
| } |
| 261 |
0 |
| fx += norm * param->orthantwise_c; |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
|
| /* We assume the initial hessian matrix H_0 as the identity matrix. */ |
| 265 |
4 |
▶ | if (param->orthantwise_c == 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c == 0.) { |
4 |
0 |
4 |
|
| 266 |
4 |
| vecncpy(d, g, n); |
| 267 |
|
| } else { |
| 268 |
|
| /* Compute the negative of psuedo-gradients. */ |
| 269 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 270 |
0 |
▶ | if (x[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x[i] < 0.) { |
0 |
0 |
0 |
|
| 271 |
|
| /* Differentiable. */ |
| 272 |
0 |
| d[i] = -g[i] + param->orthantwise_c; |
| 273 |
0 |
▶ | } else if (0. < x[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (0. < x[i]) { |
0 |
0 |
0 |
|
| 274 |
|
| /* Differentiable. */ |
| 275 |
0 |
| d[i] = -g[i] - param->orthantwise_c; |
| 276 |
|
| } else { |
| 277 |
0 |
▶ | if (g[i] < -param->orthantwise_c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (g[i] < -param->orthantwise_c) { |
0 |
0 |
0 |
|
| 278 |
|
| /* Take the right partial derivative. */ |
| 279 |
0 |
| d[i] = -g[i] - param->orthantwise_c; |
| 280 |
0 |
▶ | } else if (param->orthantwise_c < g[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (param->orthantwise_c < g[i]) { |
0 |
0 |
0 |
|
| 281 |
|
| /* Take the left partial derivative. */ |
| 282 |
0 |
| d[i] = -g[i] + param->orthantwise_c; |
| 283 |
|
| } else { |
| 284 |
0 |
| d[i] = 0.; |
| 285 |
|
| } |
| 286 |
|
| } |
| 287 |
|
| } |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| /* Compute the initial step: |
| 291 |
|
| step = 1.0 / sqrt(vecdot(d, d, n)) |
| 292 |
|
| */ |
| 293 |
4 |
| vecrnorm(&step, d, n); |
| 294 |
|
| |
| 295 |
4 |
| k = 1; |
| 296 |
4 |
| end = 0; |
| 297 |
|
| for (;;) { |
| 298 |
|
| /* Store the current position and gradient vectors. */ |
| 299 |
12 |
| veccpy(xp, x, n); |
| 300 |
12 |
| veccpy(gp, g, n); |
| 301 |
|
| |
| 302 |
|
| /* Search for an optimal step. */ |
| 303 |
12 |
| ls = line_search( |
| 304 |
|
| n, x, &fx, g, d, &step, w, proc_evaluate, instance, param); |
| 305 |
12 |
▶ | if (ls < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ls < 0) { |
0 |
12 |
12 |
|
| 306 |
0 |
| ret = ls; |
| 307 |
0 |
| goto lbfgs_exit; |
| 308 |
|
| } |
| 309 |
|
| |
| 310 |
|
| /* Compute x and g norms. */ |
| 311 |
12 |
| vecnorm(&gnorm, g, n); |
| 312 |
12 |
| vecnorm(&xnorm, x, n); |
| 313 |
|
| |
| 314 |
|
| /* Report the progress. */ |
| 315 |
12 |
▶ | if (proc_progress) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (proc_progress) { |
4 |
8 |
12 |
|
| 316 |
4 |
▶ | if (ret = proc_progress(instance, x, g, fx, xnorm, gnorm, step, n, k, ls)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret = proc_progress(instance, x, g, fx, xnorm, gnorm, step, n, k, ls)) { |
0 |
4 |
4 |
|
| 317 |
0 |
| goto lbfgs_exit; |
| 318 |
|
| } |
| 319 |
|
| } |
| 320 |
|
| |
| 321 |
|
| /* |
| 322 |
|
| Convergence test. |
| 323 |
|
| The criterion is given by the following formula: |
| 324 |
|
| |g(x)| / \max(1, |x|) < \epsilon |
| 325 |
|
| */ |
| 326 |
12 |
▶ | if (xnorm < 1.0) xnorm = 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (xnorm < 1.0) xnorm = 1.0; |
7 |
5 |
12 |
|
| 327 |
12 |
▶ | if (gnorm / xnorm <= param->epsilon) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gnorm / xnorm <= param->epsilon) { |
4 |
8 |
12 |
|
| 328 |
|
| /* Convergence. */ |
| 329 |
4 |
| ret = 0; |
| 330 |
4 |
| break; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
8 |
▶ | if (param->max_iterations != 0 && param->max_iterations < k+1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (param->max_iterations != 0 && param->max_iterations < k+1) { |
0 |
8 |
8 |
| if (param->max_iterations != 0 && param->max_iterations < k+1) { |
0 |
0 |
0 |
|
| 334 |
|
| /* Maximum number of iterations. */ |
| 335 |
0 |
| ret = LBFGSERR_MAXIMUMITERATION; |
| 336 |
0 |
| break; |
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
|
| /* |
| 340 |
|
| Update vectors s and y: |
| 341 |
|
| s_{k+1} = x_{k+1} - x_{k} = \step * d_{k}. |
| 342 |
|
| y_{k+1} = g_{k+1} - g_{k}. |
| 343 |
|
| */ |
| 344 |
8 |
| it = &lm[end]; |
| 345 |
8 |
| vecdiff(it->s, x, xp, n); |
| 346 |
8 |
| vecdiff(it->y, g, gp, n); |
| 347 |
|
| |
| 348 |
|
| /* |
| 349 |
|
| Compute scalars ys and yy: |
| 350 |
|
| ys = y^t \cdot s = 1 / \rho. |
| 351 |
|
| yy = y^t \cdot y. |
| 352 |
|
| Notice that yy is used for scaling the hessian matrix H_0 (Cholesky factor). |
| 353 |
|
| */ |
| 354 |
8 |
| vecdot(&ys, it->y, it->s, n); |
| 355 |
8 |
| vecdot(&yy, it->y, it->y, n); |
| 356 |
8 |
| it->ys = ys; |
| 357 |
|
| |
| 358 |
|
| /* |
| 359 |
|
| Recursive formula to compute dir = -(H \cdot g). |
| 360 |
|
| This is described in page 779 of: |
| 361 |
|
| Jorge Nocedal. |
| 362 |
|
| Updating Quasi-Newton Matrices with Limited Storage. |
| 363 |
|
| Mathematics of Computation, Vol. 35, No. 151, |
| 364 |
|
| pp. 773--782, 1980. |
| 365 |
|
| */ |
| 366 |
8 |
| bound = (m <= k) ? m : k; |
| 367 |
8 |
| ++k; |
| 368 |
8 |
| end = (end + 1) % m; |
| 369 |
|
| |
| 370 |
8 |
▶ | if (param->orthantwise_c == 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c == 0.) { |
8 |
0 |
8 |
|
| 371 |
|
| /* Compute the negative of gradients. */ |
| 372 |
8 |
| vecncpy(d, g, n); |
| 373 |
|
| } else { |
| 374 |
|
| /* Compute the negative of psuedo-gradients. */ |
| 375 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 376 |
0 |
▶ | if (x[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x[i] < 0.) { |
0 |
0 |
0 |
|
| 377 |
|
| /* Differentiable. */ |
| 378 |
0 |
| d[i] = -g[i] + param->orthantwise_c; |
| 379 |
0 |
▶ | } else if (0. < x[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (0. < x[i]) { |
0 |
0 |
0 |
|
| 380 |
|
| /* Differentiable. */ |
| 381 |
0 |
| d[i] = -g[i] - param->orthantwise_c; |
| 382 |
|
| } else { |
| 383 |
0 |
▶ | if (g[i] < -param->orthantwise_c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (g[i] < -param->orthantwise_c) { |
0 |
0 |
0 |
|
| 384 |
|
| /* Take the right partial derivative. */ |
| 385 |
0 |
| d[i] = -g[i] - param->orthantwise_c; |
| 386 |
0 |
▶ | } else if (param->orthantwise_c < g[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (param->orthantwise_c < g[i]) { |
0 |
0 |
0 |
|
| 387 |
|
| /* Take the left partial derivative. */ |
| 388 |
0 |
| d[i] = -g[i] + param->orthantwise_c; |
| 389 |
|
| } else { |
| 390 |
0 |
| d[i] = 0.; |
| 391 |
|
| } |
| 392 |
|
| } |
| 393 |
|
| } |
| 394 |
|
| /* Store the steepest direction.*/ |
| 395 |
0 |
| veccpy(w, d, n); |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
8 |
| j = end; |
| 399 |
22 |
▶ | for (i = 0;i < bound;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < bound;++i) { |
14 |
8 |
22 |
|
| 400 |
14 |
| j = (j + m - 1) % m; /* if (--j == -1) j = m-1; */ |
| 401 |
14 |
| it = &lm[j]; |
| 402 |
|
| /* \alpha_{j} = \rho_{j} s^{t}_{j} \cdot q_{k+1}. */ |
| 403 |
14 |
| vecdot(&it->alpha, it->s, d, n); |
| 404 |
14 |
| it->alpha /= it->ys; |
| 405 |
|
| /* q_{i} = q_{i+1} - \alpha_{i} y_{i}. */ |
| 406 |
14 |
| vecadd(d, it->y, -it->alpha, n); |
| 407 |
|
| } |
| 408 |
|
| |
| 409 |
8 |
| vecscale(d, ys / yy, n); |
| 410 |
|
| |
| 411 |
22 |
▶ | for (i = 0;i < bound;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < bound;++i) { |
14 |
8 |
22 |
|
| 412 |
14 |
| it = &lm[j]; |
| 413 |
|
| /* \beta_{j} = \rho_{j} y^t_{j} \cdot \gamma_{i}. */ |
| 414 |
14 |
| vecdot(&beta, it->y, d, n); |
| 415 |
14 |
| beta /= it->ys; |
| 416 |
|
| /* \gamma_{i+1} = \gamma_{i} + (\alpha_{j} - \beta_{j}) s_{j}. */ |
| 417 |
14 |
| vecadd(d, it->s, it->alpha - beta, n); |
| 418 |
14 |
| j = (j + 1) % m; /* if (++j == m) j = 0; */ |
| 419 |
|
| } |
| 420 |
|
| |
| 421 |
|
| /* |
| 422 |
|
| Constrain the search direction for orthant-wise updates. |
| 423 |
|
| */ |
| 424 |
8 |
▶ | if (param->orthantwise_c != 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c != 0.) { |
0 |
8 |
8 |
|
| 425 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 426 |
0 |
▶ | if (d[i] * w[i] <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (d[i] * w[i] <= 0) { |
0 |
0 |
0 |
|
| 427 |
0 |
| d[i] = 0; |
| 428 |
|
| } |
| 429 |
|
| } |
| 430 |
|
| } |
| 431 |
|
| |
| 432 |
|
| /* |
| 433 |
|
| Now the search direction d is ready. We try step = 1 first. |
| 434 |
|
| */ |
| 435 |
8 |
| step = 1.0; |
| 436 |
|
| } |
| 437 |
|
| |
| 438 |
4 |
| lbfgs_exit: |
| 439 |
|
| /* Return the final value of the objective function. */ |
| 440 |
4 |
▶ | if (ptr_fx != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr_fx != NULL) { |
0 |
4 |
4 |
|
| 441 |
0 |
| *ptr_fx = fx; |
| 442 |
|
| } |
| 443 |
|
| |
| 444 |
|
| /* Free memory blocks used by this function. */ |
| 445 |
4 |
▶ | if (lm != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lm != NULL) { |
4 |
0 |
4 |
|
| 446 |
28 |
▶ | for (i = 0;i < m;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < m;++i) { |
24 |
4 |
28 |
|
| 447 |
24 |
| vecfree(lm[i].s); |
| 448 |
24 |
| vecfree(lm[i].y); |
| 449 |
|
| } |
| 450 |
4 |
| vecfree(lm); |
| 451 |
|
| } |
| 452 |
4 |
| vecfree(w); |
| 453 |
4 |
| vecfree(d); |
| 454 |
4 |
| vecfree(gp); |
| 455 |
4 |
| vecfree(g); |
| 456 |
4 |
| vecfree(xp); |
| 457 |
|
| |
| 458 |
4 |
| return ret; |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
|
| |
| 462 |
|
| |
| 463 |
0 |
| static int line_search_backtracking( |
| 464 |
|
| int n, |
| 465 |
|
| lbfgsfloatval_t *x, |
| 466 |
|
| lbfgsfloatval_t *f, |
| 467 |
|
| lbfgsfloatval_t *g, |
| 468 |
|
| lbfgsfloatval_t *s, |
| 469 |
|
| lbfgsfloatval_t *stp, |
| 470 |
|
| lbfgsfloatval_t *xp, |
| 471 |
|
| lbfgs_evaluate_t proc_evaluate, |
| 472 |
|
| void *instance, |
| 473 |
|
| const lbfgs_parameter_t *param |
| 474 |
|
| ) |
| 475 |
|
| { |
| 476 |
0 |
| int i, ret = 0, count = 0; |
| 477 |
0 |
| lbfgsfloatval_t width = 0.5, norm = 0.; |
| 478 |
0 |
| lbfgsfloatval_t finit, dginit = 0., dg, dgtest; |
| 479 |
|
| |
| 480 |
|
| /* Check the input parameters for errors. */ |
| 481 |
0 |
▶ | if (*stp <= 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*stp <= 0.) { |
0 |
0 |
0 |
|
| 482 |
0 |
| return LBFGSERR_INVALIDPARAMETERS; |
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
|
| /* Compute the initial gradient in the search direction. */ |
| 486 |
0 |
▶ | if (param->orthantwise_c != 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c != 0.) { |
0 |
0 |
0 |
|
| 487 |
|
| /* Use psuedo-gradients for orthant-wise updates. */ |
| 488 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 489 |
|
| /* Notice that: |
| 490 |
|
| (-s[i] < 0) <==> (g[i] < -param->orthantwise_c) |
| 491 |
|
| (-s[i] > 0) <==> (param->orthantwise_c < g[i]) |
| 492 |
|
| as the result of the lbfgs() function for orthant-wise updates. |
| 493 |
|
| */ |
| 494 |
0 |
▶ | if (s[i] != 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[i] != 0.) { |
0 |
0 |
0 |
|
| 495 |
0 |
▶ | if (x[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x[i] < 0.) { |
0 |
0 |
0 |
|
| 496 |
|
| /* Differentiable. */ |
| 497 |
0 |
| dginit += s[i] * (g[i] - param->orthantwise_c); |
| 498 |
0 |
▶ | } else if (0. < x[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (0. < x[i]) { |
0 |
0 |
0 |
|
| 499 |
|
| /* Differentiable. */ |
| 500 |
0 |
| dginit += s[i] * (g[i] + param->orthantwise_c); |
| 501 |
0 |
▶ | } else if (s[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (s[i] < 0.) { |
0 |
0 |
0 |
|
| 502 |
|
| /* Take the left partial derivative. */ |
| 503 |
0 |
| dginit += s[i] * (g[i] - param->orthantwise_c); |
| 504 |
0 |
▶ | } else if (0. < s[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (0. < s[i]) { |
0 |
0 |
0 |
|
| 505 |
|
| /* Take the right partial derivative. */ |
| 506 |
0 |
| dginit += s[i] * (g[i] + param->orthantwise_c); |
| 507 |
|
| } |
| 508 |
|
| } |
| 509 |
|
| } |
| 510 |
|
| } else { |
| 511 |
0 |
| vecdot(&dginit, g, s, n); |
| 512 |
|
| } |
| 513 |
|
| |
| 514 |
|
| /* Make sure that s points to a descent direction. */ |
| 515 |
0 |
▶ | if (0 < dginit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 < dginit) { |
0 |
0 |
0 |
|
| 516 |
0 |
| return LBFGSERR_INCREASEGRADIENT; |
| 517 |
|
| } |
| 518 |
|
| |
| 519 |
|
| /* The initial value of the objective function. */ |
| 520 |
0 |
| finit = *f; |
| 521 |
0 |
| dgtest = param->ftol * dginit; |
| 522 |
|
| |
| 523 |
|
| /* Copy the value of x to the work area. */ |
| 524 |
0 |
| veccpy(xp, x, n); |
| 525 |
|
| |
| 526 |
|
| for (;;) { |
| 527 |
0 |
| veccpy(x, xp, n); |
| 528 |
0 |
| vecadd(x, s, *stp, n); |
| 529 |
|
| |
| 530 |
0 |
▶ | if (param->orthantwise_c != 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c != 0.) { |
0 |
0 |
0 |
|
| 531 |
|
| /* The current point is projected onto the orthant of the initial one. */ |
| 532 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 533 |
0 |
▶ | if (x[i] * xp[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x[i] * xp[i] < 0.) { |
0 |
0 |
0 |
|
| 534 |
0 |
| x[i] = 0.; |
| 535 |
|
| } |
| 536 |
|
| } |
| 537 |
|
| } |
| 538 |
|
| |
| 539 |
|
| /* Evaluate the function and gradient values. */ |
| 540 |
0 |
| *f = proc_evaluate(instance, x, g, n, *stp); |
| 541 |
0 |
▶ | if (0. < param->orthantwise_c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0. < param->orthantwise_c) { |
0 |
0 |
0 |
|
| 542 |
|
| /* Compute L1-regularization factor and add it to the object value. */ |
| 543 |
0 |
| norm = 0.; |
| 544 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 545 |
0 |
| norm += fabs(x[i]); |
| 546 |
|
| } |
| 547 |
0 |
| *f += norm * param->orthantwise_c; |
| 548 |
|
| } |
| 549 |
|
| |
| 550 |
0 |
| vecdot(&dg, g, s, n); |
| 551 |
0 |
| ++count; |
| 552 |
|
| |
| 553 |
0 |
▶ | if (*f <= finit + *stp * dgtest) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*f <= finit + *stp * dgtest) { |
0 |
0 |
0 |
|
| 554 |
|
| /* The sufficient decrease condition. */ |
| 555 |
0 |
| return count; |
| 556 |
|
| } |
| 557 |
0 |
▶ | if (*stp < param->min_step) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*stp < param->min_step) { |
0 |
0 |
0 |
|
| 558 |
|
| /* The step is the minimum value. */ |
| 559 |
0 |
| ret = LBFGSERR_MINIMUMSTEP; |
| 560 |
0 |
| break; |
| 561 |
|
| } |
| 562 |
0 |
▶ | if (param->max_linesearch <= count) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->max_linesearch <= count) { |
0 |
0 |
0 |
|
| 563 |
|
| /* Maximum number of iteration. */ |
| 564 |
0 |
| ret = LBFGSERR_MAXIMUMLINESEARCH; |
| 565 |
0 |
| break; |
| 566 |
|
| } |
| 567 |
|
| |
| 568 |
0 |
| (*stp) *= width; |
| 569 |
|
| } |
| 570 |
|
| |
| 571 |
|
| /* Revert to the previous position. */ |
| 572 |
0 |
| veccpy(x, xp, n); |
| 573 |
0 |
| return ret; |
| 574 |
|
| } |
| 575 |
|
| |
| 576 |
|
| |
| 577 |
|
| |
| 578 |
12 |
| static int line_search( |
| 579 |
|
| int n, |
| 580 |
|
| lbfgsfloatval_t *x, |
| 581 |
|
| lbfgsfloatval_t *f, |
| 582 |
|
| lbfgsfloatval_t *g, |
| 583 |
|
| lbfgsfloatval_t *s, |
| 584 |
|
| lbfgsfloatval_t *stp, |
| 585 |
|
| lbfgsfloatval_t *wa, |
| 586 |
|
| lbfgs_evaluate_t proc_evaluate, |
| 587 |
|
| void *instance, |
| 588 |
|
| const lbfgs_parameter_t *param |
| 589 |
|
| ) |
| 590 |
|
| { |
| 591 |
12 |
| int i, count = 0; |
| 592 |
12 |
| int brackt, stage1, uinfo = 0; |
| 593 |
|
| lbfgsfloatval_t dg, norm; |
| 594 |
|
| lbfgsfloatval_t stx, fx, dgx; |
| 595 |
|
| lbfgsfloatval_t sty, fy, dgy; |
| 596 |
|
| lbfgsfloatval_t fxm, dgxm, fym, dgym, fm, dgm; |
| 597 |
|
| lbfgsfloatval_t finit, ftest1, dginit, dgtest; |
| 598 |
|
| lbfgsfloatval_t width, prev_width; |
| 599 |
|
| lbfgsfloatval_t stmin, stmax; |
| 600 |
|
| |
| 601 |
|
| /* Check the input parameters for errors. */ |
| 602 |
12 |
▶ | if (*stp <= 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*stp <= 0.) { |
0 |
12 |
12 |
|
| 603 |
0 |
| return LBFGSERR_INVALIDPARAMETERS; |
| 604 |
|
| } |
| 605 |
|
| |
| 606 |
|
| /* Compute the initial gradient in the search direction. */ |
| 607 |
12 |
▶ | if (param->orthantwise_c != 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c != 0.) { |
0 |
12 |
12 |
|
| 608 |
|
| /* Use psuedo-gradients for orthant-wise updates. */ |
| 609 |
0 |
| dginit = 0.; |
| 610 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 611 |
|
| /* Notice that: |
| 612 |
|
| (-s[i] < 0) <==> (g[i] < -param->orthantwise_c) |
| 613 |
|
| (-s[i] > 0) <==> (param->orthantwise_c < g[i]) |
| 614 |
|
| as the result of the lbfgs() function for orthant-wise updates. |
| 615 |
|
| */ |
| 616 |
0 |
▶ | if (s[i] != 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[i] != 0.) { |
0 |
0 |
0 |
|
| 617 |
0 |
▶ | if (x[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x[i] < 0.) { |
0 |
0 |
0 |
|
| 618 |
|
| /* Differentiable. */ |
| 619 |
0 |
| dginit += s[i] * (g[i] - param->orthantwise_c); |
| 620 |
0 |
▶ | } else if (0. < x[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (0. < x[i]) { |
0 |
0 |
0 |
|
| 621 |
|
| /* Differentiable. */ |
| 622 |
0 |
| dginit += s[i] * (g[i] + param->orthantwise_c); |
| 623 |
0 |
▶ | } else if (s[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (s[i] < 0.) { |
0 |
0 |
0 |
|
| 624 |
|
| /* Take the left partial derivative. */ |
| 625 |
0 |
| dginit += s[i] * (g[i] - param->orthantwise_c); |
| 626 |
0 |
▶ | } else if (0. < s[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (0. < s[i]) { |
0 |
0 |
0 |
|
| 627 |
|
| /* Take the right partial derivative. */ |
| 628 |
0 |
| dginit += s[i] * (g[i] + param->orthantwise_c); |
| 629 |
|
| } |
| 630 |
|
| } |
| 631 |
|
| } |
| 632 |
|
| } else { |
| 633 |
12 |
| vecdot(&dginit, g, s, n); |
| 634 |
|
| } |
| 635 |
|
| |
| 636 |
|
| /* Make sure that s points to a descent direction. */ |
| 637 |
12 |
▶ | if (0 < dginit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 < dginit) { |
0 |
12 |
12 |
|
| 638 |
0 |
| return LBFGSERR_INCREASEGRADIENT; |
| 639 |
|
| } |
| 640 |
|
| |
| 641 |
|
| /* Initialize local variables. */ |
| 642 |
12 |
| brackt = 0; |
| 643 |
12 |
| stage1 = 1; |
| 644 |
12 |
| finit = *f; |
| 645 |
12 |
| dgtest = param->ftol * dginit; |
| 646 |
12 |
| width = param->max_step - param->min_step; |
| 647 |
12 |
| prev_width = 2.0 * width; |
| 648 |
|
| |
| 649 |
|
| /* Copy the value of x to the work area. */ |
| 650 |
12 |
| veccpy(wa, x, n); |
| 651 |
|
| |
| 652 |
|
| /* |
| 653 |
|
| The variables stx, fx, dgx contain the values of the step, |
| 654 |
|
| function, and directional derivative at the best step. |
| 655 |
|
| The variables sty, fy, dgy contain the value of the step, |
| 656 |
|
| function, and derivative at the other endpoint of |
| 657 |
|
| the interval of uncertainty. |
| 658 |
|
| The variables stp, f, dg contain the values of the step, |
| 659 |
|
| function, and derivative at the current step. |
| 660 |
|
| */ |
| 661 |
12 |
| stx = sty = 0.; |
| 662 |
12 |
| fx = fy = finit; |
| 663 |
12 |
| dgx = dgy = dginit; |
| 664 |
|
| |
| 665 |
|
| for (;;) { |
| 666 |
|
| /* |
| 667 |
|
| Set the minimum and maximum steps to correspond to the |
| 668 |
|
| present interval of uncertainty. |
| 669 |
|
| */ |
| 670 |
14 |
▶ | if (brackt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (brackt) { |
0 |
14 |
14 |
|
| 671 |
0 |
▶ | stmin = min2(stx, sty); |
Branch: 1 branch
| Branch | True | False | Total |
| stmin = min2(stx, sty); |
0 |
0 |
0 |
|
| 672 |
0 |
▶ | stmax = max2(stx, sty); |
Branch: 1 branch
| Branch | True | False | Total |
| stmax = max2(stx, sty); |
0 |
0 |
0 |
|
| 673 |
|
| } else { |
| 674 |
14 |
| stmin = stx; |
| 675 |
14 |
| stmax = *stp + 4.0 * (*stp - stx); |
| 676 |
|
| } |
| 677 |
|
| |
| 678 |
|
| /* Clip the step in the range of [stpmin, stpmax]. */ |
| 679 |
14 |
▶ | if (*stp < param->min_step) *stp = param->min_step; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*stp < param->min_step) *stp = param->min_step; |
0 |
14 |
14 |
|
| 680 |
14 |
▶ | if (param->max_step < *stp) *stp = param->max_step; |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->max_step < *stp) *stp = param->max_step; |
0 |
14 |
14 |
|
| 681 |
|
| |
| 682 |
|
| /* |
| 683 |
|
| If an unusual termination is to occur then let |
| 684 |
|
| stp be the lowest point obtained so far. |
| 685 |
|
| */ |
| 686 |
14 |
▶ | if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
Branch: 7 branches
| Branch | True | False | Total |
| if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
0 |
14 |
14 |
| if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
0 |
0 |
0 |
| if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
0 |
0 |
0 |
| if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
0 |
0 |
0 |
| if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
0 |
0 |
0 |
| if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
0 |
14 |
14 |
| if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->xtol * stmax))) { |
0 |
0 |
0 |
|
| 687 |
0 |
| *stp = stx; |
| 688 |
|
| } |
| 689 |
|
| |
| 690 |
|
| /* |
| 691 |
|
| Compute the current value of x: |
| 692 |
|
| x <- x + (*stp) * s. |
| 693 |
|
| */ |
| 694 |
14 |
| veccpy(x, wa, n); |
| 695 |
14 |
| vecadd(x, s, *stp, n); |
| 696 |
|
| |
| 697 |
14 |
▶ | if (param->orthantwise_c != 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->orthantwise_c != 0.) { |
0 |
14 |
14 |
|
| 698 |
|
| /* The current point is projected onto the orthant of the previous one. */ |
| 699 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 700 |
0 |
▶ | if (x[i] * wa[i] < 0.) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x[i] * wa[i] < 0.) { |
0 |
0 |
0 |
|
| 701 |
0 |
| x[i] = 0.; |
| 702 |
|
| } |
| 703 |
|
| } |
| 704 |
|
| } |
| 705 |
|
| |
| 706 |
|
| /* Evaluate the function and gradient values. */ |
| 707 |
14 |
| *f = proc_evaluate(instance, x, g, n, *stp); |
| 708 |
14 |
▶ | if (0. < param->orthantwise_c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0. < param->orthantwise_c) { |
0 |
14 |
14 |
|
| 709 |
|
| /* Compute L1-regularization factor and add it to the object value. */ |
| 710 |
0 |
| norm = 0.; |
| 711 |
0 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
0 |
0 |
0 |
|
| 712 |
0 |
| norm += fabs(x[i]); |
| 713 |
|
| } |
| 714 |
0 |
| *f += norm * param->orthantwise_c; |
| 715 |
|
| } |
| 716 |
|
| |
| 717 |
14 |
| ++count; |
| 718 |
|
| |
| 719 |
14 |
| vecdot(&dg, g, s, n); |
| 720 |
14 |
| ftest1 = finit + *stp * dgtest; |
| 721 |
|
| |
| 722 |
|
| /* Test for errors and convergence. */ |
| 723 |
14 |
▶ | if (brackt && ((*stp <= stmin || stmax <= *stp) || uinfo != 0)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (brackt && ((*stp <= stmin || stmax <= *stp) || uinfo != 0)) { |
0 |
14 |
14 |
| if (brackt && ((*stp <= stmin || stmax <= *stp) || uinfo != 0)) { |
0 |
0 |
0 |
| if (brackt && ((*stp <= stmin || stmax <= *stp) || uinfo != 0)) { |
0 |
0 |
0 |
| if (brackt && ((*stp <= stmin || stmax <= *stp) || uinfo != 0)) { |
0 |
0 |
0 |
|
| 724 |
|
| /* Rounding errors prevent further progress. */ |
| 725 |
0 |
| return LBFGSERR_ROUNDING_ERROR; |
| 726 |
|
| } |
| 727 |
14 |
▶ | if (*stp == param->max_step && *f <= ftest1 && dg <= dgtest) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*stp == param->max_step && *f <= ftest1 && dg <= dgtest) { |
0 |
14 |
14 |
| if (*stp == param->max_step && *f <= ftest1 && dg <= dgtest) { |
0 |
0 |
0 |
| if (*stp == param->max_step && *f <= ftest1 && dg <= dgtest) { |
0 |
0 |
0 |
|
| 728 |
|
| /* The step is the maximum value. */ |
| 729 |
0 |
| return LBFGSERR_MAXIMUMSTEP; |
| 730 |
|
| } |
| 731 |
14 |
▶ | if (*stp == param->min_step && (ftest1 < *f || dgtest <= dg)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*stp == param->min_step && (ftest1 < *f || dgtest <= dg)) { |
0 |
14 |
14 |
| if (*stp == param->min_step && (ftest1 < *f || dgtest <= dg)) { |
0 |
0 |
0 |
| if (*stp == param->min_step && (ftest1 < *f || dgtest <= dg)) { |
0 |
0 |
0 |
|
| 732 |
|
| /* The step is the minimum value. */ |
| 733 |
0 |
| return LBFGSERR_MINIMUMSTEP; |
| 734 |
|
| } |
| 735 |
14 |
▶ | if (brackt && (stmax - stmin) <= param->xtol * stmax) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (brackt && (stmax - stmin) <= param->xtol * stmax) { |
0 |
14 |
14 |
| if (brackt && (stmax - stmin) <= param->xtol * stmax) { |
0 |
0 |
0 |
|
| 736 |
|
| /* Relative width of the interval of uncertainty is at most xtol. */ |
| 737 |
0 |
| return LBFGSERR_WIDTHTOOSMALL; |
| 738 |
|
| } |
| 739 |
14 |
▶ | if (param->max_linesearch <= count) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->max_linesearch <= count) { |
0 |
14 |
14 |
|
| 740 |
|
| /* Maximum number of iteration. */ |
| 741 |
0 |
| return LBFGSERR_MAXIMUMLINESEARCH; |
| 742 |
|
| } |
| 743 |
14 |
▶ | if (*f <= ftest1 && fabs(dg) <= param->gtol * (-dginit)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*f <= ftest1 && fabs(dg) <= param->gtol * (-dginit)) { |
14 |
0 |
14 |
| if (*f <= ftest1 && fabs(dg) <= param->gtol * (-dginit)) { |
12 |
2 |
14 |
|
| 744 |
|
| /* The sufficient decrease condition and the directional derivative condition hold. */ |
| 745 |
12 |
| return count; |
| 746 |
|
| } |
| 747 |
|
| |
| 748 |
|
| /* |
| 749 |
|
| In the first stage we seek a step for which the modified |
| 750 |
|
| function has a nonpositive value and nonnegative derivative. |
| 751 |
|
| */ |
| 752 |
2 |
▶ | if (stage1 && *f <= ftest1 && min2(param->ftol, param->gtol) * dginit <= dg) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (stage1 && *f <= ftest1 && min2(param->ftol, param->gtol) * dginit <= dg) { |
2 |
0 |
2 |
| if (stage1 && *f <= ftest1 && min2(param->ftol, param->gtol) * dginit <= dg) { |
2 |
0 |
2 |
| if (stage1 && *f <= ftest1 && min2(param->ftol, param->gtol) * dginit <= dg) { |
2 |
0 |
2 |
| if (stage1 && *f <= ftest1 && min2(param->ftol, param->gtol) * dginit <= dg) { |
0 |
2 |
2 |
|
| 753 |
0 |
| stage1 = 0; |
| 754 |
|
| } |
| 755 |
|
| |
| 756 |
|
| /* |
| 757 |
|
| A modified function is used to predict the step only if |
| 758 |
|
| we have not obtained a step for which the modified |
| 759 |
|
| function has a nonpositive function value and nonnegative |
| 760 |
|
| derivative, and if a lower function value has been |
| 761 |
|
| obtained but the decrease is not sufficient. |
| 762 |
|
| */ |
| 763 |
2 |
▶ | if (stage1 && ftest1 < *f && *f <= fx) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (stage1 && ftest1 < *f && *f <= fx) { |
2 |
0 |
2 |
| if (stage1 && ftest1 < *f && *f <= fx) { |
0 |
2 |
2 |
| if (stage1 && ftest1 < *f && *f <= fx) { |
0 |
0 |
0 |
|
| 764 |
|
| /* Define the modified function and derivative values. */ |
| 765 |
0 |
| fm = *f - *stp * dgtest; |
| 766 |
0 |
| fxm = fx - stx * dgtest; |
| 767 |
0 |
| fym = fy - sty * dgtest; |
| 768 |
0 |
| dgm = dg - dgtest; |
| 769 |
0 |
| dgxm = dgx - dgtest; |
| 770 |
0 |
| dgym = dgy - dgtest; |
| 771 |
|
| |
| 772 |
|
| /* |
| 773 |
|
| Call update_trial_interval() to update the interval of |
| 774 |
|
| uncertainty and to compute the new step. |
| 775 |
|
| */ |
| 776 |
0 |
| uinfo = update_trial_interval( |
| 777 |
|
| &stx, &fxm, &dgxm, |
| 778 |
|
| &sty, &fym, &dgym, |
| 779 |
|
| stp, &fm, &dgm, |
| 780 |
|
| stmin, stmax, &brackt |
| 781 |
|
| ); |
| 782 |
|
| |
| 783 |
|
| /* Reset the function and gradient values for f. */ |
| 784 |
0 |
| fx = fxm + stx * dgtest; |
| 785 |
0 |
| fy = fym + sty * dgtest; |
| 786 |
0 |
| dgx = dgxm + dgtest; |
| 787 |
0 |
| dgy = dgym + dgtest; |
| 788 |
|
| } else { |
| 789 |
|
| /* |
| 790 |
|
| Call update_trial_interval() to update the interval of |
| 791 |
|
| uncertainty and to compute the new step. |
| 792 |
|
| */ |
| 793 |
2 |
| uinfo = update_trial_interval( |
| 794 |
|
| &stx, &fx, &dgx, |
| 795 |
|
| &sty, &fy, &dgy, |
| 796 |
|
| stp, f, &dg, |
| 797 |
|
| stmin, stmax, &brackt |
| 798 |
|
| ); |
| 799 |
|
| } |
| 800 |
|
| |
| 801 |
|
| /* |
| 802 |
|
| Force a sufficient decrease in the interval of uncertainty. |
| 803 |
|
| */ |
| 804 |
2 |
▶ | if (brackt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (brackt) { |
0 |
2 |
2 |
|
| 805 |
0 |
▶ | if (0.66 * prev_width <= fabs(sty - stx)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0.66 * prev_width <= fabs(sty - stx)) { |
0 |
0 |
0 |
|
| 806 |
0 |
| *stp = stx + 0.5 * (sty - stx); |
| 807 |
|
| } |
| 808 |
0 |
| prev_width = width; |
| 809 |
0 |
| width = fabs(sty - stx); |
| 810 |
|
| } |
| 811 |
|
| } |
| 812 |
|
| |
| 813 |
|
| return LBFGSERR_LOGICERROR; |
| 814 |
|
| } |
| 815 |
|
| |
| 816 |
|
| |
| 817 |
|
| |
| 818 |
|
| /** |
| 819 |
|
| * Define the local variables for computing minimizers. |
| 820 |
|
| */ |
| 821 |
|
| #define USES_MINIMIZER \ |
| 822 |
|
| lbfgsfloatval_t a, d, gamma, theta, p, q, r, s; |
| 823 |
|
| |
| 824 |
|
| /** |
| 825 |
|
| * Find a minimizer of an interpolated cubic function. |
| 826 |
|
| * @param cm The minimizer of the interpolated cubic. |
| 827 |
|
| * @param u The value of one point, u. |
| 828 |
|
| * @param fu The value of f(u). |
| 829 |
|
| * @param du The value of f'(u). |
| 830 |
|
| * @param v The value of another point, v. |
| 831 |
|
| * @param fv The value of f(v). |
| 832 |
|
| * @param du The value of f'(v). |
| 833 |
|
| */ |
| 834 |
|
| #define CUBIC_MINIMIZER(cm, u, fu, du, v, fv, dv) \ |
| 835 |
|
| d = (v) - (u); \ |
| 836 |
|
| theta = ((fu) - (fv)) * 3 / d + (du) + (dv); \ |
| 837 |
|
| p = fabs(theta); \ |
| 838 |
|
| q = fabs(du); \ |
| 839 |
|
| r = fabs(dv); \ |
| 840 |
|
| s = max3(p, q, r); \ |
| 841 |
|
| /* gamma = s*sqrt((theta/s)**2 - (du/s) * (dv/s)) */ \ |
| 842 |
|
| a = theta / s; \ |
| 843 |
|
| gamma = s * sqrt(a * a - ((du) / s) * ((dv) / s)); \ |
| 844 |
|
| if ((v) < (u)) gamma = -gamma; \ |
| 845 |
|
| p = gamma - (du) + theta; \ |
| 846 |
|
| q = gamma - (du) + gamma + (dv); \ |
| 847 |
|
| r = p / q; \ |
| 848 |
|
| (cm) = (u) + r * d; |
| 849 |
|
| |
| 850 |
|
| /** |
| 851 |
|
| * Find a minimizer of an interpolated cubic function. |
| 852 |
|
| * @param cm The minimizer of the interpolated cubic. |
| 853 |
|
| * @param u The value of one point, u. |
| 854 |
|
| * @param fu The value of f(u). |
| 855 |
|
| * @param du The value of f'(u). |
| 856 |
|
| * @param v The value of another point, v. |
| 857 |
|
| * @param fv The value of f(v). |
| 858 |
|
| * @param du The value of f'(v). |
| 859 |
|
| * @param xmin The maximum value. |
| 860 |
|
| * @param xmin The minimum value. |
| 861 |
|
| */ |
| 862 |
|
| #define CUBIC_MINIMIZER2(cm, u, fu, du, v, fv, dv, xmin, xmax) \ |
| 863 |
|
| d = (v) - (u); \ |
| 864 |
|
| theta = ((fu) - (fv)) * 3 / d + (du) + (dv); \ |
| 865 |
|
| p = fabs(theta); \ |
| 866 |
|
| q = fabs(du); \ |
| 867 |
|
| r = fabs(dv); \ |
| 868 |
|
| s = max3(p, q, r); \ |
| 869 |
|
| /* gamma = s*sqrt((theta/s)**2 - (du/s) * (dv/s)) */ \ |
| 870 |
|
| a = theta / s; \ |
| 871 |
|
| gamma = s * sqrt(max2(0, a * a - ((du) / s) * ((dv) / s))); \ |
| 872 |
|
| if ((u) < (v)) gamma = -gamma; \ |
| 873 |
|
| p = gamma - (dv) + theta; \ |
| 874 |
|
| q = gamma - (dv) + gamma + (du); \ |
| 875 |
|
| r = p / q; \ |
| 876 |
|
| if (r < 0. && gamma != 0.) { \ |
| 877 |
|
| (cm) = (v) - r * d; \ |
| 878 |
|
| } else if (a < 0) { \ |
| 879 |
|
| (cm) = (xmax); \ |
| 880 |
|
| } else { \ |
| 881 |
|
| (cm) = (xmin); \ |
| 882 |
|
| } |
| 883 |
|
| |
| 884 |
|
| /** |
| 885 |
|
| * Find a minimizer of an interpolated quadratic function. |
| 886 |
|
| * @param qm The minimizer of the interpolated quadratic. |
| 887 |
|
| * @param u The value of one point, u. |
| 888 |
|
| * @param fu The value of f(u). |
| 889 |
|
| * @param du The value of f'(u). |
| 890 |
|
| * @param v The value of another point, v. |
| 891 |
|
| * @param fv The value of f(v). |
| 892 |
|
| */ |
| 893 |
|
| #define QUARD_MINIMIZER(qm, u, fu, du, v, fv) \ |
| 894 |
|
| a = (v) - (u); \ |
| 895 |
|
| (qm) = (u) + (du) / (((fu) - (fv)) / a + (du)) / 2 * a; |
| 896 |
|
| |
| 897 |
|
| /** |
| 898 |
|
| * Find a minimizer of an interpolated quadratic function. |
| 899 |
|
| * @param qm The minimizer of the interpolated quadratic. |
| 900 |
|
| * @param u The value of one point, u. |
| 901 |
|
| * @param du The value of f'(u). |
| 902 |
|
| * @param v The value of another point, v. |
| 903 |
|
| * @param dv The value of f'(v). |
| 904 |
|
| */ |
| 905 |
|
| #define QUARD_MINIMIZER2(qm, u, du, v, dv) \ |
| 906 |
|
| a = (u) - (v); \ |
| 907 |
|
| (qm) = (v) + (dv) / ((dv) - (du)) * a; |
| 908 |
|
| |
| 909 |
|
| /** |
| 910 |
|
| * Update a safeguarded trial value and interval for line search. |
| 911 |
|
| * |
| 912 |
|
| * The parameter x represents the step with the least function value. |
| 913 |
|
| * The parameter t represents the current step. This function assumes |
| 914 |
|
| * that the derivative at the point of x in the direction of the step. |
| 915 |
|
| * If the bracket is set to true, the minimizer has been bracketed in |
| 916 |
|
| * an interval of uncertainty with endpoints between x and y. |
| 917 |
|
| * |
| 918 |
|
| * @param x The pointer to the value of one endpoint. |
| 919 |
|
| * @param fx The pointer to the value of f(x). |
| 920 |
|
| * @param dx The pointer to the value of f'(x). |
| 921 |
|
| * @param y The pointer to the value of another endpoint. |
| 922 |
|
| * @param fy The pointer to the value of f(y). |
| 923 |
|
| * @param dy The pointer to the value of f'(y). |
| 924 |
|
| * @param t The pointer to the value of the trial value, t. |
| 925 |
|
| * @param ft The pointer to the value of f(t). |
| 926 |
|
| * @param dt The pointer to the value of f'(t). |
| 927 |
|
| * @param tmin The minimum value for the trial value, t. |
| 928 |
|
| * @param tmax The maximum value for the trial value, t. |
| 929 |
|
| * @param brackt The pointer to the predicate if the trial value is |
| 930 |
|
| * bracketed. |
| 931 |
|
| * @retval int Status value. Zero indicates a normal termination. |
| 932 |
|
| * |
| 933 |
|
| * @see |
| 934 |
|
| * Jorge J. More and David J. Thuente. Line search algorithm with |
| 935 |
|
| * guaranteed sufficient decrease. ACM Transactions on Mathematical |
| 936 |
|
| * Software (TOMS), Vol 20, No 3, pp. 286-307, 1994. |
| 937 |
|
| */ |
| 938 |
2 |
| static int update_trial_interval( |
| 939 |
|
| lbfgsfloatval_t *x, |
| 940 |
|
| lbfgsfloatval_t *fx, |
| 941 |
|
| lbfgsfloatval_t *dx, |
| 942 |
|
| lbfgsfloatval_t *y, |
| 943 |
|
| lbfgsfloatval_t *fy, |
| 944 |
|
| lbfgsfloatval_t *dy, |
| 945 |
|
| lbfgsfloatval_t *t, |
| 946 |
|
| lbfgsfloatval_t *ft, |
| 947 |
|
| lbfgsfloatval_t *dt, |
| 948 |
|
| const lbfgsfloatval_t tmin, |
| 949 |
|
| const lbfgsfloatval_t tmax, |
| 950 |
|
| int *brackt |
| 951 |
|
| ) |
| 952 |
|
| { |
| 953 |
|
| int bound; |
| 954 |
2 |
| int dsign = fsigndiff(dt, dx); |
| 955 |
|
| lbfgsfloatval_t mc; /* minimizer of an interpolated cubic. */ |
| 956 |
|
| lbfgsfloatval_t mq; /* minimizer of an interpolated quadratic. */ |
| 957 |
|
| lbfgsfloatval_t newt; /* new trial value. */ |
| 958 |
|
| USES_MINIMIZER; /* for CUBIC_MINIMIZER and QUARD_MINIMIZER. */ |
| 959 |
|
| |
| 960 |
|
| /* Check the input parameters for errors. */ |
| 961 |
2 |
▶ | if (*brackt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*brackt) { |
0 |
2 |
2 |
|
| 962 |
0 |
▶ | if (*t <= min2(*x, *y) || max2(*x, *y) <= *t) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (*t <= min2(*x, *y) || max2(*x, *y) <= *t) { |
0 |
0 |
0 |
| if (*t <= min2(*x, *y) || max2(*x, *y) <= *t) { |
0 |
0 |
0 |
| if (*t <= min2(*x, *y) || max2(*x, *y) <= *t) { |
0 |
0 |
0 |
| if (*t <= min2(*x, *y) || max2(*x, *y) <= *t) { |
0 |
0 |
0 |
|
| 963 |
|
| /* The trival value t is out of the interval. */ |
| 964 |
0 |
| return LBFGSERR_OUTOFINTERVAL; |
| 965 |
|
| } |
| 966 |
0 |
▶ | if (0. <= *dx * (*t - *x)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0. <= *dx * (*t - *x)) { |
0 |
0 |
0 |
|
| 967 |
|
| /* The function must decrease from x. */ |
| 968 |
0 |
| return LBFGSERR_INCREASEGRADIENT; |
| 969 |
|
| } |
| 970 |
0 |
▶ | if (tmax < tmin) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmax < tmin) { |
0 |
0 |
0 |
|
| 971 |
|
| /* Incorrect tmin and tmax specified. */ |
| 972 |
0 |
| return LBFGSERR_INCORRECT_TMINMAX; |
| 973 |
|
| } |
| 974 |
|
| } |
| 975 |
|
| |
| 976 |
|
| /* |
| 977 |
|
| Trial value selection. |
| 978 |
|
| */ |
| 979 |
2 |
▶ | if (*fx < *ft) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*fx < *ft) { |
0 |
2 |
2 |
|
| 980 |
|
| /* |
| 981 |
|
| Case 1: a higher function value. |
| 982 |
|
| The minimum is brackt. If the cubic minimizer is closer |
| 983 |
|
| to x than the quadratic one, the cubic one is taken, else |
| 984 |
|
| the average of the minimizers is taken. |
| 985 |
|
| */ |
| 986 |
0 |
| *brackt = 1; |
| 987 |
0 |
| bound = 1; |
| 988 |
0 |
▶ | CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
Branch: 4 branches
| Branch | True | False | Total |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
|
| 989 |
0 |
| QUARD_MINIMIZER(mq, *x, *fx, *dx, *t, *ft); |
| 990 |
0 |
▶ | if (fabs(mc - *x) < fabs(mq - *x)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(mc - *x) < fabs(mq - *x)) { |
0 |
0 |
0 |
|
| 991 |
0 |
| newt = mc; |
| 992 |
|
| } else { |
| 993 |
0 |
| newt = mc + 0.5 * (mq - mc); |
| 994 |
|
| } |
| 995 |
2 |
▶ | } else if (dsign) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (dsign) { |
0 |
2 |
2 |
|
| 996 |
|
| /* |
| 997 |
|
| Case 2: a lower function value and derivatives of |
| 998 |
|
| opposite sign. The minimum is brackt. If the cubic |
| 999 |
|
| minimizer is closer to x than the quadratic (secant) one, |
| 1000 |
|
| the cubic one is taken, else the quadratic one is taken. |
| 1001 |
|
| */ |
| 1002 |
0 |
| *brackt = 1; |
| 1003 |
0 |
| bound = 0; |
| 1004 |
0 |
▶ | CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
Branch: 4 branches
| Branch | True | False | Total |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(mc, *x, *fx, *dx, *t, *ft, *dt); |
0 |
0 |
0 |
|
| 1005 |
0 |
| QUARD_MINIMIZER2(mq, *x, *dx, *t, *dt); |
| 1006 |
0 |
▶ | if (fabs(mc - *t) > fabs(mq - *t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(mc - *t) > fabs(mq - *t)) { |
0 |
0 |
0 |
|
| 1007 |
0 |
| newt = mc; |
| 1008 |
|
| } else { |
| 1009 |
0 |
| newt = mq; |
| 1010 |
|
| } |
| 1011 |
2 |
▶ | } else if (fabs(*dt) < fabs(*dx)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (fabs(*dt) < fabs(*dx)) { |
2 |
0 |
2 |
|
| 1012 |
|
| /* |
| 1013 |
|
| Case 3: a lower function value, derivatives of the |
| 1014 |
|
| same sign, and the magnitude of the derivative decreases. |
| 1015 |
|
| The cubic minimizer is only used if the cubic tends to |
| 1016 |
|
| infinity in the direction of the minimizer or if the minimum |
| 1017 |
|
| of the cubic is beyond t. Otherwise the cubic minimizer is |
| 1018 |
|
| defined to be either tmin or tmax. The quadratic (secant) |
| 1019 |
|
| minimizer is also computed and if the minimum is brackt |
| 1020 |
|
| then the the minimizer closest to x is taken, else the one |
| 1021 |
|
| farthest away is taken. |
| 1022 |
|
| */ |
| 1023 |
2 |
| bound = 1; |
| 1024 |
2 |
▶ | CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
Branch: 8 branches
| Branch | True | False | Total |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
0 |
2 |
2 |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
2 |
0 |
2 |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
0 |
2 |
2 |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
0 |
2 |
2 |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
2 |
0 |
2 |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
2 |
0 |
2 |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
2 |
0 |
2 |
| CUBIC_MINIMIZER2(mc, *x, *fx, *dx, *t, *ft, *dt, tmin, tmax); |
0 |
0 |
0 |
|
| 1025 |
2 |
| QUARD_MINIMIZER2(mq, *x, *dx, *t, *dt); |
| 1026 |
2 |
▶ | if (*brackt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*brackt) { |
0 |
2 |
2 |
|
| 1027 |
0 |
▶ | if (fabs(*t - mc) < fabs(*t - mq)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(*t - mc) < fabs(*t - mq)) { |
0 |
0 |
0 |
|
| 1028 |
0 |
| newt = mc; |
| 1029 |
|
| } else { |
| 1030 |
0 |
| newt = mq; |
| 1031 |
|
| } |
| 1032 |
|
| } else { |
| 1033 |
2 |
▶ | if (fabs(*t - mc) > fabs(*t - mq)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(*t - mc) > fabs(*t - mq)) { |
1 |
1 |
2 |
|
| 1034 |
1 |
| newt = mc; |
| 1035 |
|
| } else { |
| 1036 |
1 |
| newt = mq; |
| 1037 |
|
| } |
| 1038 |
|
| } |
| 1039 |
|
| } else { |
| 1040 |
|
| /* |
| 1041 |
|
| Case 4: a lower function value, derivatives of the |
| 1042 |
|
| same sign, and the magnitude of the derivative does |
| 1043 |
|
| not decrease. If the minimum is not brackt, the step |
| 1044 |
|
| is either tmin or tmax, else the cubic minimizer is taken. |
| 1045 |
|
| */ |
| 1046 |
0 |
| bound = 0; |
| 1047 |
0 |
▶ | if (*brackt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*brackt) { |
0 |
0 |
0 |
|
| 1048 |
0 |
▶ | CUBIC_MINIMIZER(newt, *t, *ft, *dt, *y, *fy, *dy); |
Branch: 4 branches
| Branch | True | False | Total |
| CUBIC_MINIMIZER(newt, *t, *ft, *dt, *y, *fy, *dy); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(newt, *t, *ft, *dt, *y, *fy, *dy); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(newt, *t, *ft, *dt, *y, *fy, *dy); |
0 |
0 |
0 |
| CUBIC_MINIMIZER(newt, *t, *ft, *dt, *y, *fy, *dy); |
0 |
0 |
0 |
|
| 1049 |
0 |
▶ | } else if (*x < *t) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*x < *t) { |
0 |
0 |
0 |
|
| 1050 |
0 |
| newt = tmax; |
| 1051 |
|
| } else { |
| 1052 |
0 |
| newt = tmin; |
| 1053 |
|
| } |
| 1054 |
|
| } |
| 1055 |
|
| |
| 1056 |
|
| /* |
| 1057 |
|
| Update the interval of uncertainty. This update does not |
| 1058 |
|
| depend on the new step or the case analysis above. |
| 1059 |
|
| |
| 1060 |
|
| - Case a: if f(x) < f(t), |
| 1061 |
|
| x <- x, y <- t. |
| 1062 |
|
| - Case b: if f(t) <= f(x) && f'(t)*f'(x) > 0, |
| 1063 |
|
| x <- t, y <- y. |
| 1064 |
|
| - Case c: if f(t) <= f(x) && f'(t)*f'(x) < 0, |
| 1065 |
|
| x <- t, y <- x. |
| 1066 |
|
| */ |
| 1067 |
2 |
▶ | if (*fx < *ft) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*fx < *ft) { |
0 |
2 |
2 |
|
| 1068 |
|
| /* Case a */ |
| 1069 |
0 |
| *y = *t; |
| 1070 |
0 |
| *fy = *ft; |
| 1071 |
0 |
| *dy = *dt; |
| 1072 |
|
| } else { |
| 1073 |
|
| /* Case c */ |
| 1074 |
2 |
▶ | if (dsign) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dsign) { |
0 |
2 |
2 |
|
| 1075 |
0 |
| *y = *x; |
| 1076 |
0 |
| *fy = *fx; |
| 1077 |
0 |
| *dy = *dx; |
| 1078 |
|
| } |
| 1079 |
|
| /* Cases b and c */ |
| 1080 |
2 |
| *x = *t; |
| 1081 |
2 |
| *fx = *ft; |
| 1082 |
2 |
| *dx = *dt; |
| 1083 |
|
| } |
| 1084 |
|
| |
| 1085 |
|
| /* Clip the new trial value in [tmin, tmax]. */ |
| 1086 |
2 |
▶ | if (tmax < newt) newt = tmax; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmax < newt) newt = tmax; |
2 |
0 |
2 |
|
| 1087 |
2 |
▶ | if (newt < tmin) newt = tmin; |
Branch: 1 branch
| Branch | True | False | Total |
| if (newt < tmin) newt = tmin; |
0 |
2 |
2 |
|
| 1088 |
|
| |
| 1089 |
|
| /* |
| 1090 |
|
| Redefine the new trial value if it is close to the upper bound |
| 1091 |
|
| of the interval. |
| 1092 |
|
| */ |
| 1093 |
2 |
▶ | if (*brackt && bound) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*brackt && bound) { |
0 |
2 |
2 |
| if (*brackt && bound) { |
0 |
0 |
0 |
|
| 1094 |
0 |
| mq = *x + 0.66 * (*y - *x); |
| 1095 |
0 |
▶ | if (*x < *y) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*x < *y) { |
0 |
0 |
0 |
|
| 1096 |
0 |
▶ | if (mq < newt) newt = mq; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mq < newt) newt = mq; |
0 |
0 |
0 |
|
| 1097 |
|
| } else { |
| 1098 |
0 |
▶ | if (newt < mq) newt = mq; |
Branch: 1 branch
| Branch | True | False | Total |
| if (newt < mq) newt = mq; |
0 |
0 |
0 |
|
| 1099 |
|
| } |
| 1100 |
|
| } |
| 1101 |
|
| |
| 1102 |
|
| /* Return the new trial value. */ |
| 1103 |
2 |
| *t = newt; |
| 1104 |
2 |
| return 0; |
| 1105 |
|
| } |