ROL
ROL_BoundConstraint_Partitioned.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_BOUND_CONSTRAINT_PARTITIONED_H
45 #define ROL_BOUND_CONSTRAINT_PARTITIONED_H
46 
47 #include "ROL_BoundConstraint.hpp"
49 #include "ROL_Types.hpp"
50 #include <iostream>
51 
58 namespace ROL {
59 
60 template <class Real>
62 
63  typedef Vector<Real> V;
65  typedef typename std::vector<Real>::size_type uint;
66 
67 private:
68  std::vector<Teuchos::RCP<BoundConstraint<Real> > > bnd_;
69 
70  Teuchos::RCP<V> l_;
71  Teuchos::RCP<V> u_;
72 
73  uint dim_;
74 
75 public:
77 
82  BoundConstraint_Partitioned(const std::vector<Teuchos::RCP<BoundConstraint<Real> > > &bnd )
83  : bnd_(bnd), dim_(bnd.size()) {
85  for( uint k=0; k<dim_; ++k ) {
86  if( bnd_[k]->isActivated() ) {
88  break;
89  }
90  }
91  std::vector<Teuchos::RCP<Vector<Real> > > lp;
92  std::vector<Teuchos::RCP<Vector<Real> > > up;
93 
94  for( uint k=0; k<dim_; ++k ) {
95  lp.push_back(bnd_[k]->getLowerVectorRCP());
96  up.push_back(bnd_[k]->getUpperVectorRCP());
97  }
98 
99  l_ = Teuchos::rcp(new PV(lp) );
100  u_ = Teuchos::rcp(new PV(up) );
101 
102  }
103 
111  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
112 
113  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
114 
115  for( uint k=0; k<dim_; ++k ) {
116  if( bnd_[k]->isActivated() ) {
117  bnd_[k]->update(*(xpv.get(k)),flag,iter);
118  }
119  }
120  }
121 
130  void project( Vector<Real> &x ) {
131 
132  PV &xpv = Teuchos::dyn_cast<PV>(x);
133  for( uint k=0; k<dim_; ++k ) {
134  if( bnd_[k]->isActivated() ) {
135  bnd_[k]->project(*xpv.get(k));
136  }
137  }
138  }
139 
151  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
152 
153  PV &vpv = Teuchos::dyn_cast<PV>(v);
154  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
155 
156  for( uint k=0; k<dim_; ++k ) {
157  if( bnd_[k]->isActivated() ) {
158  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(xpv.get(k)),eps);
159 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
160 // bnd_[k]->pruneUpperActive(*vk,*(xpv.get(k)),eps);
161 // vpv.set(k,*vk);
162  }
163  }
164  }
165 
179  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
180 
181  PV &vpv = Teuchos::dyn_cast<PV>(v);
182  const PV &gpv = Teuchos::dyn_cast<const PV>(g);
183  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
184 
185  for( uint k=0; k<dim_; ++k ) {
186  if( bnd_[k]->isActivated() ) {
187  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
188 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
189 // bnd_[k]->pruneUpperActive(*vk,*(gpv.get(k)),*(xpv.get(k)),eps);
190 // vpv.set(k,*vk);
191  }
192  }
193  }
194 
206  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
207 
208  PV &vpv = Teuchos::dyn_cast<PV>(v);
209  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
210 
211  for( uint k=0; k<dim_; ++k ) {
212  if( bnd_[k]->isActivated() ) {
213  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(xpv.get(k)),eps);
214 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
215 // bnd_[k]->pruneLowerActive(*vk,*(xpv.get(k)),eps);
216 // vpv.set(k,*vk);
217  }
218  }
219  }
220 
234  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
235  PV &vpv = Teuchos::dyn_cast<PV>(v);
236  const PV &gpv = Teuchos::dyn_cast<const PV>(g);
237  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
238 
239  for( uint k=0; k<dim_; ++k ) {
240  if( bnd_[k]->isActivated() ) {
241  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
242 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
243 // bnd_[k]->pruneLowerActive(*vk,*(gpv.get(k)),*(xpv.get(k)),eps);
244 // vpv.set(k,*vk);
245  }
246  }
247  }
248 
249  const Teuchos::RCP<const Vector<Real> > getLowerVectorRCP( void ) const {
250  return l_;
251  }
252 
253 
254 
255  const Teuchos::RCP<const Vector<Real> > getUpperVectorRCP( void ) const {
256  return u_;
257  }
258 
259  const Teuchos::RCP<Vector<Real> > getLowerVectorRCP( void ) {
260  return l_;
261  }
262 
263  const Teuchos::RCP<Vector<Real> > getUpperVectorRCP( void ) {
264  return u_;
265  }
266 
267 
268 
275 /* PV &upv = Teuchos::dyn_cast<PV>(u);
276 
277  for( uint k=0; k<dim_; ++k ) {
278  Teuchos::RCP<V> uk = upv.get(k)->clone();
279  bnd_[k]->setVectorToUpperBound(*uk);
280  upv.set(k,*uk);
281  } */
282  u.set(*u_);
283  }
284 
291 /* PV &lpv = Teuchos::dyn_cast<PV>(l);
292 
293  for( uint k=0; k<dim_; ++k ) {
294  Teuchos::RCP<V> lk = lpv.get(k)->clone();
295  bnd_[k]->setVectorToLowerBound(*lk);
296  lpv.set(k,*lk);
297  } */
298  l.set(*l_);
299  }
300 
312  void pruneActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
313 
314  PV &vpv = Teuchos::dyn_cast<PV>(v);
315  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
316 
317  for( uint k=0; k<dim_; ++k ) {
318  if( bnd_[k]->isActivated() ) {
319 // Teuchos::RCP<V> vk = vpv.get(k)->clone();
320 // bnd_[k]->pruneActive(*vk,*(xpv.get(k)),eps);
321 // vpv.set(k,*vk);
322  bnd_[k]->pruneActive(*(vpv.get(k)),*(xpv.get(k)),eps);
323  }
324  }
325  }
326 
339  void pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
340 
341  PV &vpv = Teuchos::dyn_cast<PV>(v);
342  const PV &gpv = Teuchos::dyn_cast<const PV>(g);
343  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
344 
345  for( uint k=0; k<dim_; ++k ) {
346  if(bnd_[k]->isActivated()) {
347 /*
348  Teuchos::RCP<V> vk = vpv.get(k)->clone();
349  bnd_[k]->pruneActive(*vk,*(gpv.get(k)),*(xpv.get(k)),eps);
350  vpv.set(k,*vk);
351 */
352  bnd_[k]->pruneActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
353 
354  }
355  }
356  }
357 
363  bool isFeasible( const Vector<Real> &v ) {
364  bool feasible = true;
365  const PV &vs = Teuchos::dyn_cast<const PV>(v);
366 
367  for( uint k=0; k<dim_; ++k ) {
368  if(bnd_[k]->isActivated()) {
369  feasible = feasible && bnd_[k]->isFeasible(*(vs.get(k)));
370  }
371  }
372  return feasible;
373  }
374 
375 /*
376  bool isActivated(void) {
377  return activated_;
378  }
379  // This looks suspicious
380  void activate(void) {
381  for( uint k=0; k<dim_; ++k ) {
382  bnd_->activate();
383  }
384  activated_ = true;
385  }
386 
387  void deactivate(void) {
388  std::cout << "deactivate()" << std::endl;
389  for( uint k=0; k<dim_; ++k ) {
390  bnd_->deactivate();
391  }
392  activated_ = false;
393  }
394 */
395 }; // class BoundConstraint_Partitioned
396 
397 
398 
399 template<class Real>
400 Teuchos::RCP<BoundConstraint<Real> >
402  const Teuchos::RCP<BoundConstraint<Real> > &bnd2 ) {
403 
404  using Teuchos::RCP; using Teuchos::rcp;
405  typedef BoundConstraint<Real> BND;
407  RCP<BND> temp[] = {bnd1, bnd2};
408  return rcp( new BNDP( std::vector<RCP<BND> >(temp,temp+2) ) );
409 }
410 
411 
412 } // namespace ROL
413 
414 #endif
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the -active set.
void activate(void)
Turn on bounds.
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< const Vector< Real > > get(size_type i) const
Contains definitions of custom data types in ROL.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the lower -active set.
void setVectorToLowerBound(Vector< Real > &l)
Set the input vector to the lower bound.
BoundConstraint_Partitioned(const std::vector< Teuchos::RCP< BoundConstraint< Real > > > &bnd)
Default constructor.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
void pruneActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the -binding set.
const Teuchos::RCP< Vector< Real > > getUpperVectorRCP(void)
Return the ref count pointer to the upper bound vector.
void setVectorToUpperBound(Vector< Real > &u)
Set the input vector to the upper bound.
bool isActivated(void)
Check if bounds are on.
const Teuchos::RCP< const Vector< Real > > getUpperVectorRCP(void) const
Return the ref count pointer to the upper bound vector.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the lower -binding set.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the upper -active set.
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update bounds.
A composite composite BoundConstraint formed from bound constraints on subvectors of a PartitionedVec...
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real eps=0.0)
Set variables to zero if they correspond to the upper -binding set.
Provides the interface to apply upper and lower bound constraints.
std::vector< Teuchos::RCP< BoundConstraint< Real > > > bnd_
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
const Teuchos::RCP< const Vector< Real > > getLowerVectorRCP(void) const
Return the ref count pointer to the lower bound vector.
bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:198
void deactivate(void)
Turn off bounds.
const Teuchos::RCP< Vector< Real > > getLowerVectorRCP(void)
Return the ref count pointer to the lower bound vector.