1 #include <petsctaolinesearch.h> 2 #include <../src/tao/bound/impls/bnk/bnk.h> 3 4 #include <petscksp.h> 5 6 /* Routine for BFGS preconditioner */ 7 8 PetscErrorCode MatLMVMSolveShell(PC pc, Vec b, Vec x) 9 { 10 PetscErrorCode ierr; 11 Mat M; 12 13 PetscFunctionBegin; 14 PetscValidHeaderSpecific(pc,PC_CLASSID,1); 15 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 16 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 17 ierr = PCShellGetContext(pc,(void**)&M);CHKERRQ(ierr); 18 ierr = MatLMVMSolve(M, b, x);CHKERRQ(ierr); 19 PetscFunctionReturn(0); 20 } 21 22 /*------------------------------------------------------------*/ 23 24 /* Routine for initializing the KSP solver, the BFGS preconditioner, and the initial trust radius estimation */ 25 26 PetscErrorCode TaoBNKInitialize(Tao tao, PetscInt initType, PetscBool *needH) 27 { 28 PetscErrorCode ierr; 29 TAO_BNK *bnk = (TAO_BNK *)tao->data; 30 PC pc; 31 32 PetscReal f_min, ftrial, prered, actred, kappa, sigma; 33 PetscReal tau, tau_1, tau_2, tau_max, tau_min, max_radius; 34 PetscReal delta; 35 36 PetscInt n,N; 37 38 PetscInt i_max = 5; 39 PetscInt j_max = 1; 40 PetscInt i, j; 41 42 PetscFunctionBegin; 43 /* Project the current point onto the feasible set */ 44 ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); 45 ierr = TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);CHKERRQ(ierr); 46 47 /* Project the initial point onto the feasible region */ 48 ierr = VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);CHKERRQ(ierr); 49 50 /* Check convergence criteria */ 51 ierr = TaoComputeObjectiveAndGradient(tao, tao->solution, &bnk->f, bnk->unprojected_gradient);CHKERRQ(ierr); 52 ierr = VecBoundGradientProjection(bnk->unprojected_gradient,tao->solution,tao->XL,tao->XU,tao->gradient);CHKERRQ(ierr); 53 ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&bnk->gnorm);CHKERRQ(ierr); 54 if (PetscIsInfOrNanReal(bnk->f) || PetscIsInfOrNanReal(bnk->gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); 55 56 /* Test the initial point for convergence */ 57 ierr = TaoLogConvergenceHistory(tao,bnk->f,bnk->gnorm,0.0,tao->ksp_its);CHKERRQ(ierr); 58 ierr = TaoMonitor(tao,tao->niter,bnk->f,bnk->gnorm,0.0,1.0);CHKERRQ(ierr); 59 ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr); 60 if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); 61 62 /* Prep the embedded BNCG algorithm */ 63 if (bnk->max_cg_its > 0) { 64 ierr = VecCopy(tao->solution, bnk->bncg_sol);CHKERRQ(ierr); 65 ierr = TaoCreate(PetscObjectComm((PetscObject)tao), &bnk->bncg);CHKERRQ(ierr); 66 ierr = PetscObjectIncrementTabLevel((PetscObject)bnk->bncg, (PetscObject)tao, 1);CHKERRQ(ierr); 67 ierr = TaoSetType(bnk->bncg, TAOBNCG);CHKERRQ(ierr); 68 69 ierr = TaoSetInitialVector(bnk->bncg, bnk->bncg_sol);CHKERRQ(ierr); 70 ierr = TaoSetObjectiveRoutine(bnk->bncg, tao->ops->computeobjective, tao->user_objP);CHKERRQ(ierr); 71 ierr = TaoSetGradientRoutine(bnk->bncg, tao->ops->computegradient, tao->user_gradP);CHKERRQ(ierr); 72 ierr = TaoSetObjectiveAndGradientRoutine(bnk->bncg, tao->ops->computeobjectiveandgradient, tao->user_objgradP);CHKERRQ(ierr); 73 74 ierr = TaoSetOptionsPrefix(bnk->bncg, "tao_bnk_bncg_");CHKERRQ(ierr); 75 ierr = TaoSetVariableBounds(bnk->bncg, tao->XL, tao->XU);CHKERRQ(ierr); 76 ierr = TaoSetMaximumIterations(bnk->bncg, bnk->max_cg_its);CHKERRQ(ierr); 77 ierr = TaoSetTolerances(bnk->bncg, tao->gatol, tao->grtol, tao->gttol);CHKERRQ(ierr); 78 ierr = TaoSetFunctionLowerBound(bnk->bncg, tao->fmin);CHKERRQ(ierr); 79 for (i=0; i<tao->numbermonitors; i++) { 80 ierr = TaoSetMonitor(bnk->bncg, tao->monitor[i], tao->monitorcontext[i], tao->monitordestroy[i]);CHKERRQ(ierr); 81 ierr = PetscObjectReference((PetscObject)(tao->monitorcontext[i]));CHKERRQ(ierr); 82 } 83 ierr = TaoSetFromOptions(bnk->bncg); 84 bnk->bncg_ctx = (TAO_BNCG *)bnk->bncg->data; 85 } 86 87 /* Number of times ksp stopped because of these reasons */ 88 bnk->ksp_atol = 0; 89 bnk->ksp_rtol = 0; 90 bnk->ksp_dtol = 0; 91 bnk->ksp_ctol = 0; 92 bnk->ksp_negc = 0; 93 bnk->ksp_iter = 0; 94 bnk->ksp_othr = 0; 95 96 /* Initialize the Hessian perturbation */ 97 bnk->pert = bnk->sval; 98 99 /* Get vectors we will need */ 100 if (BNK_PC_BFGS == bnk->pc_type && !bnk->M) { 101 ierr = VecGetLocalSize(tao->solution,&n);CHKERRQ(ierr); 102 ierr = VecGetSize(tao->solution,&N);CHKERRQ(ierr); 103 ierr = MatCreateLMVM(((PetscObject)tao)->comm,n,N,&bnk->M);CHKERRQ(ierr); 104 ierr = MatLMVMAllocateVectors(bnk->M,tao->solution);CHKERRQ(ierr); 105 } 106 107 /* create vectors for the limited memory preconditioner */ 108 if ((BNK_PC_BFGS == bnk->pc_type) && (BFGS_SCALE_BFGS != bnk->bfgs_scale_type)) { 109 if (!bnk->Diag) {ierr = VecDuplicate(tao->solution,&bnk->Diag);CHKERRQ(ierr);} 110 } 111 ierr = VecSet(bnk->Diag_min, bnk->dmin);CHKERRQ(ierr); 112 ierr = VecSet(bnk->Diag_max, bnk->dmax);CHKERRQ(ierr); 113 114 /* Modify the preconditioner to use the bfgs approximation */ 115 ierr = KSPGetPC(tao->ksp, &pc);CHKERRQ(ierr); 116 switch(bnk->pc_type) { 117 case BNK_PC_NONE: 118 ierr = PCSetType(pc, PCNONE);CHKERRQ(ierr); 119 ierr = PCSetFromOptions(pc);CHKERRQ(ierr); 120 break; 121 122 case BNK_PC_AHESS: 123 ierr = PCSetType(pc, PCJACOBI);CHKERRQ(ierr); 124 ierr = PCSetFromOptions(pc);CHKERRQ(ierr); 125 ierr = PCJacobiSetUseAbs(pc,PETSC_TRUE);CHKERRQ(ierr); 126 break; 127 128 case BNK_PC_BFGS: 129 ierr = PCSetType(pc, PCSHELL);CHKERRQ(ierr); 130 ierr = PCSetFromOptions(pc);CHKERRQ(ierr); 131 ierr = PCShellSetName(pc, "bfgs");CHKERRQ(ierr); 132 ierr = PCShellSetContext(pc, bnk->M);CHKERRQ(ierr); 133 ierr = PCShellSetApply(pc, MatLMVMSolveShell);CHKERRQ(ierr); 134 break; 135 136 default: 137 /* Use the pc method set by pc_type */ 138 break; 139 } 140 141 /* Initialize trust-region radius. The initialization is only performed 142 when we are using Nash, Steihaug-Toint or the Generalized Lanczos method. */ 143 *needH = PETSC_TRUE; 144 if (bnk->is_nash || bnk->is_stcg || bnk->is_gltr) { 145 switch(initType) { 146 case BNK_INIT_CONSTANT: 147 /* Use the initial radius specified */ 148 tao->trust = tao->trust0; 149 break; 150 151 case BNK_INIT_INTERPOLATION: 152 /* Use interpolation based on the initial Hessian */ 153 max_radius = 0.0; 154 for (j = 0; j < j_max; ++j) { 155 f_min = bnk->f; 156 sigma = 0.0; 157 158 if (*needH) { 159 /* Compute the Hessian at the new step, and extract the inactive subsystem */ 160 ierr = TaoComputeHessian(tao, tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); 161 ierr = MatDestroy(&bnk->H_inactive); 162 if (bnk->inactive_idx) { 163 ierr = ISDestroy(&bnk->inactive_idx);CHKERRQ(ierr); 164 ierr = VecWhichInactive(tao->XL,tao->solution,bnk->unprojected_gradient,tao->XU,PETSC_TRUE,&bnk->inactive_idx);CHKERRQ(ierr); 165 ierr = MatCreateSubMatrix(tao->hessian, bnk->inactive_idx, bnk->inactive_idx, MAT_INITIAL_MATRIX, &bnk->H_inactive);CHKERRQ(ierr); 166 } else { 167 ierr = MatDuplicate(tao->hessian, MAT_COPY_VALUES, &bnk->H_inactive); 168 } 169 *needH = PETSC_FALSE; 170 } 171 172 for (i = 0; i < i_max; ++i) { 173 /* Take a steepest descent step and snap it to bounds */ 174 ierr = VecCopy(tao->solution, bnk->Xold);CHKERRQ(ierr); 175 ierr = VecAXPY(tao->solution, -tao->trust/bnk->gnorm, tao->gradient);CHKERRQ(ierr); 176 ierr = VecMedian(tao->XL, tao->solution, tao->XU, tao->solution);CHKERRQ(ierr); 177 /* Recompute the step after bound snapping so that it can be used in predicted decrease calculation later */ 178 ierr = VecCopy(tao->solution, bnk->W);CHKERRQ(ierr); 179 ierr = VecAXPY(bnk->W, -1.0, bnk->Xold);CHKERRQ(ierr); 180 /* Compute the objective at the trial */ 181 ierr = TaoComputeObjective(tao, tao->solution, &ftrial);CHKERRQ(ierr); 182 ierr = VecCopy(bnk->Xold, tao->solution);CHKERRQ(ierr); 183 if (PetscIsInfOrNanReal(ftrial)) { 184 tau = bnk->gamma1_i; 185 } else { 186 if (ftrial < f_min) { 187 f_min = ftrial; 188 sigma = -tao->trust / bnk->gnorm; 189 } 190 /* Compute the predicted and actual reduction */ 191 if (bnk->inactive_idx) { 192 ierr = VecGetSubVector(bnk->unprojected_gradient, bnk->inactive_idx, &bnk->G_inactive);CHKERRQ(ierr); 193 ierr = VecGetSubVector(bnk->W, bnk->inactive_idx, &bnk->inactive_work);CHKERRQ(ierr); 194 } else { 195 bnk->G_inactive = bnk->unprojected_gradient; 196 bnk->inactive_work = bnk->W; 197 } 198 ierr = MatMult(bnk->H_inactive, bnk->G_inactive, bnk->inactive_work);CHKERRQ(ierr); 199 ierr = VecDot(bnk->G_inactive, bnk->inactive_work, &prered);CHKERRQ(ierr); 200 if (bnk->inactive_idx) { 201 ierr = VecRestoreSubVector(bnk->unprojected_gradient, bnk->inactive_idx, &bnk->G_inactive);CHKERRQ(ierr); 202 ierr = VecRestoreSubVector(bnk->W, bnk->inactive_idx, &bnk->inactive_work);CHKERRQ(ierr); 203 } 204 prered = tao->trust * (bnk->gnorm - 0.5 * tao->trust * prered / (bnk->gnorm * bnk->gnorm)); 205 actred = bnk->f - ftrial; 206 if ((PetscAbsScalar(actred) <= bnk->epsilon) && (PetscAbsScalar(prered) <= bnk->epsilon)) { 207 kappa = 1.0; 208 } else { 209 kappa = actred / prered; 210 } 211 212 tau_1 = bnk->theta_i * bnk->gnorm * tao->trust / (bnk->theta_i * bnk->gnorm * tao->trust + (1.0 - bnk->theta_i) * prered - actred); 213 tau_2 = bnk->theta_i * bnk->gnorm * tao->trust / (bnk->theta_i * bnk->gnorm * tao->trust - (1.0 + bnk->theta_i) * prered + actred); 214 tau_min = PetscMin(tau_1, tau_2); 215 tau_max = PetscMax(tau_1, tau_2); 216 217 if (PetscAbsScalar(kappa - 1.0) <= bnk->mu1_i) { 218 /* Great agreement */ 219 max_radius = PetscMax(max_radius, tao->trust); 220 221 if (tau_max < 1.0) { 222 tau = bnk->gamma3_i; 223 } else if (tau_max > bnk->gamma4_i) { 224 tau = bnk->gamma4_i; 225 } else if (tau_1 >= 1.0 && tau_1 <= bnk->gamma4_i && tau_2 < 1.0) { 226 tau = tau_1; 227 } else if (tau_2 >= 1.0 && tau_2 <= bnk->gamma4_i && tau_1 < 1.0) { 228 tau = tau_2; 229 } else { 230 tau = tau_max; 231 } 232 } else if (PetscAbsScalar(kappa - 1.0) <= bnk->mu2_i) { 233 /* Good agreement */ 234 max_radius = PetscMax(max_radius, tao->trust); 235 236 if (tau_max < bnk->gamma2_i) { 237 tau = bnk->gamma2_i; 238 } else if (tau_max > bnk->gamma3_i) { 239 tau = bnk->gamma3_i; 240 } else { 241 tau = tau_max; 242 } 243 } else { 244 /* Not good agreement */ 245 if (tau_min > 1.0) { 246 tau = bnk->gamma2_i; 247 } else if (tau_max < bnk->gamma1_i) { 248 tau = bnk->gamma1_i; 249 } else if ((tau_min < bnk->gamma1_i) && (tau_max >= 1.0)) { 250 tau = bnk->gamma1_i; 251 } else if ((tau_1 >= bnk->gamma1_i) && (tau_1 < 1.0) && ((tau_2 < bnk->gamma1_i) || (tau_2 >= 1.0))) { 252 tau = tau_1; 253 } else if ((tau_2 >= bnk->gamma1_i) && (tau_2 < 1.0) && ((tau_1 < bnk->gamma1_i) || (tau_2 >= 1.0))) { 254 tau = tau_2; 255 } else { 256 tau = tau_max; 257 } 258 } 259 } 260 tao->trust = tau * tao->trust; 261 262 /* Ensure that the trust radius is within the limits */ 263 tao->trust = PetscMax(tao->trust, bnk->min_radius); 264 tao->trust = PetscMin(tao->trust, bnk->max_radius); 265 } 266 267 if (f_min < bnk->f) { 268 bnk->f = f_min; 269 ierr = VecAXPY(tao->solution,sigma,tao->gradient);CHKERRQ(ierr); 270 ierr = VecMedian(tao->XL, tao->solution, tao->XU, tao->solution);CHKERRQ(ierr); 271 ierr = TaoComputeGradient(tao,tao->solution,bnk->unprojected_gradient);CHKERRQ(ierr); 272 ierr = VecBoundGradientProjection(bnk->unprojected_gradient,tao->solution,tao->XL,tao->XU,tao->gradient);CHKERRQ(ierr); 273 274 ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&bnk->gnorm);CHKERRQ(ierr); 275 if (PetscIsInfOrNanReal(bnk->gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute gradient generated Inf or NaN"); 276 *needH = PETSC_TRUE; 277 278 ierr = TaoLogConvergenceHistory(tao,bnk->f,bnk->gnorm,0.0,tao->ksp_its);CHKERRQ(ierr); 279 ierr = TaoMonitor(tao,tao->niter,bnk->f,bnk->gnorm,0.0,1.0);CHKERRQ(ierr); 280 ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr); 281 if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); 282 } 283 } 284 tao->trust = PetscMax(tao->trust, max_radius); 285 break; 286 287 default: 288 /* Norm of the first direction will initialize radius */ 289 tao->trust = 0.0; 290 break; 291 } 292 } 293 /* Set initial scaling for the BFGS preconditioner 294 This step is done after computing the initial trust-region radius 295 since the function value may have decreased */ 296 if (BNK_PC_BFGS == bnk->pc_type) { 297 delta = 2.0 * PetscMax(1.0, PetscAbsScalar(bnk->f)) / (bnk->gnorm*bnk->gnorm); 298 ierr = MatLMVMSetDelta(bnk->M,delta);CHKERRQ(ierr); 299 } 300 301 /* Set counter for gradient/reset steps*/ 302 bnk->newt = 0; 303 bnk->bfgs = 0; 304 bnk->sgrad = 0; 305 bnk->grad = 0; 306 PetscFunctionReturn(0); 307 } 308 309 /*------------------------------------------------------------*/ 310 311 /* Routine for computing the Hessian and preparing the preconditioner at the new iterate */ 312 313 PetscErrorCode TaoBNKComputeHessian(Tao tao) 314 { 315 PetscErrorCode ierr; 316 TAO_BNK *bnk = (TAO_BNK *)tao->data; 317 318 PetscFunctionBegin; 319 /* Compute the Hessian */ 320 ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); 321 /* Add a correction to the BFGS preconditioner */ 322 if (BNK_PC_BFGS == bnk->pc_type) { 323 ierr = MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient);CHKERRQ(ierr); 324 /* Update the BFGS diagonal scaling */ 325 if (BFGS_SCALE_AHESS == bnk->bfgs_scale_type) { 326 ierr = MatGetDiagonal(tao->hessian, bnk->Diag);CHKERRQ(ierr); 327 ierr = VecAbs(bnk->Diag);CHKERRQ(ierr); 328 ierr = VecMedian(bnk->Diag_min, bnk->Diag, bnk->Diag_max, bnk->Diag);CHKERRQ(ierr); 329 ierr = VecReciprocal(bnk->Diag);CHKERRQ(ierr); 330 ierr = MatLMVMSetScale(bnk->M,bnk->Diag);CHKERRQ(ierr); 331 } 332 } 333 PetscFunctionReturn(0); 334 } 335 336 /*------------------------------------------------------------*/ 337 338 /* Routine for estimating the active set */ 339 340 PetscErrorCode TaoBNKEstimateActiveSet(Tao tao) 341 { 342 PetscErrorCode ierr; 343 TAO_BNK *bnk = (TAO_BNK *)tao->data; 344 345 PetscFunctionBegin; 346 switch (bnk->as_type) { 347 case BNK_AS_NONE: 348 ierr = ISDestroy(&bnk->inactive_idx);CHKERRQ(ierr); 349 ierr = VecWhichInactive(tao->XL, tao->solution, bnk->unprojected_gradient, tao->XU, PETSC_TRUE, &bnk->inactive_idx);CHKERRQ(ierr); 350 ierr = ISDestroy(&bnk->active_idx);CHKERRQ(ierr); 351 ierr = ISComplementVec(bnk->inactive_idx, tao->solution, &bnk->active_idx);CHKERRQ(ierr); 352 break; 353 354 case BNK_AS_BERTSEKAS: 355 /* Compute the trial step vector with which we will estimate the active set at the next iteration */ 356 if (BNK_PC_BFGS == bnk->pc_type) { 357 /* If the BFGS preconditioner matrix is available, we will construct a trial step with it */ 358 ierr = MatLMVMSetInactive(bnk->M, NULL);CHKERRQ(ierr); 359 ierr = MatLMVMSolve(bnk->M, bnk->unprojected_gradient, bnk->W);CHKERRQ(ierr); 360 } else { 361 /* BFGS preconditioner doesn't exist so let's invert the absolute diagonal of the Hessian instead onto the gradient */ 362 ierr = MatGetDiagonal(tao->hessian, bnk->Xwork);CHKERRQ(ierr); 363 ierr = VecAbs(bnk->Xwork);CHKERRQ(ierr); 364 ierr = VecMedian(bnk->Diag_min, bnk->Xwork, bnk->Diag_max, bnk->Xwork);CHKERRQ(ierr); 365 ierr = VecReciprocal(bnk->Xwork);CHKERRQ(ierr);CHKERRQ(ierr); 366 ierr = VecPointwiseMult(bnk->W, bnk->Xwork, bnk->unprojected_gradient);CHKERRQ(ierr); 367 } 368 ierr = VecScale(bnk->W, -1.0);CHKERRQ(ierr); 369 ierr = TaoEstimateActiveBounds(tao->solution, tao->XL, tao->XU, bnk->unprojected_gradient, bnk->W, bnk->as_step, &bnk->as_tol, &bnk->active_lower, &bnk->active_upper, &bnk->active_fixed, &bnk->active_idx, &bnk->inactive_idx);CHKERRQ(ierr); 370 371 default: 372 break; 373 } 374 PetscFunctionReturn(0); 375 } 376 377 /*------------------------------------------------------------*/ 378 379 /* Routine for bounding the step direction */ 380 381 PetscErrorCode TaoBNKBoundStep(Tao tao, Vec step) 382 { 383 PetscErrorCode ierr; 384 TAO_BNK *bnk = (TAO_BNK *)tao->data; 385 386 PetscFunctionBegin; 387 if (bnk->active_idx) { 388 switch (bnk->as_type) { 389 case BNK_AS_NONE: 390 if (bnk->active_idx) { 391 ierr = VecGetSubVector(step, bnk->active_idx, &bnk->active_work);CHKERRQ(ierr); 392 ierr = VecSet(bnk->active_work, 0.0);CHKERRQ(ierr); 393 ierr = VecRestoreSubVector(step, bnk->active_idx, &bnk->active_work);CHKERRQ(ierr); 394 } 395 break; 396 397 case BNK_AS_BERTSEKAS: 398 ierr = TaoBoundStep(tao->solution, tao->XL, tao->XU, bnk->active_lower, bnk->active_upper, bnk->active_fixed, step);CHKERRQ(ierr); 399 break; 400 401 default: 402 break; 403 } 404 } 405 PetscFunctionReturn(0); 406 } 407 408 PetscErrorCode TaoBNKTakeCGSteps(Tao tao, PetscBool *terminate) 409 { 410 TAO_BNK *bnk = (TAO_BNK *)tao->data; 411 PetscErrorCode ierr; 412 413 PetscFunctionBegin; 414 *terminate = PETSC_FALSE; 415 if (bnk->max_cg_its > 0) { 416 /* Copy the current solution, unprojected gradient and step info into BNCG */ 417 ierr = VecCopy(tao->solution, bnk->bncg->solution);CHKERRQ(ierr); 418 if (tao->niter > 1) { 419 bnk->bncg_ctx->f = bnk->f; 420 ierr = VecCopy(bnk->unprojected_gradient, bnk->bncg_ctx->unprojected_gradient);CHKERRQ(ierr); 421 ierr = VecCopy(tao->stepdirection, bnk->bncg->stepdirection);CHKERRQ(ierr); 422 ierr = TaoBNCGSetRecycleFlag(bnk->bncg, PETSC_TRUE);CHKERRQ(ierr); 423 } 424 /* Take some small finite number of BNCG iterations */ 425 ierr = TaoSolve(bnk->bncg);CHKERRQ(ierr); 426 /* Add the number of gradient and function evaluations to the total */ 427 tao->nfuncs += bnk->bncg->nfuncs; 428 tao->nfuncgrads += bnk->bncg->nfuncgrads; 429 tao->ngrads += bnk->bncg->ngrads; 430 tao->nhess += bnk->bncg->nhess; 431 /* Extract the BNCG solution out and save it into BNK */ 432 bnk->f = bnk->bncg_ctx->f; 433 ierr = VecCopy(bnk->bncg->solution, tao->solution); 434 ierr = VecCopy(bnk->bncg_ctx->unprojected_gradient, bnk->unprojected_gradient); 435 ierr = VecCopy(bnk->bncg->gradient, tao->gradient); 436 /* Check to see if BNCG converged the problem */ 437 if (bnk->bncg->reason == TAO_CONVERGED_GATOL || bnk->bncg->reason == TAO_CONVERGED_GRTOL || bnk->bncg->reason == TAO_CONVERGED_GTTOL || bnk->bncg->reason == TAO_CONVERGED_MINF) { 438 *terminate = PETSC_TRUE; 439 } 440 } 441 PetscFunctionReturn(0); 442 } 443 444 /*------------------------------------------------------------*/ 445 446 /* Routine for computing the Newton step. */ 447 448 PetscErrorCode TaoBNKComputeStep(Tao tao, PetscBool shift, KSPConvergedReason *ksp_reason) 449 { 450 PetscErrorCode ierr; 451 TAO_BNK *bnk = (TAO_BNK *)tao->data; 452 453 PetscReal delta; 454 PetscInt bfgsUpdates = 0; 455 PetscInt kspits; 456 457 PetscFunctionBegin; 458 /* Determine the active and inactive sets */ 459 ierr = TaoBNKEstimateActiveSet(tao);CHKERRQ(ierr); 460 461 /* Prepare the reduced sub-matrices for the inactive set */ 462 if (BNK_PC_BFGS == bnk->pc_type) { ierr = MatLMVMSetInactive(bnk->M, bnk->inactive_idx);CHKERRQ(ierr); } 463 if (bnk->inactive_idx) { 464 ierr = MatDestroy(&bnk->H_inactive); 465 ierr = MatCreateSubMatrix(tao->hessian, bnk->inactive_idx, bnk->inactive_idx, MAT_INITIAL_MATRIX, &bnk->H_inactive);CHKERRQ(ierr); 466 if (tao->hessian == tao->hessian_pre) { 467 bnk->Hpre_inactive = bnk->H_inactive; 468 } else { 469 ierr = MatDestroy(&bnk->Hpre_inactive); 470 ierr = MatCreateSubMatrix(tao->hessian_pre, bnk->inactive_idx, bnk->inactive_idx, MAT_INITIAL_MATRIX, &bnk->Hpre_inactive);CHKERRQ(ierr); 471 } 472 } else { 473 ierr = MatDestroy(&bnk->H_inactive); 474 ierr = MatDuplicate(tao->hessian, MAT_COPY_VALUES, &bnk->H_inactive); 475 if (tao->hessian == tao->hessian_pre) { 476 bnk->Hpre_inactive = bnk->H_inactive; 477 } else { 478 ierr = MatDestroy(&bnk->Hpre_inactive); 479 ierr = MatDuplicate(tao->hessian_pre, MAT_COPY_VALUES, &bnk->Hpre_inactive); 480 } 481 } 482 483 /* Shift the reduced Hessian matrix */ 484 if ((shift) && (bnk->pert > 0)) { 485 ierr = MatShift(bnk->H_inactive, bnk->pert);CHKERRQ(ierr); 486 if (bnk->H_inactive != bnk->Hpre_inactive) { 487 ierr = MatShift(bnk->Hpre_inactive, bnk->pert);CHKERRQ(ierr); 488 } 489 } 490 491 /* Update the diagonal scaling for the BFGS preconditioner, this time with the Hessian perturbation */ 492 if ((BNK_PC_BFGS == bnk->pc_type) && (BFGS_SCALE_PHESS == bnk->bfgs_scale_type)) { 493 /* Obtain diagonal for the bfgs preconditioner */ 494 ierr = VecSet(bnk->Diag, 1.0);CHKERRQ(ierr); 495 if (bnk->inactive_idx) { 496 ierr = VecGetSubVector(bnk->Diag, bnk->inactive_idx, &bnk->Diag_red);CHKERRQ(ierr); 497 } else { 498 bnk->Diag_red = bnk->Diag; 499 } 500 ierr = MatGetDiagonal(bnk->H_inactive, bnk->Diag_red);CHKERRQ(ierr); 501 if (bnk->inactive_idx) { 502 ierr = VecRestoreSubVector(bnk->Diag, bnk->inactive_idx, &bnk->Diag_red);CHKERRQ(ierr); 503 } 504 ierr = VecAbs(bnk->Diag);CHKERRQ(ierr); 505 ierr = VecMedian(bnk->Diag_min, bnk->Diag, bnk->Diag_max, bnk->Diag);CHKERRQ(ierr); 506 ierr = VecReciprocal(bnk->Diag);CHKERRQ(ierr); 507 ierr = MatLMVMSetScale(bnk->M,bnk->Diag);CHKERRQ(ierr); 508 } 509 510 /* Solve the Newton system of equations */ 511 ierr = VecSet(tao->stepdirection, 0.0);CHKERRQ(ierr); 512 ierr = KSPReset(tao->ksp);CHKERRQ(ierr); 513 ierr = KSPSetOperators(tao->ksp,bnk->H_inactive,bnk->Hpre_inactive);CHKERRQ(ierr); 514 ierr = VecCopy(bnk->unprojected_gradient, bnk->Gwork);CHKERRQ(ierr); 515 if (bnk->inactive_idx) { 516 ierr = VecGetSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive);CHKERRQ(ierr); 517 ierr = VecGetSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive);CHKERRQ(ierr); 518 } else { 519 bnk->G_inactive = bnk->unprojected_gradient; 520 bnk->X_inactive = tao->stepdirection; 521 } 522 if (bnk->is_nash || bnk->is_stcg || bnk->is_gltr) { 523 ierr = KSPCGSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); 524 ierr = KSPSolve(tao->ksp, bnk->G_inactive, bnk->X_inactive);CHKERRQ(ierr); 525 ierr = KSPGetIterationNumber(tao->ksp,&kspits);CHKERRQ(ierr); 526 tao->ksp_its+=kspits; 527 tao->ksp_tot_its+=kspits; 528 ierr = KSPCGGetNormD(tao->ksp,&bnk->dnorm);CHKERRQ(ierr); 529 530 if (0.0 == tao->trust) { 531 /* Radius was uninitialized; use the norm of the direction */ 532 if (bnk->dnorm > 0.0) { 533 tao->trust = bnk->dnorm; 534 535 /* Modify the radius if it is too large or small */ 536 tao->trust = PetscMax(tao->trust, bnk->min_radius); 537 tao->trust = PetscMin(tao->trust, bnk->max_radius); 538 } else { 539 /* The direction was bad; set radius to default value and re-solve 540 the trust-region subproblem to get a direction */ 541 tao->trust = tao->trust0; 542 543 /* Modify the radius if it is too large or small */ 544 tao->trust = PetscMax(tao->trust, bnk->min_radius); 545 tao->trust = PetscMin(tao->trust, bnk->max_radius); 546 547 ierr = KSPCGSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); 548 ierr = KSPSolve(tao->ksp, bnk->G_inactive, bnk->X_inactive);CHKERRQ(ierr); 549 ierr = KSPGetIterationNumber(tao->ksp,&kspits);CHKERRQ(ierr); 550 tao->ksp_its+=kspits; 551 tao->ksp_tot_its+=kspits; 552 ierr = KSPCGGetNormD(tao->ksp,&bnk->dnorm);CHKERRQ(ierr); 553 554 if (bnk->dnorm == 0.0) SETERRQ(PETSC_COMM_SELF,1, "Initial direction zero"); 555 } 556 } 557 } else { 558 ierr = KSPSolve(tao->ksp, bnk->G_inactive, bnk->X_inactive);CHKERRQ(ierr); 559 ierr = KSPGetIterationNumber(tao->ksp, &kspits);CHKERRQ(ierr); 560 tao->ksp_its += kspits; 561 tao->ksp_tot_its+=kspits; 562 } 563 /* Restore sub vectors back */ 564 if (bnk->inactive_idx) { 565 ierr = VecRestoreSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive);CHKERRQ(ierr); 566 ierr = VecRestoreSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive);CHKERRQ(ierr); 567 } 568 /* Make sure the safeguarded fall-back step is zero for actively bounded variables */ 569 ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); 570 ierr = TaoBNKBoundStep(tao, tao->stepdirection);CHKERRQ(ierr); 571 572 /* Record convergence reasons */ 573 ierr = KSPGetConvergedReason(tao->ksp, ksp_reason);CHKERRQ(ierr); 574 if (KSP_CONVERGED_ATOL == *ksp_reason) { 575 ++bnk->ksp_atol; 576 } else if (KSP_CONVERGED_RTOL == *ksp_reason) { 577 ++bnk->ksp_rtol; 578 } else if (KSP_CONVERGED_CG_CONSTRAINED == *ksp_reason) { 579 ++bnk->ksp_ctol; 580 } else if (KSP_CONVERGED_CG_NEG_CURVE == *ksp_reason) { 581 ++bnk->ksp_negc; 582 } else if (KSP_DIVERGED_DTOL == *ksp_reason) { 583 ++bnk->ksp_dtol; 584 } else if (KSP_DIVERGED_ITS == *ksp_reason) { 585 ++bnk->ksp_iter; 586 } else { 587 ++bnk->ksp_othr; 588 } 589 590 /* Make sure the BFGS preconditioner is healthy */ 591 if (bnk->pc_type == BNK_PC_BFGS) { 592 ierr = MatLMVMGetUpdates(bnk->M, &bfgsUpdates);CHKERRQ(ierr); 593 if ((KSP_DIVERGED_INDEFINITE_PC == *ksp_reason) && (bfgsUpdates > 1)) { 594 /* Preconditioner is numerically indefinite; reset the approximation. */ 595 delta = 2.0 * PetscMax(1.0, PetscAbsScalar(bnk->f)) / (bnk->gnorm*bnk->gnorm); 596 ierr = MatLMVMSetDelta(bnk->M,delta);CHKERRQ(ierr); 597 ierr = MatLMVMReset(bnk->M);CHKERRQ(ierr); 598 ierr = MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient);CHKERRQ(ierr); 599 } 600 } 601 PetscFunctionReturn(0); 602 } 603 604 /*------------------------------------------------------------*/ 605 606 /* Routine for recomputing the predicted reduction for a given step vector */ 607 608 PetscErrorCode TaoBNKRecomputePred(Tao tao, Vec S, PetscReal *prered) 609 { 610 PetscErrorCode ierr; 611 TAO_BNK *bnk = (TAO_BNK *)tao->data; 612 613 PetscFunctionBegin; 614 /* Extract subvectors associated with the inactive set */ 615 if (bnk->inactive_idx){ 616 ierr = VecGetSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive);CHKERRQ(ierr); 617 ierr = VecGetSubVector(bnk->Xwork, bnk->inactive_idx, &bnk->inactive_work);CHKERRQ(ierr); 618 ierr = VecGetSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive);CHKERRQ(ierr); 619 } else { 620 bnk->X_inactive = tao->stepdirection; 621 bnk->inactive_work = bnk->Xwork; 622 bnk->G_inactive = bnk->Gwork; 623 } 624 /* Recompute the predicted decrease based on the quadratic model */ 625 ierr = MatMult(bnk->H_inactive, bnk->X_inactive, bnk->inactive_work);CHKERRQ(ierr); 626 ierr = VecAYPX(bnk->inactive_work, -0.5, bnk->G_inactive);CHKERRQ(ierr); 627 ierr = VecDot(bnk->inactive_work, bnk->X_inactive, prered); 628 /* Restore the sub vectors */ 629 if (bnk->inactive_idx){ 630 ierr = VecRestoreSubVector(tao->stepdirection, bnk->inactive_idx, &bnk->X_inactive);CHKERRQ(ierr); 631 ierr = VecRestoreSubVector(bnk->Xwork, bnk->inactive_idx, &bnk->inactive_work);CHKERRQ(ierr); 632 ierr = VecRestoreSubVector(bnk->Gwork, bnk->inactive_idx, &bnk->G_inactive);CHKERRQ(ierr); 633 } 634 PetscFunctionReturn(0); 635 } 636 637 /*------------------------------------------------------------*/ 638 639 /* Routine for ensuring that the Newton step is a descent direction. 640 641 The step direction falls back onto BFGS, scaled gradient and gradient steps 642 in the event that the Newton step fails the test. 643 */ 644 645 PetscErrorCode TaoBNKSafeguardStep(Tao tao, KSPConvergedReason ksp_reason, PetscInt *stepType) 646 { 647 PetscErrorCode ierr; 648 TAO_BNK *bnk = (TAO_BNK *)tao->data; 649 650 PetscReal gdx, delta, e_min; 651 PetscInt bfgsUpdates; 652 653 PetscFunctionBegin; 654 ierr = VecDot(tao->stepdirection, tao->gradient, &gdx);CHKERRQ(ierr); 655 if ((gdx >= 0.0) || PetscIsInfOrNanReal(gdx)) { 656 /* Newton step is not descent or direction produced Inf or NaN 657 Update the perturbation for next time */ 658 if (bnk->pert <= 0.0) { 659 /* Initialize the perturbation */ 660 bnk->pert = PetscMin(bnk->imax, PetscMax(bnk->imin, bnk->imfac * bnk->gnorm)); 661 if (bnk->is_gltr) { 662 ierr = KSPCGGLTRGetMinEig(tao->ksp,&e_min);CHKERRQ(ierr); 663 bnk->pert = PetscMax(bnk->pert, -e_min); 664 } 665 } else { 666 /* Increase the perturbation */ 667 bnk->pert = PetscMin(bnk->pmax, PetscMax(bnk->pgfac * bnk->pert, bnk->pmgfac * bnk->gnorm)); 668 } 669 670 if (BNK_PC_BFGS != bnk->pc_type) { 671 /* We don't have the bfgs matrix around and updated 672 Must use gradient direction in this case */ 673 ierr = VecCopy(tao->gradient, tao->stepdirection);CHKERRQ(ierr); 674 *stepType = BNK_GRADIENT; 675 } else { 676 /* Attempt to use the BFGS direction */ 677 ierr = MatLMVMSetInactive(bnk->M, NULL);CHKERRQ(ierr); 678 ierr = MatLMVMSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); 679 680 /* Check for success (descent direction) 681 NOTE: Negative gdx here means not a descent direction because 682 the fall-back step is missing a negative sign. */ 683 ierr = VecDot(tao->gradient, tao->stepdirection, &gdx);CHKERRQ(ierr); 684 if ((gdx <= 0) || PetscIsInfOrNanReal(gdx)) { 685 /* BFGS direction is not descent or direction produced not a number 686 We can assert bfgsUpdates > 1 in this case because 687 the first solve produces the scaled gradient direction, 688 which is guaranteed to be descent */ 689 690 /* Use steepest descent direction (scaled) */ 691 delta = 2.0 * PetscMax(1.0, PetscAbsScalar(bnk->f)) / (bnk->gnorm*bnk->gnorm); 692 ierr = MatLMVMSetDelta(bnk->M, delta);CHKERRQ(ierr); 693 ierr = MatLMVMReset(bnk->M);CHKERRQ(ierr); 694 ierr = MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient);CHKERRQ(ierr); 695 ierr = MatLMVMSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); 696 697 *stepType = BNK_SCALED_GRADIENT; 698 } else { 699 ierr = MatLMVMGetUpdates(bnk->M, &bfgsUpdates);CHKERRQ(ierr); 700 if (1 == bfgsUpdates) { 701 /* The first BFGS direction is always the scaled gradient */ 702 *stepType = BNK_SCALED_GRADIENT; 703 } else { 704 *stepType = BNK_BFGS; 705 } 706 } 707 } 708 /* Make sure the safeguarded fall-back step is zero for actively bounded variables */ 709 ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); 710 ierr = TaoBNKBoundStep(tao, tao->stepdirection);CHKERRQ(ierr); 711 } else { 712 /* Computed Newton step is descent */ 713 switch (ksp_reason) { 714 case KSP_DIVERGED_NANORINF: 715 case KSP_DIVERGED_BREAKDOWN: 716 case KSP_DIVERGED_INDEFINITE_MAT: 717 case KSP_DIVERGED_INDEFINITE_PC: 718 case KSP_CONVERGED_CG_NEG_CURVE: 719 /* Matrix or preconditioner is indefinite; increase perturbation */ 720 if (bnk->pert <= 0.0) { 721 /* Initialize the perturbation */ 722 bnk->pert = PetscMin(bnk->imax, PetscMax(bnk->imin, bnk->imfac * bnk->gnorm)); 723 if (bnk->is_gltr) { 724 ierr = KSPCGGLTRGetMinEig(tao->ksp, &e_min);CHKERRQ(ierr); 725 bnk->pert = PetscMax(bnk->pert, -e_min); 726 } 727 } else { 728 /* Increase the perturbation */ 729 bnk->pert = PetscMin(bnk->pmax, PetscMax(bnk->pgfac * bnk->pert, bnk->pmgfac * bnk->gnorm)); 730 } 731 break; 732 733 default: 734 /* Newton step computation is good; decrease perturbation */ 735 bnk->pert = PetscMin(bnk->psfac * bnk->pert, bnk->pmsfac * bnk->gnorm); 736 if (bnk->pert < bnk->pmin) { 737 bnk->pert = 0.0; 738 } 739 break; 740 } 741 *stepType = BNK_NEWTON; 742 } 743 PetscFunctionReturn(0); 744 } 745 746 /*------------------------------------------------------------*/ 747 748 /* Routine for performing a bound-projected More-Thuente line search. 749 750 Includes fallbacks to BFGS, scaled gradient, and unscaled gradient steps if the 751 Newton step does not produce a valid step length. 752 */ 753 754 PetscErrorCode TaoBNKPerformLineSearch(Tao tao, PetscInt stepType, PetscReal *steplen, TaoLineSearchConvergedReason *reason) 755 { 756 TAO_BNK *bnk = (TAO_BNK *)tao->data; 757 PetscErrorCode ierr; 758 TaoLineSearchConvergedReason ls_reason; 759 760 PetscReal e_min, gdx, delta; 761 PetscInt bfgsUpdates; 762 763 PetscFunctionBegin; 764 /* Perform the linesearch */ 765 ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &bnk->f, bnk->unprojected_gradient, tao->stepdirection, steplen, &ls_reason);CHKERRQ(ierr); 766 ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); 767 768 while (ls_reason != TAOLINESEARCH_SUCCESS && ls_reason != TAOLINESEARCH_SUCCESS_USER && (stepType != BNK_GRADIENT || stepType !=BNK_SCALED_GRADIENT)) { 769 /* Linesearch failed, revert solution */ 770 bnk->f = bnk->fold; 771 ierr = VecCopy(bnk->Xold, tao->solution);CHKERRQ(ierr); 772 ierr = VecCopy(bnk->unprojected_gradient_old, bnk->unprojected_gradient);CHKERRQ(ierr); 773 774 switch(stepType) { 775 case BNK_NEWTON: 776 /* Failed to obtain acceptable iterate with Newton step 777 Update the perturbation for next time */ 778 if (bnk->pert <= 0.0) { 779 /* Initialize the perturbation */ 780 bnk->pert = PetscMin(bnk->imax, PetscMax(bnk->imin, bnk->imfac * bnk->gnorm)); 781 if (bnk->is_gltr) { 782 ierr = KSPCGGLTRGetMinEig(tao->ksp,&e_min);CHKERRQ(ierr); 783 bnk->pert = PetscMax(bnk->pert, -e_min); 784 } 785 } else { 786 /* Increase the perturbation */ 787 bnk->pert = PetscMin(bnk->pmax, PetscMax(bnk->pgfac * bnk->pert, bnk->pmgfac * bnk->gnorm)); 788 } 789 790 if (BNK_PC_BFGS != bnk->pc_type) { 791 /* We don't have the bfgs matrix around and being updated 792 Must use gradient direction in this case */ 793 ierr = VecCopy(tao->gradient, tao->stepdirection);CHKERRQ(ierr); 794 stepType = BNK_GRADIENT; 795 } else { 796 /* Attempt to use the BFGS direction */ 797 ierr = MatLMVMSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); 798 /* Check for success (descent direction) 799 NOTE: Negative gdx means not a descent direction because the step here is missing a negative sign. */ 800 ierr = VecDot(tao->gradient, tao->stepdirection, &gdx);CHKERRQ(ierr); 801 if ((gdx <= 0) || PetscIsInfOrNanReal(gdx)) { 802 /* BFGS direction is not descent or direction produced not a number 803 We can assert bfgsUpdates > 1 in this case 804 Use steepest descent direction (scaled) */ 805 delta = 2.0 * PetscMax(1.0, PetscAbsScalar(bnk->f)) / (bnk->gnorm*bnk->gnorm); 806 ierr = MatLMVMSetDelta(bnk->M, delta);CHKERRQ(ierr); 807 ierr = MatLMVMReset(bnk->M);CHKERRQ(ierr); 808 ierr = MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient);CHKERRQ(ierr); 809 ierr = MatLMVMSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); 810 811 bfgsUpdates = 1; 812 stepType = BNK_SCALED_GRADIENT; 813 } else { 814 ierr = MatLMVMGetUpdates(bnk->M, &bfgsUpdates);CHKERRQ(ierr); 815 if (1 == bfgsUpdates) { 816 /* The first BFGS direction is always the scaled gradient */ 817 stepType = BNK_SCALED_GRADIENT; 818 } else { 819 stepType = BNK_BFGS; 820 } 821 } 822 } 823 break; 824 825 case BNK_BFGS: 826 /* Can only enter if pc_type == BNK_PC_BFGS 827 Failed to obtain acceptable iterate with BFGS step 828 Attempt to use the scaled gradient direction */ 829 delta = 2.0 * PetscMax(1.0, PetscAbsScalar(bnk->f)) / (bnk->gnorm*bnk->gnorm); 830 ierr = MatLMVMSetDelta(bnk->M, delta);CHKERRQ(ierr); 831 ierr = MatLMVMReset(bnk->M);CHKERRQ(ierr); 832 ierr = MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient);CHKERRQ(ierr); 833 ierr = MatLMVMSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); 834 835 bfgsUpdates = 1; 836 stepType = BNK_SCALED_GRADIENT; 837 break; 838 839 case BNK_SCALED_GRADIENT: 840 /* Can only enter if pc_type == BNK_PC_BFGS 841 The scaled gradient step did not produce a new iterate; 842 attemp to use the gradient direction. 843 Need to make sure we are not using a different diagonal scaling */ 844 ierr = MatLMVMSetScale(bnk->M,0);CHKERRQ(ierr); 845 ierr = MatLMVMSetDelta(bnk->M,1.0);CHKERRQ(ierr); 846 ierr = MatLMVMReset(bnk->M);CHKERRQ(ierr); 847 ierr = MatLMVMUpdate(bnk->M, tao->solution, bnk->unprojected_gradient);CHKERRQ(ierr); 848 ierr = MatLMVMSolve(bnk->M, bnk->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); 849 850 bfgsUpdates = 1; 851 stepType = BNK_GRADIENT; 852 break; 853 } 854 /* Make sure the safeguarded fall-back step is zero for actively bounded variables */ 855 ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); 856 ierr = TaoBNKBoundStep(tao, tao->stepdirection);CHKERRQ(ierr); 857 858 /* Perform one last line search with the fall-back step */ 859 ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &bnk->f, bnk->unprojected_gradient, tao->stepdirection, steplen, &ls_reason);CHKERRQ(ierr); 860 ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); 861 } 862 *reason = ls_reason; 863 PetscFunctionReturn(0); 864 } 865 866 /*------------------------------------------------------------*/ 867 868 /* Routine for updating the trust radius. 869 870 Function features three different update methods: 871 1) Line-search step length based 872 2) Predicted decrease on the CG quadratic model 873 3) Interpolation 874 */ 875 876 PetscErrorCode TaoBNKUpdateTrustRadius(Tao tao, PetscReal prered, PetscReal actred, PetscInt updateType, PetscInt stepType, PetscBool *accept) 877 { 878 TAO_BNK *bnk = (TAO_BNK *)tao->data; 879 PetscErrorCode ierr; 880 881 PetscReal step, kappa; 882 PetscReal gdx, tau_1, tau_2, tau_min, tau_max; 883 884 PetscFunctionBegin; 885 /* Update trust region radius */ 886 *accept = PETSC_FALSE; 887 switch(updateType) { 888 case BNK_UPDATE_STEP: 889 *accept = PETSC_TRUE; /* always accept here because line search succeeded */ 890 if (stepType == BNK_NEWTON) { 891 ierr = TaoLineSearchGetStepLength(tao->linesearch, &step);CHKERRQ(ierr); 892 if (step < bnk->nu1) { 893 /* Very bad step taken; reduce radius */ 894 tao->trust = bnk->omega1 * PetscMin(bnk->dnorm, tao->trust); 895 } else if (step < bnk->nu2) { 896 /* Reasonably bad step taken; reduce radius */ 897 tao->trust = bnk->omega2 * PetscMin(bnk->dnorm, tao->trust); 898 } else if (step < bnk->nu3) { 899 /* Reasonable step was taken; leave radius alone */ 900 if (bnk->omega3 < 1.0) { 901 tao->trust = bnk->omega3 * PetscMin(bnk->dnorm, tao->trust); 902 } else if (bnk->omega3 > 1.0) { 903 tao->trust = PetscMax(bnk->omega3 * bnk->dnorm, tao->trust); 904 } 905 } else if (step < bnk->nu4) { 906 /* Full step taken; increase the radius */ 907 tao->trust = PetscMax(bnk->omega4 * bnk->dnorm, tao->trust); 908 } else { 909 /* More than full step taken; increase the radius */ 910 tao->trust = PetscMax(bnk->omega5 * bnk->dnorm, tao->trust); 911 } 912 } else { 913 /* Newton step was not good; reduce the radius */ 914 tao->trust = bnk->omega1 * PetscMin(bnk->dnorm, tao->trust); 915 } 916 break; 917 918 case BNK_UPDATE_REDUCTION: 919 if (stepType == BNK_NEWTON) { 920 if (prered < 0.0) { 921 /* The predicted reduction has the wrong sign. This cannot 922 happen in infinite precision arithmetic. Step should 923 be rejected! */ 924 tao->trust = bnk->alpha1 * PetscMin(tao->trust, bnk->dnorm); 925 } 926 else { 927 if (PetscIsInfOrNanReal(actred)) { 928 tao->trust = bnk->alpha1 * PetscMin(tao->trust, bnk->dnorm); 929 } else { 930 if ((PetscAbsScalar(actred) <= PetscMax(1.0, PetscAbsScalar(bnk->f))*bnk->epsilon) && 931 (PetscAbsScalar(prered) <= PetscMax(1.0, PetscAbsScalar(bnk->f))*bnk->epsilon)) { 932 kappa = 1.0; 933 } 934 else { 935 kappa = actred / prered; 936 } 937 938 /* Accept or reject the step and update radius */ 939 if (kappa < bnk->eta1) { 940 /* Reject the step */ 941 tao->trust = bnk->alpha1 * PetscMin(tao->trust, bnk->dnorm); 942 } 943 else { 944 /* Accept the step */ 945 *accept = PETSC_TRUE; 946 /* Update the trust region radius only if the computed step is at the trust radius boundary */ 947 if (bnk->dnorm == tao->trust) { 948 if (kappa < bnk->eta2) { 949 /* Marginal bad step */ 950 tao->trust = bnk->alpha2 * tao->trust; 951 } 952 else if (kappa < bnk->eta3) { 953 /* Reasonable step */ 954 tao->trust = bnk->alpha3 * tao->trust; 955 } 956 else if (kappa < bnk->eta4) { 957 /* Good step */ 958 tao->trust = bnk->alpha4 * tao->trust; 959 } 960 else { 961 /* Very good step */ 962 tao->trust = bnk->alpha5 * tao->trust; 963 } 964 } 965 } 966 } 967 } 968 } else { 969 /* Newton step was not good; reduce the radius */ 970 tao->trust = bnk->alpha1 * PetscMin(bnk->dnorm, tao->trust); 971 } 972 break; 973 974 default: 975 if (stepType == BNK_NEWTON) { 976 if (prered < 0.0) { 977 /* The predicted reduction has the wrong sign. This cannot */ 978 /* happen in infinite precision arithmetic. Step should */ 979 /* be rejected! */ 980 tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm); 981 } else { 982 if (PetscIsInfOrNanReal(actred)) { 983 tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm); 984 } else { 985 if ((PetscAbsScalar(actred) <= bnk->epsilon) && (PetscAbsScalar(prered) <= bnk->epsilon)) { 986 kappa = 1.0; 987 } else { 988 kappa = actred / prered; 989 } 990 991 ierr = VecDot(tao->gradient, tao->stepdirection, &gdx);CHKERRQ(ierr); 992 tau_1 = bnk->theta * gdx / (bnk->theta * gdx - (1.0 - bnk->theta) * prered + actred); 993 tau_2 = bnk->theta * gdx / (bnk->theta * gdx + (1.0 + bnk->theta) * prered - actred); 994 tau_min = PetscMin(tau_1, tau_2); 995 tau_max = PetscMax(tau_1, tau_2); 996 997 if (kappa >= 1.0 - bnk->mu1) { 998 /* Great agreement */ 999 *accept = PETSC_TRUE; 1000 if (tau_max < 1.0) { 1001 tao->trust = PetscMax(tao->trust, bnk->gamma3 * bnk->dnorm); 1002 } else if (tau_max > bnk->gamma4) { 1003 tao->trust = PetscMax(tao->trust, bnk->gamma4 * bnk->dnorm); 1004 } else { 1005 tao->trust = PetscMax(tao->trust, tau_max * bnk->dnorm); 1006 } 1007 } else if (kappa >= 1.0 - bnk->mu2) { 1008 /* Good agreement */ 1009 *accept = PETSC_TRUE; 1010 if (tau_max < bnk->gamma2) { 1011 tao->trust = bnk->gamma2 * PetscMin(tao->trust, bnk->dnorm); 1012 } else if (tau_max > bnk->gamma3) { 1013 tao->trust = PetscMax(tao->trust, bnk->gamma3 * bnk->dnorm); 1014 } else if (tau_max < 1.0) { 1015 tao->trust = tau_max * PetscMin(tao->trust, bnk->dnorm); 1016 } else { 1017 tao->trust = PetscMax(tao->trust, tau_max * bnk->dnorm); 1018 } 1019 } else { 1020 /* Not good agreement */ 1021 if (tau_min > 1.0) { 1022 tao->trust = bnk->gamma2 * PetscMin(tao->trust, bnk->dnorm); 1023 } else if (tau_max < bnk->gamma1) { 1024 tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm); 1025 } else if ((tau_min < bnk->gamma1) && (tau_max >= 1.0)) { 1026 tao->trust = bnk->gamma1 * PetscMin(tao->trust, bnk->dnorm); 1027 } else if ((tau_1 >= bnk->gamma1) && (tau_1 < 1.0) && ((tau_2 < bnk->gamma1) || (tau_2 >= 1.0))) { 1028 tao->trust = tau_1 * PetscMin(tao->trust, bnk->dnorm); 1029 } else if ((tau_2 >= bnk->gamma1) && (tau_2 < 1.0) && ((tau_1 < bnk->gamma1) || (tau_2 >= 1.0))) { 1030 tao->trust = tau_2 * PetscMin(tao->trust, bnk->dnorm); 1031 } else { 1032 tao->trust = tau_max * PetscMin(tao->trust, bnk->dnorm); 1033 } 1034 } 1035 } 1036 } 1037 } else { 1038 /* Newton step was not good; reduce the radius */ 1039 tao->trust = bnk->gamma1 * PetscMin(bnk->dnorm, tao->trust); 1040 } 1041 break; 1042 } 1043 /* Make sure the radius does not violate min and max settings */ 1044 tao->trust = PetscMin(tao->trust, bnk->max_radius); 1045 tao->trust = PetscMax(tao->trust, bnk->min_radius); 1046 PetscFunctionReturn(0); 1047 } 1048 1049 /* ---------------------------------------------------------- */ 1050 1051 PetscErrorCode TaoBNKAddStepCounts(Tao tao, PetscInt stepType) 1052 { 1053 TAO_BNK *bnk = (TAO_BNK *)tao->data; 1054 1055 PetscFunctionBegin; 1056 switch (stepType) { 1057 case BNK_NEWTON: 1058 ++bnk->newt; 1059 break; 1060 case BNK_BFGS: 1061 ++bnk->bfgs; 1062 break; 1063 case BNK_SCALED_GRADIENT: 1064 ++bnk->sgrad; 1065 break; 1066 case BNK_GRADIENT: 1067 ++bnk->grad; 1068 break; 1069 default: 1070 break; 1071 } 1072 PetscFunctionReturn(0); 1073 } 1074 1075 /* ---------------------------------------------------------- */ 1076 1077 PetscErrorCode TaoSetUp_BNK(Tao tao) 1078 { 1079 TAO_BNK *bnk = (TAO_BNK *)tao->data; 1080 PetscErrorCode ierr; 1081 KSPType ksp_type; 1082 1083 PetscFunctionBegin; 1084 if (!tao->gradient) {ierr = VecDuplicate(tao->solution,&tao->gradient);CHKERRQ(ierr);} 1085 if (!tao->stepdirection) {ierr = VecDuplicate(tao->solution,&tao->stepdirection);CHKERRQ(ierr);} 1086 if (!bnk->W) {ierr = VecDuplicate(tao->solution,&bnk->W);CHKERRQ(ierr);} 1087 if (!bnk->Xold) {ierr = VecDuplicate(tao->solution,&bnk->Xold);CHKERRQ(ierr);} 1088 if (!bnk->Gold) {ierr = VecDuplicate(tao->solution,&bnk->Gold);CHKERRQ(ierr);} 1089 if (!bnk->Xwork) {ierr = VecDuplicate(tao->solution,&bnk->Xwork);CHKERRQ(ierr);} 1090 if (!bnk->Gwork) {ierr = VecDuplicate(tao->solution,&bnk->Gwork);CHKERRQ(ierr);} 1091 if (!bnk->unprojected_gradient) {ierr = VecDuplicate(tao->solution,&bnk->unprojected_gradient);CHKERRQ(ierr);} 1092 if (!bnk->unprojected_gradient_old) {ierr = VecDuplicate(tao->solution,&bnk->unprojected_gradient_old);CHKERRQ(ierr);} 1093 if (!bnk->Diag_min) {ierr = VecDuplicate(tao->solution,&bnk->Diag_min);CHKERRQ(ierr);} 1094 if (!bnk->Diag_max) {ierr = VecDuplicate(tao->solution,&bnk->Diag_max);CHKERRQ(ierr);} 1095 if (!bnk->bncg_sol && bnk->max_cg_its > 0) {ierr = VecDuplicate(tao->solution,&bnk->bncg_sol);CHKERRQ(ierr);} 1096 bnk->Diag = 0; 1097 bnk->Diag_red = 0; 1098 bnk->X_inactive = 0; 1099 bnk->G_inactive = 0; 1100 bnk->inactive_work = 0; 1101 bnk->active_work = 0; 1102 bnk->inactive_idx = 0; 1103 bnk->active_idx = 0; 1104 bnk->active_lower = 0; 1105 bnk->active_upper = 0; 1106 bnk->active_fixed = 0; 1107 bnk->M = 0; 1108 bnk->H_inactive = 0; 1109 bnk->Hpre_inactive = 0; 1110 ierr = KSPGetType(tao->ksp,&ksp_type);CHKERRQ(ierr); 1111 ierr = PetscStrcmp(ksp_type,KSPCGNASH,&bnk->is_nash);CHKERRQ(ierr); 1112 ierr = PetscStrcmp(ksp_type,KSPCGSTCG,&bnk->is_stcg);CHKERRQ(ierr); 1113 ierr = PetscStrcmp(ksp_type,KSPCGGLTR,&bnk->is_gltr);CHKERRQ(ierr); 1114 PetscFunctionReturn(0); 1115 } 1116 1117 /*------------------------------------------------------------*/ 1118 1119 static PetscErrorCode TaoDestroy_BNK(Tao tao) 1120 { 1121 TAO_BNK *bnk = (TAO_BNK *)tao->data; 1122 PetscErrorCode ierr; 1123 1124 PetscFunctionBegin; 1125 if (tao->setupcalled) { 1126 ierr = VecDestroy(&bnk->W);CHKERRQ(ierr); 1127 ierr = VecDestroy(&bnk->Xold);CHKERRQ(ierr); 1128 ierr = VecDestroy(&bnk->Gold);CHKERRQ(ierr); 1129 ierr = VecDestroy(&bnk->Xwork);CHKERRQ(ierr); 1130 ierr = VecDestroy(&bnk->Gwork);CHKERRQ(ierr); 1131 ierr = VecDestroy(&bnk->unprojected_gradient);CHKERRQ(ierr); 1132 ierr = VecDestroy(&bnk->unprojected_gradient_old);CHKERRQ(ierr); 1133 ierr = VecDestroy(&bnk->Diag_min);CHKERRQ(ierr); 1134 ierr = VecDestroy(&bnk->Diag_max);CHKERRQ(ierr); 1135 if (bnk->max_cg_its > 0) { 1136 ierr = TaoDestroy(&bnk->bncg);CHKERRQ(ierr); 1137 ierr = VecDestroy(&bnk->bncg_sol);CHKERRQ(ierr); 1138 } 1139 } 1140 ierr = VecDestroy(&bnk->Diag);CHKERRQ(ierr); 1141 ierr = MatDestroy(&bnk->M);CHKERRQ(ierr); 1142 if (bnk->Hpre_inactive != tao->hessian_pre && bnk->Hpre_inactive != bnk->H_inactive) {ierr = MatDestroy(&bnk->Hpre_inactive);CHKERRQ(ierr);} 1143 if (bnk->H_inactive != tao->hessian) {ierr = MatDestroy(&bnk->H_inactive);CHKERRQ(ierr);} 1144 ierr = PetscFree(tao->data);CHKERRQ(ierr); 1145 PetscFunctionReturn(0); 1146 } 1147 1148 /*------------------------------------------------------------*/ 1149 1150 static PetscErrorCode TaoSetFromOptions_BNK(PetscOptionItems *PetscOptionsObject,Tao tao) 1151 { 1152 TAO_BNK *bnk = (TAO_BNK *)tao->data; 1153 PetscErrorCode ierr; 1154 1155 PetscFunctionBegin; 1156 ierr = PetscOptionsHead(PetscOptionsObject,"Newton line search method for unconstrained optimization");CHKERRQ(ierr); 1157 ierr = PetscOptionsEList("-tao_bnk_pc_type", "pc type", "", BNK_PC, BNK_PC_TYPES, BNK_PC[bnk->pc_type], &bnk->pc_type, 0);CHKERRQ(ierr); 1158 ierr = PetscOptionsEList("-tao_bnk_bfgs_scale_type", "bfgs scale type", "", BFGS_SCALE, BFGS_SCALE_TYPES, BFGS_SCALE[bnk->bfgs_scale_type], &bnk->bfgs_scale_type, 0);CHKERRQ(ierr); 1159 ierr = PetscOptionsEList("-tao_bnk_init_type", "radius initialization type", "", BNK_INIT, BNK_INIT_TYPES, BNK_INIT[bnk->init_type], &bnk->init_type, 0);CHKERRQ(ierr); 1160 ierr = PetscOptionsEList("-tao_bnk_update_type", "radius update type", "", BNK_UPDATE, BNK_UPDATE_TYPES, BNK_UPDATE[bnk->update_type], &bnk->update_type, 0);CHKERRQ(ierr); 1161 ierr = PetscOptionsEList("-tao_bnk_as_type", "active set estimation method", "", BNK_AS, BNK_AS_TYPES, BNK_AS[bnk->as_type], &bnk->as_type, 0);CHKERRQ(ierr); 1162 ierr = PetscOptionsReal("-tao_bnk_sval", "perturbation starting value", "", bnk->sval, &bnk->sval,NULL);CHKERRQ(ierr); 1163 ierr = PetscOptionsReal("-tao_bnk_imin", "minimum initial perturbation", "", bnk->imin, &bnk->imin,NULL);CHKERRQ(ierr); 1164 ierr = PetscOptionsReal("-tao_bnk_imax", "maximum initial perturbation", "", bnk->imax, &bnk->imax,NULL);CHKERRQ(ierr); 1165 ierr = PetscOptionsReal("-tao_bnk_imfac", "initial merit factor", "", bnk->imfac, &bnk->imfac,NULL);CHKERRQ(ierr); 1166 ierr = PetscOptionsReal("-tao_bnk_pmin", "minimum perturbation", "", bnk->pmin, &bnk->pmin,NULL);CHKERRQ(ierr); 1167 ierr = PetscOptionsReal("-tao_bnk_pmax", "maximum perturbation", "", bnk->pmax, &bnk->pmax,NULL);CHKERRQ(ierr); 1168 ierr = PetscOptionsReal("-tao_bnk_pgfac", "growth factor", "", bnk->pgfac, &bnk->pgfac,NULL);CHKERRQ(ierr); 1169 ierr = PetscOptionsReal("-tao_bnk_psfac", "shrink factor", "", bnk->psfac, &bnk->psfac,NULL);CHKERRQ(ierr); 1170 ierr = PetscOptionsReal("-tao_bnk_pmgfac", "merit growth factor", "", bnk->pmgfac, &bnk->pmgfac,NULL);CHKERRQ(ierr); 1171 ierr = PetscOptionsReal("-tao_bnk_pmsfac", "merit shrink factor", "", bnk->pmsfac, &bnk->pmsfac,NULL);CHKERRQ(ierr); 1172 ierr = PetscOptionsReal("-tao_bnk_eta1", "poor steplength; reduce radius", "", bnk->eta1, &bnk->eta1,NULL);CHKERRQ(ierr); 1173 ierr = PetscOptionsReal("-tao_bnk_eta2", "reasonable steplength; leave radius alone", "", bnk->eta2, &bnk->eta2,NULL);CHKERRQ(ierr); 1174 ierr = PetscOptionsReal("-tao_bnk_eta3", "good steplength; increase radius", "", bnk->eta3, &bnk->eta3,NULL);CHKERRQ(ierr); 1175 ierr = PetscOptionsReal("-tao_bnk_eta4", "excellent steplength; greatly increase radius", "", bnk->eta4, &bnk->eta4,NULL);CHKERRQ(ierr); 1176 ierr = PetscOptionsReal("-tao_bnk_alpha1", "", "", bnk->alpha1, &bnk->alpha1,NULL);CHKERRQ(ierr); 1177 ierr = PetscOptionsReal("-tao_bnk_alpha2", "", "", bnk->alpha2, &bnk->alpha2,NULL);CHKERRQ(ierr); 1178 ierr = PetscOptionsReal("-tao_bnk_alpha3", "", "", bnk->alpha3, &bnk->alpha3,NULL);CHKERRQ(ierr); 1179 ierr = PetscOptionsReal("-tao_bnk_alpha4", "", "", bnk->alpha4, &bnk->alpha4,NULL);CHKERRQ(ierr); 1180 ierr = PetscOptionsReal("-tao_bnk_alpha5", "", "", bnk->alpha5, &bnk->alpha5,NULL);CHKERRQ(ierr); 1181 ierr = PetscOptionsReal("-tao_bnk_nu1", "poor steplength; reduce radius", "", bnk->nu1, &bnk->nu1,NULL);CHKERRQ(ierr); 1182 ierr = PetscOptionsReal("-tao_bnk_nu2", "reasonable steplength; leave radius alone", "", bnk->nu2, &bnk->nu2,NULL);CHKERRQ(ierr); 1183 ierr = PetscOptionsReal("-tao_bnk_nu3", "good steplength; increase radius", "", bnk->nu3, &bnk->nu3,NULL);CHKERRQ(ierr); 1184 ierr = PetscOptionsReal("-tao_bnk_nu4", "excellent steplength; greatly increase radius", "", bnk->nu4, &bnk->nu4,NULL);CHKERRQ(ierr); 1185 ierr = PetscOptionsReal("-tao_bnk_omega1", "", "", bnk->omega1, &bnk->omega1,NULL);CHKERRQ(ierr); 1186 ierr = PetscOptionsReal("-tao_bnk_omega2", "", "", bnk->omega2, &bnk->omega2,NULL);CHKERRQ(ierr); 1187 ierr = PetscOptionsReal("-tao_bnk_omega3", "", "", bnk->omega3, &bnk->omega3,NULL);CHKERRQ(ierr); 1188 ierr = PetscOptionsReal("-tao_bnk_omega4", "", "", bnk->omega4, &bnk->omega4,NULL);CHKERRQ(ierr); 1189 ierr = PetscOptionsReal("-tao_bnk_omega5", "", "", bnk->omega5, &bnk->omega5,NULL);CHKERRQ(ierr); 1190 ierr = PetscOptionsReal("-tao_bnk_mu1_i", "", "", bnk->mu1_i, &bnk->mu1_i,NULL);CHKERRQ(ierr); 1191 ierr = PetscOptionsReal("-tao_bnk_mu2_i", "", "", bnk->mu2_i, &bnk->mu2_i,NULL);CHKERRQ(ierr); 1192 ierr = PetscOptionsReal("-tao_bnk_gamma1_i", "", "", bnk->gamma1_i, &bnk->gamma1_i,NULL);CHKERRQ(ierr); 1193 ierr = PetscOptionsReal("-tao_bnk_gamma2_i", "", "", bnk->gamma2_i, &bnk->gamma2_i,NULL);CHKERRQ(ierr); 1194 ierr = PetscOptionsReal("-tao_bnk_gamma3_i", "", "", bnk->gamma3_i, &bnk->gamma3_i,NULL);CHKERRQ(ierr); 1195 ierr = PetscOptionsReal("-tao_bnk_gamma4_i", "", "", bnk->gamma4_i, &bnk->gamma4_i,NULL);CHKERRQ(ierr); 1196 ierr = PetscOptionsReal("-tao_bnk_theta_i", "", "", bnk->theta_i, &bnk->theta_i,NULL);CHKERRQ(ierr); 1197 ierr = PetscOptionsReal("-tao_bnk_mu1", "", "", bnk->mu1, &bnk->mu1,NULL);CHKERRQ(ierr); 1198 ierr = PetscOptionsReal("-tao_bnk_mu2", "", "", bnk->mu2, &bnk->mu2,NULL);CHKERRQ(ierr); 1199 ierr = PetscOptionsReal("-tao_bnk_gamma1", "", "", bnk->gamma1, &bnk->gamma1,NULL);CHKERRQ(ierr); 1200 ierr = PetscOptionsReal("-tao_bnk_gamma2", "", "", bnk->gamma2, &bnk->gamma2,NULL);CHKERRQ(ierr); 1201 ierr = PetscOptionsReal("-tao_bnk_gamma3", "", "", bnk->gamma3, &bnk->gamma3,NULL);CHKERRQ(ierr); 1202 ierr = PetscOptionsReal("-tao_bnk_gamma4", "", "", bnk->gamma4, &bnk->gamma4,NULL);CHKERRQ(ierr); 1203 ierr = PetscOptionsReal("-tao_bnk_theta", "", "", bnk->theta, &bnk->theta,NULL);CHKERRQ(ierr); 1204 ierr = PetscOptionsReal("-tao_bnk_min_radius", "lower bound on initial radius", "", bnk->min_radius, &bnk->min_radius,NULL);CHKERRQ(ierr); 1205 ierr = PetscOptionsReal("-tao_bnk_max_radius", "upper bound on radius", "", bnk->max_radius, &bnk->max_radius,NULL);CHKERRQ(ierr); 1206 ierr = PetscOptionsReal("-tao_bnk_epsilon", "tolerance used when computing actual and predicted reduction", "", bnk->epsilon, &bnk->epsilon,NULL);CHKERRQ(ierr); 1207 ierr = PetscOptionsReal("-tao_bnk_as_tol", "initial tolerance used when estimating actively bounded variables", "", bnk->as_tol, &bnk->as_tol,NULL);CHKERRQ(ierr); 1208 ierr = PetscOptionsReal("-tao_bnk_as_step", "step length used when estimating actively bounded variables", "", bnk->as_step, &bnk->as_step,NULL);CHKERRQ(ierr); 1209 ierr = PetscOptionsInt("-tao_bnk_max_cg_its", "number of BNCG iterations to take for each Newton step", "", bnk->max_cg_its, &bnk->max_cg_its,NULL);CHKERRQ(ierr); 1210 ierr = PetscOptionsTail();CHKERRQ(ierr); 1211 ierr = TaoLineSearchSetFromOptions(tao->linesearch);CHKERRQ(ierr); 1212 ierr = KSPSetFromOptions(tao->ksp);CHKERRQ(ierr); 1213 PetscFunctionReturn(0); 1214 } 1215 1216 /*------------------------------------------------------------*/ 1217 1218 static PetscErrorCode TaoView_BNK(Tao tao, PetscViewer viewer) 1219 { 1220 TAO_BNK *bnk = (TAO_BNK *)tao->data; 1221 PetscInt nrejects; 1222 PetscBool isascii; 1223 PetscErrorCode ierr; 1224 1225 PetscFunctionBegin; 1226 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); 1227 if (isascii) { 1228 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1229 if (BNK_PC_BFGS == bnk->pc_type && bnk->M) { 1230 ierr = MatLMVMGetRejects(bnk->M,&nrejects);CHKERRQ(ierr); 1231 ierr = PetscViewerASCIIPrintf(viewer, "Rejected matrix updates: %D\n",nrejects);CHKERRQ(ierr); 1232 } 1233 ierr = PetscViewerASCIIPrintf(viewer, "Newton steps: %D\n", bnk->newt);CHKERRQ(ierr); 1234 ierr = PetscViewerASCIIPrintf(viewer, "BFGS steps: %D\n", bnk->bfgs);CHKERRQ(ierr); 1235 ierr = PetscViewerASCIIPrintf(viewer, "Scaled gradient steps: %D\n", bnk->sgrad);CHKERRQ(ierr); 1236 ierr = PetscViewerASCIIPrintf(viewer, "Gradient steps: %D\n", bnk->grad);CHKERRQ(ierr); 1237 ierr = PetscViewerASCIIPrintf(viewer, "KSP termination reasons:\n");CHKERRQ(ierr); 1238 ierr = PetscViewerASCIIPrintf(viewer, " atol: %D\n", bnk->ksp_atol);CHKERRQ(ierr); 1239 ierr = PetscViewerASCIIPrintf(viewer, " rtol: %D\n", bnk->ksp_rtol);CHKERRQ(ierr); 1240 ierr = PetscViewerASCIIPrintf(viewer, " ctol: %D\n", bnk->ksp_ctol);CHKERRQ(ierr); 1241 ierr = PetscViewerASCIIPrintf(viewer, " negc: %D\n", bnk->ksp_negc);CHKERRQ(ierr); 1242 ierr = PetscViewerASCIIPrintf(viewer, " dtol: %D\n", bnk->ksp_dtol);CHKERRQ(ierr); 1243 ierr = PetscViewerASCIIPrintf(viewer, " iter: %D\n", bnk->ksp_iter);CHKERRQ(ierr); 1244 ierr = PetscViewerASCIIPrintf(viewer, " othr: %D\n", bnk->ksp_othr);CHKERRQ(ierr); 1245 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1246 } 1247 PetscFunctionReturn(0); 1248 } 1249 1250 /* ---------------------------------------------------------- */ 1251 1252 /*MC 1253 TAOBNK - Shared base-type for Bounded Newton-Krylov type algorithms. 1254 At each iteration, the BNK methods solve the symmetric 1255 system of equations to obtain the step diretion dk: 1256 Hk dk = -gk 1257 for free variables only. The step can be globalized either through 1258 trust-region methods, or a line search, or a heuristic mixture of both. 1259 1260 Options Database Keys: 1261 + -tao_bnk_pc_type - "none","ahess","bfgs","petsc" 1262 . -tao_bnk_bfgs_scale_type - "ahess","phess","bfgs" 1263 . -tao_bnk_init_type - "constant","direction","interpolation" 1264 . -tao_bnk_update_type - "step","direction","interpolation" 1265 . -tao_bnk_as_type - "none","bertsekas" 1266 . -tao_bnk_sval - perturbation starting value 1267 . -tao_bnk_imin - minimum initial perturbation 1268 . -tao_bnk_imax - maximum initial perturbation 1269 . -tao_bnk_pmin - minimum perturbation 1270 . -tao_bnk_pmax - maximum perturbation 1271 . -tao_bnk_pgfac - growth factor 1272 . -tao_bnk_psfac - shrink factor 1273 . -tao_bnk_imfac - initial merit factor 1274 . -tao_bnk_pmgfac - merit growth factor 1275 . -tao_bnk_pmsfac - merit shrink factor 1276 . -tao_bnk_eta1 - poor steplength; reduce radius 1277 . -tao_bnk_eta2 - reasonable steplength; leave radius 1278 . -tao_bnk_eta3 - good steplength; increase readius 1279 . -tao_bnk_eta4 - excellent steplength; greatly increase radius 1280 . -tao_bnk_alpha1 - alpha1 reduction 1281 . -tao_bnk_alpha2 - alpha2 reduction 1282 . -tao_bnk_alpha3 - alpha3 reduction 1283 . -tao_bnk_alpha4 - alpha4 reduction 1284 . -tao_bnk_alpha - alpha5 reduction 1285 . -tao_bnk_mu1 - mu1 interpolation update 1286 . -tao_bnk_mu2 - mu2 interpolation update 1287 . -tao_bnk_gamma1 - gamma1 interpolation update 1288 . -tao_bnk_gamma2 - gamma2 interpolation update 1289 . -tao_bnk_gamma3 - gamma3 interpolation update 1290 . -tao_bnk_gamma4 - gamma4 interpolation update 1291 . -tao_bnk_theta - theta interpolation update 1292 . -tao_bnk_omega1 - omega1 step update 1293 . -tao_bnk_omega2 - omega2 step update 1294 . -tao_bnk_omega3 - omega3 step update 1295 . -tao_bnk_omega4 - omega4 step update 1296 . -tao_bnk_omega5 - omega5 step update 1297 . -tao_bnk_mu1_i - mu1 interpolation init factor 1298 . -tao_bnk_mu2_i - mu2 interpolation init factor 1299 . -tao_bnk_gamma1_i - gamma1 interpolation init factor 1300 . -tao_bnk_gamma2_i - gamma2 interpolation init factor 1301 . -tao_bnk_gamma3_i - gamma3 interpolation init factor 1302 . -tao_bnk_gamma4_i - gamma4 interpolation init factor 1303 . -tao_bnk_theta_i - theta interpolation init factor 1304 - -tao_bnk_bound_tol - initial tolerance used in estimating bounded active variables 1305 1306 Level: beginner 1307 M*/ 1308 1309 PetscErrorCode TaoCreate_BNK(Tao tao) 1310 { 1311 TAO_BNK *bnk; 1312 const char *morethuente_type = TAOLINESEARCHMT; 1313 PetscErrorCode ierr; 1314 1315 PetscFunctionBegin; 1316 ierr = PetscNewLog(tao,&bnk);CHKERRQ(ierr); 1317 1318 tao->ops->setup = TaoSetUp_BNK; 1319 tao->ops->view = TaoView_BNK; 1320 tao->ops->setfromoptions = TaoSetFromOptions_BNK; 1321 tao->ops->destroy = TaoDestroy_BNK; 1322 1323 /* Override default settings (unless already changed) */ 1324 if (!tao->max_it_changed) tao->max_it = 50; 1325 if (!tao->trust0_changed) tao->trust0 = 100.0; 1326 1327 tao->data = (void*)bnk; 1328 1329 /* Hessian shifting parameters */ 1330 bnk->sval = 0.0; 1331 bnk->imin = 1.0e-4; 1332 bnk->imax = 1.0e+2; 1333 bnk->imfac = 1.0e-1; 1334 1335 bnk->pmin = 1.0e-12; 1336 bnk->pmax = 1.0e+2; 1337 bnk->pgfac = 1.0e+1; 1338 bnk->psfac = 4.0e-1; 1339 bnk->pmgfac = 1.0e-1; 1340 bnk->pmsfac = 1.0e-1; 1341 1342 /* Default values for trust-region radius update based on steplength */ 1343 bnk->nu1 = 0.25; 1344 bnk->nu2 = 0.50; 1345 bnk->nu3 = 1.00; 1346 bnk->nu4 = 1.25; 1347 1348 bnk->omega1 = 0.25; 1349 bnk->omega2 = 0.50; 1350 bnk->omega3 = 1.00; 1351 bnk->omega4 = 2.00; 1352 bnk->omega5 = 4.00; 1353 1354 /* Default values for trust-region radius update based on reduction */ 1355 bnk->eta1 = 1.0e-4; 1356 bnk->eta2 = 0.25; 1357 bnk->eta3 = 0.50; 1358 bnk->eta4 = 0.90; 1359 1360 bnk->alpha1 = 0.25; 1361 bnk->alpha2 = 0.50; 1362 bnk->alpha3 = 1.00; 1363 bnk->alpha4 = 2.00; 1364 bnk->alpha5 = 4.00; 1365 1366 /* Default values for trust-region radius update based on interpolation */ 1367 bnk->mu1 = 0.10; 1368 bnk->mu2 = 0.50; 1369 1370 bnk->gamma1 = 0.25; 1371 bnk->gamma2 = 0.50; 1372 bnk->gamma3 = 2.00; 1373 bnk->gamma4 = 4.00; 1374 1375 bnk->theta = 0.05; 1376 1377 /* Default values for trust region initialization based on interpolation */ 1378 bnk->mu1_i = 0.35; 1379 bnk->mu2_i = 0.50; 1380 1381 bnk->gamma1_i = 0.0625; 1382 bnk->gamma2_i = 0.5; 1383 bnk->gamma3_i = 2.0; 1384 bnk->gamma4_i = 5.0; 1385 1386 bnk->theta_i = 0.25; 1387 1388 /* Remaining parameters */ 1389 bnk->max_cg_its = 0; 1390 bnk->min_radius = 1.0e-10; 1391 bnk->max_radius = 1.0e10; 1392 bnk->epsilon = PetscPowReal(PETSC_MACHINE_EPSILON, 2.0/3.0); 1393 bnk->as_tol = 1.0e-3; 1394 bnk->as_step = 1.0e-3; 1395 bnk->dmin = 1.0e-6; 1396 bnk->dmax = 1.0e6; 1397 1398 bnk->pc_type = BNK_PC_BFGS; 1399 bnk->bfgs_scale_type = BFGS_SCALE_PHESS; 1400 bnk->init_type = BNK_INIT_INTERPOLATION; 1401 bnk->update_type = BNK_UPDATE_INTERPOLATION; 1402 bnk->as_type = BNK_AS_BERTSEKAS; 1403 1404 /* Create the line search */ 1405 ierr = TaoLineSearchCreate(((PetscObject)tao)->comm,&tao->linesearch);CHKERRQ(ierr); 1406 ierr = PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1);CHKERRQ(ierr); 1407 ierr = TaoLineSearchSetType(tao->linesearch,morethuente_type);CHKERRQ(ierr); 1408 ierr = TaoLineSearchUseTaoRoutines(tao->linesearch,tao);CHKERRQ(ierr); 1409 ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);CHKERRQ(ierr); 1410 1411 /* Set linear solver to default for symmetric matrices */ 1412 ierr = KSPCreate(((PetscObject)tao)->comm,&tao->ksp);CHKERRQ(ierr); 1413 ierr = PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1);CHKERRQ(ierr); 1414 ierr = KSPSetOptionsPrefix(tao->ksp,tao->hdr.prefix);CHKERRQ(ierr); 1415 ierr = KSPSetType(tao->ksp,KSPCGSTCG);CHKERRQ(ierr); 1416 PetscFunctionReturn(0); 1417 } 1418