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