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 
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  std::cout << bnd_[k]->isActivated() << std::endl;
87  if( bnd_[k]->isActivated() ) {
89  break;
90  }
91  }
92  std::cout << BoundConstraint<Real>::isActivated() << std::endl;
93  std::vector<Teuchos::RCP<Vector<Real> > > lp;
94  std::vector<Teuchos::RCP<Vector<Real> > > up;
95 
96  for( uint k=0; k<dim_; ++k ) {
97  lp.push_back(bnd_[k]->getLowerVectorRCP());
98  up.push_back(bnd_[k]->getUpperVectorRCP());
99  }
100 
101  l_ = Teuchos::rcp(new PV(lp) );
102  u_ = Teuchos::rcp(new PV(up) );
103 
104  }
105 
113  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
114 
115  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
116 
117  for( uint k=0; k<dim_; ++k ) {
118  if( bnd_[k]->isActivated() ) {
119  bnd_[k]->update(*(xpv.get(k)),flag,iter);
120  }
121  }
122  }
123 
132  void project( Vector<Real> &x ) {
133 
134  PV &xpv = Teuchos::dyn_cast<PV>(x);
135  for( uint k=0; k<dim_; ++k ) {
136  if( bnd_[k]->isActivated() ) {
137  bnd_[k]->project(*xpv.get(k));
138  }
139  }
140  }
141 
153  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
154 
155  PV &vpv = Teuchos::dyn_cast<PV>(v);
156  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
157 
158  for( uint k=0; k<dim_; ++k ) {
159  if( bnd_[k]->isActivated() ) {
160  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(xpv.get(k)),eps);
161 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
162 // bnd_[k]->pruneUpperActive(*vk,*(xpv.get(k)),eps);
163 // vpv.set(k,*vk);
164  }
165  }
166  }
167 
181  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
182 
183  PV &vpv = Teuchos::dyn_cast<PV>(v);
184  const PV &gpv = Teuchos::dyn_cast<const PV>(g);
185  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
186 
187  for( uint k=0; k<dim_; ++k ) {
188  if( bnd_[k]->isActivated() ) {
189  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
190 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
191 // bnd_[k]->pruneUpperActive(*vk,*(gpv.get(k)),*(xpv.get(k)),eps);
192 // vpv.set(k,*vk);
193  }
194  }
195  }
196 
208  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
209 
210  PV &vpv = Teuchos::dyn_cast<PV>(v);
211  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
212 
213  for( uint k=0; k<dim_; ++k ) {
214  if( bnd_[k]->isActivated() ) {
215  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(xpv.get(k)),eps);
216 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
217 // bnd_[k]->pruneLowerActive(*vk,*(xpv.get(k)),eps);
218 // vpv.set(k,*vk);
219  }
220  }
221  }
222 
236  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
237  PV &vpv = Teuchos::dyn_cast<PV>(v);
238  const PV &gpv = Teuchos::dyn_cast<const PV>(g);
239  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
240 
241  for( uint k=0; k<dim_; ++k ) {
242  if( bnd_[k]->isActivated() ) {
243  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
244 // Teuchos::RCP<V> vk = vpv.get(k)->clone(); vk->set(*(vpv.get(k)));
245 // bnd_[k]->pruneLowerActive(*vk,*(gpv.get(k)),*(xpv.get(k)),eps);
246 // vpv.set(k,*vk);
247  }
248  }
249  }
250 
251  const Teuchos::RCP<const Vector<Real> > getLowerVectorRCP( void ) const {
252  return l_;
253  }
254 
255 
256 
257  const Teuchos::RCP<const Vector<Real> > getUpperVectorRCP( void ) const {
258  return u_;
259  }
260 
261  const Teuchos::RCP<Vector<Real> > getLowerVectorRCP( void ) {
262  return l_;
263  }
264 
265  const Teuchos::RCP<Vector<Real> > getUpperVectorRCP( void ) {
266  return u_;
267  }
268 
269 
270 
277 /* PV &upv = Teuchos::dyn_cast<PV>(u);
278 
279  for( uint k=0; k<dim_; ++k ) {
280  Teuchos::RCP<V> uk = upv.get(k)->clone();
281  bnd_[k]->setVectorToUpperBound(*uk);
282  upv.set(k,*uk);
283  } */
284  u.set(*u_);
285  }
286 
293 /* PV &lpv = Teuchos::dyn_cast<PV>(l);
294 
295  for( uint k=0; k<dim_; ++k ) {
296  Teuchos::RCP<V> lk = lpv.get(k)->clone();
297  bnd_[k]->setVectorToLowerBound(*lk);
298  lpv.set(k,*lk);
299  } */
300  l.set(*l_);
301  }
302 
314  void pruneActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
315 
316  PV &vpv = Teuchos::dyn_cast<PV>(v);
317  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
318 
319  for( uint k=0; k<dim_; ++k ) {
320  if( bnd_[k]->isActivated() ) {
321 // Teuchos::RCP<V> vk = vpv.get(k)->clone();
322 // bnd_[k]->pruneActive(*vk,*(xpv.get(k)),eps);
323 // vpv.set(k,*vk);
324  bnd_[k]->pruneActive(*(vpv.get(k)),*(xpv.get(k)),eps);
325  }
326  }
327  }
328 
341  void pruneActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
342 
343  PV &vpv = Teuchos::dyn_cast<PV>(v);
344  const PV &gpv = Teuchos::dyn_cast<const PV>(g);
345  const PV &xpv = Teuchos::dyn_cast<const PV>(x);
346 
347  for( uint k=0; k<dim_; ++k ) {
348  if(bnd_[k]->isActivated()) {
349 /*
350  Teuchos::RCP<V> vk = vpv.get(k)->clone();
351  bnd_[k]->pruneActive(*vk,*(gpv.get(k)),*(xpv.get(k)),eps);
352  vpv.set(k,*vk);
353 */
354  bnd_[k]->pruneActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
355 
356  }
357  }
358  }
359 
365  bool isFeasible( const Vector<Real> &v ) {
366  bool feasible = true;
367  const PV &vs = Teuchos::dyn_cast<const PV>(v);
368 
369  for( uint k=0; k<dim_; ++k ) {
370  if(bnd_[k]->isActivated()) {
371  feasible = feasible && bnd_[k]->isFeasible(*(vs.get(k)));
372  }
373  }
374  return feasible;
375  }
376 
377 /*
378  bool isActivated(void) {
379  return activated_;
380  }
381  // This looks suspicious
382  void activate(void) {
383  for( uint k=0; k<dim_; ++k ) {
384  bnd_->activate();
385  }
386  activated_ = true;
387  }
388 
389  void deactivate(void) {
390  std::cout << "deactivate()" << std::endl;
391  for( uint k=0; k<dim_; ++k ) {
392  bnd_->deactivate();
393  }
394  activated_ = false;
395  }
396 */
397 }; // class BoundConstraint_Partitioned
398 
399 
400 
401 template<class Real>
402 Teuchos::RCP<BoundConstraint<Real> >
404  const Teuchos::RCP<BoundConstraint<Real> > &bnd2 ) {
405 
406  using Teuchos::RCP; using Teuchos::rcp;
407  typedef BoundConstraint<Real> BND;
409  RCP<BND> temp[] = {bnd1, bnd2};
410  return rcp( new BNDP( std::vector<RCP<BND> >(temp,temp+2) ) );
411 }
412 
413 
414 } // namespace ROL
415 
416 #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:74
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:196
void deactivate(void)
Turn off bounds.
const Teuchos::RCP< Vector< Real > > getLowerVectorRCP(void)
Return the ref count pointer to the lower bound vector.