Actual source code: blmvm.c

  1: #include <petsctaolinesearch.h>
  2: #include <../src/tao/unconstrained/impls/lmvm/lmvm.h>
  3: #include <../src/tao/bound/impls/blmvm/blmvm.h>

  5: /*------------------------------------------------------------*/
  6: static PetscErrorCode TaoSolve_BLMVM(Tao tao)
  7: {
  8:   PetscErrorCode               ierr;
  9:   TAO_BLMVM                    *blmP = (TAO_BLMVM *)tao->data;
 10:   TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING;
 11:   PetscReal                    f, fold, gdx, gnorm, gnorm2;
 12:   PetscReal                    stepsize = 1.0,delta;

 15:   /*  Project initial point onto bounds */
 16:   TaoComputeVariableBounds(tao);
 17:   VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);
 18:   TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);

 20:   /* Check convergence criteria */
 21:   TaoComputeObjectiveAndGradient(tao, tao->solution,&f,blmP->unprojected_gradient);
 22:   VecBoundGradientProjection(blmP->unprojected_gradient,tao->solution, tao->XL,tao->XU,tao->gradient);

 24:   TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);
 25:   if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_USER, "User provided compute function generated Inf or NaN");

 27:   tao->reason = TAO_CONTINUE_ITERATING;
 28:   TaoLogConvergenceHistory(tao,f,gnorm,0.0,tao->ksp_its);
 29:   TaoMonitor(tao,tao->niter,f,gnorm,0.0,stepsize);
 30:   (*tao->ops->convergencetest)(tao,tao->cnvP);
 31:   if (tao->reason != TAO_CONTINUE_ITERATING) return(0);

 33:   /* Set counter for gradient/reset steps */
 34:   if (!blmP->recycle) {
 35:     blmP->grad = 0;
 36:     blmP->reset = 0;
 37:     MatLMVMReset(blmP->M, PETSC_FALSE);
 38:   }

 40:   /* Have not converged; continue with Newton method */
 41:   while (tao->reason == TAO_CONTINUE_ITERATING) {
 42:     /* Call general purpose update function */
 43:     if (tao->ops->update) {
 44:       (*tao->ops->update)(tao, tao->niter, tao->user_update);
 45:     }
 46:     /* Compute direction */
 47:     gnorm2 = gnorm*gnorm;
 48:     if (gnorm2 == 0.0) gnorm2 = PETSC_MACHINE_EPSILON;
 49:     if (f == 0.0) {
 50:       delta = 2.0 / gnorm2;
 51:     } else {
 52:       delta = 2.0 * PetscAbsScalar(f) / gnorm2;
 53:     }
 54:     MatLMVMSymBroydenSetDelta(blmP->M, delta);
 55:     MatLMVMUpdate(blmP->M, tao->solution, tao->gradient);
 56:     MatSolve(blmP->M, blmP->unprojected_gradient, tao->stepdirection);
 57:     VecBoundGradientProjection(tao->stepdirection,tao->solution,tao->XL,tao->XU,tao->gradient);

 59:     /* Check for success (descent direction) */
 60:     VecDot(blmP->unprojected_gradient, tao->gradient, &gdx);
 61:     if (gdx <= 0) {
 62:       /* Step is not descent or solve was not successful
 63:          Use steepest descent direction (scaled) */
 64:       ++blmP->grad;

 66:       MatLMVMReset(blmP->M, PETSC_FALSE);
 67:       MatLMVMUpdate(blmP->M, tao->solution, blmP->unprojected_gradient);
 68:       MatSolve(blmP->M,blmP->unprojected_gradient, tao->stepdirection);
 69:     }
 70:     VecScale(tao->stepdirection,-1.0);

 72:     /* Perform the linesearch */
 73:     fold = f;
 74:     VecCopy(tao->solution, blmP->Xold);
 75:     VecCopy(blmP->unprojected_gradient, blmP->Gold);
 76:     TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);
 77:     TaoLineSearchApply(tao->linesearch, tao->solution, &f, blmP->unprojected_gradient, tao->stepdirection, &stepsize, &ls_status);
 78:     TaoAddLineSearchCounts(tao);

 80:     if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) {
 81:       /* Linesearch failed
 82:          Reset factors and use scaled (projected) gradient step */
 83:       ++blmP->reset;

 85:       f = fold;
 86:       VecCopy(blmP->Xold, tao->solution);
 87:       VecCopy(blmP->Gold, blmP->unprojected_gradient);

 89:       MatLMVMReset(blmP->M, PETSC_FALSE);
 90:       MatLMVMUpdate(blmP->M, tao->solution, blmP->unprojected_gradient);
 91:       MatSolve(blmP->M, blmP->unprojected_gradient, tao->stepdirection);
 92:       VecScale(tao->stepdirection, -1.0);

 94:       /* This may be incorrect; linesearch has values for stepmax and stepmin
 95:          that should be reset. */
 96:       TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);
 97:       TaoLineSearchApply(tao->linesearch,tao->solution,&f, blmP->unprojected_gradient, tao->stepdirection,  &stepsize, &ls_status);
 98:       TaoAddLineSearchCounts(tao);

100:       if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) {
101:         tao->reason = TAO_DIVERGED_LS_FAILURE;
102:         break;
103:       }
104:     }

106:     /* Check for converged */
107:     VecBoundGradientProjection(blmP->unprojected_gradient, tao->solution, tao->XL, tao->XU, tao->gradient);
108:     TaoGradientNorm(tao, tao->gradient, NORM_2, &gnorm);
109:     if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_USER, "User provided compute function generated Not-a-Number");
110:     tao->niter++;
111:     TaoLogConvergenceHistory(tao,f,gnorm,0.0,tao->ksp_its);
112:     TaoMonitor(tao,tao->niter,f,gnorm,0.0,stepsize);
113:     (*tao->ops->convergencetest)(tao,tao->cnvP);
114:   }
115:   return(0);
116: }

118: static PetscErrorCode TaoSetup_BLMVM(Tao tao)
119: {
120:   TAO_BLMVM      *blmP = (TAO_BLMVM *)tao->data;

124:   /* Existence of tao->solution checked in TaoSetup() */
125:   VecDuplicate(tao->solution,&blmP->Xold);
126:   VecDuplicate(tao->solution,&blmP->Gold);
127:   VecDuplicate(tao->solution, &blmP->unprojected_gradient);

129:   if (!tao->stepdirection) {
130:     VecDuplicate(tao->solution, &tao->stepdirection);
131:   }
132:   if (!tao->gradient) {
133:     VecDuplicate(tao->solution,&tao->gradient);
134:   }
135:   if (!tao->XL) {
136:     VecDuplicate(tao->solution,&tao->XL);
137:     VecSet(tao->XL,PETSC_NINFINITY);
138:   }
139:   if (!tao->XU) {
140:     VecDuplicate(tao->solution,&tao->XU);
141:     VecSet(tao->XU,PETSC_INFINITY);
142:   }
143:   /* Allocate matrix for the limited memory approximation */
144:   MatLMVMAllocate(blmP->M,tao->solution,blmP->unprojected_gradient);

146:   /* If the user has set a matrix to solve as the initial H0, set the options prefix here, and set up the KSP */
147:   if (blmP->H0) {
148:     MatLMVMSetJ0(blmP->M, blmP->H0);
149:   }
150:   return(0);
151: }

153: /* ---------------------------------------------------------- */
154: static PetscErrorCode TaoDestroy_BLMVM(Tao tao)
155: {
156:   TAO_BLMVM      *blmP = (TAO_BLMVM *)tao->data;

160:   if (tao->setupcalled) {
161:     VecDestroy(&blmP->unprojected_gradient);
162:     VecDestroy(&blmP->Xold);
163:     VecDestroy(&blmP->Gold);
164:   }
165:   MatDestroy(&blmP->M);
166:   if (blmP->H0) {
167:     PetscObjectDereference((PetscObject)blmP->H0);
168:   }
169:   PetscFree(tao->data);
170:   return(0);
171: }

173: /*------------------------------------------------------------*/
174: static PetscErrorCode TaoSetFromOptions_BLMVM(PetscOptionItems* PetscOptionsObject,Tao tao)
175: {
176:   TAO_BLMVM      *blmP = (TAO_BLMVM *)tao->data;
178:   PetscBool      is_spd;

181:   PetscOptionsHead(PetscOptionsObject,"Limited-memory variable-metric method for bound constrained optimization");
182:   PetscOptionsBool("-tao_blmvm_recycle","enable recycling of the BFGS matrix between subsequent TaoSolve() calls","",blmP->recycle,&blmP->recycle,NULL);
183:   PetscOptionsTail();
184:   TaoLineSearchSetFromOptions(tao->linesearch);
185:   MatSetFromOptions(blmP->M);
186:   MatGetOption(blmP->M, MAT_SPD, &is_spd);
187:   if (!is_spd) SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_INCOMP, "LMVM matrix must be symmetric positive-definite");
188:   return(0);
189: }

191: /*------------------------------------------------------------*/
192: static PetscErrorCode TaoView_BLMVM(Tao tao, PetscViewer viewer)
193: {
194:   TAO_BLMVM      *lmP = (TAO_BLMVM *)tao->data;
195:   PetscBool      isascii;

199:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii);
200:   if (isascii) {
201:     PetscViewerASCIIPrintf(viewer, "Gradient steps: %D\n", lmP->grad);
202:     PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO);
203:     MatView(lmP->M, viewer);
204:     PetscViewerPopFormat(viewer);
205:   }
206:   return(0);
207: }

209: static PetscErrorCode TaoComputeDual_BLMVM(Tao tao, Vec DXL, Vec DXU)
210: {
211:   TAO_BLMVM      *blm = (TAO_BLMVM *) tao->data;

218:   if (!tao->gradient || !blm->unprojected_gradient) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Dual variables don't exist yet or no longer exist.\n");

220:   VecCopy(tao->gradient,DXL);
221:   VecAXPY(DXL,-1.0,blm->unprojected_gradient);
222:   VecSet(DXU,0.0);
223:   VecPointwiseMax(DXL,DXL,DXU);

225:   VecCopy(blm->unprojected_gradient,DXU);
226:   VecAXPY(DXU,-1.0,tao->gradient);
227:   VecAXPY(DXU,1.0,DXL);
228:   return(0);
229: }

231: /* ---------------------------------------------------------- */
232: /*MC
233:   TAOBLMVM - Bounded limited memory variable metric is a quasi-Newton method
234:          for nonlinear minimization with bound constraints. It is an extension
235:          of TAOLMVM

237:   Options Database Keys:
238: .     -tao_lmm_recycle - enable recycling of LMVM information between subsequent TaoSolve calls

240:   Level: beginner
241: M*/
242: PETSC_EXTERN PetscErrorCode TaoCreate_BLMVM(Tao tao)
243: {
244:   TAO_BLMVM      *blmP;
245:   const char     *morethuente_type = TAOLINESEARCHMT;

249:   tao->ops->setup = TaoSetup_BLMVM;
250:   tao->ops->solve = TaoSolve_BLMVM;
251:   tao->ops->view = TaoView_BLMVM;
252:   tao->ops->setfromoptions = TaoSetFromOptions_BLMVM;
253:   tao->ops->destroy = TaoDestroy_BLMVM;
254:   tao->ops->computedual = TaoComputeDual_BLMVM;

256:   PetscNewLog(tao,&blmP);
257:   blmP->H0 = NULL;
258:   blmP->recycle = PETSC_FALSE;
259:   tao->data = (void*)blmP;

261:   /* Override default settings (unless already changed) */
262:   if (!tao->max_it_changed) tao->max_it = 2000;
263:   if (!tao->max_funcs_changed) tao->max_funcs = 4000;

265:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
266:   PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1);
267:   TaoLineSearchSetType(tao->linesearch, morethuente_type);
268:   TaoLineSearchUseTaoRoutines(tao->linesearch,tao);
269:   TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);

271:   KSPInitializePackage();
272:   MatCreate(((PetscObject)tao)->comm, &blmP->M);
273:   MatSetType(blmP->M, MATLMVMBFGS);
274:   PetscObjectIncrementTabLevel((PetscObject)blmP->M, (PetscObject)tao, 1);
275:   MatSetOptionsPrefix(blmP->M, "tao_blmvm_");
276:   return(0);
277: }

279: /*@
280:   TaoLMVMRecycle - Enable/disable recycling of the QN history between subsequent TaoSolve calls.

282:   Input Parameters:
283: +  tao  - the Tao solver context
284: -  flg - Boolean flag for recycling (PETSC_TRUE or PETSC_FALSE)

286:   Level: intermediate
287: @*/
288: PetscErrorCode TaoLMVMRecycle(Tao tao, PetscBool flg)
289: {
290:   TAO_LMVM       *lmP;
291:   TAO_BLMVM      *blmP;
292:   PetscBool      is_lmvm, is_blmvm;

296:   PetscObjectTypeCompare((PetscObject)tao,TAOLMVM,&is_lmvm);
297:   PetscObjectTypeCompare((PetscObject)tao,TAOBLMVM,&is_blmvm);
298:   if (is_lmvm) {
299:     lmP = (TAO_LMVM *)tao->data;
300:     lmP->recycle = flg;
301:   } else if (is_blmvm) {
302:     blmP = (TAO_BLMVM *)tao->data;
303:     blmP->recycle = flg;
304:   }
305:   return(0);
306: }

308: /*@
309:   TaoLMVMSetH0 - Set the initial Hessian for the QN approximation

311:   Input Parameters:
312: +  tao  - the Tao solver context
313: -  H0 - Mat object for the initial Hessian

315:   Level: advanced

317: .seealso: TaoLMVMGetH0(), TaoLMVMGetH0KSP()
318: @*/
319: PetscErrorCode TaoLMVMSetH0(Tao tao, Mat H0)
320: {
321:   TAO_LMVM       *lmP;
322:   TAO_BLMVM      *blmP;
323:   PetscBool      is_lmvm, is_blmvm;

327:   PetscObjectTypeCompare((PetscObject)tao,TAOLMVM,&is_lmvm);
328:   PetscObjectTypeCompare((PetscObject)tao,TAOBLMVM,&is_blmvm);
329:   if (is_lmvm) {
330:     lmP = (TAO_LMVM *)tao->data;
331:     PetscObjectReference((PetscObject)H0);
332:     lmP->H0 = H0;
333:   } else if (is_blmvm) {
334:     blmP = (TAO_BLMVM *)tao->data;
335:     PetscObjectReference((PetscObject)H0);
336:     blmP->H0 = H0;
337:   }
338:   return(0);
339: }

341: /*@
342:   TaoLMVMGetH0 - Get the matrix object for the QN initial Hessian

344:   Input Parameters:
345: .  tao  - the Tao solver context

347:   Output Parameters:
348: .  H0 - Mat object for the initial Hessian

350:   Level: advanced

352: .seealso: TaoLMVMSetH0(), TaoLMVMGetH0KSP()
353: @*/
354: PetscErrorCode TaoLMVMGetH0(Tao tao, Mat *H0)
355: {
356:   TAO_LMVM       *lmP;
357:   TAO_BLMVM      *blmP;
358:   PetscBool      is_lmvm, is_blmvm;
359:   Mat            M;

363:   PetscObjectTypeCompare((PetscObject)tao,TAOLMVM,&is_lmvm);
364:   PetscObjectTypeCompare((PetscObject)tao,TAOBLMVM,&is_blmvm);
365:   if (is_lmvm) {
366:     lmP = (TAO_LMVM *)tao->data;
367:     M = lmP->M;
368:   } else if (is_blmvm) {
369:     blmP = (TAO_BLMVM *)tao->data;
370:     M = blmP->M;
371:   } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONG, "This routine applies to TAO_LMVM and TAO_BLMVM.");
372:   MatLMVMGetJ0(M, H0);
373:   return(0);
374: }

376: /*@
377:   TaoLMVMGetH0KSP - Get the iterative solver for applying the inverse of the QN initial Hessian

379:   Input Parameters:
380: .  tao  - the Tao solver context

382:   Output Parameters:
383: .  ksp - KSP solver context for the initial Hessian

385:   Level: advanced

387: .seealso: TaoLMVMGetH0(), TaoLMVMGetH0KSP()
388: @*/
389: PetscErrorCode TaoLMVMGetH0KSP(Tao tao, KSP *ksp)
390: {
391:   TAO_LMVM       *lmP;
392:   TAO_BLMVM      *blmP;
393:   PetscBool      is_lmvm, is_blmvm;
394:   Mat            M;

398:   PetscObjectTypeCompare((PetscObject)tao,TAOLMVM,&is_lmvm);
399:   PetscObjectTypeCompare((PetscObject)tao,TAOBLMVM,&is_blmvm);
400:   if (is_lmvm) {
401:     lmP = (TAO_LMVM *)tao->data;
402:     M = lmP->M;
403:   } else if (is_blmvm) {
404:     blmP = (TAO_BLMVM *)tao->data;
405:     M = blmP->M;
406:   } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONG, "This routine applies to TAO_LMVM and TAO_BLMVM.");
407:   MatLMVMGetJ0KSP(M, ksp);
408:   return(0);
409: }