1af0996ceSBarry Smith #include <petsc/private/taolinesearchimpl.h> 2aaa7dc30SBarry Smith #include <../src/tao/linesearch/impls/morethuente/morethuente.h> 3a7e14dcfSSatish Balay 4a7e14dcfSSatish Balay /* 5a7e14dcfSSatish Balay This algorithm is taken from More' and Thuente, "Line search algorithms 6a7e14dcfSSatish Balay with guaranteed sufficient decrease", Argonne National Laboratory, 7a7e14dcfSSatish Balay Technical Report MCS-P330-1092. 8a7e14dcfSSatish Balay */ 9a7e14dcfSSatish Balay 1053506e15SBarry Smith static PetscErrorCode Tao_mcstep(TaoLineSearch ls,PetscReal *stx,PetscReal *fx,PetscReal *dx,PetscReal *sty,PetscReal *fy,PetscReal *dy,PetscReal *stp,PetscReal *fp,PetscReal *dp); 11a7e14dcfSSatish Balay 12a7e14dcfSSatish Balay static PetscErrorCode TaoLineSearchDestroy_MT(TaoLineSearch ls) 13a7e14dcfSSatish Balay { 14a7e14dcfSSatish Balay PetscErrorCode ierr; 158caf6e8cSBarry Smith TaoLineSearch_MT *mt; 1653506e15SBarry Smith 17a7e14dcfSSatish Balay PetscFunctionBegin; 18a7e14dcfSSatish Balay PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); 198caf6e8cSBarry Smith mt = (TaoLineSearch_MT*)(ls->data); 20a7e14dcfSSatish Balay if (mt->x) { 21a7e14dcfSSatish Balay ierr = PetscObjectDereference((PetscObject)mt->x);CHKERRQ(ierr); 22a7e14dcfSSatish Balay } 23a7e14dcfSSatish Balay ierr = VecDestroy(&mt->work);CHKERRQ(ierr); 24302440fdSBarry Smith ierr = PetscFree(ls->data);CHKERRQ(ierr); 25a7e14dcfSSatish Balay PetscFunctionReturn(0); 26a7e14dcfSSatish Balay } 27a7e14dcfSSatish Balay 284416b707SBarry Smith static PetscErrorCode TaoLineSearchSetFromOptions_MT(PetscOptionItems *PetscOptionsObject,TaoLineSearch ls) 29a7e14dcfSSatish Balay { 30a7e14dcfSSatish Balay PetscFunctionBegin; 31a7e14dcfSSatish Balay PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); 32a7e14dcfSSatish Balay PetscFunctionReturn(0); 33a7e14dcfSSatish Balay } 34a7e14dcfSSatish Balay 352a0dac07SAlp Dener static PetscErrorCode TaoLineSearchMonitor_MT(TaoLineSearch ls) 362a0dac07SAlp Dener { 372a0dac07SAlp Dener TaoLineSearch_MT *mt = (TaoLineSearch_MT*)ls->data; 382a0dac07SAlp Dener PetscErrorCode ierr; 392a0dac07SAlp Dener 402a0dac07SAlp Dener PetscFunctionBegin; 412a0dac07SAlp Dener ierr = PetscViewerASCIIPrintf(ls->viewer, "stx: %g, fx: %g, dgx: %g\n", (double)mt->stx, (double)mt->fx, (double)mt->dgx);CHKERRQ(ierr); 422a0dac07SAlp Dener ierr = PetscViewerASCIIPrintf(ls->viewer, "sty: %g, fy: %g, dgy: %g\n", (double)mt->sty, (double)mt->fy, (double)mt->dgy);CHKERRQ(ierr); 432a0dac07SAlp Dener PetscFunctionReturn(0); 442a0dac07SAlp Dener } 45a7e14dcfSSatish Balay 46a7e14dcfSSatish Balay static PetscErrorCode TaoLineSearchApply_MT(TaoLineSearch ls, Vec x, PetscReal *f, Vec g, Vec s) 47a7e14dcfSSatish Balay { 48a7e14dcfSSatish Balay PetscErrorCode ierr; 498caf6e8cSBarry Smith TaoLineSearch_MT *mt; 50a7e14dcfSSatish Balay 51a7e14dcfSSatish Balay PetscReal xtrapf = 4.0; 52a7e14dcfSSatish Balay PetscReal finit, width, width1, dginit, fm, fxm, fym, dgm, dgxm, dgym; 53a7e14dcfSSatish Balay PetscReal dgx, dgy, dg, dg2, fx, fy, stx, sty, dgtest; 54a7e14dcfSSatish Balay PetscReal ftest1=0.0, ftest2=0.0; 55a7e14dcfSSatish Balay PetscInt i, stage1,n1,n2,nn1,nn2; 56a7e14dcfSSatish Balay PetscReal bstepmin1, bstepmin2, bstepmax; 5753506e15SBarry Smith PetscBool g_computed=PETSC_FALSE; /* to prevent extra gradient computation */ 58a7e14dcfSSatish Balay 59a7e14dcfSSatish Balay PetscFunctionBegin; 60a7e14dcfSSatish Balay PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); 61a7e14dcfSSatish Balay PetscValidHeaderSpecific(x,VEC_CLASSID,2); 62a7e14dcfSSatish Balay PetscValidScalarPointer(f,3); 63a7e14dcfSSatish Balay PetscValidHeaderSpecific(g,VEC_CLASSID,4); 64a7e14dcfSSatish Balay PetscValidHeaderSpecific(s,VEC_CLASSID,5); 65a7e14dcfSSatish Balay 662a0dac07SAlp Dener ierr = TaoLineSearchMonitor(ls, 0, *f, 0.0);CHKERRQ(ierr); 672a0dac07SAlp Dener 68a7e14dcfSSatish Balay /* comm,type,size checks are done in interface TaoLineSearchApply */ 698caf6e8cSBarry Smith mt = (TaoLineSearch_MT*)(ls->data); 70a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_CONTINUE_ITERATING; 71a7e14dcfSSatish Balay 72a7e14dcfSSatish Balay /* Check work vector */ 73a7e14dcfSSatish Balay if (!mt->work) { 74a7e14dcfSSatish Balay ierr = VecDuplicate(x,&mt->work);CHKERRQ(ierr); 75a7e14dcfSSatish Balay mt->x = x; 76a7e14dcfSSatish Balay ierr = PetscObjectReference((PetscObject)mt->x);CHKERRQ(ierr); 7753506e15SBarry Smith } else if (x != mt->x) { 78a7e14dcfSSatish Balay ierr = VecDestroy(&mt->work);CHKERRQ(ierr); 79a7e14dcfSSatish Balay ierr = VecDuplicate(x,&mt->work);CHKERRQ(ierr); 80a7e14dcfSSatish Balay ierr = PetscObjectDereference((PetscObject)mt->x);CHKERRQ(ierr); 81a7e14dcfSSatish Balay mt->x = x; 82a7e14dcfSSatish Balay ierr = PetscObjectReference((PetscObject)mt->x);CHKERRQ(ierr); 83a7e14dcfSSatish Balay } 84a7e14dcfSSatish Balay 85a7e14dcfSSatish Balay if (ls->bounded) { 86a7e14dcfSSatish Balay /* Compute step length needed to make all variables equal a bound */ 87a7e14dcfSSatish Balay /* Compute the smallest steplength that will make one nonbinding variable 88a7e14dcfSSatish Balay equal the bound */ 89a7e14dcfSSatish Balay ierr = VecGetLocalSize(ls->upper,&n1);CHKERRQ(ierr); 90a7e14dcfSSatish Balay ierr = VecGetLocalSize(mt->x, &n2);CHKERRQ(ierr); 91a7e14dcfSSatish Balay ierr = VecGetSize(ls->upper,&nn1);CHKERRQ(ierr); 92a7e14dcfSSatish Balay ierr = VecGetSize(mt->x,&nn2);CHKERRQ(ierr); 9353506e15SBarry Smith if (n1 != n2 || nn1 != nn2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Variable vector not compatible with bounds vector"); 94a7e14dcfSSatish Balay ierr = VecScale(s,-1.0);CHKERRQ(ierr); 95a7e14dcfSSatish Balay ierr = VecBoundGradientProjection(s,x,ls->lower,ls->upper,s);CHKERRQ(ierr); 96a7e14dcfSSatish Balay ierr = VecScale(s,-1.0);CHKERRQ(ierr); 97e270355aSBarry Smith ierr = VecStepBoundInfo(x,s,ls->lower,ls->upper,&bstepmin1,&bstepmin2,&bstepmax);CHKERRQ(ierr); 98a7e14dcfSSatish Balay ls->stepmax = PetscMin(bstepmax,1.0e15); 99a7e14dcfSSatish Balay } 100a7e14dcfSSatish Balay 101302440fdSBarry Smith ierr = VecDot(g,s,&dginit);CHKERRQ(ierr); 102a7e14dcfSSatish Balay if (PetscIsInfOrNanReal(dginit)) { 103f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Initial Line Search step * g is Inf or Nan (%g)\n",(double)dginit);CHKERRQ(ierr); 104a7e14dcfSSatish Balay ls->reason=TAOLINESEARCH_FAILED_INFORNAN; 105a7e14dcfSSatish Balay PetscFunctionReturn(0); 106a7e14dcfSSatish Balay } 107a7e14dcfSSatish Balay if (dginit >= 0.0) { 108f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Initial Line Search step * g is not descent direction (%g)\n",(double)dginit);CHKERRQ(ierr); 109a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_FAILED_ASCENT; 110a7e14dcfSSatish Balay PetscFunctionReturn(0); 111a7e14dcfSSatish Balay } 112a7e14dcfSSatish Balay 113a7e14dcfSSatish Balay /* Initialization */ 114a7e14dcfSSatish Balay mt->bracket = 0; 115a7e14dcfSSatish Balay stage1 = 1; 116a7e14dcfSSatish Balay finit = *f; 117a7e14dcfSSatish Balay dgtest = ls->ftol * dginit; 118a7e14dcfSSatish Balay width = ls->stepmax - ls->stepmin; 119a7e14dcfSSatish Balay width1 = width * 2.0; 120a7e14dcfSSatish Balay ierr = VecCopy(x,mt->work);CHKERRQ(ierr); 121a7e14dcfSSatish Balay /* Variable dictionary: 122a7e14dcfSSatish Balay stx, fx, dgx - the step, function, and derivative at the best step 123a7e14dcfSSatish Balay sty, fy, dgy - the step, function, and derivative at the other endpoint 124a7e14dcfSSatish Balay of the interval of uncertainty 125a7e14dcfSSatish Balay step, f, dg - the step, function, and derivative at the current step */ 126a7e14dcfSSatish Balay 127a7e14dcfSSatish Balay stx = 0.0; 128a7e14dcfSSatish Balay fx = finit; 129a7e14dcfSSatish Balay dgx = dginit; 130a7e14dcfSSatish Balay sty = 0.0; 131a7e14dcfSSatish Balay fy = finit; 132a7e14dcfSSatish Balay dgy = dginit; 133a7e14dcfSSatish Balay 134a7e14dcfSSatish Balay ls->step=ls->initstep; 135a7e14dcfSSatish Balay for (i=0; i< ls->max_funcs; i++) { 136a7e14dcfSSatish Balay /* Set min and max steps to correspond to the interval of uncertainty */ 137a7e14dcfSSatish Balay if (mt->bracket) { 138a7e14dcfSSatish Balay ls->stepmin = PetscMin(stx,sty); 139a7e14dcfSSatish Balay ls->stepmax = PetscMax(stx,sty); 14053506e15SBarry Smith } else { 141a7e14dcfSSatish Balay ls->stepmin = stx; 142a7e14dcfSSatish Balay ls->stepmax = ls->step + xtrapf * (ls->step - stx); 143a7e14dcfSSatish Balay } 144a7e14dcfSSatish Balay 145a7e14dcfSSatish Balay /* Force the step to be within the bounds */ 146a7e14dcfSSatish Balay ls->step = PetscMax(ls->step,ls->stepmin); 147a7e14dcfSSatish Balay ls->step = PetscMin(ls->step,ls->stepmax); 148a7e14dcfSSatish Balay 149a7e14dcfSSatish Balay /* If an unusual termination is to occur, then let step be the lowest 150a7e14dcfSSatish Balay point obtained thus far */ 15153506e15SBarry Smith if ((stx!=0) && (((mt->bracket) && (ls->step <= ls->stepmin || ls->step >= ls->stepmax)) || ((mt->bracket) && (ls->stepmax - ls->stepmin <= ls->rtol * ls->stepmax)) || 152a7e14dcfSSatish Balay ((ls->nfeval+ls->nfgeval) >= ls->max_funcs - 1) || (mt->infoc == 0))) { 153a7e14dcfSSatish Balay ls->step = stx; 154a7e14dcfSSatish Balay } 155a7e14dcfSSatish Balay 156a7e14dcfSSatish Balay ierr = VecCopy(x,mt->work);CHKERRQ(ierr); 157a7e14dcfSSatish Balay ierr = VecAXPY(mt->work,ls->step,s);CHKERRQ(ierr); /* W = X + step*S */ 158a7e14dcfSSatish Balay 159a7e14dcfSSatish Balay if (ls->bounded) { 160a7e14dcfSSatish Balay ierr = VecMedian(ls->lower, mt->work, ls->upper, mt->work);CHKERRQ(ierr); 161a7e14dcfSSatish Balay } 162a7e14dcfSSatish Balay if (ls->usegts) { 163a7e14dcfSSatish Balay ierr = TaoLineSearchComputeObjectiveAndGTS(ls,mt->work,f,&dg);CHKERRQ(ierr); 164a7e14dcfSSatish Balay g_computed=PETSC_FALSE; 165a7e14dcfSSatish Balay } else { 166a7e14dcfSSatish Balay ierr = TaoLineSearchComputeObjectiveAndGradient(ls,mt->work,f,g);CHKERRQ(ierr); 167a7e14dcfSSatish Balay g_computed=PETSC_TRUE; 168a7e14dcfSSatish Balay if (ls->bounded) { 169a7e14dcfSSatish Balay ierr = VecDot(g,x,&dg);CHKERRQ(ierr); 170a7e14dcfSSatish Balay ierr = VecDot(g,mt->work,&dg2);CHKERRQ(ierr); 171a7e14dcfSSatish Balay dg = (dg2 - dg)/ls->step; 172a7e14dcfSSatish Balay } else { 173a7e14dcfSSatish Balay ierr = VecDot(g,s,&dg);CHKERRQ(ierr); 174a7e14dcfSSatish Balay } 175a7e14dcfSSatish Balay } 176a7e14dcfSSatish Balay 177e7709889SAlp Dener /* update bracketing parameters in the MT context for printouts in monitor */ 1782a0dac07SAlp Dener mt->stx = stx; 1792a0dac07SAlp Dener mt->fx = fx; 1802a0dac07SAlp Dener mt->dgx = dgx; 1812a0dac07SAlp Dener mt->sty = sty; 1822a0dac07SAlp Dener mt->fy = fy; 1832a0dac07SAlp Dener mt->dgy = dgy; 1842a0dac07SAlp Dener ierr = TaoLineSearchMonitor(ls, i+1, *f, ls->step);CHKERRQ(ierr); 1852a0dac07SAlp Dener 186a7e14dcfSSatish Balay if (0 == i) { 187a7e14dcfSSatish Balay ls->f_fullstep=*f; 188a7e14dcfSSatish Balay } 189a7e14dcfSSatish Balay 190a7e14dcfSSatish Balay if (PetscIsInfOrNanReal(*f) || PetscIsInfOrNanReal(dg)) { 191a7e14dcfSSatish Balay /* User provided compute function generated Not-a-Number, assume 192a7e14dcfSSatish Balay domain violation and set function value and directional 193a7e14dcfSSatish Balay derivative to infinity. */ 194e270355aSBarry Smith *f = PETSC_INFINITY; 195e270355aSBarry Smith dg = PETSC_INFINITY; 196a7e14dcfSSatish Balay } 197a7e14dcfSSatish Balay 198a7e14dcfSSatish Balay ftest1 = finit + ls->step * dgtest; 199a7e14dcfSSatish Balay if (ls->bounded) { 200a7e14dcfSSatish Balay ftest2 = finit + ls->step * dgtest * ls->ftol; 201a7e14dcfSSatish Balay } 202a7e14dcfSSatish Balay /* Convergence testing */ 20353506e15SBarry Smith if (((*f - ftest1 <= 1.0e-10 * PetscAbsReal(finit)) && (PetscAbsReal(dg) + ls->gtol*dginit <= 0.0))) { 204a7e14dcfSSatish Balay ierr = PetscInfo(ls, "Line search success: Sufficient decrease and directional deriv conditions hold\n");CHKERRQ(ierr); 205a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_SUCCESS; 206a7e14dcfSSatish Balay break; 207a7e14dcfSSatish Balay } 208a7e14dcfSSatish Balay 209a7e14dcfSSatish Balay /* Check Armijo if beyond the first breakpoint */ 210a7e14dcfSSatish Balay if (ls->bounded && (*f <= ftest2) && (ls->step >= bstepmin2)) { 211a7e14dcfSSatish Balay ierr = PetscInfo(ls,"Line search success: Sufficient decrease.\n");CHKERRQ(ierr); 2124e6ef68fSJason Sarich ls->reason = TAOLINESEARCH_SUCCESS; 213a7e14dcfSSatish Balay break; 214a7e14dcfSSatish Balay } 215a7e14dcfSSatish Balay 216a7e14dcfSSatish Balay /* Checks for bad cases */ 217a7e14dcfSSatish Balay if (((mt->bracket) && (ls->step <= ls->stepmin||ls->step >= ls->stepmax)) || (!mt->infoc)) { 218a7e14dcfSSatish Balay ierr = PetscInfo(ls,"Rounding errors may prevent further progress. May not be a step satisfying\n");CHKERRQ(ierr); 219a7e14dcfSSatish Balay ierr = PetscInfo(ls,"sufficient decrease and curvature conditions. Tolerances may be too small.\n");CHKERRQ(ierr); 220a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_OTHER; 221a7e14dcfSSatish Balay break; 222a7e14dcfSSatish Balay } 223a7e14dcfSSatish Balay if ((ls->step == ls->stepmax) && (*f <= ftest1) && (dg <= dgtest)) { 224f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Step is at the upper bound, stepmax (%g)\n",(double)ls->stepmax);CHKERRQ(ierr); 225a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_UPPERBOUND; 226a7e14dcfSSatish Balay break; 227a7e14dcfSSatish Balay } 228a7e14dcfSSatish Balay if ((ls->step == ls->stepmin) && (*f >= ftest1) && (dg >= dgtest)) { 229f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Step is at the lower bound, stepmin (%g)\n",(double)ls->stepmin);CHKERRQ(ierr); 230a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_LOWERBOUND; 231a7e14dcfSSatish Balay break; 232a7e14dcfSSatish Balay } 233a7e14dcfSSatish Balay if ((mt->bracket) && (ls->stepmax - ls->stepmin <= ls->rtol*ls->stepmax)){ 234f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Relative width of interval of uncertainty is at most rtol (%g)\n",(double)ls->rtol);CHKERRQ(ierr); 235a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_RTOL; 236a7e14dcfSSatish Balay break; 237a7e14dcfSSatish Balay } 238a7e14dcfSSatish Balay 239a7e14dcfSSatish Balay /* In the first stage, we seek a step for which the modified function 240a7e14dcfSSatish Balay has a nonpositive value and nonnegative derivative */ 241a7e14dcfSSatish Balay if ((stage1) && (*f <= ftest1) && (dg >= dginit * PetscMin(ls->ftol, ls->gtol))) { 242a7e14dcfSSatish Balay stage1 = 0; 243a7e14dcfSSatish Balay } 244a7e14dcfSSatish Balay 245a7e14dcfSSatish Balay /* A modified function is used to predict the step only if we 246a7e14dcfSSatish Balay have not obtained a step for which the modified function has a 247a7e14dcfSSatish Balay nonpositive function value and nonnegative derivative, and if a 248a7e14dcfSSatish Balay lower function value has been obtained but the decrease is not 249a7e14dcfSSatish Balay sufficient */ 250a7e14dcfSSatish Balay 251a7e14dcfSSatish Balay if ((stage1) && (*f <= fx) && (*f > ftest1)) { 252a7e14dcfSSatish Balay fm = *f - ls->step * dgtest; /* Define modified function */ 253a7e14dcfSSatish Balay fxm = fx - stx * dgtest; /* and derivatives */ 254a7e14dcfSSatish Balay fym = fy - sty * dgtest; 255a7e14dcfSSatish Balay dgm = dg - dgtest; 256a7e14dcfSSatish Balay dgxm = dgx - dgtest; 257a7e14dcfSSatish Balay dgym = dgy - dgtest; 258a7e14dcfSSatish Balay 259a7e14dcfSSatish Balay /* if (dgxm * (ls->step - stx) >= 0.0) */ 260a7e14dcfSSatish Balay /* Update the interval of uncertainty and compute the new step */ 261a7e14dcfSSatish Balay ierr = Tao_mcstep(ls,&stx,&fxm,&dgxm,&sty,&fym,&dgym,&ls->step,&fm,&dgm);CHKERRQ(ierr); 262a7e14dcfSSatish Balay 263a7e14dcfSSatish Balay fx = fxm + stx * dgtest; /* Reset the function and */ 264a7e14dcfSSatish Balay fy = fym + sty * dgtest; /* gradient values */ 265a7e14dcfSSatish Balay dgx = dgxm + dgtest; 266a7e14dcfSSatish Balay dgy = dgym + dgtest; 26753506e15SBarry Smith } else { 268a7e14dcfSSatish Balay /* Update the interval of uncertainty and compute the new step */ 269a7e14dcfSSatish Balay ierr = Tao_mcstep(ls,&stx,&fx,&dgx,&sty,&fy,&dgy,&ls->step,f,&dg);CHKERRQ(ierr); 270a7e14dcfSSatish Balay } 271a7e14dcfSSatish Balay 272a7e14dcfSSatish Balay /* Force a sufficient decrease in the interval of uncertainty */ 273a7e14dcfSSatish Balay if (mt->bracket) { 274a7e14dcfSSatish Balay if (PetscAbsReal(sty - stx) >= 0.66 * width1) ls->step = stx + 0.5*(sty - stx); 275a7e14dcfSSatish Balay width1 = width; 276a7e14dcfSSatish Balay width = PetscAbsReal(sty - stx); 277a7e14dcfSSatish Balay } 278a7e14dcfSSatish Balay } 279a7e14dcfSSatish Balay if ((ls->nfeval+ls->nfgeval) > ls->max_funcs) { 280a7e14dcfSSatish Balay ierr = PetscInfo2(ls,"Number of line search function evals (%D) > maximum (%D)\n",(ls->nfeval+ls->nfgeval),ls->max_funcs);CHKERRQ(ierr); 281a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_MAXFCN; 282a7e14dcfSSatish Balay } 283a7e14dcfSSatish Balay 284a7e14dcfSSatish Balay /* Finish computations */ 285f06e3bfaSBarry Smith ierr = PetscInfo2(ls,"%D function evals in line search, step = %g\n",(ls->nfeval+ls->nfgeval),(double)ls->step);CHKERRQ(ierr); 286a7e14dcfSSatish Balay 287a7e14dcfSSatish Balay /* Set new solution vector and compute gradient if needed */ 288a7e14dcfSSatish Balay ierr = VecCopy(mt->work,x);CHKERRQ(ierr); 289a7e14dcfSSatish Balay if (!g_computed) { 290a7e14dcfSSatish Balay ierr = TaoLineSearchComputeGradient(ls,mt->work,g);CHKERRQ(ierr); 291a7e14dcfSSatish Balay } 292a7e14dcfSSatish Balay PetscFunctionReturn(0); 293a7e14dcfSSatish Balay } 294a7e14dcfSSatish Balay 29590b6438dSAlp Dener /*MC 29690b6438dSAlp Dener TAOLINESEARCHMT - Line-search type with cubic interpolation that satisfies both the sufficient decrease and 2974c991b12SBarryFSmith curvature conditions. This method can take step lengths greater than 1. 29890b6438dSAlp Dener 29990b6438dSAlp Dener More-Thuente line-search can be selected with "-tao_ls_type more-thuente". 30090b6438dSAlp Dener 30190b6438dSAlp Dener References: 30290b6438dSAlp Dener . 1. - JORGE J. MORE AND DAVID J. THUENTE, LINE SEARCH ALGORITHMS WITH GUARANTEED SUFFICIENT DECREASE. 30390b6438dSAlp Dener ACM Trans. Math. Software 20, no. 3 (1994): 286-307. 30490b6438dSAlp Dener 30590b6438dSAlp Dener Level: developer 30690b6438dSAlp Dener 30790b6438dSAlp Dener .seealso: TaoLineSearchCreate(), TaoLineSearchSetType(), TaoLineSearchApply() 30890b6438dSAlp Dener 30990b6438dSAlp Dener .keywords: Tao, linesearch 31090b6438dSAlp Dener M*/ 311728e0ed0SBarry Smith PETSC_EXTERN PetscErrorCode TaoLineSearchCreate_MT(TaoLineSearch ls) 312a7e14dcfSSatish Balay { 313a7e14dcfSSatish Balay PetscErrorCode ierr; 3148caf6e8cSBarry Smith TaoLineSearch_MT *ctx; 31553506e15SBarry Smith 316a7e14dcfSSatish Balay PetscFunctionBegin; 317a7e14dcfSSatish Balay PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); 3183c9e27cfSGeoffrey Irving ierr = PetscNewLog(ls,&ctx);CHKERRQ(ierr); 319a7e14dcfSSatish Balay ctx->bracket=0; 320a7e14dcfSSatish Balay ctx->infoc=1; 321a7e14dcfSSatish Balay ls->data = (void*)ctx; 322a7e14dcfSSatish Balay ls->initstep = 1.0; 323*83c8fe1dSLisandro Dalcin ls->ops->setup = NULL; 324*83c8fe1dSLisandro Dalcin ls->ops->reset = NULL; 325a7e14dcfSSatish Balay ls->ops->apply = TaoLineSearchApply_MT; 326a7e14dcfSSatish Balay ls->ops->destroy = TaoLineSearchDestroy_MT; 327a7e14dcfSSatish Balay ls->ops->setfromoptions = TaoLineSearchSetFromOptions_MT; 3282a0dac07SAlp Dener ls->ops->monitor = TaoLineSearchMonitor_MT; 329a7e14dcfSSatish Balay PetscFunctionReturn(0); 330a7e14dcfSSatish Balay } 331a7e14dcfSSatish Balay 332a7e14dcfSSatish Balay /* 333a7e14dcfSSatish Balay The subroutine mcstep is taken from the work of Jorge Nocedal. 334a7e14dcfSSatish Balay this is a variant of More' and Thuente's routine. 335a7e14dcfSSatish Balay 336a7e14dcfSSatish Balay subroutine mcstep 337a7e14dcfSSatish Balay 338a7e14dcfSSatish Balay the purpose of mcstep is to compute a safeguarded step for 339a7e14dcfSSatish Balay a linesearch and to update an interval of uncertainty for 340a7e14dcfSSatish Balay a minimizer of the function. 341a7e14dcfSSatish Balay 342a7e14dcfSSatish Balay the parameter stx contains the step with the least function 343a7e14dcfSSatish Balay value. the parameter stp contains the current step. it is 344a7e14dcfSSatish Balay assumed that the derivative at stx is negative in the 345a7e14dcfSSatish Balay direction of the step. if bracket is set true then a 346a7e14dcfSSatish Balay minimizer has been bracketed in an interval of uncertainty 347a7e14dcfSSatish Balay with endpoints stx and sty. 348a7e14dcfSSatish Balay 349a7e14dcfSSatish Balay the subroutine statement is 350a7e14dcfSSatish Balay 351a7e14dcfSSatish Balay subroutine mcstep(stx,fx,dx,sty,fy,dy,stp,fp,dp,bracket, 352a7e14dcfSSatish Balay stpmin,stpmax,info) 353a7e14dcfSSatish Balay 354a7e14dcfSSatish Balay where 355a7e14dcfSSatish Balay 356a7e14dcfSSatish Balay stx, fx, and dx are variables which specify the step, 357a7e14dcfSSatish Balay the function, and the derivative at the best step obtained 358a7e14dcfSSatish Balay so far. The derivative must be negative in the direction 359a7e14dcfSSatish Balay of the step, that is, dx and stp-stx must have opposite 360a7e14dcfSSatish Balay signs. On output these parameters are updated appropriately. 361a7e14dcfSSatish Balay 362a7e14dcfSSatish Balay sty, fy, and dy are variables which specify the step, 363a7e14dcfSSatish Balay the function, and the derivative at the other endpoint of 364a7e14dcfSSatish Balay the interval of uncertainty. On output these parameters are 365a7e14dcfSSatish Balay updated appropriately. 366a7e14dcfSSatish Balay 367a7e14dcfSSatish Balay stp, fp, and dp are variables which specify the step, 368a7e14dcfSSatish Balay the function, and the derivative at the current step. 369a7e14dcfSSatish Balay If bracket is set true then on input stp must be 370a7e14dcfSSatish Balay between stx and sty. On output stp is set to the new step. 371a7e14dcfSSatish Balay 372a7e14dcfSSatish Balay bracket is a logical variable which specifies if a minimizer 373a7e14dcfSSatish Balay has been bracketed. If the minimizer has not been bracketed 374a7e14dcfSSatish Balay then on input bracket must be set false. If the minimizer 375a7e14dcfSSatish Balay is bracketed then on output bracket is set true. 376a7e14dcfSSatish Balay 377a7e14dcfSSatish Balay stpmin and stpmax are input variables which specify lower 378a7e14dcfSSatish Balay and upper bounds for the step. 379a7e14dcfSSatish Balay 380a7e14dcfSSatish Balay info is an integer output variable set as follows: 381a7e14dcfSSatish Balay if info = 1,2,3,4,5, then the step has been computed 382a7e14dcfSSatish Balay according to one of the five cases below. otherwise 383a7e14dcfSSatish Balay info = 0, and this indicates improper input parameters. 384a7e14dcfSSatish Balay 385a7e14dcfSSatish Balay subprograms called 386a7e14dcfSSatish Balay 387a7e14dcfSSatish Balay fortran-supplied ... abs,max,min,sqrt 388a7e14dcfSSatish Balay 389a7e14dcfSSatish Balay argonne national laboratory. minpack project. june 1983 390a7e14dcfSSatish Balay jorge j. more', david j. thuente 391a7e14dcfSSatish Balay 392a7e14dcfSSatish Balay */ 393a7e14dcfSSatish Balay 39453506e15SBarry Smith static PetscErrorCode Tao_mcstep(TaoLineSearch ls,PetscReal *stx,PetscReal *fx,PetscReal *dx,PetscReal *sty,PetscReal *fy,PetscReal *dy,PetscReal *stp,PetscReal *fp,PetscReal *dp) 395a7e14dcfSSatish Balay { 3968caf6e8cSBarry Smith TaoLineSearch_MT *mtP = (TaoLineSearch_MT *) ls->data; 397a7e14dcfSSatish Balay PetscReal gamma1, p, q, r, s, sgnd, stpc, stpf, stpq, theta; 398a7e14dcfSSatish Balay PetscInt bound; 399a7e14dcfSSatish Balay 400a7e14dcfSSatish Balay PetscFunctionBegin; 401a7e14dcfSSatish Balay /* Check the input parameters for errors */ 402a7e14dcfSSatish Balay mtP->infoc = 0; 403691b26d3SBarry Smith if (mtP->bracket && (*stp <= PetscMin(*stx,*sty) || (*stp >= PetscMax(*stx,*sty)))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"bad stp in bracket"); 404691b26d3SBarry Smith if (*dx * (*stp-*stx) >= 0.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"dx * (stp-stx) >= 0.0"); 405691b26d3SBarry Smith if (ls->stepmax < ls->stepmin) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"stepmax > stepmin"); 406a7e14dcfSSatish Balay 407a7e14dcfSSatish Balay /* Determine if the derivatives have opposite sign */ 408a7e14dcfSSatish Balay sgnd = *dp * (*dx / PetscAbsReal(*dx)); 409a7e14dcfSSatish Balay 410a7e14dcfSSatish Balay if (*fp > *fx) { 411a7e14dcfSSatish Balay /* Case 1: a higher function value. 412a7e14dcfSSatish Balay The minimum is bracketed. If the cubic step is closer 413a7e14dcfSSatish Balay to stx than the quadratic step, the cubic step is taken, 414a7e14dcfSSatish Balay else the average of the cubic and quadratic steps is taken. */ 415a7e14dcfSSatish Balay 416a7e14dcfSSatish Balay mtP->infoc = 1; 417a7e14dcfSSatish Balay bound = 1; 418a7e14dcfSSatish Balay theta = 3 * (*fx - *fp) / (*stp - *stx) + *dx + *dp; 419a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dx)); 420a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 421a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscPowScalar(theta/s,2.0) - (*dx/s)*(*dp/s)); 422a7e14dcfSSatish Balay if (*stp < *stx) gamma1 = -gamma1; 423a7e14dcfSSatish Balay /* Can p be 0? Check */ 424a7e14dcfSSatish Balay p = (gamma1 - *dx) + theta; 425a7e14dcfSSatish Balay q = ((gamma1 - *dx) + gamma1) + *dp; 426a7e14dcfSSatish Balay r = p/q; 427a7e14dcfSSatish Balay stpc = *stx + r*(*stp - *stx); 428a7e14dcfSSatish Balay stpq = *stx + ((*dx/((*fx-*fp)/(*stp-*stx)+*dx))*0.5) * (*stp - *stx); 429a7e14dcfSSatish Balay 430a7e14dcfSSatish Balay if (PetscAbsReal(stpc-*stx) < PetscAbsReal(stpq-*stx)) { 431a7e14dcfSSatish Balay stpf = stpc; 43253506e15SBarry Smith } else { 433a7e14dcfSSatish Balay stpf = stpc + 0.5*(stpq - stpc); 434a7e14dcfSSatish Balay } 435a7e14dcfSSatish Balay mtP->bracket = 1; 43653506e15SBarry Smith } else if (sgnd < 0.0) { 437a7e14dcfSSatish Balay /* Case 2: A lower function value and derivatives of 438a7e14dcfSSatish Balay opposite sign. The minimum is bracketed. If the cubic 439a7e14dcfSSatish Balay step is closer to stx than the quadratic (secant) step, 440a7e14dcfSSatish Balay the cubic step is taken, else the quadratic step is taken. */ 441a7e14dcfSSatish Balay 442a7e14dcfSSatish Balay mtP->infoc = 2; 443a7e14dcfSSatish Balay bound = 0; 444a7e14dcfSSatish Balay theta = 3*(*fx - *fp)/(*stp - *stx) + *dx + *dp; 445a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dx)); 446a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 447a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscPowScalar(theta/s,2.0) - (*dx/s)*(*dp/s)); 448a7e14dcfSSatish Balay if (*stp > *stx) gamma1 = -gamma1; 449a7e14dcfSSatish Balay p = (gamma1 - *dp) + theta; 450a7e14dcfSSatish Balay q = ((gamma1 - *dp) + gamma1) + *dx; 451a7e14dcfSSatish Balay r = p/q; 452a7e14dcfSSatish Balay stpc = *stp + r*(*stx - *stp); 453a7e14dcfSSatish Balay stpq = *stp + (*dp/(*dp-*dx))*(*stx - *stp); 454a7e14dcfSSatish Balay 455a7e14dcfSSatish Balay if (PetscAbsReal(stpc-*stp) > PetscAbsReal(stpq-*stp)) { 456a7e14dcfSSatish Balay stpf = stpc; 45753506e15SBarry Smith } else { 458a7e14dcfSSatish Balay stpf = stpq; 459a7e14dcfSSatish Balay } 460a7e14dcfSSatish Balay mtP->bracket = 1; 46153506e15SBarry Smith } else if (PetscAbsReal(*dp) < PetscAbsReal(*dx)) { 462a7e14dcfSSatish Balay /* Case 3: A lower function value, derivatives of the 463a7e14dcfSSatish Balay same sign, and the magnitude of the derivative decreases. 464a7e14dcfSSatish Balay The cubic step is only used if the cubic tends to infinity 465a7e14dcfSSatish Balay in the direction of the step or if the minimum of the cubic 466a7e14dcfSSatish Balay is beyond stp. Otherwise the cubic step is defined to be 467a7e14dcfSSatish Balay either stepmin or stepmax. The quadratic (secant) step is also 468df3898eeSBarry Smith computed and if the minimum is bracketed then the step 469a7e14dcfSSatish Balay closest to stx is taken, else the step farthest away is taken. */ 470a7e14dcfSSatish Balay 471a7e14dcfSSatish Balay mtP->infoc = 3; 472a7e14dcfSSatish Balay bound = 1; 473a7e14dcfSSatish Balay theta = 3*(*fx - *fp)/(*stp - *stx) + *dx + *dp; 474a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dx)); 475a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 476a7e14dcfSSatish Balay 477a7e14dcfSSatish Balay /* The case gamma1 = 0 only arises if the cubic does not tend 478a7e14dcfSSatish Balay to infinity in the direction of the step. */ 479a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscMax(0.0,PetscPowScalar(theta/s,2.0) - (*dx/s)*(*dp/s))); 480a7e14dcfSSatish Balay if (*stp > *stx) gamma1 = -gamma1; 481a7e14dcfSSatish Balay p = (gamma1 - *dp) + theta; 482a7e14dcfSSatish Balay q = (gamma1 + (*dx - *dp)) + gamma1; 483a7e14dcfSSatish Balay r = p/q; 484a7e14dcfSSatish Balay if (r < 0.0 && gamma1 != 0.0) stpc = *stp + r*(*stx - *stp); 485a7e14dcfSSatish Balay else if (*stp > *stx) stpc = ls->stepmax; 486a7e14dcfSSatish Balay else stpc = ls->stepmin; 487a7e14dcfSSatish Balay stpq = *stp + (*dp/(*dp-*dx)) * (*stx - *stp); 488a7e14dcfSSatish Balay 489a7e14dcfSSatish Balay if (mtP->bracket) { 490a7e14dcfSSatish Balay if (PetscAbsReal(*stp-stpc) < PetscAbsReal(*stp-stpq)) { 491a7e14dcfSSatish Balay stpf = stpc; 49253506e15SBarry Smith } else { 493a7e14dcfSSatish Balay stpf = stpq; 494a7e14dcfSSatish Balay } 49553506e15SBarry Smith } else { 496a7e14dcfSSatish Balay if (PetscAbsReal(*stp-stpc) > PetscAbsReal(*stp-stpq)) { 497a7e14dcfSSatish Balay stpf = stpc; 49853506e15SBarry Smith } else { 499a7e14dcfSSatish Balay stpf = stpq; 500a7e14dcfSSatish Balay } 501a7e14dcfSSatish Balay } 50253506e15SBarry Smith } else { 503a7e14dcfSSatish Balay /* Case 4: A lower function value, derivatives of the 504a7e14dcfSSatish Balay same sign, and the magnitude of the derivative does 505a7e14dcfSSatish Balay not decrease. If the minimum is not bracketed, the step 506a7e14dcfSSatish Balay is either stpmin or stpmax, else the cubic step is taken. */ 507a7e14dcfSSatish Balay 508a7e14dcfSSatish Balay mtP->infoc = 4; 509a7e14dcfSSatish Balay bound = 0; 510a7e14dcfSSatish Balay if (mtP->bracket) { 511a7e14dcfSSatish Balay theta = 3*(*fp - *fy)/(*sty - *stp) + *dy + *dp; 512a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dy)); 513a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 514a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscPowScalar(theta/s,2.0) - (*dy/s)*(*dp/s)); 515a7e14dcfSSatish Balay if (*stp > *sty) gamma1 = -gamma1; 516a7e14dcfSSatish Balay p = (gamma1 - *dp) + theta; 517a7e14dcfSSatish Balay q = ((gamma1 - *dp) + gamma1) + *dy; 518a7e14dcfSSatish Balay r = p/q; 519a7e14dcfSSatish Balay stpc = *stp + r*(*sty - *stp); 520a7e14dcfSSatish Balay stpf = stpc; 52153506e15SBarry Smith } else if (*stp > *stx) { 522a7e14dcfSSatish Balay stpf = ls->stepmax; 52353506e15SBarry Smith } else { 524a7e14dcfSSatish Balay stpf = ls->stepmin; 525a7e14dcfSSatish Balay } 526a7e14dcfSSatish Balay } 527a7e14dcfSSatish Balay 528a7e14dcfSSatish Balay /* Update the interval of uncertainty. This update does not 529a7e14dcfSSatish Balay depend on the new step or the case analysis above. */ 530a7e14dcfSSatish Balay 531a7e14dcfSSatish Balay if (*fp > *fx) { 532a7e14dcfSSatish Balay *sty = *stp; 533a7e14dcfSSatish Balay *fy = *fp; 534a7e14dcfSSatish Balay *dy = *dp; 53553506e15SBarry Smith } else { 536a7e14dcfSSatish Balay if (sgnd < 0.0) { 537a7e14dcfSSatish Balay *sty = *stx; 538a7e14dcfSSatish Balay *fy = *fx; 539a7e14dcfSSatish Balay *dy = *dx; 540a7e14dcfSSatish Balay } 541a7e14dcfSSatish Balay *stx = *stp; 542a7e14dcfSSatish Balay *fx = *fp; 543a7e14dcfSSatish Balay *dx = *dp; 544a7e14dcfSSatish Balay } 545a7e14dcfSSatish Balay 546a7e14dcfSSatish Balay /* Compute the new step and safeguard it. */ 547a7e14dcfSSatish Balay stpf = PetscMin(ls->stepmax,stpf); 548a7e14dcfSSatish Balay stpf = PetscMax(ls->stepmin,stpf); 549a7e14dcfSSatish Balay *stp = stpf; 550a7e14dcfSSatish Balay if (mtP->bracket && bound) { 551a7e14dcfSSatish Balay if (*sty > *stx) { 552a7e14dcfSSatish Balay *stp = PetscMin(*stx+0.66*(*sty-*stx),*stp); 55353506e15SBarry Smith } else { 554a7e14dcfSSatish Balay *stp = PetscMax(*stx+0.66*(*sty-*stx),*stp); 555a7e14dcfSSatish Balay } 556a7e14dcfSSatish Balay } 557a7e14dcfSSatish Balay PetscFunctionReturn(0); 558a7e14dcfSSatish Balay } 559