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(-BoundConstraint<Real>::computeInf(*x[k]));
103  }
104  }
105  catch (std::exception &e1) {
106  try {
107  lp[k] = x[k]->clone();
108  lp[k]->setScalar(-BoundConstraint<Real>::computeInf(*x[k]));
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( BoundConstraint<Real>::computeInf(*x[k]));
122  }
123  }
124  catch (std::exception &e1) {
125  try {
126  up[k] = x[k]->clone();
127  up[k]->setScalar( BoundConstraint<Real>::computeInf(*x[k]));
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  }
145 
146  void project( Vector<Real> &x ) {
147  PV &xpv = dynamic_cast<PV&>(x);
148  for( uint k=0; k<dim_; ++k ) {
149  if( bnd_[k]->isActivated() ) {
150  bnd_[k]->project(*xpv.get(k));
151  }
152  }
153  }
154 
156  PV &xpv = dynamic_cast<PV&>(x);
157  for( uint k=0; k<dim_; ++k ) {
158  if( bnd_[k]->isActivated() ) {
159  bnd_[k]->projectInterior(*xpv.get(k));
160  }
161  }
162  }
163 
164  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0) ) {
165  PV &vpv = dynamic_cast<PV&>(v);
166  const PV &xpv = dynamic_cast<const PV&>(x);
167  for( uint k=0; k<dim_; ++k ) {
168  if( bnd_[k]->isActivated() ) {
169  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(xpv.get(k)),eps);
170  }
171  }
172  }
173 
174  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0)) {
175  PV &vpv = dynamic_cast<PV&>(v);
176  const PV &gpv = dynamic_cast<const PV&>(g);
177  const PV &xpv = dynamic_cast<const PV&>(x);
178  for( uint k=0; k<dim_; ++k ) {
179  if( bnd_[k]->isActivated() ) {
180  bnd_[k]->pruneUpperActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),xeps,geps);
181  }
182  }
183  }
184 
185  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0) ) {
186  PV &vpv = dynamic_cast<PV&>(v);
187  const PV &xpv = dynamic_cast<const PV&>(x);
188  for( uint k=0; k<dim_; ++k ) {
189  if( bnd_[k]->isActivated() ) {
190  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(xpv.get(k)),eps);
191  }
192  }
193  }
194 
195  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0) ) {
196  PV &vpv = dynamic_cast<PV&>(v);
197  const PV &gpv = dynamic_cast<const PV&>(g);
198  const PV &xpv = dynamic_cast<const PV&>(x);
199  for( uint k=0; k<dim_; ++k ) {
200  if( bnd_[k]->isActivated() ) {
201  bnd_[k]->pruneLowerActive(*(vpv.get(k)),*(gpv.get(k)),*(xpv.get(k)),xeps,geps);
202  }
203  }
204  }
205 
206  bool isFeasible( const Vector<Real> &v ) {
207  bool feasible = true;
208  const PV &vs = dynamic_cast<const PV&>(v);
209  for( uint k=0; k<dim_; ++k ) {
210  if(bnd_[k]->isActivated()) {
211  feasible = feasible && bnd_[k]->isFeasible(*(vs.get(k)));
212  }
213  }
214  return feasible;
215  }
216 
217  void applyInverseScalingFunction(Vector<Real> &dv, const Vector<Real> &v, const Vector<Real> &x, const Vector<Real> &g) const {
218  PV &dvpv = dynamic_cast<PV&>(dv);
219  const PV &vpv = dynamic_cast<const PV&>(v);
220  const PV &xpv = dynamic_cast<const PV&>(x);
221  const PV &gpv = dynamic_cast<const PV&>(g);
222  for( uint k=0; k<dim_; ++k ) {
223  if( bnd_[k]->isActivated() ) {
224  bnd_[k]->applyInverseScalingFunction(*(dvpv.get(k)),*(vpv.get(k)),*(xpv.get(k)),*(gpv.get(k)));
225  }
226  }
227  }
228 
229  void applyScalingFunctionJacobian(Vector<Real> &dv, const Vector<Real> &v, const Vector<Real> &x, const Vector<Real> &g) const {
230  PV &dvpv = dynamic_cast<PV&>(dv);
231  const PV &vpv = dynamic_cast<const PV&>(v);
232  const PV &xpv = dynamic_cast<const PV&>(x);
233  const PV &gpv = dynamic_cast<const PV&>(g);
234  for( uint k=0; k<dim_; ++k ) {
235  if( bnd_[k]->isActivated() ) {
236  bnd_[k]->applyScalingFunctionJacobian(*(dvpv.get(k)),*(vpv.get(k)),*(xpv.get(k)),*(gpv.get(k)));
237  }
238  }
239  }
240 }; // class BoundConstraint_Partitioned
241 
242 
243 
244 template<typename Real>
245 Ptr<BoundConstraint<Real>>
247  const Ptr<BoundConstraint<Real>> &bnd2 ) {
248 
249 
250  typedef BoundConstraint<Real> BND;
252  Ptr<BND> temp[] = {bnd1, bnd2};
253  return makePtr<BNDP>( std::vector<Ptr<BND>>(temp,temp+2) );
254 }
255 
256 
257 } // namespace ROL
258 
259 #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)
ROL::Ptr< const Vector< Real > > get(size_type i) const
void applyScalingFunctionJacobian(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
Apply scaling function Jacobian.
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.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual const Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower bound vector.
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_
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...
virtual const Ptr< const Vector< Real > > getUpperBound(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 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 applyInverseScalingFunction(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
Apply inverse scaling function.
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
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.
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.