Lines Matching refs:ls

10 static PetscErrorCode Tao_mcstep(TaoLineSearch ls, PetscReal *stx, PetscReal *fx, PetscReal *dx, PetscReal *sty, PetscReal *fy, PetscReal *dy, PetscReal *stp, PetscReal *fp, PetscReal *dp);
12 static PetscErrorCode TaoLineSearchDestroy_MT(TaoLineSearch ls)
14 TaoLineSearch_MT *mt = (TaoLineSearch_MT *)ls->data;
19 PetscCall(PetscFree(ls->data));
23 static PetscErrorCode TaoLineSearchMonitor_MT(TaoLineSearch ls)
25 TaoLineSearch_MT *mt = (TaoLineSearch_MT *)ls->data;
28 PetscCall(PetscViewerASCIIPrintf(ls->viewer, "stx: %g, fx: %g, dgx: %g\n", (double)mt->stx, (double)mt->fx, (double)mt->dgx));
29 PetscCall(PetscViewerASCIIPrintf(ls->viewer, "sty: %g, fy: %g, dgy: %g\n", (double)mt->sty, (double)mt->fy, (double)mt->dgy));
33 static PetscErrorCode TaoLineSearchApply_MT(TaoLineSearch ls, Vec x, PetscReal *f, Vec g, Vec s)
35 TaoLineSearch_MT *mt = (TaoLineSearch_MT *)ls->data;
45 ls->reason = TAOLINESEARCH_CONTINUE_ITERATING;
46 PetscCall(TaoLineSearchMonitor(ls, 0, *f, 0.0));
60 ostepmax = ls->stepmax;
61 ostepmin = ls->stepmin;
63 if (ls->bounded) {
67 PetscCall(VecGetLocalSize(ls->upper, &n1));
69 PetscCall(VecGetSize(ls->upper, &nn1));
73 PetscCall(VecBoundGradientProjection(s, x, ls->lower, ls->upper, s));
75 PetscCall(VecStepBoundInfo(x, s, ls->lower, ls->upper, &bstepmin1, &bstepmin2, &bstepmax));
76 ls->stepmax = PetscMin(bstepmax, ls->stepmax);
81 PetscCall(PetscInfo(ls, "Initial Line Search step * g is infinity or NaN (%g)\n", (double)dginit));
82 ls->reason = TAOLINESEARCH_FAILED_INFORNAN;
86 PetscCall(PetscInfo(ls, "Initial Line Search step * g is not descent direction (%g)\n", (double)dginit));
87 ls->reason = TAOLINESEARCH_FAILED_ASCENT;
95 dgtest = ls->ftol * dginit;
96 width = ls->stepmax - ls->stepmin;
112 ls->step = ls->initstep;
113 for (i = 0; i < ls->max_funcs; i++) {
116 ls->stepmin = PetscMin(stx, sty);
117 ls->stepmax = PetscMax(stx, sty);
119 ls->stepmin = stx;
120 ls->stepmax = ls->step + xtrapf * (ls->step - stx);
124 ls->step = PetscMax(ls->step, ls->stepmin);
125 ls->step = PetscMin(ls->step, ls->stepmax);
129 if (stx != 0 && ((mt->bracket && (ls->step <= ls->stepmin || ls->step >= ls->stepmax)) || (mt->bracket && (ls->stepmax - ls->stepmin <= ls->rtol * ls->stepmax)) || (ls->nfeval + ls->nfgeval >= ls->max_funcs - 1) || mt->infoc == 0))
130 ls->step = stx;
132 PetscCall(VecWAXPY(mt->work, ls->step, s, x)); /* W = X + step*S */
134 if (ls->step == 0.0) {
135 PetscCall(PetscInfo(ls, "Step size is zero.\n"));
136 ls->reason = TAOLINESEARCH_HALTED_LOWERBOUND;
140 if (ls->bounded) PetscCall(VecMedian(ls->lower, mt->work, ls->upper, mt->work));
143 if (ls->usegts) {
144 PetscCall(TaoLineSearchComputeObjectiveAndGTS(ls, mt->work, f, &dg));
147 PetscCall(TaoLineSearchComputeObjectiveAndGradient(ls, mt->work, f, g));
149 if (ls->bounded) {
152 dg = (dg2 - dg) / ls->step;
166 PetscCall(TaoLineSearchMonitor(ls, i + 1, *f, ls->step));
168 if (i == 0) ls->f_fullstep = *f;
178 ftest1 = finit + ls->step * dgtest;
179 if (ls->bounded) ftest2 = finit + ls->step * dgtest * ls->ftol;
182 if ((*f - ftest1 <= PETSC_SMALL * PetscAbsReal(finit)) && (PetscAbsReal(dg) + ls->gtol * dginit <= 0.0)) {
183 PetscCall(PetscInfo(ls, "Line search success: Sufficient decrease and directional deriv conditions hold\n"));
184 ls->reason = TAOLINESEARCH_SUCCESS;
189 if (ls->bounded && *f <= ftest2 && ls->step >= bstepmin2) {
190 PetscCall(PetscInfo(ls, "Line search success: Sufficient decrease.\n"));
191 ls->reason = TAOLINESEARCH_SUCCESS;
196 if ((mt->bracket && (ls->step <= ls->stepmin || ls->step >= ls->stepmax)) || !mt->infoc) {
197 PetscCall(PetscInfo(ls, "Rounding errors may prevent further progress. May not be a step satisfying\nsufficient decrease and curvature conditions. Tolerances may be too small.\n"));
198 ls->reason = TAOLINESEARCH_HALTED_OTHER;
201 if (ls->step == ls->stepmax && *f <= ftest1 && dg <= dgtest) {
202 PetscCall(PetscInfo(ls, "Step is at the upper bound, stepmax (%g)\n", (double)ls->stepmax));
203 ls->reason = TAOLINESEARCH_HALTED_UPPERBOUND;
206 if (ls->step == ls->stepmin && *f >= ftest1 && dg >= dgtest) {
207 PetscCall(PetscInfo(ls, "Step is at the lower bound, stepmin (%g)\n", (double)ls->stepmin));
208 ls->reason = TAOLINESEARCH_HALTED_LOWERBOUND;
211 if (mt->bracket && (ls->stepmax - ls->stepmin <= ls->rtol * ls->stepmax)) {
212 PetscCall(PetscInfo(ls, "Relative width of interval of uncertainty is at most rtol (%g)\n", (double)ls->rtol));
213 ls->reason = TAOLINESEARCH_HALTED_RTOL;
219 if (stage1 && *f <= ftest1 && dg >= dginit * PetscMin(ls->ftol, ls->gtol)) stage1 = 0;
228 fm = *f - ls->step * dgtest; /* Define modified function */
235 /* if (dgxm * (ls->step - stx) >= 0.0) */
237 PetscCall(Tao_mcstep(ls, &stx, &fxm, &dgxm, &sty, &fym, &dgym, &ls->step, &fm, &dgm));
245 PetscCall(Tao_mcstep(ls, &stx, &fx, &dgx, &sty, &fy, &dgy, &ls->step, f, &dg));
250 if (PetscAbsReal(sty - stx) >= 0.66 * width1) ls->step = stx + 0.5 * (sty - stx);
255 if (ls->nfeval + ls->nfgeval > ls->max_funcs) {
256 PetscCall(PetscInfo(ls, "Number of line search function evals (%" PetscInt_FMT ") > maximum (%" PetscInt_FMT ")\n", ls->nfeval + ls->nfgeval, ls->max_funcs));
257 ls->reason = TAOLINESEARCH_HALTED_MAXFCN;
259 ls->stepmax = ostepmax;
260 ls->stepmin = ostepmin;
263 PetscCall(PetscInfo(ls, "%" PetscInt_FMT " function evals in line search, step = %g\n", ls->nfeval + ls->nfgeval, (double)ls->step));
267 if (!g_computed) PetscCall(TaoLineSearchComputeGradient(ls, x, g));
282 PETSC_EXTERN PetscErrorCode TaoLineSearchCreate_MT(TaoLineSearch ls)
287 PetscValidHeaderSpecific(ls, TAOLINESEARCH_CLASSID, 1);
291 ls->data = (void *)ctx;
292 ls->initstep = 1.0;
293 ls->ops->setup = NULL;
294 ls->ops->reset = NULL;
295 ls->ops->apply = TaoLineSearchApply_MT;
296 ls->ops->destroy = TaoLineSearchDestroy_MT;
297 ls->ops->monitor = TaoLineSearchMonitor_MT;
363 static PetscErrorCode Tao_mcstep(TaoLineSearch ls, PetscReal *stx, PetscReal *fx, PetscReal *dx, PetscReal *sty, PetscReal *fy, PetscReal *dy, PetscReal *stp, PetscReal *fp, PetscReal *dp)
365 TaoLineSearch_MT *mtP = (TaoLineSearch_MT *)ls->data;
374 PetscCheck(ls->stepmax >= ls->stepmin, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "stepmax > stepmin");
448 else if (*stp > *stx) stpc = ls->stepmax;
449 else stpc = ls->stepmin;
479 stpf = ls->stepmax;
481 stpf = ls->stepmin;
504 stpf = PetscMin(ls->stepmax, stpf);
505 stpf = PetscMax(ls->stepmin, stpf);