31 const
unsigned int INVALID_K =
std::numeric_limits<
unsigned int>::max();
36 m_last_selected_sector(
std::numeric_limits<
unsigned int>::max())
38 if (INI_FILE !=
nullptr) initialize(*INI_FILE);
43 options.saveToConfigFile(
c, getConfigFileSectionName());
48 options.loadFromConfigFile(
c, getConfigFileSectionName());
62 min_eval(
std::numeric_limits<double>::max()),
63 max_eval(-
std::numeric_limits<double>::max()),
64 contains_target_k(false),
70 CHolonomicFullEval::EvalOutput::EvalOutput() : best_k(
INVALID_K), best_eval(.0)
78 const auto target = ni.
targets[target_idx];
85 const double ptg_ref_dist = ptg ? ptg->getRefDistance() : 1.0;
88 const double target_dir = ::atan2(target.y, target.x);
89 const unsigned int target_k =
91 const double target_dist = target.norm();
96 std::vector<mrpt::math::TPoint2D> obstacles_2d(nDirs);
97 std::vector<double> k2dir(nDirs);
99 for (
unsigned int i = 0; i < nDirs; i++)
102 obstacles_2d[i].x = ni.
obstacles[i] * cos(k2dir[i]);
103 obstacles_2d[i].y = ni.
obstacles[i] * sin(k2dir[i]);
106 const int NUM_FACTORS = 5;
110 for (
unsigned int i = 0; i < nDirs; i++)
112 double scores[NUM_FACTORS];
121 for (
int l = 0; l < NUM_FACTORS; l++)
m_dirs_scores(i, l) = .0;
128 const double x = d * cos(k2dir[i]);
129 const double y = d * sin(k2dir[i]);
139 std::max(target_dist, ni.
obstacles[i]) / (target_dist * 1.05);
151 const double max_real_freespace =
152 ptg->getActualUnloopedPathLength(i);
153 const double max_real_freespace_norm =
154 max_real_freespace / ptg->getRefDistance();
169 double min_dist_target_along_path = sg.
distance(target);
175 const double endpt_dist_to_target = (target -
TPoint2D(
x,
y)).
norm();
176 const double endpt_dist_to_target_norm =
177 std::min(1.0, endpt_dist_to_target);
179 if ((endpt_dist_to_target_norm > target_dist &&
180 endpt_dist_to_target_norm >= 0.95 * target_dist) &&
181 min_dist_target_along_path >
184 endpt_dist_to_target_norm)
192 min_dist_target_along_path = sg.
distance(target);
195 scores[1] = 1.0 / (1.0 +
square(min_dist_target_along_path));
200 scores[2] = std::sqrt(
201 1.01 - endpt_dist_to_target_norm);
232 const double query_dist_norm =
std::min(0.99, target_dist * 0.95);
234 i , query_dist_norm,
true );
236 i , query_dist_norm,
false );
237 scores[4] = 0.5 * (avr_path_clearance + point_clearance);
241 for (
int l = 0; l < NUM_FACTORS; l++)
m_dirs_scores(i, l) = scores[l];
246 for (
int l = 0; l < NUM_FACTORS; l++)
253 if (
span <= .0)
continue;
264 std::vector<double> weights_sum_phase(NUM_PHASES, .0),
265 weights_sum_phase_inv(NUM_PHASES);
266 for (
unsigned int i = 0; i < NUM_PHASES; i++)
270 ASSERT_(weights_sum_phase[i] > .0);
271 weights_sum_phase_inv[i] = 1.0 / weights_sum_phase[i];
275 NUM_PHASES, std::vector<double>(nDirs, .0));
277 double last_phase_threshold = -1.0;
279 for (
unsigned int phase_idx = 0; phase_idx < NUM_PHASES; phase_idx++)
281 double phase_min = std::numeric_limits<double>::max(), phase_max = .0;
283 for (
unsigned int i = 0; i < nDirs; i++)
285 double this_dir_eval = 0;
290 phase_scores[phase_idx - 1][i] <
291 last_phase_threshold)
305 this_dir_eval *= weights_sum_phase_inv[phase_idx];
306 this_dir_eval = std::exp(this_dir_eval);
308 phase_scores[phase_idx][i] = this_dir_eval;
320 last_phase_threshold =
326 auto& dirs_eval = *phase_scores.rbegin();
333 double phase_min = std::numeric_limits<double>::max(), phase_max = .0;
334 for (
unsigned int i = 0; i < nDirs; i++)
339 last_phase_threshold =
349 std::vector<TGap> gaps;
350 int best_gap_idx = -1;
351 int gap_idx_for_target_dir = -1;
353 bool inside_gap =
false;
354 for (
unsigned int i = 0; i < nDirs; i++)
356 const double val = dirs_eval[i];
357 if (
val < last_phase_threshold)
362 auto& active_gap = *gaps.rbegin();
363 active_gap.k_to = i - 1;
376 gaps.emplace_back(new_gap);
383 auto& active_gap = *gaps.rbegin();
384 if (
val >= active_gap.max_eval)
386 active_gap.k_best_eval = i;
393 active_gap.contains_target_k =
true;
394 gap_idx_for_target_dir = gaps.size() - 1;
397 if (best_gap_idx == -1 ||
val > gaps[best_gap_idx].max_eval)
399 best_gap_idx = gaps.size() - 1;
407 auto& active_gap = *gaps.rbegin();
408 active_gap.k_to = nDirs - 1;
413 ASSERT_(best_gap_idx >= 0 && best_gap_idx <
int(gaps.size()));
415 const TGap& best_gap = gaps[best_gap_idx];
420 if (best_gap_idx == gap_idx_for_target_dir)
425 const auto cl_right =
428 const auto smallest_clearance_in_k_units =
std::min(cl_left, cl_right);
429 const unsigned int clearance_threshold =
432 const unsigned int gap_width = best_gap.
k_to - best_gap.
k_from;
433 const unsigned int width_threshold =
437 if (smallest_clearance_in_k_units >= clearance_threshold &&
438 gap_width >= width_threshold &&
439 ni.
obstacles[target_k] > target_dist * 1.01)
458 if (target_dist < 0.99 &&
461 (ni.
obstacles[target_k] > target_dist * 1.01 &&
463 target_k ,
std::min(0.99, target_dist * 0.95),
468 (target_dist + 0.15 / ptg_ref_dist) &&
469 target_dist < (1.5 / ptg_ref_dist))) &&
470 dirs_eval[target_k] >
478 phase_scores[NUM_PHASES - 1][target_k] += 2.0;
491 mrpt::make_aligned_shared<CLogFileRecord_FullEval>();
494 const size_t numTrgs = ni.
targets.size();
496 std::vector<EvalOutput> evals(numTrgs);
497 double best_eval = .0;
498 unsigned int best_trg_idx = 0;
500 for (
unsigned int trg_idx = 0; trg_idx < numTrgs; trg_idx++)
502 auto& eo = evals[trg_idx];
508 best_eval = eo.best_eval;
509 best_trg_idx = trg_idx;
524 const double ptg_ref_dist = ptg ? ptg->getRefDistance() : 1.0;
527 evals[best_trg_idx].best_k, ni.
obstacles.size());
531 const double targetNearnessFactor =
534 1.0, ni.
targets[best_trg_idx].norm() /
539 const double obs_dist =
540 ni.
obstacles[evals[best_trg_idx].best_k];
542 const double obs_dist_th = std::max(
546 double riskFactor = 1.0;
566 log->selectedTarget = best_trg_idx;
567 log->selectedSector = evals[best_trg_idx].best_k;
568 log->evaluation = evals[best_trg_idx].best_eval;
569 log->dirs_eval = evals[best_trg_idx].phase_scores;
579 const double a,
const unsigned int N)
585 return static_cast<unsigned int>(idx);
589 : selectedSector(0), evaluation(.0), dirs_scores(), selectedTarget(0)
652 TOO_CLOSE_OBSTACLE(0.15),
653 TARGET_SLOW_APPROACHING_DISTANCE(0.60),
654 OBSTACLE_SLOW_DOWN_DISTANCE(0.15),
655 HYSTERESIS_SECTOR_COUNT(5),
656 factorWeights{0.1, 0.5, 0.5, 0.01, 1},
657 factorNormalizeOrNot{0, 0, 0, 0, 1},
658 PHASE_FACTORS{{1, 2}, {4}, {0, 2}},
659 PHASE_THRESHOLDS{0.5, 0.6, 0.7},
660 LOG_SCORE_MATRIX(
false),
661 clearance_threshold_ratio(0.05),
662 gap_width_ratio_threshold(0.25)
681 s,
"factorWeights", std::vector<double>(), factorWeights,
true);
682 ASSERT_(factorWeights.size() == 5);
685 s,
"factorNormalizeOrNot", factorNormalizeOrNot, factorNormalizeOrNot);
686 ASSERT_(factorNormalizeOrNot.size() == factorWeights.size());
692 PHASE_FACTORS.resize(PHASE_COUNT);
693 PHASE_THRESHOLDS.resize(PHASE_COUNT);
694 for (
int i = 0; i < PHASE_COUNT; i++)
698 PHASE_FACTORS[i],
true);
701 PHASE_THRESHOLDS[i] =
c.read_double(
703 ASSERT_(PHASE_THRESHOLDS[i] >= .0 && PHASE_THRESHOLDS[i] <= 1.0);
719 "Directions with collision-free distances below this threshold are not " 722 TARGET_SLOW_APPROACHING_DISTANCE,
723 "Start to reduce speed when closer than this to target.");
725 OBSTACLE_SLOW_DOWN_DISTANCE,
726 "Start to reduce speed when clearance is below this value ([0,1] ratio " 727 "wrt obstacle reference/max distance)");
729 HYSTERESIS_SECTOR_COUNT,
730 "Range of `sectors` (directions) for hysteresis over successive " 733 LOG_SCORE_MATRIX,
"Save the entire score matrix in log files");
735 clearance_threshold_ratio,
736 "Ratio [0,1], times path_count, gives the minimum number of paths at " 737 "each side of a target direction to be accepted as desired direction");
739 gap_width_ratio_threshold,
740 "Ratio [0,1], times path_count, gives the minimum gap width to accept " 741 "a direct motion towards target.");
747 "[0]=Free space, [1]=Dist. in sectors, [2]=Closer to target " 748 "(Euclidean), [3]=Hysteresis, [4]=clearance along path");
750 s,
"factorNormalizeOrNot",
752 "Normalize factors or not (1/0)");
755 s,
"PHASE_COUNT", PHASE_FACTORS.size(), WN, WV,
756 "Number of evaluation phases to run (params for each phase below)");
758 for (
unsigned int i = 0; i < PHASE_FACTORS.size(); i++)
761 s,
mrpt::format(
"PHASE%u_THRESHOLD", i + 1), PHASE_THRESHOLDS[i],
763 "Phase scores must be above this relative range threshold [0,1] to " 764 "be considered in next phase (Default:`0.75`)");
768 "Indices of the factors above to be considered in this phase");
849 std::vector<double>& dir_evals,
const NavInput& ni,
unsigned int trg_idx)
#define ASSERT_EQUAL_(__A, __B)
double index2alpha(uint16_t k) const
Alpha value for the discrete corresponding value.
unsigned int direction2sector(const double a, const unsigned int N)
double gap_width_ratio_threshold
Ratio [0,1], times path_count, gives the minimum gap width to accept a direct motion towards target...
virtual void postProcessDirectionEvaluations(std::vector< double > &dir_evals, const NavInput &ni, unsigned int trg_idx)
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
int k_best_eval
Direction with the best evaluation inside the gap.
EIGEN_STRONG_INLINE bool empty() const
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
A class for storing extra information about the execution of CHolonomicFullEval navigation.
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
double TOO_CLOSE_OBSTACLE
Directions with collision-free distances below this threshold are not elegible.
double distance(const TPoint2D &point) const
Distance to point.
This file implements several operations that operate element-wise on individual or pairs of container...
A base class for holonomic reactive navigation methods.
double TARGET_SLOW_APPROACHING_DISTANCE
Start to reduce speed when closer than this to target [m].
mrpt::nav const unsigned int INVALID_K
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.
int32_t selectedTarget
Normally = 0.
std::vector< int32_t > factorNormalizeOrNot
0/1 to normalize factors.
IMPLEMENTS_SERIALIZABLE(CLogFileRecord_FullEval, CHolonomicLogFileRecord, mrpt::nav) IMPLEMENTS_SERIALIZABLE(CHolonomicFullEval
bool m_enableApproachTargetSlowDown
Whether to decrease speed when approaching target.
double clearance_threshold_ratio
Ratio [0,1], times path_count, gives the minimum number of paths at each side of a target direction t...
CLogFileRecord_FullEval()
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
std::vector< double > PHASE_THRESHOLDS
Phase 1,2,N-1...
This class allows loading and storing values and vectors of different types from a configuration text...
T square(const T x)
Inline function for the square of a number.
TOptions options
Parameters of the algorithm (can be set manually or loaded from CHolonomicFullEval::initialize or opt...
double OBSTACLE_SLOW_DOWN_DISTANCE
Start to reduce speed when clearance is below this value ([0,1] ratio wrt obstacle reference/max dist...
double getClearance(uint16_t k, double TPS_query_distance, bool integrate_over_path) const
Gets the clearance for path k and distance TPS_query_distance in one of two modes: ...
int mmin(const int t1, const int t2)
int32_t selectedSector
Member data.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
This base provides a set of functions for maths stuff.
2D segment, consisting of two points.
int MRPT_SAVE_NAME_PADDING()
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
#define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment)
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
A base class for log records for different holonomic navigation methods.
double desiredDirection
The desired motion direction, in the range [-PI, PI].
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. ...
std::vector< double > factorWeights
See docs above.
std::shared_ptr< CLogFileRecord_FullEval > Ptr
void evalSingleTarget(unsigned int target_idx, const NavInput &ni, EvalOutput &eo)
Evals one single target of the potentially many of them in NavInput.
CHolonomicLogFileRecord::Ptr logRecord
The navigation method will create a log record and store it here via a smart pointer.
TPoint2D point2
Destiny point.
GLsizei const GLchar ** string
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Full evaluation of all possible directions within the discrete set of input directions.
std::vector< std::vector< double > > dirs_eval
Final [0] and alternative [1..N] evaluation scores for each direction, in the same order of TP-Obstac...
TPoint2D point1
Origin point.
double desiredSpeed
The desired motion speed in that direction, from 0 up to NavInput::maxRobotSpeed. ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void navigate(const NavInput &ni, NavOutput &no) override
Invokes the holonomic navigation algorithm itself.
mrpt::math::CMatrixD m_dirs_scores
Individual scores for each direction: (i,j), i (row) are directions, j (cols) are scores...
T square(const T x)
Inline function for the square of a number.
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
double HYSTERESIS_SECTOR_COUNT
Range of "sectors" (directions) for hysteresis over successive timesteps.
int round(const T value)
Returns the closer integer (int) to x.
#define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT( variableName, variableType, configFileObject, sectionNameStr)
#define MRPT_LOAD_CONFIG_VAR( variableName, variableType, configFileObject, sectionNameStr)
An useful macro for loading variables stored in a INI-like file under a key with the same name that t...
unsigned int m_last_selected_sector
T abs_diff(const T a, const T b)
Efficient and portable evaluation of the absolute difference of two unsigned integer values (but will...
GLenum GLenum GLvoid GLvoid GLvoid * span
GLubyte GLubyte GLubyte a
Output for CAbstractHolonomicReactiveMethod::navigate()
mrpt::math::CMatrixD dirs_scores
Individual scores for each direction: (i,j), i (row) are directions, j (cols) are scores...
std::string sprintf_container(const char *fmt, const T &V)
Generates a string for a container in the format [A,B,C,...], and the fmt string for each vector elem...
std::vector< std::vector< double > > phase_scores
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()
mrpt::nav::CParameterizedTrajectoryGenerator * getAssociatedPTG() const
Returns the pointer set by setAssociatedPTG()
std::vector< std::vector< int32_t > > PHASE_FACTORS
Factor indices [0,4] for the factors to consider in each phase 1,2,...N of the movement decision (Def...
bool LOG_SCORE_MATRIX
(default:false, to save space)
uint16_t alpha2index(double alpha) const
Discrete index value for the corresponding alpha value.
CONTAINER::Scalar norm(const CONTAINER &v)