| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <esl_config.h> |
| 16 |
|
| 17 |
#include <math.h> |
| 18 |
#include <float.h> |
| 19 |
|
| 20 |
#include "easel.h" |
| 21 |
#include "esl_vectorops.h" |
| 22 |
|
| 23 |
#include "esl_minimizer.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
static void numeric_derivative(ESL_MIN_CFG *cfg, double *x, int n, |
| 28 |
double (*func)(double *, int, void*), |
| 29 |
void *prm, double *dx, ESL_MIN_DAT *dat); |
| 30 |
static int bracket(ESL_MIN_CFG *cfg, double *ori, double *d, int n, double firststep, |
| 31 |
double (*func)(double *, int, void *), void *prm, double *wrk, |
| 32 |
double *ret_ax, double *ret_bx, double *ret_cx, |
| 33 |
double *ret_fa, double *ret_fb, double *ret_fc, ESL_MIN_DAT *dat); |
| 34 |
static void brent(ESL_MIN_CFG *cfg, double *ori, double *dir, int n, |
| 35 |
double (*func)(double *, int, void *), void *prm, double a, double b, |
| 36 |
double *xvec, double *opt_x, double *opt_fx, ESL_MIN_DAT *dat); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
int |
| 103 |
esl_min_ConjugateGradientDescent(ESL_MIN_CFG *cfg, double *x, int n, |
| 104 |
double (*func)(double *, int, void *), |
| 105 |
void (*dfunc)(double *, int, void *, double *), |
| 106 |
void *prm, double *opt_fx, ESL_MIN_DAT *dat) |
| 107 |
{ |
| 108 |
int max_iterations = cfg ? cfg->max_iterations : eslMIN_MAXITER; |
| 109 |
double cg_rtol = cfg ? cfg->cg_rtol : eslMIN_CG_RTOL; |
| 110 |
double cg_atol = cfg ? cfg->cg_atol : eslMIN_CG_ATOL; |
| 111 |
double *u = cfg ? cfg->u : NULL; |
| 112 |
double *wrk = NULL; |
| 113 |
double oldfx; |
| 114 |
double coeff; |
| 115 |
int i, i1; |
| 116 |
double *dx, *cg, *w1, *w2; |
| 117 |
double fa,fb,fc; |
| 118 |
double ax,bx,cx; |
| 119 |
double fx; |
| 120 |
int status; |
| 121 |
|
| 122 |
ESL_ALLOC(wrk, sizeof(double) * 4 * n); |
| 123 |
dx = wrk; |
| 124 |
cg = wrk + n; |
| 125 |
w1 = wrk + 2*n; |
| 126 |
w2 = wrk + 3*n; |
| 127 |
|
| 128 |
oldfx = (*func)(x, n, prm); |
| 129 |
if (dat) { dat->fx[0] = oldfx; dat->nfunc[0] = 1; dat->niter = 0; } |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
if (! isfinite(oldfx)) ESL_XEXCEPTION(eslERANGE, "minimum not finite"); |
| 135 |
|
| 136 |
if (dfunc) |
| 137 |
{ |
| 138 |
(*dfunc)(x, n, prm, dx); |
| 139 |
esl_vec_DScale(dx, n, -1.0); |
| 140 |
} |
| 141 |
else numeric_derivative(cfg, x, n, func, prm, dx, dat); |
| 142 |
esl_vec_DCopy(dx, n, cg); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
for (i1 = 0; i1 < n; i1++) |
| 149 |
if (cg[i1] != 0.) break; |
| 150 |
if (i1 == n) { |
| 151 |
if (opt_fx) *opt_fx = oldfx; |
| 152 |
free(wrk); |
| 153 |
return eslOK; |
| 154 |
} |
| 155 |
|
| 156 |
for (i = 1; i <= max_iterations; i++) |
| 157 |
{ |
| 158 |
if (dat) { |
| 159 |
dat->niter = i; |
| 160 |
dat->nfunc[i] = 0; |
| 161 |
} |
| 162 |
|
| 163 |
#if (eslDEBUGLEVEL >= 2) |
| 164 |
int j; |
| 165 |
printf("\nCG iteration %d\n", i+1); |
| 166 |
printf(" current point: "); |
| 167 |
for (j = 0; j < n; j++) printf("%10.4g ", x[j]); |
| 168 |
printf("\n gradient: "); |
| 169 |
for (j = 0; j < n; j++) printf("%10.4g ", dx[j]); |
| 170 |
numeric_derivative(cfg, x, n, func, prm, w1, dat); |
| 171 |
printf("\n numeric gradient: "); |
| 172 |
for (j = 0; j < n; j++) printf("%10.4g ", w1[j]); |
| 173 |
printf("\n conjugate direction: "); |
| 174 |
for (j = 0; j < n; j++) printf("%10.4g ", cg[j]); |
| 175 |
printf("\n"); |
| 176 |
#endif |
| 177 |
|
| 178 |
|
| 179 |
if (u) bx = fabs(u[0] / cg[0]); |
| 180 |
else bx = fabs(1. / cg[0]); |
| 181 |
for (i1 = 1; i1 < n; i1++) |
| 182 |
{ |
| 183 |
if (u) cx = fabs(u[i1] / cg[i1]); |
| 184 |
else cx = fabs(1. / cg[i1]); |
| 185 |
if (cx < bx) bx = cx; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
bracket(cfg, x, cg, n, bx, func, prm, w1, &ax, &bx, &cx, &fa, &fb, &fc, dat); |
| 190 |
|
| 191 |
|
| 192 |
brent(cfg, x, cg, n, func, prm, ax, cx, w2, NULL, &fx, dat); |
| 193 |
esl_vec_DCopy(w2, n, x); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if (! isfinite(fx)) ESL_XEXCEPTION(eslERANGE, "minimum not finite"); |
| 199 |
|
| 200 |
|
| 201 |
if (dfunc != NULL) |
| 202 |
{ |
| 203 |
(*dfunc)(x, n, prm, w1); |
| 204 |
esl_vec_DScale(w1, n, -1.0); |
| 205 |
} |
| 206 |
else numeric_derivative(cfg, x, n, func, prm, w1, dat); |
| 207 |
|
| 208 |
|
| 209 |
for (coeff = 0., i1 = 0; i1 < n; i1++) |
| 210 |
coeff += (w1[i1] - dx[i1]) * w1[i1]; |
| 211 |
coeff /= esl_vec_DDot(dx, dx, n); |
| 212 |
|
| 213 |
|
| 214 |
esl_vec_DCopy(w1, n, w2); |
| 215 |
esl_vec_DAddScaled(w2, cg, coeff, n); |
| 216 |
|
| 217 |
|
| 218 |
esl_vec_DCopy(w1, n, dx); |
| 219 |
esl_vec_DCopy(w2, n, cg); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
if (dat) |
| 227 |
dat->fx[i] = fx; |
| 228 |
|
| 229 |
|
| 230 |
if (esl_DCompare(fx, oldfx, cg_rtol, cg_atol) == eslOK) break; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
for (i1 = 0; i1 < n; i1++) |
| 236 |
if (cg[i1] != 0.) break; |
| 237 |
if (i1 == n) break; |
| 238 |
|
| 239 |
oldfx = fx; |
| 240 |
} |
| 241 |
|
| 242 |
free(wrk); |
| 243 |
if (opt_fx) *opt_fx = fx; |
| 244 |
return (i > max_iterations ? eslENOHALT: eslOK); |
| 245 |
|
| 246 |
ERROR: |
| 247 |
free(wrk); |
| 248 |
if (opt_fx) *opt_fx = eslINFINITY; |
| 249 |
return status; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
ESL_MIN_CFG * |
| 266 |
esl_min_cfg_Create(int n) |
| 267 |
{ |
| 268 |
ESL_MIN_CFG *cfg = NULL; |
| 269 |
int status; |
| 270 |
|
| 271 |
ESL_ALLOC(cfg, sizeof(ESL_MIN_CFG)); |
| 272 |
cfg->u = NULL; |
| 273 |
ESL_ALLOC(cfg->u, sizeof(double) * n); |
| 274 |
|
| 275 |
cfg->max_iterations = eslMIN_MAXITER; |
| 276 |
cfg->cg_rtol = eslMIN_CG_RTOL; |
| 277 |
cfg->cg_atol = eslMIN_CG_ATOL; |
| 278 |
cfg->brent_rtol = eslMIN_BRENT_RTOL; |
| 279 |
cfg->brent_atol = eslMIN_BRENT_ATOL; |
| 280 |
cfg->brack_maxiter = eslMIN_BRACK_MAXITER; |
| 281 |
cfg->deriv_step = eslMIN_DERIV_STEP; |
| 282 |
cfg->n = n; |
| 283 |
esl_vec_DSet(cfg->u, n, eslMIN_BRACK_STEP); |
| 284 |
return cfg; |
| 285 |
|
| 286 |
ERROR: |
| 287 |
esl_min_cfg_Destroy(cfg); |
| 288 |
return NULL; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
void |
| 295 |
esl_min_cfg_Destroy(ESL_MIN_CFG *cfg) |
| 296 |
{ |
| 297 |
if (cfg) |
| 298 |
{ |
| 299 |
free(cfg->u); |
| 300 |
free(cfg); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
ESL_MIN_DAT * |
| 318 |
esl_min_dat_Create(ESL_MIN_CFG *cfg) |
| 319 |
{ |
| 320 |
int max_iterations = cfg ? cfg->max_iterations : eslMIN_MAXITER; |
| 321 |
ESL_MIN_DAT *dat = NULL; |
| 322 |
int status; |
| 323 |
|
| 324 |
ESL_ALLOC(dat, sizeof(ESL_MIN_DAT)); |
| 325 |
dat->fx = NULL; |
| 326 |
dat->brack_n = NULL; |
| 327 |
dat->brack_ax = dat->brack_bx = dat->brack_cx = NULL; |
| 328 |
dat->brack_fa = dat->brack_fb = dat->brack_fc = NULL; |
| 329 |
dat->brent_n = NULL; |
| 330 |
dat->brent_x = NULL; |
| 331 |
dat->nfunc = NULL; |
| 332 |
|
| 333 |
ESL_ALLOC(dat->fx, sizeof(double) * (max_iterations + 1)); |
| 334 |
ESL_ALLOC(dat->brack_n, sizeof(int) * (max_iterations + 1)); |
| 335 |
ESL_ALLOC(dat->brack_ax, sizeof(double) * (max_iterations + 1)); |
| 336 |
ESL_ALLOC(dat->brack_bx, sizeof(double) * (max_iterations + 1)); |
| 337 |
ESL_ALLOC(dat->brack_cx, sizeof(double) * (max_iterations + 1)); |
| 338 |
ESL_ALLOC(dat->brack_fa, sizeof(double) * (max_iterations + 1)); |
| 339 |
ESL_ALLOC(dat->brack_fb, sizeof(double) * (max_iterations + 1)); |
| 340 |
ESL_ALLOC(dat->brack_fc, sizeof(double) * (max_iterations + 1)); |
| 341 |
ESL_ALLOC(dat->brent_n, sizeof(int) * (max_iterations + 1)); |
| 342 |
ESL_ALLOC(dat->brent_x, sizeof(double) * (max_iterations + 1)); |
| 343 |
ESL_ALLOC(dat->nfunc, sizeof(int) * (max_iterations + 1)); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
dat->brack_n[0] = 0; |
| 349 |
dat->brack_ax[0] = dat->brack_bx[0] = dat->brack_cx[0] = 0.; |
| 350 |
dat->brack_fa[0] = dat->brack_fb[0] = dat->brack_fc[0] = 0.; |
| 351 |
dat->brent_n[0] = 0; |
| 352 |
dat->brent_x[0] = 0.; |
| 353 |
|
| 354 |
return dat; |
| 355 |
|
| 356 |
ERROR: |
| 357 |
esl_min_dat_Destroy(dat); |
| 358 |
return NULL; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
void |
| 367 |
esl_min_dat_Destroy(ESL_MIN_DAT *dat) |
| 368 |
{ |
| 369 |
if (dat) |
| 370 |
{ |
| 371 |
free(dat->fx); |
| 372 |
free(dat->brack_n); |
| 373 |
free(dat->brack_ax); |
| 374 |
free(dat->brack_bx); |
| 375 |
free(dat->brack_cx); |
| 376 |
free(dat->brack_fa); |
| 377 |
free(dat->brack_fb); |
| 378 |
free(dat->brack_fc); |
| 379 |
free(dat->brent_n); |
| 380 |
free(dat->brent_x); |
| 381 |
free(dat->nfunc); |
| 382 |
free(dat); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
int |
| 392 |
esl_min_dat_Dump(FILE *fp, ESL_MIN_DAT *dat) |
| 393 |
{ |
| 394 |
int iter; |
| 395 |
|
| 396 |
esl_dataheader(fp, 6, "iter", 16, "fx", 16, "diff", |
| 397 |
7, "brack_n", 16, "brack_ax", 16, "brack_bx", 16, "brack_cx", |
| 398 |
16,"brack_fa", 16, "brack_fb", 16, "brack_fc", |
| 399 |
7, "brent_n", 16, "brent_x", 5, "nfunc", |
| 400 |
0); |
| 401 |
|
| 402 |
for (iter = 0; iter <= dat->niter; iter++) |
| 403 |
{ |
| 404 |
fprintf(fp, "%6d %16g %16g %7d %16g %16g %16g %16g %16g %16g %7d %16g %5d\n", |
| 405 |
iter, |
| 406 |
dat->fx[iter], |
| 407 |
iter > 0 ? dat->fx[iter-1] - dat->fx[iter] : 0., |
| 408 |
dat->brack_n[iter], |
| 409 |
dat->brack_ax[iter], dat->brack_bx[iter], dat->brack_cx[iter], |
| 410 |
dat->brack_fa[iter], dat->brack_fb[iter], dat->brack_fc[iter], |
| 411 |
dat->brent_n[iter], dat->brent_x[iter], dat->nfunc[iter]); |
| 412 |
} |
| 413 |
return eslOK; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
static void |
| 425 |
numeric_derivative(ESL_MIN_CFG *cfg, double *x, int n, |
| 426 |
double (*func)(double *, int, void*), |
| 427 |
void *prm, double *dx, ESL_MIN_DAT *dat) |
| 428 |
{ |
| 429 |
double relstep = cfg ? cfg->deriv_step : eslMIN_DERIV_STEP; |
| 430 |
double *u = cfg ? cfg->u : NULL; |
| 431 |
int i; |
| 432 |
double delta; |
| 433 |
double f1, f2; |
| 434 |
double tmp; |
| 435 |
|
| 436 |
for (i = 0; i < n; i++) |
| 437 |
{ |
| 438 |
if (u) delta = fabs(u[i] * relstep); |
| 439 |
else delta = fabs(relstep); |
| 440 |
|
| 441 |
tmp = x[i]; |
| 442 |
x[i] = tmp + delta; |
| 443 |
f1 = (*func)(x, n, prm); |
| 444 |
x[i] = tmp - delta; |
| 445 |
f2 = (*func)(x, n, prm); |
| 446 |
x[i] = tmp; |
| 447 |
|
| 448 |
dx[i] = (-0.5 * (f1-f2)) / delta; |
| 449 |
|
| 450 |
if (dat) dat->nfunc[dat->niter] += 2; |
| 451 |
ESL_DASSERT1((! isnan(dx[i]))); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
static int |
| 520 |
bracket(ESL_MIN_CFG *cfg, double *ori, double *d, int n, double firststep, |
| 521 |
double (*func)(double *, int, void *), void *prm, |
| 522 |
double *wrk, |
| 523 |
double *ret_ax, double *ret_bx, double *ret_cx, |
| 524 |
double *ret_fa, double *ret_fb, double *ret_fc, ESL_MIN_DAT *dat) |
| 525 |
{ |
| 526 |
int max_iterations = cfg ? cfg->brack_maxiter : eslMIN_BRACK_MAXITER; |
| 527 |
double ax,bx,cx; |
| 528 |
double fa,fb,fc; |
| 529 |
double swapper; |
| 530 |
int niter; |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
ax = 0.; |
| 536 |
fa = (*func)(ori, n, prm); |
| 537 |
|
| 538 |
bx = firststep; |
| 539 |
esl_vec_DCopy(ori, n, wrk); |
| 540 |
esl_vec_DAddScaled(wrk, d, bx, n); |
| 541 |
fb = (*func)(wrk, n, prm); |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
if (fb > fa) |
| 557 |
{ |
| 558 |
swapper = ax; ax = bx; bx = swapper; |
| 559 |
swapper = fa; fa = fb; fb = swapper; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
cx = bx + (bx-ax)*1.618; |
| 569 |
esl_vec_DCopy(ori, n, wrk); |
| 570 |
esl_vec_DAddScaled(wrk, d, cx, n); |
| 571 |
fc = (*func)(wrk, n, prm); |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
niter = 0; |
| 578 |
while (fc <= fb) |
| 579 |
{ |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
ax = bx; bx = cx; |
| 584 |
fa = fb; fb = fc; |
| 585 |
cx = bx+(bx-ax)*1.618; |
| 586 |
esl_vec_DCopy(ori, n, wrk); |
| 587 |
esl_vec_DAddScaled(wrk, d, cx, n); |
| 588 |
fc = (*func)(wrk, n, prm); |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
if (ax != bx && bx != cx && fa == fb && fb == fc) break; |
| 595 |
|
| 596 |
niter++; |
| 597 |
if (niter > max_iterations) ESL_EXCEPTION(eslENORESULT, "Failed to bracket a minimum."); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
if (ax > cx) |
| 604 |
{ |
| 605 |
swapper = ax; ax = cx; cx = swapper; |
| 606 |
swapper = fa; fa = fc; fc = swapper; |
| 607 |
} |
| 608 |
|
| 609 |
if (dat) |
| 610 |
{ |
| 611 |
dat->brack_n[dat->niter] = niter; |
| 612 |
dat->nfunc[dat->niter] += niter + 3; |
| 613 |
dat->brack_ax[dat->niter] = ax; |
| 614 |
dat->brack_bx[dat->niter] = bx; |
| 615 |
dat->brack_cx[dat->niter] = cx; |
| 616 |
dat->brack_fa[dat->niter] = fa; |
| 617 |
dat->brack_fb[dat->niter] = fb; |
| 618 |
dat->brack_fc[dat->niter] = fc; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
ESL_DPRINTF2(("bracket(), in %2d iterations, brackets a minimum: a,b,c = %10.4g %10.4g %10.4g\n", niter, ax, bx, cx)); |
| 623 |
ESL_DPRINTF2(("bracket(): fa,fb,fc = %10.4g %10.4g %10.4g\n", fa, fb, fc)); |
| 624 |
|
| 625 |
|
| 626 |
*ret_ax = ax; *ret_bx = bx; *ret_cx = cx; |
| 627 |
*ret_fa = fa; *ret_fb = fb; *ret_fc = fc; |
| 628 |
return eslOK; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
static void |
| 695 |
brent(ESL_MIN_CFG *cfg, double *ori, double *dir, int n, |
| 696 |
double (*func)(double *, int, void *), void *prm, double a, double b, |
| 697 |
double *xvec, double *opt_x, double *opt_fx, ESL_MIN_DAT *dat) |
| 698 |
{ |
| 699 |
double eps = cfg ? cfg->brent_rtol : eslMIN_BRENT_RTOL; |
| 700 |
double t = cfg ? cfg->brent_atol : eslMIN_BRENT_ATOL; |
| 701 |
double tol; |
| 702 |
double w,x,v,u; |
| 703 |
double m; |
| 704 |
double fu,fv,fw,fx; |
| 705 |
double p,q; |
| 706 |
double r; |
| 707 |
double d,e; |
| 708 |
double c = 1. - (1./eslCONST_GOLD); |
| 709 |
int niter; |
| 710 |
|
| 711 |
x=v=w= a + c*(b-a); |
| 712 |
esl_vec_DCopy(ori, n, xvec); |
| 713 |
esl_vec_DAddScaled(xvec, dir, x, n); |
| 714 |
fx=fv=fw = (*func)(xvec, n, prm); |
| 715 |
|
| 716 |
d = e = 0.; |
| 717 |
niter = 0; |
| 718 |
while (1) |
| 719 |
{ |
| 720 |
m = 0.5 * (a+b); |
| 721 |
tol = eps*fabs(x) + t; |
| 722 |
if (fabs(x-m) <= 2*tol - 0.5*(b-a)) break; |
| 723 |
niter++; |
| 724 |
|
| 725 |
p = q = r = 0.; |
| 726 |
if (fabs(e) > tol) |
| 727 |
{ |
| 728 |
r = (x-w)*(fx-fv); |
| 729 |
q = (x-v)*(fx-fw); |
| 730 |
p = (x-v)*q - (x-w)*r; |
| 731 |
q = 2*(q-r); |
| 732 |
if (q > 0) { p = -p; } else {q = -q;} |
| 733 |
r = e; |
| 734 |
e=d; |
| 735 |
} |
| 736 |
|
| 737 |
if (fabs(p) < fabs(0.5*q*r) || p < q*(a-x) || p < q*(b-x)) |
| 738 |
{ |
| 739 |
d = p/q; |
| 740 |
u = x+d; |
| 741 |
|
| 742 |
if (2.0*(u-a) < tol || 2.0*(b-u) < tol) |
| 743 |
d = (x < m)? tol : -tol; |
| 744 |
} |
| 745 |
else |
| 746 |
{ |
| 747 |
e = (x<m)? b-x : a-x; |
| 748 |
d = c*e; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
if (fabs(d) >= tol) u = x+d; |
| 753 |
else if (d > 0) u = x+tol; |
| 754 |
else u = x-tol; |
| 755 |
esl_vec_DCopy(ori, n, xvec); |
| 756 |
esl_vec_DAddScaled(xvec, dir, u, n); |
| 757 |
fu = (*func)(xvec, n, prm); |
| 758 |
|
| 759 |
|
| 760 |
if (fu <= fx) |
| 761 |
{ |
| 762 |
if (u < x) b = x; else a = x; |
| 763 |
v = w; fv = fw; w = x; fw = fx; x = u; fx = fu; |
| 764 |
} |
| 765 |
else |
| 766 |
{ |
| 767 |
if (u < x) a = u; else b = u; |
| 768 |
if (fu <= fw || w == x) |
| 769 |
{ v = w; fv = fw; w = u; fw = fu; } |
| 770 |
else if (fu <= fv || v==x || v ==w) |
| 771 |
{ v = u; fv = fu; } |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
if (dat) |
| 776 |
{ |
| 777 |
dat->brent_n[dat->niter] = niter; |
| 778 |
dat->brent_x[dat->niter] = x; |
| 779 |
dat->nfunc [dat->niter] += niter + 1; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
ESL_DPRINTF2((" brent(), in %d iterations, finds 1D minimum at %10.4g, f() = %10.4g\n", niter, x, fx)); |
| 784 |
|
| 785 |
|
| 786 |
esl_vec_DCopy(ori, n, xvec); |
| 787 |
esl_vec_DAddScaled(xvec, dir, x, n); |
| 788 |
if (opt_x) *opt_x = x; |
| 789 |
if (opt_fx) *opt_fx = fx; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
#ifdef eslMINIMIZER_TESTDRIVE |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
static double |
| 801 |
test_func(double *x, int n, void *prm) |
| 802 |
{ |
| 803 |
double *a = (double *) prm; |
| 804 |
double *b = ((double *) prm) + n; |
| 805 |
double fx = 0.; |
| 806 |
int i; |
| 807 |
for (i = 0; i < n; i++) fx += a[i] * (x[i] - b[i]) * (x[i] - b[i]); |
| 808 |
return fx; |
| 809 |
} |
| 810 |
|
| 811 |
static void |
| 812 |
test_dfunc(double *x, int n, void *prm, double *dx) |
| 813 |
{ |
| 814 |
double *a = (double *) prm; |
| 815 |
double *b = ((double *) prm) + n; |
| 816 |
int i; |
| 817 |
for (i = 0; i < n; i++) |
| 818 |
dx[i] = 2. * a[i] * (x[i] - b[i]); |
| 819 |
} |
| 820 |
|
| 821 |
static void |
| 822 |
utest_simplefunc(ESL_RANDOMNESS *rng) |
| 823 |
{ |
| 824 |
char msg[] = "esl_minimizer simplefunc test failed"; |
| 825 |
int n = 1 + esl_rnd_Roll(rng, 10); |
| 826 |
double *prm = malloc(sizeof(double) * 2 * n); |
| 827 |
double *a = prm; |
| 828 |
double *b = prm+n; |
| 829 |
double *x = malloc(sizeof(double) * n); |
| 830 |
double fx; |
| 831 |
int i; |
| 832 |
|
| 833 |
|
| 834 |
for (i = 0; i < n; i++) |
| 835 |
{ |
| 836 |
a[i] = esl_random(rng) * 10.; |
| 837 |
b[i] = esl_random(rng) * 20. - 10.; |
| 838 |
x[i] = esl_random(rng) * 20. - 10.; |
| 839 |
} |
| 840 |
|
| 841 |
esl_min_ConjugateGradientDescent(NULL, x, n, &test_func, &test_dfunc, (void *) prm, &fx, NULL); |
| 842 |
|
| 843 |
for (i = 0; i < n; i++) |
| 844 |
if ( esl_DCompare( b[i], x[i], 1e-5, 1e-10) != eslOK) esl_fatal(msg); |
| 845 |
if (esl_DCompare(0., fx, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 846 |
|
| 847 |
free(prm); |
| 848 |
free(x); |
| 849 |
} |
| 850 |
|
| 851 |
#endif |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
#ifdef eslMINIMIZER_TESTDRIVE |
| 858 |
#include <esl_config.h> |
| 859 |
|
| 860 |
#include "easel.h" |
| 861 |
#include "esl_getopts.h" |
| 862 |
#include "esl_minimizer.h" |
| 863 |
#include "esl_random.h" |
| 864 |
|
| 865 |
static ESL_OPTIONS options[] = { |
| 866 |
|
| 867 |
{ "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help summary", 0 }, |
| 868 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number generator seed", 0 }, |
| 869 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 870 |
}; |
| 871 |
|
| 872 |
int |
| 873 |
main(int argc, char **argv) |
| 874 |
{ |
| 875 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, |
| 876 |
"test driver for minimizer", |
| 877 |
"[-options]"); |
| 878 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 879 |
|
| 880 |
esl_fprintf(stderr, "## %s\n", argv[0]); |
| 881 |
esl_fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 882 |
|
| 883 |
utest_simplefunc(rng); |
| 884 |
|
| 885 |
esl_fprintf(stderr, "# status = ok\n"); |
| 886 |
esl_getopts_Destroy(go); |
| 887 |
esl_randomness_Destroy(rng); |
| 888 |
return 0; |
| 889 |
} |
| 890 |
#endif |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
#ifdef eslMINIMIZER_EXAMPLE |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
#include <stdio.h> |
| 902 |
|
| 903 |
#include "easel.h" |
| 904 |
#include "esl_vectorops.h" |
| 905 |
#include "esl_minimizer.h" |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
static double |
| 911 |
example_func(double *x, int n, void *prm) |
| 912 |
{ |
| 913 |
double *a; |
| 914 |
double fx; |
| 915 |
int i; |
| 916 |
|
| 917 |
a = (double *) prm; |
| 918 |
for (fx = 0., i = 0; i < n; i++) |
| 919 |
fx += a[i] * x[i] * x[i]; |
| 920 |
return fx; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
static void |
| 925 |
example_dfunc(double *x, int n, void *prm, double *dx) |
| 926 |
{ |
| 927 |
double *a; |
| 928 |
int i; |
| 929 |
|
| 930 |
a = (double *) prm; |
| 931 |
for (i = 0; i < n; i++) |
| 932 |
dx[i] = 2.0 * a[i] * x[i]; |
| 933 |
} |
| 934 |
int |
| 935 |
main(int argc, char **argv) |
| 936 |
{ |
| 937 |
ESL_MIN_DAT *dat = esl_min_dat_Create(NULL); |
| 938 |
int n = 6; |
| 939 |
double a[6] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; |
| 940 |
double x[6] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; |
| 941 |
double fx; |
| 942 |
int i; |
| 943 |
|
| 944 |
esl_min_ConjugateGradientDescent(NULL, x, n, |
| 945 |
&example_func, &example_dfunc, (void *) a, |
| 946 |
&fx, dat); |
| 947 |
|
| 948 |
printf("At minimum: f(x) = %g\n", fx); |
| 949 |
printf("vector x = "); |
| 950 |
for (i = 0; i < 6; i++) printf("%g ", x[i]); |
| 951 |
printf("\n"); |
| 952 |
|
| 953 |
esl_min_dat_Dump(stdout, dat); |
| 954 |
|
| 955 |
esl_min_dat_Destroy(dat); |
| 956 |
return 0; |
| 957 |
} |
| 958 |
|
| 959 |
#endif |