MultiAgentDecisionProcess  Release 0.2.1
QFunctionJAOHTree.cpp
Go to the documentation of this file.
1 
28 #include "QFunctionJAOHTree.h"
30 #include "MDPSolver.h"
32 #include "JointBeliefInterface.h"
34 
35 using namespace std;
36 
37 #define DEBUG_QHEUR_COMP_TREE 0
38 
39 //Default constructor
42  QFunctionForDecPOMDP(pu), //virtual base first
43  QFunctionJAOH(pu)
44 {
45  _m_initialized = false;
46 }
47 
48 //Destructor
50 {
51  DeInitialize();
52 }
53 
55 {
56  _m_QValues.clear();
57  _m_initialized=false;
58 }
59 
61 {
62  _m_QValues.resize(GetPU()->GetNrJointActionObservationHistories(),
63  GetPU()->GetNrJointActions(),
64  false);
65  _m_initialized = true;
66 }
67 
69 {
70  DeInitialize();
72 }
73 
75 {
76  if(!_m_initialized)
77  Initialize();
78 
79  ComputeQ();
80 }
81 
82 void QFunctionJAOHTree::Save(string filename) const
83 {
85 }
86 
87 void QFunctionJAOHTree::Load(string filename)
88 {
90  GetPU()->
91  GetNrJointActionObservationHistories(),
92  GetPU()->GetNrJointActions());
93 }
94 
96 {
97  if(GetPU() == 0)
98  throw E("QFunctionJAOHTree::ComputeQ - GetPU() returns 0; no PlanningUnit available!");
99 
100  size_t time_step = 0;
101 #if QFunctionJAOH_useIndices
102 #else
104  GetJointActionObservationHistoryTree(Globals::INITIAL_JAOHI);
105 #endif
107  JointBeliefInterface& b0 = *b0p;
108 
109  bool last_t = false;
110  if( (time_step + 1) == GetPU()->GetHorizon()) //unlikely, but possible
111  last_t = true;
112 
113  if(DEBUG_QHEUR_COMP_TREE){cout << "QFunctionJAOHTree::Compute() called" << endl;}
114 
115  //in the first time_step t=0, there is no previous action and there is only
116  //the empty observation action history.
117  //Therefore we're going to construct a Bayesian game where there is only 1
118  //type for each agent.
119  vector<size_t> nrTypes = vector<size_t>(GetPU()->GetNrAgents(), 1);
120  BayesianGameIdenticalPayoff bg_time_step(GetPU()->GetNrAgents(),
121  GetPU()->GetNrActions(),nrTypes);
122 
123  size_t empty_jaohI = 0;
124  bg_time_step.SetProbability(empty_jaohI, 1.0);
125  for(Index newJAI=0; newJAI < GetPU()->GetNrJointActions(); newJAI++)
126  {
127  //calculate R(joah',newJA) - expected immediate reward for time_step
128  double exp_imm_R = 0.0;
129 #if USE_BeliefIteratorGeneric
131  do exp_imm_R += it.GetProbability() *
132  GetPU()->GetReward(it.GetStateIndex(), newJAI);
133  while (it.Next());
134 #else
135  for(Index sI=0; sI < GetPU()->GetNrStates(); sI++)
136  exp_imm_R += (b0.Get(sI)) * GetPU()->GetReward(sI, newJAI);
137 #endif
138  //calculate Q(jaoh', newJA) = R(joah',newJA) + exp. future R
139  // and the exp. future R = ComputeRecursively(t+1, root, newJA)
140  double exp_fut_R = 0.0;
141  if(!last_t)
142 #if QFunctionJAOH_useIndices
143  exp_fut_R = ComputeRecursively( 1, Globals::INITIAL_JAOHI, newJAI);
144 #else
145  exp_fut_R = ComputeRecursively( 1, root, newJAI);
146 #endif
147  double Q = exp_imm_R + GetPU()->GetDiscount() * exp_fut_R;
148  _m_QValues(empty_jaohI,newJAI)=Q;
149  bg_time_step.SetUtility(empty_jaohI, newJAI, Q);
150  }//end for newJAI
152  {
153  cout << "QFunctionJAOHTree::ComputeQ() for..."<<endl<<
154  " time_step=0 called, with ISD=";
155  b0.Print();
156  cout <<endl;
157  bg_time_step.Print();
158  }
159  //solve this bayesian game
161  double v = bgs.Solve();
163  cout << "QFunctionJAOHTree::ComputeQ() - Expected V(b0) = " << v << endl<< endl;
164  delete b0p;
165  return;
166 }