EvaluatorSampleEngine.cpp
Go to the documentation of this file.
00001 
00007 /******************************************************************************
00008 * File: Sample.cc
00009 * Date: 30/06/2007
00010 * Author: rn
00011 * Brief: Super class of all sample classes
00012 ******************************************************************************/
00013 
00014 #include "Sample.h"
00015 #include "EvaluatorSampleEngine.h"
00016 #include "BeliefCache.h"
00017 #include "AlphaPlane.h"
00018 #include "AlphaPlaneMaxMeta.h"
00019 #include "GlobalResource.h"
00020 #include "EvaluatorBeliefTreeNodeTuple.h"
00021 
00022 
00023 //#define DEBUGSYL_310708 1
00024 //#define DEBUGSYLTAG 1
00025 
00026 using namespace std;
00027 using namespace momdp;
00028 
00029 namespace momdp
00030 {
00031 
00032         /***************************************
00033         BASIC REACHABILITY TREE METHODS
00034         ****************************************/
00035         void EvaluatorSampleEngine::samplePrepare(cacherow_stval beliefIndex_Sval)
00036         {
00037                 state_val stateidx = beliefIndex_Sval.sval;
00038                 int     row =  beliefIndex_Sval.row;
00039 
00040                 //do the preparation work before sample
00041                 BeliefTreeNode* cn = (*beliefCacheSet)[stateidx]->getRow(row)->REACHABLE;
00042                 //BeliefTreeNode* cn = beliefCache->getRow(row)->REACHABLE;
00043                 
00044                 if(cn->isFringe())
00045                 {
00046                         expand(*cn);
00047                 }
00048         }
00049 
00050         void EvaluatorSampleEngine::samplePrepare(BeliefTreeNode* cn)
00051         {
00052                 if(cn->isFringe())
00053                 {
00054                         expand(*cn);
00055                 }
00056         }
00057 
00058         //ADD SYLTAG - this function just creates, initializes and returns pointer. It does not expand.
00059         BeliefForest* EvaluatorSampleEngine::getGlobalNode(void) 
00060         {
00061                 return beliefForest;
00062         }
00063 
00064 
00065 
00066 
00067         BeliefTreeNode* EvaluatorSampleEngine::getNode(SharedPointer<BeliefWithState>& b_s) 
00068         {
00069                 SharedPointer<belief_vector> s = b_s->bvec;
00070                 state_val stateidx = b_s->sval;
00071 
00072                 bool keepLowerBound = true;
00073 
00074                 DEBUG_TRACE( cout << "EvaluatorSampleEngine::getNode stateidx " << stateidx; );
00075                 DEBUG_TRACE( cout << " s" << endl; );
00076                 DEBUG_TRACE( s->write(cout) << endl; );
00077                 DEBUG_TRACE( cout << " hash: " << s->md5HashValue() << endl; );
00078 
00079                 int row = (*beliefCacheSet)[stateidx]->getBeliefRowIndex(s);
00080 
00081                 DEBUG_TRACE( cout << "testedRowIndex: " << row << endl; );
00082 
00083 
00084                 bool isTerminal;
00085                 double ubVal, lbVal;
00086 
00087                 if (row==-1) 
00088                 {
00089                         // create a new fringe node
00090                         BeliefTreeNode* cn = new BeliefTreeNode();
00091                         cn->extraData = new EvaluatorBeliefTreeNodeTuple();
00092 
00093                         int timeStamp = -1;//for initializing timeStamp of the beliefNode
00094                         cn->s = b_s;
00095                         
00096                         cn->count = 0; //for SARSOP
00097                         cn->checked = false;
00098                         
00099                         row = (*beliefCacheSet)[stateidx]->addBeliefRowWithoutCheck(cn->s->bvec);
00100                 
00101                         cn->cacheIndex.row = row;
00102                         cn->cacheIndex.sval = cn->s->sval;
00103 
00104                         isTerminal = problem->getIsTerminalState(*b_s);
00105 
00106                         (*beliefCacheSet)[stateidx]->getRow( row)->REACHABLE = cn;
00107                         // TODO:: check if anyone using this: solver->beliefCacheSet[stateidx]->getRow( row)->IS_TERMINAL = isTerminal;
00108 
00109                         for(int i = 0 ; i < onGetNode.size(); i++)
00110                         {
00111                                 (*onGetNode[i])(solver, cn, b_s);
00112                         }
00113 
00114                         
00115 
00116                         DEBUG_TRACE( cout << "cn->cacheIndex.row " << cn->cacheIndex.row << " count " << cn->count << endl; );
00117 
00118 
00119 
00120                         return cn;
00121                 }
00122                 else 
00123                 {
00124                         // return existing node
00125                         BeliefTreeNode* temp = (*beliefCacheSet)[stateidx]->getRow(row)->REACHABLE;
00126                         return temp;
00127                 }
00128         }
00129 
00130         //Assumption: cn.depth is defined already
00131         void EvaluatorSampleEngine::expand(BeliefTreeNode& cn)
00132         {
00133                 DEBUG_TRACE( cout<<"expand"<< endl; );
00134                 DEBUG_TRACE( cout << "cn.s sval " << cn.s->sval << " index " << cn.cacheIndex.row << endl; );
00135                 DEBUG_TRACE( cn.s->bvec->write(cout) << endl; );
00136                 
00137                 // set up successors for this fringe node (possibly creating new fringe nodes)
00138                 
00139                 //state_vector *sp = new state_vector;
00140                 SharedPointer<BeliefWithState> sp; // state_vector sp;
00141                 
00142                 SharedPointer<SparseVector> jspv (new SparseVector());
00143 
00144                 //obs_prob_vector  ospv; // outcome_prob_vector opv;
00145 
00146                 cn.Q.resize(problem->getNumActions());
00147 
00148                 // list<int>::iterator iter;            doesnt seem to be used
00149                 obsState_prob_vector spv;  
00150                 for(Actions::iterator aIter =  problem->actions->begin() ; aIter != problem->actions->end(); aIter ++)
00151                 {
00152                         int a = aIter.index();
00153                         DEBUG_TRACE( cout << "EvaluatorSampleEngine::expand a " << a << endl; );
00154                         BeliefTreeQEntry& Qa = cn.Q[a];
00155                         Qa.extraData = new EvaluatorAfterActionDataTuple();
00156                         
00157                         //Qa.immediateReward = problem->getReward(cn.s, a);
00158                         Qa.immediateReward = problem->rewards->getReward(*cn.s, a);
00159                         
00160 
00161                         // outcome probability for values of observed state
00162                         
00163                         problem->getObsStateProbVector(spv, *(cn.s), a); // P(Xn|cn.s,a)
00164 
00165                         ((EvaluatorAfterActionDataTuple*)Qa.extraData)->spv = SharedPointer<SparseVector>(new SparseVector());
00166                         copy(*((EvaluatorAfterActionDataTuple*)Qa.extraData)->spv, spv);
00167 
00168                         Qa.stateOutcomes.resize(spv.size());
00169                         Qa.valid = true;
00170                         
00171                         
00172 
00173                         DEBUG_TRACE( cout << "spv" << endl; );
00174                         DEBUG_TRACE( spv.write(cout) << endl; );
00175                         
00176                         for(States::iterator xIter =  problem->XStates->begin() ; xIter != problem->XStates->end(); xIter ++)
00177                         {
00178                                 int Xn = xIter.index();
00179                                 
00180                                 DEBUG_TRACE( cout << "EvaluatorSampleEngine::expand Xn " << Xn << endl; );
00181 
00182                                 double sprob = spv(Xn);
00183                                 if (sprob > OBS_IS_ZERO_EPS) 
00184                                 {
00185                                         BeliefTreeObsState* xe = new BeliefTreeObsState();
00186                                         Qa.stateOutcomes[Xn] = xe;
00187                                         xe->extraData = new EvaluatorAfterObsDataTuple();
00188 
00189                                         // outcome_prob_vector opv;
00190                                         obs_prob_vector  *opv = new obs_prob_vector();
00191 
00192                                         problem->getJointUnobsStateProbVector(*jspv, (SharedPointer<BeliefWithState> )cn.s, a, Xn);
00193                                         //problem->getStatenObsProbVectorFast(ospv, a, Xn, jspv);
00194                                         problem->getObsProbVectorFast(*opv, a, Xn, *jspv); // only the joint prob is useful for later but we calculate the observation prob P(o|Xn,cn.s,a)
00195                                         
00196                                         ((EvaluatorAfterObsDataTuple *)xe->extraData)->opv = opv;
00197 
00198                                         //problem->getObsProbVector(opv, cn.s, a, Xn);
00199                                         xe->outcomes.resize(opv->size());
00200 
00201                                         for(Observations::iterator oIter =  problem->observations->begin() ; oIter != problem->observations->end(); oIter ++)
00202                                         {
00203                                                 //FOR(o, opv.size()) 
00204                                                 int o = oIter.index();
00205 
00206                                                 DEBUG_TRACE( cout << "EvaluatorSampleEngine::expand o " << o << endl; );
00207 
00208                                                 double oprob = opv->operator ()(o);
00209                                                 if (oprob > OBS_IS_ZERO_EPS) 
00210                                                 {
00211                                                         BeliefTreeEdge* e = new BeliefTreeEdge();
00212                                                         xe->outcomes[o] = e;
00213                                                         //QaXn.outcomes[o] = e;
00214                                                         //e->obsProb = oprob;
00215                                                         e->obsProb = oprob * sprob; // P(o,Xn|cn.s,a) = P(Xn|cn.s,a) * P(o|Xn,cn.s,a)
00216                                                         //e->nextState = getNode(problem->getNextBeliefStvalFast(sp, a, o, Xn, jspv));
00217                                                         sp = (problem->beliefTransition->nextBelief2(cn.s, a, o, Xn, jspv));
00218                                                         sp->bvec->finalize();
00219                                                         e->nextState = getNode(sp);
00220                                                         
00221                                                         DEBUG_TRACE( cout << "EvaluatorSampleEngine::expand e->nextState row " << e->nextState->cacheIndex.row << endl; );
00222                                                         DEBUG_TRACE( cout << "EvaluatorSampleEngine::expand e->nextState belief " << endl; );
00223                                                         DEBUG_TRACE( e->nextState->s->bvec->write(cout) << endl; );
00224 
00225                                                         //e->nextState = getNode(problem->getNextBeliefStval(sp, cn.s, a, o, Xn));
00226                                                         e->nextState->count++;//increment valid-path count, for the current
00227 
00228                                                         DEBUG_TRACE( cout << "e->nextState->cacheIndex.row " << e->nextState->cacheIndex.row << " count " << e->nextState->count << endl; );
00229                                                         // new path which runs into the node
00230                                                 }
00231                                                 else 
00232                                                 {
00233                                                         xe->outcomes[o] = NULL;
00234                                                 }
00235                                         }
00236                                 } 
00237                                 else 
00238                                 { 
00239                                         Qa.stateOutcomes[Xn] = NULL;
00240                                 }
00241                         }
00242                         Qa.ubVal = CB_QVAL_UNDEFINED;
00243                 }
00244                 numStatesExpanded++;
00245         }
00246 
00247 };


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