ROL
ROL_ConstraintManager.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_CONSTRAINT_MANAGER_H
11 #define ROL_CONSTRAINT_MANAGER_H
12 
13 #include "ROL_Constraint.hpp"
16 
24 namespace ROL {
25 
26 template <class Real>
28 private:
29  ROL::Ptr<Constraint<Real> > con_;
30  ROL::Ptr<Vector<Real> > l_;
31  ROL::Ptr<Vector<Real> > x_;
32  ROL::Ptr<BoundConstraint<Real> > bnd_;
33 
34  std::vector<ROL::Ptr<Constraint<Real> > > cvec_;
35  std::vector<ROL::Ptr<Vector<Real> > > lvec_;
36  std::vector<ROL::Ptr<Vector<Real> > > svec_;
37  std::vector<ROL::Ptr<BoundConstraint<Real> > > sbnd_;
38 
39  std::vector<bool> isInequality_;
40 
41  bool isNull_;
43 
44  void initializeSlackVariable(const ROL::Ptr<Constraint<Real> > &con,
45  const ROL::Ptr<BoundConstraint<Real> > &cbnd,
46  const ROL::Ptr<Vector<Real> > &s,
47  const ROL::Ptr<Vector<Real> > &x) const {
48  // Set slack variable to s = proj(c(x))
49  Real tol = std::sqrt(ROL_EPSILON<Real>());
50  con->value(*s,*x,tol);
51  cbnd->project(*s);
52  }
53 
54  void initialize(const std::vector<ROL::Ptr<Constraint<Real> > > &cvec,
55  const std::vector<ROL::Ptr<Vector<Real> > > &lvec,
56  const std::vector<ROL::Ptr<BoundConstraint<Real> > > &bvec,
57  const ROL::Ptr<Vector<Real> > &x,
58  const ROL::Ptr<BoundConstraint<Real> > &bnd) {
59  // Check size of multiplier vector and constraint vector
60  int size = static_cast<int>(cvec.size());
61  if ( size != static_cast<int>(lvec.size()) ) {
62  throw Exception::NotImplemented(">>> ROL::ConstraintManager: Constraint and multiplier vectors are different sizes!");
63  }
64  if ( size != static_cast<int>(bvec.size()) ) {
65  throw Exception::NotImplemented(">>> ROL::ConstraintManager: Constraint and BoundConstraint vectors are different sizes!");
66  }
67  // If bnd is null, then make a null BoundConstraint
68  ROL::Ptr<BoundConstraint<Real> > bnd0;
69  if ( bnd == ROL::nullPtr ) {
70  bnd0 = ROL::makePtr<BoundConstraint<Real>>(*x);
71  bnd0->deactivate();
72  }
73  else {
74  bnd0 = bnd;
75  }
76  // Build slack variables
77  svec_.clear(); svec_.push_back(x);
78  sbnd_.clear(); sbnd_.push_back(bnd0);
79  cvec_.clear(); lvec_.clear(); isInequality_.clear();
80  int cnt = 1, cnt_con = 0;
81  isNull_ = true;
82  hasInequality_ = false;
83  for (int i = 0; i < size; ++i) {
84  ROL::Ptr<Constraint<Real> > con = cvec[i];
85  ROL::Ptr<Vector<Real> > l = lvec[i];
86  ROL::Ptr<BoundConstraint<Real> > cbnd = bvec[i];
87  if (con != ROL::nullPtr) {
88  if ( con->isActivated() ) {
89  // Set default type to equality
90  isInequality_.push_back(false);
91  // Fill constraint and multiplier vectors
92  cvec_.push_back(con);
93  lvec_.push_back(l);
94  if (cbnd != ROL::nullPtr) {
95  if ( cbnd->isActivated() ) {
96  // Set type to inequality
97  isInequality_.back() = true;
98  // Create slack variables
99  svec_.push_back(l->dual().clone());
100  initializeSlackVariable(con,cbnd,svec_[cnt],x);
101  // Create slack bound
102  sbnd_.push_back(cbnd);
103  // Update inequality constraint counter
104  cnt++;
105  hasInequality_ = true;
106  }
107  }
108  cnt_con++;
109  isNull_ = false;
110  }
111  }
112  }
113  // Create partitioned constraint and multiplier vector
114  if ( !isNull_ ) {
115  if ( cnt_con > 1 || hasInequality_ ) {
116  con_ = ROL::makePtr<Constraint_Partitioned<Real>>(cvec_,isInequality_);
117  l_ = ROL::makePtr<PartitionedVector<Real>>(lvec_);
118  }
119  else {
120  con_ = cvec_[0];
121  l_ = lvec_[0];
122  }
123  }
124  else {
125  con_ = ROL::nullPtr;
126  l_ = ROL::nullPtr;
127  }
128  // Create partitioned optimization vector and bound constraint
129  if ( hasInequality_ ) {
130  x_ = ROL::makePtr<PartitionedVector<Real>>(svec_);
131  bnd_ = ROL::makePtr<BoundConstraint_Partitioned<Real>>(sbnd_,svec_);
132  }
133  else {
134  x_ = x;
135  bnd_ = bnd0;
136  }
137  }
138 
139 public:
140  virtual ~ConstraintManager(void) {}
141 
142  ConstraintManager(const std::vector<ROL::Ptr<Constraint<Real> > > &cvec,
143  const std::vector<ROL::Ptr<Vector<Real> > > &lvec,
144  const std::vector<ROL::Ptr<BoundConstraint<Real> > > &bvec,
145  const ROL::Ptr<Vector<Real> > &x,
146  const ROL::Ptr<BoundConstraint<Real> > &bnd = ROL::nullPtr)
147  : isNull_(true), hasInequality_(false) {
148  initialize(cvec,lvec,bvec,x,bnd);
149  }
150 
151  ConstraintManager(const std::vector<ROL::Ptr<Constraint<Real> > > &cvec,
152  const std::vector<ROL::Ptr<Vector<Real> > > &lvec,
153  const ROL::Ptr<Vector<Real> > &x,
154  const ROL::Ptr<BoundConstraint<Real> > &bnd = ROL::nullPtr)
155  : isNull_(true), hasInequality_(false) {
156  std::vector<ROL::Ptr<BoundConstraint<Real> > > bvec(cvec.size(),ROL::nullPtr);
157  initialize(cvec,lvec,bvec,x,bnd);
158  }
159 
160  ConstraintManager(const ROL::Ptr<Constraint<Real> > &con,
161  const ROL::Ptr<Vector<Real> > &l,
162  const ROL::Ptr<BoundConstraint<Real> > &cbnd,
163  const ROL::Ptr<Vector<Real> > &x,
164  const ROL::Ptr<BoundConstraint<Real> > &bnd = ROL::nullPtr)
165  : isNull_(true), hasInequality_(false) {
166  std::vector<ROL::Ptr<Constraint<Real> > > cvec(1,con);
167  std::vector<ROL::Ptr<Vector<Real> > > lvec(1,l);
168  std::vector<ROL::Ptr<BoundConstraint<Real> > > bvec(1,cbnd);
169  initialize(cvec,lvec,bvec,x,bnd);
170  }
171 
172  ConstraintManager(const ROL::Ptr<Constraint<Real> > &con,
173  const ROL::Ptr<Vector<Real> > &l,
174  const ROL::Ptr<Vector<Real> > &x,
175  const ROL::Ptr<BoundConstraint<Real> > &bnd = ROL::nullPtr)
176  : isNull_(true), hasInequality_(false) {
177  std::vector<ROL::Ptr<Constraint<Real> > > cvec(1,con);
178  std::vector<ROL::Ptr<Vector<Real> > > lvec(1,l);
179  std::vector<ROL::Ptr<BoundConstraint<Real> > > bvec(1,ROL::nullPtr);
180  initialize(cvec,lvec,bvec,x,bnd);
181  }
182 
183  const ROL::Ptr<Constraint<Real> > getConstraint(void) const {
184  return con_;
185  }
186 
187  const ROL::Ptr<Vector<Real> > getMultiplier(void) const {
188  return l_;
189  }
190 
191  const ROL::Ptr<Vector<Real> > getOptVector(void) const {
192  return x_;
193  }
194 
195  const ROL::Ptr<BoundConstraint<Real> > getBoundConstraint(void) const {
196  return bnd_;
197  }
198 
199  bool isNull(void) const {
200  return isNull_;
201  }
202 
203  bool hasInequality(void) const {
204  return hasInequality_;
205  }
206 
207  void resetSlackVariables(void) {
208  if (hasInequality_) {
209  int size = static_cast<int>(cvec_.size());
210  int cnt = 1;
211  for (int i = 0; i < size; ++i) {
212  if (isInequality_[i]) {
213  // Reset slack variables
214  initializeSlackVariable(cvec_[i],sbnd_[cnt],svec_[cnt],svec_[0]);
215  cnt++;
216  }
217  }
218  }
219  }
220 
221 }; // class ConstraintManager
222 
223 } // namespace ROL
224 
225 #endif
ConstraintManager(const std::vector< ROL::Ptr< Constraint< Real > > > &cvec, const std::vector< ROL::Ptr< Vector< Real > > > &lvec, const ROL::Ptr< Vector< Real > > &x, const ROL::Ptr< BoundConstraint< Real > > &bnd=ROL::nullPtr)
std::vector< ROL::Ptr< Vector< Real > > > lvec_
ROL::Ptr< Vector< Real > > l_
std::vector< ROL::Ptr< Vector< Real > > > svec_
void initializeSlackVariable(const ROL::Ptr< Constraint< Real > > &con, const ROL::Ptr< BoundConstraint< Real > > &cbnd, const ROL::Ptr< Vector< Real > > &s, const ROL::Ptr< Vector< Real > > &x) const
const ROL::Ptr< Vector< Real > > getMultiplier(void) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
const ROL::Ptr< Vector< Real > > getOptVector(void) const
const ROL::Ptr< Constraint< Real > > getConstraint(void) const
ROL::Ptr< BoundConstraint< Real > > bnd_
ROL::Ptr< Constraint< Real > > con_
ConstraintManager(const ROL::Ptr< Constraint< Real > > &con, const ROL::Ptr< Vector< Real > > &l, const ROL::Ptr< Vector< Real > > &x, const ROL::Ptr< BoundConstraint< Real > > &bnd=ROL::nullPtr)
Provides a wrapper for multiple constraints.
void initialize(const std::vector< ROL::Ptr< Constraint< Real > > > &cvec, const std::vector< ROL::Ptr< Vector< Real > > > &lvec, const std::vector< ROL::Ptr< BoundConstraint< Real > > > &bvec, const ROL::Ptr< Vector< Real > > &x, const ROL::Ptr< BoundConstraint< Real > > &bnd)
const ROL::Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Provides the interface to apply upper and lower bound constraints.
std::vector< ROL::Ptr< Constraint< Real > > > cvec_
std::vector< ROL::Ptr< BoundConstraint< Real > > > sbnd_
ConstraintManager(const ROL::Ptr< Constraint< Real > > &con, const ROL::Ptr< Vector< Real > > &l, const ROL::Ptr< BoundConstraint< Real > > &cbnd, const ROL::Ptr< Vector< Real > > &x, const ROL::Ptr< BoundConstraint< Real > > &bnd=ROL::nullPtr)
ConstraintManager(const std::vector< ROL::Ptr< Constraint< Real > > > &cvec, const std::vector< ROL::Ptr< Vector< Real > > > &lvec, const std::vector< ROL::Ptr< BoundConstraint< Real > > > &bvec, const ROL::Ptr< Vector< Real > > &x, const ROL::Ptr< BoundConstraint< Real > > &bnd=ROL::nullPtr)
Defines the general constraint operator interface.
std::vector< bool > isInequality_
ROL::Ptr< Vector< Real > > x_