Actual source code: gpcg.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1:  #include <petscksp.h>
  2:  #include <../src/tao/bound/impls/gpcg/gpcg.h>


  5: static PetscErrorCode GPCGGradProjections(Tao tao);
  6: static PetscErrorCode GPCGObjectiveAndGradient(TaoLineSearch,Vec,PetscReal*,Vec,void*);

  8: /*------------------------------------------------------------*/
  9: static PetscErrorCode TaoDestroy_GPCG(Tao tao)
 10: {
 11:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;

 14:   /* Free allocated memory in GPCG structure */
 16:   VecDestroy(&gpcg->B);
 17:   VecDestroy(&gpcg->Work);
 18:   VecDestroy(&gpcg->X_New);
 19:   VecDestroy(&gpcg->G_New);
 20:   VecDestroy(&gpcg->DXFree);
 21:   VecDestroy(&gpcg->R);
 22:   VecDestroy(&gpcg->PG);
 23:   MatDestroy(&gpcg->Hsub);
 24:   MatDestroy(&gpcg->Hsub_pre);
 25:   ISDestroy(&gpcg->Free_Local);
 26:   PetscFree(tao->data);
 27:   return(0);
 28: }

 30: /*------------------------------------------------------------*/
 31: static PetscErrorCode TaoSetFromOptions_GPCG(PetscOptionItems *PetscOptionsObject,Tao tao)
 32: {
 33:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;
 35:   PetscBool      flg;

 38:   PetscOptionsHead(PetscOptionsObject,"Gradient Projection, Conjugate Gradient method for bound constrained optimization");
 39:   ierr=PetscOptionsInt("-tao_gpcg_maxpgits","maximum number of gradient projections per GPCG iterate",NULL,gpcg->maxgpits,&gpcg->maxgpits,&flg);
 40:   PetscOptionsTail();
 41:   KSPSetFromOptions(tao->ksp);
 42:   TaoLineSearchSetFromOptions(tao->linesearch);
 43:   return(0);
 44: }

 46: /*------------------------------------------------------------*/
 47: static PetscErrorCode TaoView_GPCG(Tao tao, PetscViewer viewer)
 48: {
 49:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;
 50:   PetscBool      isascii;

 54:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
 55:   if (isascii) {
 56:     PetscViewerASCIIPushTab(viewer);
 57:     PetscViewerASCIIPrintf(viewer,"Total PG its: %D,",gpcg->total_gp_its);
 58:     PetscViewerASCIIPrintf(viewer,"PG tolerance: %g \n",(double)gpcg->pg_ftol);
 59:     PetscViewerASCIIPopTab(viewer);
 60:   }
 61:   TaoLineSearchView(tao->linesearch,viewer);
 62:   return(0);
 63: }

 65: /* GPCGObjectiveAndGradient()
 66:    Compute f=0.5 * x'Hx + b'x + c
 67:            g=Hx + b
 68: */
 69: static PetscErrorCode GPCGObjectiveAndGradient(TaoLineSearch ls, Vec X, PetscReal *f, Vec G, void*tptr)
 70: {
 71:   Tao            tao = (Tao)tptr;
 72:   TAO_GPCG       *gpcg = (TAO_GPCG*)tao->data;
 74:   PetscReal      f1,f2;

 77:   MatMult(tao->hessian,X,G);
 78:   VecDot(G,X,&f1);
 79:   VecDot(gpcg->B,X,&f2);
 80:   VecAXPY(G,1.0,gpcg->B);
 81:   *f=f1/2.0 + f2 + gpcg->c;
 82:   return(0);
 83: }

 85: /* ---------------------------------------------------------- */
 86: static PetscErrorCode TaoSetup_GPCG(Tao tao)
 87: {
 89:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;

 92:   /* Allocate some arrays */
 93:   if (!tao->gradient) {
 94:       VecDuplicate(tao->solution, &tao->gradient);
 95:   }
 96:   if (!tao->stepdirection) {
 97:       VecDuplicate(tao->solution, &tao->stepdirection);
 98:   }
 99:   if (!tao->XL) {
100:       VecDuplicate(tao->solution,&tao->XL);
101:       VecSet(tao->XL,PETSC_NINFINITY);
102:   }
103:   if (!tao->XU) {
104:       VecDuplicate(tao->solution,&tao->XU);
105:       VecSet(tao->XU,PETSC_INFINITY);
106:   }

108:   ierr=VecDuplicate(tao->solution,&gpcg->B);
109:   ierr=VecDuplicate(tao->solution,&gpcg->Work);
110:   ierr=VecDuplicate(tao->solution,&gpcg->X_New);
111:   ierr=VecDuplicate(tao->solution,&gpcg->G_New);
112:   ierr=VecDuplicate(tao->solution,&gpcg->DXFree);
113:   ierr=VecDuplicate(tao->solution,&gpcg->R);
114:   ierr=VecDuplicate(tao->solution,&gpcg->PG);
115:   /*
116:     if (gpcg->ksp_type == GPCG_KSP_NASH) {
117:         KSPSetType(tao->ksp,KSPCGNASH);
118:       } else if (gpcg->ksp_type == GPCG_KSP_STCG) {
119:         KSPSetType(tao->ksp,KSPCGSTCG);
120:       } else {
121:         KSPSetType(tao->ksp,KSPCGGLTR);
122:       }
123:       if (tao->ksp->ops->setfromoptions) {
124:         (*tao->ksp->ops->setfromoptions)(tao->ksp);
125:       }

127:     }
128:   */
129:   return(0);
130: }

132: static PetscErrorCode TaoSolve_GPCG(Tao tao)
133: {
134:   TAO_GPCG                     *gpcg = (TAO_GPCG *)tao->data;
135:   PetscErrorCode               ierr;
136:   PetscInt                     its;
137:   PetscReal                    actred,f,f_new,gnorm,gdx,stepsize,xtb;
138:   PetscReal                    xtHx;
139:   TaoConvergedReason           reason = TAO_CONTINUE_ITERATING;
140:   TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING;


144:   TaoComputeVariableBounds(tao);
145:   VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);
146:   TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);

148:   /* Using f = .5*x'Hx + x'b + c and g=Hx + b,  compute b,c */
149:   TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);
150:   TaoComputeObjectiveAndGradient(tao,tao->solution,&f,tao->gradient);
151:   VecCopy(tao->gradient, gpcg->B);
152:   MatMult(tao->hessian,tao->solution,gpcg->Work);
153:   VecDot(gpcg->Work, tao->solution, &xtHx);
154:   VecAXPY(gpcg->B,-1.0,gpcg->Work);
155:   VecDot(gpcg->B,tao->solution,&xtb);
156:   gpcg->c=f-xtHx/2.0-xtb;
157:   if (gpcg->Free_Local) {
158:       ISDestroy(&gpcg->Free_Local);
159:   }
160:   VecWhichBetween(tao->XL,tao->solution,tao->XU,&gpcg->Free_Local);

162:   /* Project the gradient and calculate the norm */
163:   VecCopy(tao->gradient,gpcg->G_New);
164:   VecBoundGradientProjection(tao->gradient,tao->solution,tao->XL,tao->XU,gpcg->PG);
165:   VecNorm(gpcg->PG,NORM_2,&gpcg->gnorm);
166:   tao->step=1.0;
167:   gpcg->f = f;

169:     /* Check Stopping Condition      */
170:   ierr=TaoMonitor(tao,tao->niter,f,gpcg->gnorm,0.0,tao->step,&reason);

172:   while (reason == TAO_CONTINUE_ITERATING){
173:     tao->ksp_its=0;

175:     GPCGGradProjections(tao);
176:     ISGetSize(gpcg->Free_Local,&gpcg->n_free);

178:     f=gpcg->f; gnorm=gpcg->gnorm;

180:     KSPReset(tao->ksp);

182:     if (gpcg->n_free > 0){
183:       /* Create a reduced linear system */
184:       VecDestroy(&gpcg->R);
185:       VecDestroy(&gpcg->DXFree);
186:       TaoVecGetSubVec(tao->gradient,gpcg->Free_Local, tao->subset_type, 0.0, &gpcg->R);
187:       VecScale(gpcg->R, -1.0);
188:       TaoVecGetSubVec(tao->stepdirection,gpcg->Free_Local,tao->subset_type, 0.0, &gpcg->DXFree);
189:       VecSet(gpcg->DXFree,0.0);

191:       TaoMatGetSubMat(tao->hessian, gpcg->Free_Local, gpcg->Work, tao->subset_type, &gpcg->Hsub);

193:       if (tao->hessian_pre == tao->hessian) {
194:         MatDestroy(&gpcg->Hsub_pre);
195:         PetscObjectReference((PetscObject)gpcg->Hsub);
196:         gpcg->Hsub_pre = gpcg->Hsub;
197:       }  else {
198:         TaoMatGetSubMat(tao->hessian, gpcg->Free_Local, gpcg->Work, tao->subset_type, &gpcg->Hsub_pre);
199:       }

201:       KSPReset(tao->ksp);
202:       KSPSetOperators(tao->ksp,gpcg->Hsub,gpcg->Hsub_pre);

204:       KSPSolve(tao->ksp,gpcg->R,gpcg->DXFree);
205:       KSPGetIterationNumber(tao->ksp,&its);
206:       tao->ksp_its+=its;
207:       tao->ksp_tot_its+=its;
208:       VecSet(tao->stepdirection,0.0);
209:       VecISAXPY(tao->stepdirection,gpcg->Free_Local,1.0,gpcg->DXFree);

211:       VecDot(tao->stepdirection,tao->gradient,&gdx);
212:       TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);
213:       f_new=f;
214:       TaoLineSearchApply(tao->linesearch,tao->solution,&f_new,tao->gradient,tao->stepdirection,&stepsize,&ls_status);

216:       actred = f_new - f;

218:       /* Evaluate the function and gradient at the new point */
219:       VecBoundGradientProjection(tao->gradient,tao->solution,tao->XL,tao->XU, gpcg->PG);
220:       VecNorm(gpcg->PG, NORM_2, &gnorm);
221:       f=f_new;
222:       ISDestroy(&gpcg->Free_Local);
223:       VecWhichBetween(tao->XL,tao->solution,tao->XU,&gpcg->Free_Local);
224:     } else {
225:       actred = 0; gpcg->step=1.0;
226:       /* if there were no free variables, no cg method */
227:     }

229:     tao->niter++;
230:     TaoMonitor(tao,tao->niter,f,gnorm,0.0,gpcg->step,&reason);
231:     gpcg->f=f;gpcg->gnorm=gnorm; gpcg->actred=actred;
232:     if (reason!=TAO_CONTINUE_ITERATING) break;
233:   }  /* END MAIN LOOP  */

235:   return(0);
236: }

238: static PetscErrorCode GPCGGradProjections(Tao tao)
239: {
240:   PetscErrorCode                 ierr;
241:   TAO_GPCG                       *gpcg = (TAO_GPCG *)tao->data;
242:   PetscInt                       i;
243:   PetscReal                      actred=-1.0,actred_max=0.0, gAg,gtg=gpcg->gnorm,alpha;
244:   PetscReal                      f_new,gdx,stepsize;
245:   Vec                            DX=tao->stepdirection,XL=tao->XL,XU=tao->XU,Work=gpcg->Work;
246:   Vec                            X=tao->solution,G=tao->gradient;
247:   TaoLineSearchConvergedReason lsflag=TAOLINESEARCH_CONTINUE_ITERATING;

249:   /*
250:      The free, active, and binding variables should be already identified
251:   */
253:   for (i=0;i<gpcg->maxgpits;i++){
254:     if ( -actred <= (gpcg->pg_ftol)*actred_max) break;
255:     VecBoundGradientProjection(G,X,XL,XU,DX);
256:     VecScale(DX,-1.0);
257:     VecDot(DX,G,&gdx);

259:     MatMult(tao->hessian,DX,Work);
260:     VecDot(DX,Work,&gAg);

262:     gpcg->gp_iterates++;
263:     gpcg->total_gp_its++;

265:     gtg=-gdx;
266:     if (PetscAbsReal(gAg) == 0.0) {
267:       alpha = 1.0;
268:     } else {
269:       alpha = PetscAbsReal(gtg/gAg);
270:     }
271:     TaoLineSearchSetInitialStepLength(tao->linesearch,alpha);
272:     f_new=gpcg->f;
273:     TaoLineSearchApply(tao->linesearch,X,&f_new,G,DX,&stepsize,&lsflag);

275:     /* Update the iterate */
276:     actred = f_new - gpcg->f;
277:     actred_max = PetscMax(actred_max,-(f_new - gpcg->f));
278:     gpcg->f = f_new;
279:     ISDestroy(&gpcg->Free_Local);
280:     VecWhichBetween(XL,X,XU,&gpcg->Free_Local);
281:   }

283:   gpcg->gnorm=gtg;
284:   return(0);
285: } /* End gradient projections */

287: static PetscErrorCode TaoComputeDual_GPCG(Tao tao, Vec DXL, Vec DXU)
288: {
289:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;

293:   VecBoundGradientProjection(tao->gradient, tao->solution, tao->XL, tao->XU, gpcg->Work);
294:   VecCopy(gpcg->Work, DXL);
295:   VecAXPY(DXL,-1.0,tao->gradient);
296:   VecSet(DXU,0.0);
297:   VecPointwiseMax(DXL,DXL,DXU);

299:   VecCopy(tao->gradient,DXU);
300:   VecAXPY(DXU,-1.0,gpcg->Work);
301:   VecSet(gpcg->Work,0.0);
302:   VecPointwiseMin(DXU,gpcg->Work,DXU);
303:   return(0);
304: }

306: /*------------------------------------------------------------*/
307: /*MC
308:   TAOGPCG - gradient projected conjugate gradient algorithm is an active-set
309:         conjugate-gradient based method for bound-constrained minimization

311:   Options Database Keys:
312: + -tao_gpcg_maxpgits - maximum number of gradient projections for GPCG iterate
313: - -tao_subset_type - "subvec","mask","matrix-free", strategies for handling active-sets

315:   Level: beginner
316: M*/
317: PETSC_EXTERN PetscErrorCode TaoCreate_GPCG(Tao tao)
318: {
319:   TAO_GPCG       *gpcg;

323:   tao->ops->setup = TaoSetup_GPCG;
324:   tao->ops->solve = TaoSolve_GPCG;
325:   tao->ops->view  = TaoView_GPCG;
326:   tao->ops->setfromoptions = TaoSetFromOptions_GPCG;
327:   tao->ops->destroy = TaoDestroy_GPCG;
328:   tao->ops->computedual = TaoComputeDual_GPCG;

330:   PetscNewLog(tao,&gpcg);
331:   tao->data = (void*)gpcg;

333:   /* Override default settings (unless already changed) */
334:   if (!tao->max_it_changed) tao->max_it=500;
335:   if (!tao->max_funcs_changed) tao->max_funcs = 100000;
336: #if defined(PETSC_USE_REAL_SINGLE)
337:   if (!tao->gatol_changed) tao->gatol=1e-6;
338:   if (!tao->grtol_changed) tao->grtol=1e-6;
339: #else
340:   if (!tao->gatol_changed) tao->gatol=1e-12;
341:   if (!tao->grtol_changed) tao->grtol=1e-12;
342: #endif

344:   /* Initialize pointers and variables */
345:   gpcg->n=0;
346:   gpcg->maxgpits = 8;
347:   gpcg->pg_ftol = 0.1;

349:   gpcg->gp_iterates=0; /* Cumulative number */
350:   gpcg->total_gp_its = 0;

352:   /* Initialize pointers and variables */
353:   gpcg->n_bind=0;
354:   gpcg->n_free = 0;
355:   gpcg->n_upper=0;
356:   gpcg->n_lower=0;
357:   gpcg->subset_type = TAO_SUBSET_MASK;
358:   gpcg->Hsub=NULL;
359:   gpcg->Hsub_pre=NULL;

361:   KSPCreate(((PetscObject)tao)->comm, &tao->ksp);
362:   KSPSetOptionsPrefix(tao->ksp, tao->hdr.prefix);
363:   KSPSetType(tao->ksp,KSPCGNASH);

365:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
366:   TaoLineSearchSetType(tao->linesearch, TAOLINESEARCHGPCG);
367:   TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch, GPCGObjectiveAndGradient, tao);
368:   TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);
369:   return(0);
370: }