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