ROL
function/constraint/test_01.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
50 
53 
56 
57 
58 #include "ROL_Stream.hpp"
59 #include "Teuchos_GlobalMPISession.hpp"
60 
61 #include <iostream>
62 
63 typedef double RealT;
64 
65 int main(int argc, char *argv[]) {
66 
67  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
68 
69 
70 
71  using Obj = ROL::Objective<RealT>;
72  using Con = ROL::Constraint<RealT>;
73  using V = ROL::Vector<RealT>;
74  using StdV = ROL::StdVector<RealT>;
75  using ScalarV = ROL::SingletonVector<RealT>;
76 
77  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
78  int iprint = argc - 1;
79  ROL::Ptr<std::ostream> outStream;
80  ROL::nullstream bhs; // outputs nothing
81  if (iprint > 0)
82  outStream = ROL::makePtrFromRef(std::cout);
83  else
84  outStream = ROL::makePtrFromRef(bhs);
85 
86  // Save the format state of the original std::cout.
87  ROL::nullstream oldFormatState;
88  oldFormatState.copyfmt(std::cout);
89 
90  int errorFlag = 0;
91 
92  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
93 
94  // *** Test body.
95 
96  try {
97 
98  int xdim = 5;
99  int cdim = 3;
100 
101  // Optimization vector
102  ROL::Ptr<V> x = ROL::makePtr<StdV>( ROL::makePtr<std::vector<RealT>>(xdim) );
103  ROL::Ptr<V> c = ROL::makePtr<StdV>( ROL::makePtr<std::vector<RealT>>(cdim));
104  ROL::Ptr<V> e0 = c->basis(0);
105  ROL::Ptr<V> e1 = c->basis(1);
106  ROL::Ptr<V> e2 = c->basis(2);
107 
108  // Exact solution
109  ROL::Ptr<V> sol = x->clone();
110  ROL::Ptr<V> error = x->clone();
111 
112  ROL::Ptr<Obj> obj = ROL::nullPtr;
113  ROL::Ptr<Con> con = ROL::nullPtr;
114 
116  obj = SEC.getObjective();
117  con = SEC.getEqualityConstraint();
118  x = SEC.getInitialGuess();
119  sol = SEC.getSolution();
120 
121  error->set(*sol);
122 
123  // Extract constraint components to make objectives
124  ROL::Ptr<Obj> obj_0 = ROL::makePtr<ROL::ObjectiveFromConstraint<RealT>>( con, *e0 );
125  ROL::Ptr<Obj> obj_1 = ROL::makePtr<ROL::ObjectiveFromConstraint<RealT>>( con, *e1 );
126  ROL::Ptr<Obj> obj_2 = ROL::makePtr<ROL::ObjectiveFromConstraint<RealT>>( con, *e2 );
127 
128  // Create separate constraints from the objectives
129  ROL::Ptr<Con> con_0 = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>( obj_0 );
130  ROL::Ptr<Con> con_1 = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>( obj_1 );
131  ROL::Ptr<Con> con_2 = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>( obj_2 );
132 
133  std::vector<ROL::Ptr<Con>> con_array;
134  con_array.push_back(con_0);
135  con_array.push_back(con_1);
136  con_array.push_back(con_2);
137 
138  // Lagrange multipliers
139  ROL::Ptr<V> l0 = ROL::makePtr<ScalarV>(0);
140  ROL::Ptr<V> l1 = ROL::makePtr<ScalarV>(0);
141  ROL::Ptr<V> l2 = ROL::makePtr<ScalarV>(0);
142 
143  std::vector<ROL::Ptr<V>> l_array;
144  l_array.push_back(l0);
145  l_array.push_back(l1);
146  l_array.push_back(l2);
147 
148  ROL::OptimizationProblem<RealT> opt( obj, // Objective
149  x, // Optimization vector
150  ROL::nullPtr, // No bound constraint
151  con_array, // Array of scalar equality constraints
152  l_array); // Array of scalar lagrange multipliers
153 
154  opt.check(*outStream);
155 
156  // Define algorithm.
157  ROL::ParameterList parlist;
158  std::string stepname = "Composite Step";
159  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1.e-4);
160  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
161  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
162  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
163  parlist.sublist("Step").sublist(stepname).set("Output Level",0);
164  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
165  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
166  parlist.sublist("Status Test").set("Step Tolerance",1.e-18);
167  parlist.sublist("Status Test").set("Iteration Limit",100);
168 
169  ROL::OptimizationSolver<RealT> solver( opt, parlist );
170 
171  solver.solve( *outStream );
172 
173  error->axpy(-1.0,*x);
174  RealT error_norm = error->norm();
175 
176  *outStream << "\n\n Relative norm of final optimization vector error: " << error_norm << std::endl;
177 
178  if(error_norm>tol)
179  ++errorFlag;
180 
181  }
182  catch (std::logic_error& err) {
183  *outStream << err.what() << "\n";
184  errorFlag = -1000;
185  }; // end try
186 
187  if (errorFlag != 0)
188  std::cout << "End Result: TEST FAILED\n";
189  else
190  std::cout << "End Result: TEST PASSED\n";
191 
192  // reset format state of std::cout
193  std::cout.copyfmt(oldFormatState);
194 
195  return 0;
196 
197 }
198 
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:80
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:72
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
Defines the general constraint operator interface.