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