Actual source code: morethuente.c
1: /*$Id$*/
3: #include "morethuente.h" /*I "tao_solver.h" I*/
4: #include <stdlib.h>
6: /*
7: The subroutine mcstep is taken from the work of Jorge Nocedal.
8: this is a variant of More' and Thuente's routine.
10: subroutine mcstep
12: the purpose of mcstep is to compute a safeguarded step for
13: a linesearch and to update an interval of uncertainty for
14: a minimizer of the function.
16: the parameter stx contains the step with the least function
17: value. the parameter stp contains the current step. it is
18: assumed that the derivative at stx is negative in the
19: direction of the step. if bracket is set true then a
20: minimizer has been bracketed in an interval of uncertainty
21: with endpoints stx and sty.
23: the subroutine statement is
25: subroutine mcstep(stx,fx,dx,sty,fy,dy,stp,fp,dp,bracket,
26: stpmin,stpmax,info)
28: where
30: stx, fx, and dx are variables which specify the step,
31: the function, and the derivative at the best step obtained
32: so far. The derivative must be negative in the direction
33: of the step, that is, dx and stp-stx must have opposite
34: signs. On output these parameters are updated appropriately.
36: sty, fy, and dy are variables which specify the step,
37: the function, and the derivative at the other endpoint of
38: the interval of uncertainty. On output these parameters are
39: updated appropriately.
41: stp, fp, and dp are variables which specify the step,
42: the function, and the derivative at the current step.
43: If bracket is set true then on input stp must be
44: between stx and sty. On output stp is set to the new step.
46: bracket is a logical variable which specifies if a minimizer
47: has been bracketed. If the minimizer has not been bracketed
48: then on input bracket must be set false. If the minimizer
49: is bracketed then on output bracket is set true.
51: stpmin and stpmax are input variables which specify lower
52: and upper bounds for the step.
54: info is an integer output variable set as follows:
55: if info = 1,2,3,4,5, then the step has been computed
56: according to one of the five cases below. otherwise
57: info = 0, and this indicates improper input parameters.
59: subprograms called
61: fortran-supplied ... abs,max,min,sqrt
63: argonne national laboratory. minpack project. june 1983
64: jorge j. more', david j. thuente
66: */
70: static int TaoStep_LineSearch(TAO_SOLVER tao,
71: double *stx, double *fx, double *dx,
72: double *sty, double *fy, double *dy,
73: double *stp, double *fp, double *dp)
74: {
75: TAO_LINESEARCH *neP = (TAO_LINESEARCH *) tao->linectx;
76: double gamma1, p, q, r, s, sgnd, stpc, stpf, stpq, theta;
77: TaoInt bound;
79: TaoFunctionBegin;
81: // Check the input parameters for errors
82: neP->infoc = 0;
83: if (neP->bracket && (*stp <= TaoMin(*stx,*sty) || (*stp >= TaoMax(*stx,*sty)))) SETERRQ(1,"bad stp in bracket");
84: if (*dx * (*stp-*stx) >= 0.0) SETERRQ(1,"dx * (stp-stx) >= 0.0");
85: if (neP->stepmax < neP->stepmin) SETERRQ(1,"stepmax > stepmin");
87: // Determine if the derivatives have opposite sign */
88: sgnd = *dp * (*dx / TaoAbsDouble(*dx));
90: if (*fp > *fx) {
91: // Case 1: a higher function value.
92: // The minimum is bracketed. If the cubic step is closer
93: // to stx than the quadratic step, the cubic step is taken,
94: // else the average of the cubic and quadratic steps is taken.
96: neP->infoc = 1;
97: bound = 1;
98: theta = 3 * (*fx - *fp) / (*stp - *stx) + *dx + *dp;
99: s = TaoMax(TaoAbsDouble(theta),TaoAbsDouble(*dx));
100: s = TaoMax(s,TaoAbsDouble(*dp));
101: gamma1 = s*sqrt(pow(theta/s,2.0) - (*dx/s)*(*dp/s));
102: if (*stp < *stx) gamma1 = -gamma1;
103: /* Can p be 0? Check */
104: p = (gamma1 - *dx) + theta;
105: q = ((gamma1 - *dx) + gamma1) + *dp;
106: r = p/q;
107: stpc = *stx + r*(*stp - *stx);
108: stpq = *stx + ((*dx/((*fx-*fp)/(*stp-*stx)+*dx))*0.5) * (*stp - *stx);
110: if (TaoAbsDouble(stpc-*stx) < TaoAbsDouble(stpq-*stx)) {
111: stpf = stpc;
112: }
113: else {
114: stpf = stpc + 0.5*(stpq - stpc);
115: }
116: neP->bracket = 1;
117: }
118: else if (sgnd < 0.0) {
119: // Case 2: A lower function value and derivatives of
120: // opposite sign. The minimum is bracketed. If the cubic
121: // step is closer to stx than the quadratic (secant) step,
122: // the cubic step is taken, else the quadratic step is taken.
124: neP->infoc = 2;
125: bound = 0;
126: theta = 3*(*fx - *fp)/(*stp - *stx) + *dx + *dp;
127: s = TaoMax(TaoAbsDouble(theta),TaoAbsDouble(*dx));
128: s = TaoMax(s,TaoAbsDouble(*dp));
129: gamma1 = s*sqrt(pow(theta/s,2.0) - (*dx/s)*(*dp/s));
130: if (*stp > *stx) gamma1 = -gamma1;
131: p = (gamma1 - *dp) + theta;
132: q = ((gamma1 - *dp) + gamma1) + *dx;
133: r = p/q;
134: stpc = *stp + r*(*stx - *stp);
135: stpq = *stp + (*dp/(*dp-*dx))*(*stx - *stp);
137: if (TaoAbsDouble(stpc-*stp) > TaoAbsDouble(stpq-*stp)) {
138: stpf = stpc;
139: }
140: else {
141: stpf = stpq;
142: }
143: neP->bracket = 1;
144: }
145: else if (TaoAbsDouble(*dp) < TaoAbsDouble(*dx)) {
146: // Case 3: A lower function value, derivatives of the
147: // same sign, and the magnitude of the derivative decreases.
148: // The cubic step is only used if the cubic tends to infinity
149: // in the direction of the step or if the minimum of the cubic
150: // is beyond stp. Otherwise the cubic step is defined to be
151: // either stepmin or stepmax. The quadratic (secant) step is also
152: // computed and if the minimum is bracketed then the the step
153: // closest to stx is taken, else the step farthest away is taken.
155: neP->infoc = 3;
156: bound = 1;
157: theta = 3*(*fx - *fp)/(*stp - *stx) + *dx + *dp;
158: s = TaoMax(TaoAbsDouble(theta),TaoAbsDouble(*dx));
159: s = TaoMax(s,TaoAbsDouble(*dp));
161: // The case gamma1 = 0 only arises if the cubic does not tend
162: // to infinity in the direction of the step.
163: gamma1 = s*sqrt(TaoMax(0.0,pow(theta/s,2.0) - (*dx/s)*(*dp/s)));
164: if (*stp > *stx) gamma1 = -gamma1;
165: p = (gamma1 - *dp) + theta;
166: q = (gamma1 + (*dx - *dp)) + gamma1;
167: r = p/q;
168: if (r < 0.0 && gamma1 != 0.0) stpc = *stp + r*(*stx - *stp);
169: else if (*stp > *stx) stpc = neP->stepmax;
170: else stpc = neP->stepmin;
171: stpq = *stp + (*dp/(*dp-*dx)) * (*stx - *stp);
173: if (neP->bracket) {
174: if (TaoAbsDouble(*stp-stpc) < TaoAbsDouble(*stp-stpq)) {
175: stpf = stpc;
176: }
177: else {
178: stpf = stpq;
179: }
180: }
181: else {
182: if (TaoAbsDouble(*stp-stpc) > TaoAbsDouble(*stp-stpq)) {
183: stpf = stpc;
184: }
185: else {
186: stpf = stpq;
187: }
188: }
189: }
190: else {
191: // Case 4: A lower function value, derivatives of the
192: // same sign, and the magnitude of the derivative does
193: // not decrease. If the minimum is not bracketed, the step
194: // is either stpmin or stpmax, else the cubic step is taken.
196: neP->infoc = 4;
197: bound = 0;
198: if (neP->bracket) {
199: theta = 3*(*fp - *fy)/(*sty - *stp) + *dy + *dp;
200: s = TaoMax(TaoAbsDouble(theta),TaoAbsDouble(*dy));
201: s = TaoMax(s,TaoAbsDouble(*dp));
202: gamma1 = s*sqrt(pow(theta/s,2.0) - (*dy/s)*(*dp/s));
203: if (*stp > *sty) gamma1 = -gamma1;
204: p = (gamma1 - *dp) + theta;
205: q = ((gamma1 - *dp) + gamma1) + *dy;
206: r = p/q;
207: stpc = *stp + r*(*sty - *stp);
208: stpq = *stp + (*dp/(*dp-*dx)) * (*stx - *stp);
210: stpf = stpc;
211: }
212: else if (*stp > *stx) {
213: stpf = neP->stepmax;
214: }
215: else {
216: stpf = neP->stepmin;
217: }
218: }
219:
220: // Update the interval of uncertainty. This update does not
221: // depend on the new step or the case analysis above.
223: if (*fp > *fx) {
224: *sty = *stp;
225: *fy = *fp;
226: *dy = *dp;
227: }
228: else {
229: if (sgnd < 0.0) {
230: *sty = *stx;
231: *fy = *fx;
232: *dy = *dx;
233: }
234: *stx = *stp;
235: *fx = *fp;
236: *dx = *dp;
237: }
238:
239: // Compute the new step and safeguard it.
240: stpf = TaoMin(neP->stepmax,stpf);
241: stpf = TaoMax(neP->stepmin,stpf);
242: *stp = stpf;
243: if (neP->bracket && bound) {
244: if (*sty > *stx) {
245: *stp = TaoMin(*stx+0.66*(*sty-*stx),*stp);
246: }
247: else {
248: *stp = TaoMax(*stx+0.66*(*sty-*stx),*stp);
249: }
250: }
251: TaoFunctionReturn(0);
252: }
256: /* @
257: TaoApply_LineSearch - This routine performs a line search algorithm.
259: Input Parameters:
260: + tao - TAO_SOLVER context
261: . X - current iterate (on output X contains new iterate, X + step*S)
262: . S - search direction
263: . f - objective function evaluated at X
264: . G - gradient evaluated at X
265: . W - work vector
266: - step - initial estimate of step length
268: Output parameters:
269: + f - objective function evaluated at new iterate, X + step*S
270: . G - gradient evaluated at new iterate, X + step*S
271: . X - new iterate
272: . gnorm - 2-norm of G
273: - step - final step length
276: Info is set to one of:
277: + 0 - the line search succeeds; the sufficient decrease
278: condition and the directional derivative condition hold
280: negative number if an input parameter is invalid
281: . -1 - step < 0
282: . -2 - ftol < 0
283: . -3 - rtol < 0
284: . -4 - gtol < 0
285: . -5 - stepmin < 0
286: . -6 - stepmax < stepmin
287: - -7 - maxfev < 0
289: positive number > 1 if the line search otherwise terminates
290: + 2 - Relative width of the interval of uncertainty is
291: at most rtol.
292: . 3 - Maximum number of function evaluations (maxfev) has
293: been reached.
294: . 4 - Step is at the lower bound, stepmin.
295: . 5 - Step is at the upper bound, stepmax.
296: . 6 - Rounding errors may prevent further progress.
297: There may not be a step that satisfies the
298: sufficient decrease and curvature conditions.
299: Tolerances may be too small.
300: - 7 - Search direction is not a descent direction.
302: Notes:
303: This algorithm is taken from More' and Thuente, "Line search algorithms
304: with guaranteed sufficient decrease", Argonne National Laboratory,
305: Technical Report MCS-P330-1092.
307: Notes:
308: This routine is used within the following TAO unconstrained minimization
309: solvers: Newton linesearch (tao_nls), limited memory variable metric
310: (tao_lmvm), and conjugate gradient (tao_cg).
312: Level: advanced
314: .keywords: TAO_SOLVER, linesearch
315: @ */
316: static int TaoApply_LineSearch(TAO_SOLVER tao,TaoVec* X,TaoVec* G,TaoVec* S,TaoVec* W,double *f, double *f_full,
317: double *step,TaoInt *info2,void*ctx)
318: {
319: TAO_LINESEARCH *neP = (TAO_LINESEARCH *) tao->linectx;
320: double xtrapf = 4.0;
321: double finit, width, width1, dginit, fm, fxm, fym, dgm, dgxm, dgym;
322: double dgx, dgy, dg, fx, fy, stx, sty, dgtest, ftest1=0.0;
323: int info;
324: TaoInt i, stage1;
325: int need_gradient=0;
327: #if defined(PETSC_USE_COMPLEX)
328: PetscScalar cdginit, cdg, cstep = 0.0;
329: #endif
331: TaoFunctionBegin;
332: /* neP->stepmin - lower bound for step */
333: /* neP->stepmax - upper bound for step */
334: /* neP->rtol - relative tolerance for an acceptable step */
335: /* neP->ftol - tolerance for sufficient decrease condition */
336: /* neP->gtol - tolerance for curvature condition */
337: /* neP->nfev - number of function evaluations */
338: /* neP->maxfev - maximum number of function evaluations */
340: *info2 = 0;
342: /* Check input parameters for errors */
343: if (*step < 0.0) {
344: info = PetscInfo1(tao, "TaoApply_LineSearch: Line search error: step (%g) < 0\n",*step); CHKERRQ(info);
345: *info2 = -1;
346: }
347:
348: if (neP->ftol < 0.0) {
349: info = PetscInfo1(tao, "TaoApply_LineSearch: Line search error: ftol (%g) < 0\n",neP->ftol); CHKERRQ(info);
350: *info2 = -2;
351: }
352:
353: if (neP->rtol < 0.0) {
354: info = PetscInfo1(tao, "TaoApply_LineSearch: Line search error: rtol (%g) < 0\n",neP->rtol); CHKERRQ(info);
355: *info2 = -3;
356: }
358: if (neP->gtol < 0.0) {
359: info = PetscInfo1(tao, "TaoApply_LineSearch: Line search error: gtol (%g) < 0\n",neP->gtol); CHKERRQ(info);
360: *info2 = -4;
361: }
363: if (neP->stepmin < 0.0) {
364: info = PetscInfo1(tao, "TaoApply_LineSearch: Line search error: stepmin (%g) < 0\n",neP->stepmin); CHKERRQ(info);
365: *info2 = -5;
366: }
368: if (neP->stepmax < neP->stepmin) {
369: info = PetscInfo2(tao, "TaoApply_LineSearch: Line search error: stepmax (%g) < stepmin (%g)\n", neP->stepmax,neP->stepmin); CHKERRQ(info);
370: *info2 = -6;
371: }
372:
373: if (neP->maxfev < 0) {
374: info = PetscInfo1(tao, "TaoApply_LineSearch: Line search error: maxfev (%d) < 0\n",neP->maxfev); CHKERRQ(info);
375: *info2 = -7;
376: }
378: if (TaoInfOrNaN(*f)) {
379: info = PetscInfo1(tao, "TaoApply_LineSearch: Line search error: function (%g) inf or nan\n", *f); CHKERRQ(info);
380: *info2 = -8;
381: }
383: /* Check that search direction is a descent direction */
385: #if defined(PETSC_USE_COMPLEX)
386: info = G->Dot(S,&cdginit);CHKERRQ(info); dginit = TaoReal(cdginit);
387: #else
388: info = G->Dot(S,&dginit);CHKERRQ(info);
389: #endif
391: if (TaoInfOrNaN(dginit)) {
392: info = PetscInfo1(tao,"TaoApply_LineSearch: Line search error: dginit (%g) inf or nan\n", dginit); CHKERRQ(info);
393: *info2 = -9;
394: }
396: if (dginit >= 0.0) {
397: info = PetscInfo(tao,"TaoApply_LineSearch:Search direction not a descent direction\n"); CHKERRQ(info);
398: *info2 = -10;
399: }
401: if (*info2) {
402: TaoFunctionReturn(0);
403: }
405: /* Initialization */
406: neP->bracket = 0;
407: *info2 = 0;
408: stage1 = 1;
409: finit = *f;
410: dgtest = neP->ftol * dginit;
411: width = neP->stepmax - neP->stepmin;
412: width1 = width * 2.0;
413: info = W->CopyFrom(X);CHKERRQ(info);
414: /* Variable dictionary:
415: stx, fx, dgx - the step, function, and derivative at the best step
416: sty, fy, dgy - the step, function, and derivative at the other endpoint
417: of the interval of uncertainty
418: step, f, dg - the step, function, and derivative at the current step */
420: stx = 0.0;
421: fx = finit;
422: dgx = dginit;
423: sty = 0.0;
424: fy = finit;
425: dgy = dginit;
426:
427: neP->nfev = 0;
428: tao->new_search=TAO_TRUE;
429: for (i=0; i< neP->maxfev; i++) {
430: /* Set min and max steps to correspond to the interval of uncertainty */
431: if (neP->bracket) {
432: neP->stepmin = TaoMin(stx,sty);
433: neP->stepmax = TaoMax(stx,sty);
434: }
435: else {
436: neP->stepmin = stx;
437: neP->stepmax = *step + xtrapf * (*step - stx);
438: }
440: /* Force the step to be within the bounds */
441: *step = TaoMax(*step,neP->stepmin);
442: *step = TaoMin(*step,neP->stepmax);
443:
444: /* If an unusual termination is to occur, then let step be the lowest
445: point obtained thus far */
446: if (((neP->bracket) && (*step <= neP->stepmin || *step >= neP->stepmax)) ||
447: ((neP->bracket) && (neP->stepmax - neP->stepmin <= neP->rtol * neP->stepmax)) ||
448: (neP->nfev >= neP->maxfev - 1) || (neP->infoc == 0)) {
449: *step = stx;
450: }
452: #if defined(PETSC_USE_COMPLEX)
453: cstep = *step;
454: info = W->Waxpby(cstep,S,1.0,X);CHKERRQ(info);
455: #else
456: info = W->Waxpby(*step,S,1.0,X);CHKERRQ(info); /* W = X + step*S */
457: #endif
458: tao->current_step=*step;
459: if (tao->MeritFunctionGTSApply) {
460: info = TaoComputeMeritFunctionGTS(tao,W,S,f,&dg); CHKERRQ(info);
461: need_gradient = 1;
462: } else {
463: info = TaoComputeMeritFunctionGradient(tao,W,f,G);CHKERRQ(info);
464: tao->new_search=TAO_FALSE;
465: neP->nfev++;
466: #if defined(PETSC_USE_COMPLEX)
467: info = G->Dot(S,&cdg);CHKERRQ(info); dg = TaoReal(cdg);
468: #else
469: info = G->Dot(S,&dg);CHKERRQ(info); /* dg = G^T S */
470: #endif
471: }
472: if (0 == i) {
473: *f_full = *f;
474: }
475:
476: if (TaoInfOrNaN(*f) || TaoInfOrNaN(dg)) {
477: // User provided compute function generated Not-a-Number, assume
478: // domain violation and set function value and directional
479: // derivative to infinity.
480: *f = TAO_INFINITY;
481: dg = TAO_INFINITY;
482: }
484: ftest1 = finit + *step * dgtest;
486: /* Convergence testing */
487: if (((*f - ftest1 <= 1.0e-10 * fabs(finit)) &&
488: (TaoAbsDouble(dg) + neP->gtol*dginit <= 0.0))) {
489: info = PetscInfo(tao, "TaoApply_LineSearch:Line search success: Sufficient decrease and directional deriv conditions hold\n"); CHKERRQ(info);
490: *info2 = 0;
491: break;
492: }
494: /* Checks for bad cases */
495: if (((neP->bracket) && (*step <= neP->stepmin||*step >= neP->stepmax)) || (!neP->infoc)) {
496: info = PetscInfo(tao,"TaoApply_LineSearch:Rounding errors may prevent further progress. May not be a step satisfying\n"); CHKERRQ(info);
497: info = PetscInfo(tao,"TaoApply_LineSearch:sufficient decrease and curvature conditions. Tolerances may be too small.\n"); CHKERRQ(info);
498: *info2 = 6; break;
499: }
500: if ((*step == neP->stepmax) && (*f <= ftest1) && (dg <= dgtest)) {
501: info = PetscInfo1(tao,"TaoApply_LineSearch:Step is at the upper bound, stepmax (%g)\n",neP->stepmax); CHKERRQ(info);
502: *info2 = 5; break;
503: }
504: if ((*step == neP->stepmin) && (*f >= ftest1) && (dg >= dgtest)) {
505: info = PetscInfo1(tao,"TaoApply_LineSearch:Step is at the lower bound, stepmin (%g)\n",neP->stepmin); CHKERRQ(info);
506: *info2 = 4; break;
507: }
508: if (neP->nfev >= neP->maxfev) {
509: info = PetscInfo2(tao,"TaoApply_LineSearch:Number of line search function evals (%d) > maximum (%d)\n",neP->nfev,neP->maxfev); CHKERRQ(info);
510: *info2 = 3; break;
511: }
512: if ((neP->bracket) && (neP->stepmax - neP->stepmin <= neP->rtol*neP->stepmax)){
513: info = PetscInfo1(tao,"TaoApply_LineSearch:Relative width of interval of uncertainty is at most rtol (%g)\n",neP->rtol); CHKERRQ(info);
514: *info2 = 2; break;
515: }
517: /* In the first stage, we seek a step for which the modified function
518: has a nonpositive value and nonnegative derivative */
519: if ((stage1) && (*f <= ftest1) && (dg >= dginit * TaoMin(neP->ftol, neP->gtol))) {
520: stage1 = 0;
521: }
523: /* A modified function is used to predict the step only if we
524: have not obtained a step for which the modified function has a
525: nonpositive function value and nonnegative derivative, and if a
526: lower function value has been obtained but the decrease is not
527: sufficient */
529: if ((stage1) && (*f <= fx) && (*f > ftest1)) {
530: fm = *f - *step * dgtest; /* Define modified function */
531: fxm = fx - stx * dgtest; /* and derivatives */
532: fym = fy - sty * dgtest;
533: dgm = dg - dgtest;
534: dgxm = dgx - dgtest;
535: dgym = dgy - dgtest;
536:
537: /* Update the interval of uncertainty and compute the new step */
538: info = TaoStep_LineSearch(tao,&stx,&fxm,&dgxm,&sty,&fym,&dgym,step,&fm,&dgm);CHKERRQ(info);
539:
540: fx = fxm + stx * dgtest; /* Reset the function and */
541: fy = fym + sty * dgtest; /* gradient values */
542: dgx = dgxm + dgtest;
543: dgy = dgym + dgtest;
544: }
545: else {
546: /* Update the interval of uncertainty and compute the new step */
547: info = TaoStep_LineSearch(tao,&stx,&fx,&dgx,&sty,&fy,&dgy,step,f,&dg);CHKERRQ(info);
548: }
549:
550: /* Force a sufficient decrease in the interval of uncertainty */
551: if (neP->bracket) {
552: if (TaoAbsDouble(sty - stx) >= 0.66 * width1) *step = stx + 0.5*(sty - stx);
553: width1 = width;
554: width = TaoAbsDouble(sty - stx);
555: }
556: }
557:
558: /* Finish computations */
559: info = X->CopyFrom(W); CHKERRQ(info);
560: if (need_gradient) {
561: info = TaoComputeMeritGradient(tao,X,G); CHKERRQ(info);
562: }
563: info = PetscInfo2(tao,"TaoApply_LineSearch:%d function evals in line search, step = %10.4f\n",neP->nfev,*step); CHKERRQ(info);
564: TaoFunctionReturn(0);
565: }
569: /* @
570: TaoApply_BoundLineSearch - This routine performs a line search algorithm.
572: Input Parameters:
573: + tao - TAO_SOLVER context
574: . X - current iterate (on output X contains new iterate, X + step*S)
575: . S - search direction
576: . f - objective function evaluated at X
577: . G - gradient evaluated at X
578: . W - work vector
579: - step - initial estimate of step length
581: Output parameters:
582: + f - objective function evaluated at new iterate, X + step*S
583: . G - gradient evaluated at new iterate, X + step*S
584: . X - new iterate
585: . gnorm - 2-norm of G
586: - step - final step length
589: Info is set to one of:
590: + 0 - the line search succeeds; the sufficient decrease
591: condition and the directional derivative condition hold
593: negative number if an input parameter is invalid
594: . -1 - step < 0
595: . -2 - ftol < 0
596: . -3 - rtol < 0
597: . -4 - gtol < 0
598: . -5 - stepmin < 0
599: . -6 - stepmax < stepmin
600: - -7 - maxfev < 0
602: positive number > 1 if the line search otherwise terminates
603: + 2 - Relative width of the interval of uncertainty is
604: at most rtol.
605: . 3 - Maximum number of function evaluations (maxfev) has
606: been reached.
607: . 4 - Step is at the lower bound, stepmin.
608: . 5 - Step is at the upper bound, stepmax.
609: . 6 - Rounding errors may prevent further progress.
610: There may not be a step that satisfies the
611: sufficient decrease and curvature conditions.
612: Tolerances may be too small.
613: - 7 - Search direction is not a descent direction.
615: Notes:
616: This algorithm is is a modification of the algorithm by More' and
617: Thuente. The modifications concern bounds. This algorithm steps
618: in the direction passed into this routine. This point get
619: projected back into the feasible set. In the context of bound
620: constrained optimization, there may not be a point in the piecewise
621: linear path that satisfies the Wolfe conditions. When the active
622: set is changing, decrease in the objective function may be
623: sufficient to terminate this line search.
625: Note: Much of this coded is identical to the More' Thuente line search.
626: Variations to the code are commented.
628: Notes:
629: This routine is used within the following TAO bound constrained minimization
630: solvers: Newton linesearch (tao_tron) and limited memory variable metric
631: (tao_blmvm).
633: Level: advanced
635: .keywords: TAO_SOLVER, linesearch
636: @ */
637: static int TaoApply_BoundLineSearch(TAO_SOLVER tao,TaoVec* X,TaoVec* G,TaoVec* S,TaoVec* W,double *f, double *f_full,
638: double *step,TaoInt *info2,void*ctx)
639: {
640: TAO_LINESEARCH *neP = (TAO_LINESEARCH *) tao->linectx;
641: TaoVec *XL,*XU;
642: double xtrapf = 4.0;
643: double finit, width, width1, dginit, fm, fxm, fym, dgm, dgxm, dgym;
644: double dgx, dgy, dg, fx, fy, stx, sty, dgtest, ftest1=0.0, ftest2=0.0;
645: double bstepmin1, bstepmin2, bstepmax;
646: double dg1, dg2;
647: int info;
648: int need_gradient=0;
649: TaoInt i, stage1;
651: #if defined(PETSC_USE_COMPLEX)
652: PetscScalar cdginit, cdg, cstep = 0.0;
653: #endif
655: TaoFunctionBegin;
656: /* neP->stepmin - lower bound for step */
657: /* neP->stepmax - upper bound for step */
658: /* neP->rtol - relative tolerance for an acceptable step */
659: /* neP->ftol - tolerance for sufficient decrease condition */
660: /* neP->gtol - tolerance for curvature condition */
661: /* neP->nfev - number of function evaluations */
662: /* neP->maxfev - maximum number of function evaluations */
664: /* Check input parameters for errors */
665: if (*step < 0.0) {
666: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Line search error: step (%g) < 0\n",*step); CHKERRQ(info);
667: *info2 = -1; TaoFunctionReturn(0);
668: }
669: else if (neP->ftol < 0.0) {
670: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Line search error: ftol (%g) < 0\n",neP->ftol); CHKERRQ(info);
671: *info2 = -2; TaoFunctionReturn(0);
672: }
673: else if (neP->rtol < 0.0) {
674: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Line search error: rtol (%g) < 0\n",neP->rtol); CHKERRQ(info);
675: *info2 = -3; TaoFunctionReturn(0);
676: }
677: else if (neP->gtol < 0.0) {
678: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Line search error: gtol (%g) < 0\n",neP->gtol); CHKERRQ(info);
679: *info2 = -4; TaoFunctionReturn(0);
680: }
681: else if (neP->stepmin < 0.0) {
682: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Line search error: stepmin (%g) < 0\n",neP->stepmin); CHKERRQ(info);
683: *info2 = -5; TaoFunctionReturn(0);
684: }
685: else if (neP->stepmax < neP->stepmin) {
686: info = PetscInfo2(tao,"TaoApply_BoundLineSearch:Line search error: stepmax (%g) < stepmin (%g)\n", neP->stepmax,neP->stepmin); CHKERRQ(info);
687: *info2 = -6; TaoFunctionReturn(0);
688: }
689: else if (neP->maxfev < 0.0) {
690: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Line search error: maxfev (%d) < 0\n",neP->maxfev); CHKERRQ(info);
691: *info2 = -7; TaoFunctionReturn(0);
692: }
694: /* Compute step length needed to make all variables equal a bound */
695: /* Compute the smallest steplength that will make one nonbinding */
696: /* variable equal the bound */
697: info = TaoGetVariableBounds(tao,&XL,&XU); CHKERRQ(info);
698: info = S->Negate(); CHKERRQ(info);
699: info = S->BoundGradientProjection(S,XL,X,XU); CHKERRQ(info);
700: info = S->Negate(); CHKERRQ(info);
701: info = X->StepBoundInfo(XL,XU,S,&bstepmin1,&bstepmin2,&bstepmax); CHKERRQ(info);
702: neP->stepmax=TaoMin(bstepmax,1.0e+15);
704: /* Check that search direction is a descent direction */
706: #if defined(PETSC_USE_COMPLEX)
707: info = G->Dot(S,&cdginit);CHKERRQ(info); dginit = TaoReal(cdginit);
708: #else
709: info = G->Dot(S,&dginit);CHKERRQ(info);
710: #endif
712: if (dginit >= 0.0) {
713: info = PetscInfo(tao,"TaoApply_BoundLineSearch:Search direction not a descent direction\n"); CHKERRQ(info);
714: *info2 = 7; TaoFunctionReturn(0);
715: }
717: /* Initialization */
718: neP->bracket = 0;
719: *info2 = 0;
720: stage1 = 1;
721: finit = *f;
722: dgtest = neP->ftol * dginit;
723: width = neP->stepmax - neP->stepmin;
724: width1 = width * 2.0;
725: info = W->CopyFrom(X);CHKERRQ(info);
726: /* Variable dictionary:
727: stx, fx, dgx - the step, function, and derivative at the best step
728: sty, fy, dgy - the step, function, and derivative at the other endpoint
729: of the interval of uncertainty
730: step, f, dg - the step, function, and derivative at the current step */
732: stx = 0.0;
733: fx = finit;
734: dgx = dginit;
735: sty = 0.0;
736: fy = finit;
737: dgy = dginit;
738:
739: neP->nfev = 0;
740: tao->new_search=TAO_TRUE;
741: for (i=0; i< neP->maxfev; i++) {
742: /* Set min and max steps to correspond to the interval of uncertainty */
743: if (neP->bracket) {
744: neP->stepmin = TaoMin(stx,sty);
745: neP->stepmax = TaoMax(stx,sty);
746: } else {
747: neP->stepmin = stx;
748: neP->stepmax = *step + xtrapf * (*step - stx);
749: }
751: /* Force the step to be within the bounds */
752: *step = TaoMax(*step,neP->stepmin);
753: *step = TaoMin(*step,neP->stepmax);
755: /* If an unusual termination is to occur, then let step be the lowest
756: point obtained thus far */
757: if (((neP->bracket) && (*step <= neP->stepmin || *step >= neP->stepmax)) ||
758: ((neP->bracket) && (neP->stepmax - neP->stepmin <= neP->rtol * neP->stepmax)) ||
759: (neP->nfev >= neP->maxfev - 1) || (neP->infoc == 0)) {
760: *step = stx;
761: }
762:
763: #if defined(PETSC_USE_COMPLEX)
764: cstep = *step;
765: info = W->Waxpby(cstep,S,1.0,X);CHKERRQ(info);
766: #else
767: info = W->Waxpby(*step,S,1.0,X);CHKERRQ(info); /* W = X + step*S */
768: #endif
770: info=W->Median(XL,W,XU);CHKERRQ(info);
771: tao->current_step=*step;
772: if (tao->MeritFunctionGTSApply) {
773: info = TaoComputeMeritFunctionGTS(tao,W,S,f,&dg); CHKERRQ(info);
774: need_gradient = 1;
775: } else {
776: info = TaoComputeMeritFunctionGradient(tao,W,f,G);CHKERRQ(info);
777: tao->new_search=TAO_FALSE;
778: neP->nfev++;
779: #if defined(PETSC_USE_COMPLEX)
780: info = G->Dot(S,&cdg);CHKERRQ(info); dg = TaoReal(cdg);
781: #else
782: info = G->Dot(X,&dg1);CHKERRQ(info); /* dg = G^T S */
783: info = G->Dot(W,&dg2);CHKERRQ(info); /* dg = G^T S */
784: dg = (dg2-dg1) / (*step);
785: #endif
786: }
787: if (0 == i) {
788: *f_full = *f;
789: }
791: if ((*f != *f) || (dg1 != dg1) || (dg2 != dg2) || (dg != dg)) {
792: // User provided compute function generated Not-a-Number, assume
793: // domain violation and set function value and directional
794: // derivative to infinity.
795: *f = TAO_INFINITY;
796: dg = TAO_INFINITY;
797: dg1 = TAO_INFINITY;
798: dg2 = TAO_INFINITY;
799: }
801: ftest1 = finit + (*step) * dgtest;
802: ftest2 = finit + (*step) * dgtest * neP->ftol; // Armijo
804: /* Convergence testing */
805: if ((*f <= ftest1) && (TaoAbsDouble(dg) <= neP->gtol*(-dginit))) {
806: info = PetscInfo(tao, "TaoApply_BoundLineSearch:Line search success: Sufficient decrease and directional deriv conditions hold\n"); CHKERRQ(info);
807: *info2 = 0;
808: break;
809: }
811: /* Check Armijo if beyond the first breakpoint */
812: if ((*f <= ftest2) && (*step >= bstepmin2)) {
813: info = PetscInfo(tao,"TaoApply_BoundLineSearch:Line search success: Sufficient decrease\n"); CHKERRQ(info);
814: *info2 = 0;
815: break;
816: }
818: /* Checks for bad cases */
819: if (((neP->bracket) && (*step <= neP->stepmin||*step >= neP->stepmax)) || (!neP->infoc)) {
820: info = PetscInfo(tao,"TaoApply_LineSearch:Rounding errors may prevent further progress. May not be a step satisfying\n"); CHKERRQ(info);
821: info = PetscInfo(tao,"TaoApply_BoundLineSearch:sufficient decrease and curvature conditions. Tolerances may be too small.\n"); CHKERRQ(info);
822: *info2 = 6; break;
823: }
824: if ((*step == neP->stepmax) && (*f <= ftest1) && (dg <= dgtest)) {
825: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Step is at the upper bound, stepmax (%g)\n",neP->stepmax); CHKERRQ(info);
826: *info2 = 5; break;
827: }
828: if ((*step == neP->stepmin) && (*f >= ftest1) && (dg >= dgtest)) {
829: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Step is at the lower bound, stepmin (%g)\n",neP->stepmin); CHKERRQ(info);
830: *info2 = 4; break;
831: }
832: if (neP->nfev >= neP->maxfev) {
833: info = PetscInfo2(tao,"TaoApply_BoundLineSearch:Number of line search function evals (%d) > maximum (%d)\n",neP->nfev,neP->maxfev); CHKERRQ(info);
834: *info2 = 3; break;
835: }
836: if ((neP->bracket) && (neP->stepmax - neP->stepmin <= neP->rtol*neP->stepmax)){
837: info = PetscInfo1(tao,"TaoApply_BoundLineSearch:Relative width of interval of uncertainty is at most rtol (%g)\n",neP->rtol); CHKERRQ(info);
838: *info2 = 2; break;
839: }
841: /* In the first stage, we seek a step for which the modified function
842: has a nonpositive value and nonnegative derivative */
843: if ((stage1) && (*f <= ftest1) && (dg >= dginit * TaoMin(neP->ftol, neP->gtol)))
844: stage1 = 0;
846: /* A modified function is used to predict the step only if we
847: have not obtained a step for which the modified function has a
848: nonpositive function value and nonnegative derivative, and if a
849: lower function value has been obtained but the decrease is not
850: sufficient */
852: if ((stage1) && (*f <= fx) && (*f > ftest1)) {
853: fm = *f - *step * dgtest; /* Define modified function */
854: fxm = fx - stx * dgtest; /* and derivatives */
855: fym = fy - sty * dgtest;
856: dgm = dg - dgtest;
857: dgxm = dgx - dgtest;
858: dgym = dgy - dgtest;
860: /* Update the interval of uncertainty and compute the new step */
861: info = TaoStep_LineSearch(tao,&stx,&fxm,&dgxm,&sty,&fym,&dgym,step,&fm,&dgm);CHKERRQ(info);
863: fx = fxm + stx * dgtest; /* Reset the function and */
864: fy = fym + sty * dgtest; /* gradient values */
865: dgx = dgxm + dgtest;
866: dgy = dgym + dgtest;
867: } else {
868: /* Update the interval of uncertainty and compute the new step */
869: info = TaoStep_LineSearch(tao,&stx,&fx,&dgx,&sty,&fy,&dgy,step,f,&dg);CHKERRQ(info);
870: }
872: /* Force a sufficient decrease in the interval of uncertainty */
873: if (neP->bracket) {
874: if (TaoAbsDouble(sty - stx) >= 0.66 * width1) *step = stx + 0.5*(sty - stx);
875: width1 = width;
876: width = TaoAbsDouble(sty - stx);
877: }
878: }
880: /* Finish computations */
881: info = X->CopyFrom(W); CHKERRQ(info);
882: if (need_gradient) {
883: info = TaoComputeMeritGradient(tao,X,G); CHKERRQ(info);
884: }
885: info = PetscInfo2(tao,"TaoApply_BoundLineSearch:%d function evals in line search, step = %10.4e\n",neP->nfev,*step); CHKERRQ(info);
886: TaoFunctionReturn(0);
887: }
891: static int TaoDestroy_LineSearch(TAO_SOLVER tao, void *ctx)
892: {
893: int info;
895: TaoFunctionBegin;
896: info = TaoFree(tao->linectx);CHKERRQ(info);
897: TaoFunctionReturn(0);
898: }
902: static int TaoSetOptions_LineSearch(TAO_SOLVER tao, void*linectx)
903: {
904: TAO_LINESEARCH *ctx = (TAO_LINESEARCH *)linectx;
905: int info;
907: TaoFunctionBegin;
908: info = TaoOptionsHead("More-Thuente line line search options for minimization");CHKERRQ(info);
909: info = TaoOptionInt("-tao_ls_maxfev","max function evals in line search","",ctx->maxfev,&ctx->maxfev,0);CHKERRQ(info);
910: info = TaoOptionDouble("-tao_ls_ftol","tol for sufficient decrease","",ctx->ftol,&ctx->ftol,0);CHKERRQ(info);
911: info = TaoOptionDouble("-tao_ls_gtol","tol for curvature condition","",ctx->gtol,&ctx->gtol,0);CHKERRQ(info);
912: info = TaoOptionDouble("-tao_ls_rtol","relative tol for acceptable step","",ctx->rtol,&ctx->rtol,0);CHKERRQ(info);
913: info = TaoOptionDouble("-tao_ls_stepmin","lower bound for step","",ctx->stepmin,&ctx->stepmin,0);CHKERRQ(info);
914: info = TaoOptionDouble("-tao_ls_stepmax","upper bound for step","",ctx->stepmax,&ctx->stepmax,0);CHKERRQ(info);
915: info = TaoOptionsTail();CHKERRQ(info);
916: TaoFunctionReturn(0);
917: }
921: static int TaoView_LineSearch(TAO_SOLVER tao, void*ctx)
922: {
923: TAO_LINESEARCH *ls = (TAO_LINESEARCH *)ctx;
924: int info;
926: TaoFunctionBegin;
927: info=TaoPrintInt(tao," More'-Thuente Line search: maxf=%d,",ls->maxfev);CHKERRQ(info);
928: info=TaoPrintDouble(tao," ftol=%g,",ls->ftol);CHKERRQ(info);
929: info=TaoPrintDouble(tao," rtol=%g,",ls->rtol);CHKERRQ(info);
930: info=TaoPrintDouble(tao," gtol=%g\n",ls->gtol);CHKERRQ(info);
931: TaoFunctionReturn(0);
932: }
936: /*@C
937: TaoCreateMoreThuenteLineSearch - Create a line search
939: Input Parameters:
940: + tao - TAO_SOLVER context
941: . fftol - the sufficient descent parameter , greater than 0.
942: - ggtol - the curvature tolerance, greater than 0, less than 1.
945: Note:
946: If either fftol or ggtol is 0, default parameters will be used.
948: Note:
949: This algorithm is taken from More' and Thuente, "Line search algorithms
950: with guaranteed sufficient decrease", Argonne National Laboratory,
951: Technical Report MCS-P330-1092.
953: Note:
954: This line search enforces the strong Wolfe conditions for unconstrained
955: optimization. This routine is used within the following TAO unconstrained
956: minimization solvers: Newton linesearch (tao_nls), limited memory variable
957: metric (tao_lmvm), and nonlinear conjugate gradient methods.
959: Level: developer
961: .keywords: TAO_SOLVER, linesearch
962: @*/
963: int TaoCreateMoreThuenteLineSearch(TAO_SOLVER tao, double fftol, double ggtol)
964: {
965: int info;
966: TAO_LINESEARCH *neP;
968: TaoFunctionBegin;
970: info = TaoNew(TAO_LINESEARCH,&neP); CHKERRQ(info);
971: info = PetscLogObjectMemory(tao,sizeof(TAO_LINESEARCH)); CHKERRQ(info);
973: if (fftol<=0) fftol=0.0001;
974: if (ggtol<=0) ggtol=0.9;
976: neP->ftol = fftol;
977: neP->rtol = 1.0e-10;
978: neP->gtol = ggtol;
979: neP->stepmin = 1.0e-20;
980: neP->stepmax = 1.0e+20;
981: neP->nfev = 0;
982: neP->bracket = 0;
983: neP->infoc = 1;
984: neP->maxfev = 30;
986: info = TaoSetLineSearch(tao,0,
987: TaoSetOptions_LineSearch,
988: TaoApply_LineSearch,
989: TaoView_LineSearch,
990: TaoDestroy_LineSearch,
991: (void *) neP);CHKERRQ(info);
993: TaoFunctionReturn(0);
994: }
998: /*@C
999: TaoCreateMoreThuenteBoundLineSearch - Create a line search
1001: Input Parameters:
1002: + tao - TAO_SOLVER context
1003: . fftol - the sufficient descent parameter , greater than 0.
1004: - ggtol - the curvature tolerance, greater than 0, less than 1.
1007: Note:
1008: If either fftol or ggtol is 0, default parameters will be used.
1010: Note:
1011: This algorithm is is a modification of the algorithm by More' and Thuente.
1012: The modifications concern bounds. This algorithm steps in the direction
1013: passed into this routine. This point get projected back into the feasible set.
1014: It tries to satisfy the Wolfe conditions, but in the context of bound constrained
1015: optimization, there may not be a point in the piecewise linear
1016: path that satisfies the Wolfe conditions. When the active set
1017: is changing, decrease in the objective function may be sufficient
1018: to terminate this line search.
1020: Note:
1021: This routine is used within the following TAO bound constrained
1022: minimization solvers: Newton trust region (tao_tron) and limited memory variable
1023: metric (tao_blmvm).
1025: Level: developer
1027: .keywords: TAO_SOLVER, linesearch
1028: @*/
1029: int TaoCreateMoreThuenteBoundLineSearch(TAO_SOLVER tao, double fftol, double ggtol)
1030: {
1031: int info;
1032: TAO_LINESEARCH *neP;
1034: TaoFunctionBegin;
1036: info = TaoNew(TAO_LINESEARCH,&neP); CHKERRQ(info);
1037: info = PetscLogObjectMemory(tao,sizeof(TAO_LINESEARCH)); CHKERRQ(info);
1039: if (fftol<=0) fftol=0.0001;
1040: if (ggtol<=0) ggtol=0.9;
1042: neP->ftol = fftol;
1043: neP->rtol = 1.0e-10;
1044: neP->gtol = ggtol;
1045: neP->stepmin = 1.0e-20;
1046: neP->stepmax = 1.0e+20;
1047: neP->nfev = 0;
1048: neP->bracket = 0;
1049: neP->infoc = 1;
1050: neP->maxfev = 30;
1052: info = TaoSetLineSearch(tao,0,
1053: TaoSetOptions_LineSearch,
1054: TaoApply_BoundLineSearch,
1055: TaoView_LineSearch,
1056: TaoDestroy_LineSearch,
1057: (void *) neP);CHKERRQ(info);
1059: TaoFunctionReturn(0);
1060: }