Actual source code: lmvm.c

  1: #include "taolinesearch.h"
  2: #include "src/matrix/lmvmmat.h"
  3: #include "lmvm.h"

  5: #define LMVM_BFGS                0
  6: #define LMVM_SCALED_GRADIENT     1
  7: #define LMVM_GRADIENT            2

 11: static PetscErrorCode TaoSolve_LMVM(TaoSolver tao)
 12: {

 14:   TAO_LMVM *lmP = (TAO_LMVM *)tao->data;
 15:   
 16:   PetscReal f, fold, gdx, gnorm;
 17:   PetscReal step = 1.0;

 19:   PetscReal delta;

 22:   PetscInt stepType;
 23:   PetscInt iter = 0;
 24:   TaoSolverTerminationReason reason = TAO_CONTINUE_ITERATING;
 25:   TaoLineSearchTerminationReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING;


 29:   if (tao->XL || tao->XU || tao->ops->computebounds) {
 30:     PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by lmvm algorithm\n"); 
 31:   }

 33:   /*  Check convergence criteria */
 34:   TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient); 
 35:   VecNorm(tao->gradient,NORM_2,&gnorm); 
 36:   if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) {
 37:     SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
 38:   }

 40:   TaoMonitor(tao, iter, f, gnorm, 0.0, step, &reason); 
 41:   if (reason != TAO_CONTINUE_ITERATING) {
 42:     return(0);
 43:   }

 45:   /*  Set initial scaling for the function */
 46:   if (f != 0.0) {
 47:     delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
 48:   }
 49:   else {
 50:     delta = 2.0 / (gnorm*gnorm);
 51:   }
 52:   MatLMVMSetDelta(lmP->M,delta); 

 54:   /*  Set counter for gradient/reset steps */
 55:   lmP->bfgs = 0;
 56:   lmP->sgrad = 0;
 57:   lmP->grad = 0;

 59:   /*  Have not converged; continue with Newton method */
 60:   while (reason == TAO_CONTINUE_ITERATING) {
 61:     /*  Compute direction */
 62:     MatLMVMUpdate(lmP->M,tao->solution,tao->gradient); 
 63:     MatLMVMSolve(lmP->M, tao->gradient, lmP->D); 
 64:     ++lmP->bfgs;

 66:     /*  Check for success (descent direction) */
 67:     VecDot(lmP->D, tao->gradient, &gdx); 
 68:     if ((gdx <= 0.0) || PetscIsInfOrNanReal(gdx)) {
 69:       /* Step is not descent or direction produced not a number
 70:          We can assert bfgsUpdates > 1 in this case because
 71:          the first solve produces the scaled gradient direction,
 72:          which is guaranteed to be descent
 73:         
 74:          Use steepest descent direction (scaled)
 75:       */

 77:       ++lmP->grad;

 79:       if (f != 0.0) {
 80:         delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
 81:       }
 82:       else {
 83:         delta = 2.0 / (gnorm*gnorm);
 84:       }
 85:       MatLMVMSetDelta(lmP->M, delta); 
 86:       MatLMVMReset(lmP->M); 
 87:       MatLMVMUpdate(lmP->M, tao->solution, tao->gradient); 
 88:       MatLMVMSolve(lmP->M,tao->gradient, lmP->D); 

 90:       /* On a reset, the direction cannot be not a number; it is a 
 91:          scaled gradient step.  No need to check for this condition. */

 93:       lmP->bfgs = 1;
 94:       ++lmP->sgrad;
 95:       stepType = LMVM_SCALED_GRADIENT;
 96:     }
 97:     else {
 98:       if (1 == lmP->bfgs) {
 99:         /*  The first BFGS direction is always the scaled gradient */
100:         ++lmP->sgrad;
101:         stepType = LMVM_SCALED_GRADIENT;
102:       }
103:       else {
104:         ++lmP->bfgs;
105:         stepType = LMVM_BFGS;
106:       }
107:     }
108:     VecScale(lmP->D, -1.0); 
109:     
110:     /*  Perform the linesearch */
111:     fold = f;
112:     VecCopy(tao->solution, lmP->Xold); 
113:     VecCopy(tao->gradient, lmP->Gold); 

115:     TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, lmP->D, &step,&ls_status); 
116:     TaoAddLineSearchCounts(tao); 
117:     

119:     while (((int)ls_status < 0) && (stepType != LMVM_GRADIENT)) {
120:       /*  Linesearch failed */
121:       /*  Reset factors and use scaled gradient step */
122:       f = fold;
123:       VecCopy(lmP->Xold, tao->solution); 
124:       VecCopy(lmP->Gold, tao->gradient); 
125:         
126:       switch(stepType) {
127:       case LMVM_BFGS:
128:         /*  Failed to obtain acceptable iterate with BFGS step */
129:         /*  Attempt to use the scaled gradient direction */

131:         if (f != 0.0) {
132:           delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
133:         }
134:         else {
135:           delta = 2.0 / (gnorm*gnorm);
136:         }
137:         MatLMVMSetDelta(lmP->M, delta); 
138:         MatLMVMReset(lmP->M); 
139:         MatLMVMUpdate(lmP->M, tao->solution, tao->gradient); 
140:         MatLMVMSolve(lmP->M, tao->gradient, lmP->D); 

142:         /* On a reset, the direction cannot be not a number; it is a 
143:            scaled gradient step.  No need to check for this condition. */
144:   
145:         lmP->bfgs = 1;
146:         ++lmP->sgrad;
147:         stepType = LMVM_SCALED_GRADIENT;
148:         break;

150:       case LMVM_SCALED_GRADIENT:
151:         /* The scaled gradient step did not produce a new iterate;
152:            attempt to use the gradient direction.
153:            Need to make sure we are not using a different diagonal scaling */
154:         MatLMVMSetDelta(lmP->M, 1.0); 
155:         MatLMVMReset(lmP->M); 
156:         MatLMVMUpdate(lmP->M, tao->solution, tao->gradient); 
157:         MatLMVMSolve(lmP->M, tao->gradient, lmP->D); 

159:         lmP->bfgs = 1;
160:         ++lmP->grad;
161:         stepType = LMVM_GRADIENT;
162:         break;
163:       }
164:       VecScale(lmP->D, -1.0); 
165:         
166:       /*  Perform the linesearch */
167:       TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, lmP->D, &step, &ls_status); 
168:       TaoAddLineSearchCounts(tao); 
169:       
170:     }

172:     if ((int)ls_status < 0) {
173:       /*  Failed to find an improving point */
174:       f = fold;
175:       VecCopy(lmP->Xold, tao->solution); 
176:       VecCopy(lmP->Gold, tao->gradient); 
177:       step = 0.0;
178:     }
179:     /*  Check for termination */
180:     VecNorm(tao->gradient, NORM_2, &gnorm); 
181:     iter++;
182:     TaoMonitor(tao,iter,f,gnorm,0.0,step,&reason); 
183:   }
184:   return(0);
185: }
188: static PetscErrorCode TaoSetUp_LMVM(TaoSolver tao)
189: {
190:   TAO_LMVM *lmP = (TAO_LMVM *)tao->data;
191:   PetscInt n,N;

195:   /* Existence of tao->solution checked in TaoSetUp() */
196:   if (!tao->gradient) {VecDuplicate(tao->solution,&tao->gradient);   }
197:   if (!tao->stepdirection) {VecDuplicate(tao->solution,&tao->stepdirection);   }
198:   if (!lmP->D) {VecDuplicate(tao->solution,&lmP->D);   }
199:   if (!lmP->Xold) {VecDuplicate(tao->solution,&lmP->Xold);   }
200:   if (!lmP->Gold) {VecDuplicate(tao->solution,&lmP->Gold);   }
201:   
202:   /*  Create matrix for the limited memory approximation */
203:   VecGetLocalSize(tao->solution,&n); 
204:   VecGetSize(tao->solution,&N); 
205:   MatCreateLMVM(((PetscObject)tao)->comm,n,N,&lmP->M); 
206:   MatLMVMAllocateVectors(lmP->M,tao->solution); 
207:   

209:   return(0);
210: }

212: /* ---------------------------------------------------------- */
215: static PetscErrorCode TaoDestroy_LMVM(TaoSolver tao)
216: {

218:   TAO_LMVM *lmP = (TAO_LMVM *)tao->data;

222:   if (tao->setupcalled) {
223:     VecDestroy(&lmP->Xold); 
224:     VecDestroy(&lmP->Gold); 
225:     VecDestroy(&lmP->D); 
226:     MatDestroy(&lmP->M); 
227:   }
228:   PetscFree(tao->data); 
229:   tao->data = PETSC_NULL;

231:   return(0); 
232: }

234: /*------------------------------------------------------------*/
237: static PetscErrorCode TaoSetFromOptions_LMVM(TaoSolver tao)
238: {


243:   PetscOptionsHead("Limited-memory variable-metric method for unconstrained optimization"); 
244:   TaoLineSearchSetFromOptions(tao->linesearch); 
245:   PetscOptionsTail(); 
246:   return(0);

248:   return(0);
249: }

251: /*------------------------------------------------------------*/
254: static PetscErrorCode TaoView_LMVM(TaoSolver tao, PetscViewer viewer)
255: {

257:     TAO_LMVM *lm = (TAO_LMVM *)tao->data;
258:     PetscBool isascii;


263:     PetscTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii); 
264:     if (isascii) {

266:         PetscViewerASCIIPushTab(viewer); 
267:         PetscViewerASCIIPrintf(viewer, "BFGS steps: %D\n", lm->bfgs); 
268:         PetscViewerASCIIPrintf(viewer, "Scaled gradient steps: %D\n", lm->sgrad); 
269:         PetscViewerASCIIPrintf(viewer, "Gradient steps: %D\n", lm->grad); 
270:         PetscViewerASCIIPopTab(viewer); 
271:     } else {
272:       SETERRQ1(((PetscObject)tao)->comm,PETSC_ERR_SUP,"Viewer type %s not supported for TAO LMVM",((PetscObject)viewer)->type_name);
273:     }
274:     return(0);
275: }

277: /* ---------------------------------------------------------- */

282: PetscErrorCode TaoCreate_LMVM(TaoSolver tao)
283: {
284:     
285:   TAO_LMVM *lmP;
286:   const char *morethuente_type = TAOLINESEARCH_MT;

290:   tao->ops->setup = TaoSetUp_LMVM;
291:   tao->ops->solve = TaoSolve_LMVM;
292:   tao->ops->view = TaoView_LMVM;
293:   tao->ops->setfromoptions = TaoSetFromOptions_LMVM;
294:   tao->ops->destroy = TaoDestroy_LMVM;

296:   PetscNewLog(tao,TAO_LMVM, &lmP); 
297:   lmP->D = 0;
298:   lmP->M = 0;
299:   lmP->Xold = 0;
300:   lmP->Gold = 0;

302:   tao->data = (void*)lmP;
303:   tao->max_it = 2000;
304:   tao->max_funcs = 4000;
305:   tao->fatol = 1e-4;
306:   tao->frtol = 1e-4;

308:   TaoLineSearchCreate(((PetscObject)tao)->comm,&tao->linesearch); 
309:   TaoLineSearchSetType(tao->linesearch,morethuente_type); 
310:   TaoLineSearchUseTaoSolverRoutines(tao->linesearch,tao); 

312:   return(0);
313: }