humoto
solution.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 Return status of a solver
16  */
18  {
19  public:
20  enum Status
21  {
22  UNDEFINED = 0,
23  OK = 1,
24  MAX_ITER = 2,
25  OTHER = 3
26  };
27  };
28 
29 
30  /**
31  * @brief Analog of 'sol_structure' struct in Octave code. [determine_solution_structure.m]
32  */
34  {
35  protected:
36  std::size_t number_of_variables_;
37  std::map<std::string, Location> variable_map_;
38 
39 
40  public:
41  /**
42  * @brief Virtual destructor
43  */
44  virtual ~SolutionStructure() {}
45 
46 
47  /**
48  * @brief Default constructor
49  */
51  {
52  reset();
53  }
54 
55 
56  /**
57  * @brief Reset
58  */
59  void reset()
60  {
61  number_of_variables_ = 0;
62  variable_map_.clear();
63  }
64 
65 
66  /**
67  * @brief Add part of the solution
68  *
69  * @param[in] id string id of the solution part
70  * @param[in] length length of the solution part
71  */
72  void addSolutionPart( const std::string &id,
73  const std::size_t length)
74  {
75  Location location(number_of_variables_, length);
76 
77  variable_map_.insert(std::pair< std::string, Location > (id, location));
78 
79  number_of_variables_ += length;
80  }
81 
82 
83  /**
84  * @brief Checks if the structure is empty or not.
85  *
86  * @return true / false
87  */
88  bool isNonEmpty() const
89  {
90  if (variable_map_.size() == 0)
91  {
92  return (false);
93  }
94  else
95  {
96  return (true);
97  }
98  }
99 
100 
101  /**
102  * @brief Get total number of variables in the solution vector.
103  *
104  * @return number of variables.
105  *
106  * @attention This method should be called only after addition of
107  * all solution parts.
108  */
109  std::size_t getNumberOfVariables() const
110  {
111  return (number_of_variables_);
112  }
113 
114 
115  /**
116  * @brief Get location of a data in the solution vector
117  *
118  * @param[in] id id of the data block
119  *
120  * @return Location
121  */
122  Location getSolutionPartLocation(const std::string &id) const
123  {
124  std::map<std::string, Location>::const_iterator it;
125 
126  it = variable_map_.find(id);
127 
128  if (it == variable_map_.end())
129  {
130  return (Location(0,0));
131  }
132  else
133  {
134  return( it->second );
135  }
136  }
137 
138 
139  /**
140  * @brief Get part of a matrix corresponding to given part of the solution.
141  *
142  * @param[in] id id of the data block
143  * @param[in] matrix matrix
144  *
145  * @return
146  */
147  Eigen::Block<Eigen::MatrixXd> getMatrixPart(const std::string &id, Eigen::MatrixXd & matrix) const
148  {
149  Location loc_var = getSolutionPartLocation(id);
150 
151  return (matrix.block(0, loc_var.offset_, matrix.rows(), loc_var.length_));
152  }
153 
154 
155  /**
156  * @brief Get part of a matrix corresponding to given part of the solution.
157  *
158  * @param[in] id id of the data block
159  * @param[in] matrix matrix
160  *
161  * @return
162  */
163  const Eigen::Block<const Eigen::MatrixXd> getMatrixPart(const std::string &id, const Eigen::MatrixXd & matrix) const
164  {
165  Location loc_var = getSolutionPartLocation(id);
166 
167  return (matrix.block(0, loc_var.offset_, matrix.rows(), loc_var.length_));
168  }
169  };
170 
171 
172 
173  /**
174  * @brief Container of the solution
175  */
177  {
178  #define HUMOTO_CONFIG_SECTION_ID "Solution"
179  #define HUMOTO_CONFIG_CONSTRUCTOR Solution
180  #define HUMOTO_CONFIG_ENTRIES \
181  HUMOTO_CONFIG_COMPOUND_(x)
182  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
183 
184 
185  public:
186  Eigen::VectorXd x_;
188 
189 
190  public:
192  {
193  return_status_ = SolverStatus::UNDEFINED;
194  }
195 
196 
197  /**
198  * @brief Virtual destructor
199  */
200  virtual ~Solution() {}
201 
202 
203  /**
204  * @brief No defaults
205  */
206  void setDefaults() {}
207 
208 
209  /**
210  * @brief Initialize the solution vector
211  *
212  * @param[in] sol_structure structure of the solution
213  */
214  void initialize(const SolutionStructure &sol_structure)
215  {
216  reset();
217  SolutionStructure::operator=(sol_structure);
218  x_.resize(getNumberOfVariables());
219  }
220 
221 
222 
223  /**
224  * @brief Initialize the solution vector
225  *
226  * @param[in] sol_structure structure of the solution
227  * @param[in] value
228  */
229  void initialize(const SolutionStructure &sol_structure,
230  const double value)
231  {
232  reset();
233  SolutionStructure::operator=(sol_structure);
234  if (value == 0.0)
235  {
236  x_.setZero(getNumberOfVariables());
237  }
238  else
239  {
240  x_.setConstant(getNumberOfVariables(), value);
241  }
242  }
243 
244 
245  /**
246  * @brief Initialize the solution vector using an old solution
247  *
248  * @param[in] sol_structure structure of the solution
249  * @param[in] old_solution old solution
250  */
251  void initialize(const SolutionStructure &sol_structure,
252  const Solution &old_solution)
253  {
254  initialize(sol_structure, 0.0);
255 
256 
257  for (std::map<std::string, Location>::const_iterator i = variable_map_.begin();
258  i != variable_map_.end();
259  ++i)
260  {
261  Location old_part = old_solution.getSolutionPartLocation(i->first);
262  Location guess_part = i->second;
263 
264 
265  if (old_part.length_ == guess_part.length_)
266  {
267  getData(guess_part) = old_solution.getData(old_part);
268  }
269  }
270  }
271 
272 
273 
274  /**
275  * @brief Get part of the solution by its id
276  *
277  * @param[in] id id of the solution part
278  *
279  * @return part of the solution (not a copy!)
280  */
281  Eigen::VectorBlock<Eigen::VectorXd> getSolutionPart(const std::string &id)
282  {
283  return ( getData( getSolutionPartLocation(id) ) );
284  }
285 
286 
287  /**
288  * @brief Get data from solution vector.
289  *
290  * @param[in] location location of the data
291  *
292  * @return data (not a copy!)
293  */
294  Eigen::VectorBlock<Eigen::VectorXd> getData(const Location &location)
295  {
296  HUMOTO_ASSERT(location.checkLength(x_.rows()), "Incorrect data length or offset.")
297 
298  return (x_.segment(location.offset_, location.length_));
299  }
300 
301 
302  /**
303  * @brief Get part of the solution by its id
304  *
305  * @param[in] id id of the solution part
306  *
307  * @return part of the solution
308  */
309  Eigen::VectorXd getSolutionPart(const std::string &id) const
310  {
311  return ( getData( getSolutionPartLocation(id) ) );
312  }
313 
314 
315 
316  /**
317  * @brief Get data from solution vector.
318  *
319  * @param[in] location location of the data
320  *
321  * @return data
322  */
323  Eigen::VectorXd getData(const Location &location) const
324  {
325  HUMOTO_ASSERT(location.checkLength(x_.rows()), "Incorrect data length or offset.")
326 
327  return (x_.segment(location.offset_, location.length_));
328  }
329 
330 
331 
332  /**
333  * @brief Returns solution vector
334  *
335  * @return Solution vector (not a copy!)
336  */
337  const Eigen::VectorXd & get_x() const
338  {
339  return(x_);
340  }
341 
342 
343 
344  /**
345  * @brief Log a QP problem
346  *
347  * @param[in,out] logger logger
348  * @param[in] parent parent
349  * @param[in] name name
350  */
352  const LogEntryName & parent = LogEntryName(),
353  const std::string & name = "solution") const
354  {
355  LogEntryName subname = parent;
356  subname.add(name);
357 
358  logger.log(LogEntryName(subname).add("x"), x_);
359  logger.log(LogEntryName(subname).add("status"), return_status_);
360  }
361 
362 
363 
364  /**
365  * @brief Status description
366  *
367  * @return description of the status if available
368  */
369  virtual std::string getStatusDescription() const
370  {
371  std::string description("Status description is not available.");
372  return (description);
373  }
374  };
375 }
std::size_t getNumberOfVariables() const
Get total number of variables in the solution vector.
Definition: solution.h:109
virtual ~SolutionStructure()
Virtual destructor.
Definition: solution.h:44
void setDefaults()
No defaults.
Definition: solution.h:206
bool checkLength(const std::size_t max_length) const
Check length of location.
Definition: utility.h:208
#define HUMOTO_LOCAL
Definition: export_import.h:26
Eigen::Block< Eigen::MatrixXd > getMatrixPart(const std::string &id, Eigen::MatrixXd &matrix) const
Get part of a matrix corresponding to given part of the solution.
Definition: solution.h:147
Eigen::VectorBlock< Eigen::VectorXd > getSolutionPart(const std::string &id)
Get part of the solution by its id.
Definition: solution.h:281
Eigen::VectorXd x_
Definition: solution.h:186
#define HUMOTO_GLOBAL_LOGGER_IF_DEFINED
Definition: logger.h:997
#define HUMOTO_ASSERT(condition, message)
void initialize(const SolutionStructure &sol_structure, const double value)
Initialize the solution vector.
Definition: solution.h:229
SolutionStructure()
Default constructor.
Definition: solution.h:50
Eigen::VectorBlock< Eigen::VectorXd > getData(const Location &location)
Get data from solution vector.
Definition: solution.h:294
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
std::size_t number_of_variables_
Definition: solution.h:36
void reset()
Reset.
Definition: solution.h:59
Container of the solution.
Definition: solution.h:176
Represents log entry name.
Definition: logger.h:169
bool isNonEmpty() const
Checks if the structure is empty or not.
Definition: solution.h:88
std::size_t length_
Definition: utility.h:150
SolverStatus::Status return_status_
Definition: solution.h:187
void initialize(const SolutionStructure &sol_structure, const Solution &old_solution)
Initialize the solution vector using an old solution.
Definition: solution.h:251
const Eigen::Block< const Eigen::MatrixXd > getMatrixPart(const std::string &id, const Eigen::MatrixXd &matrix) const
Get part of a matrix corresponding to given part of the solution.
Definition: solution.h:163
virtual ~Solution()
Virtual destructor.
Definition: solution.h:200
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
void initialize(const SolutionStructure &sol_structure)
Initialize the solution vector.
Definition: solution.h:214
Return status of a solver.
Definition: solution.h:17
virtual void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="solution") const
Log a QP problem.
Definition: solution.h:351
Location getSolutionPartLocation(const std::string &id) const
Get location of a data in the solution vector.
Definition: solution.h:122
The root namespace of HuMoTo.
Definition: config.h:12
const Eigen::VectorXd & get_x() const
Returns solution vector.
Definition: solution.h:337
Eigen::VectorXd getSolutionPart(const std::string &id) const
Get part of the solution by its id.
Definition: solution.h:309
std::map< std::string, Location > variable_map_
Definition: solution.h:37
virtual std::string getStatusDescription() const
Status description.
Definition: solution.h:369
Eigen::VectorXd getData(const Location &location) const
Get data from solution vector.
Definition: solution.h:323
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
void addSolutionPart(const std::string &id, const std::size_t length)
Add part of the solution.
Definition: solution.h:72
std::size_t offset_
Definition: utility.h:149