ocra-recipes
Doxygen documentation for the ocra-recipes repository
ConstraintSet.h
Go to the documentation of this file.
1 
15 #ifndef _OCRABASE_CONSTRAINT_SET_H_
16 #define _OCRABASE_CONSTRAINT_SET_H_
17 
18 #ifdef WIN32
19 # pragma once
20 #endif
21 
22 // ocra includes
23 #include "ocra/optim/Constraint.h"
24 
25 // std includes
26 #include <vector>
27 #include <algorithm>
28 
34 namespace ocra
35 {
43  template<class T>
44  class ConstraintSet: protected ConstraintSet<typename T::functionType_t>
45  {
46  // ------------------------ constructors ------------------------------------
47  private:
49  ConstraintSet& operator= (const ConstraintSet&);
50  protected:
51  public:
52  ConstraintSet();
53 
54  // ------------------------ public interface --------------------------------
55  public:
60  ConstraintSet<T>& add(Constraint<T>* constraint);
61  ConstraintSet<T>& add(ConstraintSet<T>& constraintSet);
62  ConstraintSet<T>& remove(Constraint<T>* constraint);
63  ConstraintSet<T>& remove(ConstraintSet<T>& constraintSet);
64 
65  size_t getNumberOfEqualityConstraints() const;
66  size_t getNumberOfInequalityConstraints() const;
67  size_t getEqualityDimension() const;
68  size_t getInequalityDimension() const;
70 
71  private:
72  typedef boost::is_base_of<Function, T> T_must_derived_from_Function;
73  BOOST_STATIC_ASSERT(T_must_derived_from_Function ::value);
74  };
75 
76 
77 
78  template<>
80  {
81  // ------------------------ constructors ------------------------------------
82  private:
84  ConstraintSet& operator= (const ConstraintSet&);
85  protected:
86  public:
87  ConstraintSet();
88 
89  // ------------------------ public interface --------------------------------
90  public:
95  ConstraintSet<Function>& remove(GenericConstraint* constraint);
96  ConstraintSet<Function>& remove(ConstraintSet<Function>& constraintSet);
97 
98  size_t getNumberOfEqualityConstraints() const;
99  size_t getNumberOfInequalityConstraints() const;
100  size_t getEqualityDimension() const;
101  size_t getInequalityDimension() const;
102 
103  // ------------------------ private members ---------------------------------
104  private:
105  size_t _dime; //< sum of the dimension of each equality constraint
106  size_t _dimi; //< sum of the dimension of each inequality constraint
107  std::vector<GenericConstraint*> _equalityConstraints;
108  std::vector<GenericConstraint*> _inequalityConstraints;
109  };
110 
111 
112 
113  template<class T>
115  :ConstraintSet<typename T::functionType_t>()
116  {
117  }
118 
119 
121  :_dime(0), _dimi(0)
122  {
123  }
124 
125 
126  template<class T>
128  {
130  }
131 
132 
134  {
135  return _equalityConstraints[i];
136  }
137 
138 
139  template<class T>
141  {
143  }
144 
145 
147  {
148  return _inequalityConstraints[i];
149  }
150 
151 
152  template<class T>
154  {
156  return *this;
157  }
158 
159 
161  {
162  if (constraint->isEquality())
163  {
164  _dime += constraint->getDimension();
165  _equalityConstraints.push_back(constraint);
166  }
167  else
168  {
169  _dimi += constraint->getDimension();
170  _inequalityConstraints.push_back(constraint);
171  }
172  return *this;
173  }
174 
175 
176  template<class T>
178  {
180  return *this;
181  }
182 
183 
185  {
186  for (size_t i=0; i<constraintSet._equalityConstraints.size(); ++i)
187  add(constraintSet._equalityConstraints[i]);
188  for (size_t i=0; i<constraintSet._inequalityConstraints.size(); ++i)
189  add(constraintSet._inequalityConstraints[i]);
190  return *this;
191  }
192 
193 
194  template<class T>
196  {
198  return *this;
199  }
200 
202  {
203  std::vector<GenericConstraint*>::iterator it;
204  if (constraint->isEquality())
205  {
206  it = std::find(_equalityConstraints.begin(), _equalityConstraints.end(), constraint);
207  if (it != _equalityConstraints.end())
208  {
209  _dime -= constraint->getDimension();
210  _equalityConstraints.erase(it);
211  }
212  }
213  else
214  {
215  it = std::find(_inequalityConstraints.begin(), _inequalityConstraints.end(), constraint);
216  if (it != _inequalityConstraints.end())
217  {
218  _dimi -= constraint->getDimension();
219  _inequalityConstraints.erase(it);
220  }
221  }
222  return *this;
223  }
224 
225  template<class T>
227  {
229  return *this;
230  }
231 
233  {
234  for (size_t i=0; i<constraintSet._equalityConstraints.size(); ++i)
235  remove(constraintSet._equalityConstraints[i]);
236  for (size_t i=0; i<constraintSet._inequalityConstraints.size(); ++i)
237  remove(constraintSet._inequalityConstraints[i]);
238  return *this;
239  }
240 
241 
242  template<class T>
244  {
246  }
247 
248 
250  {
251  return _equalityConstraints.size();
252  }
253 
254 
255  template<class T>
257  {
259  }
260 
261 
263  {
264  return _inequalityConstraints.size();
265  }
266 
267 
268  template<class T>
270  {
272  }
273 
274 
276  {
277  return _dime;
278  }
279 
280 
281  template<class T>
283  {
285  }
286 
287 
289  {
290  return _dimi;
291  }
292 
293 }
294 
295 #endif //_OCRABASE_CONSTRAINT_SET_H_
296 
297 // cmake:sourcegroup=Constraint
298 
Constraint< T > * getIthInequalityConstraint(size_t i)
ConstraintSet class.
Definition: ConstraintSet.h:44
ConstraintSet< T > & add(Constraint< T > *constraint)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Function class.
Definition: Function.h:77
size_t getEqualityDimension() const
Constraint class.
Definition: Constraint.h:100
size_t getNumberOfEqualityConstraints() const
Declaration file of the Constraint class.
Constraint< T > * getIthEqualityConstraint(size_t i)
size_t getNumberOfInequalityConstraints() const
size_t getInequalityDimension() const
ConstraintSet< T > & remove(Constraint< T > *constraint)