34 #if __cplusplus >= 201103L 35 #include <unordered_map> 36 #include <unordered_set> 44 _virtualPlacePrior(
Parameters::defaultBayesVirtualPlacePriorThr()),
45 _fullPredictionUpdate(
Parameters::defaultBayesFullPredictionUpdate()),
46 _totalPredictionLCValues(0.0
f)
57 ParametersMap::const_iterator iter;
58 if((iter=parameters.find(Parameters::kBayesPredictionLC())) != parameters.end())
71 std::list<std::string> strValues =
uSplit(prediction,
' ');
72 if(strValues.size() < 2)
74 UERROR(
"The number of values < 2 (prediction=\"%s\")", prediction.c_str());
78 std::vector<double> tmpValues(strValues.size());
81 for(std::list<std::string>::iterator iter = strValues.begin(); iter!=strValues.end(); ++iter)
85 if(tmpValues[i] < 0.0 || tmpValues[i]>1.0)
95 UERROR(
"The prediction is not valid (values must be between >0 && <=1) prediction=\"%s\"", prediction.c_str());
146 if(!likelihood.size())
175 posterior = cv::Mat(likelihood.size(), 1, CV_32FC1);
180 ((
float*)posterior.data)[j++] = (*i).second;
182 ULOGGER_DEBUG(
"STEP1-update posterior=%fs, posterior=%d, _posterior size=%d", posterior.rows,
_posterior.size());
192 std::vector<float> likelihoodValues =
uValues(likelihood);
197 for(std::map<int, float>::const_iterator i=likelihood.begin(); i!= likelihood.end(); ++i)
199 std::map<int, float>::iterator p =
_posterior.find((*i).first);
202 (*p).second = (*i).second * ((
float*)prior.data)[j++];
230 const std::map<int, int> & neighbors,
231 const std::vector<double> & predictionLC,
232 #
if __cplusplus >= 201103L
233 const std::unordered_map<int, int> & idToIndex
235 const std::map<int, int> & idToIndex
239 UASSERT(col < (
unsigned int)prediction.cols &&
240 col < (
unsigned int)prediction.rows);
243 float * dataPtr = (
float*)prediction.data;
244 for(std::map<int, int>::const_iterator iter=neighbors.begin(); iter!=neighbors.end(); ++iter)
248 #if __cplusplus >= 201103L 249 std::unordered_map<int, int>::const_iterator jter = idToIndex.find(iter->first);
251 std::map<int, int>::const_iterator jter = idToIndex.find(iter->first);
253 if(jter != idToIndex.end())
255 UASSERT((iter->second+1) < (
int)predictionLC.size());
256 sum += dataPtr[col + jter->second*prediction.cols] = predictionLC[iter->second+1];
281 #if __cplusplus >= 201103L 282 std::unordered_map<int,int> idToIndexMap;
283 idToIndexMap.reserve(ids.size());
285 std::map<int,int> idToIndexMap;
287 for(
unsigned int i=0; i<ids.size(); ++i)
291 idToIndexMap[ids[i]] = i;
297 cv::Mat prediction = cv::Mat::zeros(ids.size(), ids.size(), CV_32FC1);
298 int cols = prediction.cols;
302 std::set<int> idsDone;
304 for(
unsigned int i=0; i<ids.size(); ++i)
306 if(idsDone.find(ids[i]) == idsDone.end())
320 std::list<int> idsLoopMargin;
322 for(std::map<int, int>::iterator iter=neighbors.begin(); iter!=neighbors.end();)
324 if(memory->
isInSTM(iter->first))
326 neighbors.erase(iter++);
330 if(iter->second == 0 && idToIndexMap.find(iter->first)!=idToIndexMap.end())
332 idsLoopMargin.push_back(iter->first);
339 if(idsLoopMargin.size() == 0)
341 UFATAL(
"No 0 margin neighbor for signature %d !?!?", ids[i]);
345 for(std::list<int>::iterator iter = idsLoopMargin.begin(); iter!=idsLoopMargin.end(); ++iter)
353 int index = idToIndexMap.at(*iter);
355 idsDone.insert(*iter);
356 this->
normalize(prediction, index, sum, ids[0]<0);
368 for(
int j=1; j<cols; j++)
370 ((
float*)prediction.data)[i + j*cols] = val;
375 ((
float*)prediction.data)[i] = 1;
384 float val = 1.0/cols;
385 for(
int j=0; j<cols; j++)
387 ((
float*)prediction.data)[i + j*cols] = val;
392 ((
float*)prediction.data)[i] = 1;
404 void BayesFilter::normalize(cv::Mat & prediction,
unsigned int index,
float addedProbabilitiesSum,
bool virtualPlaceUsed)
const 406 UASSERT(index < (
unsigned int)prediction.rows && index < (
unsigned int)prediction.cols);
408 int cols = prediction.cols;
413 ((
float*)prediction.data)[index + index*cols] += delta;
414 addedProbabilitiesSum+=delta;
417 float allOtherPlacesValue = 0;
424 if(allOtherPlacesValue > 0 && cols>1)
426 float value = allOtherPlacesValue / float(cols - 1);
427 for(
int j=virtualPlaceUsed?1:0; j<cols; ++j)
429 if(((
float*)prediction.data)[index + j*cols] == 0)
431 ((
float*)prediction.data)[index + j*cols] = value;
432 addedProbabilitiesSum += ((
float*)prediction.data)[index + j*cols];
439 if(addedProbabilitiesSum<maxNorm-0.0001 || addedProbabilitiesSum>maxNorm+0.0001)
441 for(
int j=virtualPlaceUsed?1:0; j<cols; ++j)
443 ((
float*)prediction.data)[index + j*cols] *= maxNorm / addedProbabilitiesSum;
445 addedProbabilitiesSum = maxNorm;
452 addedProbabilitiesSum += ((
float*)prediction.data)[index];
461 if(addedProbabilitiesSum<0.99 || addedProbabilitiesSum > 1.01)
463 UWARN(
"Prediction is not normalized sum=%f", addedProbabilitiesSum);
469 const std::vector<int> & oldIds,
470 const std::vector<int> & newIds)
478 oldIds.size() == (
unsigned int)oldPrediction.cols &&
479 oldIds.size() == (
unsigned int)oldPrediction.rows);
481 cv::Mat prediction = cv::Mat::zeros(newIds.size(), newIds.size(), CV_32FC1);
485 #if __cplusplus >= 201103L 486 std::unordered_set<int> oldIdsSet(oldIds.begin(), oldIds.end());
488 std::set<int> oldIdsSet(oldIds.begin(), oldIds.end());
492 #if __cplusplus >= 201103L 493 std::unordered_map<int,int> newIdToIndexMap;
494 newIdToIndexMap.reserve(newIds.size());
496 std::map<int,int> newIdToIndexMap;
498 for(
unsigned int i=0; i<newIds.size(); ++i)
502 newIdToIndexMap[newIds[i]] = i;
506 UDEBUG(
"time creating id-index vector (size=%d oldIds.back()=%d newIds.back()=%d) = %fs", (
int)newIdToIndexMap.size(), oldIds.back(), newIds.back(), timer.
restart());
509 std::set<int> removedIds;
510 for(
unsigned int i=0; i<oldIds.size(); ++i)
512 if(oldIds[i] > 0 && newIdToIndexMap.find(oldIds[i]) == newIdToIndexMap.end())
514 removedIds.insert(removedIds.end(), oldIds[i]);
516 UDEBUG(
"removed id=%d at oldIndex=%d", oldIds[i], i);
524 std::set<int> idsToUpdate;
525 for(
unsigned int i=0; i<oldIds.size() || i<newIds.size(); ++i)
529 if(removedIds.find(oldIds[i]) != removedIds.end())
531 unsigned int cols = oldPrediction.cols;
533 for(
unsigned int j=0; j<cols; ++j)
535 if(((
const float *)oldPrediction.data)[i + j*cols] > epsilon &&
537 removedIds.find(oldIds[j]) == removedIds.end())
540 idsToUpdate.insert(oldIds[j]);
544 UDEBUG(
"From removed id %d, %d neighbors to update.", oldIds[i], count);
547 if(i<newIds.size() && oldIdsSet.find(newIds[i]) == oldIdsSet.end())
553 for(std::map<int, int>::iterator iter=neighbors.begin(); iter!=neighbors.end(); ++iter)
555 std::map<int, std::map<int, int> >::iterator jter =
_neighborsIndex.find(iter->first);
558 uInsert(jter->second, std::make_pair(newIds[i], iter->second));
567 this->
normalize(prediction, i, sum, newIds[0]<0);
570 for(std::map<int,int>::const_iterator iter=neighbors.begin(); iter!=neighbors.end(); ++iter)
572 if(oldIdsSet.find(iter->first)!=oldIdsSet.end() &&
573 removedIds.find(iter->first) == removedIds.end())
575 idsToUpdate.insert(iter->first);
579 UDEBUG(
"From added id %d, %d neighbors to update.", newIds[i], count);
582 UDEBUG(
"time getting %d ids to update = %fs", idsToUpdate.size(), timer.
restart());
585 double e0=0,e1=0, e2=0, e3=0, e4=0;
588 for(std::set<int>::iterator iter = idsToUpdate.begin(); iter!=idsToUpdate.end(); ++iter)
593 int index = newIdToIndexMap.at(
id);
596 std::map<int, std::map<int, int> >::iterator kter =
_neighborsIndex.find(
id);
598 const std::map<int, int> & neighbors = kter->second;
605 this->
normalize(prediction, index, sum, newIds[0]<0);
610 UDEBUG(
"time updating modified/added %d ids = %fs (e0=%f e1=%f e2=%f e3=%f e4=%f)", idsToUpdate.size(), timer.
restart(), e0, e1, e2, e3, e4);
616 for(
unsigned int i=0; i<oldIds.size(); ++i)
618 if(oldIds[i]>0 && removedIds.find(oldIds[i]) == removedIds.end() && idsToUpdate.find(oldIds[i]) == idsToUpdate.end())
620 for(
int j=i; j<oldPrediction.cols; ++j)
622 if(removedIds.find(oldIds[j]) == removedIds.end() && ((
const float *)oldPrediction.data)[i + j*oldPrediction.cols] > epsilon)
628 float v = ((
const float *)oldPrediction.data)[i + j*oldPrediction.cols];
629 int ii = newIdToIndexMap.at(oldIds[i]);
630 int jj = newIdToIndexMap.at(oldIds[j]);
631 ((
float *)prediction.data)[ii + jj*prediction.cols] = v;
634 ((
float *)prediction.data)[jj + ii*prediction.cols] = v;
646 if(prediction.cols>1)
650 for(
int j=1; j<prediction.cols; j++)
652 ((
float*)prediction.data)[j*prediction.cols] = val;
656 else if(prediction.cols>0)
658 ((
float*)prediction.data)[0] = 1;
661 UDEBUG(
"time updating virtual place = %fs", timer.
restart());
663 UDEBUG(
"Modified=%d, Added=%d, Copied=%d", modified, added, copied);
670 std::map<int, float> newPosterior;
671 for(std::vector<int>::const_iterator i=likelihoodIds.begin(); i != likelihoodIds.end(); ++i)
673 std::map<int, float>::iterator post =
_posterior.find(*i);
678 newPosterior.insert(std::pair<int, float>(*i, 1));
682 newPosterior.insert(std::pair<int, float>(*i, 0));
687 newPosterior.insert(std::pair<int, float>((*post).first, (*post).second));
static bool parse(const ParametersMap ¶meters, const std::string &key, bool &value)
float _totalPredictionLCValues
std::vector< K > uKeys(const std::multimap< K, V > &mm)
bool isInSTM(int signatureId) const
virtual void parseParameters(const ParametersMap ¶meters)
bool _fullPredictionUpdate
std::string getPredictionLCStr() const
float UTILITE_EXP uStr2Float(const std::string &str)
std::map< std::string, std::string > ParametersMap
float addNeighborProb(cv::Mat &prediction, unsigned int col, const std::map< int, int > &neighbors, const std::vector< double > &predictionLC, const std::map< int, int > &idToIndex)
std::list< std::string > uSplit(const std::string &str, char separator= ' ')
cv::Mat generatePrediction(const Memory *memory, const std::vector< int > &ids)
#define UASSERT(condition)
void normalize(cv::Mat &prediction, unsigned int index, float addedProbabilitiesSum, bool virtualPlaceUsed) const
#define ULOGGER_DEBUG(...)
#define UASSERT_MSG(condition, msg_str)
cv::Mat updatePrediction(const cv::Mat &oldPrediction, const Memory *memory, const std::vector< int > &oldIds, const std::vector< int > &newIds)
const std::vector< double > & getPredictionLC() const
std::map< int, std::map< int, int > > _neighborsIndex
std::map< int, int > getNeighborsId(int signatureId, int maxGraphDepth, int maxCheckedInDatabase=-1, bool incrementMarginOnLoop=false, bool ignoreLoopIds=false, bool ignoreIntermediateNodes=false, bool ignoreLocalSpaceLoopIds=false, const std::set< int > &nodesSet=std::set< int >(), double *dbAccessTime=0) const
BayesFilter(const ParametersMap ¶meters=ParametersMap())
std::vector< V > uValues(const std::multimap< K, V > &mm)
std::map< int, float > _posterior
void updatePosterior(const Memory *memory, const std::vector< int > &likelihoodIds)
std::vector< double > _predictionLC
#define ULOGGER_ERROR(...)
std::string UTILITE_EXP uFormat(const char *fmt,...)
std::string UTILITE_EXP uNumber2Str(unsigned int number)
const std::map< int, float > & computePosterior(const Memory *memory, const std::map< int, float > &likelihood)
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
void setPredictionLC(const std::string &prediction)