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 35*2a0dac07SAlp Dener static PetscErrorCode TaoLineSearchMonitor_MT(TaoLineSearch ls) 36*2a0dac07SAlp Dener { 37*2a0dac07SAlp Dener TaoLineSearch_MT *mt = (TaoLineSearch_MT*)ls->data; 38*2a0dac07SAlp Dener PetscErrorCode ierr; 39*2a0dac07SAlp Dener 40*2a0dac07SAlp Dener PetscFunctionBegin; 41*2a0dac07SAlp Dener ierr = PetscViewerASCIIPrintf(ls->viewer, "stx: %g, fx: %g, dgx: %g\n", (double)mt->stx, (double)mt->fx, (double)mt->dgx);CHKERRQ(ierr); 42*2a0dac07SAlp Dener ierr = PetscViewerASCIIPrintf(ls->viewer, "sty: %g, fy: %g, dgy: %g\n", (double)mt->sty, (double)mt->fy, (double)mt->dgy);CHKERRQ(ierr); 43*2a0dac07SAlp Dener PetscFunctionReturn(0); 44*2a0dac07SAlp Dener } 45a7e14dcfSSatish Balay 46a7e14dcfSSatish Balay /* @ TaoApply_LineSearch - This routine takes step length of 1.0. 47a7e14dcfSSatish Balay 48a7e14dcfSSatish Balay Input Parameters: 49441846f8SBarry Smith + tao - Tao context 50a7e14dcfSSatish Balay . X - current iterate (on output X contains new iterate, X + step*S) 51a7e14dcfSSatish Balay . f - objective function evaluated at X 52a7e14dcfSSatish Balay . G - gradient evaluated at X 53a7e14dcfSSatish Balay - D - search direction 54a7e14dcfSSatish Balay 55a7e14dcfSSatish Balay 56a7e14dcfSSatish Balay Info is set to 0. 57a7e14dcfSSatish Balay 58a7e14dcfSSatish Balay @ */ 59a7e14dcfSSatish Balay 60a7e14dcfSSatish Balay static PetscErrorCode TaoLineSearchApply_MT(TaoLineSearch ls, Vec x, PetscReal *f, Vec g, Vec s) 61a7e14dcfSSatish Balay { 62a7e14dcfSSatish Balay PetscErrorCode ierr; 638caf6e8cSBarry Smith TaoLineSearch_MT *mt; 64a7e14dcfSSatish Balay 65a7e14dcfSSatish Balay PetscReal xtrapf = 4.0; 66a7e14dcfSSatish Balay PetscReal finit, width, width1, dginit, fm, fxm, fym, dgm, dgxm, dgym; 67a7e14dcfSSatish Balay PetscReal dgx, dgy, dg, dg2, fx, fy, stx, sty, dgtest; 68a7e14dcfSSatish Balay PetscReal ftest1=0.0, ftest2=0.0; 69a7e14dcfSSatish Balay PetscInt i, stage1,n1,n2,nn1,nn2; 70a7e14dcfSSatish Balay PetscReal bstepmin1, bstepmin2, bstepmax; 7153506e15SBarry Smith PetscBool g_computed=PETSC_FALSE; /* to prevent extra gradient computation */ 72a7e14dcfSSatish Balay 73a7e14dcfSSatish Balay PetscFunctionBegin; 74a7e14dcfSSatish Balay PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); 75a7e14dcfSSatish Balay PetscValidHeaderSpecific(x,VEC_CLASSID,2); 76a7e14dcfSSatish Balay PetscValidScalarPointer(f,3); 77a7e14dcfSSatish Balay PetscValidHeaderSpecific(g,VEC_CLASSID,4); 78a7e14dcfSSatish Balay PetscValidHeaderSpecific(s,VEC_CLASSID,5); 79a7e14dcfSSatish Balay 80*2a0dac07SAlp Dener ierr = TaoLineSearchMonitor(ls, 0, *f, 0.0);CHKERRQ(ierr); 81*2a0dac07SAlp Dener 82a7e14dcfSSatish Balay /* comm,type,size checks are done in interface TaoLineSearchApply */ 838caf6e8cSBarry Smith mt = (TaoLineSearch_MT*)(ls->data); 84a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_CONTINUE_ITERATING; 85a7e14dcfSSatish Balay 86a7e14dcfSSatish Balay /* Check work vector */ 87a7e14dcfSSatish Balay if (!mt->work) { 88a7e14dcfSSatish Balay ierr = VecDuplicate(x,&mt->work);CHKERRQ(ierr); 89a7e14dcfSSatish Balay mt->x = x; 90a7e14dcfSSatish Balay ierr = PetscObjectReference((PetscObject)mt->x);CHKERRQ(ierr); 9153506e15SBarry Smith } else if (x != mt->x) { 92a7e14dcfSSatish Balay ierr = VecDestroy(&mt->work);CHKERRQ(ierr); 93a7e14dcfSSatish Balay ierr = VecDuplicate(x,&mt->work);CHKERRQ(ierr); 94a7e14dcfSSatish Balay ierr = PetscObjectDereference((PetscObject)mt->x);CHKERRQ(ierr); 95a7e14dcfSSatish Balay mt->x = x; 96a7e14dcfSSatish Balay ierr = PetscObjectReference((PetscObject)mt->x);CHKERRQ(ierr); 97a7e14dcfSSatish Balay } 98a7e14dcfSSatish Balay 99a7e14dcfSSatish Balay if (ls->bounded) { 100a7e14dcfSSatish Balay /* Compute step length needed to make all variables equal a bound */ 101a7e14dcfSSatish Balay /* Compute the smallest steplength that will make one nonbinding variable 102a7e14dcfSSatish Balay equal the bound */ 103a7e14dcfSSatish Balay ierr = VecGetLocalSize(ls->upper,&n1);CHKERRQ(ierr); 104a7e14dcfSSatish Balay ierr = VecGetLocalSize(mt->x, &n2);CHKERRQ(ierr); 105a7e14dcfSSatish Balay ierr = VecGetSize(ls->upper,&nn1);CHKERRQ(ierr); 106a7e14dcfSSatish Balay ierr = VecGetSize(mt->x,&nn2);CHKERRQ(ierr); 10753506e15SBarry Smith if (n1 != n2 || nn1 != nn2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Variable vector not compatible with bounds vector"); 108a7e14dcfSSatish Balay ierr = VecScale(s,-1.0);CHKERRQ(ierr); 109a7e14dcfSSatish Balay ierr = VecBoundGradientProjection(s,x,ls->lower,ls->upper,s);CHKERRQ(ierr); 110a7e14dcfSSatish Balay ierr = VecScale(s,-1.0);CHKERRQ(ierr); 111e270355aSBarry Smith ierr = VecStepBoundInfo(x,s,ls->lower,ls->upper,&bstepmin1,&bstepmin2,&bstepmax);CHKERRQ(ierr); 112a7e14dcfSSatish Balay ls->stepmax = PetscMin(bstepmax,1.0e15); 113a7e14dcfSSatish Balay } 114a7e14dcfSSatish Balay 115302440fdSBarry Smith ierr = VecDot(g,s,&dginit);CHKERRQ(ierr); 116a7e14dcfSSatish Balay if (PetscIsInfOrNanReal(dginit)) { 117f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Initial Line Search step * g is Inf or Nan (%g)\n",(double)dginit);CHKERRQ(ierr); 118a7e14dcfSSatish Balay ls->reason=TAOLINESEARCH_FAILED_INFORNAN; 119a7e14dcfSSatish Balay PetscFunctionReturn(0); 120a7e14dcfSSatish Balay } 121a7e14dcfSSatish Balay if (dginit >= 0.0) { 122f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Initial Line Search step * g is not descent direction (%g)\n",(double)dginit);CHKERRQ(ierr); 123a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_FAILED_ASCENT; 124a7e14dcfSSatish Balay PetscFunctionReturn(0); 125a7e14dcfSSatish Balay } 126a7e14dcfSSatish Balay 127a7e14dcfSSatish Balay /* Initialization */ 128a7e14dcfSSatish Balay mt->bracket = 0; 129a7e14dcfSSatish Balay stage1 = 1; 130a7e14dcfSSatish Balay finit = *f; 131a7e14dcfSSatish Balay dgtest = ls->ftol * dginit; 132a7e14dcfSSatish Balay width = ls->stepmax - ls->stepmin; 133a7e14dcfSSatish Balay width1 = width * 2.0; 134a7e14dcfSSatish Balay ierr = VecCopy(x,mt->work);CHKERRQ(ierr); 135a7e14dcfSSatish Balay /* Variable dictionary: 136a7e14dcfSSatish Balay stx, fx, dgx - the step, function, and derivative at the best step 137a7e14dcfSSatish Balay sty, fy, dgy - the step, function, and derivative at the other endpoint 138a7e14dcfSSatish Balay of the interval of uncertainty 139a7e14dcfSSatish Balay step, f, dg - the step, function, and derivative at the current step */ 140a7e14dcfSSatish Balay 141a7e14dcfSSatish Balay stx = 0.0; 142a7e14dcfSSatish Balay fx = finit; 143a7e14dcfSSatish Balay dgx = dginit; 144a7e14dcfSSatish Balay sty = 0.0; 145a7e14dcfSSatish Balay fy = finit; 146a7e14dcfSSatish Balay dgy = dginit; 147a7e14dcfSSatish Balay 148a7e14dcfSSatish Balay ls->step=ls->initstep; 149a7e14dcfSSatish Balay for (i=0; i< ls->max_funcs; i++) { 150a7e14dcfSSatish Balay /* Set min and max steps to correspond to the interval of uncertainty */ 151a7e14dcfSSatish Balay if (mt->bracket) { 152a7e14dcfSSatish Balay ls->stepmin = PetscMin(stx,sty); 153a7e14dcfSSatish Balay ls->stepmax = PetscMax(stx,sty); 15453506e15SBarry Smith } else { 155a7e14dcfSSatish Balay ls->stepmin = stx; 156a7e14dcfSSatish Balay ls->stepmax = ls->step + xtrapf * (ls->step - stx); 157a7e14dcfSSatish Balay } 158a7e14dcfSSatish Balay 159a7e14dcfSSatish Balay /* Force the step to be within the bounds */ 160a7e14dcfSSatish Balay ls->step = PetscMax(ls->step,ls->stepmin); 161a7e14dcfSSatish Balay ls->step = PetscMin(ls->step,ls->stepmax); 162a7e14dcfSSatish Balay 163a7e14dcfSSatish Balay /* If an unusual termination is to occur, then let step be the lowest 164a7e14dcfSSatish Balay point obtained thus far */ 16553506e15SBarry Smith if ((stx!=0) && (((mt->bracket) && (ls->step <= ls->stepmin || ls->step >= ls->stepmax)) || ((mt->bracket) && (ls->stepmax - ls->stepmin <= ls->rtol * ls->stepmax)) || 166a7e14dcfSSatish Balay ((ls->nfeval+ls->nfgeval) >= ls->max_funcs - 1) || (mt->infoc == 0))) { 167a7e14dcfSSatish Balay ls->step = stx; 168a7e14dcfSSatish Balay } 169a7e14dcfSSatish Balay 170a7e14dcfSSatish Balay ierr = VecCopy(x,mt->work);CHKERRQ(ierr); 171a7e14dcfSSatish Balay ierr = VecAXPY(mt->work,ls->step,s);CHKERRQ(ierr); /* W = X + step*S */ 172a7e14dcfSSatish Balay 173a7e14dcfSSatish Balay if (ls->bounded) { 174a7e14dcfSSatish Balay ierr = VecMedian(ls->lower, mt->work, ls->upper, mt->work);CHKERRQ(ierr); 175a7e14dcfSSatish Balay } 176a7e14dcfSSatish Balay if (ls->usegts) { 177a7e14dcfSSatish Balay ierr = TaoLineSearchComputeObjectiveAndGTS(ls,mt->work,f,&dg);CHKERRQ(ierr); 178a7e14dcfSSatish Balay g_computed=PETSC_FALSE; 179a7e14dcfSSatish Balay } else { 180a7e14dcfSSatish Balay ierr = TaoLineSearchComputeObjectiveAndGradient(ls,mt->work,f,g);CHKERRQ(ierr); 181a7e14dcfSSatish Balay g_computed=PETSC_TRUE; 182a7e14dcfSSatish Balay if (ls->bounded) { 183a7e14dcfSSatish Balay ierr = VecDot(g,x,&dg);CHKERRQ(ierr); 184a7e14dcfSSatish Balay ierr = VecDot(g,mt->work,&dg2);CHKERRQ(ierr); 185a7e14dcfSSatish Balay dg = (dg2 - dg)/ls->step; 186a7e14dcfSSatish Balay } else { 187a7e14dcfSSatish Balay ierr = VecDot(g,s,&dg);CHKERRQ(ierr); 188a7e14dcfSSatish Balay } 189a7e14dcfSSatish Balay } 190a7e14dcfSSatish Balay 191*2a0dac07SAlp Dener /* Call the monitor */ 192*2a0dac07SAlp Dener mt->stx = stx; 193*2a0dac07SAlp Dener mt->fx = fx; 194*2a0dac07SAlp Dener mt->dgx = dgx; 195*2a0dac07SAlp Dener mt->sty = sty; 196*2a0dac07SAlp Dener mt->fy = fy; 197*2a0dac07SAlp Dener mt->dgy = dgy; 198*2a0dac07SAlp Dener ierr = TaoLineSearchMonitor(ls, i+1, *f, ls->step);CHKERRQ(ierr); 199*2a0dac07SAlp Dener 200a7e14dcfSSatish Balay if (0 == i) { 201a7e14dcfSSatish Balay ls->f_fullstep=*f; 202a7e14dcfSSatish Balay } 203a7e14dcfSSatish Balay 204a7e14dcfSSatish Balay if (PetscIsInfOrNanReal(*f) || PetscIsInfOrNanReal(dg)) { 205a7e14dcfSSatish Balay /* User provided compute function generated Not-a-Number, assume 206a7e14dcfSSatish Balay domain violation and set function value and directional 207a7e14dcfSSatish Balay derivative to infinity. */ 208e270355aSBarry Smith *f = PETSC_INFINITY; 209e270355aSBarry Smith dg = PETSC_INFINITY; 210a7e14dcfSSatish Balay } 211a7e14dcfSSatish Balay 212a7e14dcfSSatish Balay ftest1 = finit + ls->step * dgtest; 213a7e14dcfSSatish Balay if (ls->bounded) { 214a7e14dcfSSatish Balay ftest2 = finit + ls->step * dgtest * ls->ftol; 215a7e14dcfSSatish Balay } 216a7e14dcfSSatish Balay /* Convergence testing */ 21753506e15SBarry Smith if (((*f - ftest1 <= 1.0e-10 * PetscAbsReal(finit)) && (PetscAbsReal(dg) + ls->gtol*dginit <= 0.0))) { 218a7e14dcfSSatish Balay ierr = PetscInfo(ls, "Line search success: Sufficient decrease and directional deriv conditions hold\n");CHKERRQ(ierr); 219a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_SUCCESS; 220a7e14dcfSSatish Balay break; 221a7e14dcfSSatish Balay } 222a7e14dcfSSatish Balay 223a7e14dcfSSatish Balay /* Check Armijo if beyond the first breakpoint */ 224a7e14dcfSSatish Balay if (ls->bounded && (*f <= ftest2) && (ls->step >= bstepmin2)) { 225a7e14dcfSSatish Balay ierr = PetscInfo(ls,"Line search success: Sufficient decrease.\n");CHKERRQ(ierr); 2264e6ef68fSJason Sarich ls->reason = TAOLINESEARCH_SUCCESS; 227a7e14dcfSSatish Balay break; 228a7e14dcfSSatish Balay } 229a7e14dcfSSatish Balay 230a7e14dcfSSatish Balay /* Checks for bad cases */ 231a7e14dcfSSatish Balay if (((mt->bracket) && (ls->step <= ls->stepmin||ls->step >= ls->stepmax)) || (!mt->infoc)) { 232a7e14dcfSSatish Balay ierr = PetscInfo(ls,"Rounding errors may prevent further progress. May not be a step satisfying\n");CHKERRQ(ierr); 233a7e14dcfSSatish Balay ierr = PetscInfo(ls,"sufficient decrease and curvature conditions. Tolerances may be too small.\n");CHKERRQ(ierr); 234a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_OTHER; 235a7e14dcfSSatish Balay break; 236a7e14dcfSSatish Balay } 237a7e14dcfSSatish Balay if ((ls->step == ls->stepmax) && (*f <= ftest1) && (dg <= dgtest)) { 238f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Step is at the upper bound, stepmax (%g)\n",(double)ls->stepmax);CHKERRQ(ierr); 239a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_UPPERBOUND; 240a7e14dcfSSatish Balay break; 241a7e14dcfSSatish Balay } 242a7e14dcfSSatish Balay if ((ls->step == ls->stepmin) && (*f >= ftest1) && (dg >= dgtest)) { 243f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Step is at the lower bound, stepmin (%g)\n",(double)ls->stepmin);CHKERRQ(ierr); 244a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_LOWERBOUND; 245a7e14dcfSSatish Balay break; 246a7e14dcfSSatish Balay } 247a7e14dcfSSatish Balay if ((mt->bracket) && (ls->stepmax - ls->stepmin <= ls->rtol*ls->stepmax)){ 248f06e3bfaSBarry Smith ierr = PetscInfo1(ls,"Relative width of interval of uncertainty is at most rtol (%g)\n",(double)ls->rtol);CHKERRQ(ierr); 249a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_RTOL; 250a7e14dcfSSatish Balay break; 251a7e14dcfSSatish Balay } 252a7e14dcfSSatish Balay 253a7e14dcfSSatish Balay /* In the first stage, we seek a step for which the modified function 254a7e14dcfSSatish Balay has a nonpositive value and nonnegative derivative */ 255a7e14dcfSSatish Balay if ((stage1) && (*f <= ftest1) && (dg >= dginit * PetscMin(ls->ftol, ls->gtol))) { 256a7e14dcfSSatish Balay stage1 = 0; 257a7e14dcfSSatish Balay } 258a7e14dcfSSatish Balay 259a7e14dcfSSatish Balay /* A modified function is used to predict the step only if we 260a7e14dcfSSatish Balay have not obtained a step for which the modified function has a 261a7e14dcfSSatish Balay nonpositive function value and nonnegative derivative, and if a 262a7e14dcfSSatish Balay lower function value has been obtained but the decrease is not 263a7e14dcfSSatish Balay sufficient */ 264a7e14dcfSSatish Balay 265a7e14dcfSSatish Balay if ((stage1) && (*f <= fx) && (*f > ftest1)) { 266a7e14dcfSSatish Balay fm = *f - ls->step * dgtest; /* Define modified function */ 267a7e14dcfSSatish Balay fxm = fx - stx * dgtest; /* and derivatives */ 268a7e14dcfSSatish Balay fym = fy - sty * dgtest; 269a7e14dcfSSatish Balay dgm = dg - dgtest; 270a7e14dcfSSatish Balay dgxm = dgx - dgtest; 271a7e14dcfSSatish Balay dgym = dgy - dgtest; 272a7e14dcfSSatish Balay 273a7e14dcfSSatish Balay /* if (dgxm * (ls->step - stx) >= 0.0) */ 274a7e14dcfSSatish Balay /* Update the interval of uncertainty and compute the new step */ 275a7e14dcfSSatish Balay ierr = Tao_mcstep(ls,&stx,&fxm,&dgxm,&sty,&fym,&dgym,&ls->step,&fm,&dgm);CHKERRQ(ierr); 276a7e14dcfSSatish Balay 277a7e14dcfSSatish Balay fx = fxm + stx * dgtest; /* Reset the function and */ 278a7e14dcfSSatish Balay fy = fym + sty * dgtest; /* gradient values */ 279a7e14dcfSSatish Balay dgx = dgxm + dgtest; 280a7e14dcfSSatish Balay dgy = dgym + dgtest; 28153506e15SBarry Smith } else { 282a7e14dcfSSatish Balay /* Update the interval of uncertainty and compute the new step */ 283a7e14dcfSSatish Balay ierr = Tao_mcstep(ls,&stx,&fx,&dgx,&sty,&fy,&dgy,&ls->step,f,&dg);CHKERRQ(ierr); 284a7e14dcfSSatish Balay } 285a7e14dcfSSatish Balay 286a7e14dcfSSatish Balay /* Force a sufficient decrease in the interval of uncertainty */ 287a7e14dcfSSatish Balay if (mt->bracket) { 288a7e14dcfSSatish Balay if (PetscAbsReal(sty - stx) >= 0.66 * width1) ls->step = stx + 0.5*(sty - stx); 289a7e14dcfSSatish Balay width1 = width; 290a7e14dcfSSatish Balay width = PetscAbsReal(sty - stx); 291a7e14dcfSSatish Balay } 292a7e14dcfSSatish Balay } 293a7e14dcfSSatish Balay if ((ls->nfeval+ls->nfgeval) > ls->max_funcs) { 294a7e14dcfSSatish Balay ierr = PetscInfo2(ls,"Number of line search function evals (%D) > maximum (%D)\n",(ls->nfeval+ls->nfgeval),ls->max_funcs);CHKERRQ(ierr); 295a7e14dcfSSatish Balay ls->reason = TAOLINESEARCH_HALTED_MAXFCN; 296a7e14dcfSSatish Balay } 297a7e14dcfSSatish Balay 298a7e14dcfSSatish Balay /* Finish computations */ 299f06e3bfaSBarry Smith ierr = PetscInfo2(ls,"%D function evals in line search, step = %g\n",(ls->nfeval+ls->nfgeval),(double)ls->step);CHKERRQ(ierr); 300a7e14dcfSSatish Balay 301a7e14dcfSSatish Balay /* Set new solution vector and compute gradient if needed */ 302a7e14dcfSSatish Balay ierr = VecCopy(mt->work,x);CHKERRQ(ierr); 303a7e14dcfSSatish Balay if (!g_computed) { 304a7e14dcfSSatish Balay ierr = TaoLineSearchComputeGradient(ls,mt->work,g);CHKERRQ(ierr); 305a7e14dcfSSatish Balay } 306a7e14dcfSSatish Balay PetscFunctionReturn(0); 307a7e14dcfSSatish Balay } 308a7e14dcfSSatish Balay 309728e0ed0SBarry Smith PETSC_EXTERN PetscErrorCode TaoLineSearchCreate_MT(TaoLineSearch ls) 310a7e14dcfSSatish Balay { 311a7e14dcfSSatish Balay PetscErrorCode ierr; 3128caf6e8cSBarry Smith TaoLineSearch_MT *ctx; 31353506e15SBarry Smith 314a7e14dcfSSatish Balay PetscFunctionBegin; 315a7e14dcfSSatish Balay PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); 3163c9e27cfSGeoffrey Irving ierr = PetscNewLog(ls,&ctx);CHKERRQ(ierr); 317a7e14dcfSSatish Balay ctx->bracket=0; 318a7e14dcfSSatish Balay ctx->infoc=1; 319a7e14dcfSSatish Balay ls->data = (void*)ctx; 320a7e14dcfSSatish Balay ls->initstep = 1.0; 321a7e14dcfSSatish Balay ls->ops->setup=0; 32224517cd3SJason Sarich ls->ops->reset=0; 323a7e14dcfSSatish Balay ls->ops->apply=TaoLineSearchApply_MT; 324a7e14dcfSSatish Balay ls->ops->destroy=TaoLineSearchDestroy_MT; 325a7e14dcfSSatish Balay ls->ops->setfromoptions=TaoLineSearchSetFromOptions_MT; 326*2a0dac07SAlp Dener ls->ops->monitor=TaoLineSearchMonitor_MT; 327a7e14dcfSSatish Balay PetscFunctionReturn(0); 328a7e14dcfSSatish Balay } 329a7e14dcfSSatish Balay 330a7e14dcfSSatish Balay /* 331a7e14dcfSSatish Balay The subroutine mcstep is taken from the work of Jorge Nocedal. 332a7e14dcfSSatish Balay this is a variant of More' and Thuente's routine. 333a7e14dcfSSatish Balay 334a7e14dcfSSatish Balay subroutine mcstep 335a7e14dcfSSatish Balay 336a7e14dcfSSatish Balay the purpose of mcstep is to compute a safeguarded step for 337a7e14dcfSSatish Balay a linesearch and to update an interval of uncertainty for 338a7e14dcfSSatish Balay a minimizer of the function. 339a7e14dcfSSatish Balay 340a7e14dcfSSatish Balay the parameter stx contains the step with the least function 341a7e14dcfSSatish Balay value. the parameter stp contains the current step. it is 342a7e14dcfSSatish Balay assumed that the derivative at stx is negative in the 343a7e14dcfSSatish Balay direction of the step. if bracket is set true then a 344a7e14dcfSSatish Balay minimizer has been bracketed in an interval of uncertainty 345a7e14dcfSSatish Balay with endpoints stx and sty. 346a7e14dcfSSatish Balay 347a7e14dcfSSatish Balay the subroutine statement is 348a7e14dcfSSatish Balay 349a7e14dcfSSatish Balay subroutine mcstep(stx,fx,dx,sty,fy,dy,stp,fp,dp,bracket, 350a7e14dcfSSatish Balay stpmin,stpmax,info) 351a7e14dcfSSatish Balay 352a7e14dcfSSatish Balay where 353a7e14dcfSSatish Balay 354a7e14dcfSSatish Balay stx, fx, and dx are variables which specify the step, 355a7e14dcfSSatish Balay the function, and the derivative at the best step obtained 356a7e14dcfSSatish Balay so far. The derivative must be negative in the direction 357a7e14dcfSSatish Balay of the step, that is, dx and stp-stx must have opposite 358a7e14dcfSSatish Balay signs. On output these parameters are updated appropriately. 359a7e14dcfSSatish Balay 360a7e14dcfSSatish Balay sty, fy, and dy are variables which specify the step, 361a7e14dcfSSatish Balay the function, and the derivative at the other endpoint of 362a7e14dcfSSatish Balay the interval of uncertainty. On output these parameters are 363a7e14dcfSSatish Balay updated appropriately. 364a7e14dcfSSatish Balay 365a7e14dcfSSatish Balay stp, fp, and dp are variables which specify the step, 366a7e14dcfSSatish Balay the function, and the derivative at the current step. 367a7e14dcfSSatish Balay If bracket is set true then on input stp must be 368a7e14dcfSSatish Balay between stx and sty. On output stp is set to the new step. 369a7e14dcfSSatish Balay 370a7e14dcfSSatish Balay bracket is a logical variable which specifies if a minimizer 371a7e14dcfSSatish Balay has been bracketed. If the minimizer has not been bracketed 372a7e14dcfSSatish Balay then on input bracket must be set false. If the minimizer 373a7e14dcfSSatish Balay is bracketed then on output bracket is set true. 374a7e14dcfSSatish Balay 375a7e14dcfSSatish Balay stpmin and stpmax are input variables which specify lower 376a7e14dcfSSatish Balay and upper bounds for the step. 377a7e14dcfSSatish Balay 378a7e14dcfSSatish Balay info is an integer output variable set as follows: 379a7e14dcfSSatish Balay if info = 1,2,3,4,5, then the step has been computed 380a7e14dcfSSatish Balay according to one of the five cases below. otherwise 381a7e14dcfSSatish Balay info = 0, and this indicates improper input parameters. 382a7e14dcfSSatish Balay 383a7e14dcfSSatish Balay subprograms called 384a7e14dcfSSatish Balay 385a7e14dcfSSatish Balay fortran-supplied ... abs,max,min,sqrt 386a7e14dcfSSatish Balay 387a7e14dcfSSatish Balay argonne national laboratory. minpack project. june 1983 388a7e14dcfSSatish Balay jorge j. more', david j. thuente 389a7e14dcfSSatish Balay 390a7e14dcfSSatish Balay */ 391a7e14dcfSSatish Balay 39253506e15SBarry 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) 393a7e14dcfSSatish Balay { 3948caf6e8cSBarry Smith TaoLineSearch_MT *mtP = (TaoLineSearch_MT *) ls->data; 395a7e14dcfSSatish Balay PetscReal gamma1, p, q, r, s, sgnd, stpc, stpf, stpq, theta; 396a7e14dcfSSatish Balay PetscInt bound; 397a7e14dcfSSatish Balay 398a7e14dcfSSatish Balay PetscFunctionBegin; 399a7e14dcfSSatish Balay /* Check the input parameters for errors */ 400a7e14dcfSSatish Balay mtP->infoc = 0; 401a7e14dcfSSatish Balay if (mtP->bracket && (*stp <= PetscMin(*stx,*sty) || (*stp >= PetscMax(*stx,*sty)))) SETERRQ(PETSC_COMM_SELF,1,"bad stp in bracket"); 402a7e14dcfSSatish Balay if (*dx * (*stp-*stx) >= 0.0) SETERRQ(PETSC_COMM_SELF,1,"dx * (stp-stx) >= 0.0"); 403a7e14dcfSSatish Balay if (ls->stepmax < ls->stepmin) SETERRQ(PETSC_COMM_SELF,1,"stepmax > stepmin"); 404a7e14dcfSSatish Balay 405a7e14dcfSSatish Balay /* Determine if the derivatives have opposite sign */ 406a7e14dcfSSatish Balay sgnd = *dp * (*dx / PetscAbsReal(*dx)); 407a7e14dcfSSatish Balay 408a7e14dcfSSatish Balay if (*fp > *fx) { 409a7e14dcfSSatish Balay /* Case 1: a higher function value. 410a7e14dcfSSatish Balay The minimum is bracketed. If the cubic step is closer 411a7e14dcfSSatish Balay to stx than the quadratic step, the cubic step is taken, 412a7e14dcfSSatish Balay else the average of the cubic and quadratic steps is taken. */ 413a7e14dcfSSatish Balay 414a7e14dcfSSatish Balay mtP->infoc = 1; 415a7e14dcfSSatish Balay bound = 1; 416a7e14dcfSSatish Balay theta = 3 * (*fx - *fp) / (*stp - *stx) + *dx + *dp; 417a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dx)); 418a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 419a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscPowScalar(theta/s,2.0) - (*dx/s)*(*dp/s)); 420a7e14dcfSSatish Balay if (*stp < *stx) gamma1 = -gamma1; 421a7e14dcfSSatish Balay /* Can p be 0? Check */ 422a7e14dcfSSatish Balay p = (gamma1 - *dx) + theta; 423a7e14dcfSSatish Balay q = ((gamma1 - *dx) + gamma1) + *dp; 424a7e14dcfSSatish Balay r = p/q; 425a7e14dcfSSatish Balay stpc = *stx + r*(*stp - *stx); 426a7e14dcfSSatish Balay stpq = *stx + ((*dx/((*fx-*fp)/(*stp-*stx)+*dx))*0.5) * (*stp - *stx); 427a7e14dcfSSatish Balay 428a7e14dcfSSatish Balay if (PetscAbsReal(stpc-*stx) < PetscAbsReal(stpq-*stx)) { 429a7e14dcfSSatish Balay stpf = stpc; 43053506e15SBarry Smith } else { 431a7e14dcfSSatish Balay stpf = stpc + 0.5*(stpq - stpc); 432a7e14dcfSSatish Balay } 433a7e14dcfSSatish Balay mtP->bracket = 1; 43453506e15SBarry Smith } else if (sgnd < 0.0) { 435a7e14dcfSSatish Balay /* Case 2: A lower function value and derivatives of 436a7e14dcfSSatish Balay opposite sign. The minimum is bracketed. If the cubic 437a7e14dcfSSatish Balay step is closer to stx than the quadratic (secant) step, 438a7e14dcfSSatish Balay the cubic step is taken, else the quadratic step is taken. */ 439a7e14dcfSSatish Balay 440a7e14dcfSSatish Balay mtP->infoc = 2; 441a7e14dcfSSatish Balay bound = 0; 442a7e14dcfSSatish Balay theta = 3*(*fx - *fp)/(*stp - *stx) + *dx + *dp; 443a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dx)); 444a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 445a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscPowScalar(theta/s,2.0) - (*dx/s)*(*dp/s)); 446a7e14dcfSSatish Balay if (*stp > *stx) gamma1 = -gamma1; 447a7e14dcfSSatish Balay p = (gamma1 - *dp) + theta; 448a7e14dcfSSatish Balay q = ((gamma1 - *dp) + gamma1) + *dx; 449a7e14dcfSSatish Balay r = p/q; 450a7e14dcfSSatish Balay stpc = *stp + r*(*stx - *stp); 451a7e14dcfSSatish Balay stpq = *stp + (*dp/(*dp-*dx))*(*stx - *stp); 452a7e14dcfSSatish Balay 453a7e14dcfSSatish Balay if (PetscAbsReal(stpc-*stp) > PetscAbsReal(stpq-*stp)) { 454a7e14dcfSSatish Balay stpf = stpc; 45553506e15SBarry Smith } else { 456a7e14dcfSSatish Balay stpf = stpq; 457a7e14dcfSSatish Balay } 458a7e14dcfSSatish Balay mtP->bracket = 1; 45953506e15SBarry Smith } else if (PetscAbsReal(*dp) < PetscAbsReal(*dx)) { 460a7e14dcfSSatish Balay /* Case 3: A lower function value, derivatives of the 461a7e14dcfSSatish Balay same sign, and the magnitude of the derivative decreases. 462a7e14dcfSSatish Balay The cubic step is only used if the cubic tends to infinity 463a7e14dcfSSatish Balay in the direction of the step or if the minimum of the cubic 464a7e14dcfSSatish Balay is beyond stp. Otherwise the cubic step is defined to be 465a7e14dcfSSatish Balay either stepmin or stepmax. The quadratic (secant) step is also 466df3898eeSBarry Smith computed and if the minimum is bracketed then the step 467a7e14dcfSSatish Balay closest to stx is taken, else the step farthest away is taken. */ 468a7e14dcfSSatish Balay 469a7e14dcfSSatish Balay mtP->infoc = 3; 470a7e14dcfSSatish Balay bound = 1; 471a7e14dcfSSatish Balay theta = 3*(*fx - *fp)/(*stp - *stx) + *dx + *dp; 472a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dx)); 473a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 474a7e14dcfSSatish Balay 475a7e14dcfSSatish Balay /* The case gamma1 = 0 only arises if the cubic does not tend 476a7e14dcfSSatish Balay to infinity in the direction of the step. */ 477a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscMax(0.0,PetscPowScalar(theta/s,2.0) - (*dx/s)*(*dp/s))); 478a7e14dcfSSatish Balay if (*stp > *stx) gamma1 = -gamma1; 479a7e14dcfSSatish Balay p = (gamma1 - *dp) + theta; 480a7e14dcfSSatish Balay q = (gamma1 + (*dx - *dp)) + gamma1; 481a7e14dcfSSatish Balay r = p/q; 482a7e14dcfSSatish Balay if (r < 0.0 && gamma1 != 0.0) stpc = *stp + r*(*stx - *stp); 483a7e14dcfSSatish Balay else if (*stp > *stx) stpc = ls->stepmax; 484a7e14dcfSSatish Balay else stpc = ls->stepmin; 485a7e14dcfSSatish Balay stpq = *stp + (*dp/(*dp-*dx)) * (*stx - *stp); 486a7e14dcfSSatish Balay 487a7e14dcfSSatish Balay if (mtP->bracket) { 488a7e14dcfSSatish Balay if (PetscAbsReal(*stp-stpc) < PetscAbsReal(*stp-stpq)) { 489a7e14dcfSSatish Balay stpf = stpc; 49053506e15SBarry Smith } else { 491a7e14dcfSSatish Balay stpf = stpq; 492a7e14dcfSSatish Balay } 49353506e15SBarry Smith } else { 494a7e14dcfSSatish Balay if (PetscAbsReal(*stp-stpc) > PetscAbsReal(*stp-stpq)) { 495a7e14dcfSSatish Balay stpf = stpc; 49653506e15SBarry Smith } else { 497a7e14dcfSSatish Balay stpf = stpq; 498a7e14dcfSSatish Balay } 499a7e14dcfSSatish Balay } 50053506e15SBarry Smith } else { 501a7e14dcfSSatish Balay /* Case 4: A lower function value, derivatives of the 502a7e14dcfSSatish Balay same sign, and the magnitude of the derivative does 503a7e14dcfSSatish Balay not decrease. If the minimum is not bracketed, the step 504a7e14dcfSSatish Balay is either stpmin or stpmax, else the cubic step is taken. */ 505a7e14dcfSSatish Balay 506a7e14dcfSSatish Balay mtP->infoc = 4; 507a7e14dcfSSatish Balay bound = 0; 508a7e14dcfSSatish Balay if (mtP->bracket) { 509a7e14dcfSSatish Balay theta = 3*(*fp - *fy)/(*sty - *stp) + *dy + *dp; 510a7e14dcfSSatish Balay s = PetscMax(PetscAbsReal(theta),PetscAbsReal(*dy)); 511a7e14dcfSSatish Balay s = PetscMax(s,PetscAbsReal(*dp)); 512a7e14dcfSSatish Balay gamma1 = s*PetscSqrtScalar(PetscPowScalar(theta/s,2.0) - (*dy/s)*(*dp/s)); 513a7e14dcfSSatish Balay if (*stp > *sty) gamma1 = -gamma1; 514a7e14dcfSSatish Balay p = (gamma1 - *dp) + theta; 515a7e14dcfSSatish Balay q = ((gamma1 - *dp) + gamma1) + *dy; 516a7e14dcfSSatish Balay r = p/q; 517a7e14dcfSSatish Balay stpc = *stp + r*(*sty - *stp); 518a7e14dcfSSatish Balay stpf = stpc; 51953506e15SBarry Smith } else if (*stp > *stx) { 520a7e14dcfSSatish Balay stpf = ls->stepmax; 52153506e15SBarry Smith } else { 522a7e14dcfSSatish Balay stpf = ls->stepmin; 523a7e14dcfSSatish Balay } 524a7e14dcfSSatish Balay } 525a7e14dcfSSatish Balay 526a7e14dcfSSatish Balay /* Update the interval of uncertainty. This update does not 527a7e14dcfSSatish Balay depend on the new step or the case analysis above. */ 528a7e14dcfSSatish Balay 529a7e14dcfSSatish Balay if (*fp > *fx) { 530a7e14dcfSSatish Balay *sty = *stp; 531a7e14dcfSSatish Balay *fy = *fp; 532a7e14dcfSSatish Balay *dy = *dp; 53353506e15SBarry Smith } else { 534a7e14dcfSSatish Balay if (sgnd < 0.0) { 535a7e14dcfSSatish Balay *sty = *stx; 536a7e14dcfSSatish Balay *fy = *fx; 537a7e14dcfSSatish Balay *dy = *dx; 538a7e14dcfSSatish Balay } 539a7e14dcfSSatish Balay *stx = *stp; 540a7e14dcfSSatish Balay *fx = *fp; 541a7e14dcfSSatish Balay *dx = *dp; 542a7e14dcfSSatish Balay } 543a7e14dcfSSatish Balay 544a7e14dcfSSatish Balay /* Compute the new step and safeguard it. */ 545a7e14dcfSSatish Balay stpf = PetscMin(ls->stepmax,stpf); 546a7e14dcfSSatish Balay stpf = PetscMax(ls->stepmin,stpf); 547a7e14dcfSSatish Balay *stp = stpf; 548a7e14dcfSSatish Balay if (mtP->bracket && bound) { 549a7e14dcfSSatish Balay if (*sty > *stx) { 550a7e14dcfSSatish Balay *stp = PetscMin(*stx+0.66*(*sty-*stx),*stp); 55153506e15SBarry Smith } else { 552a7e14dcfSSatish Balay *stp = PetscMax(*stx+0.66*(*sty-*stx),*stp); 553a7e14dcfSSatish Balay } 554a7e14dcfSSatish Balay } 555a7e14dcfSSatish Balay PetscFunctionReturn(0); 556a7e14dcfSSatish Balay } 557