1 #include <petsctaolinesearch.h> 2 #include <../src/tao/matrix/lmvmmat.h> 3 #include <../src/tao/unconstrained/impls/lmvm/lmvm.h> 4 5 #define LMVM_BFGS 0 6 #define LMVM_SCALED_GRADIENT 1 7 #define LMVM_GRADIENT 2 8 9 static PetscErrorCode TaoSolve_LMVM(Tao tao) 10 { 11 TAO_LMVM *lmP = (TAO_LMVM *)tao->data; 12 PetscReal f, fold, gdx, gnorm; 13 PetscReal step = 1.0; 14 PetscReal delta; 15 PetscErrorCode ierr; 16 PetscInt stepType, nupdates; 17 PetscBool recycle; 18 TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING; 19 20 PetscFunctionBegin; 21 22 if (tao->XL || tao->XU || tao->ops->computebounds) { 23 ierr = PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by lmvm algorithm\n");CHKERRQ(ierr); 24 } 25 26 /* Check convergence criteria */ 27 ierr = TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient);CHKERRQ(ierr); 28 ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); 29 30 if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); 31 32 tao->reason = TAO_CONTINUE_ITERATING; 33 ierr = TaoLogConvergenceHistory(tao,f,gnorm,0.0,tao->ksp_its);CHKERRQ(ierr); 34 ierr = TaoMonitor(tao,tao->niter,f,gnorm,0.0,step);CHKERRQ(ierr); 35 ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr); 36 if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); 37 38 /* Set initial scaling for the function */ 39 if (f != 0.0) { 40 delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); 41 } else { 42 delta = 2.0 / (gnorm*gnorm); 43 } 44 ierr = MatLMVMSetDelta(lmP->M,delta);CHKERRQ(ierr); 45 46 /* Set counter for gradient/reset steps */ 47 ierr = MatLMVMGetRecycleFlag(lmP->M, &recycle);CHKERRQ(ierr); 48 if (!recycle) { 49 lmP->bfgs = 0; 50 lmP->sgrad = 0; 51 lmP->grad = 0; 52 ierr = MatLMVMReset(lmP->M); CHKERRQ(ierr); 53 } 54 55 /* Have not converged; continue with Newton method */ 56 while (tao->reason == TAO_CONTINUE_ITERATING) { 57 /* Compute direction */ 58 ierr = MatLMVMUpdate(lmP->M,tao->solution,tao->gradient);CHKERRQ(ierr); 59 ierr = MatLMVMSolve(lmP->M, tao->gradient, lmP->D);CHKERRQ(ierr); 60 61 /* Check for success (descent direction) */ 62 ierr = VecDot(lmP->D, tao->gradient, &gdx);CHKERRQ(ierr); 63 if ((gdx <= 0.0) || PetscIsInfOrNanReal(gdx)) { 64 /* Step is not descent or direction produced not a number 65 We can assert bfgsUpdates > 1 in this case because 66 the first solve produces the scaled gradient direction, 67 which is guaranteed to be descent 68 69 Use steepest descent direction (scaled) 70 */ 71 72 if (f != 0.0) { 73 delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); 74 } else { 75 delta = 2.0 / (gnorm*gnorm); 76 } 77 ierr = MatLMVMSetDelta(lmP->M, delta);CHKERRQ(ierr); 78 ierr = MatLMVMReset(lmP->M);CHKERRQ(ierr); 79 ierr = MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); 80 ierr = MatLMVMSolve(lmP->M,tao->gradient, lmP->D);CHKERRQ(ierr); 81 82 /* On a reset, the direction cannot be not a number; it is a 83 scaled gradient step. No need to check for this condition. */ 84 ++lmP->sgrad; 85 stepType = LMVM_SCALED_GRADIENT; 86 } else { 87 ierr = MatLMVMGetUpdates(lmP->M, &nupdates); CHKERRQ(ierr); 88 if (1 == nupdates) { 89 /* The first BFGS direction is always the scaled gradient */ 90 ++lmP->sgrad; 91 stepType = LMVM_SCALED_GRADIENT; 92 } else { 93 ++lmP->bfgs; 94 stepType = LMVM_BFGS; 95 } 96 } 97 ierr = VecScale(lmP->D, -1.0);CHKERRQ(ierr); 98 99 /* Perform the linesearch */ 100 fold = f; 101 ierr = VecCopy(tao->solution, lmP->Xold);CHKERRQ(ierr); 102 ierr = VecCopy(tao->gradient, lmP->Gold);CHKERRQ(ierr); 103 104 ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, lmP->D, &step,&ls_status);CHKERRQ(ierr); 105 ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); 106 107 while (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER && (stepType != LMVM_GRADIENT)) { 108 /* Linesearch failed */ 109 ierr = PetscInfo(lmP, "WARNING: Linesearch failed!\n"); CHKERRQ(ierr); 110 /* Reset factors and use scaled gradient step */ 111 f = fold; 112 ierr = VecCopy(lmP->Xold, tao->solution);CHKERRQ(ierr); 113 ierr = VecCopy(lmP->Gold, tao->gradient);CHKERRQ(ierr); 114 115 switch(stepType) { 116 case LMVM_BFGS: 117 /* Failed to obtain acceptable iterate with BFGS step */ 118 /* Attempt to use the scaled gradient direction */ 119 120 if (f != 0.0) { 121 delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); 122 } else { 123 delta = 2.0 / (gnorm*gnorm); 124 } 125 ierr = MatLMVMSetDelta(lmP->M, delta);CHKERRQ(ierr); 126 ierr = MatLMVMReset(lmP->M);CHKERRQ(ierr); 127 ierr = MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); 128 ierr = MatLMVMSolve(lmP->M, tao->gradient, lmP->D);CHKERRQ(ierr); 129 130 /* On a reset, the direction cannot be not a number; it is a 131 scaled gradient step. No need to check for this condition. */ 132 --lmP->bfgs; 133 ++lmP->sgrad; 134 stepType = LMVM_SCALED_GRADIENT; 135 break; 136 137 case LMVM_SCALED_GRADIENT: 138 /* The scaled gradient step did not produce a new iterate; 139 attempt to use the gradient direction. 140 Need to make sure we are not using a different diagonal scaling */ 141 ierr = MatLMVMSetDelta(lmP->M, 1.0);CHKERRQ(ierr); 142 ierr = MatLMVMReset(lmP->M);CHKERRQ(ierr); 143 ierr = MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); 144 ierr = MatLMVMSolve(lmP->M, tao->gradient, lmP->D);CHKERRQ(ierr); 145 146 --lmP->sgrad; 147 ++lmP->grad; 148 stepType = LMVM_GRADIENT; 149 break; 150 } 151 ierr = VecScale(lmP->D, -1.0);CHKERRQ(ierr); 152 153 /* Perform the linesearch */ 154 ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, lmP->D, &step, &ls_status);CHKERRQ(ierr); 155 ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); 156 } 157 158 if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) { 159 /* Failed to find an improving point */ 160 f = fold; 161 ierr = VecCopy(lmP->Xold, tao->solution);CHKERRQ(ierr); 162 ierr = VecCopy(lmP->Gold, tao->gradient);CHKERRQ(ierr); 163 step = 0.0; 164 tao->reason = TAO_DIVERGED_LS_FAILURE; 165 } 166 167 /* Check for termination */ 168 ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); 169 170 tao->niter++; 171 ierr = TaoLogConvergenceHistory(tao,f,gnorm,0.0,tao->ksp_its);CHKERRQ(ierr); 172 ierr = TaoMonitor(tao,tao->niter,f,gnorm,0.0,step);CHKERRQ(ierr); 173 ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr); 174 } 175 PetscFunctionReturn(0); 176 } 177 178 static PetscErrorCode TaoSetUp_LMVM(Tao tao) 179 { 180 TAO_LMVM *lmP = (TAO_LMVM *)tao->data; 181 PetscInt n,N; 182 PetscErrorCode ierr; 183 KSP H0ksp; 184 185 PetscFunctionBegin; 186 /* Existence of tao->solution checked in TaoSetUp() */ 187 if (!tao->gradient) {ierr = VecDuplicate(tao->solution,&tao->gradient);CHKERRQ(ierr); } 188 if (!tao->stepdirection) {ierr = VecDuplicate(tao->solution,&tao->stepdirection);CHKERRQ(ierr); } 189 if (!lmP->D) {ierr = VecDuplicate(tao->solution,&lmP->D);CHKERRQ(ierr); } 190 if (!lmP->Xold) {ierr = VecDuplicate(tao->solution,&lmP->Xold);CHKERRQ(ierr); } 191 if (!lmP->Gold) {ierr = VecDuplicate(tao->solution,&lmP->Gold);CHKERRQ(ierr); } 192 193 /* Create matrix for the limited memory approximation */ 194 ierr = VecGetLocalSize(tao->solution,&n);CHKERRQ(ierr); 195 ierr = VecGetSize(tao->solution,&N);CHKERRQ(ierr); 196 ierr = MatCreateLMVM(((PetscObject)tao)->comm,n,N,&lmP->M);CHKERRQ(ierr); 197 ierr = MatLMVMAllocateVectors(lmP->M,tao->solution);CHKERRQ(ierr); 198 199 /* If the user has set a matrix to solve as the initial H0, set the options prefix here, and set up the KSP */ 200 if (lmP->H0) { 201 const char *prefix; 202 PC H0pc; 203 204 ierr = MatLMVMSetH0(lmP->M, lmP->H0);CHKERRQ(ierr); 205 ierr = MatLMVMGetH0KSP(lmP->M, &H0ksp);CHKERRQ(ierr); 206 207 ierr = TaoGetOptionsPrefix(tao, &prefix);CHKERRQ(ierr); 208 ierr = KSPSetOptionsPrefix(H0ksp, prefix);CHKERRQ(ierr); 209 ierr = PetscObjectAppendOptionsPrefix((PetscObject)H0ksp, "tao_h0_");CHKERRQ(ierr); 210 ierr = KSPGetPC(H0ksp, &H0pc);CHKERRQ(ierr); 211 ierr = PetscObjectAppendOptionsPrefix((PetscObject)H0pc, "tao_h0_");CHKERRQ(ierr); 212 213 ierr = KSPSetFromOptions(H0ksp);CHKERRQ(ierr); 214 ierr = KSPSetUp(H0ksp);CHKERRQ(ierr); 215 } 216 217 PetscFunctionReturn(0); 218 } 219 220 /* ---------------------------------------------------------- */ 221 static PetscErrorCode TaoDestroy_LMVM(Tao tao) 222 { 223 TAO_LMVM *lmP = (TAO_LMVM *)tao->data; 224 PetscErrorCode ierr; 225 PetscBool recycle; 226 227 PetscFunctionBegin; 228 if (lmP->M) { 229 ierr = MatLMVMGetRecycleFlag(lmP->M, &recycle); CHKERRQ(ierr); 230 if (recycle) { 231 ierr = PetscInfo(lmP->M, "WARNING: TaoDestroy() called when LMVM recycling is enabled!\n"); CHKERRQ(ierr); 232 } 233 } 234 235 if (tao->setupcalled) { 236 ierr = VecDestroy(&lmP->Xold);CHKERRQ(ierr); 237 ierr = VecDestroy(&lmP->Gold);CHKERRQ(ierr); 238 ierr = VecDestroy(&lmP->D);CHKERRQ(ierr); 239 ierr = MatDestroy(&lmP->M);CHKERRQ(ierr); 240 } 241 242 if (lmP->H0) { 243 ierr = PetscObjectDereference((PetscObject)lmP->H0);CHKERRQ(ierr); 244 } 245 246 ierr = PetscFree(tao->data);CHKERRQ(ierr); 247 248 PetscFunctionReturn(0); 249 } 250 251 /*------------------------------------------------------------*/ 252 static PetscErrorCode TaoSetFromOptions_LMVM(PetscOptionItems *PetscOptionsObject,Tao tao) 253 { 254 PetscErrorCode ierr; 255 256 PetscFunctionBegin; 257 ierr = PetscOptionsHead(PetscOptionsObject,"Limited-memory variable-metric method for unconstrained optimization");CHKERRQ(ierr); 258 ierr = TaoLineSearchSetFromOptions(tao->linesearch);CHKERRQ(ierr); 259 ierr = PetscOptionsTail();CHKERRQ(ierr); 260 PetscFunctionReturn(0); 261 } 262 263 /*------------------------------------------------------------*/ 264 static PetscErrorCode TaoView_LMVM(Tao tao, PetscViewer viewer) 265 { 266 TAO_LMVM *lm = (TAO_LMVM *)tao->data; 267 PetscBool isascii, recycle; 268 PetscInt recycled_its; 269 PetscErrorCode ierr; 270 271 PetscFunctionBegin; 272 ierr = PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr); 273 if (isascii) { 274 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 275 ierr = PetscViewerASCIIPrintf(viewer, "BFGS steps: %D\n", lm->bfgs);CHKERRQ(ierr); 276 ierr = PetscViewerASCIIPrintf(viewer, "Scaled gradient steps: %D\n", lm->sgrad);CHKERRQ(ierr); 277 ierr = PetscViewerASCIIPrintf(viewer, "Gradient steps: %D\n", lm->grad);CHKERRQ(ierr); 278 ierr = MatLMVMGetRecycleFlag(lm->M, &recycle);CHKERRQ(ierr); 279 if (recycle) { 280 ierr = PetscViewerASCIIPrintf(viewer, "Recycle: on\n");CHKERRQ(ierr); 281 recycled_its = lm->bfgs + lm->sgrad + lm->grad; 282 ierr = PetscViewerASCIIPrintf(viewer, "Total recycled iterations: %D\n", recycled_its);CHKERRQ(ierr); 283 } 284 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 285 } 286 PetscFunctionReturn(0); 287 } 288 289 /* ---------------------------------------------------------- */ 290 291 /*MC 292 TAOLMVM - Limited Memory Variable Metric method is a quasi-Newton 293 optimization solver for unconstrained minimization. It solves 294 the Newton step 295 Hkdk = - gk 296 297 using an approximation Bk in place of Hk, where Bk is composed using 298 the BFGS update formula. A More-Thuente line search is then used 299 to computed the steplength in the dk direction 300 Options Database Keys: 301 + -tao_lmm_vectors - number of vectors to use for approximation 302 . -tao_lmm_scale_type - "none","scalar","broyden" 303 . -tao_lmm_limit_type - "none","average","relative","absolute" 304 . -tao_lmm_rescale_type - "none","scalar","gl" 305 . -tao_lmm_limit_mu - mu limiting factor 306 . -tao_lmm_limit_nu - nu limiting factor 307 . -tao_lmm_delta_min - minimum delta value 308 . -tao_lmm_delta_max - maximum delta value 309 . -tao_lmm_broyden_phi - phi factor for Broyden scaling 310 . -tao_lmm_scalar_alpha - alpha factor for scalar scaling 311 . -tao_lmm_rescale_alpha - alpha factor for rescaling diagonal 312 . -tao_lmm_rescale_beta - beta factor for rescaling diagonal 313 . -tao_lmm_scalar_history - amount of history for scalar scaling 314 . -tao_lmm_rescale_history - amount of history for rescaling diagonal 315 . -tao_lmm_eps - rejection tolerance 316 - -tao_lmm_recycle - enable recycling LMVM updates between TaoSolve() calls 317 318 Level: beginner 319 M*/ 320 321 PETSC_EXTERN PetscErrorCode TaoCreate_LMVM(Tao tao) 322 { 323 TAO_LMVM *lmP; 324 const char *morethuente_type = TAOLINESEARCHMT; 325 PetscErrorCode ierr; 326 327 PetscFunctionBegin; 328 tao->ops->setup = TaoSetUp_LMVM; 329 tao->ops->solve = TaoSolve_LMVM; 330 tao->ops->view = TaoView_LMVM; 331 tao->ops->setfromoptions = TaoSetFromOptions_LMVM; 332 tao->ops->destroy = TaoDestroy_LMVM; 333 334 ierr = PetscNewLog(tao,&lmP);CHKERRQ(ierr); 335 lmP->D = 0; 336 lmP->M = 0; 337 lmP->Xold = 0; 338 lmP->Gold = 0; 339 lmP->H0 = NULL; 340 341 tao->data = (void*)lmP; 342 /* Override default settings (unless already changed) */ 343 if (!tao->max_it_changed) tao->max_it = 2000; 344 if (!tao->max_funcs_changed) tao->max_funcs = 4000; 345 346 ierr = TaoLineSearchCreate(((PetscObject)tao)->comm,&tao->linesearch);CHKERRQ(ierr); 347 ierr = PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1);CHKERRQ(ierr); 348 ierr = TaoLineSearchSetType(tao->linesearch,morethuente_type);CHKERRQ(ierr); 349 ierr = TaoLineSearchUseTaoRoutines(tao->linesearch,tao);CHKERRQ(ierr); 350 ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);CHKERRQ(ierr); 351 PetscFunctionReturn(0); 352 } 353