Go to the documentation of this file.00001 #include "AlphaPlanePool.h"
00002 #include "BeliefCache.h"
00003 #include "SARSOP.h"
00004 #include <exception>
00005 #include <stdexcept>
00006 using namespace std;
00007
00008
00009
00010
00011 SharedPointer<AlphaPlane> AlphaPlanePool::getValueAlpha(SharedPointer<Belief>& belief)
00012 {
00013 return getBestAlphaPlane(belief, -1);
00014 }
00015
00016 void AlphaPlanePool::addAlphaPlane(SharedPointer<AlphaPlane> plane)
00017 {
00018 planes.push_back(plane);
00019 }
00020
00021
00022
00023 SharedPointer<AlphaPlane> AlphaPlanePool::getBestAlphaPlane( SharedPointer<belief_vector>& b, int index )
00024 {
00025 DEBUG_TRACE( cout << "getBestAlphaPlane b" << endl; );
00026 DEBUG_TRACE( b->write(cout) << endl; );
00027
00028 double val,maxval = -99e+20;
00029 SharedPointer<AlphaPlane> ret = NULL;
00030 int lastTimeStamp, maxTimeStamp, bestTimeStamp;
00031 lastTimeStamp = maxTimeStamp = bestTimeStamp = -1;
00032
00033
00034
00035 SARSOP* sarsopSolver = (SARSOP *)solver;
00036
00037 DEBUG_TRACE( cout << "index " << index << endl; );
00038
00039 if(index != -1)
00040 {
00041 maxval = beliefCache->getRow(index)->LB;
00042
00043 lastTimeStamp = dataTable->get(index).ALPHA_TIME_STAMP;
00044 maxTimeStamp = lastTimeStamp;
00045 list<SharedPointer<AlphaPlane> >* alphas = dataTable->get(index).ALPHA_PLANES;
00046
00047 DEBUG_TRACE( cout << "alphas->size()" << alphas->size() << endl; );
00048 if(alphas->size()>0)
00049 {
00050 ret = alphas->front();
00051 }
00052
00053 else
00054 {
00055 DEBUG_TRACE( cout << "no alpha plane presents" << endl; );
00056 }
00057 }
00058
00059
00060 DEBUG_TRACE( cout << "alphaPlanePool->planes.size() " << this->planes.size() << endl; );
00061 DEBUG_TRACE( cout << "maxval " << maxval << endl; );
00062
00063 LISTFOREACH(SharedPointer<AlphaPlane>, pr, this->planes)
00064 {
00065 SharedPointer<AlphaPlane> al = *pr;
00066
00067 DEBUG_TRACE( cout << "al->timeStamp" << al->timeStamp << endl; );
00068 DEBUG_TRACE( cout << "lastTimeStamp" << lastTimeStamp << endl; );
00069
00070 if(al->timeStamp > lastTimeStamp)
00071 {
00072 #if USE_MASKED_ALPHA
00073 if (!mask_subset( b, al->mask )) continue;
00074 #endif
00075 val = inner_prod(*al->alpha, *b );
00076 DEBUG_TRACE( cout << "val = inner_prod(al->alpha, b ); alpha:" << endl );
00077 DEBUG_TRACE( al->alpha->write( cout ) << endl );
00078
00079 DEBUG_TRACE( cout << "val " << val << endl; );
00080 DEBUG_TRACE( cout << "maxval " << maxval << endl; );
00081
00082
00083
00084
00085
00086 if (val >= maxval - 0.0000000000001)
00087 {
00088 maxval = val;
00089 ret = al;
00090 }
00091 if (al->timeStamp > maxTimeStamp){
00092 maxTimeStamp = al->timeStamp;
00093 }
00094 }
00095 }
00096
00097 DEBUG_TRACE( cout << "maxTimeStamp" << maxTimeStamp << endl; );
00098
00099
00100 if(index != -1)
00101 {
00102 if(ret!= NULL)
00103 {
00104
00105 dataTable->set(index).ALPHA_TIME_STAMP = maxTimeStamp;
00106 beliefCache->getRow( index)->LB = maxval;
00107
00108
00109 list<SharedPointer<AlphaPlane> >* alphas = dataTable->get(index).ALPHA_PLANES;
00110 if(alphas->size()>0)
00111 {
00112 SharedPointer<AlphaPlane> frontAlpha = alphas->front();
00113 SARSOPAlphaPlaneTuple *tempTuple = (SARSOPAlphaPlaneTuple *)frontAlpha->solverData;
00114 tempTuple->certed --;
00115 alphas->pop_front();
00116 }
00117
00118
00119 ((SARSOPAlphaPlaneTuple *)ret->solverData)->certed ++;
00120 alphas->push_front(ret);
00121 }
00122 }
00123
00124 if(ret == NULL)
00125 {
00126
00127 return getBestAlphaPlane1(b);
00128 }
00129
00130 assert(ret!=NULL);
00131
00132 return ret;
00133 }
00134 SharedPointer<AlphaPlane> AlphaPlanePool::getBestAlphaPlane( BeliefTreeNode& cn)
00135 {
00136 SharedPointer<belief_vector> b = cn.s->bvec;
00137 int index = cn.cacheIndex.row;
00138 return getBestAlphaPlane(b, index ) ;
00139 }
00140
00141 SharedPointer<AlphaPlane> AlphaPlanePool::getBestAlphaPlane( SharedPointer<belief_vector>& b)
00142 {
00143 int index = beliefCache->getBeliefRowIndex(b);
00144 return getBestAlphaPlane(b, index ) ;
00145 }
00146
00147
00148 SharedPointer<AlphaPlane> AlphaPlanePool::getBestAlphaPlane1( SharedPointer<belief_vector>& b)
00149 {
00150 double val, maxval = -99e+20;
00151 SharedPointer<AlphaPlane> ret = NULL;
00152
00153 LISTFOREACH(SharedPointer<AlphaPlane>, pr, this->planes)
00154 {
00155 SharedPointer<AlphaPlane> al = *pr;
00156 val = inner_prod( *(al->alpha), *b );
00157 if (val > maxval) {
00158 maxval = val;
00159 ret = al;
00160 }
00161 }
00162
00163 return ret;
00164 }
00165
00166
00167 REAL_VALUE AlphaPlanePool::getValue(SharedPointer<Belief>& belief)
00168 {
00169 double v = inner_prod( *getBestAlphaPlane(belief)->alpha, *belief );
00170 return v;
00171 }
00172 double AlphaPlanePool::getValue( SharedPointer<belief_vector>& belief, SharedPointer<AlphaPlane>* resultBestAlpha)
00173 {
00174 SharedPointer<AlphaPlane>bestAlpha = getBestAlphaPlane(belief);
00175 *resultBestAlpha = bestAlpha;
00176 double v = inner_prod( *(bestAlpha->alpha), *belief );
00177 return v;
00178 }