humoto
task.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 Abstract base class (a task in a stack of tasks / hierarchy).
16  *
17  * @attention Implementation of this class should perform weighting during
18  * formulation (in function form()).
19  */
21  {
22  #define HUMOTO_CONFIG_ENTRIES
23  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
24 
25 
26  // The functions collected here are supposed to be called by
27  // OptimizationProblem class members only.
28  friend class HierarchyLevel;
29 
30  private:
31  std::string string_description_;
32 
34 
35 
36  private:
37  /**
38  * @brief Prepare formulation of a task.
39  *
40  * @param[in] number_of_variables
41  *
42  * @attention This function is called automatically.
43  */
44  void preForm(const std::size_t number_of_variables)
45  {
46  is_modified_ = true;
47 
48  setNumberOfVariables(number_of_variables);
49  }
50 
51 
52  /**
53  * @brief Finalize formulation of a task.
54  *
55  * @attention This function is called automatically.
56  */
57  void postForm() const
58  {
59  // ckecks
60  try
61  {
62  checkConsistency();
63  }
64  catch (const std::exception &e)
65  {
66  HUMOTO_THROW_MSG(std::string("Task '") + getDescription() + "' is inconsistent: " + e.what());
67  }
68 
69  HUMOTO_ASSERT( (active_set_guess_.size() == 0) || (active_set_guess_.size() == getNumberOfConstraints()),
70  std::string("Wrong size of the active set guess. Task: ") + getDescription());
71  }
72 
73 
74  /**
75  * @brief Copy the actual active set of the task from the given
76  * active set.
77  *
78  * @param[in] active_set active set
79  * @param[in] location location of the active set
80  */
82  const Location &location)
83  {
84  active_set_actual_.initialize(active_set, location);
85  }
86 
87 
88  /**
89  * @brief Copy the active set guess of the task to the given active
90  * set.
91  *
92  * @param[in] location location of the active set
93  * @param[in] active_set active set
94  */
96  const Location &location) const
97  {
98  active_set.copyTo( location,
99  active_set_guess_);
100  }
101 
102 
103  protected:
106 
107 
108  protected:
109  /**
110  * @brief Protected destructor: prevent destruction of the child
111  * classes through a base pointer.
112  */
114 
115 
117  {
118  setDefaults();
119  }
120 
121 
122  const std::string & getConfigSectionID() const
123  {
124  return (string_description_);
125  }
126 
127 
128  virtual void setDefaults()
129  {
130  is_modified_ = true;
131  //string_description_ = "TaskBase";
132  }
133 
134 
135 
136  /**
137  * @brief Initialize active set guess with defaults.
138  *
139  * @param[in] sol_structure structure of the solution
140  * @param[in] model_base model
141  * @param[in] control_problem control problem
142  *
143  * @attention This function is called automatically (after form())
144  * and can be redefined in derfived tasks. It doesn't make sense to
145  * redefine it for equality tasks.
146  */
147  virtual void guessActiveSet(const humoto::SolutionStructure &sol_structure,
148  const humoto::Model &model_base,
149  const humoto::ControlProblem &control_problem)
150  {
151  std::size_t num_ctr = getNumberOfConstraints();
152 
153  if (is_modified_ || (0 == active_set_actual_.size()))
154  {
155  // default initialization
156  if (isEquality())
157  {
158  active_set_guess_.initialize(num_ctr, ConstraintActivationType::EQUALITY);
159  }
160  else
161  {
162  active_set_guess_.initialize(num_ctr, ConstraintActivationType::INACTIVE);
163  }
164  }
165  else
166  {
167  // reuse actual active set from the previous iteration
168  HUMOTO_ASSERT( (num_ctr == active_set_actual_.size()),
169  std::string("The task is marked as unmodified, "
170  "but the size of the active set is not the same. Task: ")
171  + getDescription());
172 
173  active_set_guess_ = active_set_actual_;
174  }
175  }
176 
177 
178  /**
179  * @brief Form the task.
180  *
181  * @param[in] sol_structure structure of the solution
182  * @param[in] model_base model
183  * @param[in] control_problem control problem
184  *
185  * @attention This function is called automatically and must be
186  * defined in derived tasks.
187  */
188  virtual void form( const humoto::SolutionStructure &sol_structure,
189  const humoto::Model &model_base,
190  const humoto::ControlProblem &control_problem) = 0;
191 
192 
193 
194  /**
195  * @brief By default the task is assumed to be modified. Use this
196  * function to mark it as unmodified to avoid unnecessary
197  * recomputations.
198  *
199  * @attention This method should be called from form() method only.
200  *
201  * @attention If this function is called on a modified task, the
202  * result is undefined.
203  */
205  {
206  is_modified_ = false;
207  }
208 
209 
210  /**
211  * @brief Get actual active set.
212  *
213  * @return actual active set.
214  */
216  {
217  return (active_set_actual_);
218  }
219 
220 
221  /**
222  * @brief Get active set guess.
223  *
224  * @return active set guess.
225  */
227  {
228  return (active_set_guess_);
229  }
230 
231 
232  /**
233  * @brief Log task.
234  *
235  * @param[in,out] logger logger
236  * @param[in] parent parent
237  * @param[in] name name
238  */
239  virtual void logTask(humoto::Logger &logger HUMOTO_GLOBAL_LOGGER_IF_DEFINED,
240  const LogEntryName & parent = LogEntryName(),
241  const std::string & name = "task") const = 0;
242 
243  public:
244  /**
245  * @brief Get description of the task.
246  *
247  * @return description
248  */
249  const std::string getDescription() const
250  {
251  return (string_description_);
252  }
253 
254 
255  /**
256  * @brief Set description of the task.
257  *
258  * @return description
259  */
260  void setDescription(const std::string &description)
261  {
262  string_description_ = description;
263  }
264 
265 
266  /**
267  * @brief Log task.
268  *
269  * @param[in,out] logger logger
270  * @param[in] parent parent
271  * @param[in] name name
272  */
273  void log(humoto::Logger &logger HUMOTO_GLOBAL_LOGGER_IF_DEFINED,
274  const LogEntryName & parent = LogEntryName(),
275  const std::string & name = "task") const
276  {
277  LogEntryName subname = parent;
278  subname.add(name);
279 
280  logger.log(LogEntryName(subname).add("id"), getDescription());
281  logTask(logger, subname, "");
282 
283  active_set_guess_.log(logger, subname, "active_set_guess");
284  active_set_actual_.log(logger, subname, "active_set_actual");
285 
286  ConstraintsBase::log(logger, subname, "");
287  }
288  };
289 
290 
291 
292  /**
293  * @brief General task mixin -- should be used in building general tasks
294  *
295  * @tparam t_Constraints Base class representing general constraints
296  */
297  template<class t_Constraints>
299  {
300  protected:
301  double gain_;
302 
303 
304  protected:
305  #define HUMOTO_CONFIG_ENTRIES \
306  HUMOTO_CONFIG_PARENT_CLASS(TaskBase) \
307  HUMOTO_CONFIG_SCALAR_(gain)
308  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
309 
310 
311  /**
312  * @brief Protected destructor: prevent destruction of the child
313  * classes through a base pointer.
314  */
316 
317 
318  /**
319  * @brief Constructor
320  *
321  * @param[in] description description of the task
322  * @param[in] gain gain
323  */
324  GeneralTaskBaseMixin(const std::string &description,
325  const double gain = 0.0)
326  {
327  t_Constraints::setDescription(description);
328  setGain(gain);
329  }
330 
331 
332  virtual void setDefaults()
333  {
334  setGain(0.0);
335  }
336 
337  void finalize() {}
338 
339 
340  /**
341  * @brief Log task.
342  *
343  * @param[in,out] logger logger
344  * @param[in] parent parent
345  * @param[in] name name
346  */
347  virtual void logTask(humoto::Logger &logger HUMOTO_GLOBAL_LOGGER_IF_DEFINED,
348  const LogEntryName & parent = LogEntryName(),
349  const std::string & name = "task") const
350  {
351  logger.log(LogEntryName(parent).add(name).add("gain"), getGain());
352  }
353 
354 
355  public:
356  /**
357  * @brief Sets gain.
358  *
359  * @param[in] gain new gain
360  */
361  void setGain(const double gain)
362  {
363  gain_ = gain;
364  t_Constraints::is_modified_ = true;
365  }
366 
367 
368  /**
369  * @brief Returns gain.
370  *
371  * @return gain
372  */
373  double getGain() const
374  {
375  return (gain_);
376  }
377  };
378 
379 
380  /**
381  * @brief simple task mixin -- should be used in building general tasks
382  *
383  * @tparam t_Constraints Base class representing simple constraints
384  */
385  template<class t_Constraints>
387  {
388  // The functions collected here are supposed to be called by
389  // OptimizationProblem class members only.
390  friend class HierarchyLevel;
391 
392  protected:
393  double gain_;
394 
395 
396  protected:
397  #define HUMOTO_CONFIG_ENTRIES \
398  HUMOTO_CONFIG_PARENT_CLASS(TaskBase) \
399  HUMOTO_CONFIG_SCALAR_(gain)
400  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
401 
402 
403  /**
404  * @brief Protected destructor: prevent destruction of the child
405  * classes through a base pointer.
406  */
408 
409 
410  /**
411  * @brief Constructor
412  *
413  * @param[in] description description of the task
414  * @param[in] gain gain
415  */
416  WeightedSimpleTaskBaseMixin(const std::string &description,
417  const double gain = 0.0)
418  {
419  t_Constraints::setDescription(description);
420  setGain(gain);
421  }
422 
423 
424  virtual void setDefaults()
425  {
426  setGain(0.0);
427  }
428  void finalize() {}
429 
430 
431  /**
432  * @brief Log task.
433  *
434  * @param[in,out] logger logger
435  * @param[in] parent parent
436  * @param[in] name name
437  */
438  virtual void logTask(humoto::Logger &logger HUMOTO_GLOBAL_LOGGER_IF_DEFINED,
439  const LogEntryName & parent = LogEntryName(),
440  const std::string & name = "task") const
441  {
442  logger.log(LogEntryName(parent).add(name).add("gain"), getGain());
443  }
444 
445 
446  public:
447  /**
448  * @brief Sets gain.
449  *
450  * @param[in] gain new gain
451  */
452  virtual void setGain(const double gain)
453  {
454  gain_ = gain;
455  t_Constraints::is_modified_ = true;
456  }
457 
458 
459  /**
460  * @brief Returns gain.
461  *
462  * @return gain
463  */
464  double getGain() const
465  {
466  return (gain_);
467  }
468  };
469 
470 
471 
472  /**
473  * @brief simple task mixin -- should be used in building general tasks
474  *
475  * @tparam t_Constraints Base class representing simple constraints
476  */
477  template<class t_Constraints>
479  {
480  // The functions collected here are supposed to be called by
481  // OptimizationProblem class members only.
482  friend class HierarchyLevel;
483 
484 
485  protected:
486  /**
487  * @brief Protected destructor: prevent destruction of the child
488  * classes through a base pointer.
489  */
491 
492 
493  /**
494  * @brief Constructor
495  *
496  * @param[in] description description of the task
497  */
498  explicit SimpleTaskBaseMixin(const std::string &description)
499  {
500  t_Constraints::setDescription(description);
501  }
502 
503 
504 
505  /**
506  * @brief Log task.
507  *
508  * @param[in,out] logger logger
509  * @param[in] parent parent
510  * @param[in] name name
511  */
512  virtual void logTask(humoto::Logger &logger HUMOTO_GLOBAL_LOGGER_IF_DEFINED,
513  const LogEntryName & parent = LogEntryName(),
514  const std::string & name = "task") const
515  {
516  }
517  };
518 
519 
520 #ifdef HUMOTO_DOXYGEN_PROCESSING
521  // Define fake classes instead of typedefs used below in order to force
522  // doxygen to create corresponding documentation pages.
523 #define HUMOTO_TASK_CLASS_GENERATOR(parent, name) class name : public parent {}
524 #else
525 #define HUMOTO_TASK_CLASS_GENERATOR(parent, name) typedef parent name
526 #endif
527 
528  /**
529  * @ingroup Tasks
530  *
531  * @addtogroup BaseTasks Task base classes
532  * @brief Base classes for user-defined tasks
533  * @{
534  */
535  /// @brief Task: lb <= A*x <= ub
537  /// @brief Task: lb <= A*x
539  /// @brief Task: A*x <= ub
541  /// @brief Task: A*x = b
543  /// @brief Task: A*x = 0
545 
546 
547  /// @brief Task: lb <= A*S*x <= ub
549  /// @brief Task: lb <= A*S*x
551  /// @brief Task: A*S*x <= ub
553  /// @brief Task: A*S*x = b
555  /// @brief Task: A*S*x = 0
557 
558 
559  /// @brief Task: lb <= diag(G)*x[I] <= ub
561  /// @brief Task: lb <= diag(G)*x[I]
563  /// @brief Task: diag(G)*x[I] <= ub
565  /// @brief Task: diag(G)*x[I] = b
567  /// @brief Task: diag(G)*x[I] = 0
569 
570 
571  /// @brief Task: lb <= x[I] <= ub
573  /// @brief Task: lb <= x[I]
575  /// @brief Task: x[I] <= ub
577  /// @brief Task: x[I] = b
579  /// @brief Task: x[I] = 0
581  /// @}
582 #undef HUMOTO_TASK_CLASS_GENERATOR
583 
584  typedef boost::shared_ptr<humoto::TaskBase> TaskSharedPointer;
585 }
GeneralTaskBaseMixin(const std::string &description, const double gain=0.0)
Constructor.
Definition: task.h:324
Abstract base class (for control problems)
Task: A*S*x = 0.
Definition: task.h:556
std::string string_description_
Definition: task.h:31
Task: lb <= x[I].
Definition: task.h:574
bool is_modified_
Definition: task.h:104
#define HUMOTO_LOCAL
Definition: export_import.h:26
Constraint is an equality (always active)
Definition: active_set.h:35
const ActiveSetConstraints & getActualActiveSet() const
Get actual active set.
Definition: task.h:215
~SimpleTaskBaseMixin()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: task.h:490
virtual void logTask(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="task") const
Log task.
Definition: task.h:347
virtual void guessActiveSet(const humoto::SolutionStructure &sol_structure, const humoto::Model &model_base, const humoto::ControlProblem &control_problem)
Initialize active set guess with defaults.
Definition: task.h:147
#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
Task: A*x <= ub.
Definition: task.h:540
virtual void logTask(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="task") const
Log task.
Definition: task.h:512
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="task") const
Log task.
Definition: task.h:273
Abstract base class (a task in a stack of tasks / hierarchy).
Definition: task.h:20
void copyActualActiveSetFrom(const ActiveSetConstraints &active_set, const Location &location)
Copy the actual active set of the task from the given active set.
Definition: task.h:81
std::size_t size() const
Size of the active set.
Definition: active_set.h:84
Task: x[I] = b.
Definition: task.h:578
void markAsUnmodified()
By default the task is assumed to be modified. Use this function to mark it as unmodified to avoid un...
Definition: task.h:204
Analog of &#39;sol_structure&#39; struct in Octave code. [determine_solution_structure.m].
Definition: solution.h:33
virtual void setDefaults()
Definition: task.h:332
Active set corresponding to Constraints class.
Definition: active_set.h:43
This class represents one level of a hierarchy.
const std::string & getConfigSectionID() const
Return the default name of a configuration node corresponding to this class.
Definition: task.h:122
void preForm(const std::size_t number_of_variables)
Prepare formulation of a task.
Definition: task.h:44
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
Represents log entry name.
Definition: logger.h:169
const std::string getDescription() const
Get description of the task.
Definition: task.h:249
virtual void setGain(const double gain)
Sets gain.
Definition: task.h:452
Task: A*S*x = b.
Definition: task.h:554
ActiveSetConstraints active_set_guess_
Definition: task.h:33
void postForm() const
Finalize formulation of a task.
Definition: task.h:57
void initialize(const std::size_t number_of_constraints)
Initialize active set.
Definition: active_set.h:167
#define HUMOTO_TASK_CLASS_GENERATOR(parent, name)
Definition: task.h:523
boost::shared_ptr< humoto::TaskBase > TaskSharedPointer
Definition: task.h:584
~WeightedSimpleTaskBaseMixin()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: task.h:407
Task: lb <= A*x <= ub.
Definition: task.h:536
virtual void logTask(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="task") const
Log task.
Definition: task.h:438
Task: lb <= x[I] <= ub.
Definition: task.h:572
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
void setDescription(const std::string &description)
Set description of the task.
Definition: task.h:260
Task: A*S*x <= ub.
Definition: task.h:552
ActiveSetConstraints active_set_actual_
Definition: task.h:105
SimpleTaskBaseMixin(const std::string &description)
Constructor.
Definition: task.h:498
Task: lb <= diag(G)*x[I] <= ub.
Definition: task.h:560
Task: lb <= A*S*x.
Definition: task.h:550
Task: x[I] <= ub.
Definition: task.h:576
simple task mixin – should be used in building general tasks
Definition: task.h:478
The root namespace of HuMoTo.
Definition: config.h:12
simple task mixin – should be used in building general tasks
Definition: task.h:386
WeightedSimpleTaskBaseMixin(const std::string &description, const double gain=0.0)
Constructor.
Definition: task.h:416
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
~GeneralTaskBaseMixin()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: task.h:315
void copyTo(const Location &location, const ActiveSetConstraints &other_active_set)
Set type of a set of constraints.
Definition: active_set.h:133
void setGain(const double gain)
Sets gain.
Definition: task.h:361
Task: lb <= A*x.
Definition: task.h:538
Task: diag(G)*x[I] = 0.
Definition: task.h:568
double getGain() const
Returns gain.
Definition: task.h:464
virtual void setDefaults()
Set members to their default values.
Definition: task.h:128
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
Location of a data chunk (offset + length).
Definition: utility.h:146
Task: diag(G)*x[I] <= ub.
Definition: task.h:564
General task mixin – should be used in building general tasks.
Definition: task.h:298
Task: A*x = 0.
Definition: task.h:544
Task: A*x = b.
Definition: task.h:542
double getGain() const
Returns gain.
Definition: task.h:373
Task: x[I] = 0.
Definition: task.h:580
Task: lb <= diag(G)*x[I].
Definition: task.h:562
Task: lb <= A*S*x <= ub.
Definition: task.h:548
Task: diag(G)*x[I] = b.
Definition: task.h:566
ActiveSetConstraints & getActiveSetGuess()
Get active set guess.
Definition: task.h:226
void copyActiveSetGuessTo(ActiveSetConstraints &active_set, const Location &location) const
Copy the active set guess of the task to the given active set.
Definition: task.h:95
~TaskBase()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: task.h:113
Constraints abstract interface class.