19 #include "Teuchos_GlobalMPISession.hpp"
29 template <
class Real,
class Element=Real>
32 template <
class Real,
class Element=Real>
39 template <
class Real,
class Element>
49 mutable ROL::Ptr<OptDualStdVector<Real> >
dual_vec_;
60 (*std_vec_)[i] += (*xvalptr)[i];
64 void scale(
const Real alpha ) {
67 (*std_vec_)[i] *= alpha;
77 val += (*std_vec_)[i]*(*xvalptr)[i];
84 val = std::sqrt(
dot(*
this) );
88 ROL::Ptr<ROL::Vector<Real> >
clone()
const {
89 return ROL::makePtr<OptStdVector>( ROL::makePtr<std::vector<Element>>(
std_vec_->size()) );
92 ROL::Ptr<const std::vector<Element> >
getVector()
const {
100 ROL::Ptr<ROL::Vector<Real> >
basis(
const int i )
const {
102 ROL::Ptr<vector> e_ptr = ROL::makePtr<vector>(
std_vec_->size(),0.0);
105 ROL::Ptr<V> e = ROL::makePtr<OptStdVector>( e_ptr );
113 dual_vec_ = ROL::makePtr<OptDualStdVector<Real>>( ROL::makePtr<std::vector<Element>>(*std_vec_) );
123 val += (*std_vec_)[i]*(*xvalptr)[i];
132 template <
class Real,
class Element>
154 (*std_vec_)[i] += (*xvalptr)[i];
161 (*std_vec_)[i] *= alpha;
171 val += (*std_vec_)[i]*(*xvalptr)[i];
178 val = std::sqrt(
dot(*
this) );
182 ROL::Ptr<ROL::Vector<Real> >
clone()
const {
183 return ROL::makePtr<OptDualStdVector>( ROL::makePtr<std::vector<Element>>(
std_vec_->size()) );
186 ROL::Ptr<const std::vector<Element> >
getVector()
const {
194 ROL::Ptr<ROL::Vector<Real> >
basis(
const int i )
const {
196 ROL::Ptr<vector> e_ptr = ROL::makePtr<vector>(
std_vec_->size(),0.0);
199 ROL::Ptr<V> e = ROL::makePtr<OptDualStdVector>( e_ptr );
206 dual_vec_ = ROL::makePtr<OptStdVector<Real>>( ROL::makePtr<std::vector<Element>>(*std_vec_) );
216 val += (*std_vec_)[i]*(*xvalptr)[i];
231 int main(
int argc,
char *argv[]) {
233 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
236 int iprint = argc - 1;
237 ROL::Ptr<std::ostream> outStream;
240 outStream = ROL::makePtrFromRef(std::cout);
242 outStream = ROL::makePtrFromRef(bhs);
256 ROL::Ptr<std::vector<RealT>> x_ptr = ROL::makePtr<std::vector<RealT>>(
dim, 0.0);
257 ROL::Ptr<std::vector<RealT>> g_ptr = ROL::makePtr<std::vector<RealT>>(
dim, 0.0);
259 for (
int i=0; i<dim/2; i++) {
260 (*x_ptr)[2*i] = -1.2;
261 (*x_ptr)[2*i+1] = 1.0;
265 ROL::Ptr<OptStdVector<RealT>> x = ROL::makePtr<OptStdVector<RealT>>(x_ptr);
266 ROL::Ptr<OptDualStdVector<RealT>> g = ROL::makePtr<OptDualStdVector<RealT>>(g_ptr);
269 ROL::Ptr<std::vector<RealT> > aa_ptr = ROL::makePtr<std::vector<RealT>>(1, 1.0);
271 ROL::Ptr<std::vector<RealT> > bb_ptr = ROL::makePtr<std::vector<RealT>>(1, 2.0);
273 ROL::Ptr<std::vector<RealT> > cc_ptr = ROL::makePtr<std::vector<RealT>>(1, 3.0);
275 std::vector<RealT> std_vec_err = av.
checkVector(bv,cv,
true,*outStream);
278 ROL::Ptr<ROL::Problem<RealT>> problem
279 = ROL::makePtr<ROL::Problem<RealT>>(obj,x,g);
280 problem->finalize(
false,
true,*outStream);
283 ROL::ParameterList parlist;
284 std::string stepname =
"Trust Region";
285 parlist.sublist(
"Step").set(
"Type",stepname);
287 parlist.sublist(
"Step").sublist(stepname).set(
"Subproblem Solver",
"Truncated CG");
288 parlist.sublist(
"General").set(
"Output Level",1);
289 parlist.sublist(
"General").sublist(
"Krylov").set(
"Relative Tolerance",1e-2);
290 parlist.sublist(
"General").sublist(
"Krylov").set(
"Iteration Limit",10);
291 parlist.sublist(
"General").sublist(
"Krylov").set(
"Absolute Tolerance",1e-4);
292 parlist.sublist(
"General").sublist(
"Secant").set(
"Type",
"Limited-Memory BFGS");
293 parlist.sublist(
"General").sublist(
"Secant").set(
"Use as Hessian",
true);
294 parlist.sublist(
"Status Test").set(
"Gradient Tolerance",1.e-12);
295 parlist.sublist(
"Status Test").set(
"Step Tolerance",1.e-14);
296 parlist.sublist(
"Status Test").set(
"Iteration Limit",100);
297 ROL::Ptr<ROL::Solver<RealT>> solver
298 = ROL::makePtr<ROL::Solver<RealT>>(problem,parlist);
301 solver->solve(*outStream);
304 ROL::Ptr<std::vector<RealT> > xtrue_ptr = ROL::makePtr<std::vector<RealT>>(
dim, 1.0);
308 x->axpy(-1.0, xtrue);
309 RealT abserr = x->norm();
311 *outStream << std::scientific <<
"\n Absolute solution error: " << abserr;
312 *outStream << std::scientific <<
"\n Relative solution error: " << relerr;
313 if ( relerr > sqrt(ROL::ROL_EPSILON<RealT>()) ) {
316 ROL::Ptr<std::vector<RealT> > vec_err_ptr = ROL::makePtr<std::vector<RealT>>(std_vec_err);
318 *outStream << std::scientific <<
"\n Linear algebra error: " << vec_err.norm() << std::endl;
319 if ( vec_err.norm() > 1e2*ROL::ROL_EPSILON<RealT>() ) {
323 catch (std::logic_error& err) {
324 *outStream << err.what() <<
"\n";
329 std::cout <<
"End Result: TEST FAILED\n";
331 std::cout <<
"End Result: TEST PASSED\n";
typename PV< Real >::size_type size_type
void scale(const Real alpha)
Compute where .
std::vector< Element > vector
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
ROL::Ptr< std::vector< Element > > getVector()
OptDualStdVector(const ROL::Ptr< std::vector< Element > > &std_vec)
virtual std::vector< Real > checkVector(const Vector< Real > &x, const Vector< Real > &y, const bool printToStream=true, std::ostream &outStream=std::cout) const
Verify vector-space methods.
Contains definitions for Rosenbrock's function.
ROL::Ptr< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.
std::vector< Element > vector
Defines the linear algebra or vector space interface.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Real apply(const ROL::Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
ROL::Ptr< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
ROL::Ptr< OptStdVector< Real > > dual_vec_
ROL::Ptr< std::vector< Element > > std_vec_
ROL::Ptr< std::vector< Element > > getVector()
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
ROL::Ptr< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void scale(const Real alpha)
Compute where .
ROL::Ptr< const std::vector< Element > > getVector() const
Real dot(const ROL::Vector< Real > &x) const
Compute where .
int dimension() const
Return dimension of the vector space.
ROL::Ptr< OptDualStdVector< Real > > dual_vec_
Real apply(const ROL::Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
Real norm() const
Returns where .
int dimension() const
Return dimension of the vector space.
ROL::Ptr< std::vector< Element > > std_vec_
basic_nullstream< char, char_traits< char >> nullstream
int main(int argc, char *argv[])
ROL::Ptr< const std::vector< Element > > getVector() const
Real dot(const ROL::Vector< Real > &x) const
Compute where .
void plus(const ROL::Vector< Real > &x)
Compute , where .
void plus(const ROL::Vector< Real > &x)
Compute , where .
Real norm() const
Returns where .
OptStdVector(const ROL::Ptr< std::vector< Element > > &std_vec)
ROL::Ptr< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.