ROL
sacado/example_02.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
21#include <iostream>
22
23#include "ROL_Sacado_Objective.hpp"
24#include "ROL_Sacado_Constraint.hpp"
25
26#include "ROL_Algorithm.hpp"
27#include "ROL_CompositeStep.hpp"
29#include "ROL_Constraint.hpp"
30#include "ROL_ParameterList.hpp"
31
32#include "ROL_Stream.hpp"
33#include "Teuchos_GlobalMPISession.hpp"
34
35#include "example_02.hpp"
36
37using namespace ROL;
38
39typedef double RealT;
40
41int main(int argc, char **argv)
42{
43
44
45 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
46
47 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
48 int iprint = argc - 1;
49 ROL::Ptr<std::ostream> outStream;
50 ROL::nullstream bhs; // outputs nothing
51 if (iprint > 0)
52 outStream = ROL::makePtrFromRef(std::cout);
53 else
54 outStream = ROL::makePtrFromRef(bhs);
55
56 int errorFlag = 0;
57
58 // *** Example body.
59
60 try {
61
62 // Run derivative checks, etc.
63 int dim = 5;
64 int nc = 3;
65
66 ROL::Ptr< Sacado_Objective<RealT,Example_Objective> > obj =
67 ROL::makePtr<Sacado_Objective<RealT,Example_Objective>>();
68
69 ROL::Ptr< Sacado_Constraint<RealT,Example_Constraint > > constr =
70 ROL::makePtr<Sacado_Constraint<RealT,Example_Constraint >>(nc);
71
72 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
73
74 ROL::Ptr<std::vector<RealT> > sol_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
75 ROL::StdVector<RealT> x(x_ptr); // Iteration vector.
76 ROL::StdVector<RealT> sol(sol_ptr); // Reference solution vector.
77
78 // Get initial guess
79 (*x_ptr)[0] = -1.8;
80 (*x_ptr)[1] = 1.7;
81 (*x_ptr)[2] = 1.9;
82 (*x_ptr)[3] = -0.8;
83 (*x_ptr)[4] = -0.8;
84
85 // Get solution
86 (*sol_ptr)[0] = -1.717143570394391e+00;
87 (*sol_ptr)[1] = 1.595709690183565e+00;
88 (*sol_ptr)[2] = 1.827245752927178e+00;
89 (*sol_ptr)[3] = -7.636430781841294e-01;
90 (*sol_ptr)[4] = -7.636430781841294e-01;
91
92 RealT left = -1e0, right = 1e0;
93 ROL::Ptr<std::vector<RealT> > xtest_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
94 ROL::Ptr<std::vector<RealT> > g_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
95 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
96 ROL::Ptr<std::vector<RealT> > v_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
97 ROL::Ptr<std::vector<RealT> > vc_ptr = ROL::makePtr<std::vector<RealT>>(nc, 0.0);
98 ROL::Ptr<std::vector<RealT> > vl_ptr = ROL::makePtr<std::vector<RealT>>(nc, 0.0);
99 ROL::StdVector<RealT> xtest(xtest_ptr);
100 ROL::StdVector<RealT> g(g_ptr);
101 ROL::StdVector<RealT> d(d_ptr);
102 ROL::StdVector<RealT> v(v_ptr);
103 ROL::StdVector<RealT> vc(vc_ptr);
104 ROL::StdVector<RealT> vl(vl_ptr);
105
106 // set xtest, d, v
107 for (int i=0; i<dim; i++) {
108 (*xtest_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
109 (*d_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
110 (*v_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
111 }
112 // set vc, vl
113 for (int i=0; i<nc; i++) {
114 (*vc_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
115 (*vl_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
116 }
117
118 obj->checkGradient(xtest, d, true, *outStream); *outStream << "\n";
119 obj->checkHessVec(xtest, v, true, *outStream); *outStream << "\n";
120 obj->checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n";
121 constr->checkApplyJacobian(xtest, v, vc, true, *outStream); *outStream << "\n";
122 constr->checkApplyAdjointJacobian(xtest, vl, vc, xtest, true, *outStream); *outStream << "\n";
123 constr->checkApplyAdjointHessian(xtest, vl, d, xtest, true, *outStream); *outStream << "\n";
124
125 ROL::Ptr<std::vector<RealT> > v1_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
126 ROL::Ptr<std::vector<RealT> > v2_ptr = ROL::makePtr<std::vector<RealT>>(nc, 0.0);
127 ROL::StdVector<RealT> v1(v1_ptr);
128 ROL::StdVector<RealT> v2(v2_ptr);
129 RealT augtol = 1e-8;
130 constr->solveAugmentedSystem(v1, v2, d, vc, xtest, augtol);
131
132 // Define algorithm.
133 std::string paramfile = "parameters.xml";
134 auto parlist = ROL::getParametersFromXmlFile(paramfile);
135 ROL::Ptr<ROL::Step<RealT>>
136 step = ROL::makePtr<ROL::CompositeStep<RealT>>(*parlist);
137 ROL::Ptr<ROL::StatusTest<RealT>>
138 status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist);
139 ROL::Algorithm<RealT> algo(step,status,false);
140
141 // Run algorithm.
142 vl.zero();
143 algo.run(x, g, vl, vc, *obj, *constr, true, *outStream);
144
145 // Compute Error
146 *outStream << "\nReference solution x_r =\n";
147 *outStream << std::scientific << " " << (*sol_ptr)[0] << "\n";
148 *outStream << std::scientific << " " << (*sol_ptr)[1] << "\n";
149 *outStream << std::scientific << " " << (*sol_ptr)[2] << "\n";
150 *outStream << std::scientific << " " << (*sol_ptr)[3] << "\n";
151 *outStream << std::scientific << " " << (*sol_ptr)[4] << "\n";
152 *outStream << "\nOptimal solution x =\n";
153 *outStream << std::scientific << " " << (*x_ptr)[0] << "\n";
154 *outStream << std::scientific << " " << (*x_ptr)[1] << "\n";
155 *outStream << std::scientific << " " << (*x_ptr)[2] << "\n";
156 *outStream << std::scientific << " " << (*x_ptr)[3] << "\n";
157 *outStream << std::scientific << " " << (*x_ptr)[4] << "\n";
158 x.axpy(-1.0, sol);
159 RealT abserr = x.norm();
160 RealT relerr = abserr/sol.norm();
161 *outStream << std::scientific << "\n Absolute Error: " << abserr;
162 *outStream << std::scientific << "\n Relative Error: " << relerr << "\n";
163 if ( relerr > sqrt(ROL::ROL_EPSILON<RealT>()) ) {
164 errorFlag += 1;
165 }
166 }
167 catch (std::logic_error& err) {
168 *outStream << err.what() << "\n";
169 errorFlag = -1000;
170 }; // end try
171
172 if (errorFlag != 0)
173 std::cout << "End Result: TEST FAILED\n";
174 else
175 std::cout << "End Result: TEST PASSED\n";
176
177 return 0;
178
179
180}
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.
int main(int argc, char **argv)
double RealT
constexpr auto dim