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),
47 _predictionEpsilon(0.0
f)
58 ParametersMap::const_iterator iter;
59 if((iter=parameters.find(Parameters::kBayesPredictionLC())) != parameters.end())
72 std::list<std::string> strValues =
uSplit(prediction,
' ');
73 if(strValues.size() < 2)
75 UERROR(
"The number of values < 2 (prediction=\"%s\")", prediction.c_str());
79 std::vector<double> tmpValues(strValues.size());
82 for(std::list<std::string>::iterator iter = strValues.begin(); iter!=strValues.end(); ++iter)
86 if(tmpValues[i] < 0.0 || tmpValues[i]>1.0)
96 UERROR(
"The prediction is not valid (values must be between >0 && <=1) prediction=\"%s\"", prediction.c_str());
155 if(!likelihood.size())
184 posterior = cv::Mat(likelihood.size(), 1, CV_32FC1);
189 ((
float*)posterior.data)[j++] = (*i).second;
191 ULOGGER_DEBUG(
"STEP1-update posterior=%fs, posterior=%d, _posterior size=%d", posterior.rows,
_posterior.size());
201 std::vector<float> likelihoodValues =
uValues(likelihood);
206 for(std::map<int, float>::const_iterator i=likelihood.begin(); i!= likelihood.end(); ++i)
208 std::map<int, float>::iterator p =
_posterior.find((*i).first);
211 (*p).second = (*i).second * ((
float*)prior.data)[j++];
239 const std::map<int, int> & neighbors,
240 const std::vector<double> & predictionLC,
241 #
if __cplusplus >= 201103L
242 const std::unordered_map<int, int> & idToIndex
244 const std::map<int, int> & idToIndex
248 UASSERT(col < (
unsigned int)prediction.cols &&
249 col < (
unsigned int)prediction.rows);
252 float * dataPtr = (
float*)prediction.data;
253 for(std::map<int, int>::const_iterator iter=neighbors.begin(); iter!=neighbors.end(); ++iter)
257 #if __cplusplus >= 201103L 258 std::unordered_map<int, int>::const_iterator jter = idToIndex.find(iter->first);
260 std::map<int, int>::const_iterator jter = idToIndex.find(iter->first);
262 if(jter != idToIndex.end())
264 UASSERT((iter->second+1) < (
int)predictionLC.size());
265 sum += dataPtr[col + jter->second*prediction.cols] = predictionLC[iter->second+1];
276 if(oldIds.size() == ids.size() &&
277 memcmp(oldIds.data(), ids.data(), oldIds.size()*
sizeof(int)) == 0)
297 #if __cplusplus >= 201103L 298 std::unordered_map<int,int> idToIndexMap;
299 idToIndexMap.reserve(ids.size());
301 std::map<int,int> idToIndexMap;
303 for(
unsigned int i=0; i<ids.size(); ++i)
307 idToIndexMap[ids[i]] = i;
313 cv::Mat prediction = cv::Mat::zeros(ids.size(), ids.size(), CV_32FC1);
314 int cols = prediction.cols;
318 std::set<int> idsDone;
320 for(
unsigned int i=0; i<ids.size(); ++i)
322 if(idsDone.find(ids[i]) == idsDone.end())
336 std::list<int> idsLoopMargin;
338 for(std::map<int, int>::iterator iter=neighbors.begin(); iter!=neighbors.end();)
340 if(memory->
isInSTM(iter->first))
342 neighbors.erase(iter++);
346 if(iter->second == 0 && idToIndexMap.find(iter->first)!=idToIndexMap.end())
348 idsLoopMargin.push_back(iter->first);
355 if(idsLoopMargin.size() == 0)
357 UFATAL(
"No 0 margin neighbor for signature %d !?!?", ids[i]);
361 for(std::list<int>::iterator iter = idsLoopMargin.begin(); iter!=idsLoopMargin.end(); ++iter)
369 int index = idToIndexMap.at(*iter);
371 idsDone.insert(*iter);
372 this->
normalize(prediction, index, sum, ids[0]<0);
384 for(
int j=1; j<cols; j++)
386 ((
float*)prediction.data)[i + j*cols] = val;
391 ((
float*)prediction.data)[i] = 1;
400 float val = 1.0/cols;
401 for(
int j=0; j<cols; j++)
403 ((
float*)prediction.data)[i + j*cols] = val;
408 ((
float*)prediction.data)[i] = 1;
423 memoryUsage +=
_posterior.size() * (
sizeof(float)+
sizeof(
int)+
sizeof(std::map<int, float>::iterator)) +
sizeof(std::map<int, float>);
426 memoryUsage +=
_neighborsIndex.size() * (
sizeof(int)+
sizeof(std::map<int, int>)+
sizeof(std::map<int, std::map<int, int> >::iterator)) +
sizeof(std::map<int, std::map<int, int> >);
429 memoryUsage += iter->second.size() * (
sizeof(int)*2+
sizeof(std::map<int, int>::iterator)) +
sizeof(std::map<int, int>);
434 void BayesFilter::normalize(cv::Mat & prediction,
unsigned int index,
float addedProbabilitiesSum,
bool virtualPlaceUsed)
const 436 UASSERT(index < (
unsigned int)prediction.rows && index < (
unsigned int)prediction.cols);
438 int cols = prediction.cols;
443 ((
float*)prediction.data)[index + index*cols] += delta;
444 addedProbabilitiesSum+=delta;
447 float allOtherPlacesValue = 0;
454 if(allOtherPlacesValue > 0 && cols>1)
456 float value = allOtherPlacesValue / float(cols - 1);
457 for(
int j=virtualPlaceUsed?1:0; j<cols; ++j)
459 if(((
float*)prediction.data)[index + j*cols] == 0)
461 ((
float*)prediction.data)[index + j*cols] = value;
462 addedProbabilitiesSum += ((
float*)prediction.data)[index + j*cols];
469 if(addedProbabilitiesSum<maxNorm-0.0001 || addedProbabilitiesSum>maxNorm+0.0001)
471 for(
int j=virtualPlaceUsed?1:0; j<cols; ++j)
473 ((
float*)prediction.data)[index + j*cols] *= maxNorm / addedProbabilitiesSum;
476 ((
float*)prediction.data)[index + j*cols] = 0.0f;
479 addedProbabilitiesSum = maxNorm;
486 addedProbabilitiesSum += ((
float*)prediction.data)[index];
495 if(addedProbabilitiesSum<0.99 || addedProbabilitiesSum > 1.01)
497 UWARN(
"Prediction is not normalized sum=%f", addedProbabilitiesSum);
503 const std::vector<int> & oldIds,
504 const std::vector<int> & newIds)
512 oldIds.size() == (
unsigned int)oldPrediction.cols &&
513 oldIds.size() == (
unsigned int)oldPrediction.rows);
515 cv::Mat prediction = cv::Mat::zeros(newIds.size(), newIds.size(), CV_32FC1);
519 #if __cplusplus >= 201103L 520 std::unordered_set<int> oldIdsSet(oldIds.begin(), oldIds.end());
522 std::set<int> oldIdsSet(oldIds.begin(), oldIds.end());
526 #if __cplusplus >= 201103L 527 std::unordered_map<int,int> newIdToIndexMap;
528 newIdToIndexMap.reserve(newIds.size());
530 std::map<int,int> newIdToIndexMap;
532 for(
unsigned int i=0; i<newIds.size(); ++i)
536 newIdToIndexMap[newIds[i]] = i;
540 UDEBUG(
"time creating id-index vector (size=%d oldIds.back()=%d newIds.back()=%d) = %fs", (
int)newIdToIndexMap.size(), oldIds.back(), newIds.back(), timer.
restart());
543 std::set<int> removedIds;
544 for(
unsigned int i=0; i<oldIds.size(); ++i)
546 if(oldIds[i] > 0 && newIdToIndexMap.find(oldIds[i]) == newIdToIndexMap.end())
548 removedIds.insert(removedIds.end(), oldIds[i]);
550 UDEBUG(
"removed id=%d at oldIndex=%d", oldIds[i], i);
555 bool oldAllCopied =
false;
556 if(removedIds.empty() &&
557 newIds.size() > oldIds.size() &&
558 memcmp(oldIds.data(), newIds.data(), oldIds.size()*
sizeof(int)) == 0)
560 oldPrediction.copyTo(cv::Mat(prediction, cv::Range(0, oldPrediction.rows), cv::Range(0, oldPrediction.cols)));
562 UDEBUG(
"Copied all old prediction: = %fs", timer.
ticks());
567 std::set<int> idsToUpdate;
568 for(
unsigned int i=0; i<oldIds.size() || i<newIds.size(); ++i)
572 if(removedIds.find(oldIds[i]) != removedIds.end())
574 unsigned int cols = oldPrediction.cols;
576 for(
unsigned int j=0; j<cols; ++j)
578 if(j!=i && removedIds.find(oldIds[j]) == removedIds.end())
581 idsToUpdate.insert(oldIds[j]);
585 UDEBUG(
"From removed id %d, %d neighbors to update.", oldIds[i], count);
588 if(i<newIds.size() && oldIdsSet.find(newIds[i]) == oldIdsSet.end())
594 for(std::map<int, int>::iterator iter=neighbors.begin(); iter!=neighbors.end(); ++iter)
596 std::map<int, std::map<int, int> >::iterator jter =
_neighborsIndex.find(iter->first);
599 uInsert(jter->second, std::make_pair(newIds[i], iter->second));
608 this->
normalize(prediction, i, sum, newIds[0]<0);
612 for(std::map<int,int>::const_iterator iter=neighbors.begin(); iter!=neighbors.end(); ++iter)
614 if(oldIdsSet.find(iter->first)!=oldIdsSet.end() &&
615 removedIds.find(iter->first) == removedIds.end())
617 idsToUpdate.insert(iter->first);
621 UDEBUG(
"From added id %d, %d neighbors to update.", newIds[i], count);
624 UDEBUG(
"time getting %d ids to update = %fs", idsToUpdate.size(), timer.
restart());
627 double e0=0,e1=0, e2=0, e3=0, e4=0;
630 for(std::set<int>::iterator iter = idsToUpdate.begin(); iter!=idsToUpdate.end(); ++iter)
635 int index = newIdToIndexMap.at(
id);
638 std::map<int, std::map<int, int> >::iterator kter =
_neighborsIndex.find(
id);
640 const std::map<int, int> & neighbors = kter->second;
647 this->
normalize(prediction, index, sum, newIds[0]<0);
652 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);
660 for(
unsigned int i=0; i<oldIds.size(); ++i)
662 if(oldIds[i]>0 && removedIds.find(oldIds[i]) == removedIds.end() && idsToUpdate.find(oldIds[i]) == idsToUpdate.end())
664 for(
int j=0; j<oldPrediction.cols; ++j)
666 if(oldIds[j]>0 && removedIds.find(oldIds[j]) == removedIds.end())
672 float v = ((
const float *)oldPrediction.data)[i + j*oldPrediction.cols];
673 int ii = newIdToIndexMap.at(oldIds[i]);
674 int jj = newIdToIndexMap.at(oldIds[j]);
675 ((
float *)prediction.data)[ii + jj*prediction.cols] = v;
691 if(prediction.cols>1)
695 for(
int j=1; j<prediction.cols; j++)
697 ((
float*)prediction.data)[j*prediction.cols] = val;
701 else if(prediction.cols>0)
703 ((
float*)prediction.data)[0] = 1;
706 UDEBUG(
"time updating virtual place = %fs", timer.
restart());
708 UDEBUG(
"Modified=%d, Added=%d, Copied=%d", modified, added, copied);
715 std::map<int, float> newPosterior;
716 for(std::vector<int>::const_iterator i=likelihoodIds.begin(); i != likelihoodIds.end(); ++i)
718 std::map<int, float>::iterator post =
_posterior.find(*i);
723 newPosterior.insert(std::pair<int, float>(*i, 1));
727 newPosterior.insert(std::pair<int, float>(*i, 0));
732 newPosterior.insert(std::pair<int, float>((*post).first, (*post).second));
static bool parse(const ParametersMap ¶meters, const std::string &key, bool &value)
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
std::string getPredictionLCStr() const
float _totalPredictionLCValues
std::vector< K > uKeys(const std::multimap< K, V > &mm)
virtual void parseParameters(const ParametersMap ¶meters)
bool _fullPredictionUpdate
float UTILITE_EXP uStr2Float(const std::string &str)
std::map< std::string, std::string > ParametersMap
const std::vector< double > & getPredictionLC() const
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)
cv::Mat generatePrediction(const Memory *memory, const std::vector< int > &ids)
void normalize(cv::Mat &prediction, unsigned int index, float addedProbabilitiesSum, bool virtualPlaceUsed) const
#define UASSERT(condition)
std::list< std::string > uSplit(const std::string &str, char separator=' ')
#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)
std::map< int, std::map< int, int > > _neighborsIndex
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
unsigned long getMemoryUsed() const
#define ULOGGER_ERROR(...)
std::string UTILITE_EXP uFormat(const char *fmt,...)
std::string UTILITE_EXP uNumber2Str(unsigned int number)
bool isInSTM(int signatureId) const
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)