Actual source code: gpcg.c

petsc-3.5.0 2014-06-30
Report Typos and Errors
  1: #include <petsc-private/kspimpl.h>
  2: #include <../src/tao/bound/impls/gpcg/gpcg.h>        /*I "gpcg.h" I*/


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

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

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

 33: /*------------------------------------------------------------*/
 36: static PetscErrorCode TaoSetFromOptions_GPCG(Tao tao)
 37: {
 38:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;
 40:   PetscBool      flg;

 43:   PetscOptionsHead("Gradient Projection, Conjugate Gradient method for bound constrained optimization");
 44:   ierr=PetscOptionsInt("-tao_gpcg_maxpgits","maximum number of gradient projections per GPCG iterate",0,gpcg->maxgpits,&gpcg->maxgpits,&flg);
 45:   PetscOptionsTail();
 46:   TaoLineSearchSetFromOptions(tao->linesearch);
 47:   return(0);
 48: }

 50: /*------------------------------------------------------------*/
 53: static PetscErrorCode TaoView_GPCG(Tao tao, PetscViewer viewer)
 54: {
 55:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;
 56:   PetscBool      isascii;

 60:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
 61:   if (isascii) {
 62:     PetscViewerASCIIPushTab(viewer);
 63:     PetscViewerASCIIPrintf(viewer,"Total PG its: %D,",gpcg->total_gp_its);
 64:     PetscViewerASCIIPrintf(viewer,"PG tolerance: %g \n",(double)gpcg->pg_ftol);
 65:     PetscViewerASCIIPopTab(viewer);
 66:   }
 67:   TaoLineSearchView(tao->linesearch,viewer);
 68:   return(0);
 69: }

 71: /* GPCGObjectiveAndGradient()
 72:    Compute f=0.5 * x'Hx + b'x + c
 73:            g=Hx + b
 74: */
 77: static PetscErrorCode GPCGObjectiveAndGradient(TaoLineSearch ls, Vec X, PetscReal *f, Vec G, void*tptr)
 78: {
 79:   Tao            tao = (Tao)tptr;
 80:   TAO_GPCG       *gpcg = (TAO_GPCG*)tao->data;
 82:   PetscReal      f1,f2;

 85:   MatMult(tao->hessian,X,G);
 86:   VecDot(G,X,&f1);
 87:   VecDot(gpcg->B,X,&f2);
 88:   VecAXPY(G,1.0,gpcg->B);
 89:   *f=f1/2.0 + f2 + gpcg->c;
 90:   return(0);
 91: }

 93: /* ---------------------------------------------------------- */
 96: static PetscErrorCode TaoSetup_GPCG(Tao tao)
 97: {
 99:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;

102:   /* Allocate some arrays */
103:   if (!tao->gradient) {
104:       VecDuplicate(tao->solution, &tao->gradient);
105:   }
106:   if (!tao->stepdirection) {
107:       VecDuplicate(tao->solution, &tao->stepdirection);
108:   }
109:   if (!tao->XL) {
110:       VecDuplicate(tao->solution,&tao->XL);
111:       VecSet(tao->XL,PETSC_NINFINITY);
112:   }
113:   if (!tao->XU) {
114:       VecDuplicate(tao->solution,&tao->XU);
115:       VecSet(tao->XU,PETSC_INFINITY);
116:   }

118:   ierr=VecDuplicate(tao->solution,&gpcg->B);
119:   ierr=VecDuplicate(tao->solution,&gpcg->Work);
120:   ierr=VecDuplicate(tao->solution,&gpcg->X_New);
121:   ierr=VecDuplicate(tao->solution,&gpcg->G_New);
122:   ierr=VecDuplicate(tao->solution,&gpcg->DXFree);
123:   ierr=VecDuplicate(tao->solution,&gpcg->R);
124:   ierr=VecDuplicate(tao->solution,&gpcg->PG);
125:   KSPCreate(((PetscObject)tao)->comm, &tao->ksp);
126:   KSPSetType(tao->ksp,KSPNASH);
127:   KSPSetFromOptions(tao->ksp);
128:   /*
129:     if (gpcg->ksp_type == GPCG_KSP_NASH) {
130:         KSPSetType(tao->ksp,KSPNASH);
131:       } else if (gpcg->ksp_type == GPCG_KSP_STCG) {
132:         KSPSetType(tao->ksp,KSPSTCG);
133:       } else {
134:         KSPSetType(tao->ksp,KSPGLTR);
135:       }
136:       if (tao->ksp->ops->setfromoptions) {
137:         (*tao->ksp->ops->setfromoptions)(tao->ksp);
138:       }

140:     }
141:   */
142:   return(0);
143: }

147: static PetscErrorCode TaoSolve_GPCG(Tao tao)
148: {
149:   TAO_GPCG                     *gpcg = (TAO_GPCG *)tao->data;
150:   PetscErrorCode               ierr;
151:   PetscInt                     iter=0,its;
152:   PetscReal                    actred,f,f_new,gnorm,gdx,stepsize,xtb;
153:   PetscReal                    xtHx;
154:   TaoConvergedReason           reason = TAO_CONTINUE_ITERATING;
155:   TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING;

158:   gpcg->Hsub=NULL;
159:   gpcg->Hsub_pre=NULL;

161:   TaoComputeVariableBounds(tao);
162:   VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);
163:   TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);

165:   /* Using f = .5*x'Hx + x'b + c and g=Hx + b,  compute b,c */
166:   TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);
167:   TaoComputeObjectiveAndGradient(tao,tao->solution,&f,tao->gradient);
168:   VecCopy(tao->gradient, gpcg->B);
169:   MatMult(tao->hessian,tao->solution,gpcg->Work);
170:   VecDot(gpcg->Work, tao->solution, &xtHx);
171:   VecAXPY(gpcg->B,-1.0,gpcg->Work);
172:   VecDot(gpcg->B,tao->solution,&xtb);
173:   gpcg->c=f-xtHx/2.0-xtb;
174:   if (gpcg->Free_Local) {
175:       ISDestroy(&gpcg->Free_Local);
176:   }
177:   VecWhichBetween(tao->XL,tao->solution,tao->XU,&gpcg->Free_Local);

179:   /* Project the gradient and calculate the norm */
180:   VecCopy(tao->gradient,gpcg->G_New);
181:   VecBoundGradientProjection(tao->gradient,tao->solution,tao->XL,tao->XU,gpcg->PG);
182:   VecNorm(gpcg->PG,NORM_2,&gpcg->gnorm);
183:   tao->step=1.0;
184:   gpcg->f = f;

186:     /* Check Stopping Condition      */
187:   ierr=TaoMonitor(tao,iter,f,gpcg->gnorm,0.0,tao->step,&reason);

189:   while (reason == TAO_CONTINUE_ITERATING){

191:     GPCGGradProjections(tao);
192:     ISGetSize(gpcg->Free_Local,&gpcg->n_free);

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

196:     KSPReset(tao->ksp);

198:     if (gpcg->n_free > 0){
199:       /* Create a reduced linear system */
200:       VecDestroy(&gpcg->R);
201:       VecDestroy(&gpcg->DXFree);
202:       TaoVecGetSubVec(tao->gradient,gpcg->Free_Local, tao->subset_type, 0.0, &gpcg->R);
203:       VecScale(gpcg->R, -1.0);
204:       TaoVecGetSubVec(tao->stepdirection,gpcg->Free_Local,tao->subset_type, 0.0, &gpcg->DXFree);
205:       VecSet(gpcg->DXFree,0.0);

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

209:       if (tao->hessian_pre == tao->hessian) {
210:         MatDestroy(&gpcg->Hsub_pre);
211:         PetscObjectReference((PetscObject)gpcg->Hsub);
212:         gpcg->Hsub_pre = gpcg->Hsub;
213:       }  else {
214:         TaoMatGetSubMat(tao->hessian, gpcg->Free_Local, gpcg->Work, tao->subset_type, &gpcg->Hsub_pre);
215:       }

217:       KSPReset(tao->ksp);
218:       KSPSetOperators(tao->ksp,gpcg->Hsub,gpcg->Hsub_pre);

220:       KSPSolve(tao->ksp,gpcg->R,gpcg->DXFree);
221:       KSPGetIterationNumber(tao->ksp,&its);
222:       tao->ksp_its+=its;

224:       VecSet(tao->stepdirection,0.0);
225:       VecISAXPY(tao->stepdirection,gpcg->Free_Local,1.0,gpcg->DXFree);

227:       VecDot(tao->stepdirection,tao->gradient,&gdx);
228:       TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);
229:       f_new=f;
230:       TaoLineSearchApply(tao->linesearch,tao->solution,&f_new,tao->gradient,tao->stepdirection,&stepsize,&ls_status);

232:       actred = f_new - f;

234:       /* Evaluate the function and gradient at the new point */
235:       VecBoundGradientProjection(tao->gradient,tao->solution,tao->XL,tao->XU, gpcg->PG);
236:       VecNorm(gpcg->PG, NORM_2, &gnorm);
237:       f=f_new;
238:       ISDestroy(&gpcg->Free_Local);
239:       VecWhichBetween(tao->XL,tao->solution,tao->XU,&gpcg->Free_Local);
240:     } else {
241:       actred = 0; gpcg->step=1.0;
242:       /* if there were no free variables, no cg method */
243:     }

245:     iter++;
246:     TaoMonitor(tao,iter,f,gnorm,0.0,gpcg->step,&reason);
247:     gpcg->f=f;gpcg->gnorm=gnorm; gpcg->actred=actred;
248:     if (reason!=TAO_CONTINUE_ITERATING) break;
249:   }  /* END MAIN LOOP  */

251:   return(0);
252: }

256: static PetscErrorCode GPCGGradProjections(Tao tao)
257: {
258:   PetscErrorCode                 ierr;
259:   TAO_GPCG                       *gpcg = (TAO_GPCG *)tao->data;
260:   PetscInt                       i;
261:   PetscReal                      actred=-1.0,actred_max=0.0, gAg,gtg=gpcg->gnorm,alpha;
262:   PetscReal                      f_new,gdx,stepsize;
263:   Vec                            DX=tao->stepdirection,XL=tao->XL,XU=tao->XU,Work=gpcg->Work;
264:   Vec                            X=tao->solution,G=tao->gradient;
265:   TaoLineSearchConvergedReason lsflag=TAOLINESEARCH_CONTINUE_ITERATING;

267:   /*
268:      The free, active, and binding variables should be already identified
269:   */
271:   for (i=0;i<gpcg->maxgpits;i++){
272:     if ( -actred <= (gpcg->pg_ftol)*actred_max) break;
273:     VecBoundGradientProjection(G,X,XL,XU,DX);
274:     VecScale(DX,-1.0);
275:     VecDot(DX,G,&gdx);

277:     MatMult(tao->hessian,DX,Work);
278:     VecDot(DX,Work,&gAg);

280:     gpcg->gp_iterates++;
281:     gpcg->total_gp_its++;

283:     gtg=-gdx;
284:     alpha = PetscAbsReal(gtg/gAg);
285:     TaoLineSearchSetInitialStepLength(tao->linesearch,alpha);
286:     f_new=gpcg->f;
287:     TaoLineSearchApply(tao->linesearch,X,&f_new,G,DX,&stepsize,&lsflag);

289:     /* Update the iterate */
290:     actred = f_new - gpcg->f;
291:     actred_max = PetscMax(actred_max,-(f_new - gpcg->f));
292:     gpcg->f = f_new;
293:     ISDestroy(&gpcg->Free_Local);
294:     VecWhichBetween(XL,X,XU,&gpcg->Free_Local);
295:   }

297:   gpcg->gnorm=gtg;
298:   return(0);
299: } /* End gradient projections */

303: static PetscErrorCode TaoComputeDual_GPCG(Tao tao, Vec DXL, Vec DXU)
304: {
305:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;

309:   VecBoundGradientProjection(tao->gradient, tao->solution, tao->XL, tao->XU, gpcg->Work);
310:   VecCopy(gpcg->Work, DXL);
311:   VecAXPY(DXL,-1.0,tao->gradient);
312:   VecSet(DXU,0.0);
313:   VecPointwiseMax(DXL,DXL,DXU);

315:   VecCopy(tao->gradient,DXU);
316:   VecAXPY(DXU,-1.0,gpcg->Work);
317:   VecSet(gpcg->Work,0.0);
318:   VecPointwiseMin(DXU,gpcg->Work,DXU);
319:   return(0);
320: }

322: /*------------------------------------------------------------*/
323: /*MC
324:   TAOGPCG - gradient projected conjugate gradient algorithm is an active-set
325:         conjugate-gradient based method for bound-constrained minimization

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

331:   Level: beginner
332: M*/
333: EXTERN_C_BEGIN
336: PetscErrorCode TaoCreate_GPCG(Tao tao)
337: {
338:   TAO_GPCG       *gpcg;

342:   tao->ops->setup = TaoSetup_GPCG;
343:   tao->ops->solve = TaoSolve_GPCG;
344:   tao->ops->view  = TaoView_GPCG;
345:   tao->ops->setfromoptions = TaoSetFromOptions_GPCG;
346:   tao->ops->destroy = TaoDestroy_GPCG;
347:   tao->ops->computedual = TaoComputeDual_GPCG;

349:   PetscNewLog(tao,&gpcg);
350:   tao->data = (void*)gpcg;

352:   tao->max_it = 500;
353:   tao->max_funcs = 100000;

355: #if defined(PETSC_USE_REAL_SINGLE)
356:   tao->fatol = 1e-6;
357:   tao->frtol = 1e-6;
358: #else
359:   tao->fatol = 1e-12;
360:   tao->frtol = 1e-12;
361: #endif

363:   /* Initialize pointers and variables */
364:   gpcg->n=0;
365:   gpcg->maxgpits = 8;
366:   gpcg->pg_ftol = 0.1;

368:   gpcg->gp_iterates=0; /* Cumulative number */
369:   gpcg->total_gp_its = 0;

371:   /* Initialize pointers and variables */
372:   gpcg->n_bind=0;
373:   gpcg->n_free = 0;
374:   gpcg->n_upper=0;
375:   gpcg->n_lower=0;
376:   gpcg->subset_type = TAO_SUBSET_MASK;
377:   /* gpcg->ksp_type = GPCG_KSP_STCG; */

379:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
380:   TaoLineSearchSetType(tao->linesearch, TAOLINESEARCHGPCG);
381:   TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch, GPCGObjectiveAndGradient, tao);
382:   return(0);
383: }
384: EXTERN_C_END