MultiAgentDecisionProcess  Release 0.2.1
PlanningUnitMADPDiscrete.cpp
Go to the documentation of this file.
1 
29 
30 #include "IndexTools.h"
31 
32 #include "Action.h"
33 #include "ActionDiscrete.h"
34 #include "JointActionDiscrete.h"
35 #include "Observation.h"
36 #include "ObservationDiscrete.h"
38 
39 #include "JointBelief.h"
40 #include "JointBeliefSparse.h"
41 #include "JointPolicyDiscrete.h"
42 
43 #include "ActionHistory.h"
44 #include "ActionHistoryTree.h"
45 #include "JointActionHistory.h"
46 #include "JointActionHistoryTree.h"
47 #include "ObservationHistoryTree.h"
51 
52 #include "ENotCached.h"
53 #include "EOverflow.h"
54 
55 #define DEBUG_PUD 0
56 
57 // debug individual observation/action/actionobservation histories generation
58 #define DEBUG_PUDCOHT 0
59 #define DEBUG_PUDCAHT 0
60 #define DEBUG_PUDCAOHT 0
61 
62 // debug joint observation/action/actionobservation histories generation
63 #define DEBUG_PUDCJOHT 0
64 #define DEBUG_PUDCJAHT 0
65 #define DEBUG_PUDCJAOHT 0
66 
67 #define DEBUG_PUD_JAOHPROBS 0
68 #define DEBUG_PUDCGETJB 0
69 
70 using namespace std;
71 
72 //Default constructor
75  size_t horizon,
77  ) :
78  PlanningUnit(horizon,p),
81 {
82  if(DEBUG_PU_CONSTRUCTORS) cout << "PlanningUnitMADPDiscrete(PlanningUnitMADPDiscreteParameters params, size_t horizon, MultiAgentDecisionProcessDiscreteInterface* p) called" << endl;
83  _m_initialized = false;
84  params.SanityCheck();
85  _m_params=params;
86  if(p != 0)
87  Initialize();
88 }
89 
91 PlanningUnitMADPDiscrete(size_t horizon,
93  ) :
94  PlanningUnit(horizon,p),
97 {
98  if(DEBUG_PU_CONSTRUCTORS) cout << "PlanningUnitMADPDiscrete( size_t horizon, MultiAgentDecisionProcessDiscreteInterface* p) called" << endl;
100  _m_initialized = false;
101  if(p != 0)
102  Initialize();
103 }
104 
105 //Destructor
107 {
108  Deinitialize();
109 }
110 
112 {
113  if(DEBUG_PUD){
114  cout << "\n>>>PlanningUnitMADPDiscrete::SetProblem - setting the following problem\n"
115  << m->SoftPrint() << endl;
116  }
117  SetReferred(m);
119  Deinitialize();
120  Initialize();
121 }
122 
124 {
125  Deinitialize();
127  Initialize();
128 }
129 
130 void
133 {
134  Deinitialize();
135  _m_params=params;
136  Initialize();
137 }
138 
140 {
141  return(true);
142 }
143 
144 //call methods to initialize the Planning unit
146 {
147  if(DEBUG_PUD) cout << "PlanningUnitMADPDiscrete::Initialize()" <<endl;
148 
149  // We cannot initialize before the problem is set, so return
150  // (SetProblem() will call Initialize() again)
151  if(GetReferred()==0)
152  return;
153 
154  if(!SanityCheck())
155  throw(E("PlanningUnitMADPDiscrete::Initialize sanity check failed"));
156 
157  if(_m_initialized)
158  Deinitialize();
159 
160  try
161  {
162  // calculating this stuff does not make sense for
163  // infinite-horizon problems
164  if(GetHorizon()!=MAXHORIZON)
165  {
172  }
173  }
174  catch(E& e)
175  {
176  e.Print();
177  return;
178  }
179  _m_initialized = true;
180 if(DEBUG_PUD){ cout << "PlanningUnitMADPDiscrete::Initialize() done - info:" <<endl; Print(); }
181 }
182 
184 {
185  if(_m_initialized)
186  {
187  if(GetHorizon()!=MAXHORIZON)
188  {
189  // delete old stuff if necessary:
196  }
197  _m_initialized=false;
198  }
199 }
200 
202 {
204  {
205  //constructs the observation histories for all agents and puts them in trees.
206  size_t nrAgents=GetNrAgents();
207  for(Index i = 0; i < nrAgents; i++)
208  {
209  vector<ObservationHistoryTree*> * temp = new
210  vector<ObservationHistoryTree*>;
211  _m_observationHistoryTreeVectors.push_back(* temp );
212  _m_nrObservationHistoriesT.push_back(vector<size_t>());
213  _m_firstOHIforT.push_back(vector<LIndex>());
214  delete temp; // temp is copied into the vector - so delete this imm.
216  _m_observationHistoryTreeRootPointers.push_back(treeThiAg);
217  }
218  }
219  else
220  {
221  /*Calculates the number of individual observation histories
222  * and stores this in _m_nrObservationHistories (and
223  * _m_nrObservationHistoriesT). Only used when the observation
224  * histories are not created.
225  */
228  _m_firstOHIforT.clear();
229  vector<size_t> oh_per_t;
230  vector<LIndex> firstOHindex_per_t;
231  size_t total = 0;
232  for(Index i=0; i < GetNrAgents(); i++)
233  {
234  size_t nrOi = GetReferred()->GetNrObservations(i);
235  oh_per_t.clear();
236  firstOHindex_per_t.clear();
237  total = 0;
238  for(Index t=0; t < GetHorizon(); t++)
239  {
240  firstOHindex_per_t.push_back(total);
241  size_t oh_this_t = (size_t) pow( (double)nrOi, (double)t );
242  oh_per_t.push_back(oh_this_t);
243  total += oh_this_t;
244  }
245  _m_firstOHIforT.push_back(firstOHindex_per_t);
246  _m_nrObservationHistories.push_back(total);
247  _m_nrObservationHistoriesT.push_back(oh_per_t);
248  }
249  }
250 }
251 
252 //should only be called from CreateObservationHistories()
253 //(should be called in the order of the agents)
256 {
257  //we want to number the Observation Histories breadth-first, so we
258  //use a queue:
259  queue<ObservationHistoryTree*> ohtQueue;
260 
261  Index ObservationHistoryIndex = 0;
262  Index maxLength = GetHorizon()-1;//max. observation hist length = h-1
263  size_t nrO = GetNrObservations(agentI);
264  ObservationHistory* next_oh = 0;
265  ObservationHistoryTree* next_oht = 0;
266 
267  // return a new initial (=empty) observation history
268 
269  ObservationHistory* oh = new ObservationHistory(*this, agentI);
270  ObservationHistoryTree* root = new ObservationHistoryTree(oh);//, nrO);
271  ohtQueue.push(root);
272 
273  Index ts = 0;
274  _m_firstOHIforT[agentI].push_back(ObservationHistoryIndex);
275  while(ohtQueue.size() > 0)
276  {
277  ObservationHistoryTree* oht = ohtQueue.front();
278  oht->SetIndex(ObservationHistoryIndex);
279  _m_observationHistoryTreeVectors[agentI].push_back(oht);
280 
281  size_t thisLength = oht->GetContainedElement()->GetLength();
282  if(ts != (Index) thisLength) //length 1 <-> ts 1
283  {
284  // we've now started processing OHs for the next ts
285  Index nr_OHI_for_ts = ObservationHistoryIndex -
286  _m_firstOHIforT[agentI][ts];
287 #if DEBUG_PUDCOHT
288  cout << "First oh for next ts+1: store nr of ts-OH..."<<endl<<
289  "_m_firstOHIforT[agentI="<<agentI<<"][ts="<<ts<<"] = "<<
290  _m_firstOHIforT[agentI][ts] << ", current OHI = " <<
291  ObservationHistoryIndex << endl;
292  Index tempIndex = _m_nrObservationHistoriesT[agentI].size();
293 #endif
294  _m_nrObservationHistoriesT[agentI].push_back(nr_OHI_for_ts);
295 #if DEBUG_PUDCOHT
296  cout << "Set _m_nrObservationHistoriesT[agentI="<<agentI<<"][t="<<
297  tempIndex <<" (ts="<<ts<<")] = "<< nr_OHI_for_ts << endl;
298 #endif
299  if(++ts != (Index) thisLength )
300  cout << "ERROR: CreateObservationHistories: ++ts != "<<
301  "(Index) thisLength !!! ts (now) is: "<<ts<<
302  "thisLength =" << thisLength <<endl;
303  _m_firstOHIforT[agentI].push_back(ObservationHistoryIndex);
304  }
305 
306  ObservationHistoryIndex++; //index of an Observation HISTORY
307 
308  if(thisLength < maxLength)
309  for(Index obsI = 0; obsI < nrO; obsI++) //obsI = observation index
310  {
311  next_oh = new ObservationHistory(obsI,
312  oht->GetObservationHistory());
313  next_oht = new ObservationHistoryTree(next_oh);//, nrO);
314  oht->SetSuccessor(obsI, next_oht);
315  ohtQueue.push(next_oht);
316  }
317  ohtQueue.pop();
318  }
319 
320  //there is no next time step, so _m_nrObservationHistoriesT[agentI][lastTs]
321  //have not been stored yet: do this now
322  Index nr_OHI_for_ts = (ObservationHistoryIndex)
323  - _m_firstOHIforT[agentI][ts];
324 #if DEBUG_PUDCOHT
325  cout << "Last ts end: store nr of ts=h-1 OH..."<<endl<<
326  "_m_firstOHIforT[agentI="<<agentI<<"][ts="<<ts<<"] = "<<
327  _m_firstOHIforT[agentI][ts] << ", current OHI = " <<
328  ObservationHistoryIndex << endl;
329  Index tempIndex = _m_nrObservationHistoriesT[agentI].size();
330 #endif
331  _m_nrObservationHistoriesT[agentI].push_back(nr_OHI_for_ts);
332 #if DEBUG_PUDCOHT
333  cout << "Set _m_nrObservationHistoriesT[agentI="<<agentI<<"][t="<<tempIndex
334  <<" (last time step ts="<<ts<<")] = "<< nr_OHI_for_ts<<endl;
335 #endif
336  //in a horizon h MADP without initial observation, the max. observation
337  //sequence length is h-1 observations (and the minimum is 0).
338  size_t nrObsHist;
339  size_t length = GetHorizon() - 1; // = maxLength - 1
340  nrObsHist = IndexTools::CalculateNumberOfSequences(nrO, length);
341 
342  if( ObservationHistoryIndex != nrObsHist ||
343  nrObsHist != _m_observationHistoryTreeVectors[agentI].size() )
344  cerr << " WARNING:ObservationHistoryIndex="<< ObservationHistoryIndex <<
345  " nrObsHist=" << nrObsHist <<" _m_observationHistoryTreeVectors[agentI="
346  << agentI<<"].size()=" << _m_observationHistoryTreeVectors[agentI].size()
347  << endl;
348 
349  _m_nrObservationHistories.push_back(ObservationHistoryIndex);
350 
351  return(root);
352 }
353 
354 //constructs the observation histories for all agents and puts them in trees.
356 {
357  vector<ObservationHistoryTree *>::iterator it =
359  vector<ObservationHistoryTree *>::iterator last =
361  while(it != last)
362  {
363  if( *it != 0 )
364  delete *it; //this recursively deletes the
365  //whole ObservationHistoryTree
366  it++;
367  }
368 /* this is done automatically by compiler:
369  while(int s = _m_observationHistoryTreeVectors.size() > 0 )
370  {
371  _m_observationHistoryTreeVectors[s-1].clear();
372  _m_observationHistoryTreeVectors.pop_back();
373  }
374 */
379  _m_firstOHIforT.clear();
380 }
381 
383 {
385  {
386  queue<JointObservationHistoryTree*> johtQueue;
387 
388  Index JointObservationHistoryIndex = 0;
389  Index maxLength = GetHorizon() -1 ;
390  //max length of observation history o1...oh-1
391  size_t nrJO = GetNrJointObservations();
392  JointObservationHistory* next_joh = 0;
393  JointObservationHistoryTree* next_joht = 0;
394 
395  //start with the initial (=empty) joint obs. hist.
396  JointObservationHistory* joh_init = new JointObservationHistory(*this);
398  joh_init);
399  johtQueue.push(root);
400 
401  Index ts = 0;
402  _m_firstJOHIforT.push_back(JointObservationHistoryIndex);
403  while(johtQueue.size() > 0)
404  {
405  JointObservationHistoryTree* joht = johtQueue.front();
406  joht->SetIndex(JointObservationHistoryIndex);
407  _m_jointObservationHistoryTreeVector.push_back(joht);
408 
409  Index thisLength = joht->GetContainedElement()->GetLength();
410  if(ts != thisLength ) //length 0 <-> ts 0
411  {
412  // we've now started processing JOHs for the next ts
414  JointObservationHistoryIndex - _m_firstJOHIforT[ts]);
415  if(++ts != thisLength )
416  cerr << "ERROR: CreateJointObservationHistories: ++ts != "<<
417  "(Index) thisLength !!! ts (now) is: "<<ts<<
418  "thisLength =" << thisLength <<endl;
419  _m_firstJOHIforT.push_back(JointObservationHistoryIndex);
420  }
421 
422  JointObservationHistoryIndex++; //index of an joint Observation history
423 #if DEBUG_PUDCJOHT
424  cout << "PlanningUnitMADPDiscrete::"<<
425  "CreateJointObservationHistoryTree - New joh tree from queue:"<<
426  endl<<"\t"; joht->Print();
427 #endif
428 
429  if(thisLength < maxLength)
430  {
431 #if DEBUG_PUDCJOHT
432  cout << "PlanningUnitMADPDiscrete::"<<
433  "CreateJointObservationHistoryTree - Extending this joint "<<
434  "observation history tree..."<<endl;
435 #endif
436  for(Index jObsI = 0; jObsI < nrJO; jObsI++)
437  {
438 #if DEBUG_PUDCJOHT
439  cout << "PlanningUnitMADPDiscrete::Create"
440  << "JointObservationHistoryTree - creating successor for "
441  << "joint observation "<< jObsI << endl;
442 #endif
443  JointObservationHistory* temp = joht->
444  GetJointObservationHistory();
445 #if DEBUG_PUDCJOHT
446  cout << "( history of pred was:";
447  temp->Print(); cout << ") " << endl;
448 #endif
449  next_joh = new JointObservationHistory(jObsI,temp);
450  next_joht = new JointObservationHistoryTree(next_joh);
451  joht->SetSuccessor(jObsI, next_joht);
452  johtQueue.push(next_joht);//this is safe - only the pointer is
453  //copied
454  }
455  }
456  johtQueue.pop();
457 
458  }
459  //for the last time step, the nrJointObs are not yet added, do it now:
461  (JointObservationHistoryIndex - _m_firstJOHIforT[ts]);
463 
465 
466  }
467  else
468  {
469  /*Calculates the number of joint observation histories and
470  * stores this in _m_nrJointObservationHistories (and
471  * _m_nrJointObservationHistoriesT).
472  * Only used when the joint observation histories are not created.
473  */
474  size_t nr=0;
475  for (Index t=0; t<GetHorizon() ;t++ )
476  {
477  _m_firstJOHIforT.push_back(nr);
478  size_t nrT=1;
479  for (Index aI=0 ; aI<GetNrAgents(); aI++)
480  {
481  nrT *= _m_nrObservationHistoriesT[aI][t];
482  }
483  _m_nrJointObservationHistoriesT.push_back(nrT);
484  nr += nrT;
485  }
488  }
489 }
490 
492 {
493  delete _m_jointObservationHistoryTreeRoot;//should recursively delet
497  _m_firstJOHIforT.clear();
498 }
499 
500 
501 //create / clear action histories:
502 
504 {
506  {
507  size_t nrAgents=GetNrAgents();
508  for(Index i = 0; i < nrAgents; i++)
509  {
510  vector<ActionHistoryTree*> * temp = new
511  vector<ActionHistoryTree*>;
512  _m_actionHistoryTreeVectors.push_back(* temp );
513  _m_nrActionHistoriesT.push_back(vector<size_t>());
514  _m_firstAHIforT.push_back(vector<LIndex>());
515  delete temp; // temp is copied into the vector - so delete this imm.
517  _m_actionHistoryTreeRootPointers.push_back(treeThiAg);
518  }
519  }
520  else
521  {
522  /*Calculates the number of individual action histories and
523  * stores this in _m_nrActionHistories (and
524  * _m_nrActionHistoriesT).
525  * Only used when the Action histories are not created.
526  */
527  _m_nrActionHistories.clear();
528  _m_nrActionHistoriesT.clear();
529  _m_firstAHIforT.clear();
530  vector<size_t> oh_per_t;
531  vector<LIndex> firstAHindex_per_t;
532  size_t total = 0;
533  for(Index i=0; i < GetNrAgents(); i++)
534  {
535  size_t nrAi = GetReferred()->GetNrActions(i);
536  oh_per_t.clear();
537  firstAHindex_per_t.clear();
538  total = 0;
539  for(Index t=0; t < GetHorizon(); t++)
540  {
541  firstAHindex_per_t.push_back(total);
542  size_t oh_this_t = (size_t) pow( (double)nrAi, (double)t );
543  oh_per_t.push_back(oh_this_t);
544  total += oh_this_t;
545  }
546  _m_firstAHIforT.push_back(firstAHindex_per_t);
547  _m_nrActionHistories.push_back(total);
548  _m_nrActionHistoriesT.push_back(oh_per_t);
549  }
550  }
551 }
552 
553 //should only be called from CreateActionHistories()
554 //(should be called in the order of the agents)
556 {
557  //we want to number the Action Histories breadth-first, so we
558  //use a queue:
559  queue<ActionHistoryTree*> ohtQueue;
560 
561  Index ActionHistoryIndex = 0;
562  int maxLength = GetHorizon() - 1;//max. action hist length = h - 1
563  size_t nrO = GetNrActions(agentI);
564  ActionHistory* next_oh = 0;
565  ActionHistoryTree* next_oht = 0;
566 
567  // return a new initial (=empty) action history
568 
569  //action history never has an initial action
570  //if(!GetReferred()->IsInitialActionEmpty())
571  // throw E("this MADP issues a non-empty initial action, but this is not yet supported by PlanningUnitMADPDiscrete::CreateActionHistoryTree");
572 
573  ActionHistory* oh = new ActionHistory(*this, agentI);
574  ActionHistoryTree* root = new ActionHistoryTree(oh);//, nrO);
575  ohtQueue.push(root);
576  Index ts = 0;
577  _m_firstAHIforT[agentI].push_back(ActionHistoryIndex);
578  while(ohtQueue.size() > 0)
579  {
580  ActionHistoryTree* oht = ohtQueue.front();
581  oht->SetIndex(ActionHistoryIndex);
582  _m_actionHistoryTreeVectors[agentI].push_back(oht);
583  int thisLength = oht->GetContainedElement()->GetLength();
584  if(ts != (Index) thisLength ) //length 0 <-> ts 0
585  {
586  // we've now started processing AHs for the next ts
587  Index nr_AHI_for_ts = ActionHistoryIndex -
588  _m_firstAHIforT[agentI][ts];
589 #if DEBUG_PUDCAHT
590  cout << "First ah for next ts+1: store nr of ts-AH..."<<endl<<
591  "_m_firstAHIforT[agentI="<<agentI<<"][ts="<<ts<<"] = "<<
592  _m_firstAHIforT[agentI][ts] << ", current AHI = " <<
593  ActionHistoryIndex << endl;
594  Index tempIndex = _m_nrActionHistoriesT[agentI].size();
595 #endif
596  _m_nrActionHistoriesT[agentI].push_back(nr_AHI_for_ts);
597 #if DEBUG_PUDCAHT
598  cout << "Set _m_nrActionHistoriesT[agentI="<<agentI<<"][t="<<
599  tempIndex <<" (ts="<<ts<<")] = "<< nr_AHI_for_ts <<endl;
600 #endif
601  if(++ts != (Index) thisLength)
602  cerr << "ERROR: CreateActionHistories: ++ts != "<<
603  "(Index) thisLength !!! ts (now) is: "<<ts<<
604  "thisLength =" << thisLength <<endl;
605  _m_firstAHIforT[agentI].push_back(ActionHistoryIndex);
606  }
607 
608  ActionHistoryIndex++; //index of an Action HISTORY
609 
610  if(thisLength < maxLength)
611  for(Index obsI = 0; obsI < nrO; obsI++) //obsI = action index
612  {
613  next_oh = new ActionHistory(obsI,
614  oht->GetActionHistory());
615  next_oht = new ActionHistoryTree(next_oh);//, nrO);
616  oht->SetSuccessor(obsI, next_oht);
617  ohtQueue.push(next_oht);
618  }
619  ohtQueue.pop();
620  }
621  //there is no next time step, so _m_nrObservationHistoriesT[agentI][lastTs]
622  //have not been stored yet: do this now
623  Index nr_AHI_for_ts = ActionHistoryIndex
624  - _m_firstAHIforT[agentI][ts];
625 #if DEBUG_PUDCAHT
626  cout << "Last ts end: store nr of ts=h-1 AH..."<<endl<<
627  "_m_firstAHIforT[agentI="<<agentI<<"][ts="<<ts<<"] = "<<
628  _m_firstAHIforT[agentI][ts] << ", current AHI = " <<
629  ActionHistoryIndex << endl;
630  Index tempIndex = _m_nrActionHistoriesT[agentI].size();
631 #endif
632  _m_nrActionHistoriesT[agentI].push_back(nr_AHI_for_ts);
633 #if DEBUG_PUDCAHT
634  cout << "Set _m_nrActionHistoriesT[agentI="<<agentI<<"][t="<<tempIndex
635  <<" (last time step ts="<<ts<<")] = "<< nr_AHI_for_ts<<endl;
636 #endif
637 
638  //in a horizon h MADP without initial observation, the max. action
639  //sequence length is h-1 actions (and the minimum is 0).
640  //(after h actions the problem is ended already - length-h action sequences
641  //are not considered.)
642  size_t nrActionHist;
643  size_t length = GetHorizon() - 1; // = maxLength
644  nrActionHist = IndexTools::CalculateNumberOfSequences(nrO, length);
645 
646  if( ActionHistoryIndex != nrActionHist ||
647  nrActionHist != _m_actionHistoryTreeVectors[agentI].size() )
648  cerr << " WARNING:ActionHistoryIndex="<< ActionHistoryIndex <<
649  " nrActionHistories=" << nrActionHist <<
650  " _m_actionHistoryTreeVectors[agentI="<<agentI<<"].size()="<<
651  _m_actionHistoryTreeVectors[agentI].size() << endl;
652 
653  _m_nrActionHistories.push_back(ActionHistoryIndex);
654 
655  return(root);
656 }
657 
659 {
661  {
662  queue<JointActionHistoryTree*> jahtQueue;
663 
664  Index JointActionHistoryIndex = 0;
665  int maxLength = GetHorizon() - 1 ;//max length of action history o0...oh-1
666  size_t nrJA = GetNrJointActions();
667  JointActionHistory* next_jah = 0;
668  JointActionHistoryTree* next_jaht = 0;
669 
670  //start with the initial (=empty) joint obs. hist.
671  JointActionHistory* jah_init = new JointActionHistory(*this);
673  jah_init);
674  jahtQueue.push(root);
675  while(jahtQueue.size() > 0)
676  {
677  JointActionHistoryTree* jaht = jahtQueue.front();
678  jaht->SetIndex(JointActionHistoryIndex);
679  _m_jointActionHistoryTreeVector.push_back(jaht);
680  JointActionHistoryIndex++; //index of an joint Action history
681  int thisLength = jaht->GetContainedElement()->GetLength();
682 
683 #if DEBUG_PUDCJAHT
684  cout << "PlanningUnitMADPDiscrete::CreateJointActionHistoryTree "
685  << "- New jah tree from queue:"<<endl<<"\t"; jaht->Print();
686 #endif
687  if(thisLength < maxLength)
688  {
689 #if DEBUG_PUDCJAHT
690  cout << "PlanningUnitMADPDiscrete::CreateJointActionHistoryTree "
691  << "- Extending this joint action history tree..."<<endl;
692 #endif
693  for(Index jAI = 0; jAI < nrJA; jAI++) //jAI = action index
694  {
695 #if DEBUG_PUDCJAHT
696  cout << "PlanningUnitMADPDiscrete::CreateJointActionHistoryTree"
697  << " - creating successor for joint action "
698  << jAI << endl;
699 #endif
700  JointActionHistory* temp = jaht->
702 #if DEBUG_PUDCJAHT
703  cout << "( history of pred was:";
704  temp->Print(); cout << ") " << endl;
705 #endif
706  next_jah = new JointActionHistory(jAI,temp);
707  next_jaht = new JointActionHistoryTree(next_jah);
708  jaht->SetSuccessor(jAI, next_jaht);
709  jahtQueue.push(next_jaht);//this is safe - only the pointer is
710  //copied
711  }
712  }
713  jahtQueue.pop();
714 
715  }
717 
719  }
720  else
721  {
722  /*Calculates the number of joint action histories and
723  * stores this in _m_nrJointActionHistories (and
724  * _m_nrJointActionHistoriesT).
725  * Only used when the joint action histories are not created.
726  */
727  size_t nr=0;
728  for (Index t=0; t<GetHorizon() ;t++ )
729  {
730  _m_firstJAHIforT.push_back(nr);
731  size_t nrT=1;
732  for (Index aI=0 ; aI<GetNrAgents(); aI++)
733  {
734  nrT *= _m_nrActionHistoriesT[aI][t];
735  }
736  _m_nrJointActionHistoriesT.push_back(nrT);
737  nr += nrT;
738  }
741  }
742 }
743 
745 {
746  vector<ActionHistoryTree *>::iterator it =
748  vector<ActionHistoryTree *>::iterator last =
750  while(it != last)
751  {
752  if( *it != 0 )
753  delete *it; //this recursively deletes the
754  //whole ActionHistoryTree
755  it++;
756  }
757 /* this is done automatically by compiler:
758  while(int s = _m_actionHistoryTreeVectors.size() > 0 )
759  {
760  _m_actionHistoryTreeVectors[s-1].clear();
761  _m_actionHistoryTreeVectors.pop_back();
762  }
763 */
766  _m_nrActionHistories.clear();
767  _m_nrActionHistoriesT.clear();
768  _m_firstAHIforT.clear();
769 
770 }
771 
773 {
774  delete _m_jointActionHistoryTreeRoot;//should recursively delet
778  _m_firstJAHIforT.clear();
779 }
780 
782 {
783  vector<ActionObservationHistoryTree *>::iterator it =
785  vector<ActionObservationHistoryTree *>::iterator last =
787  while(it != last)
788  {
789  if( *it != 0 )
790  delete *it; //this recursively deletes the
791  //whole ActionObservationHistoryTree
792  it++;
793  }
798  _m_firstAOHIforT.clear();
799 }
800 
802 {
803  delete _m_jointActionObservationHistoryTreeRoot;//should recursively delete
804 
806 
807  // clear the map
808 #if 0 // TODO: fix this, segfaults currently, and hence leaks memory
810  {
811  delete _m_jointActionObservationHistoryTreeMap.begin()->second;
814  }
815 #else // not sure whether everything will be deleted now
818 #endif
820 
822  {
823  vector<const JointBeliefInterface*>::iterator it =
824  _m_jBeliefCache.begin();
825  vector<const JointBeliefInterface*>::iterator last =
826  _m_jBeliefCache.end();
827 
828  while(it != last)
829  {
830  delete(*it);
831  it++;
832  }
833  _m_jBeliefCache.clear();
834  _m_jaohConditionalProbs.clear();
835  }
836 
839  _m_firstJAOHIforT.clear();
840 }
841 
843 {
845  {
846  size_t nrAgents=GetNrAgents();
847  for(Index i = 0; i < nrAgents; i++)
848  {
849  vector<ActionObservationHistoryTree*> * temp = new
850  vector<ActionObservationHistoryTree*>;
851  _m_actionObservationHistoryTreeVectors.push_back(* temp );
852  _m_nrActionObservationHistoriesT.push_back(vector<size_t>());
853  _m_firstAOHIforT.push_back(vector<LIndex>());
854  delete temp; // temp is copied into the vector - so delete this imm.
855  ActionObservationHistoryTree* treeThiAg =
857  _m_actionObservationHistoryTreeRootPointers.push_back(treeThiAg);
858  }
859  }
860  else
861  {
862  /*Calculates the number of individual action-observation histories and
863  * stores this in _m_nrActionObservationHistories (and
864  * _m_nrActionObservationHistoriesT).
865  * Only used when the action-observation histories are not created.
866  */
869  _m_firstAOHIforT.clear();
870  vector<size_t> oh_per_t;
871  vector<LIndex> firstAOHindex_per_t;
872  size_t total = 0;
873  for(Index i=0; i < GetNrAgents(); i++)
874  {
875  size_t nrAOi = GetReferred()->GetNrActions(i) *
877  oh_per_t.clear();
878  firstAOHindex_per_t.clear();
879  total = 0;
880  for(Index t=0; t < GetHorizon(); t++)
881  {
882  firstAOHindex_per_t.push_back(total);
883  size_t oh_this_t = (size_t) pow( (double)nrAOi, (double)t );
884  oh_per_t.push_back(oh_this_t);
885  total += oh_this_t;
886  }
887  _m_firstAOHIforT.push_back(firstAOHindex_per_t);
888  _m_nrActionObservationHistories.push_back(total);
889  _m_nrActionObservationHistoriesT.push_back(oh_per_t);
890 
891  // need to some similar as for the JointActionObservationHistoryTree
892 
893 // vector<ActionObservationHistoryTree*> * temp = new
894 // vector<ActionObservationHistoryTree*>;
895 // _m_actionObservationHistoryTreeVectors.push_back(* temp );
896 // _m_nrActionObservationHistoriesT.push_back(vector<size_t>());
897 // _m_firstAOHIforT.push_back(vector<size_t>());
898 // delete temp; // temp is copied into the vector - so delete this imm.
899 // ActionObservationHistoryTree* treeThiAg =
900 // CreateActionObservationHistoryTree(i);
901 // _m_actionObservationHistoryTreeRootPointers.push_back(treeThiAg);
902 
903 
904 
905 // ActionObservationHistory* aoh_0 =
906 // new ActionObservationHistory(*this);
907 // ActionObservationHistoryTree* root =
908 // new ActionObservationHistoryTree(aoh_0);
909 // vector<ActionObservationHistoryTree*> *temp = new
910 // vector<ActionObservationHistoryTree*>;
911 // temp
912 // root->SetIndex(0);
913 // _m_ActionObservationHistoryTreeVector.push_back(root);
914 
915  }
916  }
917 }
918 
921 {
922  queue<ActionObservationHistoryTree*> aohtQueue;
923 
924  Index aohI = 0;
925  size_t maxLength = GetHorizon() - 1;
926  size_t nrA = GetNrActions(agentI);
927  size_t nrO = GetNrObservations(agentI);
928 
929  //initial ActionObservationHistory aoh_0
930  ActionObservationHistory* aoh_0 =new ActionObservationHistory(*this,agentI);
932 
933  aohtQueue.push(root);
934 
935  Index ts = 0;
936  _m_firstAOHIforT[agentI].push_back(aohI);
937  while(aohtQueue.size() >= 1)
938  {
939  // aoh = queue.pop
940  ActionObservationHistoryTree* aoht = aohtQueue.front();
942  aohtQueue.pop();
943 
944  int thisLength = aoh->GetLength();
945  if(ts != (Index) thisLength ) //length 0 <-> ts 0
946  {
947  // we've now started processing AOHs for the next ts
948  Index nr_AOHI_for_ts = aohI -
949  _m_firstAOHIforT[agentI][ts];
950 #if DEBUG_PUDCAOHT
951  cout << "First aoh for next ts+1: store nr of ts-OH..."<<endl<<
952  "_m_firstAOHIforT[agentI="<<agentI<<"][ts="<<ts<<"] = "<<
953  _m_firstAOHIforT[agentI][ts] << ", current AOHI = " <<
954  aohI << endl;
955  Index tempIndex = _m_nrActionHistoriesT[agentI].size();
956 #endif
957  _m_nrActionObservationHistoriesT[agentI].push_back(nr_AOHI_for_ts);
958 #if DEBUG_PUDCAOHT
959  cout <<
960  "Set _m_nrActionObservationHistoriesT[agentI="<<agentI<<"][t="<<
961  tempIndex <<" (ts="<<ts<<")] = "<< nr_AOHI_for_ts <<endl;
962 #endif
963  if(++ts != (Index) thisLength)
964  cerr << "ERROR: CreateActionObservationHistories: ++ts != "<<
965  "(Index) thisLength !!! ts (now) is: "<<ts<<
966  "thisLength-1 =" << thisLength-1 <<endl;
967  _m_firstAOHIforT[agentI].push_back(aohI);
968  }
969  // aoh.SetIndex(index++)
970  aoht->SetIndex(aohI++);
971  _m_actionObservationHistoryTreeVectors[agentI].push_back(aoht);
972 
973  if(aoh->GetLength() >= maxLength )
974  //this is a final action-obs history:
975  continue;
976  //else
977 
978  for(Index aI = 0; aI < nrA; aI++)
979  for(Index oI = 0; oI < nrO; oI++)
980  {
981  //Constructor gets act. and obs. history and extends them
982  //with aI, oI. to find new ones,
983  ActionObservationHistory* next_aoh =
984  new ActionObservationHistory(aI, oI, aoh);
985 #if DEBUG_PUDCAOHT
986  cout << "created new aoh (for aI="<<aI<<", oI="<<oI<<":"<<
987  endl;
988  next_aoh->Print();
989  cout << endl;
990 #endif
991 
992  ActionObservationHistoryTree* next_aoht =
993  new ActionObservationHistoryTree(next_aoh);
994 #if DEBUG_PUDCAOHT
995  cout << "\ncreated new aoh tree:"<<endl;
996  next_aoht->Print();
997  cout << endl;
998 #endif
999 
1000  aoht->SetSuccessor(aI, oI, next_aoht);
1001  aohtQueue.push(next_aoht);
1002  }
1003 
1004  }
1005  Index nr_AOHI_for_ts = aohI - _m_firstAOHIforT[agentI][ts];
1006  _m_nrActionObservationHistoriesT[agentI].push_back(nr_AOHI_for_ts);
1007  _m_nrActionObservationHistories.push_back(aohI);
1008 
1009  return(root);
1010 }
1011 
1013 {
1015  {
1016  queue<JointActionObservationHistoryTree*> jaohtQueue;
1017  Index jaohI = 0;
1018  size_t maxLength = GetHorizon() - 1;
1019  size_t nrJA = GetNrJointActions();
1020  size_t nrJO = GetNrJointObservations();
1021 
1022  //initial JointActionObservationHistory aoh_0
1024  new JointActionObservationHistory(*this);
1027  jaohtQueue.push(root);
1028 
1029  queue<JointBeliefInterface*> jBeliefQueue;
1030  queue<double> cpQueue;
1031  queue<double> pQueue;
1032  const JointBeliefInterface* jb;
1033  bool cacheJBs=_m_params.GetComputeJointBeliefs();
1034 
1035  //put the initial belief and probs in the queues
1037  const StateDistribution* sd = GetReferred()->GetISD();
1038  b0->Set ( *sd ); // b0 is not yet in the cache!
1039  jBeliefQueue.push(b0);
1040  cpQueue.push(1.0);
1041  pQueue.push(1.0);
1042 
1043  //while(! empty(queue) )
1044  Index ts = 0;
1045  _m_firstJAOHIforT.push_back(0);//ts=0 starts at index 0
1046  while(jaohtQueue.size() >= 1)
1047  {
1048  // jaoh = queue.pop
1049  JointActionObservationHistoryTree* jaoht = jaohtQueue.front();
1050  JointActionObservationHistory* jaoh = jaoht->
1051  GetJointActionObservationHistory();
1052  jaohtQueue.pop();
1053 
1054  Index thisLength = jaoh//t->GetContainedElement()
1055  ->GetLength();
1056  if(ts != thisLength ) //length 0 <-> ts 0
1057  {
1058  // we've now started processing JOHs for the next ts
1060  jaohI - _m_firstJAOHIforT[ts]);
1061  if(++ts != thisLength )
1062  cerr << "ERROR: CreateJointObservationHistories: ++ts != "<<
1063  "thisLength !!! ts (now) is: "<<ts<<
1064  "thisLength =" << thisLength <<endl;
1065  _m_firstJAOHIforT.push_back(jaohI);
1066  }
1067 
1068  // jaoh.setIndex(index++)
1069  jaoht->SetIndex(jaohI++);
1071 
1072  double prob = pQueue.front();
1073  _m_jaohProbs.push_back(prob);
1074  pQueue.pop();
1075 
1076  double cprob = cpQueue.front();
1077  _m_jaohConditionalProbs.push_back(cprob);
1078  cpQueue.pop();
1079  jb = jBeliefQueue.front();
1080  jBeliefQueue.pop();
1081  if(cacheJBs) //if we cache the joint beliefs... cache it!
1082  _m_jBeliefCache.push_back(jb);
1083 
1084  if(jaoh->GetLength() >= maxLength )
1085  {
1086  //this is a final action-obs history: clean up...
1087  if(!cacheJBs) //we no longer need jb
1088  delete jb;
1089  //... and
1090  continue;
1091  }
1092  //else
1093 
1094  for(Index jaI = 0; jaI < nrJA; jaI++)
1095  for(Index joI = 0; joI < nrJO; joI++)
1096  {
1097  //Constructor gets act. and obs. history and extends them
1098  //with jaI, joI. to find new ones,
1099  JointActionObservationHistory* next_jaoh =
1100  new JointActionObservationHistory(jaI, joI, jaoh);
1101  JointActionObservationHistoryTree* next_jaoht =
1102  new JointActionObservationHistoryTree(next_jaoh);
1103 #if DEBUG_PUDCJAOHT
1104  cout << "created new jaoh (for jaI="<<jaI<<", joI="<<joI<<
1105  ":"<< endl; next_jaoh->Print(); cout << endl;
1106  cout << "\ncreated new jaoh tree:"<<endl; next_jaoht->Print();
1107  cout << endl;
1108 #endif
1109 
1110  jaoht->SetSuccessor(jaI, joI, next_jaoht);
1111  jaohtQueue.push(next_jaoht);
1112 
1114  *new_jb = *jb;
1115  double new_cond_p = new_jb->Update(*GetReferred(),jaI, joI);
1116  jBeliefQueue.push(new_jb);
1117  cpQueue.push(new_cond_p);
1118  double new_p = prob * new_cond_p;
1119  pQueue.push(new_p);
1120  }
1121 
1122  if(!cacheJBs) //we no longer need jb
1123  delete jb;
1124 
1125  }
1127 
1129  }
1130  else
1131  {
1132  /*Calculates the number of joint action-observation histories and
1133  * stores this in _m_nrJointActionObservationHistories (and
1134  * _m_nrJointActionObservationHistoriesT). Only used when the joint
1135  * act-observation histories are not created.
1136  *
1137  * !Also initializes the
1138  * root of JointActionObservationHistoryTree vector, for on-the-fly
1139  * generation of successors.
1140  */
1141  size_t nr=0;
1142  for (Index t=0; t<GetHorizon() ;t++ )
1143  {
1144  LIndex firstIndex = nr;
1145  _m_firstJAOHIforT.push_back(firstIndex);
1146  size_t nrT=1; //the number of histories at
1147  for (Index aI=0 ; aI<GetNrAgents(); aI++)
1148  {
1149  size_t historiesThisAgent =
1150  _m_nrObservationHistoriesT[aI][t] *
1151  _m_nrActionHistoriesT[aI][t];
1152  nrT *= historiesThisAgent;
1153  }
1155  nr += nrT;
1156  }
1158 
1160  new JointActionObservationHistory(*this);
1163 
1164  root->SetIndex(0);
1166 
1168  }
1169 }
1170 
1171 
1172 // GET functions:
1173 //
1174 
1176 {
1178  size_t nrs = p->GetNrStates();
1179  return (nrs);
1180 }
1181 
1183 {
1184 #if 0 // don't use powl(), we want to detect overflow
1185  return( static_cast<LIndex>(powl( GetNrActions(agentI),
1186  GetNrObservationHistories(agentI))));
1187 #endif
1188  LIndex nrPols=1;
1189  for(size_t k=0;k!=GetNrObservationHistories(agentI);++k)
1190  {
1191  LIndex nrPolsOld=nrPols;
1192  nrPols*=GetNrActions(agentI);
1193  if(nrPols<nrPolsOld)
1194  throw(EOverflow("PlanningUnitMADPDiscrete::GetNrPolicies() overflow detected"));
1195  }
1196  return(nrPols);
1197 }
1198 
1200 {
1201  LIndex n=1;
1202  for(Index ag=0; ag < GetNrAgents(); ag++)
1203  {
1204  LIndex nOld=n;
1205  n *= GetNrPolicies(ag);
1206  if(n<nOld)
1207  throw(EOverflow("PlanningUnitMADPDiscrete::GetNrJointPolicies() overflow detected"));
1208  }
1209  return(n);
1210 }
1211 
1213 {
1214  if(agentI < _m_nrObservationHistories.size() )
1215  return(_m_nrObservationHistories.at(agentI));
1216  //else
1217  throw("ERROR PlanningUnitMADPDiscrete::GetNrObservationHistories(Index agentI) index out of bounds or _m_nrObservationHistories not yet initialized?!\n");
1218 }
1219 
1221 {
1222  vector<size_t> nrOHts;
1223  for(Index agentI = 0; agentI < GetNrAgents(); agentI++)
1224  nrOHts.push_back( GetNrObservationHistories(agentI) );
1225  return nrOHts;
1226 }
1227 
1229 {
1230  vector<size_t> nrOHts;
1231  for(Index agentI = 0; agentI < GetNrAgents(); agentI++)
1232  nrOHts.push_back( GetNrObservationHistories(agentI, ts) );
1233  return nrOHts;
1234 }
1235 
1237 {
1239  throw ENotCached("PlanningUnitMADPDiscrete::GetObservationHistoryTree IndividualObservationHistories are not cached!");
1240 
1241  size_t nrA = GetNrAgents();
1242  if(_m_observationHistoryTreeVectors.size() != nrA)
1243  throw E("_m_observationHistoryTreeVectors.size() != nrA)");
1244 
1245  if(agentI < nrA )
1246  {
1247  size_t nrOH = GetNrObservationHistories(agentI);
1248 if(DEBUG_PUD){cout << "_m_observationHistoryTreeVectors["<<agentI<<"].size() = " <<_m_observationHistoryTreeVectors[agentI].size()<<endl;}
1249  if(_m_observationHistoryTreeVectors[agentI].size() != nrOH)
1250  throw E("_m_observationHistoryTreeVectors[agentI].size() != nrOH)");
1251 
1252  if(ohI < nrOH )
1253  {
1254 if(DEBUG_PUD){ cout <<"accessing _m_observationHistoryTreeVectors["<< agentI<<"]["<<ohI<<"]"<<endl;}
1255 
1256  ObservationHistoryTree* oht =
1257  _m_observationHistoryTreeVectors.at(agentI).at(ohI);
1258 if(DEBUG_PUD){ cout <<"...done - returning "<<oht<<endl;}
1259  return (oht);
1260  }
1261  else
1262  cerr << "WARNING PlanningUnitMADPDiscrete::GetObservationHistory(Index agentI, Index a) index ohI out of bounds"<<endl;
1263  }
1264  else
1265  cerr << "WARNING PlanningUnitMADPDiscrete::GetObservationHistory(Index agentI, Index ohI) index agentI out of bounds"<<endl;
1266 
1267  return(0);
1268 
1269 }
1272 {
1274  return(_m_jointObservationHistoryTreeVector.at(johI));
1275  else
1276  {
1277  cerr << "PlanningUnitMADPDiscrete::GetJointObservationHistoryTree - Warning: "<<
1278  "index (" << johI <<")out of bound!" << endl;
1279  return(0);
1280  }
1281 }
1282 
1283 
1284 
1285 
1286 //-------------------------------------------------
1287 //END of "Get...HistoryArrays/Vectors()" functions
1288 //-------------------------------------------------
1289 
1290 //TODO let functions below use ComputeHistoryArrays
1291 void
1293  (
1294  Index hI, //index of the history
1295  Index t, //stage of hI
1296  Index t_offset, //the offset within t
1297  Index Indices[], //the computed individual indices
1298  size_t indexDomainSize //the indices range from 0...(indexDomainSize-1)
1299 
1300  ) const
1301 {
1302  //let's hope the compiler optimizes out the obvious inefficientcies here
1303  //(I left them for readability)
1304 
1305  //substract the offset to find out what is the index within the
1306  //time-step-t-histories.
1307  Index hI_withinT = hI - t_offset;
1308  Index remainder = hI_withinT;
1309  size_t vec_size = t; // the number of indices we are searching
1310  size_t nrO = indexDomainSize;
1311  for(Index t2=0; t2 < vec_size ; t2++)
1312  {
1313  // suppose we have a oh = < oI[3], oI[2], o[1], oI[0] >
1314  // (the numbers represent the stage of the oI)
1315  // ->then this history is for stage 4 (we have that t=4).
1316  //
1317  // the ohI is generated by:
1318  // ohI= oI[3] * nrO^3 + ... +oI[0] * nrO^0
1319  //
1320  // we reconstruct this by performing
1321  // oI[3] = ohI / nrO^3
1322  // remainder = ohI - (oI^3)
1323  // oI[2] = remainder / nrO^2
1324  // etc.
1325  //
1326  // we start with stage
1327  size_t stage = (t-1) - t2;
1328  //we are going to calculate the joa that took place at time stage
1329  size_t b = (size_t) pow((double)nrO, (double)(stage));
1330  Index oI_t2 = remainder / b;
1331  Indices[t2] = oI_t2;
1332  remainder -= oI_t2 * b;
1333  }
1334 }
1335 
1336 void
1338  (Index jaohI, vector<Index> &jaIs, vector<Index> &joIs) const
1339 {
1340  jaIs.clear();
1341  joIs.clear();
1342  //try if cached
1343  try
1344  {
1346  GetJointActionObservationHistoryTree(jaohI);
1348  GetJointActionObservationHistoryVectors(jaIs, joIs);
1349  return;
1350  }
1351  catch(E& e){};
1352 
1353  Index t = GetTimeStepForJAOHI(jaohI);
1354  Index jaIs_arr[t];
1355  Index joIs_arr[t];
1356  GetJointActionObservationHistoryArrays(jaohI, t, jaIs_arr, joIs_arr );
1357  for(size_t t2 = 0; t2 < t; t2++)
1358  {
1359  jaIs.push_back(jaIs_arr[t2]);
1360  joIs.push_back(joIs_arr[t2]);
1361  }
1362 }
1363 
1364 void
1366  (Index jaohI, Index t, Index jaIs[], Index joIs[]) const
1367 {
1368  //substract the offset to find out what is the index within the time-step-t-
1369  //observation-histories.
1370  Index jaohIwithinThisT = jaohI - _m_firstJAOHIforT.at(t);
1371 
1372  Index remainder = jaohIwithinThisT;
1373  size_t vec_size = t; // the number of 'JAO' we are searching
1374  //note: this was t+1, but that is incorrect(?)!
1375 
1376  size_t nrJO = GetNrJointObservations();
1377  size_t nrJA = GetNrJointActions();
1378  size_t nrJAO = nrJO*nrJA;
1379 // Index jao_array[vec_size];
1380  for(Index t2=0; t2 < vec_size ; t2++)
1381  {
1382  // suppose we have a jaoh = < jaoI[3], jaoI[2], jao[1], jaoI[0] >
1383  // (the numbers represent the stage of the jaoI)
1384  // ->then this history is for stage 4 (we have that t=4).
1385  //
1386  // the jaohI is generated by:
1387  // jaohI= jaoI[3] * nrJAO^3 + ... +jaoI[0] * nrJAO^0
1388  //
1389  // we reconstruct this by performing
1390  // jaoI[3] = jaohI / nrJAO^3
1391  // remainder = jaohI - (jaoI^3)
1392  // jaoI[2] = remainder / nrJAO^2
1393  // etc.
1394  //
1395  // we start with stage
1396  size_t stage = (t-1) - t2;
1397  //we are going to calculate the joa that took place at time stage
1398  size_t b = (size_t) pow((double)nrJAO, (double)(stage));
1399 
1400  Index jaoI_t2 = remainder / b;
1401 // jao_array[t2] = jaoI_t2;
1402  remainder -= jaoI_t2 * b;
1403 
1404  jaIs[t2] = IndexTools::
1405  ActionObservation_to_ActionIndex(jaoI_t2, nrJA, nrJO);
1406  joIs[t2] = IndexTools::
1407  ActionObservation_to_ObservationIndex(jaoI_t2, nrJA, nrJO);
1408  }
1409 
1410 }
1411 
1412 void
1414  (Index johI, Index t, Index joIs[]) const
1415 {
1416  //substract the offset to find out what is the index within the time-step-t-
1417  //observation-histories.
1418  Index johIwithinThisT = johI - _m_firstJOHIforT.at(t);
1419 
1420  Index remainder = johIwithinThisT;
1421  size_t vec_size = t; // the number of joint observations we are searching
1422  size_t nrJO = GetNrJointObservations();
1423  //size_t nrJA = GetNrJointActions();
1424  for(Index t2=0; t2 < vec_size ; t2++)
1425  {
1426  // for explanation see GetJointActionObservationHistoryArrays()
1427  //
1428  // we start with stage
1429  size_t stage = (t-1) - t2;
1430  //we are going to calculate the joa that took place at time stage
1431  size_t b = (size_t) pow((double)nrJO, (double)(stage));
1432  Index joI_t2 = remainder / b;
1433  joIs[t2] = joI_t2;
1434  remainder -= joI_t2 * b;
1435  }
1436 }
1437 
1438 void
1440  (Index agentI, Index aohI, Index t, Index aIs[], Index oIs[]) const
1441 {
1442  //substract the offset to find out what is the index within the time-step-t-
1443  //observation-histories.
1444  Index aohIwithinThisT = aohI - _m_firstAOHIforT.at(agentI).at(t);
1445 
1446  Index remainder = aohIwithinThisT;
1447  size_t vec_size = t; // the number of 'JAO' we are searching
1448 
1449  size_t nrO = GetNrObservations(agentI);
1450  size_t nrA = GetNrActions(agentI);
1451  size_t nrAO = nrO*nrA;
1452  for(Index t2=0; t2 < vec_size ; t2++)
1453  {
1454  // for explanation see GetJointActionObservationHistoryArrays()
1455  //
1456  // we start with stage
1457  size_t stage = (t-1) - t2;
1458  //we are going to calculate the joa that took place at time stage
1459  size_t b = (size_t) pow((double)nrAO, (double)(stage));
1460 
1461  Index aoI_t2 = remainder / b;
1462  remainder -= aoI_t2 * b;
1463 
1464  aIs[t2] = IndexTools::
1465  ActionObservation_to_ActionIndex(aoI_t2, nrA, nrO);
1466  oIs[t2] = IndexTools::
1467  ActionObservation_to_ObservationIndex(aoI_t2, nrA, nrO);
1468  }
1469 
1470 }
1471 
1472 void
1474  (Index agentI, Index ohI, Index t, Index oIs[]) const
1475 {
1476  //substract the offset to find out what is the index within the time-step-t-
1477  //observation-histories.
1478  Index ohIwithinThisT = ohI - _m_firstOHIforT.at(agentI).at(t);
1479 
1480  Index remainder = ohIwithinThisT;
1481  size_t vec_size = t; // the number of joint observations we are searching
1482  size_t nrO = GetNrObservations(agentI);
1483  //size_t nrJA = GetNrJointActions();
1484  for(Index t2=0; t2 < vec_size ; t2++)
1485  {
1486  // for explanation see GetJointActionObservationHistoryArrays()
1487  //
1488  // we start with stage
1489  size_t stage = (t-1) - t2;
1490  //we are going to calculate the joa that took place at time stage
1491  size_t b = (size_t) pow((double)nrO, (double)(stage));
1492  Index oI_t2 = remainder / b;
1493  oIs[t2] = oI_t2;
1494  remainder -= oI_t2 * b;
1495  }
1496 }
1497 
1498 void
1500  (Index agentI, Index ahI, Index t, Index aIs[]) const
1501 {
1502  //substract the offset to find out what is the index within the time-step-t-
1503  //action-histories.
1504  Index ahIwithinThisT = ahI - _m_firstAHIforT.at(agentI).at(t);
1505 
1506  Index remainder = ahIwithinThisT;
1507  size_t vec_size = t; // the number of joint actions we are searching
1508  size_t nrA = GetNrActions(agentI);
1509  for(Index t2=0; t2 < vec_size ; t2++)
1510  {
1511  // for explanation see GetJointActionActionHistoryArrays()
1512  //
1513  // we start with stage
1514  size_t stage = (t-1) - t2;
1515  //we are going to calculate the joa that took place at time stage
1516  size_t b = (size_t) pow((double)nrA, (double)(stage));
1517  Index aI_t2 = remainder / b;
1518  aIs[t2] = aI_t2;
1519  remainder -= aI_t2 * b;
1520  }
1521 }
1522 
1523 //-------------------------------------------------
1524 //END of "Get...HistoryArrays/Vectors()" functions
1525 //-------------------------------------------------
1526 
1527 
1528 
1529 
1530 
1531 
1532 //-----------------------------------------
1533 //BEGIN of "Get...HistoryIndex()" functions
1534 //-----------------------------------------
1535 
1536 
1538  JointObservationHistoryTree* joh) const
1539 {
1540  throw E("GetJointObservationHistoryIndex(JointObservationHistoryTree* joh) seems like a dumb-ass function? I mean looping over exponentially many histories does not seem like a nice thing to do?");
1541 
1542  for(Index i=0;i<_m_nrJointObservationHistories;i++)
1543  {
1545  return(i);
1546  }
1547  // we should not get here
1548  throw E("GetJointObservationHistoryIndex index not found");
1549  return(0);
1550 }
1553 {
1554  throw E("GetJointActionObservationHistoryIndex(JointActionObservationHistoryTree* joh) seems like a dumb-ass function? I mean looping over exponentially many histories does not seem like a nice thing to do?");
1556  {
1558  return(i);
1559  }
1560  // we should not get here
1561  throw E("GetJointObservationHistoryIndex index not found");
1562  return(0);
1563 }
1564 
1565 
1567  Index t, //the stage
1568  Index t_offset, //for each stage t, there is an offset
1569  //const vector<Index>& indices, //the indices of the (J)(A/O)s
1570  const Index indices [], //the indices of the (J)(A/O)s
1571  size_t indexDomainSize //the indices range from 0...(indexDomainSize-1)
1572  ) const
1573 {
1574  //This example assumes we are computing the index of an observation history:
1575  //
1576  //indices is a vector with the observation indices for stage 0...t-1
1577  //<o^0, o^1, ... , o^(t-1) >
1578  //from this we want to find the history observation index, which is given
1579  //by:
1580  // o^0 * |O|^(t-1) + ... + o^(t-1) * |O|^0 + stage offset
1581  //
1582  //where |O| is the `base' of the number as specified by the tuple of
1583  //indices
1584  //
1585  size_t HI=t_offset;
1586  for (Index ts=0; ts<t; ts++)
1587  {
1588  int exponent = t-1 - ts;
1589  size_t b = (size_t) pow((double)indexDomainSize, (double)(exponent));
1590  HI += indices[ts] * b;
1591  }
1592  return(HI);
1593 }
1594 
1595 
1597  Index t, const vector<Index>& observations) const
1598 {
1599  return( ComputeHistoryIndex(
1600  t,
1601  _m_firstOHIforT.at(agentI).at(t),
1602  &observations[0],
1603  GetNrObservations(agentI)
1604  )
1605  );
1606 
1607  //observations is a vector with the observation indices for stage 0...t-1
1608  //<o^0, o^1, ... , o^(t-1) >
1609  //from this we want to find the history observation index, which is given
1610  //by:
1611  // o^0 * |O|^(t-1) + ... + o^(t-1) * |O|^0 + stage offset
1612  //
1613  //where |O| is the `base' of the number as specified by the tuple of
1614  //indices
1615  //
1616  //
1617  //size_t this_OHI=_m_firstOHIforT.at(agentI).at(t);
1618  //size_t nrO = GetNrObservations(agentI);
1619  //for (Index ts=0; ts<t; ts++)
1620  //{
1621  //int exponent = t-1 - ts;
1622  //size_t b = (size_t) pow((double)nrO, (double)(exponent));
1623  //this_OHI += observations.at(ts) * b;
1624  //}
1625  //return(this_OHI);
1626 }
1627 
1629  Index t, const vector<Index>& jointObservations) const
1630 {
1631  return( ComputeHistoryIndex(
1632  t,
1633  _m_firstJOHIforT.at(t),
1634  &jointObservations[0],
1636  )
1637  );
1638 /*
1639  size_t this_JOHI=_m_firstJOHIforT.at(t);
1640  size_t nrJO = GetNrJointObservations();
1641  for (Index ts=0; ts<t; ts++)
1642  {
1643  int exponent = t-1 - ts;
1644  size_t b = (size_t) pow((double)nrJO, (double)(exponent));
1645  this_JOHI += jointObservations.at(ts) * b;
1646  }
1647  return(this_JOHI);*/
1648 }
1650  Index t, const Index jointObservations[] ) const
1651 {
1652  return( ComputeHistoryIndex(
1653  t,
1654  _m_firstJOHIforT.at(t),
1655  jointObservations,
1657  )
1658  );
1659 /*
1660  size_t this_JOHI=_m_firstJOHIforT.at(t);
1661  size_t nrJO = GetNrJointObservations();
1662  for (Index ts=0; ts<t; ts++)
1663  {
1664  int exponent = t-1 - ts;
1665  size_t b = (size_t) pow((double)nrJO, (double)(exponent));
1666  this_JOHI += jointObservations[ts] * b;
1667  }
1668  return(this_JOHI);*/
1669 }
1670 
1672  Index t, const vector<Index>& jointActions) const
1673 {
1674  return( ComputeHistoryIndex(
1675  t,
1676  _m_firstJAHIforT.at(t),
1677  &jointActions[0],
1679  )
1680  );
1681 /*
1682  size_t this_JAHI=_m_firstJAHIforT.at(t);
1683  size_t nrJA = GetNrJointActions();
1684  for (Index ts=0; ts<t; ts++)
1685  {
1686  int exponent = t-1 - ts;
1687  size_t b = (size_t) pow((double)nrJA, (double)(exponent));
1688  this_JAHI += jointActions.at(ts) * b;
1689  }
1690  return(this_JAHI);*/
1691 }
1693  Index t, const Index jointActions[] ) const
1694 {
1695  return( ComputeHistoryIndex(
1696  t,
1697  _m_firstJAHIforT.at(t),
1698  jointActions,
1700  )
1701  );
1702  /*size_t this_JAHI=_m_firstJAHIforT.at(t);
1703  size_t nrJA = GetNrJointActions();
1704  for (Index ts=0; ts<t; ts++)
1705  {
1706  int exponent = t-1 - ts;
1707  size_t b = (size_t) pow((double)nrJA, (double)(exponent));
1708  this_JAHI += jointActions[ts] * b;
1709  }
1710  return(this_JAHI);*/
1711 }
1712 
1714  Index agentI,
1715  Index t,
1716  const vector<Index>& actions,
1717  const vector<Index>& observations
1718  ) const
1719 {
1720  //functioning is the same as for observation histories: only now the
1721  //base is the nr AO.
1722 
1723  //-> first convert to AO indices:
1724  size_t first_AOHI=_m_firstAOHIforT.at(agentI).at(t);
1725  size_t nrA = GetNrActions(agentI);
1726  size_t nrO = GetNrObservations(agentI);
1727  size_t nrAO = nrA * nrO;
1728  vector<Index> AO_Is(t, 0);
1729  for (Index ts=0; ts<t; ts++)
1731  actions.at(ts), observations.at(ts), nrA, nrO);
1732  return( ComputeHistoryIndex(
1733  t,
1734  first_AOHI,
1735  &AO_Is[0],
1736  nrAO
1737  )
1738  );
1739 
1740  /*for (Index ts=0; ts<t; ts++)
1741  {
1742  int exponent = t-1 - ts;
1743  size_t b = (size_t) pow((double)nrAO, (double)(exponent));
1744  Index this_t_ao = IndexTools::
1745  ActionAndObservation_to_ActionObservationIndex(
1746  actions.at(ts), observations.at(ts), nrA, nrO);
1747  this_AOHI += this_t_ao * b;
1748  }
1749  return(this_AOHI);*/
1750 }
1751 
1753  Index t,
1754  const vector<Index>& Jactions,
1755  const vector<Index>& Jobservations
1756  ) const
1757 {
1758  //functioning is the same as for observation histories:
1759  //(only now the base is the nr AO)
1760 
1761  size_t first_JAOHI=_m_firstJAOHIforT.at(t);
1762  size_t nrJA = GetNrJointActions();
1763  size_t nrJO = GetNrJointObservations();
1764  size_t nrJAO = nrJA * nrJO;
1765 
1766  vector<Index> JAO_Is(t, 0);
1767  for (Index ts=0; ts<t; ts++)
1769  Jactions.at(ts), Jobservations.at(ts), nrJA, nrJO);
1770  return( ComputeHistoryIndex(
1771  t,
1772  first_JAOHI,
1773  &JAO_Is[0],
1774  nrJAO
1775  )
1776  );
1777 }
1778 
1779 
1780 //-----------------------------------------
1781 //END of "Get...HistoryIndex()" functions
1782 //-----------------------------------------
1783 
1784 
1785 
1786 //-----------------------------------------
1787 //BEGIN of "JointToIndividual...HistoryIndices...()" functions
1788 //-----------------------------------------
1789 
1790 vector<Index>
1792  johI) const
1793 {
1794  Index t = GetTimeStepForJOHI(johI);
1795  Index jo_array[t];
1796  GetJointObservationHistoryArrays(johI, t, jo_array );
1797 
1798  //convert joint observations -> indiv. observations
1799  vector< vector<Index> > indivO_vec;
1800 
1801  for (Index agentI=0; agentI<GetNrAgents(); agentI++)
1802  indivO_vec.push_back( vector<Index>() );
1803 
1804  for (Index ts=0; ts<t; ts++)
1805  {
1806  vector<Index> indivIndicesThisT =
1808  for (Index agentI=0; agentI<GetNrAgents(); agentI++)
1809  indivO_vec.at(agentI).push_back( indivIndicesThisT.at(agentI) );
1810  }
1811 
1812  //calculate indiv observation history index from seq. of indiv. observations
1813  vector<Index> ohI_vec;
1814  for (Index agentI=0; agentI<GetNrAgents(); agentI++)
1815  {
1816  Index this_agentOHI = GetObservationHistoryIndex(agentI, t,
1817  indivO_vec.at(agentI) );
1818  ohI_vec.push_back(this_agentOHI);
1819  }
1820  return(ohI_vec);
1821 }
1822 
1823 const vector<Index>&
1825  jObsHistI) const
1826 {
1827  if(jObsHistI < _m_nrJointObservationHistories)
1828  {
1831  return(joht->GetJointObservationHistory()->
1832  GetIndividualObservationHistoryIndices());
1833  }
1834  else
1835  {
1836  stringstream ss;
1837  ss << "PlanningUnitMADPDiscrete::JointToIndividualObservationHistory"<<
1838  "Indices ERROR: index ("<< jObsHistI <<") out of bounds!"<<endl;
1839  throw E(ss);
1840  }
1841 }
1842 
1843 const vector<Index>&
1845  Index jaohI) const
1846 {
1848  {
1851  return(jaoht->GetJointActionObservationHistory()->
1852  GetIndividualActionObservationHistoryIndices());
1853  }
1854  else
1855  {
1856  stringstream ss;
1857  ss << "PlanningUnitMADPDiscrete::JointToIndividualActionActObserva"<<
1858  "tionHistory Indices ERROR: index ("<< jaohI <<
1859  ") out of bounds!"<<endl;
1860  throw E(ss);
1861  }
1862 }
1863 
1864 void
1866  Index jaohI,
1867  vector< vector<Index> >& indivAO_vec
1868  ) const
1869 {
1870  throw E("PlanningUnitMADPDiscrete::JointAOHIndexToIndividualActionObservationVectors(Index jaohI, vector< vector<Index> >& indivAO_vec ) const is not yet implemented.");
1871 
1872 }
1873 
1874 void
1876  Index jaohI,
1877  vector< vector<Index> >& indivO_vec,
1878  vector< vector<Index> >& indivA_vec
1879  ) const
1880 {
1881  Index t = GetTimeStepForJAOHI(jaohI);
1882  Index jo_array[t];
1883  Index ja_array[t];
1884 
1885  //TODO: check we might be doing duplicate work: within
1886  //GetJointActionObservationHistoryArrays we convert joint act-obs indices to
1887  //action indices and observation indices. A bit further down we do the
1888  //reverse (albeit for individual indices)
1889  GetJointActionObservationHistoryArrays(jaohI, t, ja_array, jo_array);
1890 
1891  size_t nrAgents = GetNrAgents();
1892  //convert joint observations -> indiv. observations
1893  //indivO_vec[agentI][t] = oI
1894  indivO_vec = vector< vector<Index> >(nrAgents, vector<Index>(t, 0));
1895  indivA_vec = vector< vector<Index> >(nrAgents, vector<Index>(t, 0));
1896  for (Index ts=0; ts<t; ts++)
1897  {
1898  vector<Index> indivA_IndicesThisT =
1899  JointToIndividualActionIndices(ja_array[ts]);
1900  vector<Index> indivO_IndicesThisT =
1902  for (Index agentI=0; agentI<GetNrAgents(); agentI++)
1903  {
1904  indivO_vec[agentI][ts] = indivO_IndicesThisT[agentI];
1905  indivA_vec[agentI][ts] = indivA_IndicesThisT[agentI];
1906  }
1907  }
1908 }
1909 
1910 vector<Index>
1912  Index jaohI) const
1913 {
1914  Index t = GetTimeStepForJAOHI(jaohI);
1915  //indivO_vec[agentI][t] = oI
1916  size_t nrAgents = GetNrAgents();
1917  vector< vector<Index> > indivO_vec(nrAgents, vector<Index>(t, 0));
1918  vector< vector<Index> > indivA_vec(nrAgents, vector<Index>(t, 0));
1920  jaohI, indivO_vec, indivA_vec );
1921  //calculate indiv observation history index from seq. of indiv. observations
1922  vector<Index> aohI_vec;
1923  for (Index agentI=0; agentI<GetNrAgents(); agentI++)
1924  {
1925  Index this_agentAOHI = GetActionObservationHistoryIndex(agentI, t,
1926  indivA_vec.at(agentI), indivO_vec.at(agentI) );
1927  aohI_vec.push_back(this_agentAOHI);
1928  }
1929  return(aohI_vec);
1930 }
1931 
1933  Index jObsHistI) const
1934 {
1935  throw E("PlanningUnitMADPDiscrete::JointToIndividualActionHistoryIndices(Index jObsHistI) NOT YET IMPLEMENTED");
1936 
1937 }
1938 
1940 {
1941  if(jObsHistI < _m_nrJointActionHistories)
1942  {
1943  JointActionHistoryTree* joht =
1945  return(joht->GetJointActionHistory()->
1946  GetIndividualActionHistoryIndices());
1947  }
1948  else
1949  {
1950  stringstream ss;
1951  ss << "PlanningUnitMADPDiscrete::JointToIndividualActionHistoryIndices"
1952  <<" ERROR: index ("<< jObsHistI <<") out of bounds!"<<endl;
1953  throw E(ss);
1954  }
1955 }
1956 
1957 
1958 //-----------------------------------------
1959 //END of "JointToIndividual...HistoryIndices...()" functions
1960 //-----------------------------------------
1961 
1962 vector<Index>
1966 {
1967  switch ( cat )
1968  {
1971  break;
1974  break;
1976  throw(E("JointToIndividualPolicyDomainIndices: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
1977  break;
1978  default:
1979  throw(E("JointToIndividualPolicyDomainIndices: incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
1980  break;
1981  }
1982 }
1983 
1986 {
1987  switch ( cat )
1988  {
1991  break;
1994  break;
1996  throw(E("JointToIndividualPolicyDomainIndicesRef: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
1997  break;
1998  default:
1999  throw(E("JointToIndividualPolicyDomainIndicesRef: incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
2000  break;
2001  }
2002 }
2003 
2004 
2005 
2006 
2007 //-----------------------------------------
2008 //BEGIN of "IndividualToJoint...HistoryIndex()" functions
2009 //-----------------------------------------
2010 
2011 
2012 //TODO - check if working, create copy for act-obs
2013 Index
2015  const vector<Index>& indivIs)
2016 const
2017 {
2018  //individual observation history index -> individual observation sequences
2019  size_t nrAg = GetNrAgents();
2020  vector< Index* > observSeqs(nrAg);
2021  for (Index agentI = 0; agentI < nrAg; agentI++)
2022  {
2023  observSeqs[agentI] = new Index[t];
2024  Index ohI_thisAgent = indivIs.at(agentI);
2025  GetObservationHistoryArrays (agentI, ohI_thisAgent, t,
2026  observSeqs[agentI] );
2027  }
2028 
2029  // observSeqs[agent][ts] -> stageObserv[ts][agent]
2030  vector< vector<Index> > stageObserv(t, vector<Index>(nrAg) );
2031  for (Index agentI = 0; agentI < nrAg; agentI++)
2032  for (Index ts = 0; ts < t; ts++)
2033  stageObserv[ts][agentI] = observSeqs[agentI][ts];
2034 
2035  for (Index agentI = 0; agentI < nrAg; agentI++)
2036  delete [] observSeqs[agentI];
2037 
2038  //individual observation sequences -> joint observation sequences.
2039  vector<Index> jobs(t);
2040  for (Index ts = 0; ts < t; ts++)
2041  jobs[ts] = IndividualToJointObservationIndices(stageObserv[ts]);
2042 
2043  //joint observation sequences -> joint history index.
2044  Index johI = GetJointObservationHistoryIndex(t, jobs);
2045  return(johI);
2046 
2047 }
2048 Index
2050  const vector<Index>& indivIs)
2051 const
2052 {
2053  //individual action history index -> individual action sequences
2054  size_t nrAg = GetNrAgents();
2055  vector< Index* > observSeqs(nrAg);
2056  for (Index agentI = 0; agentI < nrAg; agentI++)
2057  {
2058  observSeqs[agentI] = new Index[t];
2059  Index ahI_thisAgent = indivIs.at(agentI);
2060  GetActionHistoryArrays (agentI, ahI_thisAgent, t,
2061  observSeqs[agentI] );
2062  }
2063 
2064  // observSeqs[agent][ts] -> stageObserv[ts][agent]
2065  vector< vector<Index> > stageObserv(t, vector<Index>(nrAg) );
2066  for (Index agentI = 0; agentI < nrAg; agentI++)
2067  for (Index ts = 0; ts < t; ts++)
2068  stageObserv[ts][agentI] = observSeqs[agentI][ts];
2069 
2070  for (Index agentI = 0; agentI < nrAg; agentI++)
2071  delete [] observSeqs[agentI];
2072 
2073  //individual action sequences -> jaint action sequences.
2074  vector<Index> jabs(t);
2075  for (Index ts = 0; ts < t; ts++)
2076  jabs[ts] = IndividualToJointActionIndices(stageObserv[ts]);
2077 
2078  //jaint action sequences -> jaint history index.
2079  Index jahI = GetJointActionHistoryIndex(t, jabs);
2080  return(jahI);
2081 
2082 }
2083 
2084 Index
2086  Index t, const vector<Index>& indivIs) const
2087 {
2088  //individual observation history index -> individual observation sequences
2089  size_t nrAg = GetNrAgents();
2090  vector< Index* > observSeqs(nrAg);
2091  vector< Index* > actionSeqs(nrAg);
2092  for (Index agentI = 0; agentI < nrAg; agentI++)
2093  {
2094  observSeqs[agentI] = new Index[t];
2095  actionSeqs[agentI] = new Index[t];
2096  Index aohI_thisAgent = indivIs.at(agentI);
2097  GetActionObservationHistoryArrays (agentI, aohI_thisAgent, t,
2098  actionSeqs[agentI], observSeqs[agentI] );
2099  }
2100 
2101  // observSeqs[agent][ts] -> stageObserv[ts][agent]
2102  vector< vector<Index> > stageObserv(t, vector<Index>(nrAg) );
2103  vector< vector<Index> > stageAction(t, vector<Index>(nrAg) );
2104  for (Index agentI = 0; agentI < nrAg; agentI++)
2105  for (Index ts = 0; ts < t; ts++)
2106  {
2107  stageObserv[ts][agentI] = observSeqs[agentI][ts];
2108  stageAction[ts][agentI] = actionSeqs[agentI][ts];
2109  }
2110 
2111  for (Index agentI = 0; agentI < nrAg; agentI++)
2112  {
2113  delete [] observSeqs[agentI];
2114  delete [] actionSeqs[agentI];
2115  }
2116 
2117  //individual observation sequences -> joint observation sequences.
2118  vector<Index> jobs(t);
2119  vector<Index> jacs(t);
2120 
2121  for (Index ts = 0; ts < t; ts++)
2122  {
2123  jobs[ts] = IndividualToJointObservationIndices(stageObserv[ts]);
2124  jacs[ts] = IndividualToJointActionIndices(stageAction[ts]);
2125 
2126  }
2127 
2128  //joint observation sequences -> joint history index.
2129  Index jaohI = GetJointActionObservationHistoryIndex(t, jacs, jobs);
2130  return(jaohI);
2131 
2132 }
2133 
2134 //-----------------------------------------
2135 //END of "IndividualToJoint...HistoryIndex()" functions
2136 //-----------------------------------------
2137 
2138 
2139 
2140 
2141 
2142 
2143 
2144 
2145 
2146 
2147 
2148 
2149 
2150 //start action hist functions...
2151 
2153 {
2154  if(agentI < _m_nrActionHistories.size() )
2155  return(_m_nrActionHistories.at(agentI));
2156  //else
2157  throw("ERROR PlanningUnitMADPDiscrete::GetNrActionHistories(Index agentI) index out of bounds or _m_nrActionHistories not yet initialized?!\n");
2158 }
2160 {
2161 /* incorrect calculation (takes cartesian product over action histories
2162  * of different time-steps...)
2163  * size_t nrJointActionHistories = 1;
2164  size_t nrAgents = GetNrAgents();
2165  for(Index agentI=0; agentI < nrAgents; agentI++)
2166  nrJointActionHistories *= GetNrActionHistories(agentI);
2167 
2168  if( nrJointActionHistories != _m_jointActionHistoryTreeVector.
2169  size() )
2170  cerr << "WARNING: PlanningUnitMADPDiscrete::GetNrJointActionHistories() - nrJointActionHistories (= "<< nrJointActionHistories <<" ) != _m_jointActionHistoryTreeVector.size() (= "<< _m_jointActionHistoryTreeVector.size() <<" ) !!"<<endl; */
2171  size_t nr = _m_jointActionHistoryTreeVector.size();
2172 // cerr << " _m_jointActionHistoryTreeVector.size() = "<< _m_jointActionHistoryTreeVector.size() << " - nr = " << nr << endl;
2173  return nr;
2174 }
2176 {
2177  size_t nrA = GetNrAgents();
2178  if(_m_actionHistoryTreeVectors.size() != nrA)
2179  throw E("PlanningUnitMADPDiscrete::GetActionHistoryTree _m_actionHistoryTreeVectors.size() != nrA)");
2180 
2181  if(agentI < nrA )
2182  {
2183  size_t nrOH = GetNrActionHistories(agentI);
2184  if(DEBUG_PUD){cout << "_m_actionHistoryTreeVectors["<<agentI<<
2185  "].size() = " <<_m_actionHistoryTreeVectors[agentI].size()<<endl;}
2186  if(_m_actionHistoryTreeVectors[agentI].size() != nrOH)
2187  throw E("_m_actionHistoryTreeVectors[agentI].size() != nrOH)");
2188 
2189  if(ohI < nrOH )
2190  {
2191  if(DEBUG_PUD){ cout <<"accessing _m_actionHistoryTreeVectors["<<
2192  agentI<<"]["<<ohI<<"]"<<endl;}
2193 
2194  ActionHistoryTree* oht =
2195  _m_actionHistoryTreeVectors[agentI][ohI];
2196  if(DEBUG_PUD){ cout <<"...done - returning "<<oht<<endl;}
2197  return (oht);
2198  }
2199  else
2200  cerr << "WARNING PlanningUnitMADPDiscrete::GetActionHistory(Index"<<
2201  " agentI, Index a) index ohI out of bounds"<<endl;
2202  }
2203  else
2204  cerr << "WARNING PlanningUnitMADPDiscrete::GetActionHistory(Index agentI, Index ohI) index agentI out of bounds"<<endl;
2205 
2206  return(0);
2207 
2208 }
2211 {
2212  if(johI < _m_nrJointActionHistories)
2213  return(_m_jointActionHistoryTreeVector.at(johI));
2214  else
2215  {
2216  cerr << "PlanningUnitMADPDiscrete::GetJointActionHistoryTree - Warning: "<<
2217  "index (" << johI <<")out of bound!" << endl;
2218  return(0);
2219  }
2220 }
2221 
2224 {
2225  for(Index i=0;i<_m_nrJointActionHistories;i++)
2226  {
2227  if(_m_jointActionHistoryTreeVector.at(i)==joh)
2228  return(i);
2229  }
2230 
2231  // we should not get here
2232  throw E("GetJointActionHistoryIndex index not found");
2233  return(0);
2234 }
2235 
2236 
2238 {
2240 }
2241 
2242 
2245 {
2246  // TODO \todo review the following:
2247  // this is an awkward construction.
2248  // when beliefs are cached we return that and we typically don't want
2249  // to touch this belief
2250  // when beliefs aren't cached we create a *new* belief, which we typically
2251  // want to delete when we're done using it...
2252  //
2253  // UPDATE: I'v modified this function so that it always returns a _new_
2254  // joint belief. This means we also do not have to worry about invalidating
2255  // the beliefs cache.
2256 
2258  {
2260  *jbi = *(_m_jBeliefCache.at(jaohI)); //make a copy
2261  return jbi;
2262  }
2263  else
2264  {
2265  vector<Index> jaIs;
2266  vector<Index> joIs;
2267  GetJointActionObservationHistoryVectors(jaohI,jaIs,joIs);
2268 
2270  for(unsigned i=0;i!=jaIs.size();++i)
2271  JB->Update(*GetReferred(),jaIs[i],joIs[i]);
2272 #if DEBUG_PUDCGETJB
2273  if(!JB->SanityCheck())
2274  {
2275  JB->Print();
2276  abort();
2277  }
2278 #endif
2279  return(JB);
2280  }
2281 }
2282 
2283 
2284 
2285 double
2287  //output args:
2288  JointBeliefInterface* jb,
2289  //input args:
2290  Index jaohI, // the jaohI for which we want to know the probs
2291  Index p_jaohI, //=0 the GIVEN predecessor
2292  const JointBeliefInterface* p_jb,// = NULL, // the corresponding GIVEN jb of p
2293  const JointPolicyDiscrete * jpol// = NULL // the policy followed in
2294  ) const
2295 {
2296  if(p_jaohI == 0 && p_jb == NULL && jpol==NULL) //the cacheable case
2298  {
2299  //return the cached results...
2300  //
2301  //we have to make a copy (can't do jb = ..., because that would
2302  //not be reflected in the calling function)
2303  *jb = *_m_jBeliefCache[jaohI];
2304  return (_m_jaohProbs[jaohI]);
2305  }
2306  //we can't get the cached result, so we have to compute.
2307 
2308  Index t = GetTimeStepForJAOHI(jaohI);
2309  Index t_p = GetTimeStepForJAOHI(p_jaohI);
2310 
2311  Index jaIs[t];
2312  Index joIs[t];
2313  GetJointActionObservationHistoryArrays(jaohI, t, jaIs, joIs);
2314 #if DEBUG_PUD_JAOHPROBS
2315  vector<Index> jaIsv,joIsv;
2320 
2321  cout << "t " << t << " " << jaohI
2322  << "\njaIsv:" << SoftPrintVector(jaIsv)
2323  << ", joIsv:" << SoftPrintVector(joIsv)
2324  << ", jaoht:" << jaoht->GetJointActionObservationHistory()->SoftPrint()
2325  << endl;
2326 
2327  for(Index i=0;i!=t;++i)
2328  cout << "jaIs["<<i<<"]=" << jaIs[i] << " ";
2329  cout << endl;
2330 #endif
2331  Index p_jaIs[t_p];
2332  Index p_joIs[t_p];
2333 
2334  if(p_jaohI != 0)
2335  {
2336  //check that p_jaohI is indeed a predecessor of jaohI, this consists of
2337  //two checks 1) the (quick) stage check::
2338  if (t_p >= t)
2339  return (0.0);
2340 
2341  if(t_p == 0)
2342  throw E(" p_jaohI != 0 but t_p == 0 ");
2343 
2344  //and 2) the consistency check
2345  GetJointActionObservationHistoryArrays(p_jaohI, t_p, p_jaIs, p_joIs);
2346  for(Index tI=0; tI < t_p; tI++)
2347  {
2348  if ( jaIs[tI] == p_jaIs[tI] &&
2349  joIs[tI] == p_joIs[tI] )
2350  ; //consistent
2351  else
2352  {
2353  cerr << "GetJAOHProbs:: Warning pred. inconsistent with requested jaohI" << endl;
2354  return (0.0); // p_jaohI is inconsistent with jaohI.
2355  }
2356  }
2357  }
2358  //p_jaIs[ t_p ... (t-1) ] contains the joint actions `still to be taken'
2359  //to get from p_jaohI to jaohI.
2360 
2361  if(p_jaohI != 0 && p_jb == NULL)
2362  {
2363  // a predecessor is specified, but without accompanying joint belief.
2364  // In this case we assume that a pure (deterministic) joint policy
2365  // that is consistent with p_jaohI has been followed.
2367  {
2368  //if joint beliefs are cache use them:
2369  *jb = *_m_jBeliefCache[jaohI];
2370  double pr = _m_jaohProbs[jaohI];
2371  double p_pr = _m_jaohProbs[p_jaohI];
2372  double pr_cond = pr / p_pr;
2373  return pr_cond;
2374  }
2375  else
2376  {
2377  // Here we compute the joint belief corresponding to p_jaohI for
2378  // such a pure joint policy. by performing the belief update
2379  // starting from the first time step
2380  jb->Set( *GetMADPDI()->GetISD() );
2381  //*jb = *GetNewJointBeliefFromISD();
2382  GetJAOHProbsRecursively(jb, p_jaIs, p_joIs, 0, t_p,
2384  //GetJAOHProbsRecursively(jb, p_jaIs, p_joIs, t0, t_p, jaoh0 );
2385  }
2386  }
2387  else if ( p_jb == NULL ) // and p_jaohI == 0
2388  jb->Set( *GetMADPDI()->GetISD() );
2389  //*jb = *GetNewJointBeliefFromISD();
2390  else
2391  *jb = *p_jb; //copy by value, we don't want to alter p_jb!
2392 
2393  double p = GetJAOHProbsRecursively(jb,
2394  jaIs, joIs, t_p, t,
2395  //jaohI,
2396  p_jaohI,
2397  jpol);
2398 
2399  return(p);
2400 }
2401 
2402 double
2404  //input/output arg:
2405  JointBeliefInterface* jb,
2406  //input args:
2407  const Index jaIs[],
2408  const Index joIs[],
2409  Index t_p,
2410  Index t,
2411 // const Index jaohI, // the jaohI for which we want to know the probs
2412  Index p_jaohI,// = 0, // the GIVEN predecessor
2413 // const JointBelief* p_jb = NULL, // the corresponding GIVEN jb of p
2414  const JointPolicyDiscrete * jpol// = NULL // the policy followed in
2415  ) const
2416 {
2417  //if(jaohI == p_jaohI)
2418  if(t == t_p)
2419  return(1.0);
2420 
2421  Index jaI = jaIs[t_p];
2422  Index joI = joIs[t_p];
2423  double cond_P = jb->Update(*GetReferred(), jaI, joI);
2424 // Index next_p_jaohI = //???
2425  double P_remainder = GetJAOHProbsRecursively(
2426  jb,
2427  jaIs,
2428  joIs,
2429  t_p + 1,
2430  t,
2431 // jaohI,
2432  GetSuccessorJAOHI(p_jaohI, jaI, joI),
2433  jpol
2434  );
2435  //jb should now be the jb corresponding to jaohI
2436  double polProb = 0.0;
2437  if(jpol != NULL)
2438  {
2439  Index jdI = 0;
2440  switch ( jpol->GetIndexDomainCategory() )
2441  {
2442  case OHIST_INDEX :
2443  jdI = GetJointObservationHistoryIndex(t_p, joIs);
2444  break;
2445  case AOHIST_INDEX :
2446  jdI = p_jaohI;
2447  break;
2448  default:
2449  throw E("PlanningUnitMADPDiscrete::GetJAOHProbsRecursively --- policy has unsupported IndexDomainCategory");
2450  }
2451  polProb = jpol->GetJointActionProb( jdI, jaI ) ;
2452  }
2453  else
2454  polProb = 1.0;
2455 
2456  //line 10 of Algorithm 1 of the manual documentation:
2457  double Prob_jaohI = P_remainder * cond_P * polProb;
2458  return (Prob_jaohI);
2459 }
2460 
2461 
2462 
2464  Index jaohI,
2465  Index p_jaohI, // the GIVEN predecessor
2466  const JointBeliefInterface* p_jb,
2467  const JointPolicyDiscrete* jpol
2468  ) const
2469 {
2470  JointBelief dummy( GetNrStates() );
2471  double p = GetJAOHProbs(&dummy, jaohI, p_jaohI, p_jb, jpol );
2472  return p;
2473  //\todo TODO, because this function does not need to return to joint belief,
2474  //the last belief update is unecessary. therefore we can still gain some
2475  //speed here...
2476 
2477 
2478 }
2479 
2481 {
2483  return(_m_jaohConditionalProbs[jaohI]);
2484  //else
2485 
2486  vector<Index> jaIs;
2487  vector<Index> joIs;
2488  GetJointActionObservationHistoryVectors(jaohI,jaIs,joIs);
2489  Index previous = GetJointActionObservationHistoryIndex(jaIs.size()-1,
2490  jaIs,joIs);
2491 
2493  double prob = GetJAOHProbs(dummy, jaohI, previous);
2494  delete dummy;
2495  return prob;
2496  //\todo TODO, because this function does not need to return to joint belief,
2497  //the last belief update is unecessary. therefore we can still gain some
2498  //speed here...
2499 }
2500 
2501 //print functions
2502 
2504 {
2505  size_t nrAgents=GetNrAgents();
2506  for(Index i = 0; i < nrAgents; i++)
2507  {
2508  cout << "ObservationHistoryTree for agent "<<i<<endl;
2510  }
2511 }
2512 
2514 {
2515  size_t nrAgents=GetNrAgents();
2516  for(Index i = 0; i < nrAgents; i++)
2517  {
2518  cout << "ActionHistoryTree for agent "<<i<<endl;
2520  }
2521 }
2522 
2524 {
2526  {
2527  cout<<"PlanningUnitMADPDiscrete::PrintActionObservationHistories()"<<
2528  " called on a P.U. that did not generate action-observation"<<
2529  " histories."<<endl;
2530  return;
2531  }
2532  size_t nrAgents=GetNrAgents();
2533  for(Index i = 0; i < nrAgents; i++)
2534  {
2535  cout << "ActionObservationHistoryTree for agent "<<i<<endl;
2537  }
2538 }
2539 
2540 using namespace PrintTools;
2541 
2543 {
2544  if(! _m_initialized)
2545  cerr << "This PlanningUnitMADPDiscrete is not initialized (yet)" <<
2546  endl;
2547  else
2548  {
2549  cout << "PlanningUnitMADPDiscrete parameters:" << endl;
2550  _m_params.Print();
2551 
2552  //observ.
2553  cout << "number of individual observation histories: ";
2554  PrintVectorCout(_m_nrObservationHistories);
2555  cout << endl;
2556  cout << "number of individual observation histories per time step: ";
2557  PrintVectorCout(_m_nrObservationHistoriesT);
2558  cout << endl;
2559  cout << "index of first indiv. observation histories per time step: ";
2560  PrintVectorCout(_m_firstOHIforT);
2561  cout << endl;
2562 
2563  if(_m_params.GetComputeIndividualObservationHistories())
2564  PrintObservationHistories();
2565  cout << "number of joint observation histories: "<<
2566  _m_nrJointObservationHistories << endl;
2567  if(_m_params.GetComputeJointObservationHistories())
2568  _m_jointObservationHistoryTreeRoot->Print();
2569  //actions
2570  cout << "number of individual action histories: ";
2571  PrintVectorCout(_m_nrActionHistories);
2572  cout << endl;
2573 
2574  cout << "number of individual action histories per time step: ";
2575  PrintVectorCout(_m_nrActionHistoriesT);
2576  cout << endl;
2577 
2578  cout << "index of first indiv. action histories per time step: ";
2579  PrintVectorCout(_m_firstAHIforT);
2580  cout << endl;
2581 
2582  if(_m_params.GetComputeIndividualActionHistories())
2583  PrintActionHistories();
2584  cout << "number of joint action histories: "<<
2585  _m_nrJointActionHistories << endl;
2586  if(_m_params.GetComputeJointActionHistories())
2587  _m_jointActionHistoryTreeRoot->Print();
2588 
2589  //action-observations
2590  cout << "number of individual action-observation histories: ";
2591  PrintVectorCout(_m_nrActionObservationHistories);
2592  cout << endl;
2593  cout << "number of individual action-observ. histories per time step: ";
2594  PrintVectorCout(_m_nrActionObservationHistoriesT);
2595  cout << endl;
2596  cout << "index of first indiv. action-obs. histories per time step: ";
2597  PrintVectorCout(_m_firstAOHIforT);
2598  cout << endl;
2599  if(_m_params.GetComputeIndividualActionObservationHistories())
2600  PrintActionObservationHistories();
2601  cout << "number of joint action observation histories: "<<
2602  _m_nrJointActionObservationHistories << endl;
2603  if(_m_params.GetComputeJointActionObservationHistories())
2604  _m_jointActionObservationHistoryTreeRoot->Print();
2605 
2606  }
2607 }
2608 
2610 {
2611  if(_m_params.GetComputeIndividualObservationHistories())
2612  return(GetObservationHistoryTree(agI, ohI)->GetContainedElement()->
2613  GetLength() );
2614 
2615  //else... find out what time step this is a joh for
2616  Index t = 0;
2617  size_t h=GetHorizon();
2618  while( t < h && _m_firstOHIforT.at(agI).at(t) <= ohI )
2619  t++;
2620 
2621  // _m_firstJOHIforT[t] > ohI OR t==GetHorizon
2622  if(t == h)
2623  t--; //last time step is h-1
2624  else
2625  t--;//_m_nrJointObservationHistoriesT[t] > ohI so ohI belongs to
2626  //previous time step.
2627  return(t);
2628 }
2630 {
2631  if(_m_params.GetComputeJointObservationHistories())
2632  return(GetJointObservationHistoryTree(johI)->GetContainedElement()->
2633  GetLength() );
2634 
2635  //else... find out what time step this is a joh for
2636  Index t = 0;
2637  size_t h=GetHorizon();
2638  while( t < h && _m_firstJOHIforT[t] <= johI )
2639  t++;
2640 
2641  // _m_firstJOHIforT[t] > johI OR t==GetHorizon
2642  if(t == h)
2643  t--; //last time step is h-1
2644  else
2645  t--;//_m_nrJointObservationHistoriesT[t] > johI so johI belongs to
2646  //previous time step.
2647  return(t);
2648 }
2649 
2651 {
2652  if(_m_params.GetComputeJointObservationHistories())
2653  return(GetJointObservationHistoryTree(johI)->GetSuccessor(joI)->
2654  GetIndex());
2655 
2656  Index t = GetTimeStepForJOHI(johI);
2657  if(t >= GetHorizon() - 1)
2658  throw E("taking successor of last time step joint observation history");
2659  //Index johI Without Offset:
2660  Index johI_wo = johI - _m_firstJOHIforT[t];
2661  //johI_wo = |jo|^t * joI(0) +...+ |jo|^0 * joI(t)
2662  //johIsuc_wo= |jo^|^(t+1) *joI(0) +...+ |jo|^1 * joI(t) + |jo|^0 * joI(t+1)
2663  //therefore the index of the successor (without offset) is:
2664  Index johIsuc_wo = johI_wo * GetNrJointObservations() + joI;
2665  return(johIsuc_wo + _m_firstJOHIforT[t+1]);
2666 }
2667 
2669  const
2670 {
2671  if(_m_params.GetComputeIndividualObservationHistories())
2672  return(GetObservationHistoryTree(agI, ohI)->GetSuccessor(oI)->
2673  GetIndex());
2674 
2675  Index t = GetTimeStepForOHI(agI, ohI);
2676  if(t >= GetHorizon() - 1)
2677  throw E("taking successor of last time step observation history");
2678  //Index ohI Without Offset:
2679  Index ohI_wo = ohI - _m_firstOHIforT.at(agI).at(t);
2680  //ohI_wo = |o|^t * oI(0) +...+ |o|^0 * oI(t)
2681  //ohIsuc_wo= |o^|^(t+1) *oI(0) +...+ |o|^1 * oI(t) + |o|^0 * oI(t+1)
2682  //therefore the index of the successor (without offset) is:
2683  Index ohIsuc_wo = ohI_wo * GetNrObservations(agI) + oI;
2684  return(ohIsuc_wo + _m_firstOHIforT.at(agI).at(t+1));
2685 }
2686 
2688 {
2689  if(_m_params.GetComputeIndividualActionHistories())
2690  return(GetActionHistoryTree(agI, ahI)->GetContainedElement()->
2691  GetLength() );
2692 
2693  //else... find out what time step this is a joh for
2694  Index t = 0;
2695  size_t h=GetHorizon();
2696  while( t < h && _m_firstAHIforT.at(agI).at(t) <= ahI )
2697  t++;
2698 
2699  // _m_firstJAHIforT[t] > ahI OR t==GetHorizon
2700  if(t == h)
2701  t--; //last time step is h-1
2702  else
2703  t--;//_m_nrJointActionHistoriesT[t] > ahI so ahI belongs to
2704  //previous time step.
2705  return(t);
2706 }
2708 {
2709  if(_m_params.GetComputeJointActionHistories())
2710  return(GetJointActionHistoryTree(jahI)->GetContainedElement()->
2711  GetLength() );
2712 
2713  //else... find out what time step this is a joh for
2714  Index t = 0;
2715  size_t h=GetHorizon();
2716  while( t < h && _m_firstJAHIforT[t] <= jahI )
2717  t++;
2718 
2719  // _m_firstJAHIforT[t] > jahI OR t==GetHorizon
2720  if(t == h)
2721  t--; //last time step is h-1
2722  else
2723  t--;//_m_nrJointActionHistoriesT[t] > jahI so jahI belongs to
2724  //previous time step.
2725  return(t);
2726 }
2728 {
2729  if(_m_params.GetComputeJointActionHistories())
2730  return(GetJointActionHistoryTree(jahI)->GetSuccessor(joI)->
2731  GetIndex());
2732 
2733  Index t = GetTimeStepForJAHI(jahI);
2734  if(t >= GetHorizon() - 1)
2735  throw E("taking successor of last time step joint action history");
2736  //Index jahI Without Offset:
2737  Index jahI_wo = jahI - _m_firstJAHIforT[t];
2738  //jahI_wo = |jo|^t * joI(0) +...+ |jo|^0 * joI(t)
2739  //jahIsuc_wo= |jo^|^(t+1) *joI(0) +...+ |jo|^1 * joI(t) + |jo|^0 * joI(t+1)
2740  //therefore the index of the successor (without offset) is:
2741  Index jahIsuc_wo = jahI_wo * GetNrJointActions() + joI;
2742  return(jahIsuc_wo + _m_firstJAHIforT[t+1]);
2743 }
2744 
2746  const
2747 {
2748  if(_m_params.GetComputeIndividualActionHistories())
2749  return(GetActionHistoryTree(agI, ahI)->GetSuccessor(oI)->
2750  GetIndex());
2751 
2752  Index t = GetTimeStepForAHI(agI, ahI);
2753  if(t >= GetHorizon() - 1)
2754  throw E("taking successor of last time step action history");
2755  //Index ahI Without Offset:
2756  Index ahI_wo = ahI - _m_firstAHIforT.at(agI).at(t);
2757  //ahI_wo = |o|^t * oI(0) +...+ |o|^0 * oI(t)
2758  //ahIsuc_wo= |o^|^(t+1) *oI(0) +...+ |o|^1 * oI(t) + |o|^0 * oI(t+1)
2759  //therefore the index of the successor (without offset) is:
2760  Index ahIsuc_wo = ahI_wo * GetNrActions(agI) + oI;
2761  return(ahIsuc_wo + _m_firstAHIforT.at(agI).at(t+1));
2762 }
2763 
2765 {
2766  // copied from GetTimeStepForJOHI
2767  if(_m_params.GetComputeIndividualActionObservationHistories())
2768  return(GetActionObservationHistoryTree(agI, aohI)->
2769  GetContainedElement()->GetLength() );
2770  else
2771  {
2772  Index t = 0;
2773  while( t < GetHorizon() && _m_firstAOHIforT.at(agI).at(t) <= aohI )
2774  t++;
2775  // _m_firstJAOHIforT[t] > johI OR t==GetHorizon
2776  if(t == GetHorizon())
2777  t--; //last time step is h-1
2778  else
2779  t--;//_m_nrJointActionObservationHistoriesT[t] > aohI so
2780  //johI belongs to previous time step.
2781  return(t);
2782  }
2783 }
2785 {
2786  // copied from GetTimeStepForJOHI
2787  if(_m_params.GetComputeJointActionObservationHistories())
2788  return(GetJointActionObservationHistoryTree(jaohI)->
2789  GetContainedElement()->GetLength() );
2790  else
2791  {
2792  Index t = 0;
2793  while( t < GetHorizon() && _m_firstJAOHIforT[t] <= jaohI )
2794  t++;
2795  // _m_firstJAOHIforT[t] > johI OR t==GetHorizon
2796  if(t == GetHorizon())
2797  t--; //last time step is h-1
2798  else
2799  t--;//_m_nrJointActionObservationHistoriesT[t] > jaohI so
2800  //johI belongs to previous time step.
2801  return(t);
2802  }
2803 }
2804 
2806  Index aohI, Index aI, Index oI) const
2807 {
2808  // copied from GetSuccessorJOHI, look there for comments
2809  if(_m_params.GetComputeIndividualActionObservationHistories())
2810  return(GetActionObservationHistoryTree(agI, aohI)->
2811  GetSuccessor(aI,oI)->GetIndex());
2812  else
2813  {
2814  Index t = GetTimeStepForAOHI(agI, aohI);
2815  if(t >= GetHorizon() - 1)
2816  throw E("taking successor of last time step action observation history");
2817  Index aohI_wo = aohI - _m_firstAOHIforT.at(agI).at(t);
2818  Index aohIsuc_wo = aohI_wo * GetNrObservations(agI)*
2819  GetNrActions(agI) + aI * GetNrObservations(agI) +oI;
2820  return(aohIsuc_wo + _m_firstAOHIforT.at(agI).at(t+1));
2821  }
2822 }
2824  Index joI) const
2825 {
2826  Index jaohIsuc;
2827  // copied from GetSuccessorJOHI, look there for comments
2828  if(_m_params.GetComputeJointActionObservationHistories())
2829  jaohIsuc=GetJointActionObservationHistoryTree(jaohI)->
2830  GetSuccessor(jaI,joI)->GetIndex();
2831  else
2832  {
2833  Index t = GetTimeStepForJAOHI(jaohI);
2834  if(t >= GetHorizon() - 1)
2835  throw E("taking successor of last time step joint action observation history");
2836  Index jaohI_wo = jaohI - _m_firstJAOHIforT[t];
2837  Index jaohIsuc_wo = jaohI_wo * GetNrJointObservations()*
2838  GetNrJointActions() + jaI * GetNrJointObservations() +joI;
2839  jaohIsuc=jaohIsuc_wo + _m_firstJAOHIforT[t+1];
2840  }
2841 
2842  if(jaohIsuc<jaohI)
2843  throw(EOverflow("PlanningUnitMADPDiscrete::GetSuccessorJAOHI index overflow detected"));
2844 
2845  return(jaohIsuc);
2846 }
2847 
2850  jaoht)
2851 {
2852  Index i=jaoht->GetIndex();
2853  _m_jointActionObservationHistoryTreeMap[i]=jaoht;
2854 }
2855 
2857 {
2858  JointBeliefInterface* b0 = GetNewJointBeliefInterface();
2859  const MultiAgentDecisionProcessDiscreteInterface* madpd = GetMADPDI();
2860  const StateDistribution* isd = madpd->GetISD();
2861  b0->Set( *isd );
2862  return(b0);
2863 }
2864 
2867  const
2868 {
2869  if(_m_params.GetComputeJointActionObservationHistories())
2870  return(_m_jointActionObservationHistoryTreeVector.at(jaohI));
2871  else
2872  {
2873  // cannot use m_jointActionObservationHistoryTreeMap[jaohI],
2874  // it violates the const, as it creates an empty object if
2875  // jaohI does not exist yet
2876  map<Index,JointActionObservationHistoryTree*>::const_iterator cit
2877  = _m_jointActionObservationHistoryTreeMap.find(jaohI);
2878  if(cit!=_m_jointActionObservationHistoryTreeMap.end())
2879  return(cit->second);
2880  else
2881  throw(E("GetJointActionObservationHistoryTree:: index not found in map"));
2882  }
2883 }
2884 
2887 {
2888  switch ( pdc )
2889  {
2891  return _m_params.GetComputeJointObservationHistories();
2892  break;
2894  return _m_params.GetComputeJointActionObservationHistories();
2895  break;
2897  throw(E("PlanningUnitMADPDiscrete::AreCachedJointToIndivIndices: types are not defined in a PlanningUnitMADPDiscrete context"));
2898  return false;
2899  break;
2900  default:
2901  throw(E("Used a JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory unknown to PlanningUnitMADPDiscrete!"));
2902  break;
2903  } /* ----- end switch ----- */
2904 }
2905 
2907  Index agentI, Index dI, PolicyGlobals::IndexDomainCategory cat ) const
2908 {
2909  switch ( cat )
2910  {
2912  {
2913  string s = SoftPrintObservationHistory(agentI, dI);
2914  return s;
2915 
2916 
2917  break;
2918  }
2920  return GetActionObservationHistoryTree(agentI,dI)->
2921  GetActionObservationHistory()->SoftPrint();
2922  break;
2924  throw(E("SoftPrintPolicyDomain: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
2925  break;
2926  default:
2927  throw(E("SoftPrintPolicyDomain: incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
2928  break;
2929  }
2930  return("IMPOSSIBLE!");
2931 }
2932 
2935  size_t depth ) const
2936 {
2937  switch ( cat )
2938  {
2940  if(depth==MAXHORIZON || depth==GetHorizon())
2941  return GetNrObservationHistories(agentI);
2942  else
2943  return(_m_firstOHIforT.at(agentI).at(depth));
2944  break;
2946  if(depth==MAXHORIZON || depth==GetHorizon())
2947  return GetNrActionObservationHistories(agentI);
2948  else
2949  return(_m_firstAOHIforT.at(agentI).at(depth));
2950  break;
2952  throw(E(" GetNrPolicyDomainElements: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
2953  break;
2954  default:
2955  throw(E("incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
2956  break;
2957  }
2958 }
2959 
2961  const
2962 {
2964 }
2965 
2967 {
2968  if( _m_params.GetUseSparseJointBeliefs() )
2969  return (new JointBeliefSparse(GetNrStates()) );
2970  else
2971  return (new JointBelief(GetNrStates()) );
2972 }
2973 
2975  const
2976 {
2977  if( _m_params.GetUseSparseJointBeliefs() )
2978  return (new JointBeliefSparse(size) );
2979  else
2980  return (new JointBelief(size) );
2981 }
2982 
2984  Index ohIndex) const
2985 {
2986 
2987  string s;
2988  if(_m_params.GetComputeIndividualObservationHistories())
2989  s = GetObservationHistoryTree(agentI,ohIndex)->
2990  GetObservationHistory()->SoftPrint();
2991  else
2992  {
2993  Index t = GetTimeStepForOHI(agentI, ohIndex);
2994  Index obs[t];
2995  GetObservationHistoryArrays(agentI, ohIndex, t, obs);
2996  stringstream ss;
2997  ss << "(";
2998  for(Index t2=0; t2<t; t2++)
2999  {
3000  Index oI = obs[t2];
3001  string oName = GetObservation(agentI, oI)->GetName();
3002  ss << oName;
3003  if(t2 + 1 < t)
3004  ss << ",";
3005  }
3006  ss << ")";
3007  s = ss.str();
3008  }
3009  return(s);
3010 }
3011 
3013 {
3014  return(GetAction(agentI, actionI)->GetName());
3015 }