ROL
test_13.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 
54 
55 #include "ROL_BinaryConstraint.hpp"
56 #include "ROL_DiagonalOperator.hpp"
58 #include "ROL_RandomVector.hpp"
59 #include "ROL_StdVector.hpp"
60 
61 #include "Teuchos_oblackholestream.hpp"
62 #include "Teuchos_GlobalMPISession.hpp"
63 #include "Teuchos_XMLParameterListHelpers.hpp"
64 
65 
66 
67 // Creates f(x) = <x,Dx>/2 - <x,b> where D is a diagonal matrix
68 // If D_{ii}>0 for all i, then the minimizer is the solution to
69 // the linear system x_i=b_i/d_i
70 template<class Real>
71 Teuchos::RCP<ROL::Objective<Real>>
73  const ROL::Vector<Real> &b ) {
74  using Teuchos::rcp;
75  auto op = rcp( new ROL::DiagonalOperator<Real>(a) );
76  auto vec = b.clone();
77  vec->set(b);
78  auto obj = rcp( new ROL::QuadraticObjective<Real>(op,vec) );
79  return obj;
80 }
81 
82 typedef double RealT;
83 
84 int main(int argc, char *argv[]) {
85 
86 // typedef ROL::Vector<RealT> V;
87  typedef ROL::StdVector<RealT> SV;
88 
89  using Teuchos::RCP; using Teuchos::rcp;
90 
91  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
92 
93  // This little trick lets us print to std::cout only if a
94  // (dummy) command-line argument is provided.
95  int iprint = argc - 1;
96  Teuchos::RCP<std::ostream> outStream;
97  Teuchos::oblackholestream bhs; // outputs nothing
98  if (iprint > 0)
99  outStream = Teuchos::rcp(&std::cout, false);
100  else
101  outStream = Teuchos::rcp(&bhs, false);
102 
103  int errorFlag = 0;
104 
105  try {
106 
107  auto parlist = Teuchos::getParametersFromXmlFile("binary_constraint.xml");
108 
109  // Penalty parameter
110  RealT gamma = 1.0;
111 
112  RealT INF = ROL::ROL_INF<RealT>();
113  RealT NINF = ROL::ROL_NINF<RealT>();
114 
115  /*----- Optimization Vector -----*/
116 
117  // Initial guess
118  auto x0_rcp = rcp( new std::vector<RealT>(4) );
119  auto x0 = rcp( new SV(x0_rcp) );
121 
122  auto x = x0->clone(); x->set(*x0);
123 
124  /*----- Objective Function -----*/
125 
126  // Diagonal quadratic objective scaling vector
127  auto d_rcp = rcp( new std::vector<RealT>(4) );
128  auto d = rcp( new SV(d_rcp) );
129 
130  // Quadratic objective offset vector
131  auto b_rcp = rcp( new std::vector<RealT>(4) );
132  auto b = rcp( new SV(b_rcp) );
133 
134  // Set values for objective
135  (*b_rcp)[0] = 1.0; (*b_rcp)[1] = 1.0;
136  (*b_rcp)[2] = 1.0; (*b_rcp)[3] = 1.0;
137 
138  (*d_rcp)[0] = 1.0; (*d_rcp)[1] = 2.0;
139  (*d_rcp)[2] = 4.0; (*d_rcp)[3] = 8.0;
140 
141  auto obj = createDiagonalQuadraticObjective( *d, *b );
142 
143  // Unconstrained minimizer: x = [ 1.0, 0.5, 0.25, 0.125 ]
144 
145 
146  /*----- Bound Constraint -----*/
147 
148  // Lower bound vector
149  auto xl_rcp = rcp( new std::vector<RealT>(4) );
150  auto xl = rcp( new SV(xl_rcp) );
151 
152  // Upper bound vector
153  auto xu_rcp = rcp( new std::vector<RealT>(4) );
154  auto xu = rcp( new SV(xu_rcp) );
155 
156  // Set bounds
157  (*xl_rcp)[0] = 0.0; (*xl_rcp)[1] = 0.0;
158  (*xl_rcp)[2] = NINF; (*xl_rcp)[3] = NINF;
159 
160  (*xu_rcp)[0] = 1.0; (*xu_rcp)[1] = INF;
161  (*xu_rcp)[2] = 1.0; (*xu_rcp)[3] = INF;
162 
163 // ROL::BoundConstraint<RealT> bnd(xl,xu);
164  auto bnd = rcp( new ROL::BoundConstraint<RealT>(xl,xu) );
165 
166  /*---- Equality Constraint and Lagrange Multiplier -----*/
167 
168  auto con = rcp( new ROL::BinaryConstraint<RealT>( bnd, gamma ) );
169 
170  // Lagrange multiplier
171  auto l = x->dual().clone();
172  ROL::Elementwise::Fill<RealT> FillWithOnes(1.0);
173  l->applyUnary( ROL::Elementwise::Fill<RealT>(1.0) );
174 
175  // Constrained minimizer set X = { [ 0, 0, 1, 0.125 ], [ 1, 0, 1, 0.125 ] }
176 
177  // Create Optimization problems
178  ROL::OptimizationProblem<RealT> problem_E( obj, x, Teuchos::null, con, l );
179  ROL::OptimizationProblem<RealT> problem_EB( obj, x, bnd, con, l );
180 
181  // Perform linear algebra and finite difference checks
182  problem_E.check( *outStream );
183 
184 
185  // Solve using Composite Step where the bound is not enforced and
186  // equality constraints are satisfied asymptotically
187  parlist->sublist("Step").set("Type","Composite Step");
188  ROL::OptimizationSolver<RealT> solver_cs( problem_E, *parlist );
189  solver_cs.solve( *outStream );
190  *outStream << "\n\nFinal optimization vector:";
191  x->print(*outStream);
192 
193  // Reset optimization vector and Lagrange multiplier to initial values
194  x->set(*x0); l->applyUnary(FillWithOnes);
195 
196 
197  // Solve using Augmented Lagrangian where the bound is enforced explicitly
198  // and equality constraints are enforced through penalization
199  parlist->sublist("Step").set("Type","Augmented Lagrangian");
200  ROL::OptimizationSolver<RealT> solver_al( problem_EB, *parlist );
201  solver_al.solve( *outStream );
202  *outStream << "\n\nFinal optimization vector:";
203  x->print(*outStream);
204 
205 
206  // Reset optimization vector and Lagrange multiplier to initial values
207  x->set(*x0); l->applyUnary(FillWithOnes);
208 
209 
210  // Solve using Moreau-Yosida where the bound is enforced through penalization
211  // and equality constraints are satisfied asymptotically
212  parlist->sublist("Step").set("Type","Moreau-Yosida Penalty");
213  ROL::OptimizationSolver<RealT> solver_my( problem_EB, *parlist );
214  solver_my.solve( *outStream );
215  *outStream << "\n\nFinal optimization vector:";
216  x->print(*outStream);
217 
218 
219  }
220 
221  catch (std::logic_error err) {
222  *outStream << err.what() << "\n";
223  errorFlag = -1000;
224  }; // end try
225 
226  if (errorFlag != 0)
227  std::cout << "End Result: TEST FAILED\n";
228  else
229  std::cout << "End Result: TEST PASSED\n";
230 
231  return 0;
232 }
233 
Implements an equality constraint function that evaluates to zero on the surface of a bounded paralle...
Provides the interface to evaluate quadratic objective functions.
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
int main(int argc, char *argv[])
Definition: test_13.cpp:84
Provides the interface to apply a diagonal operator which acts like elementwise multiplication when a...
Provides the std::vector implementation of the ROL::Vector interface.
Provides the interface to apply upper and lower bound constraints.
Provides a simplified interface for solving a wide range of optimization problems.
double RealT
Definition: test_13.cpp:82
Teuchos::RCP< ROL::Objective< Real > > createDiagonalQuadraticObjective(const ROL::Vector< Real > &a, const ROL::Vector< Real > &b)
Definition: test_13.cpp:72
void check(std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)