Actual source code: bnk.c

  1: #include <petsctaolinesearch.h>
  2: #include <../src/tao/bound/impls/bnk/bnk.h>
  3: #include <petscksp.h>

  5: static const char *BNK_INIT[64]   = {"constant", "direction", "interpolation"};
  6: static const char *BNK_UPDATE[64] = {"step", "reduction", "interpolation"};
  7: static const char *BNK_AS[64]     = {"none", "bertsekas"};

  9: /* Extracts from the full Hessian the part associated with the current bnk->inactive_idx and set the PCLMVM preconditioner */

 11: static PetscErrorCode TaoBNKComputeSubHessian(Tao tao)
 12: {
 13:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

 15:   PetscFunctionBegin;
 16:   PetscCall(MatDestroy(&bnk->Hpre_inactive));
 17:   PetscCall(MatDestroy(&bnk->H_inactive));
 18:   if (bnk->active_idx) {
 19:     PetscCall(MatCreateSubMatrix(tao->hessian, bnk->inactive_idx, bnk->inactive_idx, MAT_INITIAL_MATRIX, &bnk->H_inactive));
 20:     if (tao->hessian == tao->hessian_pre) {
 21:       PetscCall(PetscObjectReference((PetscObject)bnk->H_inactive));
 22:       bnk->Hpre_inactive = bnk->H_inactive;
 23:     } else {
 24:       PetscCall(MatCreateSubMatrix(tao->hessian_pre, bnk->inactive_idx, bnk->inactive_idx, MAT_INITIAL_MATRIX, &bnk->Hpre_inactive));
 25:     }
 26:     if (bnk->bfgs_pre) PetscCall(PCLMVMSetIS(bnk->bfgs_pre, bnk->inactive_idx));
 27:   } else {
 28:     PetscCall(PetscObjectReference((PetscObject)tao->hessian));
 29:     bnk->H_inactive = tao->hessian;
 30:     PetscCall(PetscObjectReference((PetscObject)tao->hessian_pre));
 31:     bnk->Hpre_inactive = tao->hessian_pre;
 32:     if (bnk->bfgs_pre) PetscCall(PCLMVMClearIS(bnk->bfgs_pre));
 33:   }
 34:   PetscFunctionReturn(PETSC_SUCCESS);
 35: }

 37: /* Initializes the KSP solver, the BFGS preconditioner, and the initial trust radius estimation */

 39: PetscErrorCode TaoBNKInitialize(Tao tao, PetscInt initType, PetscBool *needH)
 40: {
 41:   TAO_BNK  *bnk = (TAO_BNK *)tao->data;
 42:   PC        pc;
 43:   PetscReal f_min, ftrial, prered, actred, kappa, sigma, resnorm;
 44:   PetscReal tau, tau_1, tau_2, tau_max, tau_min, max_radius;
 45:   PetscBool is_bfgs, is_jacobi, is_symmetric, sym_set;
 46:   PetscInt  n, N, nDiff;
 47:   PetscInt  i_max = 5;
 48:   PetscInt  j_max = 1;
 49:   PetscInt  i, j;
 50:   PetscBool kspTR;

 52:   PetscFunctionBegin;
 53:   /* Project the current point onto the feasible set */
 54:   PetscCall(TaoComputeVariableBounds(tao));
 55:   PetscCall(TaoSetVariableBounds(bnk->bncg, tao->XL, tao->XU));
 56:   if (tao->bounded) PetscCall(TaoLineSearchSetVariableBounds(tao->linesearch, tao->XL, tao->XU));

 58:   /* Project the initial point onto the feasible region */
 59:   PetscCall(TaoBoundSolution(tao->solution, tao->XL, tao->XU, 0.0, &nDiff, tao->solution));

 61:   /* Check convergence criteria */
 62:   PetscCall(TaoComputeObjectiveAndGradient(tao, tao->solution, &bnk->f, bnk->unprojected_gradient));
 63:   PetscCall(TaoBNKEstimateActiveSet(tao, bnk->as_type));
 64:   PetscCall(VecCopy(bnk->unprojected_gradient, tao->gradient));
 65:   if (bnk->active_idx) PetscCall(VecISSet(tao->gradient, bnk->active_idx, 0.0));
 66:   PetscCall(TaoGradientNorm(tao, tao->gradient, NORM_2, &bnk->gnorm));

 68:   /* Test the initial point for convergence */
 69:   PetscCall(VecFischer(tao->solution, bnk->unprojected_gradient, tao->XL, tao->XU, bnk->W));
 70:   PetscCall(VecNorm(bnk->W, NORM_2, &resnorm));
 71:   PetscCheck(!PetscIsInfOrNanReal(bnk->f) && !PetscIsInfOrNanReal(resnorm), PetscObjectComm((PetscObject)tao), PETSC_ERR_USER, "User provided compute function generated infinity or NaN");
 72:   PetscCall(TaoLogConvergenceHistory(tao, bnk->f, resnorm, 0.0, tao->ksp_its));
 73:   PetscCall(TaoMonitor(tao, tao->niter, bnk->f, resnorm, 0.0, 1.0));
 74:   PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
 75:   if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);

 77:   /* Reset KSP stopping reason counters */
 78:   bnk->ksp_atol = 0;
 79:   bnk->ksp_rtol = 0;
 80:   bnk->ksp_dtol = 0;
 81:   bnk->ksp_ctol = 0;
 82:   bnk->ksp_negc = 0;
 83:   bnk->ksp_iter = 0;
 84:   bnk->ksp_othr = 0;

 86:   /* Reset accepted step type counters */
 87:   bnk->tot_cg_its = 0;
 88:   bnk->newt       = 0;
 89:   bnk->bfgs       = 0;
 90:   bnk->sgrad      = 0;
 91:   bnk->grad       = 0;

 93:   /* Initialize the Hessian perturbation */
 94:   bnk->pert = bnk->sval;

 96:   /* Reset initial steplength to zero (this helps BNCG reset its direction internally) */
 97:   PetscCall(VecSet(tao->stepdirection, 0.0));

 99:   /* Allocate the vectors needed for the BFGS approximation */
100:   PetscCall(KSPGetPC(tao->ksp, &pc));
101:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCLMVM, &is_bfgs));
102:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCJACOBI, &is_jacobi));
103:   if (is_bfgs) {
104:     bnk->bfgs_pre = pc;
105:     PetscCall(PCLMVMGetMatLMVM(bnk->bfgs_pre, &bnk->M));
106:     PetscCall(VecGetLocalSize(tao->solution, &n));
107:     PetscCall(VecGetSize(tao->solution, &N));
108:     PetscCall(MatSetSizes(bnk->M, n, n, N, N));
109:     PetscCall(MatLMVMAllocate(bnk->M, tao->solution, bnk->unprojected_gradient));
110:     PetscCall(MatIsSymmetricKnown(bnk->M, &sym_set, &is_symmetric));
111:     PetscCheck(sym_set && is_symmetric, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_INCOMP, "LMVM matrix in the LMVM preconditioner must be symmetric.");
112:   } else if (is_jacobi) PetscCall(PCJacobiSetUseAbs(pc, PETSC_TRUE));

114:   /* Prepare the min/max vectors for safeguarding diagonal scales */
115:   PetscCall(VecSet(bnk->Diag_min, bnk->dmin));
116:   PetscCall(VecSet(bnk->Diag_max, bnk->dmax));

118:   /* Initialize trust-region radius.  The initialization is only performed
119:      when we are using Nash, Steihaug-Toint or the Generalized Lanczos method. */
120:   *needH = PETSC_TRUE;
121:   PetscCall(PetscObjectHasFunction((PetscObject)tao->ksp, "KSPCGSetRadius_C", &kspTR));
122:   if (kspTR) {
123:     switch (initType) {
124:     case BNK_INIT_CONSTANT:
125:       /* Use the initial radius specified */
126:       tao->trust = tao->trust0;
127:       break;

129:     case BNK_INIT_INTERPOLATION:
130:       /* Use interpolation based on the initial Hessian */
131:       max_radius = 0.0;
132:       tao->trust = tao->trust0;
133:       for (j = 0; j < j_max; ++j) {
134:         f_min = bnk->f;
135:         sigma = 0.0;

137:         if (*needH) {
138:           /* Compute the Hessian at the new step, and extract the inactive subsystem */
139:           PetscCall((*bnk->computehessian)(tao));
140:           PetscCall(TaoBNKEstimateActiveSet(tao, BNK_AS_NONE));
141:           PetscCall(TaoBNKComputeSubHessian(tao));
142:           *needH = PETSC_FALSE;
143:         }

145:         for (i = 0; i < i_max; ++i) {
146:           /* Take a steepest descent step and snap it to bounds */
147:           PetscCall(VecCopy(tao->solution, bnk->Xold));
148:           PetscCall(VecAXPY(tao->solution, -tao->trust / bnk->gnorm, tao->gradient));
149:           PetscCall(TaoBoundSolution(tao->solution, tao->XL, tao->XU, 0.0, &nDiff, tao->solution));
150:           /* Compute the step we actually accepted */
151:           PetscCall(VecCopy(tao->solution, bnk->W));
152:           PetscCall(VecAXPY(bnk->W, -1.0, bnk->Xold));
153:           /* Compute the objective at the trial */
154:           PetscCall(TaoComputeObjective(tao, tao->solution, &ftrial));
155:           PetscCheck(!PetscIsInfOrNanReal(bnk->f), PetscObjectComm((PetscObject)tao), PETSC_ERR_USER, "User provided compute function generated infinity or NaN");
156:           PetscCall(VecCopy(bnk->Xold, tao->solution));
157:           if (PetscIsInfOrNanReal(ftrial)) {
158:             tau = bnk->gamma1_i;
159:           } else {
160:             if (ftrial < f_min) {
161:               f_min = ftrial;
162:               sigma = -tao->trust / bnk->gnorm;
163:             }

165:             /* Compute the predicted and actual reduction */
166:             if (bnk->active_idx) {
167:               PetscCall(VecGetSubVector(bnk->W, bnk->inactive_idx, &bnk->X_inactive));
168:               PetscCall(VecGetSubVector(bnk->Xwork, bnk->inactive_idx, &bnk->inactive_work));
169:             } else {
170:               bnk->X_inactive    = bnk->W;
171:               bnk->inactive_work = bnk->Xwork;
172:             }
173:             PetscCall(MatMult(bnk->H_inactive, bnk->X_inactive, bnk->inactive_work));
174:             PetscCall(VecDot(bnk->X_inactive, bnk->inactive_work, &prered));
175:             if (bnk->active_idx) {
176:               PetscCall(VecRestoreSubVector(bnk->W, bnk->inactive_idx, &bnk->X_inactive));
177:               PetscCall(VecRestoreSubVector(bnk->Xwork, bnk->inactive_idx, &bnk->inactive_work));
178:             }
179:             prered = tao->trust * (bnk->gnorm - 0.5 * tao->trust * prered / (bnk->gnorm * bnk->gnorm));
180:             actred = bnk->f - ftrial;
181:             if ((PetscAbsScalar(actred) <= bnk->epsilon) && (PetscAbsScalar(prered) <= bnk->epsilon)) {
182:               kappa = 1.0;
183:             } else {
184:               kappa = actred / prered;
185:             }

187:             tau_1   = bnk->theta_i * bnk->gnorm * tao->trust / (bnk->theta_i * bnk->gnorm * tao->trust + (1.0 - bnk->theta_i) * prered - actred);
188:             tau_2   = bnk->theta_i * bnk->gnorm * tao->trust / (bnk->theta_i * bnk->gnorm * tao->trust - (1.0 + bnk->theta_i) * prered + actred);
189:             tau_min = PetscMin(tau_1, tau_2);
190:             tau_max = PetscMax(tau_1, tau_2);

192:             if (PetscAbsScalar(kappa - (PetscReal)1.0) <= bnk->mu1_i) {
193:               /*  Great agreement */
194:               max_radius = PetscMax(max_radius, tao->trust);

196:               if (tau_max < 1.0) {
197:                 tau = bnk->gamma3_i;
198:               } else if (tau_max > bnk->gamma4_i) {
199:                 tau = bnk->gamma4_i;
200:               } else {
201:                 tau = tau_max;
202:               }
203:             } else if (PetscAbsScalar(kappa - (PetscReal)1.0) <= bnk->mu2_i) {
204:               /*  Good agreement */
205:               max_radius = PetscMax(max_radius, tao->trust);

207:               if (tau_max < bnk->gamma2_i) {
208:                 tau = bnk->gamma2_i;
209:               } else if (tau_max > bnk->gamma3_i) {
210:                 tau = bnk->gamma3_i;
211:               } else {
212:                 tau = tau_max;
213:               }
214:             } else {
215:               /*  Not good agreement */
216:               if (tau_min > 1.0) {
217:                 tau = bnk->gamma2_i;
218:               } else if (tau_max < bnk->gamma1_i) {
219:                 tau = bnk->gamma1_i;
220:               } else if ((tau_min < bnk->gamma1_i) && (tau_max >= 1.0)) {
221:                 tau = bnk->gamma1_i;
222:               } else if ((tau_1 >= bnk->gamma1_i) && (tau_1 < 1.0) && ((tau_2 < bnk->gamma1_i) || (tau_2 >= 1.0))) {
223:                 tau = tau_1;
224:               } else if ((tau_2 >= bnk->gamma1_i) && (tau_2 < 1.0) && ((tau_1 < bnk->gamma1_i) || (tau_2 >= 1.0))) {
225:                 tau = tau_2;
226:               } else {
227:                 tau = tau_max;
228:               }
229:             }
230:           }
231:           tao->trust = tau * tao->trust;
232:         }

234:         if (f_min < bnk->f) {
235:           /* We accidentally found a solution better than the initial, so accept it */
236:           bnk->f = f_min;
237:           PetscCall(VecCopy(tao->solution, bnk->Xold));
238:           PetscCall(VecAXPY(tao->solution, sigma, tao->gradient));
239:           PetscCall(TaoBoundSolution(tao->solution, tao->XL, tao->XU, 0.0, &nDiff, tao->solution));
240:           PetscCall(VecCopy(tao->solution, tao->stepdirection));
241:           PetscCall(VecAXPY(tao->stepdirection, -1.0, bnk->Xold));
242:           PetscCall(TaoComputeGradient(tao, tao->solution, bnk->unprojected_gradient));
243:           PetscCall(TaoBNKEstimateActiveSet(tao, bnk->as_type));
244:           PetscCall(VecCopy(bnk->unprojected_gradient, tao->gradient));
245:           if (bnk->active_idx) PetscCall(VecISSet(tao->gradient, bnk->active_idx, 0.0));
246:           /* Compute gradient at the new iterate and flip switch to compute the Hessian later */
247:           PetscCall(TaoGradientNorm(tao, tao->gradient, NORM_2, &bnk->gnorm));
248:           *needH = PETSC_TRUE;
249:           /* Test the new step for convergence */
250:           PetscCall(VecFischer(tao->solution, bnk->unprojected_gradient, tao->XL, tao->XU, bnk->W));
251:           PetscCall(VecNorm(bnk->W, NORM_2, &resnorm));
252:           PetscCheck(!PetscIsInfOrNanReal(resnorm), PetscObjectComm((PetscObject)tao), PETSC_ERR_USER, "User provided compute function generated infinity or NaN");
253:           PetscCall(TaoLogConvergenceHistory(tao, bnk->f, resnorm, 0.0, tao->ksp_its));
254:           PetscCall(TaoMonitor(tao, tao->niter, bnk->f, resnorm, 0.0, 1.0));
255:           PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
256:           if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);
257:           /* active BNCG recycling early because we have a stepdirection computed */
258:           PetscCall(TaoSetRecycleHistory(bnk->bncg, PETSC_TRUE));
259:         }
260:       }
261:       tao->trust = PetscMax(tao->trust, max_radius);

263:       /* Ensure that the trust radius is within the limits */
264:       tao->trust = PetscMax(tao->trust, bnk->min_radius);
265:       tao->trust = PetscMin(tao->trust, bnk->max_radius);
266:       break;

268:     default:
269:       /* Norm of the first direction will initialize radius */
270:       tao->trust = 0.0;
271:       break;
272:     }
273:   }
274:   PetscFunctionReturn(PETSC_SUCCESS);
275: }

277: /* Computes the exact Hessian and extracts its subHessian */

279: PetscErrorCode TaoBNKComputeHessian(Tao tao)
280: {
281:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

283:   PetscFunctionBegin;
284:   /* Compute the Hessian */
285:   PetscCall(TaoComputeHessian(tao, tao->solution, tao->hessian, tao->hessian_pre));
286:   /* Add a correction to the BFGS preconditioner */
287:   if (bnk->M) PetscCall(MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient));
288:   /* Prepare the reduced sub-matrices for the inactive set */
289:   PetscCall(TaoBNKComputeSubHessian(tao));
290:   PetscFunctionReturn(PETSC_SUCCESS);
291: }

293: /* Routine for estimating the active set */

295: PetscErrorCode TaoBNKEstimateActiveSet(Tao tao, PetscInt asType)
296: {
297:   TAO_BNK  *bnk = (TAO_BNK *)tao->data;
298:   PetscBool hessComputed, diagExists, hadactive;

300:   PetscFunctionBegin;
301:   hadactive = bnk->active_idx ? PETSC_TRUE : PETSC_FALSE;
302:   switch (asType) {
303:   case BNK_AS_NONE:
304:     PetscCall(ISDestroy(&bnk->inactive_idx));
305:     PetscCall(VecWhichInactive(tao->XL, tao->solution, bnk->unprojected_gradient, tao->XU, PETSC_TRUE, &bnk->inactive_idx));
306:     PetscCall(ISDestroy(&bnk->active_idx));
307:     PetscCall(ISComplementVec(bnk->inactive_idx, tao->solution, &bnk->active_idx));
308:     break;

310:   case BNK_AS_BERTSEKAS:
311:     /* Compute the trial step vector with which we will estimate the active set at the next iteration */
312:     if (bnk->M) {
313:       /* If the BFGS matrix is available, we will construct a trial step with it */
314:       PetscCall(MatSolve(bnk->M, bnk->unprojected_gradient, bnk->W));
315:     } else {
316:       hessComputed = diagExists = PETSC_FALSE;
317:       if (tao->hessian) PetscCall(MatAssembled(tao->hessian, &hessComputed));
318:       if (hessComputed) PetscCall(MatHasOperation(tao->hessian, MATOP_GET_DIAGONAL, &diagExists));
319:       if (diagExists) {
320:         /* BFGS preconditioner doesn't exist so let's invert the absolute diagonal of the Hessian instead onto the gradient */
321:         PetscCall(MatGetDiagonal(tao->hessian, bnk->Xwork));
322:         PetscCall(VecAbs(bnk->Xwork));
323:         PetscCall(VecMedian(bnk->Diag_min, bnk->Xwork, bnk->Diag_max, bnk->Xwork));
324:         PetscCall(VecReciprocal(bnk->Xwork));
325:         PetscCall(VecPointwiseMult(bnk->W, bnk->Xwork, bnk->unprojected_gradient));
326:       } else {
327:         /* If the Hessian or its diagonal does not exist, we will simply use gradient step */
328:         PetscCall(VecCopy(bnk->unprojected_gradient, bnk->W));
329:       }
330:     }
331:     PetscCall(VecScale(bnk->W, -1.0));
332:     PetscCall(TaoEstimateActiveBounds(tao->solution, tao->XL, tao->XU, bnk->unprojected_gradient, bnk->W, bnk->Xwork, bnk->as_step, &bnk->as_tol, &bnk->active_lower, &bnk->active_upper, &bnk->active_fixed, &bnk->active_idx, &bnk->inactive_idx));
333:     break;

335:   default:
336:     break;
337:   }
338:   bnk->resetksp = (PetscBool)(bnk->active_idx || hadactive); /* inactive Hessian size may have changed, need to reset operators */
339:   PetscFunctionReturn(PETSC_SUCCESS);
340: }

342: /* Routine for bounding the step direction */

344: PetscErrorCode TaoBNKBoundStep(Tao tao, PetscInt asType, Vec step)
345: {
346:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

348:   PetscFunctionBegin;
349:   switch (asType) {
350:   case BNK_AS_NONE:
351:     if (bnk->active_idx) PetscCall(VecISSet(step, bnk->active_idx, 0.0));
352:     break;
353:   case BNK_AS_BERTSEKAS:
354:     PetscCall(TaoBoundStep(tao->solution, tao->XL, tao->XU, bnk->active_lower, bnk->active_upper, bnk->active_fixed, 1.0, step));
355:     break;
356:   default:
357:     break;
358:   }
359:   PetscFunctionReturn(PETSC_SUCCESS);
360: }

362: /* Routine for taking a finite number of BNCG iterations to
363:    accelerate Newton convergence.

365:    In practice, this approach simply trades off Hessian evaluations
366:    for more gradient evaluations.
367: */

369: PetscErrorCode TaoBNKTakeCGSteps(Tao tao, PetscBool *terminate)
370: {
371:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

373:   PetscFunctionBegin;
374:   *terminate = PETSC_FALSE;
375:   if (bnk->max_cg_its > 0) {
376:     /* Copy the current function value (important vectors are already shared) */
377:     bnk->bncg_ctx->f = bnk->f;
378:     /* Take some small finite number of BNCG iterations */
379:     PetscCall(TaoSolve(bnk->bncg));
380:     /* Add the number of gradient and function evaluations to the total */
381:     tao->nfuncs += bnk->bncg->nfuncs;
382:     tao->nfuncgrads += bnk->bncg->nfuncgrads;
383:     tao->ngrads += bnk->bncg->ngrads;
384:     tao->nhess += bnk->bncg->nhess;
385:     bnk->tot_cg_its += bnk->bncg->niter;
386:     /* Extract the BNCG function value out and save it into BNK */
387:     bnk->f = bnk->bncg_ctx->f;
388:     if (bnk->bncg->reason == TAO_CONVERGED_GATOL || bnk->bncg->reason == TAO_CONVERGED_GRTOL || bnk->bncg->reason == TAO_CONVERGED_GTTOL || bnk->bncg->reason == TAO_CONVERGED_MINF) {
389:       *terminate = PETSC_TRUE;
390:     } else {
391:       PetscCall(TaoBNKEstimateActiveSet(tao, bnk->as_type));
392:     }
393:   }
394:   PetscFunctionReturn(PETSC_SUCCESS);
395: }

397: /* Routine for computing the Newton step. */

399: PetscErrorCode TaoBNKComputeStep(Tao tao, PetscBool shift, KSPConvergedReason *ksp_reason, PetscInt *step_type)
400: {
401:   TAO_BNK  *bnk         = (TAO_BNK *)tao->data;
402:   PetscInt  bfgsUpdates = 0;
403:   PetscInt  kspits;
404:   PetscBool is_lmvm;
405:   PetscBool kspTR;

407:   PetscFunctionBegin;
408:   /* If there are no inactive variables left, save some computation and return an adjusted zero step
409:      that has (l-x) and (u-x) for lower and upper bounded variables. */
410:   if (!bnk->inactive_idx) {
411:     PetscCall(VecSet(tao->stepdirection, 0.0));
412:     PetscCall(TaoBNKBoundStep(tao, bnk->as_type, tao->stepdirection));
413:     PetscFunctionReturn(PETSC_SUCCESS);
414:   }

416:   /* Shift the reduced Hessian matrix */
417:   if (shift && bnk->pert > 0) {
418:     PetscCall(PetscObjectTypeCompare((PetscObject)tao->hessian, MATLMVM, &is_lmvm));
419:     if (is_lmvm) {
420:       PetscCall(MatShift(tao->hessian, bnk->pert));
421:     } else {
422:       PetscCall(MatShift(bnk->H_inactive, bnk->pert));
423:       if (bnk->H_inactive != bnk->Hpre_inactive) PetscCall(MatShift(bnk->Hpre_inactive, bnk->pert));
424:     }
425:   }

427:   /* Solve the Newton system of equations */
428:   tao->ksp_its = 0;
429:   PetscCall(VecSet(tao->stepdirection, 0.0));
430:   if (bnk->resetksp) {
431:     PetscCall(KSPReset(tao->ksp));
432:     PetscCall(KSPResetFromOptions(tao->ksp));
433:     bnk->resetksp = PETSC_FALSE;
434:   }
435:   PetscCall(KSPSetOperators(tao->ksp, bnk->H_inactive, bnk->Hpre_inactive));
436:   PetscCall(VecCopy(bnk->unprojected_gradient, bnk->Gwork));
437:   if (bnk->active_idx) {
438:     PetscCall(VecGetSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive));
439:     PetscCall(VecGetSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive));
440:   } else {
441:     bnk->G_inactive = bnk->unprojected_gradient;
442:     bnk->X_inactive = tao->stepdirection;
443:   }
444:   PetscCall(KSPCGSetRadius(tao->ksp, tao->trust));
445:   PetscCall(KSPSolve(tao->ksp, bnk->G_inactive, bnk->X_inactive));
446:   PetscCall(KSPGetIterationNumber(tao->ksp, &kspits));
447:   tao->ksp_its += kspits;
448:   tao->ksp_tot_its += kspits;
449:   PetscCall(PetscObjectHasFunction((PetscObject)tao->ksp, "KSPCGGetNormD_C", &kspTR));
450:   if (kspTR) {
451:     PetscCall(KSPCGGetNormD(tao->ksp, &bnk->dnorm));

453:     if (0.0 == tao->trust) {
454:       /* Radius was uninitialized; use the norm of the direction */
455:       if (bnk->dnorm > 0.0) {
456:         tao->trust = bnk->dnorm;

458:         /* Modify the radius if it is too large or small */
459:         tao->trust = PetscMax(tao->trust, bnk->min_radius);
460:         tao->trust = PetscMin(tao->trust, bnk->max_radius);
461:       } else {
462:         /* The direction was bad; set radius to default value and re-solve
463:            the trust-region subproblem to get a direction */
464:         tao->trust = tao->trust0;

466:         /* Modify the radius if it is too large or small */
467:         tao->trust = PetscMax(tao->trust, bnk->min_radius);
468:         tao->trust = PetscMin(tao->trust, bnk->max_radius);

470:         PetscCall(KSPCGSetRadius(tao->ksp, tao->trust));
471:         PetscCall(KSPSolve(tao->ksp, bnk->G_inactive, bnk->X_inactive));
472:         PetscCall(KSPGetIterationNumber(tao->ksp, &kspits));
473:         tao->ksp_its += kspits;
474:         tao->ksp_tot_its += kspits;
475:         PetscCall(KSPCGGetNormD(tao->ksp, &bnk->dnorm));

477:         PetscCheck(bnk->dnorm != 0.0, PetscObjectComm((PetscObject)tao), PETSC_ERR_PLIB, "Initial direction zero");
478:       }
479:     }
480:   }
481:   /* Restore sub vectors back */
482:   if (bnk->active_idx) {
483:     PetscCall(VecRestoreSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive));
484:     PetscCall(VecRestoreSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive));
485:   }
486:   /* Make sure the safeguarded fall-back step is zero for actively bounded variables */
487:   PetscCall(VecScale(tao->stepdirection, -1.0));
488:   PetscCall(TaoBNKBoundStep(tao, bnk->as_type, tao->stepdirection));

490:   /* Record convergence reasons */
491:   PetscCall(KSPGetConvergedReason(tao->ksp, ksp_reason));
492:   if (KSP_CONVERGED_ATOL == *ksp_reason) {
493:     ++bnk->ksp_atol;
494:   } else if (KSP_CONVERGED_RTOL == *ksp_reason) {
495:     ++bnk->ksp_rtol;
496:   } else if (KSP_CONVERGED_STEP_LENGTH == *ksp_reason) {
497:     ++bnk->ksp_ctol;
498:   } else if (KSP_CONVERGED_NEG_CURVE == *ksp_reason) {
499:     ++bnk->ksp_negc;
500:   } else if (KSP_DIVERGED_DTOL == *ksp_reason) {
501:     ++bnk->ksp_dtol;
502:   } else if (KSP_DIVERGED_ITS == *ksp_reason) {
503:     ++bnk->ksp_iter;
504:   } else {
505:     ++bnk->ksp_othr;
506:   }

508:   /* Make sure the BFGS preconditioner is healthy */
509:   if (bnk->M) {
510:     PetscCall(MatLMVMGetUpdateCount(bnk->M, &bfgsUpdates));
511:     if ((KSP_DIVERGED_INDEFINITE_PC == *ksp_reason) && (bfgsUpdates > 0)) {
512:       /* Preconditioner is numerically indefinite; reset the approximation. */
513:       PetscCall(MatLMVMReset(bnk->M, PETSC_FALSE));
514:       PetscCall(MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient));
515:     }
516:   }
517:   *step_type = BNK_NEWTON;
518:   PetscFunctionReturn(PETSC_SUCCESS);
519: }

521: /* Routine for recomputing the predicted reduction for a given step vector */

523: PetscErrorCode TaoBNKRecomputePred(Tao tao, Vec S, PetscReal *prered)
524: {
525:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

527:   PetscFunctionBegin;
528:   /* Extract subvectors associated with the inactive set */
529:   if (bnk->active_idx) {
530:     PetscCall(VecGetSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive));
531:     PetscCall(VecGetSubVector(bnk->Xwork, bnk->inactive_idx, &bnk->inactive_work));
532:     PetscCall(VecGetSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive));
533:   } else {
534:     bnk->X_inactive    = tao->stepdirection;
535:     bnk->inactive_work = bnk->Xwork;
536:     bnk->G_inactive    = bnk->Gwork;
537:   }
538:   /* Recompute the predicted decrease based on the quadratic model */
539:   PetscCall(MatMult(bnk->H_inactive, bnk->X_inactive, bnk->inactive_work));
540:   PetscCall(VecAYPX(bnk->inactive_work, -0.5, bnk->G_inactive));
541:   PetscCall(VecDot(bnk->inactive_work, bnk->X_inactive, prered));
542:   /* Restore the sub vectors */
543:   if (bnk->active_idx) {
544:     PetscCall(VecRestoreSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive));
545:     PetscCall(VecRestoreSubVector(bnk->Xwork, bnk->inactive_idx, &bnk->inactive_work));
546:     PetscCall(VecRestoreSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive));
547:   }
548:   PetscFunctionReturn(PETSC_SUCCESS);
549: }

551: /* Routine for ensuring that the Newton step is a descent direction.

553:    The step direction falls back onto BFGS, scaled gradient and gradient steps
554:    in the event that the Newton step fails the test.
555: */

557: PetscErrorCode TaoBNKSafeguardStep(Tao tao, KSPConvergedReason ksp_reason, PetscInt *stepType)
558: {
559:   TAO_BNK  *bnk = (TAO_BNK *)tao->data;
560:   PetscReal gdx, e_min;
561:   PetscInt  bfgsUpdates;

563:   PetscFunctionBegin;
564:   switch (*stepType) {
565:   case BNK_NEWTON:
566:     PetscCall(VecDot(tao->stepdirection, tao->gradient, &gdx));
567:     if ((gdx >= 0.0) || PetscIsInfOrNanReal(gdx)) {
568:       /* Newton step is not descent or direction produced infinity or NaN
569:         Update the perturbation for next time */
570:       if (bnk->pert <= 0.0) {
571:         PetscBool is_gltr;

573:         /* Initialize the perturbation */
574:         bnk->pert = PetscMin(bnk->imax, PetscMax(bnk->imin, bnk->imfac * bnk->gnorm));
575:         PetscCall(PetscObjectTypeCompare((PetscObject)tao->ksp, KSPGLTR, &is_gltr));
576:         if (is_gltr) {
577:           PetscCall(KSPGLTRGetMinEig(tao->ksp, &e_min));
578:           bnk->pert = PetscMax(bnk->pert, -e_min);
579:         }
580:       } else {
581:         /* Increase the perturbation */
582:         bnk->pert = PetscMin(bnk->pmax, PetscMax(bnk->pgfac * bnk->pert, bnk->pmgfac * bnk->gnorm));
583:       }

585:       if (!bnk->M) {
586:         /* We don't have the bfgs matrix around and updated
587:           Must use gradient direction in this case */
588:         PetscCall(VecCopy(tao->gradient, tao->stepdirection));
589:         *stepType = BNK_GRADIENT;
590:       } else {
591:         /* Attempt to use the BFGS direction */
592:         PetscCall(MatSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection));

594:         /* Check for success (descent direction)
595:           NOTE: Negative gdx here means not a descent direction because
596:           the fall-back step is missing a negative sign. */
597:         PetscCall(VecDot(tao->gradient, tao->stepdirection, &gdx));
598:         if ((gdx <= 0.0) || PetscIsInfOrNanReal(gdx)) {
599:           /* BFGS direction is not descent or direction produced not a number
600:             We can assert bfgsUpdates > 1 in this case because
601:             the first solve produces the scaled gradient direction,
602:             which is guaranteed to be descent */

604:           /* Use steepest descent direction (scaled) */
605:           PetscCall(MatLMVMReset(bnk->M, PETSC_FALSE));
606:           PetscCall(MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient));
607:           PetscCall(MatSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection));

609:           *stepType = BNK_SCALED_GRADIENT;
610:         } else {
611:           PetscCall(MatLMVMGetUpdateCount(bnk->M, &bfgsUpdates));
612:           if (1 == bfgsUpdates) {
613:             /* The first BFGS direction is always the scaled gradient */
614:             *stepType = BNK_SCALED_GRADIENT;
615:           } else {
616:             *stepType = BNK_BFGS;
617:           }
618:         }
619:       }
620:       /* Make sure the safeguarded fall-back step is zero for actively bounded variables */
621:       PetscCall(VecScale(tao->stepdirection, -1.0));
622:       PetscCall(TaoBNKBoundStep(tao, bnk->as_type, tao->stepdirection));
623:     } else {
624:       /* Computed Newton step is descent */
625:       switch (ksp_reason) {
626:       case KSP_DIVERGED_NANORINF:
627:       case KSP_DIVERGED_BREAKDOWN:
628:       case KSP_DIVERGED_INDEFINITE_MAT:
629:       case KSP_DIVERGED_INDEFINITE_PC:
630:       case KSP_CONVERGED_NEG_CURVE:
631:         /* Matrix or preconditioner is indefinite; increase perturbation */
632:         if (bnk->pert <= 0.0) {
633:           PetscBool is_gltr;

635:           /* Initialize the perturbation */
636:           bnk->pert = PetscMin(bnk->imax, PetscMax(bnk->imin, bnk->imfac * bnk->gnorm));
637:           PetscCall(PetscObjectTypeCompare((PetscObject)tao->ksp, KSPGLTR, &is_gltr));
638:           if (is_gltr) {
639:             PetscCall(KSPGLTRGetMinEig(tao->ksp, &e_min));
640:             bnk->pert = PetscMax(bnk->pert, -e_min);
641:           }
642:         } else {
643:           /* Increase the perturbation */
644:           bnk->pert = PetscMin(bnk->pmax, PetscMax(bnk->pgfac * bnk->pert, bnk->pmgfac * bnk->gnorm));
645:         }
646:         break;

648:       default:
649:         /* Newton step computation is good; decrease perturbation */
650:         bnk->pert = PetscMin(bnk->psfac * bnk->pert, bnk->pmsfac * bnk->gnorm);
651:         if (bnk->pert < bnk->pmin) bnk->pert = 0.0;
652:         break;
653:       }
654:       *stepType = BNK_NEWTON;
655:     }
656:     break;

658:   case BNK_BFGS:
659:     /* Check for success (descent direction) */
660:     PetscCall(VecDot(tao->stepdirection, tao->gradient, &gdx));
661:     if (gdx >= 0 || PetscIsInfOrNanReal(gdx)) {
662:       /* Step is not descent or solve was not successful
663:          Use steepest descent direction (scaled) */
664:       PetscCall(MatLMVMReset(bnk->M, PETSC_FALSE));
665:       PetscCall(MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient));
666:       PetscCall(MatSolve(bnk->M, tao->gradient, tao->stepdirection));
667:       PetscCall(VecScale(tao->stepdirection, -1.0));
668:       PetscCall(TaoBNKBoundStep(tao, bnk->as_type, tao->stepdirection));
669:       *stepType = BNK_SCALED_GRADIENT;
670:     } else {
671:       *stepType = BNK_BFGS;
672:     }
673:     break;

675:   case BNK_SCALED_GRADIENT:
676:     break;

678:   default:
679:     break;
680:   }
681:   PetscFunctionReturn(PETSC_SUCCESS);
682: }

684: /* Routine for performing a bound-projected More-Thuente line search.

686:   Includes fallbacks to BFGS, scaled gradient, and unscaled gradient steps if the
687:   Newton step does not produce a valid step length.
688: */

690: PetscErrorCode TaoBNKPerformLineSearch(Tao tao, PetscInt *stepType, PetscReal *steplen, TaoLineSearchConvergedReason *reason)
691: {
692:   TAO_BNK                     *bnk = (TAO_BNK *)tao->data;
693:   TaoLineSearchConvergedReason ls_reason;
694:   PetscReal                    e_min, gdx;
695:   PetscInt                     bfgsUpdates;

697:   PetscFunctionBegin;
698:   /* Perform the linesearch */
699:   PetscCall(TaoLineSearchApply(tao->linesearch, tao->solution, &bnk->f, bnk->unprojected_gradient, tao->stepdirection, steplen, &ls_reason));
700:   PetscCall(TaoAddLineSearchCounts(tao));

702:   while (ls_reason != TAOLINESEARCH_SUCCESS && ls_reason != TAOLINESEARCH_SUCCESS_USER && *stepType != BNK_SCALED_GRADIENT && *stepType != BNK_GRADIENT) {
703:     /* Linesearch failed, revert solution */
704:     bnk->f = bnk->fold;
705:     PetscCall(VecCopy(bnk->Xold, tao->solution));
706:     PetscCall(VecCopy(bnk->unprojected_gradient_old, bnk->unprojected_gradient));

708:     switch (*stepType) {
709:     case BNK_NEWTON:
710:       /* Failed to obtain acceptable iterate with Newton step
711:          Update the perturbation for next time */
712:       if (bnk->pert <= 0.0) {
713:         PetscBool is_gltr;

715:         /* Initialize the perturbation */
716:         bnk->pert = PetscMin(bnk->imax, PetscMax(bnk->imin, bnk->imfac * bnk->gnorm));
717:         PetscCall(PetscObjectTypeCompare((PetscObject)tao->ksp, KSPGLTR, &is_gltr));
718:         if (is_gltr) {
719:           PetscCall(KSPGLTRGetMinEig(tao->ksp, &e_min));
720:           bnk->pert = PetscMax(bnk->pert, -e_min);
721:         }
722:       } else {
723:         /* Increase the perturbation */
724:         bnk->pert = PetscMin(bnk->pmax, PetscMax(bnk->pgfac * bnk->pert, bnk->pmgfac * bnk->gnorm));
725:       }

727:       if (!bnk->M) {
728:         /* We don't have the bfgs matrix around and being updated
729:            Must use gradient direction in this case */
730:         PetscCall(VecCopy(bnk->unprojected_gradient, tao->stepdirection));
731:         *stepType = BNK_GRADIENT;
732:       } else {
733:         /* Attempt to use the BFGS direction */
734:         PetscCall(MatSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection));
735:         /* Check for success (descent direction)
736:            NOTE: Negative gdx means not a descent direction because the step here is missing a negative sign. */
737:         PetscCall(VecDot(tao->gradient, tao->stepdirection, &gdx));
738:         if ((gdx <= 0.0) || PetscIsInfOrNanReal(gdx)) {
739:           /* BFGS direction is not descent or direction produced not a number
740:              We can assert bfgsUpdates > 1 in this case
741:              Use steepest descent direction (scaled) */
742:           PetscCall(MatLMVMReset(bnk->M, PETSC_FALSE));
743:           PetscCall(MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient));
744:           PetscCall(MatSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection));

746:           bfgsUpdates = 1;
747:           *stepType   = BNK_SCALED_GRADIENT;
748:         } else {
749:           PetscCall(MatLMVMGetUpdateCount(bnk->M, &bfgsUpdates));
750:           if (1 == bfgsUpdates) {
751:             /* The first BFGS direction is always the scaled gradient */
752:             *stepType = BNK_SCALED_GRADIENT;
753:           } else {
754:             *stepType = BNK_BFGS;
755:           }
756:         }
757:       }
758:       break;

760:     case BNK_BFGS:
761:       /* Can only enter if pc_type == BNK_PC_BFGS
762:          Failed to obtain acceptable iterate with BFGS step
763:          Attempt to use the scaled gradient direction */
764:       PetscCall(MatLMVMReset(bnk->M, PETSC_FALSE));
765:       PetscCall(MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient));
766:       PetscCall(MatSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection));

768:       bfgsUpdates = 1;
769:       *stepType   = BNK_SCALED_GRADIENT;
770:       break;
771:     }
772:     /* Make sure the safeguarded fall-back step is zero for actively bounded variables */
773:     PetscCall(VecScale(tao->stepdirection, -1.0));
774:     PetscCall(TaoBNKBoundStep(tao, bnk->as_type, tao->stepdirection));

776:     /* Perform one last line search with the fall-back step */
777:     PetscCall(TaoLineSearchApply(tao->linesearch, tao->solution, &bnk->f, bnk->unprojected_gradient, tao->stepdirection, steplen, &ls_reason));
778:     PetscCall(TaoAddLineSearchCounts(tao));
779:   }
780:   *reason = ls_reason;
781:   PetscFunctionReturn(PETSC_SUCCESS);
782: }

784: /* Routine for updating the trust radius.

786:   Function features three different update methods:
787:   1) Line-search step length based
788:   2) Predicted decrease on the CG quadratic model
789:   3) Interpolation
790: */

792: PetscErrorCode TaoBNKUpdateTrustRadius(Tao tao, PetscReal prered, PetscReal actred, PetscInt updateType, PetscInt stepType, PetscBool *accept)
793: {
794:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

796:   PetscReal step, kappa;
797:   PetscReal gdx, tau_1, tau_2, tau_min, tau_max;

799:   PetscFunctionBegin;
800:   /* Update trust region radius */
801:   *accept = PETSC_FALSE;
802:   switch (updateType) {
803:   case BNK_UPDATE_STEP:
804:     *accept = PETSC_TRUE; /* always accept here because line search succeeded */
805:     if (stepType == BNK_NEWTON) {
806:       PetscCall(TaoLineSearchGetStepLength(tao->linesearch, &step));
807:       if (step < bnk->nu1) {
808:         /* Very bad step taken; reduce radius */
809:         tao->trust = bnk->omega1 * PetscMin(bnk->dnorm, tao->trust);
810:       } else if (step < bnk->nu2) {
811:         /* Reasonably bad step taken; reduce radius */
812:         tao->trust = bnk->omega2 * PetscMin(bnk->dnorm, tao->trust);
813:       } else if (step < bnk->nu3) {
814:         /*  Reasonable step was taken; leave radius alone */
815:         if (bnk->omega3 < 1.0) {
816:           tao->trust = bnk->omega3 * PetscMin(bnk->dnorm, tao->trust);
817:         } else if (bnk->omega3 > 1.0) {
818:           tao->trust = PetscMax(bnk->omega3 * bnk->dnorm, tao->trust);
819:         }
820:       } else if (step < bnk->nu4) {
821:         /*  Full step taken; increase the radius */
822:         tao->trust = PetscMax(bnk->omega4 * bnk->dnorm, tao->trust);
823:       } else {
824:         /*  More than full step taken; increase the radius */
825:         tao->trust = PetscMax(bnk->omega5 * bnk->dnorm, tao->trust);
826:       }
827:     } else {
828:       /*  Newton step was not good; reduce the radius */
829:       tao->trust = bnk->omega1 * PetscMin(bnk->dnorm, tao->trust);
830:     }
831:     break;

833:   case BNK_UPDATE_REDUCTION:
834:     if (stepType == BNK_NEWTON) {
835:       if ((prered < 0.0) || PetscIsInfOrNanReal(prered)) {
836:         /* The predicted reduction has the wrong sign.  This cannot
837:            happen in infinite precision arithmetic.  Step should
838:            be rejected! */
839:         tao->trust = bnk->alpha1 * PetscMin(tao->trust, bnk->dnorm);
840:       } else {
841:         if (PetscIsInfOrNanReal(actred)) {
842:           tao->trust = bnk->alpha1 * PetscMin(tao->trust, bnk->dnorm);
843:         } else {
844:           if ((PetscAbsScalar(actred) <= PetscMax(1.0, PetscAbsScalar(bnk->f)) * bnk->epsilon) && (PetscAbsScalar(prered) <= PetscMax(1.0, PetscAbsScalar(bnk->f)) * bnk->epsilon)) {
845:             kappa = 1.0;
846:           } else {
847:             kappa = actred / prered;
848:           }
849:           /* Accept or reject the step and update radius */
850:           if (kappa < bnk->eta1) {
851:             /* Reject the step */
852:             tao->trust = bnk->alpha1 * PetscMin(tao->trust, bnk->dnorm);
853:           } else {
854:             /* Accept the step */
855:             *accept = PETSC_TRUE;
856:             /* Update the trust region radius only if the computed step is at the trust radius boundary */
857:             if (bnk->dnorm == tao->trust) {
858:               if (kappa < bnk->eta2) {
859:                 /* Marginal bad step */
860:                 tao->trust = bnk->alpha2 * tao->trust;
861:               } else if (kappa < bnk->eta3) {
862:                 /* Reasonable step */
863:                 tao->trust = bnk->alpha3 * tao->trust;
864:               } else if (kappa < bnk->eta4) {
865:                 /* Good step */
866:                 tao->trust = bnk->alpha4 * tao->trust;
867:               } else {
868:                 /* Very good step */
869:                 tao->trust = bnk->alpha5 * tao->trust;
870:               }
871:             }
872:           }
873:         }
874:       }
875:     } else {
876:       /*  Newton step was not good; reduce the radius */
877:       tao->trust = bnk->alpha1 * PetscMin(bnk->dnorm, tao->trust);
878:     }
879:     break;

881:   default:
882:     if (stepType == BNK_NEWTON) {
883:       if (prered < 0.0) {
884:         /*  The predicted reduction has the wrong sign.  This cannot */
885:         /*  happen in infinite precision arithmetic.  Step should */
886:         /*  be rejected! */
887:         tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm);
888:       } else {
889:         if (PetscIsInfOrNanReal(actred)) {
890:           tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm);
891:         } else {
892:           if ((PetscAbsScalar(actred) <= bnk->epsilon) && (PetscAbsScalar(prered) <= bnk->epsilon)) {
893:             kappa = 1.0;
894:           } else {
895:             kappa = actred / prered;
896:           }

898:           PetscCall(VecDot(tao->gradient, tao->stepdirection, &gdx));
899:           tau_1   = bnk->theta * gdx / (bnk->theta * gdx - (1.0 - bnk->theta) * prered + actred);
900:           tau_2   = bnk->theta * gdx / (bnk->theta * gdx + (1.0 + bnk->theta) * prered - actred);
901:           tau_min = PetscMin(tau_1, tau_2);
902:           tau_max = PetscMax(tau_1, tau_2);

904:           if (kappa >= 1.0 - bnk->mu1) {
905:             /*  Great agreement */
906:             *accept = PETSC_TRUE;
907:             if (tau_max < 1.0) {
908:               tao->trust = PetscMax(tao->trust, bnk->gamma3 * bnk->dnorm);
909:             } else if (tau_max > bnk->gamma4) {
910:               tao->trust = PetscMax(tao->trust, bnk->gamma4 * bnk->dnorm);
911:             } else {
912:               tao->trust = PetscMax(tao->trust, tau_max * bnk->dnorm);
913:             }
914:           } else if (kappa >= 1.0 - bnk->mu2) {
915:             /*  Good agreement */
916:             *accept = PETSC_TRUE;
917:             if (tau_max < bnk->gamma2) {
918:               tao->trust = bnk->gamma2 * PetscMin(tao->trust, bnk->dnorm);
919:             } else if (tau_max > bnk->gamma3) {
920:               tao->trust = PetscMax(tao->trust, bnk->gamma3 * bnk->dnorm);
921:             } else if (tau_max < 1.0) {
922:               tao->trust = tau_max * PetscMin(tao->trust, bnk->dnorm);
923:             } else {
924:               tao->trust = PetscMax(tao->trust, tau_max * bnk->dnorm);
925:             }
926:           } else {
927:             /*  Not good agreement */
928:             if (tau_min > 1.0) {
929:               tao->trust = bnk->gamma2 * PetscMin(tao->trust, bnk->dnorm);
930:             } else if (tau_max < bnk->gamma1) {
931:               tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm);
932:             } else if ((tau_min < bnk->gamma1) && (tau_max >= 1.0)) {
933:               tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm);
934:             } else if ((tau_1 >= bnk->gamma1) && (tau_1 < 1.0) && ((tau_2 < bnk->gamma1) || (tau_2 >= 1.0))) {
935:               tao->trust = tau_1 * PetscMin(tao->trust, bnk->dnorm);
936:             } else if ((tau_2 >= bnk->gamma1) && (tau_2 < 1.0) && ((tau_1 < bnk->gamma1) || (tau_2 >= 1.0))) {
937:               tao->trust = tau_2 * PetscMin(tao->trust, bnk->dnorm);
938:             } else {
939:               tao->trust = tau_max * PetscMin(tao->trust, bnk->dnorm);
940:             }
941:           }
942:         }
943:       }
944:     } else {
945:       /*  Newton step was not good; reduce the radius */
946:       tao->trust = bnk->gamma1 * PetscMin(bnk->dnorm, tao->trust);
947:     }
948:     break;
949:   }
950:   /* Make sure the radius does not violate min and max settings */
951:   tao->trust = PetscMin(tao->trust, bnk->max_radius);
952:   tao->trust = PetscMax(tao->trust, bnk->min_radius);
953:   PetscFunctionReturn(PETSC_SUCCESS);
954: }

956: PetscErrorCode TaoBNKAddStepCounts(Tao tao, PetscInt stepType)
957: {
958:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

960:   PetscFunctionBegin;
961:   switch (stepType) {
962:   case BNK_NEWTON:
963:     ++bnk->newt;
964:     break;
965:   case BNK_BFGS:
966:     ++bnk->bfgs;
967:     break;
968:   case BNK_SCALED_GRADIENT:
969:     ++bnk->sgrad;
970:     break;
971:   case BNK_GRADIENT:
972:     ++bnk->grad;
973:     break;
974:   default:
975:     break;
976:   }
977:   PetscFunctionReturn(PETSC_SUCCESS);
978: }

980: PetscErrorCode TaoSetUp_BNK(Tao tao)
981: {
982:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

984:   PetscFunctionBegin;
985:   if (!tao->gradient) PetscCall(VecDuplicate(tao->solution, &tao->gradient));
986:   if (!tao->stepdirection) PetscCall(VecDuplicate(tao->solution, &tao->stepdirection));
987:   if (!bnk->W) PetscCall(VecDuplicate(tao->solution, &bnk->W));
988:   if (!bnk->Xold) PetscCall(VecDuplicate(tao->solution, &bnk->Xold));
989:   if (!bnk->Gold) PetscCall(VecDuplicate(tao->solution, &bnk->Gold));
990:   if (!bnk->Xwork) PetscCall(VecDuplicate(tao->solution, &bnk->Xwork));
991:   if (!bnk->Gwork) PetscCall(VecDuplicate(tao->solution, &bnk->Gwork));
992:   if (!bnk->unprojected_gradient) PetscCall(VecDuplicate(tao->solution, &bnk->unprojected_gradient));
993:   if (!bnk->unprojected_gradient_old) PetscCall(VecDuplicate(tao->solution, &bnk->unprojected_gradient_old));
994:   if (!bnk->Diag_min) PetscCall(VecDuplicate(tao->solution, &bnk->Diag_min));
995:   if (!bnk->Diag_max) PetscCall(VecDuplicate(tao->solution, &bnk->Diag_max));
996:   if (bnk->max_cg_its > 0) {
997:     /* Ensure that the important common vectors are shared between BNK and embedded BNCG */
998:     bnk->bncg_ctx = (TAO_BNCG *)bnk->bncg->data;
999:     PetscCall(PetscObjectReference((PetscObject)bnk->unprojected_gradient_old));
1000:     PetscCall(VecDestroy(&bnk->bncg_ctx->unprojected_gradient_old));
1001:     bnk->bncg_ctx->unprojected_gradient_old = bnk->unprojected_gradient_old;
1002:     PetscCall(PetscObjectReference((PetscObject)bnk->unprojected_gradient));
1003:     PetscCall(VecDestroy(&bnk->bncg_ctx->unprojected_gradient));
1004:     bnk->bncg_ctx->unprojected_gradient = bnk->unprojected_gradient;
1005:     PetscCall(PetscObjectReference((PetscObject)bnk->Gold));
1006:     PetscCall(VecDestroy(&bnk->bncg_ctx->G_old));
1007:     bnk->bncg_ctx->G_old = bnk->Gold;
1008:     PetscCall(PetscObjectReference((PetscObject)tao->gradient));
1009:     PetscCall(VecDestroy(&bnk->bncg->gradient));
1010:     bnk->bncg->gradient = tao->gradient;
1011:     PetscCall(PetscObjectReference((PetscObject)tao->stepdirection));
1012:     PetscCall(VecDestroy(&bnk->bncg->stepdirection));
1013:     bnk->bncg->stepdirection = tao->stepdirection;
1014:     PetscCall(TaoSetSolution(bnk->bncg, tao->solution));
1015:     /* Copy over some settings from BNK into BNCG */
1016:     PetscCall(TaoSetMaximumIterations(bnk->bncg, bnk->max_cg_its));
1017:     PetscCall(TaoSetTolerances(bnk->bncg, tao->gatol, tao->grtol, tao->gttol));
1018:     PetscCall(TaoSetFunctionLowerBound(bnk->bncg, tao->fmin));
1019:     PetscCall(TaoSetConvergenceTest(bnk->bncg, tao->ops->convergencetest, tao->cnvP));
1020:     PetscCall(TaoSetObjective(bnk->bncg, tao->ops->computeobjective, tao->user_objP));
1021:     PetscCall(TaoSetGradient(bnk->bncg, NULL, tao->ops->computegradient, tao->user_gradP));
1022:     PetscCall(TaoSetObjectiveAndGradient(bnk->bncg, NULL, tao->ops->computeobjectiveandgradient, tao->user_objgradP));
1023:     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)tao, (PetscObject)bnk->bncg));
1024:   }
1025:   bnk->X_inactive    = NULL;
1026:   bnk->G_inactive    = NULL;
1027:   bnk->inactive_work = NULL;
1028:   bnk->active_work   = NULL;
1029:   bnk->inactive_idx  = NULL;
1030:   bnk->active_idx    = NULL;
1031:   bnk->active_lower  = NULL;
1032:   bnk->active_upper  = NULL;
1033:   bnk->active_fixed  = NULL;
1034:   bnk->M             = NULL;
1035:   bnk->H_inactive    = NULL;
1036:   bnk->Hpre_inactive = NULL;
1037:   PetscFunctionReturn(PETSC_SUCCESS);
1038: }

1040: PetscErrorCode TaoDestroy_BNK(Tao tao)
1041: {
1042:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

1044:   PetscFunctionBegin;
1045:   PetscCall(VecDestroy(&bnk->W));
1046:   PetscCall(VecDestroy(&bnk->Xold));
1047:   PetscCall(VecDestroy(&bnk->Gold));
1048:   PetscCall(VecDestroy(&bnk->Xwork));
1049:   PetscCall(VecDestroy(&bnk->Gwork));
1050:   PetscCall(VecDestroy(&bnk->unprojected_gradient));
1051:   PetscCall(VecDestroy(&bnk->unprojected_gradient_old));
1052:   PetscCall(VecDestroy(&bnk->Diag_min));
1053:   PetscCall(VecDestroy(&bnk->Diag_max));
1054:   PetscCall(ISDestroy(&bnk->active_lower));
1055:   PetscCall(ISDestroy(&bnk->active_upper));
1056:   PetscCall(ISDestroy(&bnk->active_fixed));
1057:   PetscCall(ISDestroy(&bnk->active_idx));
1058:   PetscCall(ISDestroy(&bnk->inactive_idx));
1059:   PetscCall(MatDestroy(&bnk->Hpre_inactive));
1060:   PetscCall(MatDestroy(&bnk->H_inactive));
1061:   PetscCall(TaoDestroy(&bnk->bncg));
1062:   PetscCall(KSPDestroy(&tao->ksp));
1063:   PetscCall(PetscFree(tao->data));
1064:   PetscFunctionReturn(PETSC_SUCCESS);
1065: }

1067: PetscErrorCode TaoSetFromOptions_BNK(Tao tao, PetscOptionItems PetscOptionsObject)
1068: {
1069:   TAO_BNK *bnk = (TAO_BNK *)tao->data;

1071:   PetscFunctionBegin;
1072:   PetscOptionsHeadBegin(PetscOptionsObject, "Newton-Krylov method for bound constrained optimization");
1073:   PetscCall(PetscOptionsEList("-tao_bnk_init_type", "radius initialization type", "", BNK_INIT, BNK_INIT_TYPES, BNK_INIT[bnk->init_type], &bnk->init_type, NULL));
1074:   PetscCall(PetscOptionsEList("-tao_bnk_update_type", "radius update type", "", BNK_UPDATE, BNK_UPDATE_TYPES, BNK_UPDATE[bnk->update_type], &bnk->update_type, NULL));
1075:   PetscCall(PetscOptionsEList("-tao_bnk_as_type", "active set estimation method", "", BNK_AS, BNK_AS_TYPES, BNK_AS[bnk->as_type], &bnk->as_type, NULL));
1076:   PetscCall(PetscOptionsReal("-tao_bnk_sval", "(developer) Hessian perturbation starting value", "", bnk->sval, &bnk->sval, NULL));
1077:   PetscCall(PetscOptionsReal("-tao_bnk_imin", "(developer) minimum initial Hessian perturbation", "", bnk->imin, &bnk->imin, NULL));
1078:   PetscCall(PetscOptionsReal("-tao_bnk_imax", "(developer) maximum initial Hessian perturbation", "", bnk->imax, &bnk->imax, NULL));
1079:   PetscCall(PetscOptionsReal("-tao_bnk_imfac", "(developer) initial merit factor for Hessian perturbation", "", bnk->imfac, &bnk->imfac, NULL));
1080:   PetscCall(PetscOptionsReal("-tao_bnk_pmin", "(developer) minimum Hessian perturbation", "", bnk->pmin, &bnk->pmin, NULL));
1081:   PetscCall(PetscOptionsReal("-tao_bnk_pmax", "(developer) maximum Hessian perturbation", "", bnk->pmax, &bnk->pmax, NULL));
1082:   PetscCall(PetscOptionsReal("-tao_bnk_pgfac", "(developer) Hessian perturbation growth factor", "", bnk->pgfac, &bnk->pgfac, NULL));
1083:   PetscCall(PetscOptionsReal("-tao_bnk_psfac", "(developer) Hessian perturbation shrink factor", "", bnk->psfac, &bnk->psfac, NULL));
1084:   PetscCall(PetscOptionsReal("-tao_bnk_pmgfac", "(developer) merit growth factor for Hessian perturbation", "", bnk->pmgfac, &bnk->pmgfac, NULL));
1085:   PetscCall(PetscOptionsReal("-tao_bnk_pmsfac", "(developer) merit shrink factor for Hessian perturbation", "", bnk->pmsfac, &bnk->pmsfac, NULL));
1086:   PetscCall(PetscOptionsReal("-tao_bnk_eta1", "(developer) threshold for rejecting step (-tao_bnk_update_type reduction)", "", bnk->eta1, &bnk->eta1, NULL));
1087:   PetscCall(PetscOptionsReal("-tao_bnk_eta2", "(developer) threshold for accepting marginal step (-tao_bnk_update_type reduction)", "", bnk->eta2, &bnk->eta2, NULL));
1088:   PetscCall(PetscOptionsReal("-tao_bnk_eta3", "(developer) threshold for accepting reasonable step (-tao_bnk_update_type reduction)", "", bnk->eta3, &bnk->eta3, NULL));
1089:   PetscCall(PetscOptionsReal("-tao_bnk_eta4", "(developer) threshold for accepting good step (-tao_bnk_update_type reduction)", "", bnk->eta4, &bnk->eta4, NULL));
1090:   PetscCall(PetscOptionsReal("-tao_bnk_alpha1", "(developer) radius reduction factor for rejected step (-tao_bnk_update_type reduction)", "", bnk->alpha1, &bnk->alpha1, NULL));
1091:   PetscCall(PetscOptionsReal("-tao_bnk_alpha2", "(developer) radius reduction factor for marginally accepted bad step (-tao_bnk_update_type reduction)", "", bnk->alpha2, &bnk->alpha2, NULL));
1092:   PetscCall(PetscOptionsReal("-tao_bnk_alpha3", "(developer) radius increase factor for reasonable accepted step (-tao_bnk_update_type reduction)", "", bnk->alpha3, &bnk->alpha3, NULL));
1093:   PetscCall(PetscOptionsReal("-tao_bnk_alpha4", "(developer) radius increase factor for good accepted step (-tao_bnk_update_type reduction)", "", bnk->alpha4, &bnk->alpha4, NULL));
1094:   PetscCall(PetscOptionsReal("-tao_bnk_alpha5", "(developer) radius increase factor for very good accepted step (-tao_bnk_update_type reduction)", "", bnk->alpha5, &bnk->alpha5, NULL));
1095:   PetscCall(PetscOptionsReal("-tao_bnk_nu1", "(developer) threshold for small line-search step length (-tao_bnk_update_type step)", "", bnk->nu1, &bnk->nu1, NULL));
1096:   PetscCall(PetscOptionsReal("-tao_bnk_nu2", "(developer) threshold for reasonable line-search step length (-tao_bnk_update_type step)", "", bnk->nu2, &bnk->nu2, NULL));
1097:   PetscCall(PetscOptionsReal("-tao_bnk_nu3", "(developer) threshold for large line-search step length (-tao_bnk_update_type step)", "", bnk->nu3, &bnk->nu3, NULL));
1098:   PetscCall(PetscOptionsReal("-tao_bnk_nu4", "(developer) threshold for very large line-search step length (-tao_bnk_update_type step)", "", bnk->nu4, &bnk->nu4, NULL));
1099:   PetscCall(PetscOptionsReal("-tao_bnk_omega1", "(developer) radius reduction factor for very small line-search step length (-tao_bnk_update_type step)", "", bnk->omega1, &bnk->omega1, NULL));
1100:   PetscCall(PetscOptionsReal("-tao_bnk_omega2", "(developer) radius reduction factor for small line-search step length (-tao_bnk_update_type step)", "", bnk->omega2, &bnk->omega2, NULL));
1101:   PetscCall(PetscOptionsReal("-tao_bnk_omega3", "(developer) radius factor for decent line-search step length (-tao_bnk_update_type step)", "", bnk->omega3, &bnk->omega3, NULL));
1102:   PetscCall(PetscOptionsReal("-tao_bnk_omega4", "(developer) radius increase factor for large line-search step length (-tao_bnk_update_type step)", "", bnk->omega4, &bnk->omega4, NULL));
1103:   PetscCall(PetscOptionsReal("-tao_bnk_omega5", "(developer) radius increase factor for very large line-search step length (-tao_bnk_update_type step)", "", bnk->omega5, &bnk->omega5, NULL));
1104:   PetscCall(PetscOptionsReal("-tao_bnk_mu1_i", "(developer) threshold for accepting very good step (-tao_bnk_init_type interpolation)", "", bnk->mu1_i, &bnk->mu1_i, NULL));
1105:   PetscCall(PetscOptionsReal("-tao_bnk_mu2_i", "(developer) threshold for accepting good step (-tao_bnk_init_type interpolation)", "", bnk->mu2_i, &bnk->mu2_i, NULL));
1106:   PetscCall(PetscOptionsReal("-tao_bnk_gamma1_i", "(developer) radius reduction factor for rejected very bad step (-tao_bnk_init_type interpolation)", "", bnk->gamma1_i, &bnk->gamma1_i, NULL));
1107:   PetscCall(PetscOptionsReal("-tao_bnk_gamma2_i", "(developer) radius reduction factor for rejected bad step (-tao_bnk_init_type interpolation)", "", bnk->gamma2_i, &bnk->gamma2_i, NULL));
1108:   PetscCall(PetscOptionsReal("-tao_bnk_gamma3_i", "(developer) radius increase factor for accepted good step (-tao_bnk_init_type interpolation)", "", bnk->gamma3_i, &bnk->gamma3_i, NULL));
1109:   PetscCall(PetscOptionsReal("-tao_bnk_gamma4_i", "(developer) radius increase factor for accepted very good step (-tao_bnk_init_type interpolation)", "", bnk->gamma4_i, &bnk->gamma4_i, NULL));
1110:   PetscCall(PetscOptionsReal("-tao_bnk_theta_i", "(developer) trust region interpolation factor (-tao_bnk_init_type interpolation)", "", bnk->theta_i, &bnk->theta_i, NULL));
1111:   PetscCall(PetscOptionsReal("-tao_bnk_mu1", "(developer) threshold for accepting very good step (-tao_bnk_update_type interpolation)", "", bnk->mu1, &bnk->mu1, NULL));
1112:   PetscCall(PetscOptionsReal("-tao_bnk_mu2", "(developer) threshold for accepting good step (-tao_bnk_update_type interpolation)", "", bnk->mu2, &bnk->mu2, NULL));
1113:   PetscCall(PetscOptionsReal("-tao_bnk_gamma1", "(developer) radius reduction factor for rejected very bad step (-tao_bnk_update_type interpolation)", "", bnk->gamma1, &bnk->gamma1, NULL));
1114:   PetscCall(PetscOptionsReal("-tao_bnk_gamma2", "(developer) radius reduction factor for rejected bad step (-tao_bnk_update_type interpolation)", "", bnk->gamma2, &bnk->gamma2, NULL));
1115:   PetscCall(PetscOptionsReal("-tao_bnk_gamma3", "(developer) radius increase factor for accepted good step (-tao_bnk_update_type interpolation)", "", bnk->gamma3, &bnk->gamma3, NULL));
1116:   PetscCall(PetscOptionsReal("-tao_bnk_gamma4", "(developer) radius increase factor for accepted very good step (-tao_bnk_update_type interpolation)", "", bnk->gamma4, &bnk->gamma4, NULL));
1117:   PetscCall(PetscOptionsReal("-tao_bnk_theta", "(developer) trust region interpolation factor (-tao_bnk_update_type interpolation)", "", bnk->theta, &bnk->theta, NULL));
1118:   PetscCall(PetscOptionsReal("-tao_bnk_min_radius", "(developer) lower bound on initial radius", "", bnk->min_radius, &bnk->min_radius, NULL));
1119:   PetscCall(PetscOptionsReal("-tao_bnk_max_radius", "(developer) upper bound on radius", "", bnk->max_radius, &bnk->max_radius, NULL));
1120:   PetscCall(PetscOptionsReal("-tao_bnk_epsilon", "(developer) tolerance used when computing actual and predicted reduction", "", bnk->epsilon, &bnk->epsilon, NULL));
1121:   PetscCall(PetscOptionsReal("-tao_bnk_as_tol", "(developer) initial tolerance used when estimating actively bounded variables", "", bnk->as_tol, &bnk->as_tol, NULL));
1122:   PetscCall(PetscOptionsReal("-tao_bnk_as_step", "(developer) step length used when estimating actively bounded variables", "", bnk->as_step, &bnk->as_step, NULL));
1123:   PetscCall(PetscOptionsInt("-tao_bnk_max_cg_its", "number of BNCG iterations to take for each Newton step", "", bnk->max_cg_its, &bnk->max_cg_its, NULL));
1124:   PetscOptionsHeadEnd();

1126:   PetscCall(TaoSetOptionsPrefix(bnk->bncg, ((PetscObject)tao)->prefix));
1127:   PetscCall(TaoAppendOptionsPrefix(bnk->bncg, "tao_bnk_cg_"));
1128:   PetscCall(TaoSetFromOptions(bnk->bncg));

1130:   PetscCall(KSPSetOptionsPrefix(tao->ksp, ((PetscObject)tao)->prefix));
1131:   PetscCall(KSPAppendOptionsPrefix(tao->ksp, "tao_bnk_"));
1132:   PetscCall(KSPSetFromOptions(tao->ksp));
1133:   PetscFunctionReturn(PETSC_SUCCESS);
1134: }

1136: PetscErrorCode TaoView_BNK(Tao tao, PetscViewer viewer)
1137: {
1138:   TAO_BNK  *bnk = (TAO_BNK *)tao->data;
1139:   PetscInt  nrejects;
1140:   PetscBool isascii;

1142:   PetscFunctionBegin;
1143:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1144:   if (isascii) {
1145:     PetscCall(PetscViewerASCIIPushTab(viewer));
1146:     PetscCall(TaoView(bnk->bncg, viewer));
1147:     if (bnk->M) {
1148:       PetscCall(MatLMVMGetRejectCount(bnk->M, &nrejects));
1149:       PetscCall(PetscViewerASCIIPrintf(viewer, "Rejected BFGS updates: %" PetscInt_FMT "\n", nrejects));
1150:     }
1151:     PetscCall(PetscViewerASCIIPrintf(viewer, "CG steps: %" PetscInt_FMT "\n", bnk->tot_cg_its));
1152:     PetscCall(PetscViewerASCIIPrintf(viewer, "Newton steps: %" PetscInt_FMT "\n", bnk->newt));
1153:     if (bnk->M) PetscCall(PetscViewerASCIIPrintf(viewer, "BFGS steps: %" PetscInt_FMT "\n", bnk->bfgs));
1154:     PetscCall(PetscViewerASCIIPrintf(viewer, "Scaled gradient steps: %" PetscInt_FMT "\n", bnk->sgrad));
1155:     PetscCall(PetscViewerASCIIPrintf(viewer, "Gradient steps: %" PetscInt_FMT "\n", bnk->grad));
1156:     PetscCall(PetscViewerASCIIPrintf(viewer, "KSP termination reasons:\n"));
1157:     PetscCall(PetscViewerASCIIPrintf(viewer, "  atol: %" PetscInt_FMT "\n", bnk->ksp_atol));
1158:     PetscCall(PetscViewerASCIIPrintf(viewer, "  rtol: %" PetscInt_FMT "\n", bnk->ksp_rtol));
1159:     PetscCall(PetscViewerASCIIPrintf(viewer, "  ctol: %" PetscInt_FMT "\n", bnk->ksp_ctol));
1160:     PetscCall(PetscViewerASCIIPrintf(viewer, "  negc: %" PetscInt_FMT "\n", bnk->ksp_negc));
1161:     PetscCall(PetscViewerASCIIPrintf(viewer, "  dtol: %" PetscInt_FMT "\n", bnk->ksp_dtol));
1162:     PetscCall(PetscViewerASCIIPrintf(viewer, "  iter: %" PetscInt_FMT "\n", bnk->ksp_iter));
1163:     PetscCall(PetscViewerASCIIPrintf(viewer, "  othr: %" PetscInt_FMT "\n", bnk->ksp_othr));
1164:     PetscCall(PetscViewerASCIIPopTab(viewer));
1165:   }
1166:   PetscFunctionReturn(PETSC_SUCCESS);
1167: }

1169: /*MC
1170:   TAOBNK - Shared base-type for Bounded Newton-Krylov type algorithms.
1171:   At each iteration, the BNK methods solve the symmetric
1172:   system of equations to obtain the step direction dk:
1173:               Hk dk = -gk
1174:   for free variables only. The step can be globalized either through
1175:   trust-region methods, or a line search, or a heuristic mixture of both.

1177:     Options Database Keys:
1178: + -tao_bnk_max_cg_its - maximum number of bounded conjugate-gradient iterations taken in each Newton loop
1179: . -tao_bnk_init_type - trust radius initialization method ("constant", "direction", "interpolation")
1180: . -tao_bnk_update_type - trust radius update method ("step", "direction", "interpolation")
1181: . -tao_bnk_as_type - active-set estimation method ("none", "bertsekas")
1182: . -tao_bnk_as_tol - (developer) initial tolerance used in estimating bounded active variables (-as_type bertsekas)
1183: . -tao_bnk_as_step - (developer) trial step length used in estimating bounded active variables (-as_type bertsekas)
1184: . -tao_bnk_sval - (developer) Hessian perturbation starting value
1185: . -tao_bnk_imin - (developer) minimum initial Hessian perturbation
1186: . -tao_bnk_imax - (developer) maximum initial Hessian perturbation
1187: . -tao_bnk_pmin - (developer) minimum Hessian perturbation
1188: . -tao_bnk_pmax - (developer) aximum Hessian perturbation
1189: . -tao_bnk_pgfac - (developer) Hessian perturbation growth factor
1190: . -tao_bnk_psfac - (developer) Hessian perturbation shrink factor
1191: . -tao_bnk_imfac - (developer) initial merit factor for Hessian perturbation
1192: . -tao_bnk_pmgfac - (developer) merit growth factor for Hessian perturbation
1193: . -tao_bnk_pmsfac - (developer) merit shrink factor for Hessian perturbation
1194: . -tao_bnk_eta1 - (developer) threshold for rejecting step (-update_type reduction)
1195: . -tao_bnk_eta2 - (developer) threshold for accepting marginal step (-update_type reduction)
1196: . -tao_bnk_eta3 - (developer) threshold for accepting reasonable step (-update_type reduction)
1197: . -tao_bnk_eta4 - (developer) threshold for accepting good step (-update_type reduction)
1198: . -tao_bnk_alpha1 - (developer) radius reduction factor for rejected step (-update_type reduction)
1199: . -tao_bnk_alpha2 - (developer) radius reduction factor for marginally accepted bad step (-update_type reduction)
1200: . -tao_bnk_alpha3 - (developer) radius increase factor for reasonable accepted step (-update_type reduction)
1201: . -tao_bnk_alpha4 - (developer) radius increase factor for good accepted step (-update_type reduction)
1202: . -tao_bnk_alpha5 - (developer) radius increase factor for very good accepted step (-update_type reduction)
1203: . -tao_bnk_epsilon - (developer) tolerance for small pred/actual ratios that trigger automatic step acceptance (-update_type reduction)
1204: . -tao_bnk_mu1 - (developer) threshold for accepting very good step (-update_type interpolation)
1205: . -tao_bnk_mu2 - (developer) threshold for accepting good step (-update_type interpolation)
1206: . -tao_bnk_gamma1 - (developer) radius reduction factor for rejected very bad step (-update_type interpolation)
1207: . -tao_bnk_gamma2 - (developer) radius reduction factor for rejected bad step (-update_type interpolation)
1208: . -tao_bnk_gamma3 - (developer) radius increase factor for accepted good step (-update_type interpolation)
1209: . -tao_bnk_gamma4 - (developer) radius increase factor for accepted very good step (-update_type interpolation)
1210: . -tao_bnk_theta - (developer) trust region interpolation factor (-update_type interpolation)
1211: . -tao_bnk_nu1 - (developer) threshold for small line-search step length (-update_type step)
1212: . -tao_bnk_nu2 - (developer) threshold for reasonable line-search step length (-update_type step)
1213: . -tao_bnk_nu3 - (developer) threshold for large line-search step length (-update_type step)
1214: . -tao_bnk_nu4 - (developer) threshold for very large line-search step length (-update_type step)
1215: . -tao_bnk_omega1 - (developer) radius reduction factor for very small line-search step length (-update_type step)
1216: . -tao_bnk_omega2 - (developer) radius reduction factor for small line-search step length (-update_type step)
1217: . -tao_bnk_omega3 - (developer) radius factor for decent line-search step length (-update_type step)
1218: . -tao_bnk_omega4 - (developer) radius increase factor for large line-search step length (-update_type step)
1219: . -tao_bnk_omega5 - (developer) radius increase factor for very large line-search step length (-update_type step)
1220: . -tao_bnk_mu1_i -  (developer) threshold for accepting very good step (-init_type interpolation)
1221: . -tao_bnk_mu2_i -  (developer) threshold for accepting good step (-init_type interpolation)
1222: . -tao_bnk_gamma1_i - (developer) radius reduction factor for rejected very bad step (-init_type interpolation)
1223: . -tao_bnk_gamma2_i - (developer) radius reduction factor for rejected bad step (-init_type interpolation)
1224: . -tao_bnk_gamma3_i - (developer) radius increase factor for accepted good step (-init_type interpolation)
1225: . -tao_bnk_gamma4_i - (developer) radius increase factor for accepted very good step (-init_type interpolation)
1226: - -tao_bnk_theta_i - (developer) trust region interpolation factor (-init_type interpolation)

1228:   Level: beginner
1229: M*/

1231: PetscErrorCode TaoCreate_BNK(Tao tao)
1232: {
1233:   TAO_BNK *bnk;
1234:   PC       pc;

1236:   PetscFunctionBegin;
1237:   PetscCall(PetscNew(&bnk));

1239:   tao->ops->setup          = TaoSetUp_BNK;
1240:   tao->ops->view           = TaoView_BNK;
1241:   tao->ops->setfromoptions = TaoSetFromOptions_BNK;
1242:   tao->ops->destroy        = TaoDestroy_BNK;

1244:   /*  Override default settings (unless already changed) */
1245:   PetscCall(TaoParametersInitialize(tao));
1246:   PetscObjectParameterSetDefault(tao, max_it, 50);
1247:   PetscObjectParameterSetDefault(tao, trust0, 100.0);

1249:   tao->data = (void *)bnk;

1251:   /*  Hessian shifting parameters */
1252:   bnk->computehessian = TaoBNKComputeHessian;
1253:   bnk->computestep    = TaoBNKComputeStep;

1255:   bnk->sval  = 0.0;
1256:   bnk->imin  = 1.0e-4;
1257:   bnk->imax  = 1.0e+2;
1258:   bnk->imfac = 1.0e-1;

1260:   bnk->pmin   = 1.0e-12;
1261:   bnk->pmax   = 1.0e+2;
1262:   bnk->pgfac  = 1.0e+1;
1263:   bnk->psfac  = 4.0e-1;
1264:   bnk->pmgfac = 1.0e-1;
1265:   bnk->pmsfac = 1.0e-1;

1267:   /*  Default values for trust-region radius update based on steplength */
1268:   bnk->nu1 = 0.25;
1269:   bnk->nu2 = 0.50;
1270:   bnk->nu3 = 1.00;
1271:   bnk->nu4 = 1.25;

1273:   bnk->omega1 = 0.25;
1274:   bnk->omega2 = 0.50;
1275:   bnk->omega3 = 1.00;
1276:   bnk->omega4 = 2.00;
1277:   bnk->omega5 = 4.00;

1279:   /*  Default values for trust-region radius update based on reduction */
1280:   bnk->eta1 = 1.0e-4;
1281:   bnk->eta2 = 0.25;
1282:   bnk->eta3 = 0.50;
1283:   bnk->eta4 = 0.90;

1285:   bnk->alpha1 = 0.25;
1286:   bnk->alpha2 = 0.50;
1287:   bnk->alpha3 = 1.00;
1288:   bnk->alpha4 = 2.00;
1289:   bnk->alpha5 = 4.00;

1291:   /*  Default values for trust-region radius update based on interpolation */
1292:   bnk->mu1 = 0.10;
1293:   bnk->mu2 = 0.50;

1295:   bnk->gamma1 = 0.25;
1296:   bnk->gamma2 = 0.50;
1297:   bnk->gamma3 = 2.00;
1298:   bnk->gamma4 = 4.00;

1300:   bnk->theta = 0.05;

1302:   /*  Default values for trust region initialization based on interpolation */
1303:   bnk->mu1_i = 0.35;
1304:   bnk->mu2_i = 0.50;

1306:   bnk->gamma1_i = 0.0625;
1307:   bnk->gamma2_i = 0.5;
1308:   bnk->gamma3_i = 2.0;
1309:   bnk->gamma4_i = 5.0;

1311:   bnk->theta_i = 0.25;

1313:   /*  Remaining parameters */
1314:   bnk->max_cg_its = 0;
1315:   bnk->min_radius = 1.0e-10;
1316:   bnk->max_radius = 1.0e10;
1317:   bnk->epsilon    = PetscPowReal(PETSC_MACHINE_EPSILON, 2.0 / 3.0);
1318:   bnk->as_tol     = 1.0e-3;
1319:   bnk->as_step    = 1.0e-3;
1320:   bnk->dmin       = 1.0e-6;
1321:   bnk->dmax       = 1.0e6;

1323:   bnk->M           = NULL;
1324:   bnk->bfgs_pre    = NULL;
1325:   bnk->init_type   = BNK_INIT_INTERPOLATION;
1326:   bnk->update_type = BNK_UPDATE_REDUCTION;
1327:   bnk->as_type     = BNK_AS_BERTSEKAS;

1329:   /* Create the embedded BNCG solver */
1330:   PetscCall(TaoCreate(PetscObjectComm((PetscObject)tao), &bnk->bncg));
1331:   PetscCall(PetscObjectIncrementTabLevel((PetscObject)bnk->bncg, (PetscObject)tao, 1));
1332:   PetscCall(TaoSetType(bnk->bncg, TAOBNCG));

1334:   /* Create the line search */
1335:   PetscCall(TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch));
1336:   PetscCall(PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1));
1337:   PetscCall(TaoLineSearchSetType(tao->linesearch, TAOLINESEARCHMT));
1338:   PetscCall(TaoLineSearchUseTaoRoutines(tao->linesearch, tao));

1340:   /*  Set linear solver to default for symmetric matrices */
1341:   PetscCall(KSPCreate(((PetscObject)tao)->comm, &tao->ksp));
1342:   PetscCall(PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1));
1343:   PetscCall(KSPSetType(tao->ksp, KSPSTCG));
1344:   PetscCall(KSPGetPC(tao->ksp, &pc));
1345:   PetscCall(PCSetType(pc, PCLMVM));
1346:   PetscFunctionReturn(PETSC_SUCCESS);
1347: }