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;
66 
67 private:
68  std::vector<ROL::Ptr<BoundConstraint<Real> > > bnd_;
69 
70  ROL::Ptr<V> l_;
71  ROL::Ptr<V> u_;
72 
74 
75  bool hasLvec_;
76  bool hasUvec_;
77 
78 public:
80 
81  BoundConstraint_Partitioned(const std::vector<ROL::Ptr<BoundConstraint<Real> > > &bnd,
82  const std::vector<ROL::Ptr<Vector<Real> > > &x)
83  : bnd_(bnd), dim_(bnd.size()), hasLvec_(true), hasUvec_(true) {
85  for( uint k=0; k<dim_; ++k ) {
86  if( bnd_[k]->isActivated() ) {
88  break;
89  }
90  }
91  std::vector<ROL::Ptr<Vector<Real> > > lp(dim_);
92  std::vector<ROL::Ptr<Vector<Real> > > up(dim_);
93  for( uint k=0; k<dim_; ++k ) {
94  try {
95  lp[k] = x[k]->clone();
96  if (bnd_[k]->isLowerActivated()) {
97  lp[k]->set(*bnd_[k]->getLowerBound());
98  }
99  else {
100  lp[k]->setScalar(ROL_NINF<Real>());
101  }
102  }
103  catch (std::exception &e1) {
104  try {
105  lp[k] = x[k]->clone();
106  lp[k]->setScalar(ROL_NINF<Real>());
107  }
108  catch (std::exception &e2) {
109  lp[k] = ROL::nullPtr;
110  hasLvec_ = false;
111  }
112  }
113  try {
114  up[k] = x[k]->clone();
115  if (bnd_[k]->isUpperActivated()) {
116  up[k]->set(*bnd_[k]->getUpperBound());
117  }
118  else {
119  up[k]->setScalar(ROL_INF<Real>());
120  }
121  }
122  catch (std::exception &e1) {
123  try {
124  up[k] = x[k]->clone();
125  up[k]->setScalar(ROL_INF<Real>());
126  }
127  catch (std::exception &e2) {
128  up[k] = ROL::nullPtr;
129  hasUvec_ = false;
130  }
131  }
132  }
133  if (hasLvec_) {
134  l_ = ROL::makePtr<PV>(lp);
135  }
136  if (hasUvec_) {
137  u_ = ROL::makePtr<PV>(up);
138  }
139  }
140 
141  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
142  const PV &xpv = dynamic_cast<const PV&>(x);
143  for( uint k=0; k<dim_; ++k ) {
144  if( bnd_[k]->isActivated() ) {
145  bnd_[k]->update(*(xpv.get(k)),flag,iter);
146  }
147  }
148  }
149 
150  void project( Vector<Real> &x ) {
151  PV &xpv = dynamic_cast<PV&>(x);
152  for( uint k=0; k<dim_; ++k ) {
153  if( bnd_[k]->isActivated() ) {
154  bnd_[k]->project(*xpv.get(k));
155  }
156  }
157  }
158 
160  PV &xpv = dynamic_cast<PV&>(x);
161  for( uint k=0; k<dim_; ++k ) {
162  if( bnd_[k]->isActivated() ) {
163  bnd_[k]->projectInterior(*xpv.get(k));
164  }
165  }
166  }
167 
168  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
169  PV &vpv = dynamic_cast<PV&>(v);
170  const PV &xpv = dynamic_cast<const PV&>(x);
171  for( uint k=0; k<dim_; ++k ) {
172  if( bnd_[k]->isActivated() ) {
173  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(xpv.get(k)),eps);
174  }
175  }
176  }
177 
178  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
179  PV &vpv = dynamic_cast<PV&>(v);
180  const PV &gpv = dynamic_cast<const PV&>(g);
181  const PV &xpv = dynamic_cast<const PV&>(x);
182  for( uint k=0; k<dim_; ++k ) {
183  if( bnd_[k]->isActivated() ) {
184  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
185  }
186  }
187  }
188 
189  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = 0.0 ) {
190  PV &vpv = dynamic_cast<PV&>(v);
191  const PV &xpv = dynamic_cast<const PV&>(x);
192  for( uint k=0; k<dim_; ++k ) {
193  if( bnd_[k]->isActivated() ) {
194  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(xpv.get(k)),eps);
195  }
196  }
197  }
198 
199  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real eps = 0.0 ) {
200  PV &vpv = dynamic_cast<PV&>(v);
201  const PV &gpv = dynamic_cast<const PV&>(g);
202  const PV &xpv = dynamic_cast<const PV&>(x);
203  for( uint k=0; k<dim_; ++k ) {
204  if( bnd_[k]->isActivated() ) {
205  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),eps);
206  }
207  }
208  }
209 
210  const ROL::Ptr<const Vector<Real> > getLowerBound( void ) const {
211  if (hasLvec_) {
212  return l_;
213  }
214  else {
216  }
217  }
218 
219  const ROL::Ptr<const Vector<Real> > getUpperBound( void ) const {
220  if (hasUvec_) {
221  return u_;
222  }
223  else {
225  }
226  }
227 
228  bool isFeasible( const Vector<Real> &v ) {
229  bool feasible = true;
230  const PV &vs = dynamic_cast<const PV&>(v);
231  for( uint k=0; k<dim_; ++k ) {
232  if(bnd_[k]->isActivated()) {
233  feasible = feasible && bnd_[k]->isFeasible(*(vs.get(k)));
234  }
235  }
236  return feasible;
237  }
238 }; // class BoundConstraint_Partitioned
239 
240 
241 
242 template<class Real>
243 ROL::Ptr<BoundConstraint<Real> >
245  const ROL::Ptr<BoundConstraint<Real> > &bnd2 ) {
246 
247 
248  typedef BoundConstraint<Real> BND;
250  ROL::Ptr<BND> temp[] = {bnd1, bnd2};
251  return ROL::makePtr<BNDP>( std::vector<ROL::Ptr<BND>>(temp,temp+2) );
252 }
253 
254 
255 } // namespace ROL
256 
257 #endif
virtual const ROL::Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
typename PV< Real >::size_type size_type
const ROL::Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
ROL::Ptr< const Vector< Real > > get(size_type i) const
void activate(void)
Turn on bounds.
bool isActivated(void) const
Check if bounds are on.
Defines the linear algebra of vector space on a generic partitioned vector.
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.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
ROL::Ptr< BoundConstraint< Real > > CreateBoundConstraint_Partitioned(const ROL::Ptr< BoundConstraint< Real > > &bnd1, const ROL::Ptr< BoundConstraint< Real > > &bnd2)
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 -binding set.
const ROL::Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
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.
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
BoundConstraint_Partitioned(const std::vector< ROL::Ptr< BoundConstraint< Real > > > &bnd, const std::vector< ROL::Ptr< Vector< Real > > > &x)
std::vector< ROL::Ptr< BoundConstraint< Real > > > bnd_
bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
virtual const ROL::Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
void deactivate(void)
Turn off bounds.
bool isLowerActivated(void) const
Check if lower bound are on.
bool isUpperActivated(void) const
Check if upper bound are on.
void projectInterior(Vector< Real > &x)
Project optimization variables into the interior of the feasible set.