humoto
solver.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 Solver interface classes
8 */
9 
10 #pragma once
11 
12 namespace humoto
13 {
14  /**
15  * @brief Parameters of a solver
16  */
18  {
19  #define HUMOTO_CONFIG_SECTION_ID "SolverParametersBase"
20  #define HUMOTO_CONFIG_CONSTRUCTOR SolverParametersBase
21  #define HUMOTO_CONFIG_ENTRIES \
22  HUMOTO_CONFIG_SCALAR_(crash_on_any_failure) \
23  HUMOTO_CONFIG_SCALAR_(solve_two_levels_as_qp)
24  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
25 
26 
27  protected:
28  void setDefaults()
29  {
30  crash_on_any_failure_ = true;
31  solve_two_levels_as_qp_ = true;
32  }
33 
34 
35  /**
36  * @brief Protected destructor: prevent destruction of the child
37  * classes through a base pointer.
38  */
40 
42  {
43  setDefaults();
44  }
45 
46 
47  public:
48  /// Throw an exception if solver does not return sucessfully.
50 
51  /// Enable conversion of an optimization problem with two priority levels to a QP.
53  };
54 
55 
56 
57  /**
58  * @brief Base solver class
59  *
60  * @tparam t_SolverParameters class representing parameters of the solver,
61  * should inherit from #SolverParametersBase
62  */
63  template <class t_SolverParameters>
65  {
66  protected:
67  t_SolverParameters parameters_;
68 
69 
70  protected:
71  /**
72  * @brief Protected destructor: prevent destruction of the child
73  * classes through a base pointer.
74  */
75  ~Solver() {}
76  Solver() {}
77 
78 
79  /**
80  * @brief Check return status and crash if it indicates a failure
81  * and crashing is enabled.
82  *
83  * @param[in] solution
84  */
85  void checkResults(const humoto::Solution &solution) const
86  {
87  if ((true == parameters_.crash_on_any_failure_)
88  &&
89  (SolverStatus::OK != solution.return_status_))
90  {
91  HUMOTO_THROW_MSG(std::string("Solver failed: ") + solution.getStatusDescription());
92  }
93  }
94 
95 
96  /**
97  * @brief Checks input, does nothing by default, may include extra
98  * checks in mixins.
99  *
100  * @param[in] hierarchy
101  */
102  virtual void checkInputs(const humoto::OptimizationProblem &hierarchy) const
103  {
104  }
105 
106 
107  /**
108  * @brief Reset solver
109  */
110  virtual void reset()
111  {
112  }
113 
114 
115  /**
116  * @brief Initialize solver
117  *
118  * @param[in] hierarchy hierarchy
119  * @param[in] sol_structure solution structure
120  */
121  virtual void initialize( const humoto::OptimizationProblem &hierarchy,
122  const humoto::SolutionStructure &sol_structure) = 0;
123 
124  /**
125  * @brief Solve the hierarchy
126  *
127  * @param[out] solution solution
128  * @param[in] hierarchy hierarchy
129  */
130  virtual void solveHierarchy(humoto::Solution &solution,
131  const humoto::OptimizationProblem &hierarchy) = 0;
132 
133 
134 
135  public:
136  /**
137  * @brief Construct solver with specified parameters.
138  *
139  * @param[in] parameters parameters
140  */
141  void setParameters(const t_SolverParameters &parameters)
142  {
143  parameters_ = parameters;
144  reset();
145  }
146 
147 
148  /**
149  * @brief Solve an optimization problem (using previously
150  * specified parameters)
151  *
152  * @param[out] solution
153  * @param[in] hierarchy
154  */
155  void solve( humoto::Solution &solution,
156  const humoto::OptimizationProblem &hierarchy)
157  {
158  checkInputs(hierarchy);
159  initialize(hierarchy, solution);
160  solveHierarchy(solution, hierarchy);
161  checkResults(solution);
162  }
163 
164 
165  /**
166  * @brief Solve an optimization problem (using previously
167  * specified parameters)
168  *
169  * @param[out] solution
170  * @param[in] hierarchy
171  * @param[in] parameters parameters
172  */
173  void solve( humoto::Solution &solution,
174  const humoto::OptimizationProblem &hierarchy,
175  const t_SolverParameters &parameters)
176  {
177  parameters_ = parameters;
178 
179  checkInputs(hierarchy);
180  initialize(hierarchy, solution);
181  solveHierarchy(solution, hierarchy);
182  checkResults(solution);
183  }
184  };
185 
186 
187 
188  /**
189  * @brief Mixin solver interface (solution guessing)
190  *
191  * @tparam t_Solver base solver class
192  */
193  template <class t_Solver>
195  {
196  protected:
197  using t_Solver::initialize;
198  using t_Solver::solveHierarchy;
199  using t_Solver::checkResults;
200  using t_Solver::checkInputs;
201 
202  /**
203  * @brief Protected destructor: prevent destruction of the child
204  * classes through a base pointer.
205  */
208 
209 
210  /**
211  * @brief Set solution guess
212  *
213  * @param[in] solution_guess
214  */
215  virtual void setSolutionGuess(const humoto::Solution & solution_guess) = 0;
216 
217 
218  public:
219  using t_Solver::solve;
220 
221 
222  /**
223  * @brief Solve an optimization problem (using previously
224  * specified parameters)
225  *
226  * @param[out] solution
227  * @param[in] hierarchy
228  * @param[in] solution_guess
229  */
230  void solve( humoto::Solution &solution,
231  const humoto::OptimizationProblem &hierarchy,
232  const humoto::Solution &solution_guess)
233  {
234  checkInputs(hierarchy);
235  initialize(hierarchy, solution);
236  setSolutionGuess(solution_guess);
237  solveHierarchy(solution, hierarchy);
238  checkResults(solution);
239  }
240  };
241 
242 
243 
244  /**
245  * @brief Mixin solver interface (active set guessing)
246  *
247  * @tparam t_Solver base solver class
248  */
249  template <class t_Solver>
251  {
252  protected:
253  using t_Solver::initialize;
254  using t_Solver::solveHierarchy;
255  using t_Solver::checkResults;
256  using t_Solver::checkInputs;
257 
258  /**
259  * @brief Protected destructor: prevent destruction of the child
260  * classes through a base pointer.
261  */
264 
265 
266  /**
267  * @brief Set active set guess
268  *
269  * @param[in] hierarchy
270  * @param[in] active_set
271  */
272  virtual void setActiveSet( const humoto::ActiveSet &active_set,
273  const humoto::OptimizationProblem &hierarchy) = 0;
274 
275  /**
276  * @brief Get active set
277  *
278  * @param[out] active_set
279  * @param[in] hierarchy
280  */
281  virtual void getActiveSet( humoto::ActiveSet &active_set,
282  const humoto::OptimizationProblem &hierarchy) = 0;
283 
284  public:
285  using t_Solver::solve;
286 
287 
288  /**
289  * @brief Solve an optimization problem (using previously
290  * specified parameters)
291  *
292  * @param[out] solution
293  * @param[out] active_set
294  * @param[in] hierarchy
295  * @param[in] active_set_guess
296  */
297  void solve( Solution &solution,
298  ActiveSet &active_set,
299  const humoto::OptimizationProblem &hierarchy,
300  const ActiveSet &active_set_guess)
301  {
302  checkInputs(hierarchy);
303  initialize(hierarchy, solution);
304  setActiveSet(active_set_guess, hierarchy);
305  solveHierarchy(solution, hierarchy);
306  checkResults(solution);
307  getActiveSet(active_set, hierarchy);
308  }
309 
310 
311  /**
312  * @brief Solve an optimization problem (using previously
313  * specified parameters)
314  *
315  * @param[out] solution
316  * @param[in] hierarchy
317  * @param[in] active_set_guess
318  */
319  void solve( Solution &solution,
320  const humoto::OptimizationProblem &hierarchy,
321  const ActiveSet &active_set_guess)
322  {
323  checkInputs(hierarchy);
324  initialize(hierarchy, solution);
325  setActiveSet(active_set_guess, hierarchy);
326  solveHierarchy(solution, hierarchy);
327  checkResults(solution);
328  }
329 
330 
331  /**
332  * @brief Solve an optimization problem (using previously
333  * specified parameters)
334  *
335  * @param[out] solution
336  * @param[out] active_set
337  * @param[in] hierarchy
338  */
339  void solve( humoto::Solution &solution,
340  humoto::ActiveSet &active_set,
341  const humoto::OptimizationProblem &hierarchy)
342  {
343  checkInputs(hierarchy);
344  initialize(hierarchy, solution);
345  solveHierarchy(solution, hierarchy);
346  checkResults(solution);
347  getActiveSet(active_set, hierarchy);
348  }
349  };
350 
351 
352 
353  /**
354  * @brief Mixin solver interface (active set and solution guessing)
355  *
356  * @tparam t_Solver base solver class
357  */
358  template <class t_Solver>
360  {
361  protected:
362  using t_Solver::initialize;
363  using t_Solver::solveHierarchy;
364  using t_Solver::checkResults;
365  using t_Solver::checkInputs;
366  using t_Solver::getActiveSet;
367  using t_Solver::setActiveSet;
368  using t_Solver::setSolutionGuess;
369 
370 
371  /**
372  * @brief Protected destructor: prevent destruction of the child
373  * classes through a base pointer.
374  */
377 
378 
379  public:
380  using t_Solver::solve;
381 
382 
383  /**
384  * @brief Solve an optimization problem (using previously
385  * specified parameters)
386  *
387  * @param[out] solution
388  * @param[out] active_set
389  * @param[in] hierarchy
390  * @param[in] solution_guess
391  * @param[in] active_set_guess
392  */
393  void solve( Solution &solution,
394  ActiveSet &active_set,
395  const humoto::OptimizationProblem &hierarchy,
396  const humoto::Solution &solution_guess,
397  const ActiveSet &active_set_guess)
398  {
399  checkInputs(hierarchy);
400  initialize(hierarchy, solution);
401  setSolutionGuess(solution_guess);
402  setActiveSet(active_set_guess, hierarchy);
403  solveHierarchy(solution, hierarchy);
404  checkResults(solution);
405  getActiveSet(active_set, hierarchy);
406  }
407 
408 
409  /**
410  * @brief Solve an optimization problem (using previously
411  * specified parameters)
412  *
413  * @param[out] solution
414  * @param[in] hierarchy
415  * @param[in] solution_guess
416  * @param[in] active_set_guess
417  */
418  void solve( Solution &solution,
419  const humoto::OptimizationProblem &hierarchy,
420  const humoto::Solution &solution_guess,
421  const ActiveSet &active_set_guess)
422  {
423  checkInputs(hierarchy);
424  initialize(hierarchy, solution);
425  setSolutionGuess(solution_guess);
426  setActiveSet(active_set_guess, hierarchy);
427  solveHierarchy(solution, hierarchy);
428  checkResults(solution);
429  }
430 
431 
432  /**
433  * @brief Solve an optimization problem (using previously
434  * specified parameters)
435  *
436  * @param[out] solution
437  * @param[out] active_set
438  * @param[in] hierarchy
439  * @param[in] solution_guess
440  */
441  void solve( humoto::Solution &solution,
442  humoto::ActiveSet &active_set,
443  const humoto::OptimizationProblem &hierarchy,
444  const humoto::Solution &solution_guess)
445  {
446  checkInputs(hierarchy);
447  initialize(hierarchy, solution);
448  setSolutionGuess(solution_guess);
449  solveHierarchy(solution, hierarchy);
450  checkResults(solution);
451  getActiveSet(active_set, hierarchy);
452  }
453  };
454 
455 
456  /**
457  * @brief Mixin solver interface (QP solver)
458  *
459  * @tparam t_Solver base solver class
460  */
461  template <class t_Solver>
463  {
464  protected:
465  using t_Solver::parameters_;
466 
467 
468  protected:
469  virtual void checkInputs(const humoto::OptimizationProblem &hierarchy) const
470  {
471  HUMOTO_ASSERT( true == parameters_.solve_two_levels_as_qp_,
472  "Interpretation of hierarchies as QPs is disabled.");
473 
474  std::size_t number_of_levels = hierarchy.getNumberOfLevels();
475 
476  HUMOTO_ASSERT( (number_of_levels <= 2) && (number_of_levels >= 1),
477  "This type of hierarchy cannot be solved with a QP solver.");
478 
479 
480  std::size_t objective_level = number_of_levels - 1;
481 
482  HUMOTO_ASSERT( hierarchy[objective_level].isEquality(),
483  "This type of hierarchy cannot be solved with a QP solver.");
484 
485  HUMOTO_ASSERT( hierarchy[objective_level].getNumberOfConstraints() > 0,
486  "Empty objective.");
487  }
488 
489 
490  /**
491  * @brief Protected destructor: prevent destruction of the child
492  * classes through a base pointer.
493  */
496  };
497 
498 
499 
500  /**
501  * @brief Solver class with solution guessing
502  *
503  * @tparam t_SolverParameters class representing parameters of the solver,
504  * should inherit from #SolverParametersBase
505  */
506  template <class t_SolverParameters>
507  class HUMOTO_LOCAL SolverGuessSolution : public SolverGuessSolutionMixin< Solver<t_SolverParameters> >
508  {
509  protected:
510  /**
511  * @brief Protected destructor: prevent destruction of the child
512  * classes through a base pointer.
513  */
516  };
517 
518 
519  /**
520  * @brief Solver class with active set guessing
521  *
522  * @tparam t_SolverParameters class representing parameters of the solver,
523  * should inherit from #SolverParametersBase
524  */
525  template <class t_SolverParameters>
526  class HUMOTO_LOCAL SolverGuessActiveSet : public SolverGuessActiveSetMixin< Solver<t_SolverParameters> >
527  {
528  protected:
529  /**
530  * @brief Protected destructor: prevent destruction of the child
531  * classes through a base pointer.
532  */
535  };
536 
537 
538  /**
539  * @brief Solver class with active set and solution guessing
540  *
541  * @tparam t_SolverParameters class representing parameters of the solver,
542  * should inherit from #SolverParametersBase
543  */
544  template <class t_SolverParameters>
547  SolverGuessSolutionMixin<
548  SolverGuessActiveSetMixin<
549  Solver<t_SolverParameters> > > >
550  {
551  protected:
552  /**
553  * @brief Protected destructor: prevent destruction of the child
554  * classes through a base pointer.
555  */
558  };
559 }
Base solver class.
Definition: solver.h:64
void solve(Solution &solution, ActiveSet &active_set, const humoto::OptimizationProblem &hierarchy, const ActiveSet &active_set_guess)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:297
~SolverGuessSolution()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:514
~SolverParametersBase()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:39
virtual void reset()
Reset solver.
Definition: solver.h:110
#define HUMOTO_LOCAL
Definition: export_import.h:26
void setParameters(const t_SolverParameters &parameters)
Construct solver with specified parameters.
Definition: solver.h:141
Mixin solver interface (active set guessing)
Definition: solver.h:250
std::size_t getNumberOfLevels() const
Returns number of levels in the hierarchy.
Definition: hierarchy.h:280
t_SolverParameters parameters_
Definition: solver.h:67
#define HUMOTO_ASSERT(condition, message)
~SolverGuessSolutionActiveSet()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:556
Default configurable base is strict.
Definition: config.h:353
Analog of &#39;sol_structure&#39; struct in Octave code. [determine_solution_structure.m].
Definition: solution.h:33
Container of the solution.
Definition: solution.h:176
void setDefaults()
Set members to their default values.
Definition: solver.h:28
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
Active set corresponding to a hierarchy of Constraints.
Definition: active_set.h:278
~SolverGuessActiveSetMixin()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:262
SolverStatus::Status return_status_
Definition: solution.h:187
An optimization problem [initialize_stack.m, simulation_loop.m].
Definition: hierarchy.h:20
~QPSolverMixin()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:494
void solve(Solution &solution, const humoto::OptimizationProblem &hierarchy, const humoto::Solution &solution_guess, const ActiveSet &active_set_guess)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:418
void solve(humoto::Solution &solution, const humoto::OptimizationProblem &hierarchy, const humoto::Solution &solution_guess)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:230
~SolverGuessSolutionActiveSetMixin()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:375
~Solver()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:75
bool solve_two_levels_as_qp_
Enable conversion of an optimization problem with two priority levels to a QP.
Definition: solver.h:52
virtual void checkInputs(const humoto::OptimizationProblem &hierarchy) const
Checks input, does nothing by default, may include extra checks in mixins.
Definition: solver.h:102
The root namespace of HuMoTo.
Definition: config.h:12
~SolverGuessActiveSet()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:533
Solver class with active set guessing.
Definition: solver.h:526
Mixin solver interface (active set and solution guessing)
Definition: solver.h:359
void solve(humoto::Solution &solution, humoto::ActiveSet &active_set, const humoto::OptimizationProblem &hierarchy, const humoto::Solution &solution_guess)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:441
void solve(Solution &solution, const humoto::OptimizationProblem &hierarchy, const ActiveSet &active_set_guess)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:319
Mixin solver interface (QP solver)
Definition: solver.h:462
bool crash_on_any_failure_
Throw an exception if solver does not return sucessfully.
Definition: solver.h:49
Parameters of a solver.
Definition: solver.h:17
void solve(humoto::Solution &solution, humoto::ActiveSet &active_set, const humoto::OptimizationProblem &hierarchy)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:339
virtual std::string getStatusDescription() const
Status description.
Definition: solution.h:369
void solve(humoto::Solution &solution, const humoto::OptimizationProblem &hierarchy, const t_SolverParameters &parameters)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:173
void solve(humoto::Solution &solution, const humoto::OptimizationProblem &hierarchy)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:155
Mixin solver interface (solution guessing)
Definition: solver.h:194
~SolverGuessSolutionMixin()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: solver.h:206
void checkResults(const humoto::Solution &solution) const
Check return status and crash if it indicates a failure and crashing is enabled.
Definition: solver.h:85
Solver class with active set and solution guessing.
Definition: solver.h:545
void solve(Solution &solution, ActiveSet &active_set, const humoto::OptimizationProblem &hierarchy, const humoto::Solution &solution_guess, const ActiveSet &active_set_guess)
Solve an optimization problem (using previously specified parameters)
Definition: solver.h:393
virtual void checkInputs(const humoto::OptimizationProblem &hierarchy) const
Definition: solver.h:469
Solver class with solution guessing.
Definition: solver.h:507