humoto
violations.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 Violations corresponding to #Constraints class.
16  */
18  {
19  private:
20  Eigen::VectorXd data_;
21 
22  Eigen::VectorXd lower_;
23  Eigen::VectorXd upper_;
24 
25 
26  public:
27  /**
28  * @brief Access violation of a constraint (const)
29  *
30  * @param[in] constraint_index
31  *
32  * @return reference to the violation
33  */
34  double operator[] (const std::size_t constraint_index) const
35  {
36  HUMOTO_ASSERT( ( constraint_index < static_cast<std::size_t>(data_.size()) ),
37  "Violation index is out of bounds.");
38  return (data_[constraint_index]);
39  }
40 
41 
42  /**
43  * @brief Access violation of a lower/upper bounds
44  *
45  * @param[in] constraint_index
46  *
47  * @return reference to the violation
48  */
49  double getLower (const std::size_t constraint_index) const
50  {
51  HUMOTO_ASSERT( ( constraint_index < static_cast<std::size_t>(lower_.size()) ),
52  "Violation index is out of bounds.");
53  return (lower_[constraint_index]);
54  }
55 
56 
57  /// @copydoc humoto::ViolationsConstraints::getLower
58  double getUpper (const std::size_t constraint_index) const
59  {
60  HUMOTO_ASSERT( ( constraint_index < static_cast<std::size_t>(upper_.size()) ),
61  "Violation index is out of bounds.");
62  return (upper_[constraint_index]);
63  }
64 
65 
66  /**
67  * @brief Set violation of a constraint
68  *
69  * @param[in] constraint_index
70  * @param[in] lower_bound_diff A*x - lb (0.0 if lb is undefined)
71  * @param[in] upper_bound_diff A*x - ub (0.0 if ub is undefined)
72  */
73  void set ( const std::size_t constraint_index,
74  const double lower_bound_diff = 0.0,
75  const double upper_bound_diff = 0.0)
76  {
77  HUMOTO_ASSERT( ( constraint_index < static_cast<std::size_t>(data_.rows()) ),
78  "Violation index is out of bounds.");
79 
80  lower_[constraint_index] = lower_bound_diff;
81  upper_[constraint_index] = upper_bound_diff;
82 
83  if (lower_bound_diff >= 0)
84  {
85  if (upper_bound_diff <= 0)
86  {
87  data_[constraint_index] = 0.0;
88  }
89  else
90  {
91  data_[constraint_index] = upper_bound_diff;
92  }
93  }
94  else
95  {
96  data_[constraint_index] = lower_bound_diff;
97  }
98  }
99 
100 
101  /**
102  * @brief Size of the the vector of violations.
103  *
104  * @return Size of the the vector of violations.
105  */
106  std::size_t size() const
107  {
108  return(data_.rows());
109  }
110 
111 
112 
113  /**
114  * @brief Set all violations to zero
115  */
116  void setZero()
117  {
118  data_.setZero();
119  lower_.setZero();
120  upper_.setZero();
121  }
122 
123 
124 
125  /**
126  * @brief Set violations of a set of constraints to zero
127  *
128  * @param[in] location
129  */
130  void setZero( const Location &location)
131  {
132  HUMOTO_ASSERT(location.checkLength( size() ), "Incorrect location in a vector of violations.");
133 
134  data_.segment(location.offset_, location.length_).setZero();
135  lower_.segment(location.offset_, location.length_).setZero();
136  upper_.segment(location.offset_, location.length_).setZero();
137  }
138 
139 
140  /**
141  * @brief Set violations of a set of constraints
142  *
143  * @param[in] location location in this vector of violations
144  * @param[in] other_violations other vector of violations
145  */
146  void copyTo(const Location &location,
147  const ViolationsConstraints &other_violations)
148  {
149  HUMOTO_ASSERT(location.checkLength( size() ), "Incorrect location in a vector of violations.");
150 
151  data_.segment(location.offset_, location.length_) = other_violations.data_;
152  lower_.segment(location.offset_, location.length_) = other_violations.lower_;
153  upper_.segment(location.offset_, location.length_) = other_violations.upper_;
154  }
155 
156 
157  /**
158  * @brief Set violations of a set of constraints
159  *
160  * @param[in] other_violations other vector of violations
161  * @param[in] location location in other vector of violations
162  */
163  void copyFrom( const ViolationsConstraints &other_violations,
164  const Location &location)
165  {
166  HUMOTO_ASSERT(location.checkLength( other_violations.size() ), "Incorrect location in a vector of violations.");
167  HUMOTO_ASSERT(location.length_ == size(), "Incorrect location in a vector of violations.");
168 
169  data_ = other_violations.data_.segment(location.offset_, location.length_);
170  lower_ = other_violations.lower_.segment(location.offset_, location.length_);
171  upper_ = other_violations.upper_.segment(location.offset_, location.length_);
172  }
173 
174 
175  /**
176  * @brief Initialize vector of violations
177  *
178  * @param[in] other_violations other vector of violations
179  * @param[in] location location in other vector of violations
180  */
181  void initialize(const ViolationsConstraints &other_violations,
182  const Location &location)
183  {
184  HUMOTO_ASSERT(location.checkLength( other_violations.size() ), "Internal error.");
185 
186  if (size() != location.length_)
187  {
188  data_.resize(location.length_);
189  lower_.resize(location.length_);
190  upper_.resize(location.length_);
191  }
192 
193  copyFrom(other_violations, location);
194  }
195 
196 
197  /**
198  * @brief Initialize vector of violations
199  *
200  * @param[in] number_of_constraints number of constraints for each level
201  */
202  void initialize(const std::size_t number_of_constraints)
203  {
204  data_.resize(number_of_constraints);
205  lower_.resize(number_of_constraints);
206  upper_.resize(number_of_constraints);
207  }
208 
209 
210 
211  /**
212  * @brief Log violations
213  *
214  * @param[in,out] logger logger
215  * @param[in] parent parent
216  * @param[in] name
217  */
219  const LogEntryName & parent = LogEntryName(),
220  const std::string & name = "") const
221  {
222  LogEntryName subname = parent;
223  subname.add(name);
224 
225  logger.log(LogEntryName(subname).add("violations"), data_);
226  logger.log(LogEntryName(subname).add("lower_bound_diff"), lower_);
227  logger.log(LogEntryName(subname).add("upper_bound_diff"), upper_);
228  }
229  };
230 
231 
232  /**
233  * @brief Violations corresponding to a hierarchy of #Constraints.
234  */
236  {
237  private:
238  std::vector< ViolationsConstraints > data_;
239 
240 
241  public:
242  /**
243  * @brief Access violations of a given level (const)
244  *
245  * @param[in] level_index index of the level
246  *
247  * @return reference to violations.
248  */
249  const ViolationsConstraints & operator[] (const std::size_t level_index) const
250  {
251  HUMOTO_ASSERT( level_index < size(), "Internal error.");
252  return (data_[level_index]);
253  }
254 
255 
256  /**
257  * @brief Access violations of a given level
258  *
259  * @param[in] level_index index of the level
260  *
261  * @return reference to violations.
262  */
263  ViolationsConstraints & operator[] (const std::size_t level_index)
264  {
265  HUMOTO_ASSERT( level_index < size(), "Internal error.");
266  return (data_[level_index]);
267  }
268 
269 
270  /**
271  * @brief Set violations of all constraints on the given level
272  *
273  * @param[in] level_index index of the level
274  */
275  void setZero(const std::size_t level_index)
276  {
277  HUMOTO_ASSERT( level_index < size(), "Internal error.");
278  data_[level_index].setZero();
279  }
280 
281 
282  /**
283  * @brief Size
284  *
285  * @return Size
286  */
287  std::size_t size() const
288  {
289  return(data_.size());
290  }
291 
292 
293  /**
294  * @brief Reset
295  */
296  void reset()
297  {
298  data_.clear();
299  }
300 
301 
302  /**
303  * @brief Initialize violations
304  *
305  * @param[in] number_of_levels number of levels
306  * @param[in] number_of_constraints number of constraints for each level
307  */
308  void initialize(const std::size_t number_of_levels,
309  const std::vector< std::size_t > &number_of_constraints)
310  {
311  reset();
312  data_.resize(number_of_levels);
313 
314  for (std::size_t i = 0; i < number_of_levels; ++i)
315  {
316  data_[i].initialize(number_of_constraints[i]);
317  }
318  }
319 
320 
321  /**
322  * @brief Log violations
323  *
324  * @param[in,out] logger logger
325  * @param[in] parent parent
326  * @param[in] name name
327  */
329  const LogEntryName & parent = LogEntryName(),
330  const std::string & name = "violations") const
331  {
332  LogEntryName subname = parent; subname.add(name);
333  for (std::size_t i = 0; i < data_.size(); ++i)
334  {
335  data_[i].log(logger, LogEntryName(subname).add(i), "");
336  }
337  }
338  };
339 }
double getUpper(const std::size_t constraint_index) const
Access violation of a lower/upper bounds.
Definition: violations.h:58
bool checkLength(const std::size_t max_length) const
Check length of location.
Definition: utility.h:208
std::vector< ViolationsConstraints > data_
Definition: violations.h:238
Violations corresponding to Constraints class.
Definition: violations.h:17
#define HUMOTO_LOCAL
Definition: export_import.h:26
std::size_t size() const
Size.
Definition: violations.h:287
#define HUMOTO_GLOBAL_LOGGER_IF_DEFINED
Definition: logger.h:997
#define HUMOTO_ASSERT(condition, message)
void copyFrom(const ViolationsConstraints &other_violations, const Location &location)
Set violations of a set of constraints.
Definition: violations.h:163
void initialize(const std::size_t number_of_levels, const std::vector< std::size_t > &number_of_constraints)
Initialize violations.
Definition: violations.h:308
Represents log entry name.
Definition: logger.h:169
void initialize(const ViolationsConstraints &other_violations, const Location &location)
Initialize vector of violations.
Definition: violations.h:181
std::size_t length_
Definition: utility.h:150
void copyTo(const Location &location, const ViolationsConstraints &other_violations)
Set violations of a set of constraints.
Definition: violations.h:146
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="") const
Log violations.
Definition: violations.h:218
void setZero(const std::size_t level_index)
Set violations of all constraints on the given level.
Definition: violations.h:275
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
void initialize(const std::size_t number_of_constraints)
Initialize vector of violations.
Definition: violations.h:202
The root namespace of HuMoTo.
Definition: config.h:12
void reset()
Reset.
Definition: violations.h:296
void setZero(const Location &location)
Set violations of a set of constraints to zero.
Definition: violations.h:130
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="violations") const
Log violations.
Definition: violations.h:328
Violations corresponding to a hierarchy of Constraints.
Definition: violations.h:235
std::size_t size() const
Size of the the vector of violations.
Definition: violations.h:106
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
void setZero()
Set all violations to zero.
Definition: violations.h:116
Location of a data chunk (offset + length).
Definition: utility.h:146
std::size_t offset_
Definition: utility.h:149
double getLower(const std::size_t constraint_index) const
Access violation of a lower/upper bounds.
Definition: violations.h:49