Main MRPT website > C++ reference for MRPT 1.5.7
CMultiObjMotionOpt_Scalarization.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "nav-precomp.h" // Precomp header
11 
13 
14 using namespace mrpt::nav;
15 using namespace mrpt::utils;
16 
18 
21 {
22 }
23 
25 {
27  m_expr_scalar_formula = mrpt::math::CRuntimeCompiledExpression();
28  m_expr_scalar_vars.clear();
29 }
30 
32 {
33  parameters.CMultiObjectiveMotionOptimizerBase::TParamsBase::loadFromConfigFile(c,"CMultiObjectiveMotionOptimizerBase"); // call base-member with special argument
34  parameters.loadFromConfigFile(c, "CMultiObjMotionOpt_Scalarization");
35 }
36 
38 {
39  parameters.CMultiObjectiveMotionOptimizerBase::TParamsBase::saveToConfigFile(c, "CMultiObjectiveMotionOptimizerBase"); // call base-member with special argument
40  parameters.saveToConfigFile(c, "CMultiObjMotionOpt_Scalarization");
41 }
42 
43 int CMultiObjMotionOpt_Scalarization::impl_decide(const std::vector<mrpt::nav::TCandidateMovementPTG> &movs, TResultInfo &extra_info)
44 {
45  std::vector<double> & final_evaluation = extra_info.final_evaluation;
46  final_evaluation.clear();
47 
48  if (extra_info.score_values.empty())
49  return -1; // No valid candidate
50 
51  // compile expression upon first use:
52  if (m_expr_scalar_vars.empty())
53  {
54  // vars:
55  for (const auto &c : extra_info.score_values) {
56  for (const auto &score : c) {
57  //double & var =
58  m_expr_scalar_vars[score.first]; // create or reuse placeholder
59  }
60  }
61 
62  // formula:
63  m_expr_scalar_formula = mrpt::math::CRuntimeCompiledExpression();
64  // Compile user-given expressions:
65  try
66  {
67  m_expr_scalar_formula.compile(parameters.scalar_score_formula, m_expr_scalar_vars);
68  }
69  catch (std::exception &)
70  {
71  m_expr_scalar_vars.clear();
72  throw; // rethrow
73  }
74  }
75 
76  // Evaluate the formula for all candidates:
77  const size_t N = extra_info.score_values.size();
78  final_evaluation.assign(N, .0);
79  int best_idx = -1;
80  double best_val = .0;
81  for (size_t i = 0; i < N; i++)
82  {
83  if (extra_info.score_values[i].empty())
84  continue; // this candidate is non-eligible.
85 
86  // Update variables & evaluate formula:
87  for (const auto &score : extra_info.score_values[i]) {
88  const auto & it = m_expr_scalar_vars.find(score.first); // this should never *create* a new entry...
89  if (it == m_expr_scalar_vars.cend()) {
90  THROW_EXCEPTION_FMT("Error: found unexpected (unregistered) score named `%s`.", score.first.c_str());
91  }
92  double & var = it->second;
93  var = score.second;
94  }
95  const double val = m_expr_scalar_formula.eval();
96  extra_info.final_evaluation[i] = val;
97 
98  if (val > 0 && (best_idx == -1 || val > best_val))
99  {
100  best_idx = i;
101  best_val = val;
102  }
103  }
104 
105  return best_idx;
106 }
107 
109 {
110 }
111 
113 {
114  // TParamsBase members are already dealt elsewhere.
115  MRPT_LOAD_CONFIG_VAR_REQUIRED_CS(scalar_score_formula, string);
116 }
117 
119 {
120  // TParamsBase members are already dealt elsewhere.
121  MRPT_SAVE_CONFIG_VAR_COMMENT(scalar_score_formula, "A formula that takes all/a subset of scores and generates a scalar global score.");
122 }
123 
int impl_decide(const std::vector< mrpt::nav::TCandidateMovementPTG > &movs, TResultInfo &extra_info) MRPT_OVERRIDE
void saveConfigFile(mrpt::utils::CConfigFileBase &c) const MRPT_OVERRIDE
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define MRPT_LOAD_CONFIG_VAR_REQUIRED_CS(variableName, variableType)
Shortcut for MRPT_LOAD_CONFIG_VAR_NO_DEFAULT() for REQUIRED variables config file object named c and ...
#define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace)
This must be inserted in all CObject classes implementation files.
Definition: CObject.h:256
std::vector< double > final_evaluation
The final evaluation score for each candidate.
const GLfloat * c
Definition: glew.h:10088
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
virtual void clear()
Resets the object state; use if the parameters change, so they are re-read and applied.
virtual void clear() MRPT_OVERRIDE
Resets the object state; use if the parameters change, so they are re-read and applied.
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
std::vector< std::map< std::string, double > > score_values
For each candidate (vector indices), the numerical evaluation of all scores defined in TParamsBase::f...
This class allows loading and storing values and vectors of different types from a configuration text...
GLdouble s
Definition: glew.h:1295
#define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment)
Implementation of multi-objective motion chooser using scalarization: a user-given formula is used to...
void loadConfigFile(const mrpt::utils::CConfigFileBase &c) MRPT_OVERRIDE
Virtual base class for multi-objective motion choosers, as used for reactive navigation engines...
GLsizei const GLcharARB ** string
Definition: glew.h:3293
void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string &section) const MRPT_OVERRIDE
This method saves the options to a ".ini"-like file or memory-stored string list. ...
GLuint GLfloat * val
Definition: glew.h:7785
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section) MRPT_OVERRIDE
This method load the options from a ".ini"-like file or memory-stored string list.



Page generated by Doxygen 1.8.11 for MRPT 1.5.7 Git: 2190203 Tue May 15 02:01:15 2018 +0200 at miƩ may 16 12:40:16 CEST 2018