humoto
hierarchy.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @author Jan Michalczyk
5  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @brief
9 */
10 
11 #pragma once
12 
13 namespace humoto
14 {
15  /**
16  * @brief An optimization problem [initialize_stack.m, simulation_loop.m]
17  *
18  * A stack of tasks / hierarchy, convertible to a QP in some cases.
19  */
21  {
22  protected:
23  std::size_t number_of_levels_;
24 
25  std::vector< humoto::HierarchyLevel > hierarchy_;
26  std::vector< std::size_t > number_of_constraints_;
27 
28 
29 
30  protected:
31  /**
32  * @brief Form hierarchy.
33  *
34  * @param[out] solution initialized solution
35  * @param[in] model model of the system
36  * @param[in] control_problem control problem associated with the model
37  * @param[in] is_active_set_guessing_enabled
38  */
39  void formHierarchy( humoto::Solution & solution,
40  const humoto::Model & model,
41  const humoto::ControlProblem & control_problem,
42  const bool is_active_set_guessing_enabled)
43  {
44  control_problem.initSolutionStructure(solution);
45 
46 
47  // loop over levels
48  HUMOTO_ASSERT(getNumberOfLevels() > 0, "Empty hierarchy.");
49  for (std::size_t i = 0; i < getNumberOfLevels(); ++i)
50  {
51  hierarchy_[i].form(solution, model, control_problem, is_active_set_guessing_enabled);
52  number_of_constraints_[i] = hierarchy_[i].getNumberOfConstraints();
53  }
54  }
55 
56 
57  /**
58  * @brief Form active set guess
59  *
60  * @param[out] active_set active set guess
61  */
62  void guessActiveSet(ActiveSet &active_set) const
63  {
64  active_set.initialize(number_of_levels_, number_of_constraints_);
65 
66  for (std::size_t i = 0; i < getNumberOfLevels(); ++i)
67  {
68  hierarchy_[i].guessActiveSet(active_set[i]);
69  }
70  }
71 
72 
73  public:
74  /**
75  * @brief Default constructor
76  */
78  {
79  reset(0);
80  }
81 
82 
83  /**
84  * @brief Add task to the optimization problem.
85  *
86  * @param[in,out] task_pointer pointer to a task
87  * @param[in] level_index level in the hierarchy
88  *
89  * @todo It might be a good idea to check for collisions of task ids.
90  */
91  void pushTask( TaskSharedPointer task_pointer,
92  const std::size_t level_index)
93  {
94  HUMOTO_ASSERT(level_index < number_of_levels_, "Wrong hierarchy level number.");
95  hierarchy_[level_index].pushTask(task_pointer);
96  }
97 
98 
99  /**
100  * @brief Form hierarchy.
101  *
102  * @param[out] solution initialized solution
103  * @param[in] model model of the system
104  * @param[in] control_problem control problem associated with the model
105  */
106  void form( humoto::Solution &solution,
107  const humoto::Model & model,
108  const humoto::ControlProblem & control_problem)
109  {
110  formHierarchy(solution, model, control_problem, false);
111  }
112 
113 
114  /**
115  * @brief Form hierarchy.
116  *
117  * @param[out] solution initialized solution
118  * @param[out] active_set_guess active set guess
119  * @param[in] model model of the system
120  * @param[in] control_problem control problem associated with the model
121  */
122  void form( humoto::Solution & solution,
123  humoto::ActiveSet & active_set_guess,
124  const humoto::Model & model,
125  const humoto::ControlProblem & control_problem)
126  {
127  formHierarchy(solution, model, control_problem, true);
128  guessActiveSet(active_set_guess);
129  }
130 
131 
132  /**
133  * @brief Form hierarchy.
134  *
135  * @param[out] solution initialized solution
136  * @param[out] solution_guess guess of the solution
137  * @param[out] active_set_guess active set guess
138  * @param[in] model model of the system
139  * @param[in] control_problem control problem associated with the model
140  * @param[in] old_solution old solution
141  */
142  void form( humoto::Solution & solution,
143  humoto::Solution & solution_guess,
144  humoto::ActiveSet & active_set_guess,
145  const humoto::Model & model,
146  const humoto::ControlProblem & control_problem,
147  const humoto::Solution & old_solution)
148  {
149  formHierarchy(solution, model, control_problem, true);
150  control_problem.guessSolution(solution_guess, old_solution);
151  guessActiveSet(active_set_guess);
152  }
153 
154 
155  /**
156  * @brief Form hierarchy.
157  *
158  * @param[out] solution initialized solution
159  * @param[out] solution_guess guess of the solution
160  * @param[in] model model of the system
161  * @param[in] control_problem control problem associated with the model
162  * @param[in] old_solution old solution
163  */
164  void form( humoto::Solution & solution,
165  humoto::Solution & solution_guess,
166  const humoto::Model & model,
167  const humoto::ControlProblem & control_problem,
168  const humoto::Solution & old_solution)
169  {
170  formHierarchy(solution, model, control_problem, false);
171  control_problem.guessSolution(solution_guess, old_solution);
172  }
173 
174 
175  /**
176  * @brief Process actual active set: extract active sets of
177  * individual tasks.
178  *
179  * @param[in] active_set
180  */
181  void processActiveSet(const humoto::ActiveSet & active_set)
182  {
183  HUMOTO_ASSERT( (active_set.size() == getNumberOfLevels()),
184  "Wrong number of levels in the active set.");
185 
186  for (std::size_t i = 0; i < getNumberOfLevels(); ++i)
187  {
188  hierarchy_[i].processActiveSet(active_set[i]);
189  }
190  }
191 
192 
193 
194  /**
195  * @brief Determine active set based on the solution. This active
196  * set may differ from the active set returned by the solver due to
197  * tolerances.
198  *
199  * @param[out] active_set active set
200  * @param[in] solution solution
201  */
203  const humoto::Solution & solution) const
204  {
205  active_set.initialize(number_of_levels_, number_of_constraints_);
206 
207  for (std::size_t i = 0; i < getNumberOfLevels(); ++i)
208  {
209  hierarchy_[i].determineActiveSet(active_set[i], solution);
210  }
211  }
212 
213 
214 
215  /**
216  * @brief Compute violations based on the solution.
217  *
218  * @param[out] violations violations
219  * @param[in] solution solution
220  */
222  const humoto::Solution & solution) const
223  {
224  violations.initialize(number_of_levels_, number_of_constraints_);
225 
226  for (std::size_t i = 0; i < getNumberOfLevels(); ++i)
227  {
228  hierarchy_[i].computeViolations(violations[i], solution);
229  }
230  }
231 
232 
233  /**
234  * @brief Reset the optimization problem
235  *
236  * @param[in] number_of_levels new number of levels
237  */
238  void reset( const std::size_t number_of_levels)
239  {
240  number_of_levels_ = number_of_levels;
241 
242  hierarchy_.clear();
243  hierarchy_.resize(number_of_levels_);
244 
245  number_of_constraints_.resize(number_of_levels_);
246  }
247 
248 
249  /**
250  * @brief Access hierarchy level (const)
251  *
252  * @param[in] level_index index of the level
253  *
254  * @return reference to a hierarchy level.
255  */
256  const humoto::HierarchyLevel & operator[] (const std::size_t level_index) const
257  {
258  return (hierarchy_[level_index]);
259  }
260 
261 
262  /**
263  * @brief Access hierarchy level
264  *
265  * @param[in] level_index index of the level
266  *
267  * @return reference to a hierarchy level.
268  */
269  humoto::HierarchyLevel & operator[] (const std::size_t level_index)
270  {
271  return (hierarchy_[level_index]);
272  }
273 
274 
275  /**
276  * @brief Returns number of levels in the hierarchy.
277  *
278  * @return number of levels in the hierarchy.
279  */
280  std::size_t getNumberOfLevels() const
281  {
282  return (number_of_levels_);
283  }
284 
285 
286  /**
287  * @brief Log hierarchy as a set of tasks
288  *
289  * @param[in,out] logger logger
290  * @param[in] parent parent
291  * @param[in] name name
292  */
294  const LogEntryName & parent = LogEntryName(),
295  const std::string & name = "") const
296  {
297  LogEntryName subname = parent;
298  subname.add("tasks");
299 
300  for (std::size_t i = 0; i < getNumberOfLevels(); ++i)
301  {
302  hierarchy_[i].log(logger, LogEntryName(subname).add(i), "");
303  }
304  }
305 
306 
307  /**
308  * @brief Initialize active set of the hierarchy.
309  *
310  * @param[in,out] active_set active set.
311  */
312  void initializeActiveSet(humoto::ActiveSet &active_set) const
313  {
314  active_set.initialize(number_of_levels_, number_of_constraints_);
315  }
316 
317 
318 
319  /**
320  * @brief Form a QP problem.
321  *
322  * @tparam t_QPConstraints QP constraints type
323  *
324  * @param[in] qp_problem QP problem
325  * @param[in] sol_structure solution structure
326  * @param[in] initialize_upper_triangular_part of the Hessian
327  */
328  template<class t_QPConstraints>
330  const humoto::SolutionStructure & sol_structure,
331  const bool initialize_upper_triangular_part = true) const
332  {
333  std::size_t number_of_levels = getNumberOfLevels();
334 
335  HUMOTO_ASSERT( (number_of_levels <= 2) && (number_of_levels >= 1),
336  "Too many or too few levels.");
337 
338 
339  std::size_t objective_level = number_of_levels - 1;
340 
341  qp_problem.initializeObjective(hierarchy_[objective_level], initialize_upper_triangular_part);
342 
343  if (number_of_levels > 1)
344  {
345  qp_problem.initializeConstraints(hierarchy_[0], sol_structure);
346  }
347  }
348  };
349 }
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="") const
Log hierarchy as a set of tasks.
Definition: hierarchy.h:293
Abstract base class (for control problems)
std::vector< std::size_t > number_of_constraints_
Definition: hierarchy.h:26
void determineActiveSet(humoto::ActiveSet &active_set, const humoto::Solution &solution) const
Determine active set based on the solution. This active set may differ from the active set returned b...
Definition: hierarchy.h:202
#define HUMOTO_LOCAL
Definition: export_import.h:26
std::size_t getNumberOfLevels() const
Returns number of levels in the hierarchy.
Definition: hierarchy.h:280
#define HUMOTO_GLOBAL_LOGGER_IF_DEFINED
Definition: logger.h:997
#define HUMOTO_ASSERT(condition, message)
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
Container of the solution.
Definition: solution.h:176
This class represents one level of a hierarchy.
void reset(const std::size_t number_of_levels)
Reset the optimization problem.
Definition: hierarchy.h:238
OptimizationProblem()
Default constructor.
Definition: hierarchy.h:77
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
Active set corresponding to a hierarchy of Constraints.
Definition: active_set.h:278
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
virtual void guessSolution(Solution &solution_guess, const Solution &old_solution) const
Guess solution.
void initSolutionStructure(humoto::Solution &solution) const
Initialize structure of the given solution based on the internally stored solution structure...
An optimization problem [initialize_stack.m, simulation_loop.m].
Definition: hierarchy.h:20
void formHierarchy(humoto::Solution &solution, const humoto::Model &model, const humoto::ControlProblem &control_problem, const bool is_active_set_guessing_enabled)
Form hierarchy.
Definition: hierarchy.h:39
std::vector< humoto::HierarchyLevel > hierarchy_
Definition: hierarchy.h:25
void form(humoto::Solution &solution, const humoto::Model &model, const humoto::ControlProblem &control_problem)
Form hierarchy.
Definition: hierarchy.h:106
void processActiveSet(const humoto::ActiveSet &active_set)
Process actual active set: extract active sets of individual tasks.
Definition: hierarchy.h:181
boost::shared_ptr< humoto::TaskBase > TaskSharedPointer
Definition: task.h:584
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
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
void form(humoto::Solution &solution, humoto::Solution &solution_guess, humoto::ActiveSet &active_set_guess, const humoto::Model &model, const humoto::ControlProblem &control_problem, const humoto::Solution &old_solution)
Form hierarchy.
Definition: hierarchy.h:142
The root namespace of HuMoTo.
Definition: config.h:12
void computeViolations(humoto::Violations &violations, const humoto::Solution &solution) const
Compute violations based on the solution.
Definition: hierarchy.h:221
void initializeActiveSet(humoto::ActiveSet &active_set) const
Initialize active set of the hierarchy.
Definition: hierarchy.h:312
void form(humoto::Solution &solution, humoto::Solution &solution_guess, const humoto::Model &model, const humoto::ControlProblem &control_problem, const humoto::Solution &old_solution)
Form hierarchy.
Definition: hierarchy.h:164
Instances of this class are passed to a virtual method &#39;humoto::TaskBase::form()&#39;, so even though this class is basically useless in its present form we cannot avoid its definition using a template.
Definition: model.h:41
std::size_t size() const
Size of the active set.
Definition: active_set.h:332
Violations corresponding to a hierarchy of Constraints.
Definition: violations.h:235
void form(humoto::Solution &solution, humoto::ActiveSet &active_set_guess, const humoto::Model &model, const humoto::ControlProblem &control_problem)
Form hierarchy.
Definition: hierarchy.h:122
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
void guessActiveSet(ActiveSet &active_set) const
Form active set guess.
Definition: hierarchy.h:62
void pushTask(TaskSharedPointer task_pointer, const std::size_t level_index)
Add task to the optimization problem.
Definition: hierarchy.h:91
std::size_t number_of_levels_
Definition: hierarchy.h:23
void getQPProblem(QPProblemBase< t_QPConstraints > &qp_problem, const humoto::SolutionStructure &sol_structure, const bool initialize_upper_triangular_part=true) const
Form a QP problem.
Definition: hierarchy.h:329