Lines Matching refs:tao
1 #include <../src/tao/unconstrained/impls/ntl/ntlimpl.h>
28 static PetscErrorCode TaoSolve_NTL(Tao tao)
30 TAO_NTL *tl = (TAO_NTL *)tao->data;
56 if (tao->XL || tao->XU || tao->ops->computebounds) PetscCall(PetscInfo(tao, "WARNING: Variable bounds have been set but will be ignored by ntl algorithm\n"));
58 PetscCall(KSPGetType(tao->ksp, &ksp_type));
62 PetscCheck(is_nash || is_stcg || is_gltr, PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "TAO_NTR requires nash, stcg, or gltr for the KSP");
65 tao->trust = tao->trust0;
66 tao->trust = PetscMax(tao->trust, tl->min_radius);
67 tao->trust = PetscMin(tao->trust, tl->max_radius);
70 PetscCall(KSPGetPC(tao->ksp, &pc));
76 PetscCall(VecGetLocalSize(tao->solution, &n));
77 PetscCall(VecGetSize(tao->solution, &N));
79 PetscCall(MatLMVMAllocate(tl->M, tao->solution, tao->gradient));
81 PetscCheck(sym_set && is_symmetric, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_INCOMP, "LMVM matrix in the LMVM preconditioner must be symmetric.");
85 PetscCall(TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient));
86 PetscCall(VecNorm(tao->gradient, NORM_2, &gnorm));
87 PetscCheck(!PetscIsInfOrNanReal(f) && !PetscIsInfOrNanReal(gnorm), PetscObjectComm((PetscObject)tao), PETSC_ERR_USER, "User provided compute function generated infinity or NaN");
90 tao->reason = TAO_CONTINUE_ITERATING;
91 PetscCall(TaoLogConvergenceHistory(tao, f, gnorm, 0.0, tao->ksp_its));
92 PetscCall(TaoMonitor(tao, tao->niter, f, gnorm, 0.0, step));
93 PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
94 if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);
111 PetscCall(TaoComputeHessian(tao, tao->solution, tao->hessian, tao->hessian_pre));
116 PetscCall(VecCopy(tao->solution, tl->W));
117 PetscCall(VecAXPY(tl->W, -tao->trust / gnorm, tao->gradient));
119 PetscCall(TaoComputeObjective(tao, tl->W, &ftrial));
125 sigma = -tao->trust / gnorm;
128 PetscCall(MatMult(tao->hessian, tao->gradient, tao->stepdirection));
129 PetscCall(VecDot(tao->gradient, tao->stepdirection, &prered));
131 prered = tao->trust * (gnorm - 0.5 * tao->trust * prered / (gnorm * gnorm));
139 tau_1 = tl->theta_i * gnorm * tao->trust / (tl->theta_i * gnorm * tao->trust + (1.0 - tl->theta_i) * prered - actred);
140 tau_2 = tl->theta_i * gnorm * tao->trust / (tl->theta_i * gnorm * tao->trust - (1.0 + tl->theta_i) * prered + actred);
146 max_radius = PetscMax(max_radius, tao->trust);
161 max_radius = PetscMax(max_radius, tao->trust);
187 tao->trust = tau * tao->trust;
192 PetscCall(VecAXPY(tao->solution, sigma, tao->gradient));
193 PetscCall(TaoComputeGradient(tao, tao->solution, tao->gradient));
195 PetscCall(VecNorm(tao->gradient, NORM_2, &gnorm));
196 PetscCheck(!PetscIsInfOrNanReal(f) && !PetscIsInfOrNanReal(gnorm), PetscObjectComm((PetscObject)tao), PETSC_ERR_USER, "User provided compute function generated infinity or NaN");
199 PetscCall(TaoLogConvergenceHistory(tao, f, gnorm, 0.0, tao->ksp_its));
200 PetscCall(TaoMonitor(tao, tao->niter, f, gnorm, 0.0, step));
201 PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
202 if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);
205 tao->trust = PetscMax(tao->trust, max_radius);
208 tao->trust = PetscMax(tao->trust, tl->min_radius);
209 tao->trust = PetscMin(tao->trust, tl->max_radius);
214 tao->trust = 0.0;
225 while (tao->reason == TAO_CONTINUE_ITERATING) {
227 if (tao->ops->update) {
228 PetscUseTypeMethod(tao, update, tao->niter, tao->user_update);
229 PetscCall(TaoComputeObjective(tao, tao->solution, &f));
231 ++tao->niter;
232 tao->ksp_its = 0;
234 if (needH) PetscCall(TaoComputeHessian(tao, tao->solution, tao->hessian, tao->hessian_pre));
238 PetscCall(MatLMVMUpdate(tl->M, tao->solution, tao->gradient));
241 PetscCall(KSPSetOperators(tao->ksp, tao->hessian, tao->hessian_pre));
243 PetscCall(KSPCGSetRadius(tao->ksp, tl->max_radius));
244 PetscCall(KSPSolve(tao->ksp, tao->gradient, tao->stepdirection));
245 PetscCall(KSPGetIterationNumber(tao->ksp, &its));
246 tao->ksp_its += its;
247 tao->ksp_tot_its += its;
248 PetscCall(KSPCGGetNormD(tao->ksp, &norm_d));
250 if (0.0 == tao->trust) {
253 tao->trust = norm_d;
256 tao->trust = PetscMax(tao->trust, tl->min_radius);
257 tao->trust = PetscMin(tao->trust, tl->max_radius);
261 tao->trust = tao->trust0;
264 tao->trust = PetscMax(tao->trust, tl->min_radius);
265 tao->trust = PetscMin(tao->trust, tl->max_radius);
267 PetscCall(KSPCGSetRadius(tao->ksp, tl->max_radius));
268 PetscCall(KSPSolve(tao->ksp, tao->gradient, tao->stepdirection));
269 PetscCall(KSPGetIterationNumber(tao->ksp, &its));
270 tao->ksp_its += its;
271 tao->ksp_tot_its += its;
272 PetscCall(KSPCGGetNormD(tao->ksp, &norm_d));
274 PetscCheck(norm_d != 0.0, PetscObjectComm((PetscObject)tao), PETSC_ERR_PLIB, "Initial direction zero");
278 PetscCall(VecScale(tao->stepdirection, -1.0));
279 PetscCall(KSPGetConvergedReason(tao->ksp, &ksp_reason));
284 PetscCall(MatLMVMUpdate(tl->M, tao->solution, tao->gradient));
292 PetscCall(KSPCGGetObjFcn(tao->ksp, &prered));
297 tao->trust = tl->alpha1 * PetscMin(tao->trust, norm_d);
301 PetscCall(VecCopy(tao->solution, tl->W));
302 PetscCall(VecAXPY(tl->W, 1.0, tao->stepdirection));
303 PetscCall(TaoComputeObjective(tao, tl->W, &ftrial));
306 tao->trust = tl->alpha1 * PetscMin(tao->trust, norm_d);
321 tao->trust = tl->alpha1 * PetscMin(tao->trust, norm_d);
327 tao->trust = tl->alpha2 * PetscMin(tao->trust, norm_d);
330 tao->trust = tl->alpha3 * tao->trust;
333 tao->trust = PetscMax(tl->alpha4 * norm_d, tao->trust);
336 tao->trust = PetscMax(tl->alpha5 * norm_d, tao->trust);
343 PetscCall(KSPCGGetObjFcn(tao->ksp, &prered));
348 tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
351 PetscCall(VecCopy(tao->solution, tl->W));
352 PetscCall(VecAXPY(tl->W, 1.0, tao->stepdirection));
353 PetscCall(TaoComputeObjective(tao, tl->W, &ftrial));
355 tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
358 PetscCall(VecDot(tao->gradient, tao->stepdirection, &gdx));
376 tao->trust = PetscMax(tao->trust, tl->gamma3 * norm_d);
378 tao->trust = PetscMax(tao->trust, tl->gamma4 * norm_d);
380 tao->trust = PetscMax(tao->trust, tau_max * norm_d);
386 tao->trust = tl->gamma2 * PetscMin(tao->trust, norm_d);
388 tao->trust = PetscMax(tao->trust, tl->gamma3 * norm_d);
390 tao->trust = tau_max * PetscMin(tao->trust, norm_d);
392 tao->trust = PetscMax(tao->trust, tau_max * norm_d);
397 tao->trust = tl->gamma2 * PetscMin(tao->trust, norm_d);
399 tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
401 tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
403 tao->trust = tau_1 * PetscMin(tao->trust, norm_d);
405 tao->trust = tau_2 * PetscMin(tao->trust, norm_d);
407 tao->trust = tau_max * PetscMin(tao->trust, norm_d);
418 PetscCall(VecDot(tao->stepdirection, tao->gradient, &gdx));
425 PetscCall(VecCopy(tao->gradient, tao->stepdirection));
426 PetscCall(VecScale(tao->stepdirection, -1.0));
431 PetscCall(MatSolve(tl->M, tao->gradient, tao->stepdirection));
432 PetscCall(VecScale(tao->stepdirection, -1.0));
435 PetscCall(VecDot(tao->stepdirection, tao->gradient, &gdx));
444 PetscCall(MatLMVMUpdate(tl->M, tao->solution, tao->gradient));
445 PetscCall(MatSolve(tl->M, tao->gradient, tao->stepdirection));
446 PetscCall(VecScale(tao->stepdirection, -1.0));
471 PetscCall(VecCopy(tao->solution, tl->Xold));
472 PetscCall(VecCopy(tao->gradient, tl->Gold));
475 PetscCall(TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, tao->stepdirection, &step, &ls_reason));
476 PetscCall(TaoAddLineSearchCounts(tao));
481 PetscCall(VecCopy(tl->Xold, tao->solution));
482 PetscCall(VecCopy(tl->Gold, tao->gradient));
491 PetscCall(VecCopy(tao->gradient, tao->stepdirection));
496 PetscCall(MatSolve(tl->M, tao->gradient, tao->stepdirection));
499 PetscCall(VecDot(tao->stepdirection, tao->gradient, &gdx));
505 PetscCall(MatLMVMUpdate(tl->M, tao->solution, tao->gradient));
506 PetscCall(MatSolve(tl->M, tao->gradient, tao->stepdirection));
530 PetscCall(MatLMVMUpdate(tl->M, tao->solution, tao->gradient));
531 PetscCall(MatSolve(tl->M, tao->gradient, tao->stepdirection));
538 PetscCall(VecScale(tao->stepdirection, -1.0));
543 PetscCall(TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, tao->stepdirection, &step, &ls_reason));
544 PetscCall(TaoAddLineSearchCounts(tao));
550 PetscCall(VecCopy(tl->Xold, tao->solution));
551 PetscCall(VecCopy(tl->Gold, tao->gradient));
552 tao->trust = 0.0;
554 tao->reason = TAO_DIVERGED_LS_FAILURE;
559 tao->trust = tl->omega1 * PetscMin(norm_d, tao->trust);
562 tao->trust = tl->omega2 * PetscMin(norm_d, tao->trust);
566 tao->trust = tl->omega3 * PetscMin(norm_d, tao->trust);
568 tao->trust = PetscMax(tl->omega3 * norm_d, tao->trust);
572 tao->trust = PetscMax(tl->omega4 * norm_d, tao->trust);
575 tao->trust = PetscMax(tl->omega5 * norm_d, tao->trust);
579 tao->trust = tl->omega1 * PetscMin(norm_d, tao->trust);
583 PetscCall(VecCopy(tl->W, tao->solution));
585 PetscCall(TaoComputeGradient(tao, tao->solution, tao->gradient));
590 tao->trust = PetscMin(tao->trust, tl->max_radius);
593 PetscCall(VecNorm(tao->gradient, NORM_2, &gnorm));
594 PetscCheck(!PetscIsInfOrNanReal(f) && !PetscIsInfOrNanReal(gnorm), PetscObjectComm((PetscObject)tao), PETSC_ERR_USER, "User provided compute function generated Not-a-Number");
597 PetscCall(TaoLogConvergenceHistory(tao, f, gnorm, 0.0, tao->ksp_its));
598 PetscCall(TaoMonitor(tao, tao->niter, f, gnorm, 0.0, step));
599 PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
604 static PetscErrorCode TaoSetUp_NTL(Tao tao)
606 TAO_NTL *tl = (TAO_NTL *)tao->data;
609 if (!tao->gradient) PetscCall(VecDuplicate(tao->solution, &tao->gradient));
610 if (!tao->stepdirection) PetscCall(VecDuplicate(tao->solution, &tao->stepdirection));
611 if (!tl->W) PetscCall(VecDuplicate(tao->solution, &tl->W));
612 if (!tl->Xold) PetscCall(VecDuplicate(tao->solution, &tl->Xold));
613 if (!tl->Gold) PetscCall(VecDuplicate(tao->solution, &tl->Gold));
619 static PetscErrorCode TaoDestroy_NTL(Tao tao)
621 TAO_NTL *tl = (TAO_NTL *)tao->data;
624 if (tao->setupcalled) {
629 PetscCall(KSPDestroy(&tao->ksp));
630 PetscCall(PetscFree(tao->data));
634 static PetscErrorCode TaoSetFromOptions_NTL(Tao tao, PetscOptionItems PetscOptionsObject)
636 TAO_NTL *tl = (TAO_NTL *)tao->data;
678 PetscCall(TaoLineSearchSetFromOptions(tao->linesearch));
679 PetscCall(KSPSetFromOptions(tao->ksp));
683 static PetscErrorCode TaoView_NTL(Tao tao, PetscViewer viewer)
685 TAO_NTL *tl = (TAO_NTL *)tao->data;
740 PETSC_EXTERN PetscErrorCode TaoCreate_NTL(Tao tao)
747 tao->ops->setup = TaoSetUp_NTL;
748 tao->ops->solve = TaoSolve_NTL;
749 tao->ops->view = TaoView_NTL;
750 tao->ops->setfromoptions = TaoSetFromOptions_NTL;
751 tao->ops->destroy = TaoDestroy_NTL;
754 PetscCall(TaoParametersInitialize(tao));
755 PetscObjectParameterSetDefault(tao, max_it, 50);
756 PetscObjectParameterSetDefault(tao, trust0, 100.0);
758 tao->data = (void *)tl;
814 PetscCall(TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch));
815 PetscCall(PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1));
816 PetscCall(TaoLineSearchSetType(tao->linesearch, morethuente_type));
817 PetscCall(TaoLineSearchUseTaoRoutines(tao->linesearch, tao));
818 PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, tao->hdr.prefix));
819 PetscCall(KSPCreate(((PetscObject)tao)->comm, &tao->ksp));
820 PetscCall(PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1));
821 PetscCall(KSPSetOptionsPrefix(tao->ksp, tao->hdr.prefix));
822 PetscCall(KSPAppendOptionsPrefix(tao->ksp, "tao_ntl_"));
823 PetscCall(KSPSetType(tao->ksp, KSPSTCG));