ROL
ROL_OptimizationProblemRefactor.hpp
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 #ifndef ROL_OPTIMIZATIONPROBLEMREFACTOR_HPP
45 #define ROL_OPTIMIZATIONPROBLEMREFACTOR_HPP
46 
50 #include "ROL_RandomVector.hpp"
51 
52 namespace ROL {
53 
54 /* Represents optimization problems in Type-EB form
55  */
56 
57 template<class Real>
58 class OptimizationProblem {
59 
60  typedef Vector<Real> V;
68 
69  typedef Elementwise::AbsoluteValue<Real> ABS;
70  typedef Elementwise::Fill<Real> FILL;
71 
72  typedef typename PV::size_type size_type;
73 
74 private:
75 
76  Teuchos::RCP<OBJ> ORIGINAL_obj_;
77  Teuchos::RCP<V> ORIGINAL_sol_;
78  Teuchos::RCP<BND> ORIGINAL_bnd_;
79  Teuchos::RCP<EQCON> ORIGINAL_econ_;
80  Teuchos::RCP<V> ORIGINAL_emul_;
81  Teuchos::RCP<INCON> ORIGINAL_icon_;
82  Teuchos::RCP<V> ORIGINAL_imul_;
83 
84  Teuchos::RCP<OBJ> obj_;
85  Teuchos::RCP<V> sol_;
86  Teuchos::RCP<BND> bnd_;
87  Teuchos::RCP<EQCON> con_;
88  Teuchos::RCP<V> mul_;
89 
91 
93 
94 protected:
95  void initialize(const Teuchos::RCP<Objective<Real> > &obj,
96  const Teuchos::RCP<Vector<Real> > &x,
97  const Teuchos::RCP<BoundConstraint<Real> > &bnd,
98  const Teuchos::RCP<EqualityConstraint<Real> > &eqcon,
99  const Teuchos::RCP<Vector<Real> > &le,
100  const Teuchos::RCP<InequalityConstraint<Real> > &incon,
101  const Teuchos::RCP<Vector<Real> > &li ) {
102  using Teuchos::RCP; using Teuchos::rcp;
103  if (!isInitialized_) {
104  // If we have an inequality constraint
105  if( incon != Teuchos::null ) {
106 
107  Real tol = std::sqrt(ROL_EPSILON<Real>());
108 
109  // Create slack variables s = |c_i(x)|
110  RCP<V> s = li->dual().clone();
111  incon->value(*s,*x,tol);
112  s->applyUnary(ABS());
113 
114  sol_ = CreatePartitionedVector(x,s);
115 
116  RCP<BND> xbnd, sbnd;
117 
118  RCP<V> sl = s->clone();
119  RCP<V> su = s->clone();
120 
121  sl->applyUnary( FILL(0.0) );
122  su->applyUnary( FILL(ROL_INF<Real>()) );
123 
124  sbnd = rcp( new BND(sl,su) );
125 
126  // Create a do-nothing bound constraint for x if we don't have one
127  if( bnd == Teuchos::null ) {
128  xbnd = rcp( new BND(*x) );
129  }
130  else { // Otherwise use the given bound constraint on x
131  xbnd = bnd;
132  }
133 
134  // Create a partitioned bound constraint on the optimization and slack variables
135  bnd_ = CreateBoundConstraint_Partitioned(xbnd,sbnd);
136 
137  // Create partitioned lagrange multiplier and composite constraint
138  if( eqcon == Teuchos::null ) {
139  mul_ = CreatePartitionedVector(li);
140  con_ = rcp( new CCON(incon,x) );
141  }
142  else {
143  mul_ = CreatePartitionedVector(li,le);
144  con_ = rcp( new CCON(incon,eqcon,x) );
145  }
146 
147  obj_ = rcp( new SLOBJ(obj) );
148  }
149  else { // There is no inequality constraint
150 
151  obj_ = obj;
152  sol_ = x;
153  mul_ = le;
154  bnd_ = bnd;
155  con_ = eqcon;
156  }
157 
158  if( con_ == Teuchos::null ) { // Type-U or Type-B
159  if( bnd_ == Teuchos::null || !bnd_->isActivated() ) { // Type-U
160  problemType_ = TYPE_U;
161  }
162  else { // Type-B
163  problemType_ = TYPE_B;
164  }
165  }
166  else { // Type-E or Type-EB
167  if( bnd_ == Teuchos::null || !bnd_->isActivated() ) { // Type-E
168  problemType_ = TYPE_E;
169  }
170  else { // Type-EB
171  problemType_ = TYPE_EB;
172  }
173  }
174  isInitialized_ = true;
175  }
176  }
177 
178 public:
179  virtual ~OptimizationProblem(void) {}
180 
181  // Complete option constructor [1]
182  OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
183  const Teuchos::RCP<Vector<Real> > &x,
184  const Teuchos::RCP<BoundConstraint<Real> > &bnd,
185  const Teuchos::RCP<EqualityConstraint<Real> > &eqcon,
186  const Teuchos::RCP<Vector<Real> > &le,
187  const Teuchos::RCP<InequalityConstraint<Real> > &incon,
188  const Teuchos::RCP<Vector<Real> > &li )
189  : ORIGINAL_obj_(obj), ORIGINAL_sol_(x), ORIGINAL_bnd_(bnd),
190  ORIGINAL_econ_(eqcon), ORIGINAL_emul_(le),
191  ORIGINAL_icon_(incon), ORIGINAL_imul_(li),
192  isInitialized_(false) {
193  initialize(obj,x,bnd,eqcon,le,incon,li);
194  }
195 
196  // No inequality constructor [2]
197  OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
198  const Teuchos::RCP<Vector<Real> > &x,
199  const Teuchos::RCP<BoundConstraint<Real> > &bnd,
200  const Teuchos::RCP<EqualityConstraint<Real> > &eqcon,
201  const Teuchos::RCP<Vector<Real> > &le ) :
202  OptimizationProblem( obj, x, bnd, eqcon, le, Teuchos::null, Teuchos::null ) { }
203 
204  // No equality constructor [3]
205 // OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
206 // const Teuchos::RCP<Vector<Real> > &x,
207 // const Teuchos::RCP<BoundConstraint<Real> > &bnd,
208 // const Teuchos::RCP<InequalityConstraint<Real> > &incon,
209 // const Teuchos::RCP<Vector<Real> > &li ):
210 // OptimizationProblem( obj, x, bnd, Teuchos::null, Teuchos::null, incon, li ) { }
211 
212  // No bound constuctor [4]
213  OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
214  const Teuchos::RCP<Vector<Real> > &x,
215  const Teuchos::RCP<EqualityConstraint<Real> > &eqcon,
216  const Teuchos::RCP<Vector<Real> > &le,
217  const Teuchos::RCP<InequalityConstraint<Real> > &incon,
218  const Teuchos::RCP<Vector<Real> > &li ) :
219  OptimizationProblem( obj, x, Teuchos::null, eqcon, le, incon, li ) {}
220 
221  // No inequality or equality [5]
222  OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
223  const Teuchos::RCP<Vector<Real> > &x,
224  const Teuchos::RCP<BoundConstraint<Real> > &bnd ) :
225  OptimizationProblem( obj, x, bnd, Teuchos::null, Teuchos::null, Teuchos::null, Teuchos::null ) { }
226  // No inequality or bound [6]
227  OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
228  const Teuchos::RCP<Vector<Real> > &x,
229  const Teuchos::RCP<EqualityConstraint<Real> > &eqcon,
230  const Teuchos::RCP<Vector<Real> > &le ) :
231  OptimizationProblem( obj, x, Teuchos::null, eqcon, le, Teuchos::null, Teuchos::null ) { }
232 
233  // No equality or bound [7]
234 // OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
235 // const Teuchos::RCP<Vector<Real> > &x,
236 // const Teuchos::RCP<InequalityConstraint<Real> > &incon,
237 // const Teuchos::RCP<Vector<Real> > &li ) :
238 // OptimizationProblem( obj, x, Teuchos::null, Teuchos::null, Teuchos::null, incon, li ) { }
239 
240  // Unconstrained problem [8]
241  OptimizationProblem( const Teuchos::RCP<Objective<Real> > &obj,
242  const Teuchos::RCP<Vector<Real> > &x ) :
243  OptimizationProblem( obj, x, Teuchos::null, Teuchos::null, Teuchos::null,
244  Teuchos::null, Teuchos::null ) { }
245 
246  /* Set methods */
247 
248  virtual void setObjective(const Teuchos::RCP<Objective<Real> > &obj) {
249  isInitialized_ = false;
250  ORIGINAL_obj_ = obj;
251  }
252 
253  virtual void setSolutionVector(const Teuchos::RCP<Vector<Real> > &sol) {
254  isInitialized_ = false;
255  ORIGINAL_sol_ = sol;
256  }
257 
258  virtual void setBoundConstraint(const Teuchos::RCP<BoundConstraint<Real> > &bnd) {
259  isInitialized_ = false;
260  ORIGINAL_bnd_ = bnd;
261  }
262 
263  virtual void setEqualityConstraint(const Teuchos::RCP<EqualityConstraint<Real> > &con) {
264  isInitialized_ = false;
265  ORIGINAL_econ_ = con;
266  }
267 
268  virtual void setEMultiplierVector(const Teuchos::RCP<Vector<Real> > &mul) {
269  isInitialized_ = false;
270  ORIGINAL_emul_ = mul;
271  }
272 
273  virtual void setInequalityConstraint(const Teuchos::RCP<InequalityConstraint<Real> > &con) {
274  isInitialized_ = false;
275  ORIGINAL_icon_ = con;
276  }
277 
278  virtual void setIMultiplierVector(const Teuchos::RCP<Vector<Real> > &mul) {
279  isInitialized_ = false;
280  ORIGINAL_imul_ = mul;
281  }
282 
283  /* Get methods */
284 
285  Teuchos::RCP<Objective<Real> > getObjective(void) {
286  initialize(ORIGINAL_obj_,ORIGINAL_sol_,ORIGINAL_bnd_,
287  ORIGINAL_econ_,ORIGINAL_emul_,
288  ORIGINAL_icon_,ORIGINAL_imul_);
289  return obj_;
290  }
291 
292  Teuchos::RCP<Vector<Real> > getSolutionVector(void) {
293  initialize(ORIGINAL_obj_,ORIGINAL_sol_,ORIGINAL_bnd_,
294  ORIGINAL_econ_,ORIGINAL_emul_,
295  ORIGINAL_icon_,ORIGINAL_imul_);
296  return sol_;
297  }
298 
299  Teuchos::RCP<BoundConstraint<Real> > getBoundConstraint(void) {
300  initialize(ORIGINAL_obj_,ORIGINAL_sol_,ORIGINAL_bnd_,
301  ORIGINAL_econ_,ORIGINAL_emul_,
302  ORIGINAL_icon_,ORIGINAL_imul_);
303  return bnd_;
304  }
305 
306  Teuchos::RCP<EqualityConstraint<Real> > getEqualityConstraint(void) {
307  initialize(ORIGINAL_obj_,ORIGINAL_sol_,ORIGINAL_bnd_,
308  ORIGINAL_econ_,ORIGINAL_emul_,
309  ORIGINAL_icon_,ORIGINAL_imul_);
310  return con_;
311  }
312 
313  Teuchos::RCP<Vector<Real> > getMultiplierVector(void) {
314  initialize(ORIGINAL_obj_,ORIGINAL_sol_,ORIGINAL_bnd_,
315  ORIGINAL_econ_,ORIGINAL_emul_,
316  ORIGINAL_icon_,ORIGINAL_imul_);
317  return mul_;
318  }
319 
321  return problemType_;
322  }
323 
324  // Check derivatives, and consistency
325  void check( std::ostream &outStream = std::cout, const int numSteps = ROL_NUM_CHECKDERIV_STEPS, const int order = 1 ) {
326 
327  Teuchos::RCP<V> x = sol_->clone();
328  Teuchos::RCP<V> y = sol_->clone();
329  Teuchos::RCP<V> u = sol_->clone();
330  Teuchos::RCP<V> v = sol_->clone();
331 
332  RandomizeVector(*x);
333  RandomizeVector(*y);
334  RandomizeVector(*u);
335  RandomizeVector(*v);
336 
337  outStream << "\nPerforming OptimizationProblem diagnostics.\n\n";
338 
339  outStream << "Checking vector operations in optimization vector space X." << std::endl;
340  x->checkVector(*y,*u,true,outStream);
341 
342  outStream << "Checking objective function." << std::endl;
343 
344  obj_->checkGradient(*x,*v,true,outStream,numSteps,order); outStream << std::endl;
345  obj_->checkHessVec(*x,*u,true,outStream,numSteps,order); outStream << std::endl;
346  obj_->checkHessSym(*x,*u,*v,true,outStream); outStream << std::endl;
347 
348  if(con_ != Teuchos::null) {
349  Teuchos::RCP<V> c = mul_->dual().clone();
350  Teuchos::RCP<V> l = mul_->clone();
351  Teuchos::RCP<V> w = mul_->clone();
352  Teuchos::RCP<V> q = mul_->clone();
353 
354  RandomizeVector(*c);
355  RandomizeVector(*l);
356  RandomizeVector(*w);
357  RandomizeVector(*q);
358 
359  outStream << "Checking vector operations in constraint multiplier space C*." << std::endl;
360  l->checkVector(*q,*w,true,outStream);
361 
362  outStream << "Checking equality constraint." << std::endl;
363  con_->checkApplyJacobian(*x,*v,*c,true,outStream,numSteps,order); outStream << std::endl;
364  con_->checkAdjointConsistencyJacobian(*l,*u,*x,true,outStream); outStream << std::endl;
365  con_->checkApplyAdjointHessian(*x,*l,*v,*u,true,outStream,numSteps,order); outStream << std::endl;
366  }
367 
368  }
369 
370 
371 
372 }; // class OptimizationProblem
373 
374 } // namespace ROL
375 
376 #endif // ROL_OPTIMIZATIONPROBLEMREFACTOR_HPP
Provides the interface to evaluate objective functions.
Teuchos::RCP< BoundConstraint< Real > > CreateBoundConstraint_Partitioned(const Teuchos::RCP< BoundConstraint< Real > > &bnd1, const Teuchos::RCP< BoundConstraint< Real > > &bnd2)
Defines the linear algebra of vector space on a generic partitioned vector.
Teuchos::RCP< Objective< Real > > obj_
virtual void setIMultiplierVector(const Teuchos::RCP< Vector< Real > > &mul)
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 void setSolutionVector(const Teuchos::RCP< Vector< Real > > &sol)
InequalityConstraint< Real > INCON
Teuchos::RCP< Vector< Real > > CreatePartitionedVector(const Teuchos::RCP< Vector< Real > > &a)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
Teuchos::RCP< BoundConstraint< Real > > getBoundConstraint(void)
OptimizationProblem(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x)
Defines the equality constraint operator interface.
virtual void setEMultiplierVector(const Teuchos::RCP< Vector< Real > > &mul)
virtual void setInequalityConstraint(const Teuchos::RCP< InequalityConstraint< Real > > &con)
Teuchos::RCP< Vector< Real > > mul_
OptimizationProblem(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x, const Teuchos::RCP< EqualityConstraint< Real > > &eqcon, const Teuchos::RCP< Vector< Real > > &le, const Teuchos::RCP< InequalityConstraint< Real > > &incon, const Teuchos::RCP< Vector< Real > > &li)
Has both inequality and equality constraints. Treat inequality constraint as equality with slack vari...
OptimizationProblem(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x, const Teuchos::RCP< EqualityConstraint< Real > > &eqcon, const Teuchos::RCP< Vector< Real > > &le)
Elementwise::AbsoluteValue< Real > ABS
void initialize(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x, const Teuchos::RCP< BoundConstraint< Real > > &bnd, const Teuchos::RCP< EqualityConstraint< Real > > &eqcon, const Teuchos::RCP< Vector< Real > > &le, const Teuchos::RCP< InequalityConstraint< Real > > &incon, const Teuchos::RCP< Vector< Real > > &li)
virtual void setEqualityConstraint(const Teuchos::RCP< EqualityConstraint< Real > > &con)
Teuchos::RCP< Vector< Real > > getMultiplierVector(void)
Provides the interface to apply upper and lower bound constraints.
virtual void setObjective(const Teuchos::RCP< Objective< Real > > &obj)
This class strips out the slack variables from objective evaluations to create the new objective ...
#define ROL_NUM_CHECKDERIV_STEPS
Number of steps for derivative checks.
Definition: ROL_Types.hpp:73
Teuchos::RCP< EqualityConstraint< Real > > con_
OptimizationProblem(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x, const Teuchos::RCP< BoundConstraint< Real > > &bnd, const Teuchos::RCP< EqualityConstraint< Real > > &eqcon, const Teuchos::RCP< Vector< Real > > &le, const Teuchos::RCP< InequalityConstraint< Real > > &incon, const Teuchos::RCP< Vector< Real > > &li)
Teuchos::RCP< BoundConstraint< Real > > bnd_
OptimizationProblem(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x, const Teuchos::RCP< BoundConstraint< Real > > &bnd)
std::vector< PV >::size_type size_type
Teuchos::RCP< EqualityConstraint< Real > > getEqualityConstraint(void)
Teuchos::RCP< Vector< Real > > getSolutionVector(void)
Teuchos::RCP< Vector< Real > > sol_
Provides a unique argument for inequality constraints, which otherwise behave exactly as equality con...
virtual void setBoundConstraint(const Teuchos::RCP< BoundConstraint< Real > > &bnd)
void check(std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
OptimizationProblem(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x, const Teuchos::RCP< BoundConstraint< Real > > &bnd, const Teuchos::RCP< EqualityConstraint< Real > > &eqcon, const Teuchos::RCP< Vector< Real > > &le)
EProblem
Definition: ROL_Types.hpp:182
Teuchos::RCP< Objective< Real > > getObjective(void)