ROL
sacado/example_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// Drew Kouri (dpkouri@sandia.gov) and
39// Denis Ridzal (dridzal@sandia.gov)
40//
41// ************************************************************************
42// @HEADER
43
55#include <iostream>
56
57#include "ROL_Sacado_Objective.hpp"
58#include "ROL_Sacado_Constraint.hpp"
59
60#include "ROL_Algorithm.hpp"
61#include "ROL_CompositeStep.hpp"
63#include "ROL_Constraint.hpp"
64#include "ROL_ParameterList.hpp"
65
66#include "ROL_Stream.hpp"
67#include "Teuchos_GlobalMPISession.hpp"
68
69#include "example_02.hpp"
70
71using namespace ROL;
72
73typedef double RealT;
74
75int main(int argc, char **argv)
76{
77
78
79 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
80
81 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
82 int iprint = argc - 1;
83 ROL::Ptr<std::ostream> outStream;
84 ROL::nullstream bhs; // outputs nothing
85 if (iprint > 0)
86 outStream = ROL::makePtrFromRef(std::cout);
87 else
88 outStream = ROL::makePtrFromRef(bhs);
89
90 int errorFlag = 0;
91
92 // *** Example body.
93
94 try {
95
96 // Run derivative checks, etc.
97 int dim = 5;
98 int nc = 3;
99
100 ROL::Ptr< Sacado_Objective<RealT,Example_Objective> > obj =
101 ROL::makePtr<Sacado_Objective<RealT,Example_Objective>>();
102
103 ROL::Ptr< Sacado_Constraint<RealT,Example_Constraint > > constr =
104 ROL::makePtr<Sacado_Constraint<RealT,Example_Constraint >>(nc);
105
106 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
107
108 ROL::Ptr<std::vector<RealT> > sol_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
109 ROL::StdVector<RealT> x(x_ptr); // Iteration vector.
110 ROL::StdVector<RealT> sol(sol_ptr); // Reference solution vector.
111
112 // Get initial guess
113 (*x_ptr)[0] = -1.8;
114 (*x_ptr)[1] = 1.7;
115 (*x_ptr)[2] = 1.9;
116 (*x_ptr)[3] = -0.8;
117 (*x_ptr)[4] = -0.8;
118
119 // Get solution
120 (*sol_ptr)[0] = -1.717143570394391e+00;
121 (*sol_ptr)[1] = 1.595709690183565e+00;
122 (*sol_ptr)[2] = 1.827245752927178e+00;
123 (*sol_ptr)[3] = -7.636430781841294e-01;
124 (*sol_ptr)[4] = -7.636430781841294e-01;
125
126 RealT left = -1e0, right = 1e0;
127 ROL::Ptr<std::vector<RealT> > xtest_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
128 ROL::Ptr<std::vector<RealT> > g_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
129 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
130 ROL::Ptr<std::vector<RealT> > v_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
131 ROL::Ptr<std::vector<RealT> > vc_ptr = ROL::makePtr<std::vector<RealT>>(nc, 0.0);
132 ROL::Ptr<std::vector<RealT> > vl_ptr = ROL::makePtr<std::vector<RealT>>(nc, 0.0);
133 ROL::StdVector<RealT> xtest(xtest_ptr);
134 ROL::StdVector<RealT> g(g_ptr);
135 ROL::StdVector<RealT> d(d_ptr);
136 ROL::StdVector<RealT> v(v_ptr);
137 ROL::StdVector<RealT> vc(vc_ptr);
138 ROL::StdVector<RealT> vl(vl_ptr);
139
140 // set xtest, d, v
141 for (int i=0; i<dim; i++) {
142 (*xtest_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
143 (*d_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
144 (*v_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
145 }
146 // set vc, vl
147 for (int i=0; i<nc; i++) {
148 (*vc_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
149 (*vl_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
150 }
151
152 obj->checkGradient(xtest, d, true, *outStream); *outStream << "\n";
153 obj->checkHessVec(xtest, v, true, *outStream); *outStream << "\n";
154 obj->checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n";
155 constr->checkApplyJacobian(xtest, v, vc, true, *outStream); *outStream << "\n";
156 constr->checkApplyAdjointJacobian(xtest, vl, vc, xtest, true, *outStream); *outStream << "\n";
157 constr->checkApplyAdjointHessian(xtest, vl, d, xtest, true, *outStream); *outStream << "\n";
158
159 ROL::Ptr<std::vector<RealT> > v1_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
160 ROL::Ptr<std::vector<RealT> > v2_ptr = ROL::makePtr<std::vector<RealT>>(nc, 0.0);
161 ROL::StdVector<RealT> v1(v1_ptr);
162 ROL::StdVector<RealT> v2(v2_ptr);
163 RealT augtol = 1e-8;
164 constr->solveAugmentedSystem(v1, v2, d, vc, xtest, augtol);
165
166 // Define algorithm.
167 std::string paramfile = "parameters.xml";
168 auto parlist = ROL::getParametersFromXmlFile(paramfile);
169 ROL::Ptr<ROL::Step<RealT>>
170 step = ROL::makePtr<ROL::CompositeStep<RealT>>(*parlist);
171 ROL::Ptr<ROL::StatusTest<RealT>>
172 status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist);
173 ROL::Algorithm<RealT> algo(step,status,false);
174
175 // Run algorithm.
176 vl.zero();
177 algo.run(x, g, vl, vc, *obj, *constr, true, *outStream);
178
179 // Compute Error
180 *outStream << "\nReference solution x_r =\n";
181 *outStream << std::scientific << " " << (*sol_ptr)[0] << "\n";
182 *outStream << std::scientific << " " << (*sol_ptr)[1] << "\n";
183 *outStream << std::scientific << " " << (*sol_ptr)[2] << "\n";
184 *outStream << std::scientific << " " << (*sol_ptr)[3] << "\n";
185 *outStream << std::scientific << " " << (*sol_ptr)[4] << "\n";
186 *outStream << "\nOptimal solution x =\n";
187 *outStream << std::scientific << " " << (*x_ptr)[0] << "\n";
188 *outStream << std::scientific << " " << (*x_ptr)[1] << "\n";
189 *outStream << std::scientific << " " << (*x_ptr)[2] << "\n";
190 *outStream << std::scientific << " " << (*x_ptr)[3] << "\n";
191 *outStream << std::scientific << " " << (*x_ptr)[4] << "\n";
192 x.axpy(-1.0, sol);
193 RealT abserr = x.norm();
194 RealT relerr = abserr/sol.norm();
195 *outStream << std::scientific << "\n Absolute Error: " << abserr;
196 *outStream << std::scientific << "\n Relative Error: " << relerr << "\n";
197 if ( relerr > sqrt(ROL::ROL_EPSILON<RealT>()) ) {
198 errorFlag += 1;
199 }
200 }
201 catch (std::logic_error& err) {
202 *outStream << err.what() << "\n";
203 errorFlag = -1000;
204 }; // end try
205
206 if (errorFlag != 0)
207 std::cout << "End Result: TEST FAILED\n";
208 else
209 std::cout << "End Result: TEST PASSED\n";
210
211 return 0;
212
213
214}
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides an interface to run optimization algorithms.
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.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Real norm() const
Returns where .
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
int main(int argc, char **argv)
double RealT
constexpr auto dim