ROL
step/test_05.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"
18 #include "ROL_Stream.hpp"
19 #include "Teuchos_GlobalMPISession.hpp"
20 
21 
22 #include <iostream>
23 
24 typedef double RealT;
25 
26 int main(int argc, char *argv[]) {
27 
28  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
29 
30  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
31  int iprint = argc - 1;
32  ROL::Ptr<std::ostream> outStream;
33  ROL::nullstream bhs; // outputs nothing
34  if (iprint > 0)
35  outStream = ROL::makePtrFromRef(std::cout);
36  else
37  outStream = ROL::makePtrFromRef(bhs);
38 
39  int errorFlag = 0;
40 
41  // *** Test body.
42 
43  try {
44 
45  std::string filename = "input.xml";
46 
47  auto parlist = ROL::getParametersFromXmlFile( filename );
48  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",true);
49 #if USE_HESSVEC
50  parlist->sublist("General").set("Inexact Hessian-Times-A-Vector",false);
51 #endif
52 
53  // Krylov parameters.
54  parlist->sublist("General").sublist("Krylov").set("Type", "Conjugate Residuals");
55  parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance", 1.e-8);
56  parlist->sublist("General").sublist("Krylov").set("Relative Tolerance", 1.e-4);
57  parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 50);
58  parlist->sublist("Step").set("Type","Primal Dual Active Set");
59 
61  // Get Objective Function
62  ROL::Ptr<ROL::Vector<RealT> > x0;
63  std::vector<ROL::Ptr<ROL::Vector<RealT> > > z;
64  ROL::Ptr<ROL::OptimizationProblem<RealT> > problem;
65  ROL::GetTestProblem<RealT>(problem,x0,z,prob);
66 
67  if (problem->getProblemType() == ROL::TYPE_B) {
68  if ( prob != ROL::TESTOPTPROBLEM_HS5 ) {
69  // PDAS parameters.
70  if (prob == ROL::TESTOPTPROBLEM_HS1 ||
71  prob == ROL::TESTOPTPROBLEM_HS2 ||
72  prob == ROL::TESTOPTPROBLEM_HS3 ||
73  prob == ROL::TESTOPTPROBLEM_HS4 ||
74  prob == ROL::TESTOPTPROBLEM_HS45) {
75  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
76  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
77  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit",1);
78  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",1.e8);
79  }
80  else if (prob == ROL::TESTOPTPROBLEM_HS5) {
81  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
82  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
83  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit",10);
84  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",1.e-2);
85  }
86  else if (prob == ROL::TESTOPTPROBLEM_HS25) {
87  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
88  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
89  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit",10);
90  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",1.e10);
91  }
92  else if (prob == ROL::TESTOPTPROBLEM_HS38) {
93  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
94  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
95  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit",1);
96  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",1.e-3);
97  }
98  else if (prob == ROL::TESTOPTPROBLEM_BVP) {
99  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
100  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
101  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit",1);
102  parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",1.e0);
103  }
104  *outStream << std::endl << std::endl << ROL:: ETestOptProblemToString(prob) << std::endl << std::endl;
105 
106  // Get Dimension of Problem
107  int dim = x0->dimension();
108  parlist->sublist("General").sublist("Krylov").set("Iteration Limit", 2*dim);
109 
110  // Error Vector
111  ROL::Ptr<ROL::Vector<RealT> > e = x0->clone();
112  e->zero();
113 
114  // Define Solver
115  ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
116 
117  // Run Solver
118  solver.solve(*outStream);
119 
120  // Compute Error
121  RealT err(0);
122  for (int i = 0; i < static_cast<int>(z.size()); ++i) {
123  e->set(*x0);
124  e->axpy(-1.0,*z[i]);
125  if (i == 0) {
126  err = e->norm();
127  }
128  else {
129  err = std::min(err,e->norm());
130  }
131  }
132  *outStream << std::endl << "Norm of Error: " << err << std::endl;
133 
134  // Update error flag
135  ROL::Ptr<const ROL::AlgorithmState<RealT> > state = solver.getAlgorithmState();
136  errorFlag += ((err < std::max(1.e-6*z[0]->norm(),1.e-8) || (state->gnorm < 1.e-6)) ? 0 : 1);
137  }
138  }
139  }
140  }
141  catch (std::logic_error& err) {
142  *outStream << err.what() << std::endl;
143  errorFlag = -1000;
144  }; // end try
145 
146  if (errorFlag != 0)
147  std::cout << "End Result: TEST FAILED" << std::endl;
148  else
149  std::cout << "End Result: TEST PASSED" << std::endl;
150 
151  return 0;
152 
153 }
ETestOptProblem
Enumeration of test optimization problems.
Contains definitions of test objective functions.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
std::string ETestOptProblemToString(ETestOptProblem to)
Provides a simplified interface for solving a wide range of optimization problems.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
constexpr auto dim
ROL::Ptr< const AlgorithmState< Real > > getAlgorithmState(void) const
Return the AlgorithmState.