MRPT  2.0.1
CMultiObjMotionOpt_Scalarization.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "nav-precomp.h" // Precomp header
11 
13 
14 using namespace mrpt::nav;
15 
18  mrpt::nav)
19 
22 {
23 }
24 
26 {
29  m_expr_scalar_vars.clear();
30 }
31 
34 {
36  .CMultiObjectiveMotionOptimizerBase::TParamsBase::loadFromConfigFile(
37  c, "CMultiObjectiveMotionOptimizerBase"); // call base-member with
38  // special argument
39  parameters.loadFromConfigFile(c, "CMultiObjMotionOpt_Scalarization");
40 }
41 
44 {
46  .CMultiObjectiveMotionOptimizerBase::TParamsBase::saveToConfigFile(
47  c, "CMultiObjectiveMotionOptimizerBase"); // call base-member with
48  // special argument
49  parameters.saveToConfigFile(c, "CMultiObjMotionOpt_Scalarization");
50 }
51 
53  const std::vector<mrpt::nav::TCandidateMovementPTG>& movs,
54  TResultInfo& extra_info)
55 {
56  std::vector<double>& final_evaluation = extra_info.final_evaluation;
57  final_evaluation.clear();
58 
59  if (extra_info.score_values.empty()) return -1; // No valid candidate
60 
61  // compile expression upon first use:
62  if (m_expr_scalar_vars.empty())
63  {
64  // vars:
65  for (const auto& c : extra_info.score_values)
66  {
67  for (const auto& score : c)
68  {
69  // double & var =
70  m_expr_scalar_vars[score.first]; // create or reuse placeholder
71  }
72  }
73 
74  // formula:
76  // Compile user-given expressions:
77  try
78  {
81  }
82  catch (std::exception&)
83  {
84  m_expr_scalar_vars.clear();
85  throw; // rethrow
86  }
87  }
88 
89  // Evaluate the formula for all candidates:
90  const size_t N = extra_info.score_values.size();
91  final_evaluation.assign(N, .0);
92  int best_idx = -1;
93  double best_val = .0;
94  for (size_t i = 0; i < N; i++)
95  {
96  if (extra_info.score_values[i].empty())
97  continue; // this candidate is non-eligible.
98 
99  // Update variables & evaluate formula:
100  for (const auto& score : extra_info.score_values[i])
101  {
102  const auto& it = m_expr_scalar_vars.find(
103  score.first); // this should never *create* a new entry...
104  if (it == m_expr_scalar_vars.cend())
105  {
107  "Error: found unexpected (unregistered) score named `%s`.",
108  score.first.c_str());
109  }
110  double& var = it->second;
111  var = score.second;
112  }
113  const double val = m_expr_scalar_formula.eval();
114  extra_info.final_evaluation[i] = val;
115 
116  if (val > 0 && (best_idx == -1 || val > best_val))
117  {
118  best_idx = i;
119  best_val = val;
120  }
121  }
122 
123  return best_idx;
124 }
125 
128  const mrpt::config::CConfigFileBase& c, const std::string& s)
129 {
130  // TParamsBase members are already dealt elsewhere.
132 }
133 
135  mrpt::config::CConfigFileBase& c, const std::string& s) const
136 {
137  // TParamsBase members are already dealt elsewhere.
139  scalar_score_formula,
140  "A formula that takes all/a subset of scores and generates a scalar "
141  "global score.");
142 }
void loadConfigFile(const mrpt::config::CConfigFileBase &c) override
std::vector< double > final_evaluation
The final evaluation score for each candidate.
void loadFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
virtual void clear()
Resets the object state; use if the parameters change, so they are re-read and applied.
void saveToConfigFile(mrpt::config::CConfigFileBase &cfg, const std::string &section) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
IMPLEMENTS_MRPT_OBJECT(CMultiObjMotionOpt_Scalarization, CMultiObjectiveMotionOptimizerBase, mrpt::nav) CMultiObjMotionOpt_Scalarization
int impl_decide(const std::vector< mrpt::nav::TCandidateMovementPTG > &movs, TResultInfo &extra_info) override
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...
Implementation of multi-objective motion chooser using scalarization: a user-given formula is used to...
int val
Definition: mrpt_jpeglib.h:957
mrpt::expr::CRuntimeCompiledExpression m_expr_scalar_formula
void compile(const std::string &expression, const std::map< std::string, double > &variables=std::map< std::string, double >(), const std::string &expr_name_for_error_reporting=std::string())
Initializes the object by compiling an expression.
Virtual base class for multi-objective motion choosers, as used for reactive navigation engines...
double eval() const
Evaluates the current value of the precompiled formula.
#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 ...
std::string scalar_score_formula
A formula that takes all/a subset of scores and generates a scalar global score.
#define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
void saveConfigFile(mrpt::config::CConfigFileBase &c) const override
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:183



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020