44 #ifndef ROL_TYPEB_NEWTONKRYLOVALGORITHM_DEF_HPP
45 #define ROL_TYPEB_NEWTONKRYLOVALGORITHM_DEF_HPP
50 template<
typename Real>
57 secantName_ = list.sublist(
"General").sublist(
"Secant").get(
"Type",
"Limited-Memory BFGS");
59 secant_ = SecantFactory<Real>(list);
62 secantName_ = list.sublist(
"General").sublist(
"Secant").get(
"User Defined Secant Name",
63 "Unspecified User Defined Secant Method");
66 krylovName_ = list.sublist(
"General").sublist(
"Krylov").get(
"Type",
"Conjugate Gradients");
68 krylov_ = KrylovFactory<Real>(list);
71 template<
typename Real>
79 secantName_ = list.sublist(
"General").sublist(
"Secant").get(
"Type",
"Limited-Memory BFGS");
81 secant_ = SecantFactory<Real>(list);
84 secantName_ = list.sublist(
"General").sublist(
"Secant").get(
"User Defined Secant Name",
85 "Unspecified User Defined Secant Method");
88 krylovName_ = list.sublist(
"General").sublist(
"Krylov").get(
"User Defined Krylov Name",
89 "Unspecified User Defined Krylov Method");
92 template<
typename Real>
99 ParameterList &lslist = list.sublist(
"Step").sublist(
"Line Search");
100 maxit_ = lslist.get(
"Function Evaluation Limit", 20);
101 alpha0_ = lslist.get(
"Initial Step Size", 1.0);
102 useralpha_ = lslist.get(
"User Defined Initial Step Size",
false);
103 usePrevAlpha_ = lslist.get(
"Use Previous Step Length as Initial Guess",
false);
104 c1_ = lslist.get(
"Sufficient Decrease Tolerance", 1e-4);
105 rhodec_ = lslist.sublist(
"Line-Search Method").get(
"Backtracking Rate", 0.5);
107 useSecantHessVec_ = list.sublist(
"General").sublist(
"Secant").get(
"Use as Hessian",
false);
108 useSecantPrecond_ = list.sublist(
"General").sublist(
"Secant").get(
"Use as Preconditioner",
false);
110 verbosity_ = list.sublist(
"General").get(
"Output Level", 0);
111 writeHeader_ = verbosity_ > 2;
114 template<
typename Real>
119 std::ostream &outStream) {
121 if (proj_ == nullPtr) {
122 proj_ = makePtr<PolyhedralProjection<Real>>(makePtrFromRef(bnd));
127 Real ftol = std::sqrt(ROL_EPSILON<Real>());
128 proj_->project(x,outStream);
129 state_->iterateVec->set(x);
131 state_->value = obj.
value(x,ftol); state_->nfval++;
132 obj.
gradient(*state_->gradientVec,x,ftol); state_->ngrad++;
133 state_->stepVec->set(x);
134 state_->stepVec->axpy(-one,state_->gradientVec->dual());
135 proj_->project(*state_->stepVec,outStream);
136 state_->stepVec->axpy(-one,x);
137 state_->gnorm = state_->stepVec->norm();
138 state_->snorm = ROL_INF<Real>();
139 if (!useralpha_) alpha0_ = one;
140 state_->searchSize = alpha0_;
143 template<
typename Real>
148 std::ostream &outStream ) {
151 initialize(x,g,obj,bnd,outStream);
153 Ptr<Vector<Real>> pwa = x.
clone(), pwa1 = x.
clone();
154 Real ftrial(0), gs(0), tol(std::sqrt(ROL_EPSILON<Real>()));
156 Ptr<LinearOperator<Real>> hessian, precond;
159 if (verbosity_ > 0) writeOutput(outStream,
true);
162 gp->set(state_->gradientVec->dual());
163 while (status_->check(*state_)) {
165 hessian = makePtr<HessianPNK>(makePtrFromRef(obj),makePtrFromRef(bnd),
166 state_->iterateVec,state_->gradientVec,state_->gnorm,
167 secant_,useSecantHessVec_,pwa);
168 precond = makePtr<PrecondPNK>(makePtrFromRef(obj),makePtrFromRef(bnd),
169 state_->iterateVec,state_->gradientVec,state_->gnorm,
170 secant_,useSecantPrecond_,pwa1);
172 krylov_->run(*s,*hessian,*state_->gradientVec,*precond,iterKrylov_,flagKrylov_);
173 if (flagKrylov_ == 2 && iterKrylov_ <= 1) {
177 if (!usePrevAlpha_) state_->searchSize = alpha0_;
178 x.
set(*state_->iterateVec);
179 x.
axpy(-state_->searchSize,*s);
180 proj_->project(x,outStream);
182 ftrial = obj.
value(x,tol); ls_nfval_ = 1;
183 state_->stepVec->set(x);
184 state_->stepVec->axpy(-one,*state_->iterateVec);
185 gs = state_->stepVec->dot(*gp);
186 if (verbosity_ > 1) {
187 outStream <<
" In TypeB::NewtonKrylovAlgorithm: Line Search" << std::endl;
188 outStream <<
" Step size: " << state_->searchSize << std::endl;
189 outStream <<
" Trial objective value: " << ftrial << std::endl;
190 outStream <<
" Computed reduction: " << state_->value-ftrial << std::endl;
191 outStream <<
" Dot product of gradient and step: " << gs << std::endl;
192 outStream <<
" Sufficient decrease bound: " << -gs*c1_ << std::endl;
193 outStream <<
" Number of function evaluations: " << ls_nfval_ << std::endl;
195 while ( state_->value - ftrial < -c1_*gs && ls_nfval_ < maxit_ ) {
196 state_->searchSize *= rhodec_;
197 x.
set(*state_->iterateVec);
198 x.
axpy(-state_->searchSize,*s);
199 proj_->project(x,outStream);
201 ftrial = obj.
value(x,tol); ls_nfval_++;
202 state_->stepVec->set(x);
203 state_->stepVec->axpy(-one,*state_->iterateVec);
204 gs = state_->stepVec->dot(*gp);
205 if (verbosity_ > 1) {
206 outStream << std::endl;
207 outStream <<
" Step size: " << state_->searchSize << std::endl;
208 outStream <<
" Trial objective value: " << ftrial << std::endl;
209 outStream <<
" Computed reduction: " << state_->value-ftrial << std::endl;
210 outStream <<
" Dot product of gradient and step: " << gs << std::endl;
211 outStream <<
" Sufficient decrease bound: " << -gs*c1_ << std::endl;
212 outStream <<
" Number of function evaluations: " << ls_nfval_ << std::endl;
215 state_->nfval += ls_nfval_;
218 state_->snorm = state_->stepVec->norm();
221 state_->iterateVec->set(x);
225 state_->value = ftrial;
227 gold->set(*state_->gradientVec);
228 obj.
gradient(*state_->gradientVec,x,tol); state_->ngrad++;
229 gp->set(state_->gradientVec->dual());
232 s->set(x); s->axpy(-one,*gp);
233 proj_->project(*s,outStream);
235 state_->gnorm = s->norm();
238 secant_->updateStorage(x,*state_->gradientVec,*gold,*state_->stepVec,state_->snorm,state_->iter);
241 if (verbosity_ > 0) writeOutput(outStream,writeHeader_);
246 template<
typename Real>
248 std::ostream &outStream ) {
253 throw Exception::NotImplemented(
">>> TypeB::NewtonKrylovAlgorithm::run : This algorithm cannot solve problems with linear equality constraints!");
257 template<
typename Real>
265 std::ostream &outStream ) {
266 throw Exception::NotImplemented(
">>> TypeB::NewtonKrylovAlgorithm::run : This algorithm cannot solve problems with linear equality constraints!");
269 template<
typename Real>
271 std::ios_base::fmtflags osFlags(os.flags());
272 if (verbosity_ > 1) {
273 os << std::string(114,
'-') << std::endl;
274 if (!useSecantHessVec_) {
275 os <<
"Line-Search Projected Newton";
278 os <<
"Line-Search Projected Quasi-Newton with " << secantName_ <<
" Hessian approximation";
280 os <<
" status output definitions" << std::endl << std::endl;
281 os <<
" iter - Number of iterates (steps taken)" << std::endl;
282 os <<
" value - Objective function value" << std::endl;
283 os <<
" gnorm - Norm of the gradient" << std::endl;
284 os <<
" snorm - Norm of the step (update to optimization vector)" << std::endl;
285 os <<
" alpha - Line search step length" << std::endl;
286 os <<
" #fval - Cumulative number of times the objective function was evaluated" << std::endl;
287 os <<
" #grad - Cumulative number of times the gradient was computed" << std::endl;
288 os <<
" ls_#fval - Number of the times the objective function was evaluated during the line search" << std::endl;
289 os <<
" iterCG - Number of Krylov iterations" << std::endl << std::endl;
290 os <<
" flagGC - Krylov flag" << std::endl;
295 os << std::string(114,
'-') << std::endl;
299 os << std::setw(6) << std::left <<
"iter";
300 os << std::setw(15) << std::left <<
"value";
301 os << std::setw(15) << std::left <<
"gnorm";
302 os << std::setw(15) << std::left <<
"snorm";
303 os << std::setw(15) << std::left <<
"alpha";
304 os << std::setw(10) << std::left <<
"#fval";
305 os << std::setw(10) << std::left <<
"#grad";
306 os << std::setw(10) << std::left <<
"#ls_fval";
307 os << std::setw(10) << std::left <<
"iterCG";
308 os << std::setw(10) << std::left <<
"flagCG";
313 template<
typename Real>
315 std::ios_base::fmtflags osFlags(os.flags());
316 if (!useSecantHessVec_) {
317 os << std::endl <<
"Line-Search Projected Newton (Type B, Bound Constraints)" << std::endl;
320 os << std::endl <<
"Line-Search Projected Quasi-Newton with "
321 << secantName_ <<
" Hessian approximation" << std::endl;
326 template<
typename Real>
328 std::ios_base::fmtflags osFlags(os.flags());
329 os << std::scientific << std::setprecision(6);
330 if ( state_->iter == 0 ) writeName(os);
331 if ( write_header ) writeHeader(os);
332 if ( state_->iter == 0 ) {
334 os << std::setw(6) << std::left << state_->iter;
335 os << std::setw(15) << std::left << state_->value;
336 os << std::setw(15) << std::left << state_->gnorm;
337 os << std::setw(15) << std::left <<
"---";
338 os << std::setw(15) << std::left <<
"---";
339 os << std::setw(10) << std::left << state_->nfval;
340 os << std::setw(10) << std::left << state_->ngrad;
341 os << std::setw(10) << std::left <<
"---";
342 os << std::setw(10) << std::left <<
"---";
343 os << std::setw(10) << std::left <<
"---";
348 os << std::setw(6) << std::left << state_->iter;
349 os << std::setw(15) << std::left << state_->value;
350 os << std::setw(15) << std::left << state_->gnorm;
351 os << std::setw(15) << std::left << state_->snorm;
352 os << std::setw(15) << std::left << state_->searchSize;
353 os << std::setw(10) << std::left << state_->nfval;
354 os << std::setw(10) << std::left << state_->ngrad;
355 os << std::setw(10) << std::left << ls_nfval_;
356 os << std::setw(10) << std::left << iterKrylov_;
357 os << std::setw(10) << std::left << flagKrylov_;
std::string ECGFlagToString(ECGFlag cgf)
Provides the interface to evaluate objective functions.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void writeHeader(std::ostream &os) const override
Print iterate header.
NewtonKrylovAlgorithm(ParameterList &list, const Ptr< Secant< Real >> &secant=nullPtr)
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
void initialize(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout)
virtual void writeExitStatus(std::ostream &os) const
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout) override
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
ESecant StringToESecant(std::string s)
Defines the linear algebra or vector space interface.
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
EKrylov StringToEKrylov(std::string s)
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
std::string NumberToString(T Number)
std::string krylovName_
Krylov name.
void writeOutput(std::ostream &os, const bool write_header=false) const override
Print iterate status.
Provides interface for and implements limited-memory secant operators.
std::string secantName_
Secant name.
Provides an interface to check status of optimization algorithms.
Provides definitions for Krylov solvers.
Provides the interface to apply upper and lower bound constraints.
const Ptr< PolyhedralProjection< Real > > & getPolyhedralProjection()
Get the polyhedral projection object. This is a null pointer if no linear constraints and/or bounds a...
Ptr< Secant< Real > > secant_
Secant object (used for quasi-Newton)
void parseParameterList(ParameterList &list)
void initialize(const Vector< Real > &x, const Vector< Real > &g)
Ptr< Krylov< Real > > krylov_
Krylov solver object (used for inexact Newton)
ESecant esec_
Secant type.
virtual void run(Problem< Real > &problem, std::ostream &outStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This is the primary Type-B interface.
virtual void set(const Vector &x)
Set where .
void writeName(std::ostream &os) const override
Print step name.
Defines the general constraint operator interface.