44 #ifndef ROL_TYPEB_SPECTRALGRADIENTALGORITHM_DEF_HPP
45 #define ROL_TYPEB_SPECTRALGRADIENTALGORITHM_DEF_HPP
52 template<
typename Real>
59 ParameterList &lslist = list.sublist(
"Step").sublist(
"Spectral Gradient");
60 maxit_ = lslist.get(
"Function Evaluation Limit", 20);
61 lambda_ = lslist.get(
"Initial Spectral Step Size", -1.0);
62 lambdaMin_ = lslist.get(
"Minimum Spectral Step Size", 1e-8);
63 lambdaMax_ = lslist.get(
"Maximum Spectral Step Size", 1e8);
64 sigma1_ = lslist.get(
"Lower Step Size Safeguard", 0.1);
65 sigma2_ = lslist.get(
"Upper Step Size Safeguard", 0.9);
66 rhodec_ = lslist.get(
"Backtracking Rate", 0.5);
67 gamma_ = lslist.get(
"Sufficient Decrease Tolerance", 1e-4);
68 maxSize_ = lslist.get(
"Maximum Storage Size", 10);
69 verbosity_ = list.sublist(
"General").get(
"Output Level", 0);
70 writeHeader_ = verbosity_ > 2;
73 template<
typename Real>
78 std::ostream &outStream) {
79 const Real
zero(0), one(1);
81 proj_ = makePtr<PolyhedralProjection<Real>>(makePtrFromRef(bnd));
85 Real ftol = std::sqrt(ROL_EPSILON<Real>());
86 proj_->project(x,outStream); state_->nproj++;
88 state_->value = obj.
value(x,ftol); state_->nfval++;
89 obj.
gradient(*state_->gradientVec,x,ftol); state_->ngrad++;
90 state_->stepVec->set(x);
91 state_->stepVec->axpy(-one,state_->gradientVec->dual());
92 proj_->project(*state_->stepVec,outStream); state_->nproj++;
93 state_->stepVec->axpy(-one,x);
94 state_->gnorm = state_->stepVec->norm();
95 state_->snorm = ROL_INF<Real>();
96 if (lambda_ <= zero && state_->gnorm !=
zero)
97 lambda_ = std::max(lambdaMin_,std::min(one/state_->gnorm,lambdaMax_));
100 template<
typename Real>
105 std::ostream &outStream ) {
106 const Real half(0.5), one(1), eps(std::sqrt(ROL_EPSILON<Real>()));
108 initialize(x,g,obj,bnd,outStream);
110 Real ftrial(0), fmax(0), gs(0), alpha(1), alphaTmp(1), fmin(0);
111 Real ys(0), ss(0), tol(std::sqrt(ROL_EPSILON<Real>()));
113 std::deque<Real> fqueue; fqueue.push_back(state_->value);
115 fmin = state_->value;
119 if (verbosity_ > 0) writeOutput(outStream,
true);
122 state_->stepVec->set(state_->gradientVec->dual());
123 while (status_->check(*state_)) {
125 state_->iterateVec->set(x);
126 state_->iterateVec->axpy(-lambda_,*state_->stepVec);
127 proj_->project(*state_->iterateVec,outStream); state_->nproj++;
128 s->set(*state_->iterateVec);
134 ftrial = obj.
value(*state_->iterateVec,tol); ls_nfval++;
136 fmax = *std::max_element(fqueue.begin(),fqueue.end());
137 gs = state_->gradientVec->apply(*s);
138 if (verbosity_ > 1) {
139 outStream <<
" In TypeB::SpectralGradientAlgorithm Line Search" << std::endl;
140 outStream <<
" Step size: " << alpha << std::endl;
141 outStream <<
" Trial objective value: " << ftrial << std::endl;
142 outStream <<
" Max stored objective value: " << fmax << std::endl;
143 outStream <<
" Computed reduction: " << fmax-ftrial << std::endl;
144 outStream <<
" Dot product of gradient and step: " << gs << std::endl;
145 outStream <<
" Sufficient decrease bound: " << -gs*gamma_*alpha << std::endl;
146 outStream <<
" Number of function evaluations: " << ls_nfval << std::endl;
148 while (ftrial > fmax + gamma_*alpha*gs && ls_nfval < maxit_) {
149 alphaTmp = -half*alpha*alpha*gs/(ftrial-state_->value-alpha*gs);
150 alpha = (sigma1_*alpha <= alphaTmp && alphaTmp <= sigma2_*alpha) ? alphaTmp : rhodec_*alpha;
151 state_->iterateVec->set(x);
152 state_->iterateVec->axpy(alpha,*s);
154 ftrial = obj.
value(*state_->iterateVec,tol); ls_nfval++;
155 if (verbosity_ > 1) {
156 outStream <<
" In TypeB::SpectralGradientAlgorithm: Line Search" << std::endl;
157 outStream <<
" Step size: " << alpha << std::endl;
158 outStream <<
" Trial objective value: " << ftrial << std::endl;
159 outStream <<
" Max stored objective value: " << fmax << std::endl;
160 outStream <<
" Computed reduction: " << fmax-ftrial << std::endl;
161 outStream <<
" Dot product of gradient and step: " << gs << std::endl;
162 outStream <<
" Sufficient decrease bound: " << -gs*gamma_*alpha << std::endl;
163 outStream <<
" Number of function evaluations: " << ls_nfval << std::endl;
166 state_->nfval += ls_nfval;
167 if (static_cast<int>(fqueue.size()) == maxSize_) fqueue.pop_front();
168 fqueue.push_back(ftrial);
172 state_->value = ftrial;
173 state_->searchSize = alpha;
174 x.
set(*state_->iterateVec);
178 if (state_->value <= fmin) {
179 fmin = state_->value;
185 y->set(*state_->gradientVec);
187 obj.
gradient(*state_->gradientVec,x,tol); state_->ngrad++;
188 y->plus(*state_->gradientVec);
191 lambda_ = (ys<=eps ? lambdaMax_ : std::max(lambdaMin_,std::min(ss/ys,lambdaMax_)));
192 state_->snorm = std::sqrt(ss);
195 state_->stepVec->set(state_->gradientVec->dual());
198 s->set(x); s->axpy(-one,*state_->stepVec);
199 proj_->project(*s,outStream); state_->nproj++;
201 state_->gnorm = s->norm();
204 if (verbosity_ > 0) writeOutput(outStream,writeHeader_);
207 state_->value = fmin;
211 template<
typename Real>
213 std::ios_base::fmtflags osFlags(os.flags());
214 if (verbosity_ > 1) {
215 os << std::string(109,
'-') << std::endl;
216 os <<
"Spectral projected gradient descent";
217 os <<
" status output definitions" << std::endl << std::endl;
218 os <<
" iter - Number of iterates (steps taken)" << std::endl;
219 os <<
" value - Objective function value" << std::endl;
220 os <<
" gnorm - Norm of the gradient" << std::endl;
221 os <<
" snorm - Norm of the step (update to optimization vector)" << std::endl;
222 os <<
" alpha - Line search step length" << std::endl;
223 os <<
" lambda - Spectral step length" << std::endl;
224 os <<
" #fval - Cumulative number of times the objective function was evaluated" << std::endl;
225 os <<
" #grad - Cumulative number of times the gradient was computed" << std::endl;
226 os <<
" #proj - Cumulative number of times the projection was computed" << std::endl;
227 os << std::string(109,
'-') << std::endl;
231 os << std::setw(6) << std::left <<
"iter";
232 os << std::setw(15) << std::left <<
"value";
233 os << std::setw(15) << std::left <<
"gnorm";
234 os << std::setw(15) << std::left <<
"snorm";
235 os << std::setw(15) << std::left <<
"alpha";
236 os << std::setw(15) << std::left <<
"lambda";
237 os << std::setw(10) << std::left <<
"#fval";
238 os << std::setw(10) << std::left <<
"#grad";
239 os << std::setw(10) << std::left <<
"#proj";
244 template<
typename Real>
246 std::ios_base::fmtflags osFlags(os.flags());
247 os << std::endl <<
"Projected Spectral Gradient Method (Type B, Bound Constraints)" << std::endl;
251 template<
typename Real>
253 std::ios_base::fmtflags osFlags(os.flags());
254 os << std::scientific << std::setprecision(6);
255 if ( state_->iter == 0 ) writeName(os);
256 if ( write_header ) writeHeader(os);
257 if ( state_->iter == 0 ) {
259 os << std::setw(6) << std::left << state_->iter;
260 os << std::setw(15) << std::left << state_->value;
261 os << std::setw(15) << std::left << state_->gnorm;
262 os << std::setw(15) << std::left <<
"---";
263 os << std::setw(15) << std::left <<
"---";
264 os << std::setw(15) << std::left << lambda_;
265 os << std::setw(10) << std::left << state_->nfval;
266 os << std::setw(10) << std::left << state_->ngrad;
267 os << std::setw(10) << std::left << state_->nproj;
272 os << std::setw(6) << std::left << state_->iter;
273 os << std::setw(15) << std::left << state_->value;
274 os << std::setw(15) << std::left << state_->gnorm;
275 os << std::setw(15) << std::left << state_->snorm;
276 os << std::setw(15) << std::left << state_->searchSize;
277 os << std::setw(15) << std::left << lambda_;
278 os << std::setw(10) << std::left << state_->nfval;
279 os << std::setw(10) << std::left << state_->ngrad;
280 os << std::setw(10) << std::left << state_->nproj;
Provides the interface to evaluate objective functions.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
virtual void writeExitStatus(std::ostream &os) const
Defines the linear algebra or vector space interface.
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void writeOutput(std::ostream &os, const bool write_header=false) const override
Print iterate status.
void writeName(std::ostream &os) const override
Print step name.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
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...
void initialize(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout)
void writeHeader(std::ostream &os) const override
Print iterate header.
Provides an interface to check status of optimization algorithms.
Provides the interface to apply upper and lower bound constraints.
SpectralGradientAlgorithm(ParameterList &list)
void initialize(const Vector< Real > &x, const Vector< Real > &g)
virtual void set(const Vector &x)
Set where .