| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/*! \file example_01.cpp |
| 11 |
\brief Shows how to solve the equality constrained NLP |
| 12 |
from Nocedal/Wright, 2nd edition, page 574, example 18.2. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "ROL_SimpleEqConstrained.hpp" |
| 16 |
#include "ROL_Algorithm.hpp" |
| 17 |
#include "ROL_ConstraintStatusTest.hpp" |
| 18 |
#include "ROL_CompositeStep.hpp" |
| 19 |
#include "ROL_Stream.hpp" |
| 20 |
#include "Teuchos_GlobalMPISession.hpp" |
| 21 |
|
| 22 |
#include <iostream> |
| 23 |
|
| 24 |
typedef double RealT; |
| 25 |
|
| 26 |
/*** Declare four vector spaces. ***/ |
| 27 |
|
| 28 |
// Forward declarations: |
| 29 |
|
| 30 |
template <class Real, class Element=Real> |
| 31 |
class OptStdVector; // Optimization space. |
| 32 |
|
| 33 |
template <class Real, class Element=Real> |
| 34 |
class OptDualStdVector; // Dual optimization space. |
| 35 |
|
| 36 |
template <class Real, class Element=Real> |
| 37 |
class ConStdVector; // Constraint space. |
| 38 |
|
| 39 |
template <class Real, class Element=Real> |
| 40 |
class ConDualStdVector; // Dual constraint space. |
| 41 |
|
| 42 |
// Vector space definitions: |
| 43 |
|
| 44 |
// Optimization space. |
| 45 |
template <class Real, class Element> |
| 46 |
class OptStdVector : public ROL::Vector<Real> { |
| 47 |
|
| 48 |
typedef std::vector<Element> vector; |
| 49 |
typedef ROL::Vector<Real> V; |
| 50 |
typedef OptStdVector<Real,Element> PV; |
| 51 |
typedef OptDualStdVector<Real,Element> DV; |
| 52 |
|
| 53 |
typedef typename vector::size_type uint; |
| 54 |
|
| 55 |
private: |
| 56 |
ROL::Ptr<std::vector<Element> > std_vec_; |
| 57 |
|
| 58 |
public: |
| 59 |
|
| 60 |
OptStdVector(const ROL::Ptr<std::vector<Element> > & std_vec) : std_vec_(std_vec) {} |
| 61 |
|
| 62 |
void plus( const ROL::Vector<Real> &x ) { |
| 63 |
|
| 64 |
ROL::Ptr<const vector> xp; |
| 65 |
try { |
| 66 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 67 |
} |
| 68 |
catch (const std::bad_cast &e) { |
| 69 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 70 |
} |
| 71 |
uint dimension = std_vec_->size(); |
| 72 |
for (uint i=0; i<dimension; i++) { |
| 73 |
(*std_vec_)[i] += (*xp)[i]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
void scale( const Real alpha ) { |
| 78 |
uint dimension = std_vec_->size(); |
| 79 |
for (uint i=0; i<dimension; i++) { |
| 80 |
(*std_vec_)[i] *= alpha; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 85 |
|
| 86 |
Real val = 0; |
| 87 |
ROL::Ptr<const vector> xp; |
| 88 |
try { // duality pairing |
| 89 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 90 |
} |
| 91 |
catch (const std::bad_cast &e) { // inner product |
| 92 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 93 |
} |
| 94 |
uint dimension = std_vec_->size(); |
| 95 |
for (uint i=0; i<dimension; i++) { |
| 96 |
val += (*std_vec_)[i]*(*xp)[i]; |
| 97 |
} |
| 98 |
return val; |
| 99 |
} |
| 100 |
|
| 101 |
Real norm() const { |
| 102 |
Real val = 0; |
| 103 |
val = std::sqrt( dot(*this) ); |
| 104 |
return val; |
| 105 |
} |
| 106 |
|
| 107 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 108 |
|
| 109 |
return ROL::makePtr<OptStdVector>( ROL::makePtr<vector>(std_vec_->size()) ); |
| 110 |
} |
| 111 |
|
| 112 |
ROL::Ptr<const std::vector<Element> > getVector() const { |
| 113 |
return std_vec_; |
| 114 |
} |
| 115 |
|
| 116 |
ROL::Ptr<std::vector<Element> > getVector() { |
| 117 |
return std_vec_; |
| 118 |
} |
| 119 |
|
| 120 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 121 |
|
| 122 |
ROL::Ptr<vector> e_ptr = ROL::makePtr<vector>(std_vec_->size(),0.0); |
| 123 |
ROL::Ptr<V> e = ROL::makePtr<PV>(e_ptr); |
| 124 |
(*e_ptr)[i] = 1.0; |
| 125 |
return e; |
| 126 |
} |
| 127 |
|
| 128 |
int dimension() const {return static_cast<int>(std_vec_->size());} |
| 129 |
|
| 130 |
}; // class OptStdVector |
| 131 |
|
| 132 |
|
| 133 |
// Dual optimization space. |
| 134 |
template <class Real, class Element> |
| 135 |
class OptDualStdVector : public ROL::Vector<Real> { |
| 136 |
|
| 137 |
typedef std::vector<Element> vector; |
| 138 |
typedef ROL::Vector<Real> V; |
| 139 |
typedef OptStdVector<Real,Element> DV; |
| 140 |
typedef OptDualStdVector<Real,Element> PV; |
| 141 |
|
| 142 |
typedef typename vector::size_type uint; |
| 143 |
|
| 144 |
private: |
| 145 |
ROL::Ptr<std::vector<Element> > std_vec_; |
| 146 |
|
| 147 |
public: |
| 148 |
|
| 149 |
OptDualStdVector(const ROL::Ptr<std::vector<Element> > & std_vec) : std_vec_(std_vec) {} |
| 150 |
|
| 151 |
void plus( const ROL::Vector<Real> &x ) { |
| 152 |
|
| 153 |
|
| 154 |
ROL::Ptr<const vector> xp; |
| 155 |
try { |
| 156 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 157 |
} |
| 158 |
catch (const std::bad_cast &e) { |
| 159 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 160 |
} |
| 161 |
uint dimension = std_vec_->size(); |
| 162 |
for (uint i=0; i<dimension; i++) { |
| 163 |
(*std_vec_)[i] += (*xp)[i]; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
void scale( const Real alpha ) { |
| 168 |
uint dimension = std_vec_->size(); |
| 169 |
for (uint i=0; i<dimension; i++) { |
| 170 |
(*std_vec_)[i] *= alpha; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 175 |
|
| 176 |
Real val = 0; |
| 177 |
|
| 178 |
ROL::Ptr<const vector> xp; |
| 179 |
|
| 180 |
try { // duality pairing |
| 181 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 182 |
} |
| 183 |
catch (const std::bad_cast &e) { // inner product |
| 184 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 185 |
} |
| 186 |
|
| 187 |
uint dimension = std_vec_->size(); |
| 188 |
for (uint i=0; i<dimension; i++) { |
| 189 |
val += (*std_vec_)[i]*(*xp)[i]; |
| 190 |
} |
| 191 |
return val; |
| 192 |
} |
| 193 |
|
| 194 |
Real norm() const { |
| 195 |
Real val = 0; |
| 196 |
val = std::sqrt( dot(*this) ); |
| 197 |
return val; |
| 198 |
} |
| 199 |
|
| 200 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 201 |
|
| 202 |
return ROL::makePtr<OptDualStdVector>( ROL::makePtr<std::vector<Element>>(std_vec_->size()) ); |
| 203 |
} |
| 204 |
|
| 205 |
ROL::Ptr<const std::vector<Element> > getVector() const { |
| 206 |
return std_vec_; |
| 207 |
} |
| 208 |
|
| 209 |
ROL::Ptr<std::vector<Element> > getVector() { |
| 210 |
return std_vec_; |
| 211 |
} |
| 212 |
|
| 213 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 214 |
|
| 215 |
ROL::Ptr<vector> e_ptr = ROL::makePtr<vector>(std_vec_->size(),0.0); |
| 216 |
ROL::Ptr<V> e = ROL::makePtr<PV>(e_ptr); |
| 217 |
(*e_ptr)[i] = 1.0; |
| 218 |
return e; |
| 219 |
} |
| 220 |
|
| 221 |
int dimension() const {return static_cast<int>(std_vec_->size());} |
| 222 |
|
| 223 |
}; // class OptDualStdVector |
| 224 |
|
| 225 |
|
| 226 |
// Constraint space. |
| 227 |
template <class Real, class Element> |
| 228 |
class ConStdVector : public ROL::Vector<Real> { |
| 229 |
|
| 230 |
typedef std::vector<Element> vector; |
| 231 |
typedef ROL::Vector<Real> V; |
| 232 |
typedef ConStdVector<Real,Element> PV; |
| 233 |
typedef ConDualStdVector<Real,Element> DV; |
| 234 |
|
| 235 |
typedef typename vector::size_type uint; |
| 236 |
|
| 237 |
private: |
| 238 |
ROL::Ptr<std::vector<Element> > std_vec_; |
| 239 |
|
| 240 |
public: |
| 241 |
|
| 242 |
ConStdVector(const ROL::Ptr<std::vector<Element> > & std_vec) : std_vec_(std_vec) {} |
| 243 |
|
| 244 |
void plus( const ROL::Vector<Real> &x ) { |
| 245 |
|
| 246 |
ROL::Ptr<const vector> xp; |
| 247 |
try { |
| 248 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 249 |
} |
| 250 |
catch (const std::bad_cast &e) { |
| 251 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 252 |
} |
| 253 |
|
| 254 |
uint dimension = std_vec_->size(); |
| 255 |
for (uint i=0; i<dimension; i++) { |
| 256 |
(*std_vec_)[i] += (*xp)[i]; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
void scale( const Real alpha ) { |
| 261 |
uint dimension = std_vec_->size(); |
| 262 |
for (uint i=0; i<dimension; i++) { |
| 263 |
(*std_vec_)[i] *= alpha; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 268 |
|
| 269 |
ROL::Ptr<const vector> xp; |
| 270 |
Real val = 0; |
| 271 |
try { // duality pairing |
| 272 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 273 |
} |
| 274 |
catch (const std::bad_cast &e) { // inner product |
| 275 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 276 |
} |
| 277 |
uint dimension = std_vec_->size(); |
| 278 |
for (uint i=0; i<dimension; i++) { |
| 279 |
val += (*std_vec_)[i]*(*xp)[i]; |
| 280 |
} |
| 281 |
|
| 282 |
return val; |
| 283 |
} |
| 284 |
|
| 285 |
Real norm() const { |
| 286 |
Real val = 0; |
| 287 |
val = std::sqrt( dot(*this) ); |
| 288 |
return val; |
| 289 |
} |
| 290 |
|
| 291 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 292 |
return ROL::makePtr<ConStdVector>( ROL::makePtr<std::vector<Element>>(std_vec_->size())); |
| 293 |
} |
| 294 |
|
| 295 |
ROL::Ptr<const std::vector<Element> > getVector() const { |
| 296 |
return std_vec_; |
| 297 |
} |
| 298 |
|
| 299 |
ROL::Ptr<std::vector<Element> > getVector() { |
| 300 |
return std_vec_; |
| 301 |
} |
| 302 |
|
| 303 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 304 |
|
| 305 |
ROL::Ptr<vector> e_ptr = ROL::makePtr<vector>(std_vec_->size(),0.0); |
| 306 |
ROL::Ptr<V> e = ROL::makePtr<PV>(e_ptr); |
| 307 |
(*e_ptr)[i] = 1.0; |
| 308 |
return e; |
| 309 |
} |
| 310 |
|
| 311 |
int dimension() const {return static_cast<int>(std_vec_->size());} |
| 312 |
|
| 313 |
}; // class ConStdVector |
| 314 |
|
| 315 |
|
| 316 |
// Dual constraint space. |
| 317 |
template <class Real, class Element> |
| 318 |
class ConDualStdVector : public ROL::Vector<Real> { |
| 319 |
|
| 320 |
typedef std::vector<Element> vector; |
| 321 |
typedef ROL::Vector<Real> V; |
| 322 |
typedef ConStdVector<Real,Element> DV; |
| 323 |
typedef ConDualStdVector<Real,Element> PV; |
| 324 |
|
| 325 |
typedef typename vector::size_type uint; |
| 326 |
|
| 327 |
private: |
| 328 |
|
| 329 |
ROL::Ptr<std::vector<Element> > std_vec_; |
| 330 |
|
| 331 |
public: |
| 332 |
|
| 333 |
ConDualStdVector(const ROL::Ptr<std::vector<Element> > & std_vec) : std_vec_(std_vec) {} |
| 334 |
|
| 335 |
void plus( const ROL::Vector<Real> &x ) { |
| 336 |
|
| 337 |
|
| 338 |
ROL::Ptr<const vector> xp; |
| 339 |
|
| 340 |
try { |
| 341 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 342 |
} |
| 343 |
catch (const std::bad_cast &e) { |
| 344 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 345 |
} |
| 346 |
uint dimension = std_vec_->size(); |
| 347 |
for (uint i=0; i<dimension; i++) { |
| 348 |
(*std_vec_)[i] += (*xp)[i]; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
void scale( const Real alpha ) { |
| 353 |
uint dimension = std_vec_->size(); |
| 354 |
for (uint i=0; i<dimension; i++) { |
| 355 |
(*std_vec_)[i] *= alpha; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
Real dot( const ROL::Vector<Real> &x ) const { |
| 360 |
|
| 361 |
Real val = 0; |
| 362 |
ROL::Ptr<const vector> xp; |
| 363 |
try { // duality pairing |
| 364 |
xp = dynamic_cast<const PV&>(x).getVector(); |
| 365 |
} |
| 366 |
catch (const std::bad_cast &e) { // inner product |
| 367 |
xp = dynamic_cast<const DV&>(x).getVector(); |
| 368 |
} |
| 369 |
|
| 370 |
uint dimension = std_vec_->size(); |
| 371 |
for (uint i=0; i<dimension; i++) { |
| 372 |
val += (*std_vec_)[i]*(*xp)[i]; |
| 373 |
} |
| 374 |
return val; |
| 375 |
} |
| 376 |
|
| 377 |
Real norm() const { |
| 378 |
Real val = 0; |
| 379 |
val = std::sqrt( dot(*this) ); |
| 380 |
return val; |
| 381 |
} |
| 382 |
|
| 383 |
ROL::Ptr<ROL::Vector<Real> > clone() const { |
| 384 |
return ROL::makePtr<ConDualStdVector>( ROL::makePtr<std::vector<Element>>(std_vec_->size())); |
| 385 |
} |
| 386 |
|
| 387 |
ROL::Ptr<const std::vector<Element> > getVector() const { |
| 388 |
return std_vec_; |
| 389 |
} |
| 390 |
|
| 391 |
ROL::Ptr<std::vector<Element> > getVector() { |
| 392 |
return std_vec_; |
| 393 |
} |
| 394 |
|
| 395 |
ROL::Ptr<ROL::Vector<Real> > basis( const int i ) const { |
| 396 |
|
| 397 |
ROL::Ptr<vector> e_ptr = ROL::makePtr<vector>(std_vec_->size(),0.0); |
| 398 |
ROL::Ptr<V> e = ROL::makePtr<PV>(e_ptr); |
| 399 |
(*e_ptr)[i] = 1.0; |
| 400 |
return e; |
| 401 |
} |
| 402 |
|
| 403 |
int dimension() const {return static_cast<int>(std_vec_->size());} |
| 404 |
|
| 405 |
}; // class ConDualStdVector |
| 406 |
|
| 407 |
/*** End of declaration of four vector spaces. ***/ |
| 408 |
|
| 409 |
|
| 410 |
int main(int argc, char *argv[]) { |
| 411 |
|
| 412 |
typedef std::vector<RealT> vector; |
| 413 |
typedef typename vector::size_type uint; |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 418 |
|
| 419 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 420 |
int iprint = argc - 1; |
| 421 |
ROL::Ptr<std::ostream> outStream; |
| 422 |
ROL::nullstream bhs; // outputs nothing |
| 423 |
if (iprint > 0) |
| 424 |
outStream = ROL::makePtrFromRef(std::cout); |
| 425 |
else |
| 426 |
outStream = ROL::makePtrFromRef(bhs); |
| 427 |
|
| 428 |
int errorFlag = 0; |
| 429 |
|
| 430 |
// *** Example body. |
| 431 |
|
| 432 |
try { |
| 433 |
|
| 434 |
uint dim = 5; |
| 435 |
uint nc = 3; |
| 436 |
ROL::Ptr<ROL::Objective<RealT> > obj; |
| 437 |
ROL::Ptr<ROL::Constraint<RealT> > constr; |
| 438 |
ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 439 |
ROL::Ptr<vector> sol_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 440 |
OptStdVector<RealT> x(x_ptr); // Iteration vector. |
| 441 |
OptStdVector<RealT> sol(sol_ptr); // Reference solution vector. |
| 442 |
|
| 443 |
// Retrieve objective, constraint, iteration vector, solution vector. |
| 444 |
ROL::ZOO::getSimpleEqConstrained <RealT, OptStdVector<RealT>, OptDualStdVector<RealT>, ConStdVector<RealT>, ConDualStdVector<RealT> > SEC; |
| 445 |
obj = SEC.getObjective(); |
| 446 |
constr = SEC.getEqualityConstraint(); |
| 447 |
x.set(*SEC.getInitialGuess()); |
| 448 |
sol.set(*SEC.getSolution()); |
| 449 |
|
| 450 |
// Run derivative checks, etc. |
| 451 |
RealT left = -1e0, right = 1e0; |
| 452 |
ROL::Ptr<vector> xtest_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 453 |
ROL::Ptr<vector> g_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 454 |
ROL::Ptr<vector> d_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 455 |
ROL::Ptr<vector> gd_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 456 |
ROL::Ptr<vector> v_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 457 |
ROL::Ptr<vector> vc_ptr = ROL::makePtr<vector>(nc, 0.0); |
| 458 |
ROL::Ptr<vector> vl_ptr = ROL::makePtr<vector>(nc, 0.0); |
| 459 |
OptStdVector<RealT> xtest(xtest_ptr); |
| 460 |
OptDualStdVector<RealT> g(g_ptr); |
| 461 |
OptStdVector<RealT> d(d_ptr); |
| 462 |
OptDualStdVector<RealT> gd(gd_ptr); |
| 463 |
OptStdVector<RealT> v(v_ptr); |
| 464 |
ConStdVector<RealT> vc(vc_ptr); |
| 465 |
ConDualStdVector<RealT> vl(vl_ptr); |
| 466 |
// set xtest, d, v |
| 467 |
for (uint i=0; i<dim; i++) { |
| 468 |
(*xtest_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 469 |
(*d_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 470 |
(*gd_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 471 |
(*v_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 472 |
} |
| 473 |
// set vc, vl |
| 474 |
for (uint i=0; i<nc; i++) { |
| 475 |
(*vc_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 476 |
(*vl_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; |
| 477 |
} |
| 478 |
obj->checkGradient(xtest, g, d, true, *outStream); *outStream << "\n"; |
| 479 |
obj->checkHessVec(xtest, g, v, true, *outStream); *outStream << "\n"; |
| 480 |
obj->checkHessSym(xtest, g, d, v, true, *outStream); *outStream << "\n"; |
| 481 |
constr->checkApplyJacobian(xtest, v, vc, true, *outStream); *outStream << "\n"; |
| 482 |
constr->checkApplyAdjointJacobian(xtest, vl, vc, g, true, *outStream); *outStream << "\n"; |
| 483 |
constr->checkApplyAdjointHessian(xtest, vl, d, g, true, *outStream); *outStream << "\n"; |
| 484 |
|
| 485 |
ROL::Ptr<vector> v1_ptr = ROL::makePtr<vector>(dim, 0.0); |
| 486 |
ROL::Ptr<vector> v2_ptr = ROL::makePtr<vector>(nc, 0.0); |
| 487 |
OptStdVector<RealT> v1(v1_ptr); |
| 488 |
ConDualStdVector<RealT> v2(v2_ptr); |
| 489 |
RealT augtol = 1e-8; |
| 490 |
constr->solveAugmentedSystem(v1, v2, gd, vc, xtest, augtol); |
| 491 |
|
| 492 |
|
| 493 |
// Define algorithm. |
| 494 |
ROL::ParameterList parlist; |
| 495 |
std::string stepname = "Composite Step"; |
| 496 |
parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1.e-4); |
| 497 |
parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true); |
| 498 |
parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20); |
| 499 |
parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2); |
| 500 |
parlist.sublist("Step").sublist(stepname).set("Output Level",0); |
| 501 |
parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12); |
| 502 |
parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12); |
| 503 |
parlist.sublist("Status Test").set("Step Tolerance",1.e-18); |
| 504 |
parlist.sublist("Status Test").set("Iteration Limit",100); |
| 505 |
ROL::Ptr<ROL::Step<RealT>> |
| 506 |
step = ROL::makePtr<ROL::CompositeStep<RealT>>(parlist); |
| 507 |
ROL::Ptr<ROL::StatusTest<RealT>> |
| 508 |
status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(parlist); |
| 509 |
ROL::Algorithm<RealT> algo(step,status,false); |
| 510 |
|
| 511 |
// Run Algorithm |
| 512 |
vl.zero(); |
| 513 |
//(*x_ptr)[0] = 3.0; (*x_ptr)[1] = 2.0; (*x_ptr)[2] = 2.0; (*x_ptr)[3] = 1.0; (*x_ptr)[4] = 1.0; |
| 514 |
//(*x_ptr)[0] = -5.0; (*x_ptr)[1] = -5.0; (*x_ptr)[2] = -5.0; (*x_ptr)[3] = -6.0; (*x_ptr)[4] = -6.0; |
| 515 |
|
| 516 |
std::vector<std::string> output = algo.run(x, g, vl, vc, *obj, *constr, false); |
| 517 |
for ( uint i = 0; i < output.size(); i++ ) { |
| 518 |
*outStream << output[i]; |
| 519 |
} |
| 520 |
|
| 521 |
// Compute Error |
| 522 |
*outStream << "\nReference solution x_r =\n"; |
| 523 |
*outStream << std::scientific << " " << (*sol_ptr)[0] << "\n"; |
| 524 |
*outStream << std::scientific << " " << (*sol_ptr)[1] << "\n"; |
| 525 |
*outStream << std::scientific << " " << (*sol_ptr)[2] << "\n"; |
| 526 |
*outStream << std::scientific << " " << (*sol_ptr)[3] << "\n"; |
| 527 |
*outStream << std::scientific << " " << (*sol_ptr)[4] << "\n"; |
| 528 |
*outStream << "\nOptimal solution x =\n"; |
| 529 |
*outStream << std::scientific << " " << (*x_ptr)[0] << "\n"; |
| 530 |
*outStream << std::scientific << " " << (*x_ptr)[1] << "\n"; |
| 531 |
*outStream << std::scientific << " " << (*x_ptr)[2] << "\n"; |
| 532 |
*outStream << std::scientific << " " << (*x_ptr)[3] << "\n"; |
| 533 |
*outStream << std::scientific << " " << (*x_ptr)[4] << "\n"; |
| 534 |
x.axpy(-1.0, sol); |
| 535 |
RealT abserr = x.norm(); |
| 536 |
RealT relerr = abserr/sol.norm(); |
| 537 |
*outStream << std::scientific << "\n Absolute Error: " << abserr; |
| 538 |
*outStream << std::scientific << "\n Relative Error: " << relerr << "\n"; |
| 539 |
if ( relerr > sqrt(ROL::ROL_EPSILON<RealT>()) ) { |
| 540 |
errorFlag += 1; |
| 541 |
} |
| 542 |
} |
| 543 |
catch (std::logic_error& err) { |
| 544 |
*outStream << err.what() << "\n"; |
| 545 |
errorFlag = -1000; |
| 546 |
}; // end try |
| 547 |
|
| 548 |
if (errorFlag != 0) |
| 549 |
std::cout << "End Result: TEST FAILED\n"; |
| 550 |
else |
| 551 |
std::cout << "End Result: TEST PASSED\n"; |
| 552 |
|
| 553 |
return 0; |
| 554 |
|
| 555 |
} |
| 556 |
|