ROL
step/test_10.cpp
Go to the documentation of this file.
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 
14 #define USE_HESSVEC 1
15 
16 #include "ROL_GetTestProblems.hpp"
17 #include "ROL_Algorithm.hpp"
18 #include "ROL_Stream.hpp"
20 
21 #include "Teuchos_GlobalMPISession.hpp"
22 
23 #include <iostream>
24 
25 typedef double RealT;
26 
27 int main(int argc, char *argv[]) {
28 
29  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
30 
31  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
32  int iprint = argc - 1;
33  ROL::Ptr<std::ostream> outStream;
34  ROL::nullstream bhs; // outputs nothing
35  if (iprint > 0)
36  outStream = ROL::makePtrFromRef(std::cout);
37  else
38  outStream = ROL::makePtrFromRef(bhs);
39 
40  int errorFlag = 0;
41 
42  // *** Test body.
43 
44  try {
45  // Get Objective Function
46  ROL::Ptr<ROL::Vector<RealT> > x0;
47  std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
48  ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
49  ROL::GetTestProblem<RealT>(problem,x0,z,ROL::TESTOPTPROBLEM_HS38);
50  // Parse input
51  std::string filename = "input.xml";
52 
53  auto parlist = ROL::getParametersFromXmlFile( filename );
54  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",true);
55 #if USE_HESSVEC
56  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",false);
57 #endif
58 
59  RealT mu = 0.1; // Initial penalty parameter
60  RealT factor = 0.1; // Penalty reduction factor
61 
62  // Set solver parameters
63  parlist->sublist("Step").set("Type","Interior Point");
64  parlist->sublist("Step").sublist("Interior Point").set("Initial Barrier Penalty",mu);
65  parlist->sublist("Step").sublist("Interior Point").set("Minimum Barrier Penalty",1e-8);
66  parlist->sublist("Step").sublist("Interior Point").set("Barrier Penalty Reduction Factor",factor);
67  parlist->sublist("Step").sublist("Interior Point").set("Subproblem Iteration Limit",30);
68 
69  parlist->sublist("Step").sublist("Composite Step").sublist("Optimality System Solver").set("Nominal Relative Tolerance",1.e-4);
70  parlist->sublist("Step").sublist("Composite Step").sublist("Optimality System Solver").set("Fix Tolerance",true);
71  parlist->sublist("Step").sublist("Composite Step").sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
72  parlist->sublist("Step").sublist("Composite Step").sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
73  parlist->sublist("Step").sublist("Composite Step").set("Output Level",0);
74 
75  parlist->sublist("Status Test").set("Gradient Tolerance",1.e-12);
76  parlist->sublist("Status Test").set("Constraint Tolerance",1.e-8);
77  parlist->sublist("Status Test").set("Step Tolerance",1.e-8);
78  parlist->sublist("Status Test").set("Iteration Limit",100);
79 
80  // Solve optimization problem with interior points
81  ROL::Ptr<ROL::Step<RealT>>
82  step = ROL::makePtr<ROL::InteriorPointStep<RealT>>(*parlist);
83  ROL::Ptr<ROL::StatusTest<RealT>>
84  status = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
85  ROL::Algorithm<RealT> algo(step,status,false);
86  algo.run(optProb, true, *outStream);
87 
88  // Compute Error
89  ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
90  RealT err(0);
91  for (int i = 0; i < static_cast<int>(z.size()); ++i) {
92  e->set(*x0);
93  e->axpy(-1.0,*z[i]);
94  if (i == 0) {
95  err = e->norm();
96  }
97  else {
98  err = std::min(err,e->norm());
99  }
100  }
101  *outStream << std::endl << "Norm of Error: " << err << std::endl;
102  }
103  catch (std::logic_error& err) {
104  *outStream << err.what() << std::endl;
105  errorFlag = -1000;
106  }; // end try
107 
108  if (errorFlag != 0)
109  std::cout << "End Result: TEST FAILED" << std::endl;
110  else
111  std::cout << "End Result: TEST PASSED" << std::endl;
112 
113  return 0;
114 
115 }
116 
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
Contains definitions of test objective functions.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides an interface to run optimization algorithms.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])