ROL
ROL_StdBoundConstraint_Def.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 
15 #ifndef ROL_STDBOUNDCONSTRAINT_DEF_HPP
16 #define ROL_STDBOUNDCONSTRAINT_DEF_HPP
17 
18 namespace ROL {
19 
20 template<class Real>
21 StdBoundConstraint<Real>::StdBoundConstraint(std::vector<Real> &x, bool isLower, Real scale, Real feasTol)
22  : scale_(scale), feasTol_(feasTol), min_diff_(ROL_INF<Real>()){
23  dim_ = x.size();
24  x_lo_.clear(); x_up_.clear();
25  if (isLower) {
26  x_lo_.assign(x.begin(),x.end());
27  x_up_.resize(dim_,ROL_INF<Real>());
29  }
30  else {
31  x_lo_.resize(dim_,ROL_NINF<Real>());
32  x_up_.assign(x.begin(),x.end());
34  }
35 
36  lower_ = makePtr<StdVector<Real>>(makePtrFromRef(x_lo_));
37  upper_ = makePtr<StdVector<Real>>(makePtrFromRef(x_up_));
38 }
39 
40 template<class Real>
41 StdBoundConstraint<Real>::StdBoundConstraint(std::vector<Real> &l, std::vector<Real> &u, Real scale, Real feasTol)
42  : x_lo_(l), x_up_(u), scale_(scale), feasTol_(feasTol) {
44  dim_ = x_lo_.size();
45  for ( int i = 0; i < dim_; i++ ) {
46  if ( i == 0 ) {
47  min_diff_ = x_up_[i] - x_lo_[i];
48  }
49  else {
50  min_diff_ = ( (min_diff_ < (x_up_[i] - x_lo_[i])) ? min_diff_ : (x_up_[i] - x_lo_[i]) );
51  }
52  }
53  min_diff_ *= 0.5;
54 
55  lower_ = makePtr<StdVector<Real>>(makePtrFromRef(x_lo_));
56  upper_ = makePtr<StdVector<Real>>(makePtrFromRef(x_up_));
57 }
58 
59 template<class Real>
62  Ptr<std::vector<Real>> ex =
63  dynamic_cast<StdVector<Real>&>(x).getVector();
65  for ( int i = 0; i < dim_; ++i ) {
66  (*ex)[i] = std::max(x_lo_[i],(*ex)[i]);
67  }
68  }
70  for ( int i = 0; i < dim_; ++i ) {
71  (*ex)[i] = std::min(x_up_[i],(*ex)[i]);
72  }
73  }
74  }
75 }
76 
77 template<class Real>
80  Ptr<std::vector<Real>> ex =
81  dynamic_cast<StdVector<Real>&>(x).getVector();
82  const Real eps = feasTol_;
83  const Real tol = 100.0*ROL_EPSILON<Real>();
84  const Real one(1);
86  for ( int i = 0; i < dim_; ++i ) {
87  Real val = ((x_lo_[i] < -tol) ? (one-eps)*x_lo_[i]
88  : ((x_lo_[i] > tol) ? (one+eps)*x_lo_[i]
89  : x_lo_[i]+eps));
90  val = std::min(x_lo_[i]+eps*min_diff_, val);
91  (*ex)[i] = ((*ex)[i] < val) ? val : (*ex)[i];
92  }
93  }
95  for ( int i = 0; i < dim_; ++i ) {
96  Real val = ((x_up_[i] < -tol) ? (one+eps)*x_up_[i]
97  : ((x_up_[i] > tol) ? (one-eps)*x_up_[i]
98  : x_up_[i]-eps));
99  val = std::max(x_up_[i]-eps*min_diff_, val);
100  (*ex)[i] = ((*ex)[i] > val) ? val : (*ex)[i];
101  }
102  }
103  }
104 }
105 
106 template<class Real>
109  Ptr<const std::vector<Real>> ex =
110  dynamic_cast<const StdVector<Real>&>(x).getVector();
111  Ptr<std::vector<Real>> ev =
112  dynamic_cast<StdVector<Real>&>(v).getVector();
113  Real epsn = std::min(scale_*eps,min_diff_);
114  for ( int i = 0; i < dim_; ++i ) {
115  if ( ((*ex)[i] >= x_up_[i]-epsn) ) {
116  (*ev)[i] = static_cast<Real>(0);
117  }
118  }
119  }
120 }
121 
122 template<class Real>
123 void StdBoundConstraint<Real>::pruneUpperActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps) {
125  Ptr<const std::vector<Real>> ex =
126  dynamic_cast<const StdVector<Real>&>(x).getVector();
127  Ptr<const std::vector<Real>> eg =
128  dynamic_cast<const StdVector<Real>&>(g).getVector();
129  Ptr<std::vector<Real>> ev =
130  dynamic_cast<StdVector<Real>&>(v).getVector();
131  Real epsn = std::min(scale_*xeps,min_diff_);
132  for ( int i = 0; i < dim_; ++i ) {
133  if ( (*ex)[i] >= x_up_[i]-epsn && (*eg)[i] < -geps ) {
134  (*ev)[i] = static_cast<Real>(0);
135  }
136  }
137  }
138 }
139 
140 template<class Real>
143  Ptr<const std::vector<Real>> ex =
144  dynamic_cast<const StdVector<Real>&>(x).getVector();
145  Ptr<std::vector<Real>> ev =
146  dynamic_cast<StdVector<Real>&>(v).getVector();
147  Real epsn = std::min(scale_*eps,min_diff_);
148  for ( int i = 0; i < dim_; ++i ) {
149  if ( ((*ex)[i] <= x_lo_[i]+epsn) ) {
150  (*ev)[i] = static_cast<Real>(0);
151  }
152  }
153  }
154 }
155 
156 template<class Real>
157 void StdBoundConstraint<Real>::pruneLowerActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps, Real geps) {
159  Ptr<const std::vector<Real>> ex =
160  dynamic_cast<const StdVector<Real>&>(x).getVector();
161  Ptr<const std::vector<Real>> eg =
162  dynamic_cast<const StdVector<Real>&>(g).getVector();
163  Ptr<std::vector<Real>> ev =
164  dynamic_cast<StdVector<Real>&>(v).getVector();
165  Real epsn = std::min(scale_*xeps,this->min_diff_);
166  for ( int i = 0; i < dim_; ++i ) {
167  if ( (*ex)[i] <= x_lo_[i]+epsn && (*eg)[i] > geps ) {
168  (*ev)[i] = static_cast<Real>(0);
169  }
170  }
171  }
172 }
173 
174 template<class Real>
176  bool lflag = true, uflag = true;
178  Ptr<const std::vector<Real>> ex =
179  dynamic_cast<const StdVector<Real>&>(x).getVector();
181  for ( int i = 0; i < dim_; ++i ) {
182  if ( (*ex)[i] < x_lo_[i] ) {
183  lflag = false;
184  break;
185  }
186  }
187  }
189  for ( int i = 0; i < dim_; ++i ) {
190  if ( (*ex)[i] > x_up_[i] ) {
191  uflag = false;
192  break;
193  }
194  }
195  }
196  }
197  return (lflag && uflag);
198 }
199 
200 template<class Real>
202  Ptr<std::vector<Real>> ed =
203  dynamic_cast<StdVector<Real>&>(d).getVector();
204  Ptr<const std::vector<Real>> ex =
205  dynamic_cast<const StdVector<Real>&>(x).getVector();
206  Ptr<const std::vector<Real>> eg =
207  dynamic_cast<const StdVector<Real>&>(g).getVector();
208 
209  Real grad, lodiff, updiff, c;
210 
211  for ( int i = 0; i < dim_; ++i ) {
212  grad = (*eg)[i];
213  lodiff = (*ex)[i] - x_lo_[i];
214  updiff = x_up_[i] - (*ex)[i];
215  c = buildC(i);
216  if (-grad > lodiff) {
217  if (lodiff <= updiff) {
218  (*ed)[i] = std::min(std::abs(grad), c);
219  continue;
220  }
221  }
222  if (+grad > updiff) {
223  if (updiff <= lodiff) {
224  (*ed)[i] = std::min(std::abs(grad), c);
225  continue;
226  }
227  }
228  (*ed)[i] = std::min({lodiff, updiff, c});
229  }
230 }
231 
232 template<class Real>
234  buildScalingFunction(dv, x, g);
235 
236  Ptr<std::vector<Real>> edv =
237  dynamic_cast<StdVector<Real>&>(dv).getVector();
238  Ptr<const std::vector<Real>> ev =
239  dynamic_cast<const StdVector<Real>&>(v).getVector();
240 
241  for ( int i = 0; i < dim_; ++i ) {
242  (*edv)[i] = (*ev)[i]/(*edv)[i];
243  }
244 }
245 
246 template<class Real>
248  buildScalingFunction(dv, x, g);
249 
250  Ptr<std::vector<Real>> edv =
251  dynamic_cast<StdVector<Real>&>(dv).getVector();
252  Ptr<const std::vector<Real>> ev =
253  dynamic_cast<const StdVector<Real>&>(v).getVector();
254  Ptr<const std::vector<Real>> ex =
255  dynamic_cast<const StdVector<Real>&>(x).getVector();
256  Ptr<const std::vector<Real>> eg =
257  dynamic_cast<const StdVector<Real>&>(g).getVector();
258 
259  Real zero(0), one(1), indicator, d1prime;
260 
261  for ( int i = 0; i < dim_; ++i ) {
262  indicator = (*edv)[i] < buildC(i) ? one : zero;
263 
264  if (indicator == zero) {
265  (*edv)[i] = zero;
266  continue;
267  }
268 
269  // When indicator is not zero...
270 
271  d1prime = sgn((*eg)[i]);
272  if (d1prime == zero) {
273  d1prime = one;
274  if (x_up_[i] - (*ex)[i] < (*ex)[i] - x_lo_[i])
275  d1prime = -one;
276  }
277  (*edv)[i] = d1prime*(*eg)[i]*(*ev)[i];
278  }
279 }
280 
281 }// End ROL Namespace
282 
283 #endif
Ptr< Vector< Real > > upper_
void projectInterior(Vector< Real > &x) override
Project optimization variables into the interior of the feasible set.
void activateLower(void)
Turn on lower bound.
void activate(void)
Turn on bounds.
Real ROL_INF(void)
Definition: ROL_Types.hpp:71
void buildScalingFunction(Vector< Real > &d, const Vector< Real > &x, const Vector< Real > &g) const
bool isFeasible(const Vector< Real > &v) override
Check if the vector, v, is feasible.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void activateUpper(void)
Turn on upper bound.
Ptr< Vector< Real > > lower_
void applyInverseScalingFunction(Vector< Real > &dv, const Vector< Real > &v, const Vector< Real > &x, const Vector< Real > &g) const override
Apply inverse scaling function.
void pruneUpperActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0)) override
Set variables to zero if they correspond to the upper -active set.
void project(Vector< Real > &x) override
Project optimization variables onto the bounds.
StdBoundConstraint(std::vector< Real > &x, bool isLower=false, Real scale=Real(1), const Real feasTol=std::sqrt(ROL_EPSILON< Real >()))
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 override
Apply scaling function Jacobian.
void pruneLowerActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x, Real xeps=Real(0), Real geps=Real(0)) override
Set variables to zero if they correspond to the -binding set.