Main MRPT website > C++ reference for MRPT 1.9.9
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 
19  mrpt::nav)
20 
23 {
24 }
25 
27 {
29  m_expr_scalar_formula = mrpt::math::CRuntimeCompiledExpression();
30  m_expr_scalar_vars.clear();
31 }
32 
35 {
37  .CMultiObjectiveMotionOptimizerBase::TParamsBase::loadFromConfigFile(
38  c, "CMultiObjectiveMotionOptimizerBase"); // call base-member with
39  // special argument
40  parameters.loadFromConfigFile(c, "CMultiObjMotionOpt_Scalarization");
41 }
42 
45 {
47  .CMultiObjectiveMotionOptimizerBase::TParamsBase::saveToConfigFile(
48  c, "CMultiObjectiveMotionOptimizerBase"); // call base-member with
49  // special argument
50  parameters.saveToConfigFile(c, "CMultiObjMotionOpt_Scalarization");
51 }
52 
54  const std::vector<mrpt::nav::TCandidateMovementPTG>& movs,
55  TResultInfo& extra_info)
56 {
57  std::vector<double>& final_evaluation = extra_info.final_evaluation;
58  final_evaluation.clear();
59 
60  if (extra_info.score_values.empty()) return -1; // No valid candidate
61 
62  // compile expression upon first use:
63  if (m_expr_scalar_vars.empty())
64  {
65  // vars:
66  for (const auto& c : extra_info.score_values)
67  {
68  for (const auto& score : c)
69  {
70  // double & var =
71  m_expr_scalar_vars[score.first]; // create or reuse placeholder
72  }
73  }
74 
75  // formula:
76  m_expr_scalar_formula = mrpt::math::CRuntimeCompiledExpression();
77  // Compile user-given expressions:
78  try
79  {
80  m_expr_scalar_formula.compile(
82  }
83  catch (std::exception&)
84  {
85  m_expr_scalar_vars.clear();
86  throw; // rethrow
87  }
88  }
89 
90  // Evaluate the formula for all candidates:
91  const size_t N = extra_info.score_values.size();
92  final_evaluation.assign(N, .0);
93  int best_idx = -1;
94  double best_val = .0;
95  for (size_t i = 0; i < N; i++)
96  {
97  if (extra_info.score_values[i].empty())
98  continue; // this candidate is non-eligible.
99 
100  // Update variables & evaluate formula:
101  for (const auto& score : extra_info.score_values[i])
102  {
103  const auto& it = m_expr_scalar_vars.find(
104  score.first); // this should never *create* a new entry...
105  if (it == m_expr_scalar_vars.cend())
106  {
108  "Error: found unexpected (unregistered) score named `%s`.",
109  score.first.c_str());
110  }
111  double& var = it->second;
112  var = score.second;
113  }
114  const double val = m_expr_scalar_formula.eval();
115  extra_info.final_evaluation[i] = val;
116 
117  if (val > 0 && (best_idx == -1 || val > best_val))
118  {
119  best_idx = i;
120  best_val = val;
121  }
122  }
123 
124  return best_idx;
125 }
126 
130 {
131  // TParamsBase members are already dealt elsewhere.
132  MRPT_LOAD_CONFIG_VAR_REQUIRED_CS(scalar_score_formula, string);
133 }
134 
137 {
138  // TParamsBase members are already dealt elsewhere.
140  scalar_score_formula,
141  "A formula that takes all/a subset of scores and generates a scalar "
142  "global score.");
143 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#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::vector< double > final_evaluation
The final evaluation score for each candidate.
#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.
IMPLEMENTS_MRPT_OBJECT(CMultiObjMotionOpt_Scalarization, CMultiObjectiveMotionOptimizerBase, mrpt::nav) CMultiObjMotionOpt_Scalarization
GLdouble s
Definition: glext.h:3676
int impl_decide(const std::vector< mrpt::nav::TCandidateMovementPTG > &movs, TResultInfo &extra_info) override
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:189
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...
#define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment)
const GLubyte * c
Definition: glext.h:6313
Implementation of multi-objective motion chooser using scalarization: a user-given formula is used to...
int val
Definition: mrpt_jpeglib.h:955
void saveConfigFile(mrpt::utils::CConfigFileBase &c) const override
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
GLsizei const GLchar ** string
Definition: glext.h:4101
void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string &section) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
Virtual base class for multi-objective motion choosers, as used for reactive navigation engines...
void loadConfigFile(const mrpt::utils::CConfigFileBase &c) override
std::string scalar_score_formula
A formula that takes all/a subset of scores and generates a scalar global score.
mrpt::math::CRuntimeCompiledExpression m_expr_scalar_formula



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019