27 int CMultiObjectiveMotionOptimizerBase::decide(
28 const std::vector<mrpt::nav::TCandidateMovementPTG>& movs,
32 score_values.resize(movs.size());
35 for (
unsigned int mov_idx = 0; mov_idx < movs.size(); ++mov_idx)
37 const auto& m = movs[mov_idx];
42 p.second = std::numeric_limits<double>::quiet_NaN();
45 for (
const auto& prop : m.props)
65 catch (std::exception&)
78 "Error: Expression name `%s` already exists as an " 84 std::numeric_limits<double>::quiet_NaN();
96 for (
size_t i = 0; i < N; i++)
105 catch (std::exception&)
124 val = sc.second.eval();
130 "Undefined value evaluating score `%s` for mov_idx=%u!",
131 sc.first.c_str(), mov_idx);
135 score_values[mov_idx][sc.first] =
val;
143 double maxScore = .0;
144 for (
const auto&
s : score_values)
146 const auto it =
s.find(sScoreName);
154 for (
auto&
s : score_values)
156 auto it =
s.find(sScoreName);
163 else if (maxScore > 0 && maxScore != 1.0 )
165 double K = 1.0 / maxScore;
166 for (
auto&
s : score_values)
168 auto it =
s.find(sScoreName);
178 for (
unsigned int mov_idx = 0; mov_idx < movs.size(); ++mov_idx)
180 const auto& m = movs[mov_idx];
184 p.second = std::numeric_limits<double>::quiet_NaN();
186 for (
const auto& prop : m.props)
192 bool assert_failed =
false;
196 const double val = ma.eval();
199 assert_failed =
true;
202 "[CMultiObjectiveMotionOptimizerBase] " 203 "mov_idx=%u ASSERT failed: `%s`",
204 mov_idx, ma.get_original_expression().c_str()));
211 for (
auto& e : score_values[mov_idx])
234 if (!classId)
return nullptr;
237 dynamic_cast<CMultiObjectiveMotionOptimizerBase*>(
249 formula_score[
"collision_free_distance"] =
"collision_free_distance";
251 "var dif:=abs(target_k-move_k); if (dif>(num_paths/2)) { " 252 "dif:=num_paths-dif; }; exp(-abs(dif / (num_paths/10.0)));";
254 "(ref_dist - dist_eucl_final) / ref_dist";
267 formula_score.clear();
276 const bool none = (sName.empty() && sValue.empty());
277 const bool both = (!sName.empty() && !sValue.empty());
279 if (none && idx == 1)
281 "Expect at least a first `%s` and `%s` pair defining one " 282 "score in section `[%s]`",
283 sKeyName.c_str(), sKeyValue.c_str(),
s.c_str());
290 "Both `%s` and `%s` must be provided in section `[%s]`",
291 sKeyName.c_str(), sKeyValue.c_str(),
s.c_str());
294 formula_score[sName] = sValue;
300 movement_assert.clear();
306 if (sValue.empty())
break;
307 movement_assert.push_back(sValue);
312 scores_to_normalize.clear();
313 std::string sLst =
c.read_string(
s,
"scores_to_normalize",
"");
331 "# Next follows a list of `score%i_{name,formula}` pairs for " 333 "# Each one defines one of the scores that will be evaluated for " 334 "each candidate movement.\n" 335 "# Multiobjective optimizers will then use those scores to select " 336 "the best candidate, \n" 337 "# possibly using more parameters that follow below.\n";
338 c.write(
s,
"dummy",
"", WN, WV, sComment);
341 for (
const auto&
p : this->formula_score)
346 c.write(
s, sKeyName,
p.first, WN, WV);
347 c.write(
s, sKeyValue,
p.second, WN, WV);
355 "# Next follows a list of `movement_assert%i` exprtk expressions " 357 "# defining expressions for conditions that any candidate movement " 359 "# in order to get through the evaluation process. *All* assert " 360 "conditions must be satisfied.\n";
361 c.write(
s,
"dummy2",
"", WN, WV, sComment);
363 for (
unsigned int idx = 0; idx < movement_assert.size(); idx++)
366 c.write(
s, sKey, movement_assert[idx], WN, WV);
372 for (
const auto&
s : scores_to_normalize)
377 c.write(
s,
"scores_to_normalize", sLst);
std::map< std::string, mrpt::math::CRuntimeCompiledExpression > m_score_exprs
score names -> score compiled expressions
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::vector< std::string > log_entries
Optionally, debug logging info will be stored here by the implementor classes.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
std::vector< mrpt::math::CRuntimeCompiledExpression > m_movement_assert_exprs
std::map< std::string, double > m_expr_vars
void registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
#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.
std::shared_ptr< CMultiObjectiveMotionOptimizerBase > Ptr
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...
IMPLEMENTS_VIRTUAL_MRPT_OBJECT(CMultiObjectiveMotionOptimizerBase, CObject, mrpt::nav) CMultiObjectiveMotionOptimizerBase
mrpt::utils::CObject * createObject() const
int MRPT_SAVE_NAME_PADDING()
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
static CMultiObjectiveMotionOptimizerBase::Ptr Factory(const std::string &className) noexcept
Class factory from C++ class name.
virtual void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string §ion) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
GLsizei const GLchar ** string
std::map< std::string, std::string > formula_score
A list of name -> mathematical expression (in the format of the exprtk library) for the list of "scor...
void tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
Virtual base class for multi-objective motion choosers, as used for reactive navigation engines...
std::vector< std::string > scores_to_normalize
List of score names (as defined in the key of formula_score) that must be normalized across all candi...
std::vector< std::string > movement_assert
A list of exprtk expressions for conditions that any candidate movement must fulfill in order to get ...
TParamsBase & m_params_base
A structure that holds runtime class type information.
The virtual base class of all MRPT classes with a unified RTTI system.
CMultiObjectiveMotionOptimizerBase(TParamsBase ¶ms)
GLenum const GLfloat * params
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
int MRPT_SAVE_VALUE_PADDING()
virtual int impl_decide(const std::vector< mrpt::nav::TCandidateMovementPTG > &movs, TResultInfo &extra_info)=0
virtual void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string §ion) override
This method load the options from a ".ini"-like file or memory-stored string list.