humoto
configurable_optimization_problem.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Jan Michalczyk
4  @author Alexander Sherikov
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 
14 namespace humoto
15 {
16  /**
17  * @brief Class representing the hierarchy of the problem
18  */
22  {
23  #define HUMOTO_CONFIG_SECTION_ID "OptimizationProblem"
24  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
25 
26 
27  public:
28  /**
29  * @brief Default constructor
30  */
32  {
33  setDefaults();
34  }
35 
36 
37  /**
38  * @brief Push task to the hierarchy
39  *
40  * @param[in,out] task_pointer task
41  * @param[in] level_index level index
42  * @param[in] task_class_name name of the task class (not necessarily the same as task description!)
43  */
44  void pushTask( TaskSharedPointer task_pointer,
45  const std::size_t level_index,
46  const std::string &task_class_name)
47  {
48  humoto::OptimizationProblem::pushTask(task_pointer, level_index);
49 
50  task_class_names_[level_index].push_back(task_class_name);
51  task_ids_[level_index].push_back(task_pointer->getDescription());
52  }
53 
54 
55  /**
56  * @brief Reset the optimization problem
57  *
58  * @param[in] number_of_levels new number of levels
59  */
60  void reset( const std::size_t number_of_levels)
61  {
62  humoto::OptimizationProblem::reset(number_of_levels);
63 
64  task_class_names_.clear();
65  task_ids_.clear();
66 
67  task_class_names_.resize(number_of_levels);
68  task_ids_.resize(number_of_levels);
69  }
70 
71 
72  protected:
73  std::vector<std::vector<std::string> > task_class_names_;
74  std::vector<std::vector<std::string> > task_ids_;
75 
76 
77  protected:
79 
80 
81  /**
82  * @brief Initialize to default values
83  */
84  void setDefaults()
85  {
86  task_class_names_.clear();
87  }
88 
89 
90  /**
91  * @brief Fill map with all pointers to all tasks for given module
92  */
93  virtual humoto::TaskSharedPointer getTask(const std::string &string_id) const
94  {
95  if (string_id == "TaskInfeasibleInequality")
96  {
98  }
99  if (string_id == "TaskZeroVariables")
100  {
102  }
103  if (string_id == "TaskZeroSelectedVariables")
104  {
105  humoto::IndexVector index_vector;
107  }
108 
109  return (humoto::TaskSharedPointer());
110  }
111 
112 
113 
114  private:
115  /**
116  * @brief Read config entries
117  *
118  * @param[in] reader
119  * @param[in] crash_on_missing_entry
120  */
121  template <class t_Reader>
122  void readConfigEntriesTemplate( t_Reader& reader,
123  const bool crash_on_missing_entry = true)
124  {
125  HUMOTO_CONFIG_READ_COMPOUND_(task_class_names)
127 
128 
129  if(task_class_names_.empty())
130  {
131  HUMOTO_THROW_MSG("Enabled tasks entry empty.");
132  }
133 
134  if (task_class_names_.size() != task_ids_.size())
135  {
136  HUMOTO_THROW_MSG("Fields 'task_class_names' and 'task_ids' do not match.");
137  }
138 
139 
140  // reset the optimization problem
141  humoto::OptimizationProblem::reset(task_class_names_.size());
142 
143  for(std::size_t i = 0; i < task_class_names_.size(); ++i)
144  {
145  if (task_class_names_[i].size() != task_ids_[i].size())
146  {
147  HUMOTO_THROW_MSG("Fields 'task_class_names' and 'task_ids' do not match.");
148  }
149 
150  for(std::size_t j = 0; j < task_class_names_[i].size(); ++j)
151  {
152  humoto::TaskSharedPointer task = getTask(task_class_names_[i][j]);
153  task->setDescription(task_ids_[i][j]);
154 
155  if (task)
156  {
157  // configure tasks
158  task->readConfig(reader, task_ids_[i][j], crash_on_missing_entry);
159  // push tasks into the stack/hierarchy
161  }
162  else
163  {
164  HUMOTO_THROW_MSG(std::string("Unknown task name '") + task_class_names_[i][j] + "'.");
165  }
166  }
167  }
168  finalize();
169  }
170 
171 
172  /**
173  * @brief Write config entries
174  *
175  * @param[in] writer
176  */
177  template <class t_Writer>
178  void writeConfigEntriesTemplate(t_Writer& writer) const
179  {
180  HUMOTO_CONFIG_WRITE_COMPOUND_(task_class_names)
182 
183  if(task_class_names_.empty())
184  {
185  HUMOTO_THROW_MSG("Enabled tasks entry empty.");
186  }
187 
188  for(std::size_t i = 0; i < getNumberOfLevels(); ++i)
189  {
190  for ( std::list<humoto::HierarchyLevel::TaskInfo>::const_iterator it
191  = hierarchy_[i].tasks_.begin();
192  it != hierarchy_[i].tasks_.end();
193  ++it)
194  {
195  it->ptr_->writeNestedConfig(writer);
196  }
197  }
198  }
199 
200 
201  /**
202  * @brief Count entries
203  *
204  * @return number of entries
205  */
206  std::size_t getNumberOfEntries() const
207  {
208  std::size_t num_entries = 2; // task_class_names + task_ids
209 
210  for(std::size_t i = 0; i < getNumberOfLevels(); ++i)
211  {
212  num_entries += hierarchy_[i].tasks_.size();
213  }
214 
215  return (num_entries);
216  }
217  };
218 }
std::vector< std::vector< std::string > > task_ids_
#define HUMOTO_LOCAL
Definition: export_import.h:26
void setDefaults()
Initialize to default values.
#define HUMOTO_CONFIG_READ_COMPOUND_(entry)
Definition: config.h:41
void reset(const std::size_t number_of_levels)
Reset the optimization problem.
Definition: hierarchy.h:238
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
virtual humoto::TaskSharedPointer getTask(const std::string &string_id) const
Fill map with all pointers to all tasks for given module.
Eigen::Matrix< unsigned int, Eigen::Dynamic, 1 > IndexVector
Definition: utility.h:19
An optimization problem [initialize_stack.m, simulation_loop.m].
Definition: hierarchy.h:20
void readConfigEntriesTemplate(t_Reader &reader, const bool crash_on_missing_entry=true)
Read config entries.
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
boost::shared_ptr< humoto::TaskBase > TaskSharedPointer
Definition: task.h:584
void writeConfigEntriesTemplate(t_Writer &writer) const
Write config entries.
[task_zerovars.m] Set given variables to zero, i.e. minimize these variables.
Definition: task_generic.h:26
The root namespace of HuMoTo.
Definition: config.h:12
void reset(const std::size_t number_of_levels)
Reset the optimization problem.
void pushTask(TaskSharedPointer task_pointer, const std::size_t level_index, const std::string &task_class_name)
Push task to the hierarchy.
Class representing the hierarchy of the problem.
std::vector< std::vector< std::string > > task_class_names_
#define HUMOTO_CONFIG_WRITE_COMPOUND_(entry)
Definition: config.h:28
void pushTask(TaskSharedPointer task_pointer, const std::size_t level_index)
Add task to the optimization problem.
Definition: hierarchy.h:91