humoto
active_set.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
5  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
6 
7  @brief
8 */
9 
10 #pragma once
11 
12 namespace humoto
13 {
14  /**
15  * @brief Type of constraint activation
16  */
18  {
19  public:
20  enum Type
21  {
22  /// Udefined
23  UNDEFINED = 0,
24 
25  /// Constraint is inactive
26  INACTIVE = 1,
27 
28  /// Lower bound is activated
30 
31  /// Upper bound is activated
33 
34  /// Constraint is an equality (always active)
36  };
37  };
38 
39 
40  /**
41  * @brief Active set corresponding to #Constraints class.
42  */
44  {
45  private:
46  std::vector<humoto::ConstraintActivationType::Type> data_;
47 
48  public:
49  /**
50  * @brief Access activation flag of a constraint (const)
51  *
52  * @param[in] constraint_index
53  *
54  * @return reference to the activation flag
55  */
56  const humoto::ConstraintActivationType::Type & operator[] (const std::size_t constraint_index) const
57  {
58  HUMOTO_ASSERT( ( constraint_index < data_.size() ),
59  "Active set index is out of bounds.");
60  return (data_[constraint_index]);
61  }
62 
63 
64  /**
65  * @brief Access activation flag of a constraint
66  *
67  * @param[in] constraint_index
68  *
69  * @return reference to the activation flag
70  */
71  humoto::ConstraintActivationType::Type & operator[] (const std::size_t constraint_index)
72  {
73  HUMOTO_ASSERT( ( constraint_index < data_.size() ),
74  "Active set index is out of bounds.");
75  return (data_[constraint_index]);
76  }
77 
78 
79  /**
80  * @brief Size of the active set.
81  *
82  * @return Size of the active set.
83  */
84  std::size_t size() const
85  {
86  return(data_.size());
87  }
88 
89 
90  /**
91  * @brief Reset active set.
92  */
93  void reset()
94  {
95  data_.clear();
96  }
97 
98 
99  /**
100  * @brief Set type of all constraints
101  *
102  * @param[in] type type of the constraints
103  */
104  void set( const ConstraintActivationType::Type type)
105  {
106  set(Location(0, size()), type);
107  }
108 
109 
110 
111  /**
112  * @brief Set type of a set of constraints
113  *
114  * @param[in] location
115  * @param[in] type
116  */
117  void set( const Location &location,
119  {
120  HUMOTO_ASSERT(location.checkLength( size() ), "Incorrect location in an active set.");
121  std::fill( data_.begin() + location.front(),
122  data_.begin() + location.end(),
123  type);
124  }
125 
126 
127  /**
128  * @brief Set type of a set of constraints
129  *
130  * @param[in] location location in this active set
131  * @param[in] other_active_set other active set
132  */
133  void copyTo(const Location &location,
134  const ActiveSetConstraints &other_active_set)
135  {
136  HUMOTO_ASSERT(location.checkLength( size() ), "Incorrect location in an active set.");
137 
138  std::copy( other_active_set.data_.begin(),
139  other_active_set.data_.end(),
140  data_.begin() + location.front());
141  }
142 
143 
144  /**
145  * @brief Set type of a set of constraints
146  *
147  * @param[in] other_active_set other active set
148  * @param[in] location location in other active set
149  */
150  void copyFrom( const ActiveSetConstraints &other_active_set,
151  const Location &location)
152  {
153  HUMOTO_ASSERT(location.checkLength( other_active_set.size() ), "Incorrect location in an active set.");
154  HUMOTO_ASSERT(location.length_ == size(), "Incorrect location in an active set.");
155 
156  std::copy( other_active_set.data_.begin() + location.front(),
157  other_active_set.data_.begin() + location.end(),
158  data_.begin());
159  }
160 
161 
162  /**
163  * @brief Initialize active set
164  *
165  * @param[in] number_of_constraints number of constraints for each level
166  */
167  void initialize( const std::size_t number_of_constraints)
168  {
169  reset();
170  data_.resize(number_of_constraints);
171  }
172 
173 
174  /**
175  * @brief Initialize active set
176  *
177  * @param[in] other_active_set other active set
178  * @param[in] location location in other active set
179  */
180  void initialize(const ActiveSetConstraints &other_active_set,
181  const Location &location)
182  {
183  HUMOTO_ASSERT(location.checkLength( other_active_set.size() ), "Internal error.");
184 
185  if (size() != location.length_)
186  {
187  reset();
188  data_.resize(location.length_);
189  }
190 
191  copyFrom(other_active_set, location);
192  }
193 
194 
195  /**
196  * @brief Initialize active set
197  *
198  * @param[in] number_of_constraints number of constraints for each level
199  * @param[in] type type of the constraints
200  */
201  void initialize(const std::size_t number_of_constraints,
203  {
204  reset();
205  data_.resize(number_of_constraints, type);
206  }
207 
208 
209 
210  /**
211  * @brief Shift active set forward while preserving the length,
212  * the trailing elements are initialized with the given type.
213  *
214  * @param[in] shift_size shift size
215  * @param[in] type type of the trailing elements
216  */
217  void shift( const std::size_t shift_size,
219  {
220  HUMOTO_ASSERT( shift_size < size(),
221  "Shift size exceeded size of the active set.");
222 
223  std::rotate(data_.begin(),
224  data_.begin() + shift_size,
225  data_.end());
226 
227  std::fill( data_.begin() + (size() - shift_size),
228  data_.end(),
229  type);
230  }
231 
232 
233  /**
234  * @brief Returns number of active inequality constraints.
235  *
236  * @return number of active inequality constraints.
237  */
238  std::size_t countActiveInequalities() const
239  {
240  std::size_t num_activated_inequalities = 0;
241 
242  for (std::size_t i = 0; i < data_.size(); ++i)
243  {
244  switch (data_[i])
245  {
248  ++num_activated_inequalities;
249  break;
250  default:
251  break;
252  }
253  }
254 
255  return(num_activated_inequalities);
256  }
257 
258 
259  /**
260  * @brief Log an active set
261  *
262  * @param[in,out] logger logger
263  * @param[in] parent parent
264  * @param[in] name name
265  */
267  const LogEntryName & parent = LogEntryName(),
268  const std::string & name = "") const
269  {
270  logger.log(LogEntryName(parent).add(name), data_);
271  }
272  };
273 
274 
275  /**
276  * @brief Active set corresponding to a hierarchy of #Constraints.
277  */
279  {
280  private:
281  std::vector< ActiveSetConstraints > data_;
282 
283 
284  public:
285  /**
286  * @brief Access active set of a given level (const)
287  *
288  * @param[in] level_index index of the level
289  *
290  * @return reference to the active set.
291  */
292  const humoto::ActiveSetConstraints & operator[] (const std::size_t level_index) const
293  {
294  HUMOTO_ASSERT( level_index < size(), "Internal error.");
295  return (data_[level_index]);
296  }
297 
298 
299  /**
300  * @brief Access active set of a given level
301  *
302  * @param[in] level_index index of the level
303  *
304  * @return reference to the active set.
305  */
306  humoto::ActiveSetConstraints & operator[] (const std::size_t level_index)
307  {
308  HUMOTO_ASSERT( level_index < size(), "Internal error.");
309  return (data_[level_index]);
310  }
311 
312 
313  /**
314  * @brief Set type of all constraints on the given level
315  *
316  * @param[in] level_index index of the level
317  * @param[in] type type of the constraints
318  */
319  void set( const std::size_t level_index,
321  {
322  HUMOTO_ASSERT( level_index < size(), "Internal error.");
323  data_[level_index].set(type);
324  }
325 
326 
327  /**
328  * @brief Size of the active set.
329  *
330  * @return Size of the active set.
331  */
332  std::size_t size() const
333  {
334  return(data_.size());
335  }
336 
337 
338  /**
339  * @brief Reset active set.
340  */
341  void reset()
342  {
343  data_.clear();
344  }
345 
346 
347  /**
348  * @brief Initialize active set
349  *
350  * @param[in] number_of_levels number of levels
351  * @param[in] number_of_constraints number of constraints for each level
352  */
353  void initialize(const std::size_t number_of_levels,
354  const std::vector< std::size_t > &number_of_constraints)
355  {
356  reset();
357  data_.resize(number_of_levels);
358 
359  for (std::size_t i = 0; i < number_of_levels; ++i)
360  {
361  data_[i].initialize(number_of_constraints[i]);
362  }
363  }
364 
365 
366  /**
367  * @brief Log an active set
368  *
369  * @param[in,out] logger logger
370  * @param[in] parent parent
371  * @param[in] name name
372  */
374  const LogEntryName & parent = LogEntryName(),
375  const std::string & name = "active_set") const
376  {
377  LogEntryName subname = parent;
378  subname.add(name);
379  for (std::size_t i = 0; i < data_.size(); ++i)
380  {
381  data_[i].log(logger, LogEntryName(subname).add(i), "");
382  }
383  }
384  };
385 }
bool checkLength(const std::size_t max_length) const
Check length of location.
Definition: utility.h:208
std::size_t front() const
Offset of the first element.
Definition: utility.h:220
#define HUMOTO_LOCAL
Definition: export_import.h:26
Constraint is an equality (always active)
Definition: active_set.h:35
void initialize(const ActiveSetConstraints &other_active_set, const Location &location)
Initialize active set.
Definition: active_set.h:180
void initialize(const std::size_t number_of_constraints, const ConstraintActivationType::Type type)
Initialize active set.
Definition: active_set.h:201
#define HUMOTO_GLOBAL_LOGGER_IF_DEFINED
Definition: logger.h:997
#define HUMOTO_ASSERT(condition, message)
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="") const
Log an active set.
Definition: active_set.h:266
std::size_t size() const
Size of the active set.
Definition: active_set.h:84
Active set corresponding to Constraints class.
Definition: active_set.h:43
void copyFrom(const ActiveSetConstraints &other_active_set, const Location &location)
Set type of a set of constraints.
Definition: active_set.h:150
Represents log entry name.
Definition: logger.h:169
Active set corresponding to a hierarchy of Constraints.
Definition: active_set.h:278
std::size_t length_
Definition: utility.h:150
void initialize(const std::size_t number_of_levels, const std::vector< std::size_t > &number_of_constraints)
Initialize active set.
Definition: active_set.h:353
void reset()
Reset active set.
Definition: active_set.h:341
void initialize(const std::size_t number_of_constraints)
Initialize active set.
Definition: active_set.h:167
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
std::vector< ActiveSetConstraints > data_
Definition: active_set.h:281
std::vector< humoto::ConstraintActivationType::Type > data_
Definition: active_set.h:46
The root namespace of HuMoTo.
Definition: config.h:12
Type of constraint activation.
Definition: active_set.h:17
void copyTo(const Location &location, const ActiveSetConstraints &other_active_set)
Set type of a set of constraints.
Definition: active_set.h:133
std::size_t size() const
Size of the active set.
Definition: active_set.h:332
std::size_t countActiveInequalities() const
Returns number of active inequality constraints.
Definition: active_set.h:238
std::size_t end() const
Offset of the element following the last element.
Definition: utility.h:231
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="active_set") const
Log an active set.
Definition: active_set.h:373
Location of a data chunk (offset + length).
Definition: utility.h:146
void reset()
Reset active set.
Definition: active_set.h:93
void shift(const std::size_t shift_size, const ConstraintActivationType::Type type)
Shift active set forward while preserving the length, the trailing elements are initialized with the ...
Definition: active_set.h:217