ROL
function/constraint/test_01.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 
16 
19 
22 
23 
24 #include "ROL_Stream.hpp"
25 #include "Teuchos_GlobalMPISession.hpp"
26 
27 #include <iostream>
28 
29 typedef double RealT;
30 
31 int main(int argc, char *argv[]) {
32 
33  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
34 
35 
36 
37  using Obj = ROL::Objective<RealT>;
38  using Con = ROL::Constraint<RealT>;
39  using V = ROL::Vector<RealT>;
40  using StdV = ROL::StdVector<RealT>;
41  using ScalarV = ROL::SingletonVector<RealT>;
42 
43  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
44  int iprint = argc - 1;
45  ROL::Ptr<std::ostream> outStream;
46  ROL::nullstream bhs; // outputs nothing
47  if (iprint > 0)
48  outStream = ROL::makePtrFromRef(std::cout);
49  else
50  outStream = ROL::makePtrFromRef(bhs);
51 
52  // Save the format state of the original std::cout.
53  ROL::nullstream oldFormatState;
54  oldFormatState.copyfmt(std::cout);
55 
56  int errorFlag = 0;
57 
58  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
59 
60  // *** Test body.
61 
62  try {
63 
64  int xdim = 5;
65  int cdim = 3;
66 
67  // Optimization vector
68  ROL::Ptr<V> x = ROL::makePtr<StdV>( ROL::makePtr<std::vector<RealT>>(xdim) );
69  ROL::Ptr<V> c = ROL::makePtr<StdV>( ROL::makePtr<std::vector<RealT>>(cdim));
70  ROL::Ptr<V> e0 = c->basis(0);
71  ROL::Ptr<V> e1 = c->basis(1);
72  ROL::Ptr<V> e2 = c->basis(2);
73 
74  // Exact solution
75  ROL::Ptr<V> sol = x->clone();
76  ROL::Ptr<V> error = x->clone();
77 
78  ROL::Ptr<Obj> obj = ROL::nullPtr;
79  ROL::Ptr<Con> con = ROL::nullPtr;
80 
82  obj = SEC.getObjective();
83  con = SEC.getEqualityConstraint();
84  x = SEC.getInitialGuess();
85  sol = SEC.getSolution();
86 
87  error->set(*sol);
88 
89  // Extract constraint components to make objectives
90  ROL::Ptr<Obj> obj_0 = ROL::makePtr<ROL::ObjectiveFromConstraint<RealT>>( con, *e0 );
91  ROL::Ptr<Obj> obj_1 = ROL::makePtr<ROL::ObjectiveFromConstraint<RealT>>( con, *e1 );
92  ROL::Ptr<Obj> obj_2 = ROL::makePtr<ROL::ObjectiveFromConstraint<RealT>>( con, *e2 );
93 
94  // Create separate constraints from the objectives
95  ROL::Ptr<Con> con_0 = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>( obj_0 );
96  ROL::Ptr<Con> con_1 = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>( obj_1 );
97  ROL::Ptr<Con> con_2 = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>( obj_2 );
98 
99  std::vector<ROL::Ptr<Con>> con_array;
100  con_array.push_back(con_0);
101  con_array.push_back(con_1);
102  con_array.push_back(con_2);
103 
104  // Lagrange multipliers
105  ROL::Ptr<V> l0 = ROL::makePtr<ScalarV>(0);
106  ROL::Ptr<V> l1 = ROL::makePtr<ScalarV>(0);
107  ROL::Ptr<V> l2 = ROL::makePtr<ScalarV>(0);
108 
109  std::vector<ROL::Ptr<V>> l_array;
110  l_array.push_back(l0);
111  l_array.push_back(l1);
112  l_array.push_back(l2);
113 
114  ROL::OptimizationProblem<RealT> opt( obj, // Objective
115  x, // Optimization vector
116  ROL::nullPtr, // No bound constraint
117  con_array, // Array of scalar equality constraints
118  l_array); // Array of scalar lagrange multipliers
119 
120  opt.check(*outStream);
121 
122  // Define algorithm.
123  ROL::ParameterList parlist;
124  std::string stepname = "Composite Step";
125  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1.e-4);
126  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
127  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
128  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
129  parlist.sublist("Step").sublist(stepname).set("Output Level",0);
130  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
131  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
132  parlist.sublist("Status Test").set("Step Tolerance",1.e-18);
133  parlist.sublist("Status Test").set("Iteration Limit",100);
134 
135  ROL::OptimizationSolver<RealT> solver( opt, parlist );
136 
137  solver.solve( *outStream );
138 
139  error->axpy(-1.0,*x);
140  RealT error_norm = error->norm();
141 
142  *outStream << "\n\n Relative norm of final optimization vector error: " << error_norm << std::endl;
143 
144  if(error_norm>tol)
145  ++errorFlag;
146 
147  }
148  catch (std::logic_error& err) {
149  *outStream << err.what() << "\n";
150  errorFlag = -1000;
151  }; // end try
152 
153  if (errorFlag != 0)
154  std::cout << "End Result: TEST FAILED\n";
155  else
156  std::cout << "End Result: TEST PASSED\n";
157 
158  // reset format state of std::cout
159  std::cout.copyfmt(oldFormatState);
160 
161  return 0;
162 
163 }
164 
Provides the interface to evaluate objective functions.
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Objective_TimeSimOpt< Real > Obj
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Ptr< Objective< Real > > getObjective(void) const
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.
Provides a simplified interface for solving a wide range of optimization problems.
Ptr< Vector< Real > > getSolution(const int i=0) const
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
void check(std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
int solve(const ROL::Ptr< StatusTest< Real > > &status=ROL::nullPtr, const bool combineStatus=true)
Solve optimization problem with no iteration output.
Ptr< Vector< Real > > getInitialGuess(void) const