added basic probability computation to global routing matrix, and debug output. Fixed bug in Float serialisation (this should be improved btw)

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6890 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2013-11-05 21:15:26 +00:00
parent f3750d0d61
commit 03e6fb8a4a
5 changed files with 217 additions and 22 deletions

View file

@ -25,9 +25,117 @@
#include "groutermatrix.h"
bool GRouterMatrix::addRoutingClue(const GRouterKeyId& id,const GRouterServiceId& sid,float distance,const std::string& desc_string,const SSLIdType& source_friend)
GRouterMatrix::GRouterMatrix()
{
std::cerr << "(WW) " << __PRETTY_FUNCTION__ << ": not implemented" << std::endl;
_proba_need_updating = true ;
}
bool GRouterMatrix::addRoutingClue( const GRouterKeyId& key_id,const GRouterServiceId& sid,float distance,
const std::string& desc_string,const SSLIdType& source_friend)
{
// 1 - get the friend index.
//
uint32_t fid = getFriendId(source_friend) ;
// 2 - get the Key map, and add the routing clue.
//
time_t now = time(NULL) ;
RoutingMatrixHitEntry rc ;
rc.weight = 1.0f / (1.0f + distance) ;
rc.time_stamp = now ;
rc.friend_id = fid ;
_routing_clues[key_id].push_front(rc) ; // create it if necessary
_proba_need_updating = true ; // always, since we added new clues.
return true ;
}
uint32_t GRouterMatrix::getFriendId(const SSLIdType& source_friend)
{
std::map<SSLIdType,uint32_t>::const_iterator it = _friend_indices.find(source_friend) ;
if(it == _friend_indices.end())
{
// add a new friend
uint32_t new_id = _reverse_friend_indices.size() ;
_reverse_friend_indices.push_back(source_friend) ;
_friend_indices[source_friend] = new_id ;
return new_id ;
}
else
return it->second ;
}
void GRouterMatrix::debugDump() const
{
std::cerr << " Proba needs up: " << _proba_need_updating << std::endl;
std::cerr << " Routing events: " << std::endl;
time_t now = time(NULL) ;
for(std::map<GRouterKeyId, std::list<RoutingMatrixHitEntry> >::const_iterator it(_routing_clues.begin());it!=_routing_clues.end();++it)
{
std::cerr << " " << it->first.toStdString() << " : " ;
for(std::list<RoutingMatrixHitEntry>::const_iterator it2(it->second.begin());it2!=it->second.end();++it2)
std::cerr << now - (*it2).time_stamp << " (" << (*it2).friend_id << "," << (*it2).weight << ") " ;
std::cerr << std::endl;
}
std::cerr << " Routing probabilities: " << std::endl;
for(std::map<GRouterKeyId, std::vector<float> >::const_iterator it(_time_combined_hits.begin());it!=_time_combined_hits.end();++it)
{
std::cerr << it->first.toStdString() << " : " ;
for(uint32_t i=0;i<it->second.size();++i)
std::cerr << it->second[i] << " " ;
std::cerr << std::endl;
}
}
bool GRouterMatrix::computeRoutingProbabilities(const GRouterKeyId& id, const std::vector<SSLIdType>& friends, std::vector<float>& probas) const
{
// Routing probabilities are computed according to routing clues
//
// For a given key, each friend has a known set of routing clues (time_t, weight)
// We combine these to compute a static weight for each friend/key pair.
// This is performed in updateRoutingProbabilities()
//
// Then for a given list of online friends, the weights are computed into probabilities,
// that always sum up to 1.
//
return true ;
}
bool GRouterMatrix::updateRoutingProbabilities()
{
if(!_proba_need_updating)
return false ;
time_t now = time(NULL) ;
for(std::map<GRouterKeyId, std::list<RoutingMatrixHitEntry> >::const_iterator it(_routing_clues.begin());it!=_routing_clues.end();++it)
{
std::cerr << " " << it->first.toStdString() << " : " ;
std::vector<float>& v(_time_combined_hits[it->first]) ;
v.clear() ;
v.resize(_friend_indices.size(),0.0f) ;
for(std::list<RoutingMatrixHitEntry>::const_iterator it2(it->second.begin());it2!=it->second.end();++it2)
{
float time_difference_in_days = 1 + (now - (*it2).time_stamp ) / 86400.0f ;
v[(*it2).friend_id] += (*it2).weight / (time_difference_in_days*time_difference_in_days) ;
}
}
_proba_need_updating = false ;
return true ;
}