Actual source code: gpcg.c

petsc-dev 2014-02-02
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("-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:   MatStructure                   structure;
155:   TaoTerminationReason     reason = TAO_CONTINUE_ITERATING;
156:   TaoLineSearchTerminationReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING;

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

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

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

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

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

190:   while (reason == TAO_CONTINUE_ITERATING){

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

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

197:     KSPReset(tao->ksp);

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

208:       MatGetSubMat(tao->hessian, gpcg->Free_Local, gpcg->Work, tao->subset_type, &gpcg->Hsub);

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

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

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

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

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

233:       actred = f_new - f;

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

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

252:   return(0);
253: }

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

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

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

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

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

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

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

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

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

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

323: /*------------------------------------------------------------*/
324: EXTERN_C_BEGIN
327: PetscErrorCode TaoCreate_GPCG(Tao tao)
328: {
329:   TAO_GPCG       *gpcg;

333:   tao->ops->setup = TaoSetup_GPCG;
334:   tao->ops->solve = TaoSolve_GPCG;
335:   tao->ops->view  = TaoView_GPCG;
336:   tao->ops->setfromoptions = TaoSetFromOptions_GPCG;
337:   tao->ops->destroy = TaoDestroy_GPCG;
338:   tao->ops->computedual = TaoComputeDual_GPCG;

340:   PetscNewLog(tao,&gpcg);
341:   tao->data = (void*)gpcg;

343:   tao->max_it = 500;
344:   tao->max_funcs = 100000;

346: #if defined(PETSC_USE_REAL_SINGLE)
347:   tao->fatol = 1e-6;
348:   tao->frtol = 1e-6;
349: #else
350:   tao->fatol = 1e-12;
351:   tao->frtol = 1e-12;
352: #endif

354:   /* Initialize pointers and variables */
355:   gpcg->n=0;
356:   gpcg->maxgpits = 8;
357:   gpcg->pg_ftol = 0.1;

359:   gpcg->gp_iterates=0; /* Cumulative number */
360:   gpcg->total_gp_its = 0;

362:   /* Initialize pointers and variables */
363:   gpcg->n_bind=0;
364:   gpcg->n_free = 0;
365:   gpcg->n_upper=0;
366:   gpcg->n_lower=0;
367:   gpcg->subset_type = TAO_SUBSET_MASK;
368:   /* gpcg->ksp_type = GPCG_KSP_STCG; */

370:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
371:   TaoLineSearchSetType(tao->linesearch, TAOLINESEARCH_GPCG);
372:   TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch, GPCGObjectiveAndGradient, tao);
373:   return(0);
374: }
375: EXTERN_C_END