ROL
function/test_07.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
44#include "ROL_Stream.hpp"
45#include "Teuchos_GlobalMPISession.hpp"
46
47#include "ROL_StdVector.hpp"
48#include "ROL_StdObjective.hpp"
50
51typedef double RealT;
52
53template<class Real>
55public:
56 Real value( const std::vector<Real> &x, Real &tol ) {
57 Real half(0.5), quad(0);
58 unsigned size = x.size();
59 for ( unsigned i = 0; i < size; i++ ) {
60 quad += x[i]*x[i];
61 }
62 return half*quad;
63 }
64
65 void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
66 g.assign(x.begin(),x.end());
67 }
68
69 void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
70 hv.assign(v.begin(),v.end());
71 }
72};
73
74template<class Real>
76public:
77 Real value( const std::vector<Real> &x, Real &tol ) {
78 Real lin(0);
79 unsigned size = x.size();
80 for ( unsigned i = 0; i < size; i++ ) {
81 lin += x[i];
82 }
83 return lin;
84 }
85
86 void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
87 g.assign(x.size(),1);
88 }
89
90 void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
91 hv.assign(x.size(),0);
92 }
93};
94
95template<class Real>
97public:
98 Real value( const std::vector<Real> &x, Real &tol ) {
99 return std::log(x[0]) * std::exp(x[1]);
100 }
101
102 void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
103 g[0] = std::exp(x[1])/x[0];
104 g[1] = std::exp(x[1]) * std::log(x[0]);
105 }
106
107 void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
108 Real H11 = -std::exp(x[1])/(x[0]*x[0]);
109 Real H12 = std::exp(x[1])/x[0];
110 Real H21 = std::exp(x[1])/x[0];
111 Real H22 = std::exp(x[1]) * std::log(x[0]);
112 hv[0] = H11*v[0] + H12*v[1];
113 hv[1] = H21*v[0] + H22*v[1];
114 }
115};
116
117void setRandomVector(std::vector<RealT> &x) {
118 unsigned dim = x.size();
119 for ( unsigned i = 0; i < dim; i++ ) {
120 x[i] = (RealT)rand()/(RealT)RAND_MAX;
121 }
122}
123
124int main(int argc, char* argv[]) {
125
126 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
127
128 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
129 int iprint = argc - 1;
130 ROL::Ptr<std::ostream> outStream;
131 ROL::nullstream bhs; // outputs nothing
132 if (iprint > 0)
133 outStream = ROL::makePtrFromRef(std::cout);
134 else
135 outStream = ROL::makePtrFromRef(bhs);
136
137 int errorFlag = 0;
138
139 try {
140 /**********************************************************************************************/
141 /************************* CONSTRUCT SOL COMPONENTS *******************************************/
142 /**********************************************************************************************/
143 // Build vectors
144 unsigned dim = 4;
145 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
146 ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
147 setRandomVector(*x_ptr);
148 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
149 ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
150 setRandomVector(*d_ptr);
151 // Build objective function
152 std::vector<ROL::Ptr<ROL::Objective<RealT> > > vec_obj(2,ROL::nullPtr);
153 vec_obj[0] = ROL::makePtr<ObjectiveFunctionTest07_1<RealT>>();
154 vec_obj[1] = ROL::makePtr<ObjectiveFunctionTest07_2<RealT>>();
155 ROL::Ptr<ROL::StdObjective<RealT> > obj_scalarize
156 = ROL::makePtr<ObjectiveFunctionTest07_scalarize<RealT>>();
157 ROL::Ptr<ROL::Objective<RealT> > obj
158 = ROL::makePtr<ROL::CompositeObjective<RealT>>(vec_obj,obj_scalarize);
159 // Test parametrized objective functions
160 *outStream << "Check Derivatives of CompositeObjective\n";
161 obj->checkGradient(*x,*d,true,*outStream);
162 obj->checkHessVec(*x,*d,true,*outStream);
163 }
164 catch (std::logic_error& err) {
165 *outStream << err.what() << "\n";
166 errorFlag = -1000;
167 }; // end try
168
169 if (errorFlag != 0)
170 std::cout << "End Result: TEST FAILED\n";
171 else
172 std::cout << "End Result: TEST PASSED\n";
173
174 return 0;
175}
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Real value(const std::vector< Real > &x, Real &tol)
Real value(const std::vector< Real > &x, Real &tol)
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Real value(const std::vector< Real > &x, Real &tol)
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector's.
int main(int argc, char *argv[])
void setRandomVector(std::vector< RealT > &x)
double RealT
constexpr auto dim