Go to the documentation of this file.00001
00007
00008
00009
00010
00011
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
00024
00025
00026 using namespace std;
00027 using namespace momdp;
00028
00029 namespace momdp
00030 {
00031
00032
00033
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
00041 BeliefTreeNode* cn = (*beliefCacheSet)[stateidx]->getRow(row)->REACHABLE;
00042
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
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
00090 BeliefTreeNode* cn = new BeliefTreeNode();
00091 cn->extraData = new EvaluatorBeliefTreeNodeTuple();
00092
00093 int timeStamp = -1;
00094 cn->s = b_s;
00095
00096 cn->count = 0;
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
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
00125 BeliefTreeNode* temp = (*beliefCacheSet)[stateidx]->getRow(row)->REACHABLE;
00126 return temp;
00127 }
00128 }
00129
00130
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
00138
00139
00140 SharedPointer<BeliefWithState> sp;
00141
00142 SharedPointer<SparseVector> jspv (new SparseVector());
00143
00144
00145
00146 cn.Q.resize(problem->getNumActions());
00147
00148
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
00158 Qa.immediateReward = problem->rewards->getReward(*cn.s, a);
00159
00160
00161
00162
00163 problem->getObsStateProbVector(spv, *(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
00190 obs_prob_vector *opv = new obs_prob_vector();
00191
00192 problem->getJointUnobsStateProbVector(*jspv, (SharedPointer<BeliefWithState> )cn.s, a, Xn);
00193
00194 problem->getObsProbVectorFast(*opv, a, Xn, *jspv);
00195
00196 ((EvaluatorAfterObsDataTuple *)xe->extraData)->opv = opv;
00197
00198
00199 xe->outcomes.resize(opv->size());
00200
00201 for(Observations::iterator oIter = problem->observations->begin() ; oIter != problem->observations->end(); oIter ++)
00202 {
00203
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
00214
00215 e->obsProb = oprob * sprob;
00216
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
00226 e->nextState->count++;
00227
00228 DEBUG_TRACE( cout << "e->nextState->cacheIndex.row " << e->nextState->cacheIndex.row << " count " << e->nextState->count << endl; );
00229
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 };