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_BOUND_CONSTRAINT_SIMOPT_H
45 #define ROL_BOUND_CONSTRAINT_SIMOPT_H
46 
47 #include "ROL_BoundConstraint.hpp"
48 #include "ROL_Vector_SimOpt.hpp"
49 #include "ROL_Types.hpp"
50 #include <iostream>
51 
52 /** @ingroup func_group
53     \class ROL::BoundConstraint
54     \brief Provides the interface to apply upper and lower bound constraints.
55 
56     ROL's bound constraint class is to designed to handle point wise bound
57     constraints on optimization variables.  That is, let \f$\mathcal{X}\f$
58     be a Banach space of functions from \f$\Xi\f$ into \f$\mathbb{R}\f$
59     (for example, \f$\Xi\subset\mathbb{R}^d\f$ for some positive integer \f$d\f$
60     and \f$\mathcal{X}=L^2(\Xi)\f$ or \f$\Xi = \{1,\ldots,n\}\f$ and
61     \f$\mathcal{X}=\mathbb{R}^n\f$).  For any \f$x\in\mathcal{X}\f$, we consider
62     bounds of the form
63     \f[
64         a(\xi) \le x(\xi) \le b(\xi) \quad \text{for almost every }\xi\in\Xi.
65     \f]
66     Here, \f$a(\xi)\le b(\xi)\f$ for almost every \f$\xi\in\Xi\f$ and \f$a,b\in \mathcal{X}\f$.
67 */
68 
69 
70 namespace ROL {
71 
72 template <class Real>
73 class BoundConstraint_SimOpt : public BoundConstraint<Real> {
74 private:
75   ROL::Ptr<BoundConstraint<Real> > bnd1_;
76   ROL::Ptr<BoundConstraint<Real> > bnd2_;
77 
78 public:
~BoundConstraint_SimOpt()79   ~BoundConstraint_SimOpt() {}
80 
81   /** \brief Default constructor.
82 
83       The default constructor automatically turns the constraints on.
84   */
BoundConstraint_SimOpt(const ROL::Ptr<BoundConstraint<Real>> & bnd1,const ROL::Ptr<BoundConstraint<Real>> & bnd2)85   BoundConstraint_SimOpt(const ROL::Ptr<BoundConstraint<Real> > &bnd1,
86                          const ROL::Ptr<BoundConstraint<Real> > &bnd2)
87     : bnd1_(bnd1), bnd2_(bnd2) {
88     if ( bnd1_->isActivated() || bnd2_->isActivated() ) {
89       BoundConstraint<Real>::activate();
90     }
91     else {
92       BoundConstraint<Real>::deactivate();
93     }
94   }
95 
96   /** \brief Update bounds.
97 
98       The update function allows the user to update the bounds at each new iterations.
99           @param[in]      x      is the optimization variable.
100           @param[in]      flag   is set to true if control is changed.
101           @param[in]      iter   is the outer algorithm iterations count.
102   */
update(const Vector<Real> & x,bool flag=true,int iter=-1)103   void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
104     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
105       dynamic_cast<const ROL::Vector<Real>&>(x));
106     if ( bnd1_->isActivated() ) {
107       bnd1_->update(*(xs.get_1()),flag,iter);
108     }
109     if ( bnd2_->isActivated() ) {
110       bnd2_->update(*(xs.get_2()),flag,iter);
111     }
112   }
113 
114   /** \brief Project optimization variables onto the bounds.
115 
116       This function implements the projection of \f$x\f$ onto the bounds, i.e.,
117       \f[
118          (P_{[a,b]}(x))(\xi) = \min\{b(\xi),\max\{a(\xi),x(\xi)\}\} \quad \text{for almost every }\xi\in\Xi.
119       \f]
120        @param[in,out]      x is the optimization variable.
121   */
project(Vector<Real> & x)122   void project( Vector<Real> &x ) {
123     ROL::Vector_SimOpt<Real> &xs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(
124       dynamic_cast<ROL::Vector<Real>&>(x));
125     if ( bnd1_->isActivated() ) {
126       ROL::Ptr<Vector<Real> > x1 = xs.get_1()->clone(); x1->set(*(xs.get_1()));
127       bnd1_->project(*x1);
128       xs.set_1(*x1);
129     }
130     if ( bnd2_->isActivated() ) {
131       ROL::Ptr<Vector<Real> > x2 = xs.get_2()->clone(); x2->set(*(xs.get_2()));
132       bnd2_->project(*x2);
133       xs.set_2(*x2);
134     }
135   }
136 
137   /** \brief Project optimization variables into the interior of the feasible set.
138 
139       This function implements the projection of \f$x\f$ into the interior of the
140       feasible set, i.e.,
141       \f[
142          (P_{[a,b]}(x))(\xi) \in (a(\xi),b(\xi))
143              \quad \text{for almost every }\xi\in\Xi.
144       \f]
145        @param[in,out]      x is the optimization variable.
146   */
projectInterior(Vector<Real> & x)147   void projectInterior( Vector<Real> &x ) {
148     ROL::Vector_SimOpt<Real> &xs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(
149       dynamic_cast<ROL::Vector<Real>&>(x));
150     if ( bnd1_->isActivated() ) {
151       ROL::Ptr<Vector<Real> > x1 = xs.get_1()->clone(); x1->set(*(xs.get_1()));
152       bnd1_->projectInterior(*x1);
153       xs.set_1(*x1);
154     }
155     if ( bnd2_->isActivated() ) {
156       ROL::Ptr<Vector<Real> > x2 = xs.get_2()->clone(); x2->set(*(xs.get_2()));
157       bnd2_->projectInterior(*x2);
158       xs.set_2(*x2);
159     }
160   }
161 
162   /** \brief Determine if a vector of Lagrange multipliers is nonnegative components.
163 
164       This function returns true if components of \f$l\f$ corresponding to the components of \f$x\f$
165       that are active at the upper bound are nonpositive or the components of \f$l\f$ corresponding
166       to the components of \f$x\f$ that are active at the lower bound are nonnegative.
167   */
checkMultipliers(const Vector<Real> & l,const Vector<Real> & x)168   bool checkMultipliers( const Vector<Real> &l, const Vector<Real> &x ) {
169     const ROL::Vector_SimOpt<Real> &ls = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
170       dynamic_cast<const ROL::Vector<Real>&>(l));
171     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
172       dynamic_cast<const ROL::Vector<Real>&>(x));
173     bool nn1 = true;
174     if ( bnd1_->isActivated() ) {
175       nn1 = bnd1_->checkMultipliers(*(ls.get_1()),*(xs.get_1()));
176     }
177     bool nn2 = true;
178     if ( bnd2_->isActivated() ) {
179       nn2 = bnd2_->checkMultipliers(*(ls.get_2()),*(xs.get_2()));
180     }
181     return (nn1 && nn2);
182   }
183 
184   /** \brief Set variables to zero if they correspond to the upper \f$\epsilon\f$-active set.
185 
186       This function sets \f$v(\xi)=0\f$ if \f$\xi\in\mathcal{A}^+_\epsilon(x)\f$.  Here,
187       the upper \f$\epsilon\f$-active set is defined as
188       \f[
189          \mathcal{A}^+_\epsilon(x) = \{\,\xi\in\Xi\,:\,x(\xi) = b(\xi)-\epsilon\,\}.
190       \f]
191       @param[out]      v   is the variable to be pruned.
192       @param[in]       x   is the current optimization variable.
193       @param[in]       eps is the active-set tolerance \f$\epsilon\f$.
194   */
pruneUpperActive(Vector<Real> & v,const Vector<Real> & x,Real eps=0.0)195   void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
196     ROL::Vector_SimOpt<Real> &vs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(
197       dynamic_cast<ROL::Vector<Real>&>(v));
198     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
199       dynamic_cast<const ROL::Vector<Real>&>(x));
200     if ( bnd1_->isActivated() ) {
201       ROL::Ptr<Vector<Real> > v1 = vs.get_1()->clone(); v1->set(*(vs.get_1()));
202       bnd1_->pruneUpperActive(*v1,*(xs.get_1()),eps);
203       vs.set_1(*v1);
204     }
205     if ( bnd2_->isActivated() ) {
206       ROL::Ptr<Vector<Real> > v2 = vs.get_2()->clone(); v2->set(*(vs.get_2()));
207       bnd2_->pruneUpperActive(*v2,*(xs.get_2()),eps);
208       vs.set_2(*v2);
209     }
210   }
211 
212   /** \brief Set variables to zero if they correspond to the upper \f$\epsilon\f$-binding set.
213 
214       This function sets \f$v(\xi)=0\f$ if \f$\xi\in\mathcal{B}^+_\epsilon(x)\f$.  Here,
215       the upper \f$\epsilon\f$-binding set is defined as
216       \f[
217          \mathcal{B}^+_\epsilon(x) = \{\,\xi\in\Xi\,:\,x(\xi) = b(\xi)-\epsilon,\;
218                 g(\xi) < 0 \,\}.
219       \f]
220       @param[out]      v   is the variable to be pruned.
221       @param[in]       x   is the current optimization variable.
222       @param[in]       g   is the negative search direction.
223       @param[in]       eps is the active-set tolerance \f$\epsilon\f$.
224   */
pruneUpperActive(Vector<Real> & v,const Vector<Real> & g,const Vector<Real> & x,Real eps=0.0)225   void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
226     ROL::Vector_SimOpt<Real> &vs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(
227       dynamic_cast<ROL::Vector<Real>&>(v));
228     const ROL::Vector_SimOpt<Real> &gs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
229       dynamic_cast<const ROL::Vector<Real>&>(g));
230     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
231       dynamic_cast<const ROL::Vector<Real>&>(x));
232     if ( bnd1_->isActivated() ) {
233       ROL::Ptr<Vector<Real> > v1 = vs.get_1()->clone(); v1->set(*(vs.get_1()));
234       bnd1_->pruneUpperActive(*v1,*(gs.get_1()),*(xs.get_1()),eps);
235       vs.set_1(*v1);
236     }
237     if ( bnd2_->isActivated() ) {
238       ROL::Ptr<Vector<Real> > v2 = vs.get_2()->clone(); v2->set(*(vs.get_2()));
239       bnd2_->pruneUpperActive(*v2,*(gs.get_2()),*(xs.get_2()),eps);
240       vs.set_2(*v2);
241     }
242   }
243 
244   /** \brief Set variables to zero if they correspond to the lower \f$\epsilon\f$-active set.
245 
246       This function sets \f$v(\xi)=0\f$ if \f$\xi\in\mathcal{A}^-_\epsilon(x)\f$.  Here,
247       the lower \f$\epsilon\f$-active set is defined as
248       \f[
249          \mathcal{A}^-_\epsilon(x) = \{\,\xi\in\Xi\,:\,x(\xi) = a(\xi)+\epsilon\,\}.
250       \f]
251       @param[out]      v   is the variable to be pruned.
252       @param[in]       x   is the current optimization variable.
253       @param[in]       eps is the active-set tolerance \f$\epsilon\f$.
254   */
pruneLowerActive(Vector<Real> & v,const Vector<Real> & x,Real eps=0.0)255   void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
256     ROL::Vector_SimOpt<Real> &vs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(
257       dynamic_cast<ROL::Vector<Real>&>(v));
258     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
259       dynamic_cast<const ROL::Vector<Real>&>(x));
260     if ( bnd1_->isActivated() ) {
261       ROL::Ptr<Vector<Real> > v1 = vs.get_1()->clone(); v1->set(*(vs.get_1()));
262       bnd1_->pruneLowerActive(*v1,*(xs.get_1()),eps);
263       vs.set_1(*v1);
264     }
265     if ( bnd2_->isActivated() ) {
266       ROL::Ptr<Vector<Real> > v2 = vs.get_2()->clone(); v2->set(*(vs.get_2()));
267       bnd2_->pruneLowerActive(*v2,*(xs.get_2()),eps);
268       vs.set_2(*v2);
269     }
270   }
271 
272   /** \brief Set variables to zero if they correspond to the lower \f$\epsilon\f$-binding set.
273 
274       This function sets \f$v(\xi)=0\f$ if \f$\xi\in\mathcal{B}^-_\epsilon(x)\f$.  Here,
275       the lower \f$\epsilon\f$-binding set is defined as
276       \f[
277          \mathcal{B}^-_\epsilon(x) = \{\,\xi\in\Xi\,:\,x(\xi) = a(\xi)+\epsilon,\;
278                 g(\xi) > 0 \,\}.
279       \f]
280       @param[out]      v   is the variable to be pruned.
281       @param[in]       x   is the current optimization variable.
282       @param[in]       g   is the negative search direction.
283       @param[in]       eps is the active-set tolerance \f$\epsilon\f$.
284   */
pruneLowerActive(Vector<Real> & v,const Vector<Real> & g,const Vector<Real> & x,Real eps=0.0)285   void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
286     ROL::Vector_SimOpt<Real> &vs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(
287       dynamic_cast<ROL::Vector<Real>&>(v));
288     const ROL::Vector_SimOpt<Real> &gs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
289       dynamic_cast<const ROL::Vector<Real>&>(g));
290     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
291       dynamic_cast<const ROL::Vector<Real>&>(x));
292     if ( bnd1_->isActivated() ) {
293       ROL::Ptr<Vector<Real> > v1 = vs.get_1()->clone(); v1->set(*(vs.get_1()));
294       bnd1_->pruneLowerActive(*v1,*(gs.get_1()),*(xs.get_1()),eps);
295       vs.set_1(*v1);
296     }
297     if ( bnd2_->isActivated() ) {
298       ROL::Ptr<Vector<Real> > v2 = vs.get_2()->clone(); v2->set(*(vs.get_2()));
299       bnd2_->pruneLowerActive(*v2,*(gs.get_2()),*(xs.get_2()),eps);
300       vs.set_2(*v2);
301     }
302   }
303 
getLowerBound(void) const304   const ROL::Ptr<const Vector<Real>> getLowerBound( void ) const {
305     const ROL::Ptr<const Vector<Real>> l1 = bnd1_->getLowerBound();
306     const ROL::Ptr<const Vector<Real>> l2 = bnd2_->getLowerBound();
307     return ROL::makePtr<Vector_SimOpt<Real>>( ROL::constPtrCast<Vector<Real>>(l1),
308                                                  ROL::constPtrCast<Vector<Real>>(l2) );
309   }
310 
getUpperBound(void) const311   const ROL::Ptr<const Vector<Real>> getUpperBound(void) const {
312     const ROL::Ptr<const Vector<Real>> u1 = bnd1_->getUpperBound();
313     const ROL::Ptr<const Vector<Real>> u2 = bnd2_->getUpperBound();
314     return ROL::makePtr<Vector_SimOpt<Real>>( ROL::constPtrCast<Vector<Real>>(u1),
315                                                  ROL::constPtrCast<Vector<Real>>(u2) );
316   }
317 
318   /** \brief Set variables to zero if they correspond to the \f$\epsilon\f$-active set.
319 
320       This function sets \f$v(\xi)=0\f$ if \f$\xi\in\mathcal{A}_\epsilon(x)\f$.  Here,
321       the \f$\epsilon\f$-active set is defined as
322       \f[
323          \mathcal{A}_\epsilon(x) = \mathcal{A}^+_\epsilon(x)\cap\mathcal{A}^-_\epsilon(x).
324       \f]
325       @param[out]      v   is the variable to be pruned.
326       @param[in]       x   is the current optimization variable.
327       @param[in]       eps is the active-set tolerance \f$\epsilon\f$.
328   */
pruneActive(Vector<Real> & v,const Vector<Real> & x,Real eps=0.0)329   void pruneActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
330     ROL::Vector_SimOpt<Real> &vs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(
331       dynamic_cast<ROL::Vector<Real>&>(v));
332     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(
333       dynamic_cast<const ROL::Vector<Real>&>(x));
334     if ( bnd1_->isActivated() ) {
335       ROL::Ptr<Vector<Real> > v1 = vs.get_1()->clone(); v1->set(*(vs.get_1()));
336       bnd1_->pruneActive(*v1,*(xs.get_1()),eps);
337       vs.set_1(*v1);
338     }
339     if ( bnd2_->isActivated() ) {
340       ROL::Ptr<Vector<Real> > v2 = vs.get_2()->clone(); v2->set(*(vs.get_2()));
341       bnd2_->pruneActive(*v2,*(xs.get_2()),eps);
342       vs.set_2(*v2);
343     }
344   }
345 
346   /** \brief Set variables to zero if they correspond to the \f$\epsilon\f$-binding set.
347 
348       This function sets \f$v(\xi)=0\f$ if \f$\xi\in\mathcal{B}_\epsilon(x)\f$.  Here,
349       the \f$\epsilon\f$-binding set is defined as
350       \f[
351          \mathcal{B}^+_\epsilon(x) = \mathcal{B}^+_\epsilon(x)\cap\mathcal{B}^-_\epsilon(x).
352       \f]
353       @param[out]      v   is the variable to be pruned.
354       @param[in]       x   is the current optimization variable.
355       @param[in]       g   is the negative search direction.
356       @param[in]       eps is the active-set tolerance \f$\epsilon\f$.
357   */
pruneActive(Vector<Real> & v,const Vector<Real> & g,const Vector<Real> & x,Real eps=0.0)358   void pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
359     ROL::Vector_SimOpt<Real> &vs = dynamic_cast<ROL::Vector_SimOpt<Real>&>(v);
360     const ROL::Vector_SimOpt<Real> &gs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(g);
361     const ROL::Vector_SimOpt<Real> &xs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(x);
362     if ( bnd1_->isActivated() ) {
363       ROL::Ptr<Vector<Real> > v1 = vs.get_1()->clone(); v1->set(*(vs.get_1()));
364       bnd1_->pruneActive(*v1,*(gs.get_1()),*(xs.get_1()),eps);
365       vs.set_1(*v1);
366     }
367     if ( bnd2_->isActivated() ) {
368       ROL::Ptr<Vector<Real> > v2 = vs.get_2()->clone(); v2->set(*(vs.get_2()));
369       bnd2_->pruneActive(*v2,*(gs.get_2()),*(xs.get_2()),eps);
370       vs.set_2(*v2);
371     }
372   }
373 
374   /** \brief Check if the vector, v, is feasible.
375 
376       This function returns true if \f$v = P_{[a,b]}(v)\f$.
377       @param[in]    v   is the vector to be checked.
378   */
isFeasible(const Vector<Real> & v)379   bool isFeasible( const Vector<Real> &v ) {
380     const ROL::Vector_SimOpt<Real> &vs = dynamic_cast<const ROL::Vector_SimOpt<Real>&>(v);
381     return (bnd1_->isFeasible(*(vs.get_1()))) && (bnd2_->isFeasible(*(vs.get_2())));
382   }
383 
384 }; // class BoundConstraint
385 
386 } // namespace ROL
387 
388 #endif
389