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