Date: 2015-10-02T22:30:46+00:00

Actual source code: gpcg.c
petsc-3.6.2 2015-10-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(PetscOptions *PetscOptionsObject,Tao tao)
     37: {
     38:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;
     40:   PetscBool      flg;

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

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

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

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

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

     94: /* ---------------------------------------------------------- */
     97: static PetscErrorCode TaoSetup_GPCG(Tao tao)
     98: {
    100:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;

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

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

    138:     }
    139:   */
    140:   return(0);
    141: }

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

    157:   TaoComputeVariableBounds(tao);
    158:   VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);
    159:   TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);

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

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

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

    185:   while (reason == TAO_CONTINUE_ITERATING){
    186:     tao->ksp_its=0;

    188:     GPCGGradProjections(tao);
    189:     ISGetSize(gpcg->Free_Local,&gpcg->n_free);

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

    193:     KSPReset(tao->ksp);

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

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

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

    214:       KSPReset(tao->ksp);
    215:       KSPSetOperators(tao->ksp,gpcg->Hsub,gpcg->Hsub_pre);

    217:       KSPSolve(tao->ksp,gpcg->R,gpcg->DXFree);
    218:       KSPGetIterationNumber(tao->ksp,&its);
    219:       tao->ksp_its+=its;
    220:       tao->ksp_tot_its+=its;
    221:       VecSet(tao->stepdirection,0.0);
    222:       VecISAXPY(tao->stepdirection,gpcg->Free_Local,1.0,gpcg->DXFree);

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

    229:       actred = f_new - f;

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

    242:     tao->niter++;
    243:     TaoMonitor(tao,tao->niter,f,gnorm,0.0,gpcg->step,&reason);
    244:     gpcg->f=f;gpcg->gnorm=gnorm; gpcg->actred=actred;
    245:     if (reason!=TAO_CONTINUE_ITERATING) break;
    246:   }  /* END MAIN LOOP  */

    248:   return(0);
    249: }

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

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

    274:     MatMult(tao->hessian,DX,Work);
    275:     VecDot(DX,Work,&gAg);

    277:     gpcg->gp_iterates++;
    278:     gpcg->total_gp_its++;

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

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

    294:   gpcg->gnorm=gtg;
    295:   return(0);
    296: } /* End gradient projections */

    300: static PetscErrorCode TaoComputeDual_GPCG(Tao tao, Vec DXL, Vec DXU)
    301: {
    302:   TAO_GPCG       *gpcg = (TAO_GPCG *)tao->data;

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

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

    319: /*------------------------------------------------------------*/
    320: /*MC
    321:   TAOGPCG - gradient projected conjugate gradient algorithm is an active-set
    322:         conjugate-gradient based method for bound-constrained minimization

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

    328:   Level: beginner
    329: M*/
    332: PETSC_EXTERN PetscErrorCode TaoCreate_GPCG(Tao tao)
    333: {
    334:   TAO_GPCG       *gpcg;

    338:   tao->ops->setup = TaoSetup_GPCG;
    339:   tao->ops->solve = TaoSolve_GPCG;
    340:   tao->ops->view  = TaoView_GPCG;
    341:   tao->ops->setfromoptions = TaoSetFromOptions_GPCG;
    342:   tao->ops->destroy = TaoDestroy_GPCG;
    343:   tao->ops->computedual = TaoComputeDual_GPCG;

    345:   PetscNewLog(tao,&gpcg);
    346:   tao->data = (void*)gpcg;

    348:   /* Override default settings (unless already changed) */
    349:   if (!tao->max_it_changed) tao->max_it=500;
    350:   if (!tao->max_funcs_changed) tao->max_funcs = 100000;
    351: #if defined(PETSC_USE_REAL_SINGLE)
    352:   if (!tao->fatol_changed) tao->fatol=1e-6;
    353:   if (!tao->frtol_changed) tao->frtol=1e-6;
    354:   if (!tao->gatol_changed) tao->grtol=1e-6;
    355:   if (!tao->grtol_changed) tao->grtol=1e-6;
    356: #else
    357:   if (!tao->fatol_changed) tao->fatol=1e-12;
    358:   if (!tao->frtol_changed) tao->frtol=1e-12;
    359:   if (!tao->gatol_changed) tao->grtol=1e-12;
    360:   if (!tao->grtol_changed) tao->grtol=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->Hsub=NULL;
    378:   gpcg->Hsub_pre=NULL;

    380:   KSPCreate(((PetscObject)tao)->comm, &tao->ksp);
    381:   KSPSetOptionsPrefix(tao->ksp, tao->hdr.prefix);
    382:   KSPSetType(tao->ksp,KSPNASH);

    384:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
    385:   TaoLineSearchSetType(tao->linesearch, TAOLINESEARCHGPCG);
    386:   TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch, GPCGObjectiveAndGradient, tao);
    387:   TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);
    388:   return(0);
    389: }