Actual source code: bqnls.c

petsc-3.10.5 2019-03-28
Report Typos and Errors
  1:  #include <../src/tao/bound/impls/bqnk/bqnk.h>

  3: static const char *BNK_AS[64] = {"none", "bertsekas"};

  5: static PetscErrorCode TaoBQNLSComputeHessian(Tao tao)
  6: {
  7:   TAO_BNK        *bnk = (TAO_BNK *)tao->data;
  8:   TAO_BQNK       *bqnk = (TAO_BQNK*)bnk->ctx;
 10:   PetscReal      gnorm2, delta;
 11: 
 13:   /* Compute the initial scaling and update the approximation */
 14:   gnorm2 = bnk->gnorm*bnk->gnorm;
 15:   if (gnorm2 == 0.0) gnorm2 = PETSC_MACHINE_EPSILON;
 16:   if (bnk->f == 0.0) {
 17:     delta = 2.0 / gnorm2;
 18:   } else {
 19:     delta = 2.0 * PetscAbsScalar(bnk->f) / gnorm2;
 20:   }
 21:   MatSymBrdnSetDelta(bqnk->B, delta);
 22:   MatLMVMUpdate(bqnk->B, tao->solution, bnk->unprojected_gradient);
 23:   return(0);
 24: }

 26: static PetscErrorCode TaoBQNLSComputeStep(Tao tao, PetscBool shift, KSPConvergedReason *ksp_reason, PetscInt *step_type)
 27: {
 28:   TAO_BNK        *bnk = (TAO_BNK *)tao->data;
 29:   TAO_BQNK       *bqnk = (TAO_BQNK*)bnk->ctx;
 31:   PetscInt       nupdates;

 34:   MatSolve(bqnk->B, tao->gradient, tao->stepdirection);
 35:   VecScale(tao->stepdirection, -1.0);
 36:   TaoBNKBoundStep(tao, bnk->as_type, tao->stepdirection);
 37:   *ksp_reason = KSP_CONVERGED_ATOL;
 38:   MatLMVMGetUpdateCount(bqnk->B, &nupdates);
 39:   if (nupdates == 0) {
 40:     *step_type = BNK_SCALED_GRADIENT;
 41:   } else {
 42:     *step_type = BNK_BFGS;
 43:   }
 44:   return(0);
 45: }

 47: static PetscErrorCode TaoSetFromOptions_BQNLS(PetscOptionItems *PetscOptionsObject,Tao tao)
 48: {
 49:   TAO_BNK        *bnk = (TAO_BNK *)tao->data;
 50:   TAO_BQNK       *bqnk = (TAO_BQNK*)bnk->ctx;
 52:   KSPType        ksp_type;
 53:   PetscBool      is_spd;

 56:   PetscOptionsHead(PetscOptionsObject,"Quasi-Newton-Krylov method for bound constrained optimization");
 57:   PetscOptionsEList("-tao_bqnls_as_type", "active set estimation method", "", BNK_AS, BNK_AS_TYPES, BNK_AS[bnk->as_type], &bnk->as_type, 0);
 58:   PetscOptionsReal("-tao_bqnls_epsilon", "(developer) tolerance used when computing actual and predicted reduction", "", bnk->epsilon, &bnk->epsilon,NULL);
 59:   PetscOptionsReal("-tao_bqnls_as_tol", "(developer) initial tolerance used when estimating actively bounded variables", "", bnk->as_tol, &bnk->as_tol,NULL);
 60:   PetscOptionsReal("-tao_bqnls_as_step", "(developer) step length used when estimating actively bounded variables", "", bnk->as_step, &bnk->as_step,NULL);
 61:   PetscOptionsInt("-tao_bqnls_max_cg_its", "number of BNCG iterations to take for each Newton step", "", bnk->max_cg_its, &bnk->max_cg_its,NULL);
 62:   PetscOptionsTail();
 63:   TaoSetFromOptions(bnk->bncg);
 64:   TaoLineSearchSetFromOptions(tao->linesearch);
 65:   KSPSetFromOptions(tao->ksp);
 66:   KSPGetType(tao->ksp,&ksp_type);
 67:   bnk->is_nash = bnk->is_gltr = bnk->is_stcg = PETSC_FALSE;
 68:   MatSetFromOptions(bqnk->B);
 69:   MatGetOption(bqnk->B, MAT_SPD, &is_spd);
 70:   if (!is_spd) SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_INCOMP, "LMVM matrix must be symmetric positive-definite");
 71:   return(0);
 72: }

 74: PETSC_EXTERN PetscErrorCode TaoCreate_BQNLS(Tao tao)
 75: {
 76:   TAO_BNK        *bnk;
 77:   TAO_BQNK       *bqnk;
 79: 
 81:   TaoCreate_BQNK(tao);
 82:   KSPSetOptionsPrefix(tao->ksp, "unused");
 83:   tao->ops->solve = TaoSolve_BNLS;
 84:   tao->ops->setfromoptions = TaoSetFromOptions_BQNLS;
 85: 
 86:   bnk = (TAO_BNK*)tao->data;
 87:   bnk->update_type = BNK_UPDATE_STEP;
 88:   bnk->computehessian = TaoBQNLSComputeHessian;
 89:   bnk->computestep = TaoBQNLSComputeStep;
 90: 
 91:   bqnk = (TAO_BQNK*)bnk->ctx;
 92:   MatSetOptionsPrefix(bqnk->B, "tao_bqnls_");
 93:   MatSetType(bqnk->B, MATLMVMBFGS);
 94:   return(0);
 95: }