54 #include "Teuchos_oblackholestream.hpp" 55 #include "Teuchos_GlobalMPISession.hpp" 65 template <
class Real,
class Element=Real>
68 template <
class Real,
class Element=Real>
75 template <
class Real,
class Element>
81 typedef typename vector::size_type
uint;
85 mutable Teuchos::RCP<OptDualStdVector<Real> >
dual_vec_;
92 using Teuchos::RCP;
using Teuchos::dyn_cast;
96 (*std_vec_)[i] += (*xvalptr)[i];
103 (*std_vec_)[i] *= alpha;
109 using Teuchos::RCP;
using Teuchos::dyn_cast;
113 val += (*std_vec_)[i]*(*xvalptr)[i];
120 val = std::sqrt(
dot(*
this) );
124 Teuchos::RCP<ROL::Vector<Real> >
clone()
const {
125 return Teuchos::rcp(
new OptStdVector( Teuchos::rcp(
new std::vector<Element>(
std_vec_->size()) ) ) );
128 Teuchos::RCP<const std::vector<Element> >
getVector()
const {
136 Teuchos::RCP<ROL::Vector<Real> >
basis(
const int i )
const {
137 using Teuchos::RCP;
using Teuchos::rcp;
157 template <
class Real,
class Element>
163 typedef typename vector::size_type
uint;
174 using Teuchos::RCP;
using Teuchos::dyn_cast;
179 (*std_vec_)[i] += (*xvalptr)[i];
186 (*std_vec_)[i] *= alpha;
192 using Teuchos::RCP;
using Teuchos::dyn_cast;
196 val += (*std_vec_)[i]*(*xvalptr)[i];
203 val = std::sqrt(
dot(*
this) );
207 Teuchos::RCP<ROL::Vector<Real> >
clone()
const {
211 Teuchos::RCP<const std::vector<Element> >
getVector()
const {
219 Teuchos::RCP<ROL::Vector<Real> >
basis(
const int i )
const {
220 using Teuchos::RCP;
using Teuchos::rcp;
245 int main(
int argc,
char *argv[]) {
247 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
250 int iprint = argc - 1;
251 Teuchos::RCP<std::ostream> outStream;
252 Teuchos::oblackholestream bhs;
254 outStream = Teuchos::rcp(&std::cout,
false);
256 outStream = Teuchos::rcp(&bhs,
false);
268 Teuchos::ParameterList parlist;
269 std::string stepname =
"Trust Region";
270 parlist.sublist(
"Step").sublist(stepname).
set(
"Subproblem Solver",
"Truncated CG");
271 parlist.sublist(
"General").sublist(
"Krylov").set(
"Iteration Limit",10);
272 parlist.sublist(
"General").sublist(
"Krylov").set(
"Relative Tolerance",1e-2);
273 parlist.sublist(
"General").sublist(
"Krylov").set(
"Absolute Tolerance",1e-4);
274 parlist.sublist(
"General").sublist(
"Secant").set(
"Use as Hessian",
true);
275 parlist.sublist(
"Status Test").set(
"Gradient Tolerance",1.e-12);
276 parlist.sublist(
"Status Test").set(
"Step Tolerance",1.e-14);
277 parlist.sublist(
"Status Test").set(
"Iteration Limit",100);
281 Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
282 Teuchos::RCP<std::vector<RealT> > g_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
284 for (
int i=0; i<dim/2; i++) {
285 (*x_rcp)[2*i] = -1.2;
286 (*x_rcp)[2*i+1] = 1.0;
294 Teuchos::RCP<std::vector<RealT> > aa_rcp = Teuchos::rcp(
new std::vector<RealT> (1, 1.0) );
296 Teuchos::RCP<std::vector<RealT> > bb_rcp = Teuchos::rcp(
new std::vector<RealT> (1, 2.0) );
298 Teuchos::RCP<std::vector<RealT> > cc_rcp = Teuchos::rcp(
new std::vector<RealT> (1, 3.0) );
300 std::vector<RealT> std_vec_err = av.
checkVector(bv,cv,
true,*outStream);
303 std::vector<std::string> output = algo.
run(x,g, obj,
true, *outStream);
306 Teuchos::RCP<std::vector<RealT> > xtrue_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 1.0) );
313 *outStream << std::scientific <<
"\n Absolute solution error: " << abserr;
314 *outStream << std::scientific <<
"\n Relative solution error: " << relerr;
315 if ( relerr > sqrt(ROL::ROL_EPSILON<RealT>()) ) {
318 Teuchos::RCP<std::vector<RealT> > vec_err_rcp = Teuchos::rcp(
new std::vector<RealT> (std_vec_err) );
320 *outStream << std::scientific <<
"\n Linear algebra error: " << vec_err.
norm() << std::endl;
321 if ( vec_err.
norm() > 1e2*ROL::ROL_EPSILON<RealT>() ) {
325 catch (std::logic_error err) {
326 *outStream << err.what() <<
"\n";
331 std::cout <<
"End Result: TEST FAILED\n";
333 std::cout <<
"End Result: TEST PASSED\n";
Teuchos::RCP< std::vector< Element > > std_vec_
Teuchos::RCP< OptDualStdVector< Real > > dual_vec_
Rosenbrock's function.
void scale(const Real alpha)
Compute where .
OptStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec)
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Teuchos::RCP< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
std::vector< Element > vector
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Real norm() const
Returns where .
Teuchos::RCP< std::vector< Element > > getVector()
Teuchos::RCP< std::vector< Element > > getVector()
Teuchos::RCP< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.
int main(int argc, char *argv[])
Teuchos::RCP< OptStdVector< Real > > dual_vec_
Contains definitions for Rosenbrock's function.
std::vector< Element > vector
Defines the linear algebra or vector space interface.
Teuchos::RCP< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.
int dimension() const
Return dimension of the vector space.
OptDualStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec)
Provides the std::vector implementation of the ROL::Vector interface.
void scale(const Real alpha)
Compute where .
Teuchos::RCP< const std::vector< Element > > getVector() const
Provides an interface to run optimization algorithms.
Real norm() const
Returns where .
int dimension() const
Return dimension of the vector space.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
virtual void set(const Vector &x)
Set where .
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.
Real dot(const ROL::Vector< Real > &x) const
Compute where .
Teuchos::RCP< std::vector< Element > > std_vec_
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 .
Teuchos::RCP< const std::vector< Element > > getVector() const
Teuchos::RCP< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Real norm() const
Returns where .