static PetscErrorCode MatCopy_LMVM(Mat B, Mat M, MatStructure str) { Mat_LMVM *bctx = (Mat_LMVM*)B->data; Mat_LMVM *mctx; PetscErrorCode ierr; PetscInt i; PetscBool allocatedM; PetscFunctionBegin; if (str == DIFFERENT_NONZERO_PATTERN) { ierr = MatLMVMReset(M, PETSC_TRUE);CHKERRQ(ierr); ierr = MatLMVMAllocate(M, bctx->Xprev, bctx->Fprev);CHKERRQ(ierr); } else { ierr = MatLMVMIsAllocated(M, &allocatedM);CHKERRQ(ierr); if (!allocatedM) SETERRQ(PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Target matrix must be allocated first"); MatCheckSameSize(B, 1, M, 2); } mctx = (Mat_LMVM*)M->data; if (bctx->user_pc) { ierr = MatLMVMSetJ0PC(M, bctx->J0pc);CHKERRQ(ierr); } else if (bctx->user_ksp) { ierr = MatLMVMSetJ0KSP(M, bctx->J0ksp);CHKERRQ(ierr); } else if (bctx->J0) { ierr = MatLMVMSetJ0(M, bctx->J0);CHKERRQ(ierr); } else if (bctx->user_scale) { if (bctx->J0diag) { ierr = MatLMVMSetJ0Diag(M, bctx->J0diag);CHKERRQ(ierr); } else { ierr = MatLMVMSetJ0Scale(M, bctx->J0scalar);CHKERRQ(ierr); } } mctx->nupdates = bctx->nupdates; mctx->nrejects = bctx->nrejects; mctx->k = bctx->k; for (i=0; i<=bctx->k; ++i) { ierr = VecCopy(bctx->S[i], mctx->S[i]);CHKERRQ(ierr); ierr = VecCopy(bctx->Y[i], mctx->Y[i]);CHKERRQ(ierr); ierr = VecCopy(bctx->Xprev, mctx->Xprev);CHKERRQ(ierr); ierr = VecCopy(bctx->Fprev, mctx->Fprev);CHKERRQ(ierr); } if (bctx->ops->copy) { ierr = (*bctx->ops->copy)(B, M, str);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode MatAllocate_LMVM(Mat B, Vec X, Vec F) { Mat_LMVM *lmvm = (Mat_LMVM*)B->data; PetscErrorCode ierr; PetscBool same, allocate = PETSC_FALSE; PetscInt m, n, M, N; VecType type; PetscFunctionBegin; if (lmvm->allocated) { VecCheckMatCompatible(B, X, 2, F, 3); ierr = VecGetType(X, &type);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)lmvm->Xprev, type, &same);CHKERRQ(ierr); if (!same) { /* Given X vector has a different type than allocated X-type data structures. We need to destroy all of this and duplicate again out of the given vector. */ allocate = PETSC_TRUE; ierr = MatLMVMReset(B, PETSC_TRUE);CHKERRQ(ierr); } } else { allocate = PETSC_TRUE; } if (allocate) { ierr = VecGetLocalSize(X, &n);CHKERRQ(ierr); ierr = VecGetSize(X, &N);CHKERRQ(ierr); ierr = VecGetLocalSize(F, &m);CHKERRQ(ierr); ierr = VecGetSize(F, &M);CHKERRQ(ierr); ierr = MatSetSizes(B, m, n, M, N);CHKERRQ(ierr); ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); ierr = VecDuplicate(X, &lmvm->Xprev);CHKERRQ(ierr); ierr = VecDuplicate(F, &lmvm->Fprev);CHKERRQ(ierr); if (lmvm->m > 0) { ierr = VecDuplicateVecs(lmvm->Xprev, lmvm->m, &lmvm->S);CHKERRQ(ierr); ierr = VecDuplicateVecs(lmvm->Fprev, lmvm->m, &lmvm->Y);CHKERRQ(ierr); } lmvm->allocated = PETSC_TRUE; B->preallocated = PETSC_TRUE; B->assembled = PETSC_TRUE; } PetscFunctionReturn(0); }
static PetscErrorCode TaoSolve_OWLQN(Tao tao) { TAO_OWLQN *lmP = (TAO_OWLQN *)tao->data; PetscReal f, fold, gdx, gnorm; PetscReal step = 1.0; PetscReal delta; PetscErrorCode ierr; PetscInt stepType; PetscInt iter = 0; TaoConvergedReason reason = TAO_CONTINUE_ITERATING; TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING; PetscFunctionBegin; if (tao->XL || tao->XU || tao->ops->computebounds) { ierr = PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by owlqn algorithm\n");CHKERRQ(ierr); } /* Check convergence criteria */ ierr = TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient);CHKERRQ(ierr); ierr = VecCopy(tao->gradient, lmP->GV);CHKERRQ(ierr); ierr = ComputePseudoGrad_OWLQN(tao->solution,lmP->GV,lmP->lambda);CHKERRQ(ierr); ierr = VecNorm(lmP->GV,NORM_2,&gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, step, &reason);CHKERRQ(ierr); if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); /* Set initial scaling for the function */ if (f != 0.0) { delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(lmP->M,delta);CHKERRQ(ierr); /* Set counter for gradient/reset steps */ lmP->bfgs = 0; lmP->sgrad = 0; lmP->grad = 0; /* Have not converged; continue with Newton method */ while (reason == TAO_CONTINUE_ITERATING) { /* Compute direction */ ierr = MatLMVMUpdate(lmP->M,tao->solution,tao->gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(lmP->M, lmP->GV, lmP->D);CHKERRQ(ierr); ierr = ProjDirect_OWLQN(lmP->D,lmP->GV);CHKERRQ(ierr); ++lmP->bfgs; /* Check for success (descent direction) */ ierr = VecDot(lmP->D, lmP->GV , &gdx);CHKERRQ(ierr); if ((gdx <= 0.0) || PetscIsInfOrNanReal(gdx)) { /* Step is not descent or direction produced not a number We can assert bfgsUpdates > 1 in this case because the first solve produces the scaled gradient direction, which is guaranteed to be descent Use steepest descent direction (scaled) */ ++lmP->grad; if (f != 0.0) { delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(lmP->M, delta);CHKERRQ(ierr); ierr = MatLMVMReset(lmP->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(lmP->M,lmP->GV, lmP->D);CHKERRQ(ierr); ierr = ProjDirect_OWLQN(lmP->D,lmP->GV);CHKERRQ(ierr); lmP->bfgs = 1; ++lmP->sgrad; stepType = OWLQN_SCALED_GRADIENT; } else { if (1 == lmP->bfgs) { /* The first BFGS direction is always the scaled gradient */ ++lmP->sgrad; stepType = OWLQN_SCALED_GRADIENT; } else { ++lmP->bfgs; stepType = OWLQN_BFGS; } } ierr = VecScale(lmP->D, -1.0);CHKERRQ(ierr); /* Perform the linesearch */ fold = f; ierr = VecCopy(tao->solution, lmP->Xold);CHKERRQ(ierr); ierr = VecCopy(tao->gradient, lmP->Gold);CHKERRQ(ierr); ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, lmP->GV, lmP->D, &step,&ls_status);CHKERRQ(ierr); ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); while (((int)ls_status < 0) && (stepType != OWLQN_GRADIENT)) { /* Reset factors and use scaled gradient step */ f = fold; ierr = VecCopy(lmP->Xold, tao->solution);CHKERRQ(ierr); ierr = VecCopy(lmP->Gold, tao->gradient);CHKERRQ(ierr); ierr = VecCopy(tao->gradient, lmP->GV);CHKERRQ(ierr); ierr = ComputePseudoGrad_OWLQN(tao->solution,lmP->GV,lmP->lambda);CHKERRQ(ierr); switch(stepType) { case OWLQN_BFGS: /* Failed to obtain acceptable iterate with BFGS step Attempt to use the scaled gradient direction */ if (f != 0.0) { delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(lmP->M, delta);CHKERRQ(ierr); ierr = MatLMVMReset(lmP->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(lmP->M, lmP->GV, lmP->D);CHKERRQ(ierr); ierr = ProjDirect_OWLQN(lmP->D,lmP->GV);CHKERRQ(ierr); lmP->bfgs = 1; ++lmP->sgrad; stepType = OWLQN_SCALED_GRADIENT; break; case OWLQN_SCALED_GRADIENT: /* The scaled gradient step did not produce a new iterate; attempt to use the gradient direction. Need to make sure we are not using a different diagonal scaling */ ierr = MatLMVMSetDelta(lmP->M, 1.0);CHKERRQ(ierr); ierr = MatLMVMReset(lmP->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(lmP->M, lmP->GV, lmP->D);CHKERRQ(ierr); ierr = ProjDirect_OWLQN(lmP->D,lmP->GV);CHKERRQ(ierr); lmP->bfgs = 1; ++lmP->grad; stepType = OWLQN_GRADIENT; break; } ierr = VecScale(lmP->D, -1.0);CHKERRQ(ierr); /* Perform the linesearch */ ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, lmP->GV, lmP->D, &step, &ls_status);CHKERRQ(ierr); ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); } if ((int)ls_status < 0) { /* Failed to find an improving point*/ f = fold; ierr = VecCopy(lmP->Xold, tao->solution);CHKERRQ(ierr); ierr = VecCopy(lmP->Gold, tao->gradient);CHKERRQ(ierr); ierr = VecCopy(tao->gradient, lmP->GV);CHKERRQ(ierr); step = 0.0; } else { /* a little hack here, because that gv is used to store g */ ierr = VecCopy(lmP->GV, tao->gradient);CHKERRQ(ierr); } ierr = ComputePseudoGrad_OWLQN(tao->solution,lmP->GV,lmP->lambda);CHKERRQ(ierr); /* Check for termination */ ierr = VecNorm(lmP->GV,NORM_2,&gnorm);CHKERRQ(ierr); iter++; ierr = TaoMonitor(tao,iter,f,gnorm,0.0,step,&reason);CHKERRQ(ierr); if ((int)ls_status < 0) break; } PetscFunctionReturn(0); }
static PetscErrorCode TaoSolve_NTR(Tao tao) { TAO_NTR *tr = (TAO_NTR *)tao->data; PC pc; KSPConvergedReason ksp_reason; TaoConvergedReason reason; PetscReal fmin, ftrial, prered, actred, kappa, sigma, beta; PetscReal tau, tau_1, tau_2, tau_max, tau_min, max_radius; PetscReal f, gnorm; PetscReal delta; PetscReal norm_d; PetscErrorCode ierr; PetscInt iter = 0; PetscInt bfgsUpdates = 0; PetscInt needH; PetscInt i_max = 5; PetscInt j_max = 1; PetscInt i, j, N, n, its; PetscFunctionBegin; if (tao->XL || tao->XU || tao->ops->computebounds) { ierr = PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by ntr algorithm\n");CHKERRQ(ierr); } tao->trust = tao->trust0; /* Modify the radius if it is too large or small */ tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); if (NTR_PC_BFGS == tr->pc_type && !tr->M) { ierr = VecGetLocalSize(tao->solution,&n);CHKERRQ(ierr); ierr = VecGetSize(tao->solution,&N);CHKERRQ(ierr); ierr = MatCreateLMVM(((PetscObject)tao)->comm,n,N,&tr->M);CHKERRQ(ierr); ierr = MatLMVMAllocateVectors(tr->M,tao->solution);CHKERRQ(ierr); } /* Check convergence criteria */ ierr = TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient);CHKERRQ(ierr); ierr = VecNorm(tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); needH = 1; ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, 1.0, &reason);CHKERRQ(ierr); if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); /* Create vectors for the limited memory preconditioner */ if ((NTR_PC_BFGS == tr->pc_type) && (BFGS_SCALE_BFGS != tr->bfgs_scale_type)) { if (!tr->Diag) { ierr = VecDuplicate(tao->solution, &tr->Diag);CHKERRQ(ierr); } } switch(tr->ksp_type) { case NTR_KSP_NASH: ierr = KSPSetType(tao->ksp, KSPNASH);CHKERRQ(ierr); if (tao->ksp->ops->setfromoptions) { (*tao->ksp->ops->setfromoptions)(tao->ksp); } break; case NTR_KSP_STCG: ierr = KSPSetType(tao->ksp, KSPSTCG);CHKERRQ(ierr); if (tao->ksp->ops->setfromoptions) { (*tao->ksp->ops->setfromoptions)(tao->ksp); } break; default: ierr = KSPSetType(tao->ksp, KSPGLTR);CHKERRQ(ierr); if (tao->ksp->ops->setfromoptions) { (*tao->ksp->ops->setfromoptions)(tao->ksp); } break; } /* Modify the preconditioner to use the bfgs approximation */ ierr = KSPGetPC(tao->ksp, &pc);CHKERRQ(ierr); switch(tr->pc_type) { case NTR_PC_NONE: ierr = PCSetType(pc, PCNONE);CHKERRQ(ierr); if (pc->ops->setfromoptions) { (*pc->ops->setfromoptions)(pc); } break; case NTR_PC_AHESS: ierr = PCSetType(pc, PCJACOBI);CHKERRQ(ierr); if (pc->ops->setfromoptions) { (*pc->ops->setfromoptions)(pc); } ierr = PCJacobiSetUseAbs(pc);CHKERRQ(ierr); break; case NTR_PC_BFGS: ierr = PCSetType(pc, PCSHELL);CHKERRQ(ierr); if (pc->ops->setfromoptions) { (*pc->ops->setfromoptions)(pc); } ierr = PCShellSetName(pc, "bfgs");CHKERRQ(ierr); ierr = PCShellSetContext(pc, tr->M);CHKERRQ(ierr); ierr = PCShellSetApply(pc, MatLMVMSolveShell);CHKERRQ(ierr); break; default: /* Use the pc method set by pc_type */ break; } /* Initialize trust-region radius */ switch(tr->init_type) { case NTR_INIT_CONSTANT: /* Use the initial radius specified */ break; case NTR_INIT_INTERPOLATION: /* Use the initial radius specified */ max_radius = 0.0; for (j = 0; j < j_max; ++j) { fmin = f; sigma = 0.0; if (needH) { ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); needH = 0; } for (i = 0; i < i_max; ++i) { ierr = VecCopy(tao->solution, tr->W);CHKERRQ(ierr); ierr = VecAXPY(tr->W, -tao->trust/gnorm, tao->gradient);CHKERRQ(ierr); ierr = TaoComputeObjective(tao, tr->W, &ftrial);CHKERRQ(ierr); if (PetscIsInfOrNanReal(ftrial)) { tau = tr->gamma1_i; } else { if (ftrial < fmin) { fmin = ftrial; sigma = -tao->trust / gnorm; } ierr = MatMult(tao->hessian, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = VecDot(tao->gradient, tao->stepdirection, &prered);CHKERRQ(ierr); prered = tao->trust * (gnorm - 0.5 * tao->trust * prered / (gnorm * gnorm)); actred = f - ftrial; if ((PetscAbsScalar(actred) <= tr->epsilon) && (PetscAbsScalar(prered) <= tr->epsilon)) { kappa = 1.0; } else { kappa = actred / prered; } tau_1 = tr->theta_i * gnorm * tao->trust / (tr->theta_i * gnorm * tao->trust + (1.0 - tr->theta_i) * prered - actred); tau_2 = tr->theta_i * gnorm * tao->trust / (tr->theta_i * gnorm * tao->trust - (1.0 + tr->theta_i) * prered + actred); tau_min = PetscMin(tau_1, tau_2); tau_max = PetscMax(tau_1, tau_2); if (PetscAbsScalar(kappa - 1.0) <= tr->mu1_i) { /* Great agreement */ max_radius = PetscMax(max_radius, tao->trust); if (tau_max < 1.0) { tau = tr->gamma3_i; } else if (tau_max > tr->gamma4_i) { tau = tr->gamma4_i; } else { tau = tau_max; } } else if (PetscAbsScalar(kappa - 1.0) <= tr->mu2_i) { /* Good agreement */ max_radius = PetscMax(max_radius, tao->trust); if (tau_max < tr->gamma2_i) { tau = tr->gamma2_i; } else if (tau_max > tr->gamma3_i) { tau = tr->gamma3_i; } else { tau = tau_max; } } else { /* Not good agreement */ if (tau_min > 1.0) { tau = tr->gamma2_i; } else if (tau_max < tr->gamma1_i) { tau = tr->gamma1_i; } else if ((tau_min < tr->gamma1_i) && (tau_max >= 1.0)) { tau = tr->gamma1_i; } else if ((tau_1 >= tr->gamma1_i) && (tau_1 < 1.0) && ((tau_2 < tr->gamma1_i) || (tau_2 >= 1.0))) { tau = tau_1; } else if ((tau_2 >= tr->gamma1_i) && (tau_2 < 1.0) && ((tau_1 < tr->gamma1_i) || (tau_2 >= 1.0))) { tau = tau_2; } else { tau = tau_max; } } } tao->trust = tau * tao->trust; } if (fmin < f) { f = fmin; ierr = VecAXPY(tao->solution, sigma, tao->gradient);CHKERRQ(ierr); ierr = TaoComputeGradient(tao,tao->solution, tao->gradient);CHKERRQ(ierr); ierr = VecNorm(tao->gradient, NORM_2, &gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); needH = 1; ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, 1.0, &reason);CHKERRQ(ierr); if (reason != TAO_CONTINUE_ITERATING) { PetscFunctionReturn(0); } } } tao->trust = PetscMax(tao->trust, max_radius); /* Modify the radius if it is too large or small */ tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); break; default: /* Norm of the first direction will initialize radius */ tao->trust = 0.0; break; } /* Set initial scaling for the BFGS preconditioner This step is done after computing the initial trust-region radius since the function value may have decreased */ if (NTR_PC_BFGS == tr->pc_type) { if (f != 0.0) { delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(tr->M,delta);CHKERRQ(ierr); } /* Have not converged; continue with Newton method */ while (reason == TAO_CONTINUE_ITERATING) { ++iter; tao->ksp_its=0; /* Compute the Hessian */ if (needH) { ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); needH = 0; } if (NTR_PC_BFGS == tr->pc_type) { if (BFGS_SCALE_AHESS == tr->bfgs_scale_type) { /* Obtain diagonal for the bfgs preconditioner */ ierr = MatGetDiagonal(tao->hessian, tr->Diag);CHKERRQ(ierr); ierr = VecAbs(tr->Diag);CHKERRQ(ierr); ierr = VecReciprocal(tr->Diag);CHKERRQ(ierr); ierr = MatLMVMSetScale(tr->M,tr->Diag);CHKERRQ(ierr); } /* Update the limited memory preconditioner */ ierr = MatLMVMUpdate(tr->M, tao->solution, tao->gradient);CHKERRQ(ierr); ++bfgsUpdates; } while (reason == TAO_CONTINUE_ITERATING) { ierr = KSPSetOperators(tao->ksp, tao->hessian, tao->hessian_pre);CHKERRQ(ierr); /* Solve the trust region subproblem */ if (NTR_KSP_NASH == tr->ksp_type) { ierr = KSPNASHSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = KSPNASHGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); } else if (NTR_KSP_STCG == tr->ksp_type) { ierr = KSPSTCGSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = KSPSTCGGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); } else { /* NTR_KSP_GLTR */ ierr = KSPGLTRSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = KSPGLTRGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); } if (0.0 == tao->trust) { /* Radius was uninitialized; use the norm of the direction */ if (norm_d > 0.0) { tao->trust = norm_d; /* Modify the radius if it is too large or small */ tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); } else { /* The direction was bad; set radius to default value and re-solve the trust-region subproblem to get a direction */ tao->trust = tao->trust0; /* Modify the radius if it is too large or small */ tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); if (NTR_KSP_NASH == tr->ksp_type) { ierr = KSPNASHSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = KSPNASHGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); } else if (NTR_KSP_STCG == tr->ksp_type) { ierr = KSPSTCGSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = KSPSTCGGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); } else { /* NTR_KSP_GLTR */ ierr = KSPGLTRSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = KSPGLTRGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); } if (norm_d == 0.0) SETERRQ(PETSC_COMM_SELF,1, "Initial direction zero"); } } ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); ierr = KSPGetConvergedReason(tao->ksp, &ksp_reason);CHKERRQ(ierr); if ((KSP_DIVERGED_INDEFINITE_PC == ksp_reason) && (NTR_PC_BFGS == tr->pc_type) && (bfgsUpdates > 1)) { /* Preconditioner is numerically indefinite; reset the approximate if using BFGS preconditioning. */ if (f != 0.0) { delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(tr->M, delta);CHKERRQ(ierr); ierr = MatLMVMReset(tr->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(tr->M, tao->solution, tao->gradient);CHKERRQ(ierr); bfgsUpdates = 1; } if (NTR_UPDATE_REDUCTION == tr->update_type) { /* Get predicted reduction */ if (NTR_KSP_NASH == tr->ksp_type) { ierr = KSPNASHGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); } else if (NTR_KSP_STCG == tr->ksp_type) { ierr = KSPSTCGGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); } else { /* gltr */ ierr = KSPGLTRGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); } if (prered >= 0.0) { /* The predicted reduction has the wrong sign. This cannot happen in infinite precision arithmetic. Step should be rejected! */ tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d); } else { /* Compute trial step and function value */ ierr = VecCopy(tao->solution,tr->W);CHKERRQ(ierr); ierr = VecAXPY(tr->W, 1.0, tao->stepdirection);CHKERRQ(ierr); ierr = TaoComputeObjective(tao, tr->W, &ftrial);CHKERRQ(ierr); if (PetscIsInfOrNanReal(ftrial)) { tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d); } else { /* Compute and actual reduction */ actred = f - ftrial; prered = -prered; if ((PetscAbsScalar(actred) <= tr->epsilon) && (PetscAbsScalar(prered) <= tr->epsilon)) { kappa = 1.0; } else { kappa = actred / prered; } /* Accept or reject the step and update radius */ if (kappa < tr->eta1) { /* Reject the step */ tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d); } else { /* Accept the step */ if (kappa < tr->eta2) { /* Marginal bad step */ tao->trust = tr->alpha2 * PetscMin(tao->trust, norm_d); } else if (kappa < tr->eta3) { /* Reasonable step */ tao->trust = tr->alpha3 * tao->trust; } else if (kappa < tr->eta4) { /* Good step */ tao->trust = PetscMax(tr->alpha4 * norm_d, tao->trust); } else { /* Very good step */ tao->trust = PetscMax(tr->alpha5 * norm_d, tao->trust); } break; } } } } else { /* Get predicted reduction */ if (NTR_KSP_NASH == tr->ksp_type) { ierr = KSPNASHGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); } else if (NTR_KSP_STCG == tr->ksp_type) { ierr = KSPSTCGGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); } else { /* gltr */ ierr = KSPGLTRGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); } if (prered >= 0.0) { /* The predicted reduction has the wrong sign. This cannot happen in infinite precision arithmetic. Step should be rejected! */ tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else { ierr = VecCopy(tao->solution, tr->W);CHKERRQ(ierr); ierr = VecAXPY(tr->W, 1.0, tao->stepdirection);CHKERRQ(ierr); ierr = TaoComputeObjective(tao, tr->W, &ftrial);CHKERRQ(ierr); if (PetscIsInfOrNanReal(ftrial)) { tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else { ierr = VecDot(tao->gradient, tao->stepdirection, &beta);CHKERRQ(ierr); actred = f - ftrial; prered = -prered; if ((PetscAbsScalar(actred) <= tr->epsilon) && (PetscAbsScalar(prered) <= tr->epsilon)) { kappa = 1.0; } else { kappa = actred / prered; } tau_1 = tr->theta * beta / (tr->theta * beta - (1.0 - tr->theta) * prered + actred); tau_2 = tr->theta * beta / (tr->theta * beta + (1.0 + tr->theta) * prered - actred); tau_min = PetscMin(tau_1, tau_2); tau_max = PetscMax(tau_1, tau_2); if (kappa >= 1.0 - tr->mu1) { /* Great agreement; accept step and update radius */ if (tau_max < 1.0) { tao->trust = PetscMax(tao->trust, tr->gamma3 * norm_d); } else if (tau_max > tr->gamma4) { tao->trust = PetscMax(tao->trust, tr->gamma4 * norm_d); } else { tao->trust = PetscMax(tao->trust, tau_max * norm_d); } break; } else if (kappa >= 1.0 - tr->mu2) { /* Good agreement */ if (tau_max < tr->gamma2) { tao->trust = tr->gamma2 * PetscMin(tao->trust, norm_d); } else if (tau_max > tr->gamma3) { tao->trust = PetscMax(tao->trust, tr->gamma3 * norm_d); } else if (tau_max < 1.0) { tao->trust = tau_max * PetscMin(tao->trust, norm_d); } else { tao->trust = PetscMax(tao->trust, tau_max * norm_d); } break; } else { /* Not good agreement */ if (tau_min > 1.0) { tao->trust = tr->gamma2 * PetscMin(tao->trust, norm_d); } else if (tau_max < tr->gamma1) { tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else if ((tau_min < tr->gamma1) && (tau_max >= 1.0)) { tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else if ((tau_1 >= tr->gamma1) && (tau_1 < 1.0) && ((tau_2 < tr->gamma1) || (tau_2 >= 1.0))) { tao->trust = tau_1 * PetscMin(tao->trust, norm_d); } else if ((tau_2 >= tr->gamma1) && (tau_2 < 1.0) && ((tau_1 < tr->gamma1) || (tau_2 >= 1.0))) { tao->trust = tau_2 * PetscMin(tao->trust, norm_d); } else { tao->trust = tau_max * PetscMin(tao->trust, norm_d); } } } } } /* The step computed was not good and the radius was decreased. Monitor the radius to terminate. */ ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, tao->trust, &reason);CHKERRQ(ierr); } /* The radius may have been increased; modify if it is too large */ tao->trust = PetscMin(tao->trust, tr->max_radius); if (reason == TAO_CONTINUE_ITERATING) { ierr = VecCopy(tr->W, tao->solution);CHKERRQ(ierr); f = ftrial; ierr = TaoComputeGradient(tao, tao->solution, tao->gradient); ierr = VecNorm(tao->gradient, NORM_2, &gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); needH = 1; ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, tao->trust, &reason);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
extern PetscErrorCode MatLMVMUpdate(Mat M, Vec x, Vec g) { MatLMVMCtx *ctx; PetscReal rhotemp, rhotol; PetscReal y0temp, s0temp; PetscReal yDy, yDs, sDs; PetscReal sigmanew, denom; PetscErrorCode ierr; PetscInt i; PetscBool same; PetscReal yy_sum=0.0, ys_sum=0.0, ss_sum=0.0; PetscFunctionBegin; PetscValidHeaderSpecific(x,VEC_CLASSID,2); PetscValidHeaderSpecific(g,VEC_CLASSID,3); ierr = PetscObjectTypeCompare((PetscObject)M,MATSHELL,&same);CHKERRQ(ierr); if (!same) SETERRQ(PETSC_COMM_SELF,1,"Matrix M is not type MatLMVM"); ierr = MatShellGetContext(M,(void**)&ctx);CHKERRQ(ierr); if (!ctx->allocated) { ierr = MatLMVMAllocateVectors(M, x); CHKERRQ(ierr); } if (0 == ctx->iter) { ierr = MatLMVMReset(M);CHKERRQ(ierr); } else { ierr = VecAYPX(ctx->Gprev,-1.0,g);CHKERRQ(ierr); ierr = VecAYPX(ctx->Xprev,-1.0,x);CHKERRQ(ierr); ierr = VecDot(ctx->Gprev,ctx->Xprev,&rhotemp);CHKERRQ(ierr); ierr = VecDot(ctx->Gprev,ctx->Gprev,&y0temp);CHKERRQ(ierr); rhotol = ctx->eps * y0temp; if (rhotemp > rhotol) { ++ctx->nupdates; ctx->lmnow = PetscMin(ctx->lmnow+1, ctx->lm); ierr=PetscObjectDereference((PetscObject)ctx->S[ctx->lm]);CHKERRQ(ierr); ierr=PetscObjectDereference((PetscObject)ctx->Y[ctx->lm]);CHKERRQ(ierr); for (i = ctx->lm-1; i >= 0; --i) { ctx->S[i+1] = ctx->S[i]; ctx->Y[i+1] = ctx->Y[i]; ctx->rho[i+1] = ctx->rho[i]; } ctx->S[0] = ctx->Xprev; ctx->Y[0] = ctx->Gprev; PetscObjectReference((PetscObject)ctx->S[0]); PetscObjectReference((PetscObject)ctx->Y[0]); ctx->rho[0] = 1.0 / rhotemp; /* Compute the scaling */ switch(ctx->scaleType) { case MatLMVM_Scale_None: break; case MatLMVM_Scale_Scalar: /* Compute s^T s */ ierr = VecDot(ctx->Xprev,ctx->Xprev,&s0temp);CHKERRQ(ierr); /* Scalar is positive; safeguards are not required. */ /* Save information for scalar scaling */ ctx->yy_history[(ctx->nupdates - 1) % ctx->scalar_history] = y0temp; ctx->ys_history[(ctx->nupdates - 1) % ctx->scalar_history] = rhotemp; ctx->ss_history[(ctx->nupdates - 1) % ctx->scalar_history] = s0temp; /* Compute summations for scalar scaling */ yy_sum = 0; /* No safeguard required; y^T y > 0 */ ys_sum = 0; /* No safeguard required; y^T s > 0 */ ss_sum = 0; /* No safeguard required; s^T s > 0 */ for (i = 0; i < PetscMin(ctx->nupdates, ctx->scalar_history); ++i) { yy_sum += ctx->yy_history[i]; ys_sum += ctx->ys_history[i]; ss_sum += ctx->ss_history[i]; } if (0.0 == ctx->s_alpha) { /* Safeguard ys_sum */ if (0.0 == ys_sum) { ys_sum = TAO_ZERO_SAFEGUARD; } sigmanew = ss_sum / ys_sum; } else if (1.0 == ctx->s_alpha) { /* Safeguard yy_sum */ if (0.0 == yy_sum) { yy_sum = TAO_ZERO_SAFEGUARD; } sigmanew = ys_sum / yy_sum; } else { denom = 2*ctx->s_alpha*yy_sum; /* Safeguard denom */ if (0.0 == denom) { denom = TAO_ZERO_SAFEGUARD; } sigmanew = ((2*ctx->s_alpha-1)*ys_sum + PetscSqrtScalar((2*ctx->s_alpha-1)*(2*ctx->s_alpha-1)*ys_sum*ys_sum - 4*(ctx->s_alpha)*(ctx->s_alpha-1)*yy_sum*ss_sum)) / denom; } switch(ctx->limitType) { case MatLMVM_Limit_Average: if (1.0 == ctx->mu) { ctx->sigma = sigmanew; } else if (ctx->mu) { ctx->sigma = ctx->mu * sigmanew + (1.0 - ctx->mu) * ctx->sigma; } break; case MatLMVM_Limit_Relative: if (ctx->mu) { ctx->sigma = TaoMid((1.0 - ctx->mu) * ctx->sigma, sigmanew, (1.0 + ctx->mu) * ctx->sigma); } break; case MatLMVM_Limit_Absolute: if (ctx->nu) { ctx->sigma = TaoMid(ctx->sigma - ctx->nu, sigmanew, ctx->sigma + ctx->nu); } break; default: ctx->sigma = sigmanew; break; } break; case MatLMVM_Scale_Broyden: /* Original version */ /* Combine DFP and BFGS */ /* This code appears to be numerically unstable. We use the */ /* original version because this was used to generate all of */ /* the data and because it may be the least unstable of the */ /* bunch. */ /* P = Q = inv(D); */ ierr = VecCopy(ctx->D,ctx->P);CHKERRQ(ierr); ierr = VecReciprocal(ctx->P);CHKERRQ(ierr); ierr = VecCopy(ctx->P,ctx->Q);CHKERRQ(ierr); /* V = y*y */ ierr = VecPointwiseMult(ctx->V,ctx->Gprev,ctx->Gprev);CHKERRQ(ierr); /* W = inv(D)*s */ ierr = VecPointwiseMult(ctx->W,ctx->Xprev,ctx->P);CHKERRQ(ierr); ierr = VecDot(ctx->W,ctx->Xprev,&sDs);CHKERRQ(ierr); /* Safeguard rhotemp and sDs */ if (0.0 == rhotemp) { rhotemp = TAO_ZERO_SAFEGUARD; } if (0.0 == sDs) { sDs = TAO_ZERO_SAFEGUARD; } if (1.0 != ctx->phi) { /* BFGS portion of the update */ /* U = (inv(D)*s)*(inv(D)*s) */ ierr = VecPointwiseMult(ctx->U,ctx->W,ctx->W);CHKERRQ(ierr); /* Assemble */ ierr = VecAXPY(ctx->P,1.0/rhotemp,ctx->V);CHKERRQ(ierr); ierr = VecAXPY(ctx->P,-1.0/sDs,ctx->U);CHKERRQ(ierr); } if (0.0 != ctx->phi) { /* DFP portion of the update */ /* U = inv(D)*s*y */ ierr = VecPointwiseMult(ctx->U, ctx->W, ctx->Gprev);CHKERRQ(ierr); /* Assemble */ ierr = VecAXPY(ctx->Q,1.0/rhotemp + sDs/(rhotemp*rhotemp), ctx->V);CHKERRQ(ierr); ierr = VecAXPY(ctx->Q,-2.0/rhotemp,ctx->U);CHKERRQ(ierr); } if (0.0 == ctx->phi) { ierr = VecCopy(ctx->P,ctx->U);CHKERRQ(ierr); } else if (1.0 == ctx->phi) { ierr = VecCopy(ctx->Q,ctx->U);CHKERRQ(ierr); } else { /* Broyden update U=(1-phi)*P + phi*Q */ ierr = VecCopy(ctx->Q,ctx->U);CHKERRQ(ierr); ierr = VecAXPBY(ctx->U,1.0-ctx->phi, ctx->phi, ctx->P);CHKERRQ(ierr); } /* Obtain inverse and ensure positive definite */ ierr = VecReciprocal(ctx->U);CHKERRQ(ierr); ierr = VecAbs(ctx->U);CHKERRQ(ierr); switch(ctx->rScaleType) { case MatLMVM_Rescale_None: break; case MatLMVM_Rescale_Scalar: case MatLMVM_Rescale_GL: if (ctx->rScaleType == MatLMVM_Rescale_GL) { /* Gilbert and Lemarachal use the old diagonal */ ierr = VecCopy(ctx->D,ctx->P);CHKERRQ(ierr); } else { /* The default version uses the current diagonal */ ierr = VecCopy(ctx->U,ctx->P);CHKERRQ(ierr); } /* Compute s^T s */ ierr = VecDot(ctx->Xprev,ctx->Xprev,&s0temp);CHKERRQ(ierr); /* Save information for special cases of scalar rescaling */ ctx->yy_rhistory[(ctx->nupdates - 1) % ctx->rescale_history] = y0temp; ctx->ys_rhistory[(ctx->nupdates - 1) % ctx->rescale_history] = rhotemp; ctx->ss_rhistory[(ctx->nupdates - 1) % ctx->rescale_history] = s0temp; if (0.5 == ctx->r_beta) { if (1 == PetscMin(ctx->nupdates, ctx->rescale_history)) { ierr = VecPointwiseMult(ctx->V,ctx->Y[0],ctx->P);CHKERRQ(ierr); ierr = VecDot(ctx->V,ctx->Y[0],&yy_sum);CHKERRQ(ierr); ierr = VecPointwiseDivide(ctx->W,ctx->S[0],ctx->P);CHKERRQ(ierr); ierr = VecDot(ctx->W,ctx->S[0],&ss_sum);CHKERRQ(ierr); ys_sum = ctx->ys_rhistory[0]; } else { ierr = VecCopy(ctx->P,ctx->Q);CHKERRQ(ierr); ierr = VecReciprocal(ctx->Q);CHKERRQ(ierr); /* Compute summations for scalar scaling */ yy_sum = 0; /* No safeguard required */ ys_sum = 0; /* No safeguard required */ ss_sum = 0; /* No safeguard required */ for (i = 0; i < PetscMin(ctx->nupdates, ctx->rescale_history); ++i) { ierr = VecPointwiseMult(ctx->V,ctx->Y[i],ctx->P);CHKERRQ(ierr); ierr = VecDot(ctx->V,ctx->Y[i],&yDy);CHKERRQ(ierr); yy_sum += yDy; ierr = VecPointwiseMult(ctx->W,ctx->S[i],ctx->Q);CHKERRQ(ierr); ierr = VecDot(ctx->W,ctx->S[i],&sDs);CHKERRQ(ierr); ss_sum += sDs; ys_sum += ctx->ys_rhistory[i]; } } } else if (0.0 == ctx->r_beta) { if (1 == PetscMin(ctx->nupdates, ctx->rescale_history)) { /* Compute summations for scalar scaling */ ierr = VecPointwiseDivide(ctx->W,ctx->S[0],ctx->P);CHKERRQ(ierr); ierr = VecDot(ctx->W, ctx->Y[0], &ys_sum);CHKERRQ(ierr); ierr = VecDot(ctx->W, ctx->W, &ss_sum);CHKERRQ(ierr); yy_sum += ctx->yy_rhistory[0]; } else { ierr = VecCopy(ctx->Q, ctx->P);CHKERRQ(ierr); ierr = VecReciprocal(ctx->Q);CHKERRQ(ierr); /* Compute summations for scalar scaling */ yy_sum = 0; /* No safeguard required */ ys_sum = 0; /* No safeguard required */ ss_sum = 0; /* No safeguard required */ for (i = 0; i < PetscMin(ctx->nupdates, ctx->rescale_history); ++i) { ierr = VecPointwiseMult(ctx->W, ctx->S[i], ctx->Q);CHKERRQ(ierr); ierr = VecDot(ctx->W, ctx->Y[i], &yDs);CHKERRQ(ierr); ys_sum += yDs; ierr = VecDot(ctx->W, ctx->W, &sDs);CHKERRQ(ierr); ss_sum += sDs; yy_sum += ctx->yy_rhistory[i]; } } } else if (1.0 == ctx->r_beta) { /* Compute summations for scalar scaling */ yy_sum = 0; /* No safeguard required */ ys_sum = 0; /* No safeguard required */ ss_sum = 0; /* No safeguard required */ for (i = 0; i < PetscMin(ctx->nupdates, ctx->rescale_history); ++i) { ierr = VecPointwiseMult(ctx->V, ctx->Y[i], ctx->P);CHKERRQ(ierr); ierr = VecDot(ctx->V, ctx->S[i], &yDs);CHKERRQ(ierr); ys_sum += yDs; ierr = VecDot(ctx->V, ctx->V, &yDy);CHKERRQ(ierr); yy_sum += yDy; ss_sum += ctx->ss_rhistory[i]; } } else { ierr = VecCopy(ctx->Q, ctx->P);CHKERRQ(ierr); ierr = VecPow(ctx->P, ctx->r_beta);CHKERRQ(ierr); ierr = VecPointwiseDivide(ctx->Q, ctx->P, ctx->Q);CHKERRQ(ierr); /* Compute summations for scalar scaling */ yy_sum = 0; /* No safeguard required */ ys_sum = 0; /* No safeguard required */ ss_sum = 0; /* No safeguard required */ for (i = 0; i < PetscMin(ctx->nupdates, ctx->rescale_history); ++i) { ierr = VecPointwiseMult(ctx->V, ctx->P, ctx->Y[i]);CHKERRQ(ierr); ierr = VecPointwiseMult(ctx->W, ctx->Q, ctx->S[i]);CHKERRQ(ierr); ierr = VecDot(ctx->V, ctx->V, &yDy);CHKERRQ(ierr); ierr = VecDot(ctx->V, ctx->W, &yDs);CHKERRQ(ierr); ierr = VecDot(ctx->W, ctx->W, &sDs);CHKERRQ(ierr); yy_sum += yDy; ys_sum += yDs; ss_sum += sDs; } } if (0.0 == ctx->r_alpha) { /* Safeguard ys_sum */ if (0.0 == ys_sum) { ys_sum = TAO_ZERO_SAFEGUARD; } sigmanew = ss_sum / ys_sum; } else if (1.0 == ctx->r_alpha) { /* Safeguard yy_sum */ if (0.0 == yy_sum) { ys_sum = TAO_ZERO_SAFEGUARD; } sigmanew = ys_sum / yy_sum; } else { denom = 2*ctx->r_alpha*yy_sum; /* Safeguard denom */ if (0.0 == denom) { denom = TAO_ZERO_SAFEGUARD; } sigmanew = ((2*ctx->r_alpha-1)*ys_sum + PetscSqrtScalar((2*ctx->r_alpha-1)*(2*ctx->r_alpha-1)*ys_sum*ys_sum - 4*ctx->r_alpha*(ctx->r_alpha-1)*yy_sum*ss_sum)) / denom; } /* If Q has small values, then Q^(r_beta - 1) */ /* can have very large values. Hence, ys_sum */ /* and ss_sum can be infinity. In this case, */ /* sigmanew can either be not-a-number or infinity. */ if (PetscIsInfOrNanReal(sigmanew)) { /* sigmanew is not-a-number; skip rescaling */ } else if (!sigmanew) { /* sigmanew is zero; this is a bad case; skip rescaling */ } else { /* sigmanew is positive */ ierr = VecScale(ctx->U, sigmanew);CHKERRQ(ierr); } break; } /* Modify for previous information */ switch(ctx->limitType) { case MatLMVM_Limit_Average: if (1.0 == ctx->mu) { ierr = VecCopy(ctx->D, ctx->U);CHKERRQ(ierr); } else if (ctx->mu) { ierr = VecAXPBY(ctx->D,ctx->mu, 1.0-ctx->mu,ctx->U);CHKERRQ(ierr); } break; case MatLMVM_Limit_Relative: if (ctx->mu) { /* P = (1-mu) * D */ ierr = VecAXPBY(ctx->P, 1.0-ctx->mu, 0.0, ctx->D);CHKERRQ(ierr); /* Q = (1+mu) * D */ ierr = VecAXPBY(ctx->Q, 1.0+ctx->mu, 0.0, ctx->D);CHKERRQ(ierr); ierr = VecMedian(ctx->P, ctx->U, ctx->Q, ctx->D);CHKERRQ(ierr); } break; case MatLMVM_Limit_Absolute: if (ctx->nu) { ierr = VecCopy(ctx->P, ctx->D);CHKERRQ(ierr); ierr = VecShift(ctx->P, -ctx->nu);CHKERRQ(ierr); ierr = VecCopy(ctx->D, ctx->Q);CHKERRQ(ierr); ierr = VecShift(ctx->Q, ctx->nu);CHKERRQ(ierr); ierr = VecMedian(ctx->P, ctx->U, ctx->Q, ctx->P);CHKERRQ(ierr); } break; default: ierr = VecCopy(ctx->U, ctx->D);CHKERRQ(ierr); break; } break; } ierr = PetscObjectDereference((PetscObject)ctx->Xprev);CHKERRQ(ierr); ierr = PetscObjectDereference((PetscObject)ctx->Gprev);CHKERRQ(ierr); ctx->Xprev = ctx->S[ctx->lm]; ctx->Gprev = ctx->Y[ctx->lm]; ierr = PetscObjectReference((PetscObject)ctx->S[ctx->lm]);CHKERRQ(ierr); ierr = PetscObjectReference((PetscObject)ctx->Y[ctx->lm]);CHKERRQ(ierr); } else { ++ctx->nrejects; } } ++ctx->iter; ierr = VecCopy(x, ctx->Xprev);CHKERRQ(ierr); ierr = VecCopy(g, ctx->Gprev);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode TaoSolve_BLMVM(Tao tao) { PetscErrorCode ierr; TAO_BLMVM *blmP = (TAO_BLMVM *)tao->data; TaoConvergedReason reason = TAO_CONTINUE_ITERATING; TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING; PetscReal f, fold, gdx, gnorm; PetscReal stepsize = 1.0,delta; PetscFunctionBegin; /* Project initial point onto bounds */ ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); ierr = VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);CHKERRQ(ierr); ierr = TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);CHKERRQ(ierr); /* Check convergence criteria */ ierr = TaoComputeObjectiveAndGradient(tao, tao->solution,&f,blmP->unprojected_gradient);CHKERRQ(ierr); ierr = VecBoundGradientProjection(blmP->unprojected_gradient,tao->solution, tao->XL,tao->XU,tao->gradient);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf pr NaN"); ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, stepsize, &reason);CHKERRQ(ierr); if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); /* Set initial scaling for the function */ if (f != 0.0) { delta = 2.0*PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(blmP->M,delta);CHKERRQ(ierr); /* Set counter for gradient/reset steps */ blmP->grad = 0; blmP->reset = 0; /* Have not converged; continue with Newton method */ while (reason == TAO_CONTINUE_ITERATING) { /* Compute direction */ ierr = MatLMVMUpdate(blmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(blmP->M, blmP->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); ierr = VecBoundGradientProjection(tao->stepdirection,tao->solution,tao->XL,tao->XU,tao->gradient);CHKERRQ(ierr); /* Check for success (descent direction) */ ierr = VecDot(blmP->unprojected_gradient, tao->gradient, &gdx);CHKERRQ(ierr); if (gdx <= 0) { /* Step is not descent or solve was not successful Use steepest descent direction (scaled) */ ++blmP->grad; if (f != 0.0) { delta = 2.0*PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(blmP->M,delta);CHKERRQ(ierr); ierr = MatLMVMReset(blmP->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(blmP->M, tao->solution, blmP->unprojected_gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(blmP->M,blmP->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); } ierr = VecScale(tao->stepdirection,-1.0);CHKERRQ(ierr); /* Perform the linesearch */ fold = f; ierr = VecCopy(tao->solution, blmP->Xold);CHKERRQ(ierr); ierr = VecCopy(blmP->unprojected_gradient, blmP->Gold);CHKERRQ(ierr); ierr = TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);CHKERRQ(ierr); ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, blmP->unprojected_gradient, tao->stepdirection, &stepsize, &ls_status);CHKERRQ(ierr); ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) { /* Linesearch failed Reset factors and use scaled (projected) gradient step */ ++blmP->reset; f = fold; ierr = VecCopy(blmP->Xold, tao->solution);CHKERRQ(ierr); ierr = VecCopy(blmP->Gold, blmP->unprojected_gradient);CHKERRQ(ierr); if (f != 0.0) { delta = 2.0* PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0/ (gnorm*gnorm); } ierr = MatLMVMSetDelta(blmP->M,delta);CHKERRQ(ierr); ierr = MatLMVMReset(blmP->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(blmP->M, tao->solution, blmP->unprojected_gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(blmP->M, blmP->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); /* This may be incorrect; linesearch has values fo stepmax and stepmin that should be reset. */ ierr = TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);CHKERRQ(ierr); ierr = TaoLineSearchApply(tao->linesearch,tao->solution,&f, blmP->unprojected_gradient, tao->stepdirection, &stepsize, &ls_status);CHKERRQ(ierr); ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) { tao->reason = TAO_DIVERGED_LS_FAILURE; break; } } /* Check for converged */ ierr = VecBoundGradientProjection(blmP->unprojected_gradient, tao->solution, tao->XL, tao->XU, tao->gradient);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient, NORM_2, &gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Not-a-Number"); tao->niter++; ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, stepsize, &reason);CHKERRQ(ierr); } PetscFunctionReturn(0); }