UniqueBeliefHeap.cpp
Go to the documentation of this file.
00001 
00005 #include <sstream>
00006 #include <iostream>
00007 
00008 #include "UniqueBeliefHeap.h"
00009 #include "BeliefException.h"
00010 #include "GlobalResource.h"
00011 
00012 
00013 using namespace std;
00014 #ifdef _MSC_VER
00015 using namespace stdext;
00016 #endif
00017 
00018 #define HashResolveRes 1.0e-7
00019 
00020 namespace momdp
00021 {
00022         int UniqueBeliefHeap::addBeliefRowPair(SharedPointer<belief_vector>& bel, int row)
00023         {
00024                 //  string newHash = bel.hashCode();
00025                 // cout << "addBeliefRowPair: bel size: " << bel.size() << endl;
00026 
00027 
00028                 string newHash = bel->md5HashValue();
00029                 //cout << "Hash: " << newHash << endl;
00030 
00031 
00032                 //if(!isStorageMaterial(bel))
00033                 //{
00034                 //      cout << "Trying to add a duplicate hash key " << newHash << endl;
00035                 //      return -1;
00036                 //}
00037 
00038                 map<string, vector<BeliefVector_RowIndexPair> > ::iterator iter = uniqueBelHeap.find(newHash);
00039                 
00040                 if(iter == uniqueBelHeap.end())
00041                 {
00042                         vector<BeliefVector_RowIndexPair> *vecBvrip = new vector<BeliefVector_RowIndexPair>();
00043                         
00044                         BeliefVector_RowIndexPair *bvrip = new BeliefVector_RowIndexPair(bel, row);
00045                         vecBvrip->push_back(*bvrip);
00046 
00047                         uniqueBelHeap[newHash] = *vecBvrip;
00048                 }
00049                 else
00050                 {
00051                         BeliefVector_RowIndexPair *bvrip = new BeliefVector_RowIndexPair(bel, row);
00052                         iter->second.push_back(*bvrip);
00053                         //uniqueBelHeap[newHash].push_back();
00054                 }
00055 
00056                 
00057 
00058                 //cout << "-------------- " << uniqueBelHeap[newHash].size() << " -----------------------------" << endl;
00059                 //printHashMap();
00060 
00061                 return row;
00062         }
00063 
00064 
00065         bool UniqueBeliefHeap::hasBelief(SharedPointer<belief_vector>& bel)
00066         {
00067                 int rowIndex = getBeliefRowIndex(bel);
00068                 return (rowIndex >= 0);
00069         }
00070 
00071         int UniqueBeliefHeap::getBeliefRowIndex(SharedPointer<belief_vector>& bel)const 
00072         {
00073                 string newHash = bel->md5HashValue();
00074                 GlobalResource::getInstance()->hashRequest ++;
00075                 map<string, vector<BeliefVector_RowIndexPair> > ::const_iterator iter = uniqueBelHeap.find(newHash);
00076                 if(iter != uniqueBelHeap.end())
00077                 {
00078                         GlobalResource::getInstance()->hashSame ++;
00079                         // same hash, should go and check each element inside
00080                         vector<BeliefVector_RowIndexPair> vecBvrip = iter->second;
00081                         for(int i = 0 ; i < vecBvrip.size(); i++)
00082                         {
00083                                 BeliefVector_RowIndexPair bvrip = vecBvrip[i];
00084 
00085                                 // Resolve by total difference, currently disabled
00086                                 /* 
00087                                 double  diff = bvrip.vector->totalDifference(bel);
00088                                 if (diff < 0.0001)
00089                                 {
00090                                         // difference small enough to be considered same vector;
00091                                         //cout << "Not Collision : diff: " << diff << endl;
00092                                         return bvrip.row;
00093                                 }
00094                                 */
00095                                 
00096                                 // Consider different if single differs
00097                                 if(!bvrip.vector->isDifferentByAtLeastSingleEntry(*bel, HashResolveRes))
00098                                 {
00099                                         return bvrip.row;
00100                                 }
00101                         }
00102                         //cout << "Collision" << endl;
00103                         GlobalResource::getInstance()->hashCollision ++;
00104                         return -1;
00105                 }
00106                 else
00107                 {
00108                         return -1;
00109                 }
00110         }
00111 
00112         
00113         void UniqueBeliefHeap::printHashMap()const 
00114         {
00115                 map<string, vector<BeliefVector_RowIndexPair> > ::const_iterator iter = uniqueBelHeap.begin();
00116                 for(; iter != uniqueBelHeap.end();iter++)
00117                 {
00118                         cout << "Key: " << iter->first << " Length: " << iter->second.size() << endl;
00119                 }
00120 
00121         }
00122         bool UniqueBeliefHeap::isStorageMaterial(SharedPointer<belief_vector>& bel) {
00123                 return !hasBelief(bel);
00124         }
00125 
00126         UniqueBeliefHeap::~UniqueBeliefHeap(){
00127                 //  uniqueBelHeap.clear();
00128         }
00129 }


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29