Actual source code: ntr.c

petsc-dev 2014-02-02
Report Typos and Errors
  1: #include <../src/tao/matrix/lmvmmat.h>
  2: #include <../src/tao/unconstrained/impls/ntr/ntr.h>

  4: #include <petscksp.h>
  5: #include <petscpc.h>
  6: #include <petsc-private/kspimpl.h>
  7: #include <petsc-private/pcimpl.h>

  9: #define NTR_KSP_NASH    0
 10: #define NTR_KSP_STCG    1
 11: #define NTR_KSP_GLTR    2
 12: #define NTR_KSP_TYPES   3

 14: #define NTR_PC_NONE     0
 15: #define NTR_PC_AHESS    1
 16: #define NTR_PC_BFGS     2
 17: #define NTR_PC_PETSC    3
 18: #define NTR_PC_TYPES    4

 20: #define BFGS_SCALE_AHESS   0
 21: #define BFGS_SCALE_BFGS    1
 22: #define BFGS_SCALE_TYPES   2

 24: #define NTR_INIT_CONSTANT         0
 25: #define NTR_INIT_DIRECTION        1
 26: #define NTR_INIT_INTERPOLATION    2
 27: #define NTR_INIT_TYPES            3

 29: #define NTR_UPDATE_REDUCTION      0
 30: #define NTR_UPDATE_INTERPOLATION  1
 31: #define NTR_UPDATE_TYPES          2

 33: static const char *NTR_KSP[64] = {  "nash", "stcg", "gltr"};

 35: static const char *NTR_PC[64] = {  "none", "ahess", "bfgs", "petsc"};

 37: static const char *BFGS_SCALE[64] = {  "ahess", "bfgs"};

 39: static const char *NTR_INIT[64] = {  "constant", "direction", "interpolation"};

 41: static const char *NTR_UPDATE[64] = {  "reduction", "interpolation"};

 43: /*  Routine for BFGS preconditioner */
 44: static PetscErrorCode MatLMVMSolveShell(PC pc, Vec xin, Vec xout);

 46: /*
 47:    TaoSolve_NTR - Implements Newton's Method with a trust region approach
 48:    for solving unconstrained minimization problems.

 50:    The basic algorithm is taken from MINPACK-2 (dstrn).

 52:    TaoSolve_NTR computes a local minimizer of a twice differentiable function
 53:    f by applying a trust region variant of Newton's method.  At each stage
 54:    of the algorithm, we use the prconditioned conjugate gradient method to
 55:    determine an approximate minimizer of the quadratic equation

 57:         q(s) = <s, Hs + g>

 59:    subject to the trust region constraint

 61:         || s ||_M <= radius,

 63:    where radius is the trust region radius and M is a symmetric positive
 64:    definite matrix (the preconditioner).  Here g is the gradient and H
 65:    is the Hessian matrix.

 67:    Note:  TaoSolve_NTR MUST use the iterative solver KSPNASH, KSPSTCG,
 68:           or KSPGLTR.  Thus, we set KSPNASH, KSPSTCG, or KSPGLTR in this
 69:           routine regardless of what the user may have previously specified.
 70: */
 73: static PetscErrorCode TaoSolve_NTR(Tao tao)
 74: {
 75:   TAO_NTR                    *tr = (TAO_NTR *)tao->data;
 76:   PC                         pc;
 77:   KSPConvergedReason         ksp_reason;
 78:   TaoTerminationReason reason;
 79:   MatStructure               matflag;
 80:   PetscReal                  fmin, ftrial, prered, actred, kappa, sigma, beta;
 81:   PetscReal                  tau, tau_1, tau_2, tau_max, tau_min, max_radius;
 82:   PetscReal                  f, gnorm;

 84:   PetscReal                  delta;
 85:   PetscReal                  norm_d;
 86:   PetscErrorCode             ierr;

 88:   PetscInt                   iter = 0;
 89:   PetscInt                   bfgsUpdates = 0;
 90:   PetscInt                   needH;

 92:   PetscInt                   i_max = 5;
 93:   PetscInt                   j_max = 1;
 94:   PetscInt                   i, j, N, n, its;

 97:   if (tao->XL || tao->XU || tao->ops->computebounds) {
 98:     PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by ntr algorithm\n");
 99:   }

101:   tao->trust = tao->trust0;

103:   /* Modify the radius if it is too large or small */
104:   tao->trust = PetscMax(tao->trust, tr->min_radius);
105:   tao->trust = PetscMin(tao->trust, tr->max_radius);


108:   if (NTR_PC_BFGS == tr->pc_type && !tr->M) {
109:     VecGetLocalSize(tao->solution,&n);
110:     VecGetSize(tao->solution,&N);
111:     MatCreateLMVM(((PetscObject)tao)->comm,n,N,&tr->M);
112:     MatLMVMAllocateVectors(tr->M,tao->solution);
113:   }

115:   /* Check convergence criteria */
116:   TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient);
117:   VecNorm(tao->gradient,NORM_2,&gnorm);
118:   if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
119:   needH = 1;

121:   TaoMonitor(tao, iter, f, gnorm, 0.0, 1.0, &reason);
122:   if (reason != TAO_CONTINUE_ITERATING) return(0);

124:   /* Create vectors for the limited memory preconditioner */
125:   if ((NTR_PC_BFGS == tr->pc_type) &&
126:       (BFGS_SCALE_BFGS != tr->bfgs_scale_type)) {
127:     if (!tr->Diag) {
128:         VecDuplicate(tao->solution, &tr->Diag);
129:     }
130:   }

132:   switch(tr->ksp_type) {
133:   case NTR_KSP_NASH:
134:     KSPSetType(tao->ksp, KSPNASH);
135:     if (tao->ksp->ops->setfromoptions) {
136:       (*tao->ksp->ops->setfromoptions)(tao->ksp);
137:     }
138:     break;

140:   case NTR_KSP_STCG:
141:     KSPSetType(tao->ksp, KSPSTCG);
142:     if (tao->ksp->ops->setfromoptions) {
143:       (*tao->ksp->ops->setfromoptions)(tao->ksp);
144:     }
145:     break;

147:   default:
148:     KSPSetType(tao->ksp, KSPGLTR);
149:     if (tao->ksp->ops->setfromoptions) {
150:       (*tao->ksp->ops->setfromoptions)(tao->ksp);
151:     }
152:     break;
153:   }

155:   /*  Modify the preconditioner to use the bfgs approximation */
156:   KSPGetPC(tao->ksp, &pc);
157:   switch(tr->pc_type) {
158:   case NTR_PC_NONE:
159:     PCSetType(pc, PCNONE);
160:     if (pc->ops->setfromoptions) {
161:       (*pc->ops->setfromoptions)(pc);
162:     }
163:     break;

165:   case NTR_PC_AHESS:
166:     PCSetType(pc, PCJACOBI);
167:     if (pc->ops->setfromoptions) {
168:       (*pc->ops->setfromoptions)(pc);
169:     }
170:     PCJacobiSetUseAbs(pc);
171:     break;

173:   case NTR_PC_BFGS:
174:     PCSetType(pc, PCSHELL);
175:     if (pc->ops->setfromoptions) {
176:       (*pc->ops->setfromoptions)(pc);
177:     }
178:     PCShellSetName(pc, "bfgs");
179:     PCShellSetContext(pc, tr->M);
180:     PCShellSetApply(pc, MatLMVMSolveShell);
181:     break;

183:   default:
184:     /*  Use the pc method set by pc_type */
185:     break;
186:   }

188:   /*  Initialize trust-region radius */
189:   switch(tr->init_type) {
190:   case NTR_INIT_CONSTANT:
191:     /*  Use the initial radius specified */
192:     break;

194:   case NTR_INIT_INTERPOLATION:
195:     /*  Use the initial radius specified */
196:     max_radius = 0.0;

198:     for (j = 0; j < j_max; ++j) {
199:       fmin = f;
200:       sigma = 0.0;

202:       if (needH) {
203:           TaoComputeHessian(tao, tao->solution, &tao->hessian, &tao->hessian_pre, &matflag);
204:         needH = 0;
205:       }

207:       for (i = 0; i < i_max; ++i) {

209:         VecCopy(tao->solution, tr->W);
210:         VecAXPY(tr->W, -tao->trust/gnorm, tao->gradient);
211:         TaoComputeObjective(tao, tr->W, &ftrial);

213:         if (PetscIsInfOrNanReal(ftrial)) {
214:           tau = tr->gamma1_i;
215:         }
216:         else {
217:           if (ftrial < fmin) {
218:             fmin = ftrial;
219:             sigma = -tao->trust / gnorm;
220:           }

222:           MatMult(tao->hessian, tao->gradient, tao->stepdirection);
223:           VecDot(tao->gradient, tao->stepdirection, &prered);

225:           prered = tao->trust * (gnorm - 0.5 * tao->trust * prered / (gnorm * gnorm));
226:           actred = f - ftrial;
227:           if ((PetscAbsScalar(actred) <= tr->epsilon) &&
228:               (PetscAbsScalar(prered) <= tr->epsilon)) {
229:             kappa = 1.0;
230:           }
231:           else {
232:             kappa = actred / prered;
233:           }

235:           tau_1 = tr->theta_i * gnorm * tao->trust / (tr->theta_i * gnorm * tao->trust + (1.0 - tr->theta_i) * prered - actred);
236:           tau_2 = tr->theta_i * gnorm * tao->trust / (tr->theta_i * gnorm * tao->trust - (1.0 + tr->theta_i) * prered + actred);
237:           tau_min = PetscMin(tau_1, tau_2);
238:           tau_max = PetscMax(tau_1, tau_2);

240:           if (PetscAbsScalar(kappa - 1.0) <= tr->mu1_i) {
241:             /*  Great agreement */
242:             max_radius = PetscMax(max_radius, tao->trust);

244:             if (tau_max < 1.0) {
245:               tau = tr->gamma3_i;
246:             }
247:             else if (tau_max > tr->gamma4_i) {
248:               tau = tr->gamma4_i;
249:             }
250:             else {
251:               tau = tau_max;
252:             }
253:           }
254:           else if (PetscAbsScalar(kappa - 1.0) <= tr->mu2_i) {
255:             /*  Good agreement */
256:             max_radius = PetscMax(max_radius, tao->trust);

258:             if (tau_max < tr->gamma2_i) {
259:               tau = tr->gamma2_i;
260:             }
261:             else if (tau_max > tr->gamma3_i) {
262:               tau = tr->gamma3_i;
263:             }
264:             else {
265:               tau = tau_max;
266:             }
267:           }
268:           else {
269:             /*  Not good agreement */
270:             if (tau_min > 1.0) {
271:               tau = tr->gamma2_i;
272:             }
273:             else if (tau_max < tr->gamma1_i) {
274:               tau = tr->gamma1_i;
275:             }
276:             else if ((tau_min < tr->gamma1_i) && (tau_max >= 1.0)) {
277:               tau = tr->gamma1_i;
278:             }
279:             else if ((tau_1 >= tr->gamma1_i) && (tau_1 < 1.0) &&
280:                      ((tau_2 < tr->gamma1_i) || (tau_2 >= 1.0))) {
281:               tau = tau_1;
282:             }
283:             else if ((tau_2 >= tr->gamma1_i) && (tau_2 < 1.0) &&
284:                      ((tau_1 < tr->gamma1_i) || (tau_2 >= 1.0))) {
285:               tau = tau_2;
286:             }
287:             else {
288:               tau = tau_max;
289:             }
290:           }
291:         }
292:         tao->trust = tau * tao->trust;
293:       }

295:       if (fmin < f) {
296:         f = fmin;
297:         VecAXPY(tao->solution, sigma, tao->gradient);
298:         TaoComputeGradient(tao,tao->solution, tao->gradient);

300:         VecNorm(tao->gradient, NORM_2, &gnorm);

302:         if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
303:         needH = 1;

305:         TaoMonitor(tao, iter, f, gnorm, 0.0, 1.0, &reason);
306:         if (reason != TAO_CONTINUE_ITERATING) {
307:           return(0);
308:         }
309:       }
310:     }
311:     tao->trust = PetscMax(tao->trust, max_radius);

313:     /*  Modify the radius if it is too large or small */
314:     tao->trust = PetscMax(tao->trust, tr->min_radius);
315:     tao->trust = PetscMin(tao->trust, tr->max_radius);
316:     break;

318:   default:
319:     /*  Norm of the first direction will initialize radius */
320:     tao->trust = 0.0;
321:     break;
322:   }

324:   /* Set initial scaling for the BFGS preconditioner
325:      This step is done after computing the initial trust-region radius
326:      since the function value may have decreased */
327:   if (NTR_PC_BFGS == tr->pc_type) {
328:     if (f != 0.0) {
329:       delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
330:     }
331:     else {
332:       delta = 2.0 / (gnorm*gnorm);
333:     }
334:     MatLMVMSetDelta(tr->M,delta);
335:   }

337:   /* Have not converged; continue with Newton method */
338:   while (reason == TAO_CONTINUE_ITERATING) {
339:     ++iter;

341:     /* Compute the Hessian */
342:     if (needH) {
343:       TaoComputeHessian(tao, tao->solution, &tao->hessian, &tao->hessian_pre, &matflag);
344:       needH = 0;
345:     }

347:     if (NTR_PC_BFGS == tr->pc_type) {
348:       if (BFGS_SCALE_AHESS == tr->bfgs_scale_type) {
349:         /* Obtain diagonal for the bfgs preconditioner */
350:         MatGetDiagonal(tao->hessian, tr->Diag);
351:         VecAbs(tr->Diag);
352:         VecReciprocal(tr->Diag);
353:         MatLMVMSetScale(tr->M,tr->Diag);
354:       }

356:       /* Update the limited memory preconditioner */
357:       MatLMVMUpdate(tr->M, tao->solution, tao->gradient);
358:       ++bfgsUpdates;
359:     }

361:     while (reason == TAO_CONTINUE_ITERATING) {
362:       KSPSetOperators(tao->ksp, tao->hessian, tao->hessian_pre, matflag);

364:       /* Solve the trust region subproblem */
365:       if (NTR_KSP_NASH == tr->ksp_type) {
366:         KSPNASHSetRadius(tao->ksp,tao->trust);
367:         KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);
368:         KSPGetIterationNumber(tao->ksp,&its);
369:         tao->ksp_its+=its;
370:         KSPNASHGetNormD(tao->ksp, &norm_d);
371:       } else if (NTR_KSP_STCG == tr->ksp_type) {
372:         KSPSTCGSetRadius(tao->ksp,tao->trust);
373:         KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);
374:         KSPGetIterationNumber(tao->ksp,&its);
375:         tao->ksp_its+=its;
376:         KSPSTCGGetNormD(tao->ksp, &norm_d);
377:       } else { /* NTR_KSP_GLTR */
378:         KSPGLTRSetRadius(tao->ksp,tao->trust);
379:         KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);
380:         KSPGetIterationNumber(tao->ksp,&its);
381:         tao->ksp_its+=its;
382:         KSPGLTRGetNormD(tao->ksp, &norm_d);
383:       }

385:       if (0.0 == tao->trust) {
386:         /* Radius was uninitialized; use the norm of the direction */
387:         if (norm_d > 0.0) {
388:           tao->trust = norm_d;

390:           /* Modify the radius if it is too large or small */
391:           tao->trust = PetscMax(tao->trust, tr->min_radius);
392:           tao->trust = PetscMin(tao->trust, tr->max_radius);
393:         }
394:         else {
395:           /* The direction was bad; set radius to default value and re-solve
396:              the trust-region subproblem to get a direction */
397:           tao->trust = tao->trust0;

399:           /* Modify the radius if it is too large or small */
400:           tao->trust = PetscMax(tao->trust, tr->min_radius);
401:           tao->trust = PetscMin(tao->trust, tr->max_radius);

403:           if (NTR_KSP_NASH == tr->ksp_type) {
404:             KSPNASHSetRadius(tao->ksp,tao->trust);
405:             KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);
406:             KSPGetIterationNumber(tao->ksp,&its);
407:             tao->ksp_its+=its;
408:             KSPNASHGetNormD(tao->ksp, &norm_d);
409:           } else if (NTR_KSP_STCG == tr->ksp_type) {
410:             KSPSTCGSetRadius(tao->ksp,tao->trust);
411:             KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);
412:             KSPGetIterationNumber(tao->ksp,&its);
413:             tao->ksp_its+=its;
414:             KSPSTCGGetNormD(tao->ksp, &norm_d);
415:           } else { /* NTR_KSP_GLTR */
416:             KSPGLTRSetRadius(tao->ksp,tao->trust);
417:             KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);
418:             KSPGetIterationNumber(tao->ksp,&its);
419:             tao->ksp_its+=its;
420:             KSPGLTRGetNormD(tao->ksp, &norm_d);
421:           }

423:           if (norm_d == 0.0) SETERRQ(PETSC_COMM_SELF,1, "Initial direction zero");
424:         }
425:       }
426:       VecScale(tao->stepdirection, -1.0);
427:       KSPGetConvergedReason(tao->ksp, &ksp_reason);
428:       if ((KSP_DIVERGED_INDEFINITE_PC == ksp_reason) &&
429:           (NTR_PC_BFGS == tr->pc_type) && (bfgsUpdates > 1)) {
430:         /* Preconditioner is numerically indefinite; reset the
431:            approximate if using BFGS preconditioning. */

433:         if (f != 0.0) {
434:           delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
435:         }
436:         else {
437:           delta = 2.0 / (gnorm*gnorm);
438:         }
439:         MatLMVMSetDelta(tr->M, delta);
440:         MatLMVMReset(tr->M);
441:         MatLMVMUpdate(tr->M, tao->solution, tao->gradient);
442:         bfgsUpdates = 1;
443:       }

445:       if (NTR_UPDATE_REDUCTION == tr->update_type) {
446:         /* Get predicted reduction */
447:         if (NTR_KSP_NASH == tr->ksp_type) {
448:           KSPNASHGetObjFcn(tao->ksp,&prered);
449:         } else if (NTR_KSP_STCG == tr->ksp_type) {
450:           KSPSTCGGetObjFcn(tao->ksp,&prered);
451:         } else { /* gltr */
452:           KSPGLTRGetObjFcn(tao->ksp,&prered);
453:         }

455:         if (prered >= 0.0) {
456:           /* The predicted reduction has the wrong sign.  This cannot
457:              happen in infinite precision arithmetic.  Step should
458:              be rejected! */
459:           tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d);
460:         }
461:         else {
462:           /* Compute trial step and function value */
463:           VecCopy(tao->solution,tr->W);
464:           VecAXPY(tr->W, 1.0, tao->stepdirection);
465:           TaoComputeObjective(tao, tr->W, &ftrial);

467:           if (PetscIsInfOrNanReal(ftrial)) {
468:             tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d);
469:           } else {
470:             /* Compute and actual reduction */
471:             actred = f - ftrial;
472:             prered = -prered;
473:             if ((PetscAbsScalar(actred) <= tr->epsilon) &&
474:                 (PetscAbsScalar(prered) <= tr->epsilon)) {
475:               kappa = 1.0;
476:             }
477:             else {
478:               kappa = actred / prered;
479:             }

481:             /* Accept or reject the step and update radius */
482:             if (kappa < tr->eta1) {
483:               /* Reject the step */
484:               tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d);
485:             }
486:             else {
487:               /* Accept the step */
488:               if (kappa < tr->eta2) {
489:                 /* Marginal bad step */
490:                 tao->trust = tr->alpha2 * PetscMin(tao->trust, norm_d);
491:               }
492:               else if (kappa < tr->eta3) {
493:                 /* Reasonable step */
494:                 tao->trust = tr->alpha3 * tao->trust;
495:               }
496:               else if (kappa < tr->eta4) {
497:                 /* Good step */
498:                 tao->trust = PetscMax(tr->alpha4 * norm_d, tao->trust);
499:               }
500:               else {
501:                 /* Very good step */
502:                 tao->trust = PetscMax(tr->alpha5 * norm_d, tao->trust);
503:               }
504:               break;
505:             }
506:           }
507:         }
508:       }
509:       else {
510:         /* Get predicted reduction */
511:         if (NTR_KSP_NASH == tr->ksp_type) {
512:           KSPNASHGetObjFcn(tao->ksp,&prered);
513:         } else if (NTR_KSP_STCG == tr->ksp_type) {
514:           KSPSTCGGetObjFcn(tao->ksp,&prered);
515:         } else { /* gltr */
516:           KSPGLTRGetObjFcn(tao->ksp,&prered);
517:         }

519:         if (prered >= 0.0) {
520:           /* The predicted reduction has the wrong sign.  This cannot
521:              happen in infinite precision arithmetic.  Step should
522:              be rejected! */
523:           tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d);
524:         }
525:         else {
526:           VecCopy(tao->solution, tr->W);
527:           VecAXPY(tr->W, 1.0, tao->stepdirection);
528:           TaoComputeObjective(tao, tr->W, &ftrial);
529:           if (PetscIsInfOrNanReal(ftrial)) {
530:             tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d);
531:           }
532:           else {
533:             VecDot(tao->gradient, tao->stepdirection, &beta);
534:             actred = f - ftrial;
535:             prered = -prered;
536:             if ((PetscAbsScalar(actred) <= tr->epsilon) &&
537:                 (PetscAbsScalar(prered) <= tr->epsilon)) {
538:               kappa = 1.0;
539:             }
540:             else {
541:               kappa = actred / prered;
542:             }

544:             tau_1 = tr->theta * beta / (tr->theta * beta - (1.0 - tr->theta) * prered + actred);
545:             tau_2 = tr->theta * beta / (tr->theta * beta + (1.0 + tr->theta) * prered - actred);
546:             tau_min = PetscMin(tau_1, tau_2);
547:             tau_max = PetscMax(tau_1, tau_2);

549:             if (kappa >= 1.0 - tr->mu1) {
550:               /* Great agreement; accept step and update radius */
551:               if (tau_max < 1.0) {
552:                 tao->trust = PetscMax(tao->trust, tr->gamma3 * norm_d);
553:               }
554:               else if (tau_max > tr->gamma4) {
555:                 tao->trust = PetscMax(tao->trust, tr->gamma4 * norm_d);
556:               }
557:               else {
558:                 tao->trust = PetscMax(tao->trust, tau_max * norm_d);
559:               }
560:               break;
561:             }
562:             else if (kappa >= 1.0 - tr->mu2) {
563:               /* Good agreement */

565:               if (tau_max < tr->gamma2) {
566:                 tao->trust = tr->gamma2 * PetscMin(tao->trust, norm_d);
567:               }
568:               else if (tau_max > tr->gamma3) {
569:                 tao->trust = PetscMax(tao->trust, tr->gamma3 * norm_d);
570:               }
571:               else if (tau_max < 1.0) {
572:                 tao->trust = tau_max * PetscMin(tao->trust, norm_d);
573:               }
574:               else {
575:                 tao->trust = PetscMax(tao->trust, tau_max * norm_d);
576:               }
577:               break;
578:             }
579:             else {
580:               /* Not good agreement */
581:               if (tau_min > 1.0) {
582:                 tao->trust = tr->gamma2 * PetscMin(tao->trust, norm_d);
583:               }
584:               else if (tau_max < tr->gamma1) {
585:                 tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d);
586:               }
587:               else if ((tau_min < tr->gamma1) && (tau_max >= 1.0)) {
588:                 tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d);
589:               }
590:               else if ((tau_1 >= tr->gamma1) && (tau_1 < 1.0) &&
591:                        ((tau_2 < tr->gamma1) || (tau_2 >= 1.0))) {
592:                 tao->trust = tau_1 * PetscMin(tao->trust, norm_d);
593:               }
594:               else if ((tau_2 >= tr->gamma1) && (tau_2 < 1.0) &&
595:                        ((tau_1 < tr->gamma1) || (tau_2 >= 1.0))) {
596:                 tao->trust = tau_2 * PetscMin(tao->trust, norm_d);
597:               }
598:               else {
599:                 tao->trust = tau_max * PetscMin(tao->trust, norm_d);
600:               }
601:             }
602:           }
603:         }
604:       }

606:       /* The step computed was not good and the radius was decreased.
607:          Monitor the radius to terminate. */
608:       TaoMonitor(tao, iter, f, gnorm, 0.0, tao->trust, &reason);
609:     }

611:     /* The radius may have been increased; modify if it is too large */
612:     tao->trust = PetscMin(tao->trust, tr->max_radius);

614:     if (reason == TAO_CONTINUE_ITERATING) {
615:       VecCopy(tr->W, tao->solution);
616:       f = ftrial;
617:       TaoComputeGradient(tao, tao->solution, tao->gradient);
618:       VecNorm(tao->gradient, NORM_2, &gnorm);
619:       if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
620:       needH = 1;
621:       TaoMonitor(tao, iter, f, gnorm, 0.0, tao->trust, &reason);
622:     }
623:   }
624:   return(0);
625: }

627: /*------------------------------------------------------------*/
630: static PetscErrorCode TaoSetUp_NTR(Tao tao)
631: {
632:   TAO_NTR *tr = (TAO_NTR *)tao->data;


637:   if (!tao->gradient) {VecDuplicate(tao->solution, &tao->gradient);}
638:   if (!tao->stepdirection) {VecDuplicate(tao->solution, &tao->stepdirection);}
639:   if (!tr->W) {VecDuplicate(tao->solution, &tr->W);}

641:   tr->Diag = 0;
642:   tr->M = 0;


645:   return(0);
646: }

648: /*------------------------------------------------------------*/
651: static PetscErrorCode TaoDestroy_NTR(Tao tao)
652: {
653:   TAO_NTR        *tr = (TAO_NTR *)tao->data;

657:   if (tao->setupcalled) {
658:     VecDestroy(&tr->W);
659:   }
660:   MatDestroy(&tr->M);
661:   VecDestroy(&tr->Diag);
662:   PetscFree(tao->data);
663:   return(0);
664: }

666: /*------------------------------------------------------------*/
669: static PetscErrorCode TaoSetFromOptions_NTR(Tao tao)
670: {
671:   TAO_NTR *tr = (TAO_NTR *)tao->data;

675:   PetscOptionsHead("Newton trust region method for unconstrained optimization");
676:   PetscOptionsEList("-tao_ntr_ksp_type", "ksp type", "", NTR_KSP, NTR_KSP_TYPES, NTR_KSP[tr->ksp_type], &tr->ksp_type, 0);
677:   PetscOptionsEList("-tao_ntr_pc_type", "pc type", "", NTR_PC, NTR_PC_TYPES, NTR_PC[tr->pc_type], &tr->pc_type, 0);
678:   PetscOptionsEList("-tao_ntr_bfgs_scale_type", "bfgs scale type", "", BFGS_SCALE, BFGS_SCALE_TYPES, BFGS_SCALE[tr->bfgs_scale_type], &tr->bfgs_scale_type, 0);
679:   PetscOptionsEList("-tao_ntr_init_type", "tao->trust initialization type", "", NTR_INIT, NTR_INIT_TYPES, NTR_INIT[tr->init_type], &tr->init_type, 0);
680:   PetscOptionsEList("-tao_ntr_update_type", "radius update type", "", NTR_UPDATE, NTR_UPDATE_TYPES, NTR_UPDATE[tr->update_type], &tr->update_type, 0);
681:   PetscOptionsReal("-tao_ntr_eta1", "step is unsuccessful if actual reduction < eta1 * predicted reduction", "", tr->eta1, &tr->eta1, 0);
682:   PetscOptionsReal("-tao_ntr_eta2", "", "", tr->eta2, &tr->eta2, 0);
683:   PetscOptionsReal("-tao_ntr_eta3", "", "", tr->eta3, &tr->eta3, 0);
684:   PetscOptionsReal("-tao_ntr_eta4", "", "", tr->eta4, &tr->eta4, 0);
685:   PetscOptionsReal("-tao_ntr_alpha1", "", "", tr->alpha1, &tr->alpha1, 0);
686:   PetscOptionsReal("-tao_ntr_alpha2", "", "", tr->alpha2, &tr->alpha2, 0);
687:   PetscOptionsReal("-tao_ntr_alpha3", "", "", tr->alpha3, &tr->alpha3, 0);
688:   PetscOptionsReal("-tao_ntr_alpha4", "", "", tr->alpha4, &tr->alpha4, 0);
689:   PetscOptionsReal("-tao_ntr_alpha5", "", "", tr->alpha5, &tr->alpha5, 0);
690:   PetscOptionsReal("-tao_ntr_mu1", "", "", tr->mu1, &tr->mu1, 0);
691:   PetscOptionsReal("-tao_ntr_mu2", "", "", tr->mu2, &tr->mu2, 0);
692:   PetscOptionsReal("-tao_ntr_gamma1", "", "", tr->gamma1, &tr->gamma1, 0);
693:   PetscOptionsReal("-tao_ntr_gamma2", "", "", tr->gamma2, &tr->gamma2, 0);
694:   PetscOptionsReal("-tao_ntr_gamma3", "", "", tr->gamma3, &tr->gamma3, 0);
695:   PetscOptionsReal("-tao_ntr_gamma4", "", "", tr->gamma4, &tr->gamma4, 0);
696:   PetscOptionsReal("-tao_ntr_theta", "", "", tr->theta, &tr->theta, 0);
697:   PetscOptionsReal("-tao_ntr_mu1_i", "", "", tr->mu1_i, &tr->mu1_i, 0);
698:   PetscOptionsReal("-tao_ntr_mu2_i", "", "", tr->mu2_i, &tr->mu2_i, 0);
699:   PetscOptionsReal("-tao_ntr_gamma1_i", "", "", tr->gamma1_i, &tr->gamma1_i, 0);
700:   PetscOptionsReal("-tao_ntr_gamma2_i", "", "", tr->gamma2_i, &tr->gamma2_i, 0);
701:   PetscOptionsReal("-tao_ntr_gamma3_i", "", "", tr->gamma3_i, &tr->gamma3_i, 0);
702:   PetscOptionsReal("-tao_ntr_gamma4_i", "", "", tr->gamma4_i, &tr->gamma4_i, 0);
703:   PetscOptionsReal("-tao_ntr_theta_i", "", "", tr->theta_i, &tr->theta_i, 0);
704:   PetscOptionsReal("-tao_ntr_min_radius", "lower bound on initial trust-region radius", "", tr->min_radius, &tr->min_radius, 0);
705:   PetscOptionsReal("-tao_ntr_max_radius", "upper bound on trust-region radius", "", tr->max_radius, &tr->max_radius, 0);
706:   PetscOptionsReal("-tao_ntr_epsilon", "tolerance used when computing actual and predicted reduction", "", tr->epsilon, &tr->epsilon, 0);
707:   PetscOptionsTail();
708:   KSPSetFromOptions(tao->ksp);
709:   return(0);
710: }

712: /*------------------------------------------------------------*/
715: static PetscErrorCode TaoView_NTR(Tao tao, PetscViewer viewer)
716: {
717:   TAO_NTR        *tr = (TAO_NTR *)tao->data;
719:   PetscInt       nrejects;
720:   PetscBool      isascii;

723:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
724:   if (isascii) {
725:     PetscViewerASCIIPushTab(viewer);
726:     if (NTR_PC_BFGS == tr->pc_type && tr->M) {
727:       MatLMVMGetRejects(tr->M, &nrejects);
728:       PetscViewerASCIIPrintf(viewer, "Rejected matrix updates: %D\n", nrejects);
729:     }
730:     PetscViewerASCIIPopTab(viewer);
731:   }
732:   return(0);
733: }

735: /*------------------------------------------------------------*/
736: EXTERN_C_BEGIN
739: PetscErrorCode TaoCreate_NTR(Tao tao)
740: {
741:   TAO_NTR *tr;


746:   PetscNewLog(tao,&tr);

748:   tao->ops->setup = TaoSetUp_NTR;
749:   tao->ops->solve = TaoSolve_NTR;
750:   tao->ops->view = TaoView_NTR;
751:   tao->ops->setfromoptions = TaoSetFromOptions_NTR;
752:   tao->ops->destroy = TaoDestroy_NTR;

754:   tao->max_it = 50;
755: #if defined(PETSC_USE_REAL_SINGLE)
756:   tao->fatol = 1e-5;
757:   tao->frtol = 1e-5;
758: #else
759:   tao->fatol = 1e-10;
760:   tao->frtol = 1e-10;
761: #endif
762:   tao->data = (void*)tr;

764:   tao->trust0 = 100.0;

766:   /*  Standard trust region update parameters */
767:   tr->eta1 = 1.0e-4;
768:   tr->eta2 = 0.25;
769:   tr->eta3 = 0.50;
770:   tr->eta4 = 0.90;

772:   tr->alpha1 = 0.25;
773:   tr->alpha2 = 0.50;
774:   tr->alpha3 = 1.00;
775:   tr->alpha4 = 2.00;
776:   tr->alpha5 = 4.00;

778:   /*  Interpolation parameters */
779:   tr->mu1_i = 0.35;
780:   tr->mu2_i = 0.50;

782:   tr->gamma1_i = 0.0625;
783:   tr->gamma2_i = 0.50;
784:   tr->gamma3_i = 2.00;
785:   tr->gamma4_i = 5.00;

787:   tr->theta_i = 0.25;

789:   /*  Interpolation trust region update parameters */
790:   tr->mu1 = 0.10;
791:   tr->mu2 = 0.50;

793:   tr->gamma1 = 0.25;
794:   tr->gamma2 = 0.50;
795:   tr->gamma3 = 2.00;
796:   tr->gamma4 = 4.00;

798:   tr->theta = 0.05;

800:   tr->min_radius = 1.0e-10;
801:   tr->max_radius = 1.0e10;
802:   tr->epsilon = 1.0e-6;

804:   tr->ksp_type        = NTR_KSP_STCG;
805:   tr->pc_type         = NTR_PC_BFGS;
806:   tr->bfgs_scale_type = BFGS_SCALE_AHESS;
807:   tr->init_type       = NTR_INIT_INTERPOLATION;
808:   tr->update_type     = NTR_UPDATE_REDUCTION;


811:   /* Set linear solver to default for trust region */
812:   KSPCreate(((PetscObject)tao)->comm, &tao->ksp);

814:   return(0);


817: }
818: EXTERN_C_END


823: static PetscErrorCode MatLMVMSolveShell(PC pc, Vec b, Vec x)
824: {
826:     Mat M;
831:     PCShellGetContext(pc,(void**)&M);
832:     MatLMVMSolve(M, b, x);
833:     return(0);
834: }