52 #include "Teuchos_oblackholestream.hpp"
53 #include "Teuchos_GlobalMPISession.hpp"
63 template <
class Real,
class Element=Real>
66 template <
class Real,
class Element=Real>
69 template <
class Real,
class Element=Real>
72 template <
class Real,
class Element=Real>
78 template <
class Real,
class Element>
82 Teuchos::RCP<std::vector<Element> >
std_vec_;
83 mutable Teuchos::RCP<OptDualStdVector<Real> >
dual_vec_;
91 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
94 (*std_vec_)[i] += (*xvalptr)[i];
98 void scale(
const Real alpha ) {
101 (*std_vec_)[i] *= alpha;
108 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
111 val += (*std_vec_)[i]*(*xvalptr)[i];
118 val = std::sqrt(
dot(*
this) );
122 Teuchos::RCP<ROL::Vector<Real> >
clone()
const {
123 return Teuchos::rcp(
new OptStdVector( Teuchos::rcp(
new std::vector<Element>(
std_vec_->size()) ) ) );
126 Teuchos::RCP<const std::vector<Element> >
getVector()
const {
130 Teuchos::RCP<ROL::Vector<Real> >
basis(
const int i )
const {
131 Teuchos::RCP<OptStdVector> e = Teuchos::rcp(
new OptStdVector( Teuchos::rcp(
new std::vector<Element>(
std_vec_->size(), 0.0)) ) );
132 (const_cast <std::vector<Element> &> (*e->getVector()))[i]= 1.0;
147 template <
class Real,
class Element>
151 Teuchos::RCP<std::vector<Element> >
std_vec_;
152 mutable Teuchos::RCP<OptStdVector<Real> >
dual_vec_;
160 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
163 (*std_vec_)[i] += (*xvalptr)[i];
170 (*std_vec_)[i] *= alpha;
177 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
180 val += (*std_vec_)[i]*(*xvalptr)[i];
187 val = std::sqrt(
dot(*
this) );
191 Teuchos::RCP<ROL::Vector<Real> >
clone()
const {
195 Teuchos::RCP<const std::vector<Element> >
getVector()
const {
199 Teuchos::RCP<ROL::Vector<Real> >
basis(
const int i )
const {
200 Teuchos::RCP<OptDualStdVector> e = Teuchos::rcp(
new OptDualStdVector( Teuchos::rcp(
new std::vector<Element>(
std_vec_->size(), 0.0)) ) );
201 (const_cast <std::vector<Element> &> (*e->getVector()))[i]= 1.0;
216 template <
class Real,
class Element>
221 mutable Teuchos::RCP<ConDualStdVector<Real> >
dual_vec_;
229 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
232 (*std_vec_)[i] += (*xvalptr)[i];
239 (*std_vec_)[i] *= alpha;
246 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
249 val += (*std_vec_)[i]*(*xvalptr)[i];
256 val = std::sqrt(
dot(*
this) );
260 Teuchos::RCP<ROL::Vector<Real> >
clone()
const {
261 return Teuchos::rcp(
new ConStdVector( Teuchos::rcp(
new std::vector<Element>(
std_vec_->size())) ) );
264 Teuchos::RCP<const std::vector<Element> >
getVector()
const {
268 Teuchos::RCP<ROL::Vector<Real> >
basis(
const int i )
const {
269 Teuchos::RCP<ConStdVector> e = Teuchos::rcp(
new ConStdVector( Teuchos::rcp(
new std::vector<Element>(
std_vec_->size(), 0.0)) ) );
270 (const_cast <std::vector<Element> &> (*e->getVector()))[i]= 1.0;
285 template <
class Real,
class Element>
298 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
301 (*std_vec_)[i] += (*xvalptr)[i];
308 (*std_vec_)[i] *= alpha;
315 Teuchos::RCP<const std::vector<Element> > xvalptr = ex.
getVector();
318 val += (*std_vec_)[i]*(*xvalptr)[i];
325 val = std::sqrt(
dot(*
this) );
329 Teuchos::RCP<ROL::Vector<Real> >
clone()
const {
333 Teuchos::RCP<const std::vector<Element> >
getVector()
const {
337 Teuchos::RCP<ROL::Vector<Real> >
basis(
const int i )
const {
338 Teuchos::RCP<ConDualStdVector> e = Teuchos::rcp(
new ConDualStdVector( Teuchos::rcp(
new std::vector<Element>(
std_vec_->size(), 0.0)) ) );
339 (const_cast <std::vector<Element> &> (*e->getVector()))[i]= 1.0;
355 int main(
int argc,
char *argv[]) {
357 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
360 int iprint = argc - 1;
361 Teuchos::RCP<std::ostream> outStream;
362 Teuchos::oblackholestream bhs;
364 outStream = Teuchos::rcp(&std::cout,
false);
366 outStream = Teuchos::rcp(&bhs,
false);
374 Teuchos::RCP<ROL::Objective<RealT> > obj;
375 Teuchos::RCP<ROL::EqualityConstraint<RealT> > constr;
376 Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp(
new std::vector<RealT> (0, 0.0) );
377 Teuchos::RCP<std::vector<RealT> > sol_rcp = Teuchos::rcp(
new std::vector<RealT> (0, 0.0) );
384 Teuchos::ParameterList parlist;
386 parlist.
set(
"Nominal SQP Optimality Solver Tolerance", 1.e-2);
392 RealT left = -1e0, right = 1e0;
393 Teuchos::RCP<std::vector<RealT> > xtest_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
394 Teuchos::RCP<std::vector<RealT> > g_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
395 Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
396 Teuchos::RCP<std::vector<RealT> > gd_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
397 Teuchos::RCP<std::vector<RealT> > v_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
398 Teuchos::RCP<std::vector<RealT> > vc_rcp = Teuchos::rcp(
new std::vector<RealT> (nc, 0.0) );
399 Teuchos::RCP<std::vector<RealT> > vl_rcp = Teuchos::rcp(
new std::vector<RealT> (nc, 0.0) );
401 OptDualStdVector<RealT> g(g_rcp);
403 OptDualStdVector<RealT> gd(gd_rcp);
405 ConStdVector<RealT> vc(vc_rcp);
408 for (
int i=0; i<dim; i++) {
409 (*xtest_rcp)[i] = ( (
RealT)rand() / (
RealT)RAND_MAX ) * (right - left) + left;
410 (*d_rcp)[i] = ( (
RealT)rand() / (
RealT)RAND_MAX ) * (right - left) + left;
411 (*gd_rcp)[i] = ( (
RealT)rand() / (
RealT)RAND_MAX ) * (right - left) + left;
412 (*v_rcp)[i] = ( (
RealT)rand() / (
RealT)RAND_MAX ) * (right - left) + left;
415 for (
int i=0; i<nc; i++) {
416 (*vc_rcp)[i] = ( (
RealT)rand() / (
RealT)RAND_MAX ) * (right - left) + left;
417 (*vl_rcp)[i] = ( (
RealT)rand() / (
RealT)RAND_MAX ) * (right - left) + left;
419 obj->checkGradient(xtest, g, d,
true, *outStream); *outStream <<
"\n";
420 obj->checkHessVec(xtest, g, v,
true, *outStream); *outStream <<
"\n";
421 obj->checkHessSym(xtest, g, d, v,
true, *outStream); *outStream <<
"\n";
422 constr->checkApplyJacobian(xtest, v, vc,
true, *outStream); *outStream <<
"\n";
423 constr->checkApplyAdjointJacobian(xtest, vl, vc, g,
true, *outStream); *outStream <<
"\n";
424 constr->checkApplyAdjointHessian(xtest, vl, d, g,
true, *outStream); *outStream <<
"\n";
426 Teuchos::RCP<std::vector<RealT> > v1_rcp = Teuchos::rcp(
new std::vector<RealT> (dim, 0.0) );
427 Teuchos::RCP<std::vector<RealT> > v2_rcp = Teuchos::rcp(
new std::vector<RealT> (nc, 0.0) );
431 constr->solveAugmentedSystem(v1, v2, gd, vc, xtest, augtol);
448 std::vector<std::string> output = algo.
run(x, g, vl, vc, *obj, *constr,
false);
449 for (
unsigned i = 0; i < output.size(); i++ ) {
450 *outStream << output[i];
454 *outStream <<
"\nReference solution x_r =\n";
455 *outStream << std::scientific <<
" " << (*sol_rcp)[0] <<
"\n";
456 *outStream << std::scientific <<
" " << (*sol_rcp)[1] <<
"\n";
457 *outStream << std::scientific <<
" " << (*sol_rcp)[2] <<
"\n";
458 *outStream << std::scientific <<
" " << (*sol_rcp)[3] <<
"\n";
459 *outStream << std::scientific <<
" " << (*sol_rcp)[4] <<
"\n";
460 *outStream <<
"\nOptimal solution x =\n";
461 *outStream << std::scientific <<
" " << (*x_rcp)[0] <<
"\n";
462 *outStream << std::scientific <<
" " << (*x_rcp)[1] <<
"\n";
463 *outStream << std::scientific <<
" " << (*x_rcp)[2] <<
"\n";
464 *outStream << std::scientific <<
" " << (*x_rcp)[3] <<
"\n";
465 *outStream << std::scientific <<
" " << (*x_rcp)[4] <<
"\n";
467 RealT abserr = x.norm();
469 *outStream << std::scientific <<
"\n Absolute Error: " << abserr;
470 *outStream << std::scientific <<
"\n Relative Error: " << relerr <<
"\n";
475 catch (std::logic_error err) {
476 *outStream << err.what() <<
"\n";
481 std::cout <<
"End Result: TEST FAILED\n";
483 std::cout <<
"End Result: TEST PASSED\n";
Teuchos::RCP< std::vector< Element > > std_vec_
Teuchos::RCP< OptDualStdVector< Real > > dual_vec_
void scale(const Real alpha)
Compute where .
OptStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec)
Real dot(const ROL::Vector< Real > &x) const
Compute where .
Teuchos::RCP< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.
ConStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec)
Real norm() const
Returns where .
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
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< const std::vector< Element > > getVector() const
Teuchos::RCP< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
int dimension() const
Return dimension of the vector space.
Teuchos::RCP< OptStdVector< Real > > dual_vec_
Teuchos::RCP< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Teuchos::RCP< std::vector< Element > > std_vec_
virtual void zero()
Set to zero vector.
Teuchos::RCP< const std::vector< Element > > getVector() const
Defines the linear algebra or vector space interface.
Teuchos::RCP< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.
void plus(const ROL::Vector< Real > &x)
Compute , where .
Teuchos::RCP< ConDualStdVector< Real > > dual_vec_
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
const ROL::Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
OptDualStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec)
Teuchos::RCP< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
void scale(const Real alpha)
Compute where .
void scale(const Real alpha)
Compute where .
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.
Teuchos::RCP< const std::vector< Element > > getVector() const
Real dot(const ROL::Vector< Real > &x) const
Compute where .
Implements the computation of optimization steps with composite-step trust-region SQP methods...
int dimension() const
Return dimension of the vector space.
int dimension() const
Return dimension of the vector space.
Teuchos::RCP< ROL::Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Teuchos::RCP< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.
Real norm() const
Returns where .
Teuchos::RCP< ROL::Vector< Real > > basis(const int i) const
Return i-th basis vector.
Contains definitions for the equality constrained NLP from Nocedal/Wright, 2nd edition, page 574, example 18.2; note the typo in reversing the initial guess and the solution.
int dimension() const
Return dimension of the vector space.
Teuchos::RCP< std::vector< Element > > std_vec_
Real norm() const
Returns where .
ConDualStdVector(const Teuchos::RCP< std::vector< Element > > &std_vec)
Teuchos::RCP< ConStdVector< Real > > dual_vec_
Real dot(const ROL::Vector< Real > &x) const
Compute where .
Real dot(const ROL::Vector< Real > &x) const
Compute where .
virtual void set(const Vector &x)
Set where .
Teuchos::RCP< const std::vector< Element > > getVector() const
void plus(const ROL::Vector< Real > &x)
Compute , where .
int main(int argc, char *argv[])
Teuchos::RCP< std::vector< Element > > std_vec_
void plus(const ROL::Vector< Real > &x)
Compute , where .
void plus(const ROL::Vector< Real > &x)
Compute , where .
Real norm() const
Returns where .
void scale(const Real alpha)
Compute where .
static const double ROL_EPSILON
Platform-dependent machine epsilon.