humoto
qp_problem.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 
13 
14 namespace humoto
15 {
16  /**
17  * @brief A container for a QP Objective.
18  */
20  {
21  public:
22  enum Status
23  {
24  UNDEFINED = 0,
25  INITIALIZED = 1,
26  MODIFIED = 2
27  };
28 
30  {
31  HESSIAN_UNDEFINED = 0,
32  HESSIAN_GENERIC = 1,
33  HESSIAN_DIAGONAL = 2,
34  HESSIAN_LOWER_TRIANGULAR = 3
35  };
36 
37 
38  protected:
39  /// Hessian
40  Eigen::MatrixXd H_;
41 
42  /// Gradient vector
43  Eigen::VectorXd g_;
44 
47 
48 
49  public:
50  /**
51  * @brief Generates (general) objective containing all constraints on this level
52  *
53  * @param[in] hlevel hierarchy level
54  * @param[in] initialize_upper_triangular_part
55  */
57  const bool initialize_upper_triangular_part = true)
58  {
59  HUMOTO_ASSERT(hlevel.isEquality(), "Objective contains inequality constraints.");
60  HUMOTO_ASSERT(hlevel.getNumberOfConstraints() > 0, "Empty objective.");
61 
62 
63  hlevel.getObjective(H_, g_);
64  g_ = -g_;
65 
66 
67  if (hlevel.isSimple())
68  {
69  if (initialize_upper_triangular_part)
70  {
72  }
73 
74  hessian_type_ = HESSIAN_DIAGONAL;
75  }
76  else
77  {
78  if (initialize_upper_triangular_part)
79  {
81  hessian_type_ = HESSIAN_GENERIC;
82  }
83  else
84  {
85  hessian_type_ = HESSIAN_LOWER_TRIANGULAR;
86  }
87  }
88 
89  status_ = INITIALIZED;
90  }
91 
92 
93  /**
94  * @brief Const accessor
95  *
96  * @return objective component
97  */
98  const Eigen::MatrixXd & getHessian() const
99  {
100  return (H_);
101  }
102 
103  /**
104  * @brief Const accessor
105  *
106  * @return objective component
107  */
108  const Eigen::VectorXd & getGradient() const
109  {
110  return (g_);
111  }
112 
113 
114 
115  /**
116  * @brief Non-const accessor
117  *
118  * @return objective component
119  */
120  Eigen::MatrixXd & getHessian()
121  {
122  hessian_type_ = HESSIAN_GENERIC;
123  status_ = MODIFIED;
124  return (H_);
125  }
126 
127  /**
128  * @brief Non-const accessor
129  *
130  * @return objective component
131  */
132  Eigen::VectorXd & getGradient()
133  {
134  status_ = MODIFIED;
135  return (g_);
136  }
137 
138 
139 
140  /**
141  * @brief Status of the objective.
142  *
143  * @return status
144  */
146  {
147  return (status_);
148  }
149 
150 
151 
152  /**
153  * @brief Hessian type
154  *
155  * @return Hessian type
156  */
158  {
159  return (hessian_type_);
160  }
161 
162 
163  /**
164  * @brief Adds regularization to the Hessian.
165  *
166  * @param[in] regularization_factor
167  */
168  void regularize(const double regularization_factor)
169  {
170  status_ = MODIFIED;
171 
172  for (EigenIndex i = 0; i < H_.rows(); ++i)
173  {
174  H_(i,i) += regularization_factor;
175  }
176  }
177 
178 
179  /**
180  * @brief Log objective
181  *
182  * @param[in,out] logger logger
183  * @param[in] parent parent
184  * @param[in] name name
185  */
187  const LogEntryName &parent = LogEntryName(),
188  const std::string &name = "objective") const
189  {
190  LogEntryName subname = parent; subname.add(name);
191 
192  logger.log(LogEntryName(subname).add("H"), H_);
193  logger.log(LogEntryName(subname).add("g"), g_);
194 
195  logger.log(LogEntryName(subname).add("hessian_type"), hessian_type_);
196  logger.log(LogEntryName(subname).add("status"), status_);
197  }
198  };
199 
200 
201  /**
202  * @brief QP constraints container
203  */
205  {
206  public:
209 
210  //@{
211  /// Simple bounds on variables
212  Eigen::VectorXd lb_;
213  Eigen::VectorXd ub_;
214  //@}
215 
216 
217 
218  public:
219  /**
220  * @brief Generates (general) objective containing all constraints on this level
221  *
222  * @param[in] hlevel hierarchy level
223  * @param[in] sol_structure structure of the solution
224  */
226  const humoto::SolutionStructure & sol_structure)
227  {
228  hlevel.getGeneralConstraints(general_constraints_, sol_structure);
229 
230  hlevel.getSimpleConstraints(bounds_, sol_structure);
231  bounds_.initializeSolutionBounds(lb_, ub_);
232  }
233 
234 
235  /**
236  * @brief get container with simple bounds
237  *
238  * @return constraints container
239  */
241  {
242  return(bounds_);
243  }
244 
245  /**
246  * @brief get container with general constraints
247  *
248  * @return constraints container
249  */
251  {
252  return(general_constraints_);
253  }
254 
255  /**
256  * @brief Get LB vector: 'LB <= X'
257  *
258  * @return LB vector
259  */
260  const Eigen::VectorXd & getSolutionLowerBounds() const
261  {
262  return(lb_);
263  }
264 
265  /**
266  * @brief Get UB vector: 'X <= UB'
267  *
268  * @return UB vector
269  */
270  const Eigen::VectorXd & getSolutionUpperBounds() const
271  {
272  return(ub_);
273  }
274 
275 
276  /**
277  * @brief Log a QP problem
278  *
279  * @param[in,out] logger logger
280  * @param[in] parent parent
281  * @param[in] name name
282  */
284  const LogEntryName &parent = LogEntryName(),
285  const std::string &name = "constraints") const
286  {
287  LogEntryName subname = parent; subname.add(name);
288 
289  bounds_.log(logger, subname, "bounds");
290  general_constraints_.log(logger, subname, "general_constraints");
291 
292  logger.log(LogEntryName(subname).add("lb"), lb_);
293  logger.log(LogEntryName(subname).add("ub"), ub_);
294  }
295  };
296 
297 
298 
299  /**
300  * @brief QP constraints container
301  */
303  {
304  protected:
307 
308 
309  public:
310  /**
311  * @brief Generates (general) objective containing all constraints on this level
312  *
313  * @param[in] hlevel hierarchy level
314  * @param[in] sol_structure structure of the solution
315  */
317  const humoto::SolutionStructure & sol_structure)
318  {
319  hlevel.getEqualityConstraints(equality_constraints_, sol_structure);
320  hlevel.getInequalityConstraints(inequality_constraints_, sol_structure);
321  }
322 
323 
324  /**
325  * @brief get container with inequalities
326  *
327  * @return constraints container
328  */
330  {
331  return(inequality_constraints_);
332  }
333 
334  /**
335  * @brief get container with equalities
336  *
337  * @return constraints container
338  */
340  {
341  return(equality_constraints_);
342  }
343 
344 
345  /**
346  * @brief Log a QP problem
347  *
348  * @param[in,out] logger logger
349  * @param[in] parent parent
350  * @param[in] name name
351  */
353  const LogEntryName &parent = LogEntryName(),
354  const std::string &name = "constraints") const
355  {
356  LogEntryName subname = parent; subname.add(name);
357 
358  equality_constraints_.log(logger, subname, "equality_constraints");
359  inequality_constraints_.log(logger, subname, "inequality_constraints");
360  }
361  };
362 
363 
364  /**
365  * @brief QP constraints container
366  */
368  {
369  protected:
371 
372 
373  public:
374  /**
375  * @brief Generates (general) objective containing all constraints on this level
376  *
377  * @param[in] hlevel hierarchy level
378  * @param[in] sol_structure structure of the solution
379  */
381  const humoto::SolutionStructure & sol_structure)
382  {
383  HUMOTO_ASSERT(hlevel.isEquality(), "Inequality constraints are not supported.");
384 
385  hlevel.getEqualityConstraints(equality_constraints_, sol_structure);
386  }
387 
388 
389  /**
390  * @brief get container with equalities
391  *
392  * @return constraints container
393  */
395  {
396  return(equality_constraints_);
397  }
398 
399 
400  /**
401  * @brief Log a QP problem
402  *
403  * @param[in,out] logger logger
404  * @param[in] parent parent
405  * @param[in] name name
406  */
408  const LogEntryName &parent = LogEntryName(),
409  const std::string &name = "constraints") const
410  {
411  LogEntryName subname = parent; subname.add(name);
412 
413  equality_constraints_.log(logger, subname, "equality_constraints");
414  }
415  };
416 
417 
418  /**
419  * @brief QP problem base class
420  *
421  * @tparam t_Constraints Constraints container type.
422  */
423  template<class t_Constraints>
424  class QPProblemBase : public QPObjective, public t_Constraints
425  {
426  public:
427  /**
428  * @brief Log a QP problem
429  *
430  * @param[in,out] logger logger
431  * @param[in] parent parent
432  * @param[in] name name
433  */
435  const LogEntryName &parent = LogEntryName(),
436  const std::string &name = "qp_problem") const
437  {
438  LogEntryName subname = parent; subname.add(name);
439 
440  QPObjective::log(logger, subname);
441  t_Constraints::log(logger, subname);
442  }
443  };
444 
445 
446 
447  /**
448  * @brief A QP problem with constraints of a specific type
449  */
450  class QPProblem_ILU_ALU : public QPProblemBase<QPConstraints_ILU_ALU>
451  {
452  };
453 
454  /**
455  * @brief A QP problem with constraints of a specific type
456  */
457  class QPProblem_AB_AL : public QPProblemBase<QPConstraints_AB_AL>
458  {
459  };
460 
461  /**
462  * @brief A QP problem with constraints of a specific type
463  */
464  class QPProblem_AB : public QPProblemBase<QPConstraints_AB>
465  {
466  };
467 }
Container for simple inequality constraints.
humoto::constraints::ContainerILU bounds_
Definition: qp_problem.h:208
const humoto::constraints::ContainerAL & getInequalities() const
get container with inequalities
Definition: qp_problem.h:329
const humoto::constraints::ContainerALU & getGeneralConstraints() const
get container with general constraints
Definition: qp_problem.h:250
A container for a QP Objective.
Definition: qp_problem.h:19
#define HUMOTO_LOCAL
Definition: export_import.h:26
humoto::constraints::ContainerAL inequality_constraints_
Definition: qp_problem.h:305
HessianType hessian_type_
Definition: qp_problem.h:45
humoto::constraints::ContainerAB equality_constraints_
Definition: qp_problem.h:306
#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="constraints") const
Log a QP problem.
Definition: qp_problem.h:407
void initializeConstraints(const HierarchyLevel &hlevel, const humoto::SolutionStructure &sol_structure)
Generates (general) objective containing all constraints on this level.
Definition: qp_problem.h:225
void getGeneralConstraints(constraints::ContainerALU &constraints, const humoto::SolutionStructure &sol_structure) const
Generates objective containing all general constraints on this level.
Analog of &#39;sol_structure&#39; struct in Octave code. [determine_solution_structure.m].
Definition: solution.h:33
QP problem base class.
Definition: qp_problem.h:424
void getObjective(Eigen::MatrixXd &H, Eigen::VectorXd &g) const
Form objective of a QP.
This class represents one level of a hierarchy.
A QP problem with constraints of a specific type.
Definition: qp_problem.h:457
const humoto::constraints::ContainerILU & getSimpleBounds() const
get container with simple bounds
Definition: qp_problem.h:240
Eigen::VectorXd g_
Gradient vector.
Definition: qp_problem.h:43
Represents log entry name.
Definition: logger.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE EigenIndex
Definition: utility.h:26
const Eigen::VectorXd & getSolutionLowerBounds() const
Get LB vector: &#39;LB <= X&#39;.
Definition: qp_problem.h:260
QP constraints container.
Definition: qp_problem.h:302
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="constraints") const
Log a QP problem.
Definition: qp_problem.h:352
const Eigen::VectorXd & getGradient() const
Const accessor.
Definition: qp_problem.h:108
void EIGENTOOLS_VISIBILITY_ATTRIBUTE convertLLTtoSymmetric(Eigen::PlainObjectBase< t_Derived > &matrix)
Converts left lower triangular matrix to a symmetric matrix.
Definition: eigentools.h:312
Status getStatus() const
Status of the objective.
Definition: qp_problem.h:145
const humoto::constraints::ContainerAB & getEqualities() const
get container with equalities
Definition: qp_problem.h:339
bool isEquality() const
True if all constraints on this level are equalitites.
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
void initializeSolutionBounds(Eigen::VectorXd &lb, Eigen::VectorXd &ub) const
Generates LB and UB, such that &#39;LB <= X <= UB&#39;.
void initializeConstraints(const HierarchyLevel &hlevel, const humoto::SolutionStructure &sol_structure)
Generates (general) objective containing all constraints on this level.
Definition: qp_problem.h:380
const Eigen::VectorXd & getSolutionUpperBounds() const
Get UB vector: &#39;X <= UB&#39;.
Definition: qp_problem.h:270
Eigen::MatrixXd H_
Hessian.
Definition: qp_problem.h:40
const humoto::constraints::ContainerAB & getEqualities() const
get container with equalities
Definition: qp_problem.h:394
HessianType getHessianType() const
Hessian type.
Definition: qp_problem.h:157
void initializeObjective(const humoto::HierarchyLevel &hlevel, const bool initialize_upper_triangular_part=true)
Generates (general) objective containing all constraints on this level.
Definition: qp_problem.h:56
A QP problem with constraints of a specific type.
Definition: qp_problem.h:450
void getEqualityConstraints(constraints::ContainerAB &eq_constraints, const humoto::SolutionStructure &sol_structure) const
Generates (general) objective containing all equality constraints on this level.
void getSimpleConstraints(constraints::ContainerILU &constraints, const humoto::SolutionStructure &sol_structure) const
Generates objective containing all simple constraints on this level.
Container for general onesided inequality constraints.
The root namespace of HuMoTo.
Definition: config.h:12
std::size_t getNumberOfConstraints() const
Get total number of constraints.
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="constraints") const
Log a QP problem.
Definition: qp_problem.h:283
QP constraints container.
Definition: qp_problem.h:204
Eigen::VectorXd & getGradient()
Non-const accessor.
Definition: qp_problem.h:132
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="constraints") const
Log data.
void regularize(const double regularization_factor)
Adds regularization to the Hessian.
Definition: qp_problem.h:168
A QP problem with constraints of a specific type.
Definition: qp_problem.h:464
QP constraints container.
Definition: qp_problem.h:367
Container for general equality constraints.
void initializeConstraints(const HierarchyLevel &hlevel, const humoto::SolutionStructure &sol_structure)
Generates (general) objective containing all constraints on this level.
Definition: qp_problem.h:316
bool isSimple() const
True if all constraints on this level are simple.
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
Eigen::MatrixXd & getHessian()
Non-const accessor.
Definition: qp_problem.h:120
Eigen::VectorXd lb_
Simple bounds on variables.
Definition: qp_problem.h:212
void getInequalityConstraints(constraints::ContainerAL &ineq_constraints, const humoto::SolutionStructure &sol_structure) const
Generates (general) objective containing all equality constraints on this level.
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="qp_problem") const
Log a QP problem.
Definition: qp_problem.h:434
humoto::constraints::ContainerAB equality_constraints_
Definition: qp_problem.h:370
humoto::constraints::ContainerALU general_constraints_
Definition: qp_problem.h:207
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="objective") const
Log objective.
Definition: qp_problem.h:186
const Eigen::MatrixXd & getHessian() const
Const accessor.
Definition: qp_problem.h:98
Container for general inequality constraints.