humoto
task_generic.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  * @ingroup Tasks
17  *
18  * @defgroup PredefinedTasks Predefined tasks
19  * @brief Predefined tasks
20  */
21 
22  /**
23  * @brief [task_zerovars.m] Set given variables to zero, i.e. minimize these variables.
24  * @ingroup PredefinedTasks
25  */
27  {
28  #define HUMOTO_CONFIG_ENTRIES \
29  HUMOTO_CONFIG_PARENT_CLASS(TaskGIB0) \
30  HUMOTO_CONFIG_SCALAR_(variables_id)
31  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
32 
33 
34  private:
36 
37  std::string variables_id_;
38 
39 
40  public:
41  /**
42  * @brief Set variables string id to a given value
43  *
44  * @param[in] variables_id string id of variables
45  */
46  void setVariablesID(const std::string& variables_id)
47  {
48  variables_id_ = variables_id;
49  }
50 
51 
52  /**
53  * @brief Constructor
54  *
55  * @param[in] gain gain, by default is 2 so that 'sqrt(gain/2) = 1'.
56  * @param[in] task_id string id of the task.
57  * @param[in] variables_id string id of variables: if empty, all variables are minimized.
58  */
60  const double gain = 2.0,
61  const std::string &task_id = "Minimize_All_Variables",
62  const std::string &variables_id = "")
63  : TaskGIB0(task_id.c_str(), gain)
64  {
65  location_of_variables_.set(0,0);
66 
67  variables_id_ = variables_id;
68  }
69 
70 
71  /**
72  * @brief Form task
73  *
74  * @param[in] sol_structure structure of the solution
75  * @param[in] model_base model
76  * @param[in] control_problem control problem
77  */
78  void form( const humoto::SolutionStructure &sol_structure,
79  const humoto::Model &model_base,
80  const humoto::ControlProblem &control_problem)
81  {
82  Location location_of_variables;
83 
84  if (variables_id_.size() == 0)
85  {
86  location_of_variables.set(0, sol_structure.getNumberOfVariables());
87  }
88  else
89  {
90  location_of_variables = sol_structure.getSolutionPartLocation(variables_id_);
91  }
92 
93 
94  if ( location_of_variables != location_of_variables_ )
95  {
96  // Task was modified
97  location_of_variables_ = location_of_variables;
98 
99 
100  humoto::IndexVector &I = getIndices();
101  Eigen::VectorXd &gains = getIGains();
102 
103  gains.setConstant(location_of_variables_.length_, getGain());
104 
105  I.resize(location_of_variables_.length_);
106  for (std::size_t i = 0; i < location_of_variables_.length_; ++i)
107  {
108  I[i] = location_of_variables_.offset_ + i;
109  }
110  }
111  else
112  {
113  // Task is not modified.
114  // In this particular case performance is hardly
115  // improved. This code is just a demo.
116  markAsUnmodified();
117  }
118  }
119  };
120 
121 
122  /**
123  * @brief Same as humoto::TaskZeroVariables, but the variables need not to
124  * be continuous.
125  * @ingroup PredefinedTasks
126  */
128  {
129  #define HUMOTO_CONFIG_ENTRIES \
130  HUMOTO_CONFIG_PARENT_CLASS(TaskGIB0) \
131  HUMOTO_CONFIG_COMPOUND_(variables_indices)
132  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
133 
134 
135  private:
137 
138 
139  public:
140  /**
141  * @brief Constructor
142  *
143  * @param[in] var_indices indices of variables
144  * @param[in] gain gain, by default is 2 so that 'sqrt(gain/2) = 1'.
145  * @param[in] task_id string id of the task.
146  */
148  const humoto::IndexVector &var_indices,
149  const double gain = 2.0,
150  const std::string &task_id = "Minimize_Selected_Variables")
151  : TaskGIB0(task_id.c_str(), gain)
152  {
153  variables_indices_ = var_indices;
154  }
155 
156 
157  /**
158  * @brief Form task
159  *
160  * @param[in] sol_structure structure of the solution
161  * @param[in] model_base model
162  * @param[in] control_problem control problem
163  */
164  void form( const humoto::SolutionStructure &sol_structure,
165  const humoto::Model &model_base,
166  const humoto::ControlProblem &control_problem)
167  {
168  getIndices() = variables_indices_;
169 
170  Eigen::VectorXd &gains = getIGains();
171  gains.setConstant(variables_indices_.rows(), getGain());
172  }
173  };
174 
175 
176  /**
177  * @brief Infeasible inequality task for testing purposes.
178  * @ingroup PredefinedTasks
179  */
181  {
182  private:
183  std::size_t number_of_variables_;
184 
185  public:
186  /**
187  * @brief Constructor
188  *
189  * @param[in] gain gain, by default is 2 so that 'sqrt(gain/2) = 1'.
190  * @param[in] task_id string id of the task.
191  */
193  const double gain = 2.0,
194  const std::string &task_id = "Infeasible_Inequality_Task")
195  : TaskALU(task_id.c_str(), gain)
196  {
197  number_of_variables_ = 0;
198  }
199 
200 
201  /**
202  * @brief Form task
203  *
204  * @param[in] sol_structure structure of the solution
205  * @param[in] model_base model
206  * @param[in] control_problem control problem
207  */
208  void form( const humoto::SolutionStructure &sol_structure,
209  const humoto::Model &model_base,
210  const humoto::ControlProblem &control_problem)
211  {
212  if (number_of_variables_ != sol_structure.getNumberOfVariables())
213  {
214  number_of_variables_ = sol_structure.getNumberOfVariables();
215 
216  getA().resize(1, number_of_variables_);
217  getA().setConstant(1);
218 
219  getLowerBounds().resize(1);
220  getUpperBounds().resize(1);
221 
222  getLowerBounds()[0] = 1;
223  getUpperBounds()[0] = -1;
224  }
225  else
226  {
227  // Task is not modified.
228  // In this particular case perfromance is hardly
229  // improved. This code is just a demo.
230  markAsUnmodified();
231  }
232  }
233  };
234 }
Abstract base class (for control problems)
std::size_t getNumberOfVariables() const
Get total number of variables in the solution vector.
Definition: solution.h:109
#define HUMOTO_LOCAL
Definition: export_import.h:26
Analog of &#39;sol_structure&#39; struct in Octave code. [determine_solution_structure.m].
Definition: solution.h:33
void form(const humoto::SolutionStructure &sol_structure, const humoto::Model &model_base, const humoto::ControlProblem &control_problem)
Form task.
Definition: task_generic.h:78
void form(const humoto::SolutionStructure &sol_structure, const humoto::Model &model_base, const humoto::ControlProblem &control_problem)
Form task.
Definition: task_generic.h:164
std::size_t length_
Definition: utility.h:150
Eigen::Matrix< unsigned int, Eigen::Dynamic, 1 > IndexVector
Definition: utility.h:19
humoto::IndexVector variables_indices_
Definition: task_generic.h:136
Same as humoto::TaskZeroVariables, but the variables need not to be continuous.
Definition: task_generic.h:127
Infeasible inequality task for testing purposes.
Definition: task_generic.h:180
Task: lb <= A*x <= ub.
Definition: task.h:536
[task_zerovars.m] Set given variables to zero, i.e. minimize these variables.
Definition: task_generic.h:26
void set(const std::size_t offset, const std::size_t length)
Set location.
Definition: utility.h:243
Location getSolutionPartLocation(const std::string &id) const
Get location of a data in the solution vector.
Definition: solution.h:122
void form(const humoto::SolutionStructure &sol_structure, const humoto::Model &model_base, const humoto::ControlProblem &control_problem)
Form task.
Definition: task_generic.h:208
The root namespace of HuMoTo.
Definition: config.h:12
TaskInfeasibleInequality(const double gain=2.0, const std::string &task_id="Infeasible_Inequality_Task")
Constructor.
Definition: task_generic.h:192
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
Task: diag(G)*x[I] = 0.
Definition: task.h:568
void setVariablesID(const std::string &variables_id)
Set variables string id to a given value.
Definition: task_generic.h:46
TaskZeroSelectedVariables(const humoto::IndexVector &var_indices, const double gain=2.0, const std::string &task_id="Minimize_Selected_Variables")
Constructor.
Definition: task_generic.h:147
Location of a data chunk (offset + length).
Definition: utility.h:146
TaskZeroVariables(const double gain=2.0, const std::string &task_id="Minimize_All_Variables", const std::string &variables_id="")
Constructor.
Definition: task_generic.h:59
std::size_t offset_
Definition: utility.h:149