Actual source code: bncg.c
1: #include <petsctaolinesearch.h>
2: #include <../src/tao/bound/impls/bncg/bncg.h>
3: #include <petscksp.h>
5: #define CG_GradientDescent 0
6: #define CG_HestenesStiefel 1
7: #define CG_FletcherReeves 2
8: #define CG_PolakRibierePolyak 3
9: #define CG_PolakRibierePlus 4
10: #define CG_DaiYuan 5
11: #define CG_HagerZhang 6
12: #define CG_DaiKou 7
13: #define CG_KouDai 8
14: #define CG_SSML_BFGS 9
15: #define CG_SSML_DFP 10
16: #define CG_SSML_BROYDEN 11
17: #define CG_PCGradientDescent 12
18: #define CGTypes 13
20: static const char *CG_Table[64] = {"gd", "hs", "fr", "pr", "prp", "dy", "hz", "dk", "kd", "ssml_bfgs", "ssml_dfp", "ssml_brdn", "pcgd"};
22: #define CG_AS_NONE 0
23: #define CG_AS_BERTSEKAS 1
24: #define CG_AS_SIZE 2
26: static const char *CG_AS_TYPE[64] = {"none", "bertsekas"};
28: PetscErrorCode TaoBNCGEstimateActiveSet(Tao tao, PetscInt asType)
29: {
30: PetscErrorCode ierr;
31: TAO_BNCG *cg = (TAO_BNCG *)tao->data;
34: ISDestroy(&cg->inactive_old);
35: if (cg->inactive_idx) {
36: ISDuplicate(cg->inactive_idx, &cg->inactive_old);
37: ISCopy(cg->inactive_idx, cg->inactive_old);
38: }
39: switch (asType) {
40: case CG_AS_NONE:
41: ISDestroy(&cg->inactive_idx);
42: VecWhichInactive(tao->XL, tao->solution, cg->unprojected_gradient, tao->XU, PETSC_TRUE, &cg->inactive_idx);
43: ISDestroy(&cg->active_idx);
44: ISComplementVec(cg->inactive_idx, tao->solution, &cg->active_idx);
45: break;
47: case CG_AS_BERTSEKAS:
48: /* Use gradient descent to estimate the active set */
49: VecCopy(cg->unprojected_gradient, cg->W);
50: VecScale(cg->W, -1.0);
51: TaoEstimateActiveBounds(tao->solution, tao->XL, tao->XU, cg->unprojected_gradient, cg->W, cg->work, cg->as_step, &cg->as_tol,
52: &cg->active_lower, &cg->active_upper, &cg->active_fixed, &cg->active_idx, &cg->inactive_idx);
53: break;
55: default:
56: break;
57: }
58: return(0);
59: }
61: PetscErrorCode TaoBNCGBoundStep(Tao tao, PetscInt asType, Vec step)
62: {
63: PetscErrorCode ierr;
64: TAO_BNCG *cg = (TAO_BNCG *)tao->data;
67: switch (asType) {
68: case CG_AS_NONE:
69: VecISSet(step, cg->active_idx, 0.0);
70: break;
72: case CG_AS_BERTSEKAS:
73: TaoBoundStep(tao->solution, tao->XL, tao->XU, cg->active_lower, cg->active_upper, cg->active_fixed, 1.0, step);
74: break;
76: default:
77: break;
78: }
79: return(0);
80: }
82: static PetscErrorCode TaoSolve_BNCG(Tao tao)
83: {
84: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
85: PetscErrorCode ierr;
86: PetscReal step=1.0,gnorm,gnorm2, resnorm;
87: PetscInt nDiff;
90: /* Project the current point onto the feasible set */
91: TaoComputeVariableBounds(tao);
92: TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);
94: /* Project the initial point onto the feasible region */
95: TaoBoundSolution(tao->solution, tao->XL,tao->XU, 0.0, &nDiff, tao->solution);
97: if (nDiff > 0 || !tao->recycle) {
98: TaoComputeObjectiveAndGradient(tao, tao->solution, &cg->f, cg->unprojected_gradient);
99: }
100: VecNorm(cg->unprojected_gradient,NORM_2,&gnorm);
101: if (PetscIsInfOrNanReal(cg->f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_USER, "User provided compute function generated Inf or NaN");
103: /* Estimate the active set and compute the projected gradient */
104: TaoBNCGEstimateActiveSet(tao, cg->as_type);
106: /* Project the gradient and calculate the norm */
107: VecCopy(cg->unprojected_gradient, tao->gradient);
108: VecISSet(tao->gradient, cg->active_idx, 0.0);
109: VecNorm(tao->gradient,NORM_2,&gnorm);
110: gnorm2 = gnorm*gnorm;
112: /* Initialize counters */
113: tao->niter = 0;
114: cg->ls_fails = cg->descent_error = 0;
115: cg->resets = -1;
116: cg->skipped_updates = cg->pure_gd_steps = 0;
117: cg->iter_quad = 0;
119: /* Convergence test at the starting point. */
120: tao->reason = TAO_CONTINUE_ITERATING;
122: VecFischer(tao->solution, cg->unprojected_gradient, tao->XL, tao->XU, cg->W);
123: VecNorm(cg->W, NORM_2, &resnorm);
124: if (PetscIsInfOrNanReal(resnorm)) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_USER, "User provided compute function generated Inf or NaN");
125: TaoLogConvergenceHistory(tao, cg->f, resnorm, 0.0, tao->ksp_its);
126: TaoMonitor(tao, tao->niter, cg->f, resnorm, 0.0, step);
127: (*tao->ops->convergencetest)(tao,tao->cnvP);
128: if (tao->reason != TAO_CONTINUE_ITERATING) return(0);
129: /* Calculate initial direction. */
130: if (!tao->recycle) {
131: /* We are not recycling a solution/history from a past TaoSolve */
132: TaoBNCGResetUpdate(tao, gnorm2);
133: }
134: /* Initial gradient descent step. Scaling by 1.0 also does a decent job for some problems. */
135: while (1) {
136: /* Call general purpose update function */
137: if (tao->ops->update) {
138: (*tao->ops->update)(tao, tao->niter, tao->user_update);
139: }
140: TaoBNCGConductIteration(tao, gnorm);
141: if (tao->reason != TAO_CONTINUE_ITERATING) return(0);
142: }
143: }
145: static PetscErrorCode TaoSetUp_BNCG(Tao tao)
146: {
147: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
151: if (!tao->gradient) {
152: VecDuplicate(tao->solution,&tao->gradient);
153: }
154: if (!tao->stepdirection) {
155: VecDuplicate(tao->solution,&tao->stepdirection);
156: }
157: if (!cg->W) {
158: VecDuplicate(tao->solution,&cg->W);
159: }
160: if (!cg->work) {
161: VecDuplicate(tao->solution,&cg->work);
162: }
163: if (!cg->sk) {
164: VecDuplicate(tao->solution,&cg->sk);
165: }
166: if (!cg->yk) {
167: VecDuplicate(tao->gradient,&cg->yk);
168: }
169: if (!cg->X_old) {
170: VecDuplicate(tao->solution,&cg->X_old);
171: }
172: if (!cg->G_old) {
173: VecDuplicate(tao->gradient,&cg->G_old);
174: }
175: if (cg->diag_scaling) {
176: VecDuplicate(tao->solution,&cg->d_work);
177: VecDuplicate(tao->solution,&cg->y_work);
178: VecDuplicate(tao->solution,&cg->g_work);
179: }
180: if (!cg->unprojected_gradient) {
181: VecDuplicate(tao->gradient,&cg->unprojected_gradient);
182: }
183: if (!cg->unprojected_gradient_old) {
184: VecDuplicate(tao->gradient,&cg->unprojected_gradient_old);
185: }
186: MatLMVMAllocate(cg->B, cg->sk, cg->yk);
187: if (cg->pc) {
188: MatLMVMSetJ0(cg->B, cg->pc);
189: }
190: return(0);
191: }
193: static PetscErrorCode TaoDestroy_BNCG(Tao tao)
194: {
195: TAO_BNCG *cg = (TAO_BNCG*) tao->data;
199: if (tao->setupcalled) {
200: VecDestroy(&cg->W);
201: VecDestroy(&cg->work);
202: VecDestroy(&cg->X_old);
203: VecDestroy(&cg->G_old);
204: VecDestroy(&cg->unprojected_gradient);
205: VecDestroy(&cg->unprojected_gradient_old);
206: VecDestroy(&cg->g_work);
207: VecDestroy(&cg->d_work);
208: VecDestroy(&cg->y_work);
209: VecDestroy(&cg->sk);
210: VecDestroy(&cg->yk);
211: }
212: ISDestroy(&cg->active_lower);
213: ISDestroy(&cg->active_upper);
214: ISDestroy(&cg->active_fixed);
215: ISDestroy(&cg->active_idx);
216: ISDestroy(&cg->inactive_idx);
217: ISDestroy(&cg->inactive_old);
218: ISDestroy(&cg->new_inactives);
219: MatDestroy(&cg->B);
220: if (cg->pc) {
221: MatDestroy(&cg->pc);
222: }
223: PetscFree(tao->data);
224: return(0);
225: }
227: static PetscErrorCode TaoSetFromOptions_BNCG(PetscOptionItems *PetscOptionsObject,Tao tao)
228: {
229: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
233: TaoLineSearchSetFromOptions(tao->linesearch);
234: PetscOptionsHead(PetscOptionsObject,"Nonlinear Conjugate Gradient method for unconstrained optimization");
235: PetscOptionsEList("-tao_bncg_type","cg formula", "", CG_Table, CGTypes, CG_Table[cg->cg_type], &cg->cg_type,NULL);
236: if (cg->cg_type != CG_SSML_BFGS) {
237: cg->alpha = -1.0; /* Setting defaults for non-BFGS methods. User can change it below. */
238: }
239: if (CG_GradientDescent == cg->cg_type) {
240: cg->cg_type = CG_PCGradientDescent;
241: /* Set scaling equal to none or, at best, scalar scaling. */
242: cg->unscaled_restart = PETSC_TRUE;
243: cg->diag_scaling = PETSC_FALSE;
244: }
245: PetscOptionsEList("-tao_bncg_as_type","active set estimation method", "", CG_AS_TYPE, CG_AS_SIZE, CG_AS_TYPE[cg->cg_type], &cg->cg_type,NULL);
247: PetscOptionsReal("-tao_bncg_hz_eta","(developer) cutoff tolerance for HZ", "", cg->hz_eta,&cg->hz_eta,NULL);
248: PetscOptionsReal("-tao_bncg_eps","(developer) cutoff value for restarts", "", cg->epsilon,&cg->epsilon,NULL);
249: PetscOptionsReal("-tao_bncg_dk_eta","(developer) cutoff tolerance for DK", "", cg->dk_eta,&cg->dk_eta,NULL);
250: PetscOptionsReal("-tao_bncg_xi","(developer) Parameter in the KD method", "", cg->xi,&cg->xi,NULL);
251: PetscOptionsReal("-tao_bncg_theta", "(developer) update parameter for the Broyden method", "", cg->theta, &cg->theta, NULL);
252: PetscOptionsReal("-tao_bncg_hz_theta", "(developer) parameter for the HZ (2006) method", "", cg->hz_theta, &cg->hz_theta, NULL);
253: PetscOptionsReal("-tao_bncg_alpha","(developer) parameter for the scalar scaling","",cg->alpha,&cg->alpha,NULL);
254: PetscOptionsReal("-tao_bncg_bfgs_scale", "(developer) update parameter for bfgs/brdn CG methods", "", cg->bfgs_scale, &cg->bfgs_scale, NULL);
255: PetscOptionsReal("-tao_bncg_dfp_scale", "(developer) update parameter for bfgs/brdn CG methods", "", cg->dfp_scale, &cg->dfp_scale, NULL);
256: PetscOptionsBool("-tao_bncg_diag_scaling","Enable diagonal Broyden-like preconditioning","",cg->diag_scaling,&cg->diag_scaling,NULL);
257: PetscOptionsBool("-tao_bncg_dynamic_restart","(developer) use dynamic restarts as in HZ, DK, KD","",cg->use_dynamic_restart,&cg->use_dynamic_restart,NULL);
258: PetscOptionsBool("-tao_bncg_unscaled_restart","(developer) use unscaled gradient restarts","",cg->unscaled_restart,&cg->unscaled_restart,NULL);
259: PetscOptionsReal("-tao_bncg_zeta", "(developer) Free parameter for the Kou-Dai method", "", cg->zeta, &cg->zeta, NULL);
260: PetscOptionsInt("-tao_bncg_min_quad", "(developer) Number of iterations with approximate quadratic behavior needed for restart", "", cg->min_quad, &cg->min_quad, NULL);
261: PetscOptionsInt("-tao_bncg_min_restart_num", "(developer) Number of iterations between restarts (times dimension)", "", cg->min_restart_num, &cg->min_restart_num, NULL);
262: PetscOptionsBool("-tao_bncg_spaced_restart","(developer) Enable regular steepest descent restarting every fixed number of iterations","",cg->spaced_restart,&cg->spaced_restart,NULL);
263: PetscOptionsBool("-tao_bncg_no_scaling","Disable all scaling except in restarts","",cg->no_scaling,&cg->no_scaling,NULL);
264: if (cg->no_scaling) {
265: cg->diag_scaling = PETSC_FALSE;
266: cg->alpha = -1.0;
267: }
268: if (cg->alpha == -1.0 && cg->cg_type == CG_KouDai && !cg->diag_scaling) { /* Some more default options that appear to be good. */
269: cg->neg_xi = PETSC_TRUE;
270: }
271: PetscOptionsBool("-tao_bncg_neg_xi","(developer) Use negative xi when it might be a smaller descent direction than necessary","",cg->neg_xi,&cg->neg_xi,NULL);
272: PetscOptionsReal("-tao_bncg_as_tol", "(developer) initial tolerance used when estimating actively bounded variables","",cg->as_tol,&cg->as_tol,NULL);
273: PetscOptionsReal("-tao_bncg_as_step", "(developer) step length used when estimating actively bounded variables","",cg->as_step,&cg->as_step,NULL);
274: PetscOptionsReal("-tao_bncg_delta_min", "(developer) minimum scaling factor used for scaled gradient restarts","",cg->delta_min,&cg->delta_min,NULL);
275: PetscOptionsReal("-tao_bncg_delta_max", "(developer) maximum scaling factor used for scaled gradient restarts","",cg->delta_max,&cg->delta_max,NULL);
277: PetscOptionsTail();
278: MatSetFromOptions(cg->B);
279: return(0);
280: }
282: static PetscErrorCode TaoView_BNCG(Tao tao, PetscViewer viewer)
283: {
284: PetscBool isascii;
285: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
289: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii);
290: if (isascii) {
291: PetscViewerASCIIPushTab(viewer);
292: PetscViewerASCIIPrintf(viewer, "CG Type: %s\n", CG_Table[cg->cg_type]);
293: PetscViewerASCIIPrintf(viewer, "Skipped Stepdirection Updates: %i\n", cg->skipped_updates);
294: PetscViewerASCIIPrintf(viewer, "Scaled gradient steps: %i\n", cg->resets);
295: PetscViewerASCIIPrintf(viewer, "Pure gradient steps: %i\n", cg->pure_gd_steps);
296: PetscViewerASCIIPrintf(viewer, "Not a descent direction: %i\n", cg->descent_error);
297: PetscViewerASCIIPrintf(viewer, "Line search fails: %i\n", cg->ls_fails);
298: if (cg->diag_scaling) {
299: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
300: if (isascii) {
301: PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO);
302: MatView(cg->B, viewer);
303: PetscViewerPopFormat(viewer);
304: }
305: }
306: PetscViewerASCIIPopTab(viewer);
307: }
308: return(0);
309: }
311: PetscErrorCode TaoBNCGComputeScalarScaling(PetscReal yty, PetscReal yts, PetscReal sts, PetscReal *scale, PetscReal alpha)
312: {
313: PetscReal a, b, c, sig1, sig2;
316: *scale = 0.0;
318: if (1.0 == alpha) {
319: *scale = yts/yty;
320: } else if (0.0 == alpha) {
321: *scale = sts/yts;
322: }
323: else if (-1.0 == alpha) *scale = 1.0;
324: else {
325: a = yty;
326: b = yts;
327: c = sts;
328: a *= alpha;
329: b *= -(2.0*alpha - 1.0);
330: c *= alpha - 1.0;
331: sig1 = (-b + PetscSqrtReal(b*b - 4.0*a*c))/(2.0*a);
332: sig2 = (-b - PetscSqrtReal(b*b - 4.0*a*c))/(2.0*a);
333: /* accept the positive root as the scalar */
334: if (sig1 > 0.0) {
335: *scale = sig1;
336: } else if (sig2 > 0.0) {
337: *scale = sig2;
338: } else {
339: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_CONV_FAILED, "Cannot find positive scalar");
340: }
341: }
342: return(0);
343: }
345: /*MC
346: TAOBNCG - Bound-constrained Nonlinear Conjugate Gradient method.
348: Options Database Keys:
349: + -tao_bncg_recycle - enable recycling the latest calculated gradient vector in subsequent TaoSolve() calls (currently disabled)
350: . -tao_bncg_eta <r> - restart tolerance
351: . -tao_bncg_type <taocg_type> - cg formula
352: . -tao_bncg_as_type <none,bertsekas> - active set estimation method
353: . -tao_bncg_as_tol <r> - tolerance used in Bertsekas active-set estimation
354: . -tao_bncg_as_step <r> - trial step length used in Bertsekas active-set estimation
355: . -tao_bncg_eps <r> - cutoff used for determining whether or not we restart based on steplength each iteration, as well as determining whether or not we continue using the last stepdirection. Defaults to machine precision.
356: . -tao_bncg_theta <r> - convex combination parameter for the Broyden method
357: . -tao_bncg_hz_eta <r> - cutoff tolerance for the beta term in the HZ, DK methods
358: . -tao_bncg_dk_eta <r> - cutoff tolerance for the beta term in the HZ, DK methods
359: . -tao_bncg_xi <r> - Multiplicative constant of the gamma term in the KD method
360: . -tao_bncg_hz_theta <r> - Multiplicative constant of the theta term for the HZ method
361: . -tao_bncg_bfgs_scale <r> - Scaling parameter of the bfgs contribution to the scalar Broyden method
362: . -tao_bncg_dfp_scale <r> - Scaling parameter of the dfp contribution to the scalar Broyden method
363: . -tao_bncg_diag_scaling <b> - Whether or not to use diagonal initialization/preconditioning for the CG methods. Default True.
364: . -tao_bncg_dynamic_restart <b> - use dynamic restart strategy in the HZ, DK, KD methods
365: . -tao_bncg_unscaled_restart <b> - whether or not to scale the gradient when doing gradient descent restarts
366: . -tao_bncg_zeta <r> - Scaling parameter in the KD method
367: . -tao_bncg_delta_min <r> - Minimum bound for rescaling during restarted gradient descent steps
368: . -tao_bncg_delta_max <r> - Maximum bound for rescaling during restarted gradient descent steps
369: . -tao_bncg_min_quad <i> - Number of quadratic-like steps in a row necessary to do a dynamic restart
370: . -tao_bncg_min_restart_num <i> - This number, x, makes sure there is a gradient descent step every x*n iterations, where n is the dimension of the problem
371: . -tao_bncg_spaced_restart <b> - whether or not to do gradient descent steps every x*n iterations
372: . -tao_bncg_no_scaling <b> - If true, eliminates all scaling, including defaults.
373: - -tao_bncg_neg_xi <b> - Whether or not to use negative xi in the KD method under certain conditions
375: Notes:
376: CG formulas are:
377: + "gd" - Gradient Descent
378: . "fr" - Fletcher-Reeves
379: . "pr" - Polak-Ribiere-Polyak
380: . "prp" - Polak-Ribiere-Plus
381: . "hs" - Hestenes-Steifel
382: . "dy" - Dai-Yuan
383: . "ssml_bfgs" - Self-Scaling Memoryless BFGS
384: . "ssml_dfp" - Self-Scaling Memoryless DFP
385: . "ssml_brdn" - Self-Scaling Memoryless Broyden
386: . "hz" - Hager-Zhang (CG_DESCENT 5.3)
387: . "dk" - Dai-Kou (2013)
388: - "kd" - Kou-Dai (2015)
390: Level: beginner
391: M*/
393: PETSC_EXTERN PetscErrorCode TaoCreate_BNCG(Tao tao)
394: {
395: TAO_BNCG *cg;
396: const char *morethuente_type = TAOLINESEARCHMT;
400: tao->ops->setup = TaoSetUp_BNCG;
401: tao->ops->solve = TaoSolve_BNCG;
402: tao->ops->view = TaoView_BNCG;
403: tao->ops->setfromoptions = TaoSetFromOptions_BNCG;
404: tao->ops->destroy = TaoDestroy_BNCG;
406: /* Override default settings (unless already changed) */
407: if (!tao->max_it_changed) tao->max_it = 2000;
408: if (!tao->max_funcs_changed) tao->max_funcs = 4000;
410: /* Note: nondefault values should be used for nonlinear conjugate gradient */
411: /* method. In particular, gtol should be less that 0.5; the value used in */
412: /* Nocedal and Wright is 0.10. We use the default values for the */
413: /* linesearch because it seems to work better. */
414: TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
415: PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1);
416: TaoLineSearchSetType(tao->linesearch, morethuente_type);
417: TaoLineSearchUseTaoRoutines(tao->linesearch, tao);
418: TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);
420: PetscNewLog(tao,&cg);
421: tao->data = (void*)cg;
422: KSPInitializePackage();
423: MatCreate(PetscObjectComm((PetscObject)tao), &cg->B);
424: PetscObjectIncrementTabLevel((PetscObject)cg->B, (PetscObject)tao, 1);
425: MatSetOptionsPrefix(cg->B, "tao_bncg_");
426: MatSetType(cg->B, MATLMVMDIAGBROYDEN);
428: cg->pc = NULL;
430: cg->dk_eta = 0.5;
431: cg->hz_eta = 0.4;
432: cg->dynamic_restart = PETSC_FALSE;
433: cg->unscaled_restart = PETSC_FALSE;
434: cg->no_scaling = PETSC_FALSE;
435: cg->delta_min = 1e-7;
436: cg->delta_max = 100;
437: cg->theta = 1.0;
438: cg->hz_theta = 1.0;
439: cg->dfp_scale = 1.0;
440: cg->bfgs_scale = 1.0;
441: cg->zeta = 0.1;
442: cg->min_quad = 6;
443: cg->min_restart_num = 6; /* As in CG_DESCENT and KD2015*/
444: cg->xi = 1.0;
445: cg->neg_xi = PETSC_TRUE;
446: cg->spaced_restart = PETSC_FALSE;
447: cg->tol_quad = 1e-8;
448: cg->as_step = 0.001;
449: cg->as_tol = 0.001;
450: cg->eps_23 = PetscPowReal(PETSC_MACHINE_EPSILON, 2.0/3.0); /* Just a little tighter*/
451: cg->as_type = CG_AS_BERTSEKAS;
452: cg->cg_type = CG_SSML_BFGS;
453: cg->alpha = 1.0;
454: cg->diag_scaling = PETSC_TRUE;
455: return(0);
456: }
458: PetscErrorCode TaoBNCGResetUpdate(Tao tao, PetscReal gnormsq)
459: {
460: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
461: PetscErrorCode ierr;
462: PetscReal scaling;
465: ++cg->resets;
466: scaling = 2.0 * PetscMax(1.0, PetscAbsScalar(cg->f)) / PetscMax(gnormsq, cg->eps_23);
467: scaling = PetscMin(cg->delta_max, PetscMax(cg->delta_min, scaling));
468: if (cg->unscaled_restart) {
469: scaling = 1.0;
470: ++cg->pure_gd_steps;
471: }
472: VecAXPBY(tao->stepdirection, -scaling, 0.0, tao->gradient);
473: /* Also want to reset our diagonal scaling with each restart */
474: if (cg->diag_scaling) {
475: MatLMVMReset(cg->B, PETSC_FALSE);
476: }
477: return(0);
478: }
480: PetscErrorCode TaoBNCGCheckDynamicRestart(Tao tao, PetscReal stepsize, PetscReal gd, PetscReal gd_old, PetscBool *dynrestart, PetscReal fold)
481: {
482: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
483: PetscReal quadinterp;
486: if (cg->f < cg->min_quad/10) {
487: *dynrestart = PETSC_FALSE;
488: return(0);
489: } /* just skip this since this strategy doesn't work well for functions near zero */
490: quadinterp = 2.0*(cg->f - fold)/(stepsize*(gd + gd_old));
491: if (PetscAbs(quadinterp - 1.0) < cg->tol_quad) ++cg->iter_quad;
492: else {
493: cg->iter_quad = 0;
494: *dynrestart = PETSC_FALSE;
495: }
496: if (cg->iter_quad >= cg->min_quad) {
497: cg->iter_quad = 0;
498: *dynrestart = PETSC_TRUE;
499: }
500: return(0);
501: }
503: PETSC_INTERN PetscErrorCode TaoBNCGStepDirectionUpdate(Tao tao, PetscReal gnorm2, PetscReal step, PetscReal fold, PetscReal gnorm2_old, PetscReal dnorm, PetscBool pcgd_fallback)
504: {
505: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
506: PetscErrorCode ierr;
507: PetscReal gamma = 1.0, tau_k, beta;
508: PetscReal tmp = 1.0, ynorm, ynorm2 = 1.0, snorm = 1.0, dk_yk=1.0, gd;
509: PetscReal gkp1_yk, gd_old, tau_bfgs, tau_dfp, gkp1D_yk, gtDg;
510: PetscInt dim;
511: PetscBool cg_restart = PETSC_FALSE;
514: /* Local curvature check to see if we need to restart */
515: if (tao->niter >= 1 || tao->recycle) {
516: VecWAXPY(cg->yk, -1.0, cg->G_old, tao->gradient);
517: VecNorm(cg->yk, NORM_2, &ynorm);
518: ynorm2 = ynorm*ynorm;
519: VecDot(cg->yk, tao->stepdirection, &dk_yk);
520: if (step*dnorm < PETSC_MACHINE_EPSILON || step*dk_yk < PETSC_MACHINE_EPSILON) {
521: cg_restart = PETSC_TRUE;
522: ++cg->skipped_updates;
523: }
524: if (cg->spaced_restart) {
525: VecGetSize(tao->gradient, &dim);
526: if (tao->niter % (dim*cg->min_restart_num)) cg_restart = PETSC_TRUE;
527: }
528: }
529: /* If the user wants regular restarts, do it every 6n iterations, where n=dimension */
530: if (cg->spaced_restart) {
531: VecGetSize(tao->gradient, &dim);
532: if (0 == tao->niter % (6*dim)) cg_restart = PETSC_TRUE;
533: }
534: /* Compute the diagonal scaling vector if applicable */
535: if (cg->diag_scaling) {
536: MatLMVMUpdate(cg->B, tao->solution, tao->gradient);
537: }
539: /* A note on diagonal scaling (to be added to paper):
540: For the FR, PR, PRP, and DY methods, the diagonally scaled versions
541: must be derived as a preconditioned CG method rather than as
542: a Hessian initialization like in the Broyden methods. */
544: /* In that case, one writes the objective function as
545: f(x) \equiv f(Ay). Gradient evaluations yield g(x_k) = A g(Ay_k) = A g(x_k).
546: Furthermore, the direction d_k \equiv (x_k - x_{k-1})/step according to
547: HZ (2006) becomes A^{-1} d_k, such that d_k^T g_k remains the
548: same under preconditioning. Note that A is diagonal, such that A^T = A. */
550: /* This yields questions like what the dot product d_k^T y_k
551: should look like. HZ mistakenly treats that as the same under
552: preconditioning, but that is not necessarily true. */
554: /* Observe y_k \equiv g_k - g_{k-1}, and under the P.C. transformation,
555: we get d_k^T y_k = (d_k^T A_k^{-T} A_k g_k - d_k^T A_k^{-T} A_{k-1} g_{k-1}),
556: yielding d_k^T y_k = d_k^T g_k - d_k^T (A_k^{-T} A_{k-1} g_{k-1}), which is
557: NOT the same if our preconditioning matrix is updated between iterations.
558: This same issue is found when considering dot products of the form g_{k+1}^T y_k. */
560: /* Compute CG step direction */
561: if (cg_restart) {
562: TaoBNCGResetUpdate(tao, gnorm2);
563: } else if (pcgd_fallback) {
564: /* Just like preconditioned CG */
565: MatSolve(cg->B, tao->gradient, cg->g_work);
566: VecAXPBY(tao->stepdirection, -1.0, 0.0, cg->g_work);
567: } else if (ynorm2 > PETSC_MACHINE_EPSILON) {
568: switch (cg->cg_type) {
569: case CG_PCGradientDescent:
570: if (!cg->diag_scaling) {
571: if (!cg->no_scaling) {
572: cg->sts = step*step*dnorm*dnorm;
573: TaoBNCGComputeScalarScaling(ynorm2, step*dk_yk, cg->sts, &tau_k, cg->alpha);
574: } else {
575: tau_k = 1.0;
576: ++cg->pure_gd_steps;
577: }
578: VecAXPBY(tao->stepdirection, -tau_k, 0.0, tao->gradient);
579: } else {
580: MatSolve(cg->B, tao->gradient, cg->g_work);
581: VecAXPBY(tao->stepdirection, -1.0, 0.0, cg->g_work);
582: }
583: break;
585: case CG_HestenesStiefel:
586: /* Classic Hestenes-Stiefel method, modified with scalar and diagonal preconditioning. */
587: if (!cg->diag_scaling) {
588: cg->sts = step*step*dnorm*dnorm;
589: VecDot(cg->yk, tao->gradient, &gkp1_yk);
590: TaoBNCGComputeScalarScaling(ynorm2, step*dk_yk, cg->sts, &tau_k, cg->alpha);
591: beta = tau_k*gkp1_yk/dk_yk;
592: VecAXPBY(tao->stepdirection, -tau_k, beta, tao->gradient);
593: } else {
594: MatSolve(cg->B, tao->gradient, cg->g_work);
595: VecDot(cg->yk, cg->g_work, &gkp1_yk);
596: beta = gkp1_yk/dk_yk;
597: VecAXPBY(tao->stepdirection, -1.0, beta, cg->g_work);
598: }
599: break;
601: case CG_FletcherReeves:
602: VecDot(cg->G_old, cg->G_old, &gnorm2_old);
603: VecWAXPY(cg->yk, -1.0, cg->G_old, tao->gradient);
604: VecNorm(cg->yk, NORM_2, &ynorm);
605: ynorm2 = ynorm*ynorm;
606: VecDot(cg->yk, tao->stepdirection, &dk_yk);
607: if (!cg->diag_scaling) {
608: TaoBNCGComputeScalarScaling(ynorm2, step*dk_yk, step*step*dnorm*dnorm, &tau_k, cg->alpha);
609: beta = tau_k*gnorm2/gnorm2_old;
610: VecAXPBY(tao->stepdirection, -tau_k, beta, tao->gradient);
611: } else {
612: VecDot(cg->G_old, cg->g_work, &gnorm2_old); /* Before it's updated */
613: MatSolve(cg->B, tao->gradient, cg->g_work);
614: VecDot(tao->gradient, cg->g_work, &tmp);
615: beta = tmp/gnorm2_old;
616: VecAXPBY(tao->stepdirection, -1.0, beta, cg->g_work);
617: }
618: break;
620: case CG_PolakRibierePolyak:
621: snorm = step*dnorm;
622: if (!cg->diag_scaling) {
623: VecDot(cg->G_old, cg->G_old, &gnorm2_old);
624: VecDot(cg->yk, tao->gradient, &gkp1_yk);
625: TaoBNCGComputeScalarScaling(ynorm2, step*dk_yk, snorm*snorm, &tau_k, cg->alpha);
626: beta = tau_k*gkp1_yk/gnorm2_old;
627: VecAXPBY(tao->stepdirection, -tau_k, beta, tao->gradient);
628: } else {
629: VecDot(cg->G_old, cg->g_work, &gnorm2_old);
630: MatSolve(cg->B, tao->gradient, cg->g_work);
631: VecDot(cg->g_work, cg->yk, &gkp1_yk);
632: beta = gkp1_yk/gnorm2_old;
633: VecAXPBY(tao->stepdirection, -1.0, beta, cg->g_work);
634: }
635: break;
637: case CG_PolakRibierePlus:
638: VecWAXPY(cg->yk, -1.0, cg->G_old, tao->gradient);
639: VecNorm(cg->yk, NORM_2, &ynorm);
640: ynorm2 = ynorm*ynorm;
641: if (!cg->diag_scaling) {
642: VecDot(cg->G_old, cg->G_old, &gnorm2_old);
643: VecDot(cg->yk, tao->gradient, &gkp1_yk);
644: TaoBNCGComputeScalarScaling(ynorm2, step*dk_yk, snorm*snorm, &tau_k, cg->alpha);
645: beta = tau_k*gkp1_yk/gnorm2_old;
646: beta = PetscMax(beta, 0.0);
647: VecAXPBY(tao->stepdirection, -tau_k, beta, tao->gradient);
648: } else {
649: VecDot(cg->G_old, cg->g_work, &gnorm2_old); /* Old gtDg */
650: MatSolve(cg->B, tao->gradient, cg->g_work);
651: VecDot(cg->g_work, cg->yk, &gkp1_yk);
652: beta = gkp1_yk/gnorm2_old;
653: beta = PetscMax(beta, 0.0);
654: VecAXPBY(tao->stepdirection, -1.0, beta, cg->g_work);
655: }
656: break;
658: case CG_DaiYuan:
659: /* Dai, Yu-Hong, and Yaxiang Yuan. "A nonlinear conjugate gradient method with a strong global convergence property."
660: SIAM Journal on optimization 10, no. 1 (1999): 177-182. */
661: if (!cg->diag_scaling) {
662: VecDot(tao->stepdirection, tao->gradient, &gd);
663: VecDot(cg->G_old, tao->stepdirection, &gd_old);
664: TaoBNCGComputeScalarScaling(ynorm2, step*dk_yk, cg->yts, &tau_k, cg->alpha);
665: beta = tau_k*gnorm2/(gd - gd_old);
666: VecAXPBY(tao->stepdirection, -tau_k, beta, tao->gradient);
667: } else {
668: MatMult(cg->B, tao->stepdirection, cg->d_work);
669: MatSolve(cg->B, tao->gradient, cg->g_work);
670: VecDot(cg->g_work, tao->gradient, >Dg);
671: VecDot(tao->stepdirection, cg->G_old, &gd_old);
672: VecDot(cg->d_work, cg->g_work, &dk_yk);
673: dk_yk = dk_yk - gd_old;
674: beta = gtDg/dk_yk;
675: VecScale(cg->d_work, beta);
676: VecWAXPY(tao->stepdirection, -1.0, cg->g_work, cg->d_work);
677: }
678: break;
680: case CG_HagerZhang:
681: /* Hager, William W., and Hongchao Zhang. "Algorithm 851: CG_DESCENT, a conjugate gradient method with guaranteed descent."
682: ACM Transactions on Mathematical Software (TOMS) 32, no. 1 (2006): 113-137. */
683: VecDot(tao->gradient, tao->stepdirection, &gd);
684: VecDot(cg->G_old, tao->stepdirection, &gd_old);
685: VecWAXPY(cg->sk, -1.0, cg->X_old, tao->solution);
686: snorm = dnorm*step;
687: cg->yts = step*dk_yk;
688: if (cg->use_dynamic_restart) {
689: TaoBNCGCheckDynamicRestart(tao, step, gd, gd_old, &cg->dynamic_restart, fold);
690: }
691: if (cg->dynamic_restart) {
692: TaoBNCGResetUpdate(tao, gnorm2);
693: } else {
694: if (!cg->diag_scaling) {
695: VecDot(cg->yk, tao->gradient, &gkp1_yk);
696: TaoBNCGComputeScalarScaling(ynorm2, cg->yts, snorm*snorm, &tau_k, cg->alpha);
697: /* Supplying cg->alpha = -1.0 will give the CG_DESCENT 5.3 special case of tau_k = 1.0 */
698: tmp = gd/dk_yk;
699: beta = tau_k*(gkp1_yk/dk_yk - ynorm2*gd/(dk_yk*dk_yk));
700: /* Bound beta as in CG_DESCENT 5.3, as implemented, with the third comparison from DK 2013 */
701: beta = PetscMax(PetscMax(beta, cg->hz_eta*tau_k*gd_old/(dnorm*dnorm)), cg->dk_eta*tau_k*gd/(dnorm*dnorm));
702: /* d <- -t*g + beta*t*d */
703: VecAXPBY(tao->stepdirection, -tau_k, beta, tao->gradient);
704: } else {
705: /* We have diagonal scaling enabled and are taking a diagonally-scaled memoryless BFGS step */
706: cg->yty = ynorm2;
707: cg->sts = snorm*snorm;
708: /* Apply the diagonal scaling to all my vectors */
709: MatSolve(cg->B, tao->gradient, cg->g_work);
710: MatSolve(cg->B, cg->yk, cg->y_work);
711: MatSolve(cg->B, tao->stepdirection, cg->d_work);
712: /* Construct the constant ytDgkp1 */
713: VecDot(cg->yk, cg->g_work, &gkp1_yk);
714: /* Construct the constant for scaling Dkyk in the update */
715: tmp = gd/dk_yk;
716: VecDot(cg->yk, cg->y_work, &tau_k);
717: tau_k = -tau_k*gd/(dk_yk*dk_yk);
718: /* beta is the constant which adds the dk contribution */
719: beta = gkp1_yk/dk_yk + cg->hz_theta*tau_k; /* HZ; (1.15) from DK 2013 */
720: /* From HZ2013, modified to account for diagonal scaling*/
721: VecDot(cg->G_old, cg->d_work, &gd_old);
722: VecDot(tao->stepdirection, cg->g_work, &gd);
723: beta = PetscMax(PetscMax(beta, cg->hz_eta*gd_old/(dnorm*dnorm)), cg->dk_eta*gd/(dnorm*dnorm));
724: /* Do the update */
725: VecAXPBY(tao->stepdirection, -1.0, beta, cg->g_work);
726: }
727: }
728: break;
730: case CG_DaiKou:
731: /* Dai, Yu-Hong, and Cai-Xia Kou. "A nonlinear conjugate gradient algorithm with an optimal property and an improved Wolfe line search."
732: SIAM Journal on Optimization 23, no. 1 (2013): 296-320. */
733: VecDot(tao->gradient, tao->stepdirection, &gd);
734: VecDot(cg->G_old, tao->stepdirection, &gd_old);
735: VecWAXPY(cg->sk, -1.0, cg->X_old, tao->solution);
736: snorm = step*dnorm;
737: cg->yts = dk_yk*step;
738: if (!cg->diag_scaling) {
739: VecDot(cg->yk, tao->gradient, &gkp1_yk);
740: TaoBNCGComputeScalarScaling(ynorm2, cg->yts, snorm*snorm, &tau_k, cg->alpha);
741: /* Use cg->alpha = -1.0 to get tau_k = 1.0 as in CG_DESCENT 5.3 */
742: tmp = gd/dk_yk;
743: beta = tau_k*(gkp1_yk/dk_yk - ynorm2*gd/(dk_yk*dk_yk) + gd/(dnorm*dnorm)) - step*gd/dk_yk;
744: beta = PetscMax(PetscMax(beta, cg->hz_eta*tau_k*gd_old/(dnorm*dnorm)), cg->dk_eta*tau_k*gd/(dnorm*dnorm));
745: /* d <- -t*g + beta*t*d */
746: VecAXPBYPCZ(tao->stepdirection, -tau_k, 0.0, beta, tao->gradient, cg->yk);
747: } else {
748: /* We have diagonal scaling enabled and are taking a diagonally-scaled memoryless BFGS step */
749: cg->yty = ynorm2;
750: cg->sts = snorm*snorm;
751: MatSolve(cg->B, tao->gradient, cg->g_work);
752: MatSolve(cg->B, cg->yk, cg->y_work);
753: MatSolve(cg->B, tao->stepdirection, cg->d_work);
754: /* Construct the constant ytDgkp1 */
755: VecDot(cg->yk, cg->g_work, &gkp1_yk);
756: VecDot(cg->yk, cg->y_work, &tau_k);
757: tau_k = tau_k*gd/(dk_yk*dk_yk);
758: tmp = gd/dk_yk;
759: /* beta is the constant which adds the dk contribution */
760: beta = gkp1_yk/dk_yk - step*tmp - tau_k;
761: /* Update this for the last term in beta */
762: VecDot(cg->y_work, tao->stepdirection, &dk_yk);
763: beta += tmp*dk_yk/(dnorm*dnorm); /* projection of y_work onto dk */
764: VecDot(tao->stepdirection, cg->g_work, &gd);
765: VecDot(cg->G_old, cg->d_work, &gd_old);
766: beta = PetscMax(PetscMax(beta, cg->hz_eta*gd_old/(dnorm*dnorm)), cg->dk_eta*gd/(dnorm*dnorm));
767: /* Do the update */
768: VecAXPBY(tao->stepdirection, -1.0, beta, cg->g_work);
769: }
770: break;
772: case CG_KouDai:
773: /* Kou, Cai-Xia, and Yu-Hong Dai. "A modified self-scaling memoryless Broyden-Fletcher-Goldfarb-Shanno method for unconstrained optimization."
774: Journal of Optimization Theory and Applications 165, no. 1 (2015): 209-224. */
775: VecDot(tao->gradient, tao->stepdirection, &gd);
776: VecDot(cg->G_old, tao->stepdirection, &gd_old);
777: VecWAXPY(cg->sk, -1.0, cg->X_old, tao->solution);
778: snorm = step*dnorm;
779: cg->yts = dk_yk*step;
780: if (cg->use_dynamic_restart) {
781: TaoBNCGCheckDynamicRestart(tao, step, gd, gd_old, &cg->dynamic_restart, fold);
782: }
783: if (cg->dynamic_restart) {
784: TaoBNCGResetUpdate(tao, gnorm2);
785: } else {
786: if (!cg->diag_scaling) {
787: VecDot(cg->yk, tao->gradient, &gkp1_yk);
788: TaoBNCGComputeScalarScaling(ynorm2, cg->yts, snorm*snorm, &tau_k, cg->alpha);
789: beta = tau_k*(gkp1_yk/dk_yk - ynorm2*gd/(dk_yk*dk_yk)) - step*gd/dk_yk;
790: if (beta < cg->zeta*tau_k*gd/(dnorm*dnorm)) /* 0.1 is KD's zeta parameter */
791: {
792: beta = cg->zeta*tau_k*gd/(dnorm*dnorm);
793: gamma = 0.0;
794: } else {
795: if (gkp1_yk < 0 && cg->neg_xi) gamma = -1.0*gd/dk_yk;
796: /* This seems to be very effective when there's no tau_k scaling.
797: This guarantees a large descent step every iteration, going through DK 2015 Lemma 3.1's proof but allowing for negative xi */
798: else {
799: gamma = cg->xi*gd/dk_yk;
800: }
801: }
802: /* d <- -t*g + beta*t*d + t*tmp*yk */
803: VecAXPBYPCZ(tao->stepdirection, -tau_k, gamma*tau_k, beta, tao->gradient, cg->yk);
804: } else {
805: /* We have diagonal scaling enabled and are taking a diagonally-scaled memoryless BFGS step */
806: cg->yty = ynorm2;
807: cg->sts = snorm*snorm;
808: MatSolve(cg->B, tao->gradient, cg->g_work);
809: MatSolve(cg->B, cg->yk, cg->y_work);
810: /* Construct the constant ytDgkp1 */
811: VecDot(cg->yk, cg->g_work, &gkp1D_yk);
812: /* Construct the constant for scaling Dkyk in the update */
813: gamma = gd/dk_yk;
814: /* tau_k = -ytDy/(ytd)^2 * gd */
815: VecDot(cg->yk, cg->y_work, &tau_k);
816: tau_k = tau_k*gd/(dk_yk*dk_yk);
817: /* beta is the constant which adds the d_k contribution */
818: beta = gkp1D_yk/dk_yk - step*gamma - tau_k;
819: /* Here is the requisite check */
820: VecDot(tao->stepdirection, cg->g_work, &tmp);
821: if (cg->neg_xi) {
822: /* modified KD implementation */
823: if (gkp1D_yk/dk_yk < 0) gamma = -1.0*gd/dk_yk;
824: else {
825: gamma = cg->xi*gd/dk_yk;
826: }
827: if (beta < cg->zeta*tmp/(dnorm*dnorm)) {
828: beta = cg->zeta*tmp/(dnorm*dnorm);
829: gamma = 0.0;
830: }
831: } else { /* original KD 2015 implementation */
832: if (beta < cg->zeta*tmp/(dnorm*dnorm)) {
833: beta = cg->zeta*tmp/(dnorm*dnorm);
834: gamma = 0.0;
835: } else {
836: gamma = cg->xi*gd/dk_yk;
837: }
838: }
839: /* Do the update in two steps */
840: VecAXPBY(tao->stepdirection, -1.0, beta, cg->g_work);
841: VecAXPY(tao->stepdirection, gamma, cg->y_work);
842: }
843: }
844: break;
846: case CG_SSML_BFGS:
847: /* Perry, J. M. "A class of conjugate gradient algorithms with a two-step variable-metric memory."
848: Discussion Papers 269 (1977). */
849: VecDot(tao->gradient, tao->stepdirection, &gd);
850: VecWAXPY(cg->sk, -1.0, cg->X_old, tao->solution);
851: snorm = step*dnorm;
852: cg->yts = dk_yk*step;
853: cg->yty = ynorm2;
854: cg->sts = snorm*snorm;
855: if (!cg->diag_scaling) {
856: VecDot(cg->yk, tao->gradient, &gkp1_yk);
857: TaoBNCGComputeScalarScaling(cg->yty, cg->yts, cg->sts, &tau_k, cg->alpha);
858: tmp = gd/dk_yk;
859: beta = tau_k*(gkp1_yk/dk_yk - cg->yty*gd/(dk_yk*dk_yk)) - step*tmp;
860: /* d <- -t*g + beta*t*d + t*tmp*yk */
861: VecAXPBYPCZ(tao->stepdirection, -tau_k, tmp*tau_k, beta, tao->gradient, cg->yk);
862: } else {
863: /* We have diagonal scaling enabled and are taking a diagonally-scaled memoryless BFGS step */
864: MatSolve(cg->B, tao->gradient, cg->g_work);
865: MatSolve(cg->B, cg->yk, cg->y_work);
866: /* compute scalar gamma */
867: VecDot(cg->g_work, cg->yk, &gkp1_yk);
868: VecDot(cg->y_work, cg->yk, &tmp);
869: gamma = gd/dk_yk;
870: /* Compute scalar beta */
871: beta = (gkp1_yk/dk_yk - gd*tmp/(dk_yk*dk_yk)) - step*gd/dk_yk;
872: /* Compute stepdirection d_kp1 = gamma*Dkyk + beta*dk - Dkgkp1 */
873: VecAXPBYPCZ(tao->stepdirection, -1.0, gamma, beta, cg->g_work, cg->y_work);
874: }
875: break;
877: case CG_SSML_DFP:
878: VecDot(tao->gradient, tao->stepdirection, &gd);
879: VecWAXPY(cg->sk, -1.0, cg->X_old, tao->solution);
880: snorm = step*dnorm;
881: cg->yts = dk_yk*step;
882: cg->yty = ynorm2;
883: cg->sts = snorm*snorm;
884: if (!cg->diag_scaling) {
885: /* Instead of a regular convex combination, we will solve a quadratic formula. */
886: TaoBNCGComputeScalarScaling(cg->yty, cg->yts, cg->sts, &tau_k, cg->alpha);
887: VecDot(cg->yk, tao->gradient, &gkp1_yk);
888: tau_k = cg->dfp_scale*tau_k;
889: tmp = tau_k*gkp1_yk/cg->yty;
890: beta = -step*gd/dk_yk;
891: /* d <- -t*g + beta*d + tmp*yk */
892: VecAXPBYPCZ(tao->stepdirection, -tau_k, tmp, beta, tao->gradient, cg->yk);
893: } else {
894: /* We have diagonal scaling enabled and are taking a diagonally-scaled memoryless DFP step */
895: MatSolve(cg->B, tao->gradient, cg->g_work);
896: MatSolve(cg->B, cg->yk, cg->y_work);
897: /* compute scalar gamma */
898: VecDot(cg->g_work, cg->yk, &gkp1_yk);
899: VecDot(cg->y_work, cg->yk, &tmp);
900: gamma = (gkp1_yk/tmp);
901: /* Compute scalar beta */
902: beta = -step*gd/dk_yk;
903: /* Compute stepdirection d_kp1 = gamma*Dkyk + beta*dk - Dkgkp1 */
904: VecAXPBYPCZ(tao->stepdirection, -1.0, gamma, beta, cg->g_work, cg->y_work);
905: }
906: break;
908: case CG_SSML_BROYDEN:
909: VecDot(tao->gradient, tao->stepdirection, &gd);
910: VecWAXPY(cg->sk, -1.0, cg->X_old, tao->solution);
911: snorm = step*dnorm;
912: cg->yts = step*dk_yk;
913: cg->yty = ynorm2;
914: cg->sts = snorm*snorm;
915: if (!cg->diag_scaling) {
916: /* Instead of a regular convex combination, we will solve a quadratic formula. */
917: TaoBNCGComputeScalarScaling(cg->yty, step*dk_yk, snorm*snorm, &tau_bfgs, cg->bfgs_scale);
918: TaoBNCGComputeScalarScaling(cg->yty, step*dk_yk, snorm*snorm, &tau_dfp, cg->dfp_scale);
919: VecDot(cg->yk, tao->gradient, &gkp1_yk);
920: tau_k = cg->theta*tau_bfgs + (1.0-cg->theta)*tau_dfp;
921: /* If bfgs_scale = 1.0, it should reproduce the bfgs tau_bfgs. If bfgs_scale = 0.0,
922: it should reproduce the tau_dfp scaling. Same with dfp_scale. */
923: tmp = cg->theta*tau_bfgs*gd/dk_yk + (1-cg->theta)*tau_dfp*gkp1_yk/cg->yty;
924: beta = cg->theta*tau_bfgs*(gkp1_yk/dk_yk - cg->yty*gd/(dk_yk*dk_yk)) - step*gd/dk_yk;
925: /* d <- -t*g + beta*d + tmp*yk */
926: VecAXPBYPCZ(tao->stepdirection, -tau_k, tmp, beta, tao->gradient, cg->yk);
927: } else {
928: /* We have diagonal scaling enabled */
929: MatSolve(cg->B, tao->gradient, cg->g_work);
930: MatSolve(cg->B, cg->yk, cg->y_work);
931: /* compute scalar gamma */
932: VecDot(cg->g_work, cg->yk, &gkp1_yk);
933: VecDot(cg->y_work, cg->yk, &tmp);
934: gamma = cg->theta*gd/dk_yk + (1-cg->theta)*(gkp1_yk/tmp);
935: /* Compute scalar beta */
936: beta = cg->theta*(gkp1_yk/dk_yk - gd*tmp/(dk_yk*dk_yk)) - step*gd/dk_yk;
937: /* Compute stepdirection dkp1 = gamma*Dkyk + beta*dk - Dkgkp1 */
938: VecAXPBYPCZ(tao->stepdirection, -1.0, gamma, beta, cg->g_work, cg->y_work);
939: }
940: break;
942: default:
943: break;
945: }
946: }
947: return(0);
948: }
950: PETSC_INTERN PetscErrorCode TaoBNCGConductIteration(Tao tao, PetscReal gnorm)
951: {
952: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
953: PetscErrorCode ierr;
954: TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING;
955: PetscReal step=1.0,gnorm2,gd,dnorm=0.0;
956: PetscReal gnorm2_old,f_old,resnorm, gnorm_old;
957: PetscBool pcgd_fallback = PETSC_FALSE;
960: /* We are now going to perform a line search along the direction. */
961: /* Store solution and gradient info before it changes */
962: VecCopy(tao->solution, cg->X_old);
963: VecCopy(tao->gradient, cg->G_old);
964: VecCopy(cg->unprojected_gradient, cg->unprojected_gradient_old);
966: gnorm_old = gnorm;
967: gnorm2_old = gnorm_old*gnorm_old;
968: f_old = cg->f;
969: /* Perform bounded line search. If we are recycling a solution from a previous */
970: /* TaoSolve, then we want to immediately skip to calculating a new direction rather than performing a linesearch */
971: if (!(tao->recycle && 0 == tao->niter)) {
972: /* Above logic: the below code happens every iteration, except for the first iteration of a recycled TaoSolve */
973: TaoLineSearchSetInitialStepLength(tao->linesearch, 1.0);
974: TaoLineSearchApply(tao->linesearch, tao->solution, &cg->f, cg->unprojected_gradient, tao->stepdirection, &step, &ls_status);
975: TaoAddLineSearchCounts(tao);
977: /* Check linesearch failure */
978: if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) {
979: ++cg->ls_fails;
980: if (cg->cg_type == CG_GradientDescent) {
981: /* Nothing left to do but fail out of the optimization */
982: step = 0.0;
983: tao->reason = TAO_DIVERGED_LS_FAILURE;
984: } else {
985: /* Restore previous point, perform preconditioned GD and regular GD steps at the last good point */
986: VecCopy(cg->X_old, tao->solution);
987: VecCopy(cg->G_old, tao->gradient);
988: VecCopy(cg->unprojected_gradient_old, cg->unprojected_gradient);
989: gnorm = gnorm_old;
990: gnorm2 = gnorm2_old;
991: cg->f = f_old;
993: /* Fall back on preconditioned CG (so long as you're not already using it) */
994: if (cg->cg_type != CG_PCGradientDescent && cg->diag_scaling) {
995: pcgd_fallback = PETSC_TRUE;
996: TaoBNCGStepDirectionUpdate(tao, gnorm2, step, f_old, gnorm2_old, dnorm, pcgd_fallback);
998: TaoBNCGResetUpdate(tao, gnorm2);
999: TaoBNCGBoundStep(tao, cg->as_type, tao->stepdirection);
1001: TaoLineSearchSetInitialStepLength(tao->linesearch, 1.0);
1002: TaoLineSearchApply(tao->linesearch, tao->solution, &cg->f, cg->unprojected_gradient, tao->stepdirection, &step, &ls_status);
1003: TaoAddLineSearchCounts(tao);
1005: pcgd_fallback = PETSC_FALSE;
1006: if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) {
1007: /* Going to perform a regular gradient descent step. */
1008: ++cg->ls_fails;
1009: step = 0.0;
1010: }
1011: }
1012: /* Fall back on the scaled gradient step */
1013: if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) {
1014: ++cg->ls_fails;
1015: TaoBNCGResetUpdate(tao, gnorm2);
1016: TaoBNCGBoundStep(tao, cg->as_type, tao->stepdirection);
1017: TaoLineSearchSetInitialStepLength(tao->linesearch, 1.0);
1018: TaoLineSearchApply(tao->linesearch, tao->solution, &cg->f, cg->unprojected_gradient, tao->stepdirection, &step, &ls_status);
1019: TaoAddLineSearchCounts(tao);
1020: }
1022: if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) {
1023: /* Nothing left to do but fail out of the optimization */
1024: ++cg->ls_fails;
1025: step = 0.0;
1026: tao->reason = TAO_DIVERGED_LS_FAILURE;
1027: } else {
1028: /* One of the fallbacks worked. Set them both back equal to false. */
1029: pcgd_fallback = PETSC_FALSE;
1030: }
1031: }
1032: }
1033: /* Convergence test for line search failure */
1034: if (tao->reason != TAO_CONTINUE_ITERATING) return(0);
1036: /* Standard convergence test */
1037: VecFischer(tao->solution, cg->unprojected_gradient, tao->XL, tao->XU, cg->W);
1038: VecNorm(cg->W, NORM_2, &resnorm);
1039: if (PetscIsInfOrNanReal(resnorm)) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_USER, "User provided compute function generated Inf or NaN");
1040: TaoLogConvergenceHistory(tao, cg->f, resnorm, 0.0, tao->ksp_its);
1041: TaoMonitor(tao, tao->niter, cg->f, resnorm, 0.0, step);
1042: (*tao->ops->convergencetest)(tao,tao->cnvP);
1043: if (tao->reason != TAO_CONTINUE_ITERATING) return(0);
1044: }
1045: /* Assert we have an updated step and we need at least one more iteration. */
1046: /* Calculate the next direction */
1047: /* Estimate the active set at the new solution */
1048: TaoBNCGEstimateActiveSet(tao, cg->as_type);
1049: /* Compute the projected gradient and its norm */
1050: VecCopy(cg->unprojected_gradient, tao->gradient);
1051: VecISSet(tao->gradient, cg->active_idx, 0.0);
1052: VecNorm(tao->gradient,NORM_2,&gnorm);
1053: gnorm2 = gnorm*gnorm;
1055: /* Calculate some quantities used in the StepDirectionUpdate. */
1056: VecNorm(tao->stepdirection, NORM_2, &dnorm);
1057: /* Update the step direction. */
1058: TaoBNCGStepDirectionUpdate(tao, gnorm2, step, f_old, gnorm2_old, dnorm, pcgd_fallback);
1059: ++tao->niter;
1060: TaoBNCGBoundStep(tao, cg->as_type, tao->stepdirection);
1062: if (cg->cg_type != CG_GradientDescent) {
1063: /* Figure out which previously active variables became inactive this iteration */
1064: ISDestroy(&cg->new_inactives);
1065: if (cg->inactive_idx && cg->inactive_old) {
1066: ISDifference(cg->inactive_idx, cg->inactive_old, &cg->new_inactives);
1067: }
1068: /* Selectively reset the CG step those freshly inactive variables */
1069: if (cg->new_inactives) {
1070: VecGetSubVector(tao->stepdirection, cg->new_inactives, &cg->inactive_step);
1071: VecGetSubVector(cg->unprojected_gradient, cg->new_inactives, &cg->inactive_grad);
1072: VecCopy(cg->inactive_grad, cg->inactive_step);
1073: VecScale(cg->inactive_step, -1.0);
1074: VecRestoreSubVector(tao->stepdirection, cg->new_inactives, &cg->inactive_step);
1075: VecRestoreSubVector(cg->unprojected_gradient, cg->new_inactives, &cg->inactive_grad);
1076: }
1077: /* Verify that this is a descent direction */
1078: VecDot(tao->gradient, tao->stepdirection, &gd);
1079: VecNorm(tao->stepdirection, NORM_2, &dnorm);
1080: if (PetscIsInfOrNanReal(gd) || (gd/(dnorm*dnorm) <= -1e10 || gd/(dnorm*dnorm) >= -1e-10)) {
1081: /* Not a descent direction, so we reset back to projected gradient descent */
1082: TaoBNCGResetUpdate(tao, gnorm2);
1083: TaoBNCGBoundStep(tao, cg->as_type, tao->stepdirection);
1084: ++cg->descent_error;
1085: } else {
1086: }
1087: }
1088: return(0);
1089: }
1091: PetscErrorCode TaoBNCGSetH0(Tao tao, Mat H0)
1092: {
1093: TAO_BNCG *cg = (TAO_BNCG*)tao->data;
1094: PetscErrorCode ierr;
1097: PetscObjectReference((PetscObject)H0);
1098: cg->pc = H0;
1099: return(0);
1100: }