Actual source code: blmvm.c

petsc-master 2019-12-13
Report Typos and Errors
  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);


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

267:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
268:   PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1);
269:   TaoLineSearchSetType(tao->linesearch, morethuente_type);
270:   TaoLineSearchUseTaoRoutines(tao->linesearch,tao);
271:   TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);
272: 
273:   KSPInitializePackage();
274:   MatCreate(((PetscObject)tao)->comm, &blmP->M);
275:   MatSetType(blmP->M, MATLMVMBFGS);
276:   PetscObjectIncrementTabLevel((PetscObject)blmP->M, (PetscObject)tao, 1);
277:   MatSetOptionsPrefix(blmP->M, "tao_blmvm_");
278:   return(0);
279: }

281: PetscErrorCode TaoLMVMRecycle(Tao tao, PetscBool flg)
282: {
283:   TAO_LMVM       *lmP;
284:   TAO_BLMVM      *blmP;
285:   TaoType        type;
286:   PetscBool      is_lmvm, is_blmvm;
288: 
290:   TaoGetType(tao, &type);
291:   PetscStrcmp(type, TAOLMVM,  &is_lmvm);
292:   PetscStrcmp(type, TAOBLMVM, &is_blmvm);
293: 
294:   if (is_lmvm) {
295:     lmP = (TAO_LMVM *)tao->data;
296:     lmP->recycle = flg;
297:   } else if (is_blmvm) {
298:     blmP = (TAO_BLMVM *)tao->data;
299:     blmP->recycle = flg;
300:   } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "This routine applies to TAO_LMVM and TAO_BLMVM.");
301:   return(0);
302: }

304: PetscErrorCode TaoLMVMSetH0(Tao tao, Mat H0)
305: {
306:   TAO_LMVM       *lmP;
307:   TAO_BLMVM      *blmP;
308:   TaoType        type;
309:   PetscBool      is_lmvm, is_blmvm;
311: 
313:   TaoGetType(tao, &type);
314:   PetscStrcmp(type, TAOLMVM,  &is_lmvm);
315:   PetscStrcmp(type, TAOBLMVM, &is_blmvm);

317:   if (is_lmvm) {
318:     lmP = (TAO_LMVM *)tao->data;
319:     PetscObjectReference((PetscObject)H0);
320:     lmP->H0 = H0;
321:   } else if (is_blmvm) {
322:     blmP = (TAO_BLMVM *)tao->data;
323:     PetscObjectReference((PetscObject)H0);
324:     blmP->H0 = H0;
325:   } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "This routine applies to TAO_LMVM and TAO_BLMVM.");
326:   return(0);
327: }

329: PetscErrorCode TaoLMVMGetH0(Tao tao, Mat *H0)
330: {
331:   TAO_LMVM       *lmP;
332:   TAO_BLMVM      *blmP;
333:   TaoType        type;
334:   PetscBool      is_lmvm, is_blmvm;
335:   Mat            M;

338: 
340:   TaoGetType(tao, &type);
341:   PetscStrcmp(type, TAOLMVM,  &is_lmvm);
342:   PetscStrcmp(type, TAOBLMVM, &is_blmvm);

344:   if (is_lmvm) {
345:     lmP = (TAO_LMVM *)tao->data;
346:     M = lmP->M;
347:   } else if (is_blmvm) {
348:     blmP = (TAO_BLMVM *)tao->data;
349:     M = blmP->M;
350:   } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "This routine applies to TAO_LMVM and TAO_BLMVM.");
351:   MatLMVMGetJ0(M, H0);
352:   return(0);
353: }

355: PetscErrorCode TaoLMVMGetH0KSP(Tao tao, KSP *ksp)
356: {
357:   TAO_LMVM       *lmP;
358:   TAO_BLMVM      *blmP;
359:   TaoType        type;
360:   PetscBool      is_lmvm, is_blmvm;
361:   Mat            M;

364:   TaoGetType(tao, &type);
365:   PetscStrcmp(type, TAOLMVM,  &is_lmvm);
366:   PetscStrcmp(type, TAOBLMVM, &is_blmvm);

368:   if (is_lmvm) {
369:     lmP = (TAO_LMVM *)tao->data;
370:     M = lmP->M;
371:   } else if (is_blmvm) {
372:     blmP = (TAO_BLMVM *)tao->data;
373:     M = blmP->M;
374:   } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "This routine applies to TAO_LMVM and TAO_BLMVM.");
375:   MatLMVMGetJ0KSP(M, ksp);
376:   return(0);
377: }