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