ROL
ROL_SimulatedBoundConstraint.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_SIMULATED_BOUND_CONSTRAINT_H
11 #define ROL_SIMULATED_BOUND_CONSTRAINT_H
12 
13 #include "ROL_BoundConstraint.hpp"
14 #include "ROL_SimulatedVector.hpp"
15 
22 namespace ROL {
23 
24 template <class Real>
26 private:
27  const Ptr<SampleGenerator<Real>> sampler_;
28  const Ptr<BoundConstraint<Real>> bnd_;
29  Ptr<Vector<Real>> l_;
30  Ptr<Vector<Real>> u_;
31 
32  const Vector<Real>& getVector(const Vector<Real> &x, int k) const {
33  try {
34  return *(dynamic_cast<const SimulatedVector<Real>&>(x).get(k));
35  }
36  catch (const std::bad_cast &e) {
37  return x;
38  }
39  }
40 
41  Vector<Real>& getVector(Vector<Real> &x, int k) const {
42  try {
43  return *(dynamic_cast<SimulatedVector<Real>&>(x).get(k));
44  }
45  catch (const std::bad_cast &e) {
46  return x;
47  }
48  }
49 
50 public:
52 
54  const Ptr<BoundConstraint<Real>> &bnd )
55  : sampler_(sampler), bnd_(bnd) {
56  int nsamp = sampler_->numMySamples();
57  std::vector<Ptr<Vector<Real>>> lvec(nsamp), uvec(nsamp);
58  for ( int k=0; k<sampler_->numMySamples(); ++k) {
59  lvec[k] = bnd_->getLowerBound()->clone();
60  lvec[k]->set(*bnd_->getLowerBound());
61  uvec[k] = bnd_->getUpperBound()->clone();
62  uvec[k]->set(*bnd_->getUpperBound());
63  }
64  l_ = makePtr<SimulatedVector<Real>>(lvec,sampler_->getBatchManager());
65  u_ = makePtr<SimulatedVector<Real>>(uvec,sampler_->getBatchManager());
66  }
67 
68  void project( Vector<Real> &x ) {
69  for( int k=0; k<sampler_->numMySamples(); ++k ) {
70  if( bnd_->isActivated() ) {
71  bnd_->project(getVector(x,k));
72  }
73  }
74  }
75 
77  for( int k=0; k<sampler_->numMySamples(); ++k ) {
78  if( bnd_->isActivated() ) {
79  bnd_->projectInterior(getVector(x,k));
80  }
81  }
82  }
83 
84  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0) ) {
85  if( bnd_->isActivated() ) {
86  for( int k=0; k<sampler_->numMySamples(); ++k ) {
87  bnd_->pruneUpperActive(getVector(v,k),getVector(x,k),eps);
88  }
89  }
90  }
91 
92  void pruneUpperActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0) ) {
93  if( bnd_->isActivated() ) {
94  for( int k=0; k<sampler_->numMySamples(); ++k ) {
95  bnd_->pruneUpperActive(getVector(v,k),getVector(g,k),getVector(x,k),xeps,geps);
96  }
97  }
98  }
99 
100  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0) ) {
101  if( bnd_->isActivated() ) {
102  for( int k=0; k<sampler_->numMySamples(); ++k ) {
103  bnd_->pruneLowerActive(getVector(v,k),getVector(x,k),eps);
104  }
105  }
106  }
107 
108  void pruneLowerActive( Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0) ) {
109  if( bnd_->isActivated() ) {
110  for( int k=0; k<sampler_->numMySamples(); ++k ) {
111  bnd_->pruneLowerActive(getVector(v,k),getVector(g,k),getVector(x,k),xeps,geps);
112  }
113  }
114  }
115 
116  const Ptr<const Vector<Real>> getLowerBound( void ) const {
117  return l_;
118  }
119 
120  const Ptr<const Vector<Real>> getUpperBound( void ) const {
121  return u_;
122  }
123 
124  bool isFeasible( const Vector<Real> &v ) {
125  bool feasible = true;
126  if(bnd_->isActivated()) {
127  for( int k=0; k<sampler_->numMySamples(); ++k ) {
128  feasible = feasible && bnd_->isFeasible(getVector(v,k));
129  }
130  }
131  return feasible;
132  }
133 
134  void applyInverseScalingFunction(Vector<Real> &dv, const Vector<Real> &v, const Vector<Real> &x, const Vector<Real> &g) const {
135  if( bnd_->isActivated() ) {
136  for( int k=0; k<sampler_->numMySamples(); ++k ) {
137  bnd_->applyInverseScalingFunction(getVector(dv,k),getVector(v,k),getVector(x,k),getVector(g,k));
138  }
139  }
140  }
141 
142  void applyScalingFunctionJacobian(Vector<Real> &dv, const Vector<Real> &v, const Vector<Real> &x, const Vector<Real> &g) const {
143  if( bnd_->isActivated() ) {
144  for( int k=0; k<sampler_->numMySamples(); ++k ) {
145  bnd_->applyScalingFunctionJacobian(getVector(dv,k),getVector(v,k),getVector(x,k),getVector(g,k));
146  }
147  }
148  }
149 }; // class SimulatedBoundConstraint
150 } // namespace ROL
151 
152 #endif
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.
Vector< Real > & getVector(Vector< Real > &x, int k) const
void project(Vector< Real > &x)
Project optimization variables onto the bounds.
A BoundConstraint formed from a single bound constraint replacated according to a SampleGenerator...
void projectInterior(Vector< Real > &x)
Project optimization variables into the interior of the feasible 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 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.
SimulatedBoundConstraint(const Ptr< SampleGenerator< Real >> &sampler, const Ptr< BoundConstraint< Real >> &bnd)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
const Ptr< SampleGenerator< Real > > sampler_
Defines the linear algebra of a vector space on a generic partitioned vector where the individual vec...
const Vector< Real > & getVector(const Vector< Real > &x, int k) const
bool isFeasible(const Vector< Real > &v)
Check if the vector, v, is feasible.
Provides the interface to apply upper and lower bound constraints.
void applyScalingFunctionJacobian(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
Apply scaling function Jacobian.
const Ptr< BoundConstraint< Real > > bnd_
const Ptr< const Vector< Real > > getLowerBound(void) const
Return the ref count pointer to the lower 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.
const Ptr< const Vector< Real > > getUpperBound(void) const
Return the ref count pointer to the upper bound vector.
void applyInverseScalingFunction(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const
Apply inverse scaling function.