Debian Codegraph @

packages/rol/adapters/teuchos/test/function/test_01.cpp

raw
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
10 /*! \file test_01.cpp
11 \brief Test for ROL::TeuchosObjective and
12 ROL::TeuchosConstraint
13
14 Solves the optimization problem
15
16 \f[ \min_x f(x) = \frac{1}{2} x^\top A x-x^\top b \f]
17
18 For simplicity, we take A to be positive definte
19
20 Subject to the equality constraint
21
22 \f[ c(x) = Cx-d = 0 \f]
23 */
24
25 #include "ROL_Teuchos_Objective.hpp"
26 #include "ROL_Teuchos_Constraint.hpp"
27 #include "ROL_OptimizationSolver.hpp"
28
29 #include "ROL_Stream.hpp"
30 #include "Teuchos_GlobalMPISession.hpp"
31 #include "Teuchos_SerialDenseSolver.hpp"
32
33 #include <random>
34 #include <iostream>
35
36 template<class Ordinal, class Real>
37 class QuadraticTestObjective : public ROL::TeuchosObjective<Ordinal,Real> {
38
39 template<class T> using RCP = Teuchos::RCP<T>;
40 using Vector = Teuchos::SerialDenseVector<Ordinal,Real>;
41 using Matrix = Teuchos::SerialDenseMatrix<Ordinal,Real>;
42 using Solver = Teuchos::SerialDenseSolver<Ordinal,Real>;
43
44 private:
45
46 const RCP<const Matrix> A_;
47 const RCP<const Vector> b_;
48 Vector Ax_;
49 RCP<Vector> scratch_;
50 Real bx_;
51 Solver solver_;
52
53 static constexpr Real zero{0.0};
54 static constexpr Real one{1.0};
55 static constexpr Real half{0.5};
56
57 void applyA( Vector& Av, const Vector& v ) {
58 Av.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS, one, *A_, v, zero);
59 }
60
61 public:
62
63 QuadraticTestObjective( const RCP<const Matrix>& A,
64 const RCP<const Vector>& b ) :
65 A_{A}, b_{b}, Ax_{*b}, scratch_{Teuchos::rcp( new Vector{*b} )} {
66 RCP<Matrix> Af = Teuchos::rcp( new Matrix{*A} );
67 solver_.setMatrix(Af);
68 }
69
70 void update( const Vector& x, bool flag = true, int iter=-1 ) {
71 applyA(Ax_,x);
72 bx_ = b_->dot(x);
73 }
74
75 Real value( const Vector& x, Real& tol ) {
76 return half*x.dot(Ax_)-bx_;
77 }
78
79 void gradient( Vector& g, const Vector& x, Real& tol ) {
80 g.assign(Ax_);
81 g -= *b_;
82 }
83
84 void hessVec( Vector& hv, const Vector& v, const Vector& x, Real& tol ) {
85 applyA(hv,v);
86 }
87
88 void invHessVec( Vector& hv, const Vector& v, const Vector& x, Real& tol ) {
89 scratch_->assign(v);
90 auto hvp = Teuchos::rcpFromRef(hv);
91 solver_.setVectors(hvp,scratch_);
92 solver_.solve();
93 }
94 };
95
96
97 template<class Ordinal, class Real>
98 class LinearTestConstraint : public ROL::TeuchosConstraint<Ordinal,Real> {
99
100 template<class T> using RCP = Teuchos::RCP<T>;
101 using Vector = Teuchos::SerialDenseVector<Ordinal,Real>;
102 using Matrix = Teuchos::SerialDenseMatrix<Ordinal,Real>;
103
104 private:
105
106 const RCP<const Matrix> C_;
107 const RCP<const Vector> d_;
108 Vector c_;
109
110 static constexpr Real one{1.0};
111 static constexpr Real zero{0.0};
112
113 void applyC( Vector& Cv, const Vector& v ) {
114 Cv.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, one, *C_, v, zero);
115 }
116
117 public:
118
119 LinearTestConstraint( const RCP<const Matrix>& C,
120 const RCP<const Vector>& d ) :
121 C_{C}, d_{d}, c_{ C->numRows() } {}
122
123 void update( const Vector& x, bool flag = true, int iter = -1 ) {
124 applyC(c_,x); c_ -= *d_;
125 }
126
127 void value( Vector& c, const Vector& x, Real& tol ) {
128 c.assign(c_);
129 }
130
131 void applyJacobian( Vector& jv, const Vector& v, const Vector& x, Real& tol ) {
132 applyC(jv,v);
133 }
134
135 void applyAdjointJacobian( Vector& ajv, const Vector& v, const Vector& x, Real& tol ) {
136 ajv.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, one, *C_, v, zero);
137 }
138
139 void applyAdjointHessian( Vector& ahuv, const Vector& u,
140 const Vector& v, const Vector& x, Real& tol ) {
141 ahuv.putScalar(Real{0});
142 }
143 };
144
145
146
147
148 using OrdinalT = int;
149 using RealT = double;
150
151 int main( int argc, char *argv[] ) {
152
153 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
154
155 using Teuchos::RCP;
156 using Teuchos::rcp;
157 using Vector = Teuchos::SerialDenseVector<OrdinalT,RealT>;
158 using Matrix = Teuchos::SerialDenseMatrix<OrdinalT,RealT>;
159
160 // This little trick lets us print to std::cout only if a
161 // (dummy) command-line argument is provided.
162 int iprint = argc - 1;
163 ROL::Ptr<std::ostream> outStream;
164 ROL::nullstream bhs; // outputs nothing
165 if (iprint > 0)
166 outStream = ROL::makePtrFromRef(std::cout);
167 else
168 outStream = ROL::makePtrFromRef(bhs);
169
170 int errorFlag = 0;
171
172 // RealT errtol = ROL::ROL_THRESHOLD<RealT>();
173
174 std::default_random_engine gen;
175 std::normal_distribution<RealT> dist(0.0,1.0);
176
177 // *** Test body.
178 try {
179
180 // Dimension of optimization space
181 OrdinalT Nopt = 10;
182
183 // Dimension of constraint space
184 OrdinalT Ncon = 3;
185
186 auto A = rcp( new Matrix{Nopt,Nopt} );
187 auto b = rcp( new Vector{Nopt} );
188 auto C = rcp( new Matrix{Ncon,Nopt} );
189 auto d = rcp( new Vector{Ncon} );
190
191 // Create a symmetric random positive definte matrix A
192 // random rectangular matrix C, and rectangular vectors b and d
193
194 for( OrdinalT i=0; i<Nopt; ++i ) {
195 RealT sum{0};
196 for( OrdinalT j=i+1; j<Nopt; ++j ) {
197 (*A)(i,j) = dist(gen);
198 (*A)(j,i) = (*A)(i,j);
199 sum += std::abs((*A)(i,j));
200 }
201 (*A)(i,i) = 5*sum;
202 (*b)(i) = dist(gen);
203 for( OrdinalT k=0; k<Ncon; ++k ) {
204 (*C)(k,i) = dist(gen);
205 if(i==0) {
206 (*d)(k) = dist(gen);
207 }
208 }
209 }
210
211 *outStream << "\nA = " << printMat(*A);
212 *outStream << "\nb = " << printMat(*b);
213 *outStream << "\nC = " << printMat(*C);
214 *outStream << "\nd = " << printMat(*d);
215
216
217 auto x = rcp( new Vector{Nopt,1} );
218 auto l = rcp( new Vector{Ncon,1} );
219
220 // Solution vector
221 auto sol = rcp( new ROL::TeuchosVector<OrdinalT,RealT>{x} );
222
223 // Equality multiplier
224 auto mul = rcp( new ROL::TeuchosVector<OrdinalT,RealT>{l} );
225
226 // Objective
227 auto obj = rcp( new QuadraticTestObjective<OrdinalT,RealT>{A,b} );
228
229 // Constraint
230 auto con = rcp( new LinearTestConstraint<OrdinalT,RealT>{C,d} );
231
232 ROL::OptimizationProblem<RealT> problem(obj,sol,con,mul);
233
234 problem.check(*outStream);
235
236 Teuchos::ParameterList emptyList;
237
238 ROL::OptimizationSolver<RealT> solver(problem,emptyList);
239 solver.solve(*outStream);
240
241
242 }
243 catch (std::logic_error& err) {
244 *outStream << err.what() << "\n";
245 errorFlag = -1000;
246 }; // end try
247
248 if (errorFlag != 0)
249 std::cout << "End Result: TEST FAILED\n";
250 else
251 std::cout << "End Result: TEST PASSED\n";
252
253
254 return 0;
255 }