BeliefTransitionMOMDP.cpp
Go to the documentation of this file.
00001 #include "BeliefTransitionMOMDP.h"
00002 #include "MOMDP.h"
00003 #include "BeliefWithState.h"
00004 #include "Observations.h"
00005 #include "exception" 
00006 
00007 using namespace std;
00008 
00009 BeliefTransitionMOMDP::BeliefTransitionMOMDP(void)
00010 {
00011 }
00012 
00013 BeliefTransitionMOMDP::~BeliefTransitionMOMDP(void)
00014 {
00015 }
00016 
00017 // SYL07292010 added for belief update when there is b_x and b_y, a, x' and o. Only used during simulation/evaluation
00018 // The output is (x',b'_y)
00019 SharedPointer<BeliefWithState> BeliefTransitionMOMDP::nextBelief(SharedPointer<belief_vector>& belY, DenseVector& belX, int a, int o, int obsX)
00020 {
00021         SharedPointer<MOMDP> momdpProblem = dynamic_pointer_cast<MOMDP> (problem);
00022         SharedPointer<BeliefWithState> result (new BeliefWithState());
00023 
00024         DenseVector Bc; 
00025         copy(Bc, *belY);
00026 
00027         unsigned int xn = obsX;
00028 
00029         // for given a, o and x'
00030         // for a particular x'
00031         if (!((momdpProblem->obsProb->getMatrix(a, xn))->isColumnEmpty(o))) 
00032         {
00033                 SparseVector jspv_sum;  // sum of P(x',y' | x, b_{y|x}, a)  * b_x(x) over values of x
00034                 jspv_sum.resize(momdpProblem->YStates->size());
00035 
00036                 // loop over x
00037                 FOR (xc, momdpProblem->XStates->size())
00038                 {
00039                         if (!(belX(xc) == 0)) 
00040                         {
00041                                 // for a particular x
00042                                 // skip all the calculations to add to jspv_sum if *(momdpProblem->XTrans->getMatrix(a, xc)) is all zero for column xn
00043                                 if (!((momdpProblem->XTrans->getMatrix(a, xc))->isColumnEmpty(xn))) 
00044                                 {
00045                                         DenseVector tmp, tmp1;
00046                                         SparseVector jspv;
00047                                         
00048                                         emult_column( tmp, *(momdpProblem->XTrans->getMatrix(a, xc)), xn, Bc );
00049         
00050                                         mult( tmp1, *(momdpProblem->YTrans->getMatrixTr(a, xc, xn)), tmp );
00051                                         copy(jspv, tmp1);  // P(x',y' | x, b_{y|x}, a) for a particular x and x'
00052         
00053                                         // multiply with belX(xc) and add to sum over x values
00054                                         jspv *= belX(xc);
00055                                         jspv_sum += jspv;
00056                                 }
00057                         }
00058                 }
00059 
00060                 emult_column( *result->bvec, *(momdpProblem->obsProb->getMatrix(a, xn)), o,  jspv_sum); // P(x',y',o | b_x, b_{y|x}, a)
00061                 
00062                 // check that P(x',y',o | b_x, b_{y|x}, a) is not a zero vector for this particular combination of b, a, o and x'
00063                 if (!(result->bvec->data.size() == 0)) {
00064                         // normalize to get P(y'| x', b_x, b_{y|x}, a, o)
00065                         // the normalization factor is P(x',o | b_x, b_{y|x}, a)
00066                         (*result->bvec) *= (1.0/result->bvec->norm_1());                
00067                 } else  throw runtime_error("In nextBelief(), the combination of b, a, o and x' is not possible.");
00068 
00069         } else  throw runtime_error("In nextBelief(), the combination of b, a, o and x' is not possible."); 
00070         result->bvec->finalize();
00071         result->sval = obsX;
00072         return result;
00073 
00074 }
00075 
00076 
00077 //BeliefWithState& POMDP::getNextBeliefStvalFast(BeliefWithState& result, int a, int o, int Xn, const SparseVector tmp1) const
00078 
00079 SharedPointer<BeliefWithState> BeliefTransitionMOMDP::nextBelief(SharedPointer<BeliefWithState> bp, int a, int o, int obsX )
00080 {
00081         SparseVector jspv;
00082         SharedPointer<MOMDP> momdpProblem = dynamic_pointer_cast<MOMDP> (problem);
00083         // TODO:: should cache jspv somethere
00084         momdpProblem->getJointUnobsStateProbVector(jspv, bp , a, obsX);
00085 
00086         
00087         //cout << " jspv " << endl;
00088         //jspv.write(cout) << endl;
00089 
00090         SharedPointer<BeliefWithState> result (new BeliefWithState());
00091         emult_column( *result->bvec, *(momdpProblem->obsProb->getMatrix(a, obsX)), o,  jspv);
00092         (*result->bvec) *= (1.0/result->bvec->norm_1());
00093         
00094         result->sval = obsX;
00095         return result;
00096 
00097 }
00098 
00099 
00100 // TODO:: nextBelief2 does not make use of bp, remove it
00101 SharedPointer<BeliefWithState> BeliefTransitionMOMDP::nextBelief2(SharedPointer<BeliefWithState> bp, int a, int o, int obsX, SharedPointer<SparseVector>& jspv )
00102 {
00103         SharedPointer<MOMDP> momdpProblem = dynamic_pointer_cast<MOMDP> (problem);
00104         //cout << " jspv " << endl;
00105         //jspv.write(cout) << endl;
00106 
00107         SharedPointer<BeliefWithState> result (new BeliefWithState());
00108         emult_column( *result->bvec, *(momdpProblem->obsProb->getMatrix(a, obsX)), o,  *jspv);
00109         (*result->bvec) *= (1.0/result->bvec->norm_1());
00110         
00111         result->sval = obsX;
00112         return result;
00113 
00114 }


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