MultiAgentDecisionProcess  Release 0.2.1
JointBelief.cpp
Go to the documentation of this file.
1 
28 #include "JointBelief.h"
29 #include <float.h>
30 //Necessary as header file contains a forward declaration:
32 #include <typeinfo>
33 
34 #include "TGet.h"
35 
36 using namespace std;
37 
38 #define JointBelief_doSanityCheckAfterEveryUpdate 0
39 
41  Belief(size)
42 {
43 }
44 
45 JointBelief::JointBelief(const vector<double> &belief) :
46  Belief(belief)
47 {
48 }
49 
51  Belief(belief)
52 {
53 }
55  Belief(belief)
56 {}
57 
58 //Destructor
60 {
61 }
62 
65 {
66  if (this == &o) return *this; // Gracefully handle self assignment
67 
69 
70  return(*this);
71 }
72 
75 {
76  //this code is called when we perform
77  // jb1 = jb2
78  // and jb1 is a JointBelief (I.e., this is the operator= function of jb1
79  // and *this* is a JointBelief, since we got here)
80  //
81  // Therefore this code assumes that jb2 (I.e., 'o') is also a JointBelief
82  // (and not a JointBeliefSparse or something like that).
83  //
84  // If it is something else, we get in trouble here!
85  if (this == &o) return *this; // Gracefully handle self assignment
86  try{
87  const JointBelief& casted_o = dynamic_cast<const JointBelief&>(o);
88  return(operator=(casted_o));// call the operator= for JointBelief
89  }catch(std::bad_cast bc){
90  throw E("JointBelief::operator= bad_cast exception. Are you trying to assign me (a JointBelief) with some other JointBeliefInterface (e.g. JointBeliefSparse) ??");
91  }
92 
93 }
94 
96  Index lastJAI, Index newJOI)
97 {
98  double Po_ba = 0.0; // P(o|b,a) with o=newJO
99  vector<double> newJB_unnorm;
100  size_t nrS = pu.GetNrStates();
101 
102  TGet* T = 0;
103  T = pu.GetTGet();
104  if(T != 0)
105  {
106  for(Index sI=0; sI < nrS; sI++)
107  {
108  //P(newJOI | lastJAI, sI) :
109  double Po_as = pu.GetObservationProbability(lastJAI, sI, newJOI);
110  //P(sI | b, a) = sum_(prec_s) P(sI | prec_s, a)*JB(prec_s)
111  double Ps_ba = 0.0;
112  //for(BScit it=_m_b.begin(); it!=_m_b.end(); ++it)
113  //Ps_ba += T->Get(it.index(), lastJAI, sI) * *it;
114  for(Index prec_sI=0; prec_sI < nrS; prec_sI++)
115  Ps_ba += T->Get(prec_sI, lastJAI, sI) * _m_b[prec_sI];
116  //Ps_ba += pu.GetTransitionProbability(prec_sI, lastJAI, sI) *
117  //_m_b[prec_sI];
118 
119  //the new (unormalized) belief P(s,o|b,a)
120  double Pso_ba = Po_as * Ps_ba;
121 
122  newJB_unnorm.push_back(Pso_ba); //unormalized new belief
123  Po_ba += Pso_ba; //running sum of P(o|b,a)
124  }
125  }
126  else
127  {
128  for(Index sI=0; sI < nrS; sI++)
129  {
130  //P(newJOI | lastJAI, sI) :
131  double Po_as = pu.GetObservationProbability(lastJAI, sI, newJOI);
132  //P(sI | b, a) = sum_(prec_s) P(sI | prec_s, a)*JB(prec_s)
133  double Ps_ba = 0.0;
134  //for(BScit it=_m_b.begin(); it!=_m_b.end(); ++it)
135  //Ps_ba += T->Get(it.index(), lastJAI, sI) * *it;
136  for(Index prec_sI=0; prec_sI < nrS; prec_sI++)
137  Ps_ba += pu.GetTransitionProbability(prec_sI, lastJAI, sI) *
138  _m_b[prec_sI];
139 
140  //the new (unormalized) belief P(s,o|b,a)
141  double Pso_ba = Po_as * Ps_ba;
142 
143  newJB_unnorm.push_back(Pso_ba); //unormalized new belief
144  Po_ba += Pso_ba; //running sum of P(o|b,a)
145  }
146  //throw E("JointBelief::Update tried to obtain a TGet, but apparently the transition model is not cached? (it should be, since this belief update loops over all states it should be possible to cache the transition model)");
147  }
148 
149  //normalize:
150  if(Po_ba>0)
151  for(Index sI=0; sI < nrS; sI++)
152  _m_b[sI]=newJB_unnorm[sI]/Po_ba;
153 
154  delete T;
155 
156 #if JointBelief_doSanityCheckAfterEveryUpdate
157  if(!SanityCheck())
158  throw(E("JointBelief::Update SanityCheck failed"));
159 #endif
160 
161  return(Po_ba);
162 }