ROL
step/fletcher/test_02.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 //
39 // Drew Kouri (dpkouri@sandia.gov) and
40 // Denis Ridzal (dridzal@sandia.gov)
41 //
42 // ************************************************************************
43 // @HEADER
44 
49 #include "ROL_HS29.hpp"
50 #include "ROL_Algorithm.hpp"
51 #include "ROL_BoundFletcher.hpp"
52 #include "ROL_Bounds.hpp"
53 #include "ROL_Constraint.hpp"
55 
56 #include "ROL_Stream.hpp"
57 #include "Teuchos_GlobalMPISession.hpp"
58 
59 
60 typedef double RealT;
61 
62 int main(int argc, char *argv[]) {
63 
64 
65  typedef std::vector<RealT> vec;
66  typedef ROL::StdVector<RealT> SV;
67  typedef ROL::Ptr<ROL::Vector<RealT> > PtrV;
68 
69  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
70 
71  std::string filename = "input_ex02.xml";
72 
73  auto parlist = ROL::getParametersFromXmlFile( filename );
74 
75  int iprint = argc - 1;
76  ROL::Ptr<std::ostream> outStream;
77  ROL::nullstream bhs; // outputs nothing
78  if (iprint > 0)
79  outStream = ROL::makePtrFromRef(std::cout);
80  else
81  outStream = ROL::makePtrFromRef(bhs);
82 
83  int errorFlag = 0;
84 
85  try {
86 
87  int xopt_dim = 3; // Dimension of optimization vectors
88  int ci_dim = 1; // Dimension of inequality constraint
89 
90  ROL::Ptr<vec> xopt_ptr = ROL::makePtr<vec>(xopt_dim,1.0); // Feasible initial guess
91  ROL::Ptr<vec> li_ptr = ROL::makePtr<vec>(ci_dim,0.0);
92 
93  PtrV xopt = ROL::makePtr<SV>(xopt_ptr);
94  PtrV li = ROL::makePtr<SV>(li_ptr);
95 
96  // Original obective
99 
100  ROL::Ptr<ROL::Objective<RealT> > obj_hs29 = ROL::makePtr<Objective_HS29<RealT>>();
101  ROL::Ptr<ROL::Constraint<RealT> > incon_hs29 = ROL::makePtr<InequalityConstraint_HS29<RealT>>();
102 
103  ROL::Ptr<ROL::Vector<RealT> > bndc = li->clone();
104  ROL::Ptr<ROL::BoundConstraint<RealT> > bndcon = ROL::makePtr<ROL::Bounds<RealT> >(*bndc);
105 
106  ROL::Ptr<vec> low_ptr = ROL::makePtr<vec>(xopt_dim, ROL::ROL_NINF<RealT>());
107  ROL::Ptr<vec> upp_ptr = ROL::makePtr<vec>(xopt_dim, ROL::ROL_INF<RealT>());
108  PtrV low = ROL::makePtr<SV>(low_ptr);
109  PtrV upp = ROL::makePtr<SV>(upp_ptr);
110  ROL::Ptr<ROL::BoundConstraint<RealT> > bndx = ROL::makePtr<ROL::Bounds<RealT> >(low, upp);
111 
112  std::string stepname = "Trust Region";
113 
114  ROL::OptimizationProblem<RealT> problem( obj_hs29, xopt, bndx, incon_hs29, li, bndcon );
115 
116  ROL::Ptr<ROL::Objective<RealT> > obj = problem.getObjective();
117  ROL::Ptr<ROL::Constraint<RealT> > con = problem.getConstraint();
118  ROL::Ptr<ROL::BoundConstraint<RealT> > bndxs = problem.getBoundConstraint();
119  ROL::Ptr<ROL::Vector<RealT> > xs = problem.getSolutionVector();
120  ROL::Ptr<ROL::Vector<RealT> > lis = problem.getMultiplierVector();
121 
122  ROL::Ptr<ROL::BoundFletcher<RealT> > fletcher_penalty = ROL::makePtr<ROL::BoundFletcher<RealT> >(obj, con, bndxs, *xs, *lis, *parlist);
123 
124  ROL::Ptr<ROL::Vector<RealT> > v = xs->clone(); v->randomize();
125  std::vector<std::vector<RealT> > gCheck = fletcher_penalty->checkGradient(*xs, *v, true );
126 
127  ROL::Ptr<ROL::Vector<RealT> > w = xs->clone(); w->randomize();
128  std::vector<RealT> hCheck = fletcher_penalty->checkHessSym( *xs, *v, *w, true, *outStream);
129 
130  // Define algorithm.
131  // ROL::Ptr<ROL::Algorithm<RealT> > algo;
132  // algo = ROL::makePtr<ROL::Algorithm<RealT>>(stepname, *parlist);
133  // algo->run(*xs, *fletcher_penalty, *bndxs, true, *outStream);
134 
135  ROL::OptimizationSolver<RealT> optSolver(problem, *parlist);
136  optSolver.solve(*outStream);
137 
138  *outStream << std::endl << std::setw(20) << "Computed Minimizer" << std::endl;
139  for( int i=0;i<xopt_dim;++i ) {
140  *outStream << std::setw(20) << (*xopt_ptr)[i] << std::endl;
141  }
142 
143  *outStream << "Exact minimizers: x* = (a,b,c), (a,-b,-c), (-a,b,-c), (-a,-b,c)" << std::endl;
144  *outStream << "Where a=4, b=" << 2*std::sqrt(2) << ", and c=2" << std::endl;
145 
146  }
147  catch (std::logic_error& err) {
148  *outStream << err.what() << "\n";
149  errorFlag = -1000;
150  }; // end try
151 
152  if (errorFlag != 0)
153  std::cout << "End Result: TEST FAILED\n";
154  else
155  std::cout << "End Result: TEST PASSED\n";
156 
157  return 0;
158 
159 
160 
161 }
Contains definitions for W. Hock and K. Schittkowski 32nd test problem which contains only inequality...
virtual Ptr< Objective< Real > > getObjective(void)
virtual Ptr< BoundConstraint< Real > > getBoundConstraint(void)
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...
virtual Ptr< Vector< Real > > getSolutionVector(void)
virtual Ptr< Vector< Real > > getMultiplierVector(void)
Provides a simplified interface for solving a wide range of optimization problems.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
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.
virtual Ptr< Constraint< Real > > getConstraint(void)