58 #define DEBUG_PUDCOHT 0
59 #define DEBUG_PUDCAHT 0
60 #define DEBUG_PUDCAOHT 0
63 #define DEBUG_PUDCJOHT 0
64 #define DEBUG_PUDCJAHT 0
65 #define DEBUG_PUDCJAOHT 0
67 #define DEBUG_PUD_JAOHPROBS 0
68 #define DEBUG_PUDCGETJB 0
82 if(
DEBUG_PU_CONSTRUCTORS) cout <<
"PlanningUnitMADPDiscrete(PlanningUnitMADPDiscreteParameters params, size_t horizon, MultiAgentDecisionProcessDiscreteInterface* p) called" << endl;
98 if(
DEBUG_PU_CONSTRUCTORS) cout <<
"PlanningUnitMADPDiscrete( size_t horizon, MultiAgentDecisionProcessDiscreteInterface* p) called" << endl;
114 cout <<
"\n>>>PlanningUnitMADPDiscrete::SetProblem - setting the following problem\n"
147 if(
DEBUG_PUD) cout <<
"PlanningUnitMADPDiscrete::Initialize()" <<endl;
155 throw(
E(
"PlanningUnitMADPDiscrete::Initialize sanity check failed"));
180 if(
DEBUG_PUD){ cout <<
"PlanningUnitMADPDiscrete::Initialize() done - info:" <<endl;
Print(); }
207 for(
Index i = 0; i < nrAgents; i++)
209 vector<ObservationHistoryTree*> * temp =
new
210 vector<ObservationHistoryTree*>;
229 vector<size_t> oh_per_t;
230 vector<LIndex> firstOHindex_per_t;
236 firstOHindex_per_t.clear();
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);
259 queue<ObservationHistoryTree*> ohtQueue;
261 Index ObservationHistoryIndex = 0;
275 while(ohtQueue.size() > 0)
278 oht->
SetIndex(ObservationHistoryIndex);
282 if(ts != (
Index) thisLength)
285 Index nr_OHI_for_ts = ObservationHistoryIndex -
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;
296 cout <<
"Set _m_nrObservationHistoriesT[agentI="<<agentI<<
"][t="<<
297 tempIndex <<
" (ts="<<ts<<
")] = "<< nr_OHI_for_ts << endl;
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);
306 ObservationHistoryIndex++;
308 if(thisLength < maxLength)
309 for(
Index obsI = 0; obsI < nrO; obsI++)
315 ohtQueue.push(next_oht);
322 Index nr_OHI_for_ts = (ObservationHistoryIndex)
325 cout <<
"Last ts end: store nr of ts=h-1 OH..."<<endl<<
326 "_m_firstOHIforT[agentI="<<agentI<<
"][ts="<<ts<<
"] = "<<
328 ObservationHistoryIndex << endl;
333 cout <<
"Set _m_nrObservationHistoriesT[agentI="<<agentI<<
"][t="<<tempIndex
334 <<
" (last time step ts="<<ts<<
")] = "<< nr_OHI_for_ts<<endl;
342 if( ObservationHistoryIndex != nrObsHist ||
344 cerr <<
" WARNING:ObservationHistoryIndex="<< ObservationHistoryIndex <<
345 " nrObsHist=" << nrObsHist <<
" _m_observationHistoryTreeVectors[agentI="
357 vector<ObservationHistoryTree *>::iterator it =
359 vector<ObservationHistoryTree *>::iterator last =
386 queue<JointObservationHistoryTree*> johtQueue;
388 Index JointObservationHistoryIndex = 0;
399 johtQueue.push(root);
403 while(johtQueue.size() > 0)
406 joht->
SetIndex(JointObservationHistoryIndex);
410 if(ts != thisLength )
415 if(++ts != thisLength )
416 cerr <<
"ERROR: CreateJointObservationHistories: ++ts != "<<
417 "(Index) thisLength !!! ts (now) is: "<<ts<<
418 "thisLength =" << thisLength <<endl;
422 JointObservationHistoryIndex++;
424 cout <<
"PlanningUnitMADPDiscrete::"<<
425 "CreateJointObservationHistoryTree - New joh tree from queue:"<<
426 endl<<
"\t"; joht->
Print();
429 if(thisLength < maxLength)
432 cout <<
"PlanningUnitMADPDiscrete::"<<
433 "CreateJointObservationHistoryTree - Extending this joint "<<
434 "observation history tree..."<<endl;
436 for(
Index jObsI = 0; jObsI < nrJO; jObsI++)
439 cout <<
"PlanningUnitMADPDiscrete::Create"
440 <<
"JointObservationHistoryTree - creating successor for "
441 <<
"joint observation "<< jObsI << endl;
444 GetJointObservationHistory();
446 cout <<
"( history of pred was:";
447 temp->
Print(); cout <<
") " << endl;
452 johtQueue.push(next_joht);
508 for(
Index i = 0; i < nrAgents; i++)
510 vector<ActionHistoryTree*> * temp =
new
511 vector<ActionHistoryTree*>;
530 vector<size_t> oh_per_t;
531 vector<LIndex> firstAHindex_per_t;
537 firstAHindex_per_t.clear();
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);
559 queue<ActionHistoryTree*> ohtQueue;
561 Index ActionHistoryIndex = 0;
578 while(ohtQueue.size() > 0)
581 oht->SetIndex(ActionHistoryIndex);
583 int thisLength = oht->GetContainedElement()->GetLength();
584 if(ts != (
Index) thisLength )
587 Index nr_AHI_for_ts = ActionHistoryIndex -
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;
598 cout <<
"Set _m_nrActionHistoriesT[agentI="<<agentI<<
"][t="<<
599 tempIndex <<
" (ts="<<ts<<
")] = "<< nr_AHI_for_ts <<endl;
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);
608 ActionHistoryIndex++;
610 if(thisLength < maxLength)
611 for(
Index obsI = 0; obsI < nrO; obsI++)
614 oht->GetActionHistory());
616 oht->SetSuccessor(obsI, next_oht);
617 ohtQueue.push(next_oht);
623 Index nr_AHI_for_ts = ActionHistoryIndex
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;
634 cout <<
"Set _m_nrActionHistoriesT[agentI="<<agentI<<
"][t="<<tempIndex
635 <<
" (last time step ts="<<ts<<
")] = "<< nr_AHI_for_ts<<endl;
646 if( ActionHistoryIndex != nrActionHist ||
648 cerr <<
" WARNING:ActionHistoryIndex="<< ActionHistoryIndex <<
649 " nrActionHistories=" << nrActionHist <<
650 " _m_actionHistoryTreeVectors[agentI="<<agentI<<
"].size()="<<
662 queue<JointActionHistoryTree*> jahtQueue;
664 Index JointActionHistoryIndex = 0;
674 jahtQueue.push(root);
675 while(jahtQueue.size() > 0)
678 jaht->SetIndex(JointActionHistoryIndex);
680 JointActionHistoryIndex++;
681 int thisLength = jaht->GetContainedElement()->GetLength();
684 cout <<
"PlanningUnitMADPDiscrete::CreateJointActionHistoryTree "
685 <<
"- New jah tree from queue:"<<endl<<
"\t"; jaht->Print();
687 if(thisLength < maxLength)
690 cout <<
"PlanningUnitMADPDiscrete::CreateJointActionHistoryTree "
691 <<
"- Extending this joint action history tree..."<<endl;
693 for(
Index jAI = 0; jAI < nrJA; jAI++)
696 cout <<
"PlanningUnitMADPDiscrete::CreateJointActionHistoryTree"
697 <<
" - creating successor for joint action "
703 cout <<
"( history of pred was:";
704 temp->
Print(); cout <<
") " << endl;
708 jaht->SetSuccessor(jAI, next_jaht);
709 jahtQueue.push(next_jaht);
746 vector<ActionHistoryTree *>::iterator it =
748 vector<ActionHistoryTree *>::iterator last =
783 vector<ActionObservationHistoryTree *>::iterator it =
785 vector<ActionObservationHistoryTree *>::iterator last =
808 #if 0 // TODO: fix this, segfaults currently, and hence leaks memory
815 #else // not sure whether everything will be deleted now
823 vector<const JointBeliefInterface*>::iterator it =
825 vector<const JointBeliefInterface*>::iterator last =
847 for(
Index i = 0; i < nrAgents; i++)
849 vector<ActionObservationHistoryTree*> * temp =
new
850 vector<ActionObservationHistoryTree*>;
870 vector<size_t> oh_per_t;
871 vector<LIndex> firstAOHindex_per_t;
878 firstAOHindex_per_t.clear();
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);
922 queue<ActionObservationHistoryTree*> aohtQueue;
933 aohtQueue.push(root);
937 while(aohtQueue.size() >= 1)
945 if(ts != (
Index) thisLength )
948 Index nr_AOHI_for_ts = aohI -
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 = " <<
960 "Set _m_nrActionObservationHistoriesT[agentI="<<agentI<<
"][t="<<
961 tempIndex <<
" (ts="<<ts<<
")] = "<< nr_AOHI_for_ts <<endl;
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);
978 for(
Index aI = 0; aI < nrA; aI++)
979 for(
Index oI = 0; oI < nrO; oI++)
986 cout <<
"created new aoh (for aI="<<aI<<
", oI="<<oI<<
":"<<
995 cout <<
"\ncreated new aoh tree:"<<endl;
1001 aohtQueue.push(next_aoht);
1016 queue<JointActionObservationHistoryTree*> jaohtQueue;
1027 jaohtQueue.push(root);
1029 queue<JointBeliefInterface*> jBeliefQueue;
1030 queue<double> cpQueue;
1031 queue<double> pQueue;
1039 jBeliefQueue.push(b0);
1046 while(jaohtQueue.size() >= 1)
1051 GetJointActionObservationHistory();
1054 Index thisLength = jaoh
1056 if(ts != thisLength )
1061 if(++ts != thisLength )
1062 cerr <<
"ERROR: CreateJointObservationHistories: ++ts != "<<
1063 "thisLength !!! ts (now) is: "<<ts<<
1064 "thisLength =" << thisLength <<endl;
1072 double prob = pQueue.front();
1076 double cprob = cpQueue.front();
1079 jb = jBeliefQueue.front();
1094 for(
Index jaI = 0; jaI < nrJA; jaI++)
1095 for(
Index joI = 0; joI < nrJO; joI++)
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();
1111 jaohtQueue.push(next_jaoht);
1116 jBeliefQueue.push(new_jb);
1117 cpQueue.push(new_cond_p);
1118 double new_p = prob * new_cond_p;
1149 size_t historiesThisAgent =
1152 nrT *= historiesThisAgent;
1184 #if 0 // don't use powl(), we want to detect overflow
1185 return( static_cast<LIndex>(powl(
GetNrActions(agentI),
1193 if(nrPols<nrPolsOld)
1194 throw(
EOverflow(
"PlanningUnitMADPDiscrete::GetNrPolicies() overflow detected"));
1207 throw(
EOverflow(
"PlanningUnitMADPDiscrete::GetNrJointPolicies() overflow detected"));
1217 throw(
"ERROR PlanningUnitMADPDiscrete::GetNrObservationHistories(Index agentI) index out of bounds or _m_nrObservationHistories not yet initialized?!\n");
1222 vector<size_t> nrOHts;
1230 vector<size_t> nrOHts;
1239 throw ENotCached(
"PlanningUnitMADPDiscrete::GetObservationHistoryTree IndividualObservationHistories are not cached!");
1243 throw E(
"_m_observationHistoryTreeVectors.size() != nrA)");
1250 throw E(
"_m_observationHistoryTreeVectors[agentI].size() != nrOH)");
1254 if(
DEBUG_PUD){ cout <<
"accessing _m_observationHistoryTreeVectors["<< agentI<<
"]["<<ohI<<
"]"<<endl;}
1258 if(
DEBUG_PUD){ cout <<
"...done - returning "<<oht<<endl;}
1262 cerr <<
"WARNING PlanningUnitMADPDiscrete::GetObservationHistory(Index agentI, Index a) index ohI out of bounds"<<endl;
1265 cerr <<
"WARNING PlanningUnitMADPDiscrete::GetObservationHistory(Index agentI, Index ohI) index agentI out of bounds"<<endl;
1277 cerr <<
"PlanningUnitMADPDiscrete::GetJointObservationHistoryTree - Warning: "<<
1278 "index (" << johI <<
")out of bound!" << endl;
1298 size_t indexDomainSize
1307 Index hI_withinT = hI - t_offset;
1308 Index remainder = hI_withinT;
1309 size_t vec_size = t;
1310 size_t nrO = indexDomainSize;
1311 for(
Index t2=0; t2 < vec_size ; t2++)
1327 size_t stage = (t-1) - t2;
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;
1338 (
Index jaohI, vector<Index> &jaIs, vector<Index> &joIs)
const
1346 GetJointActionObservationHistoryTree(jaohI);
1348 GetJointActionObservationHistoryVectors(jaIs, joIs);
1353 Index t = GetTimeStepForJAOHI(jaohI);
1356 GetJointActionObservationHistoryArrays(jaohI, t, jaIs_arr, joIs_arr );
1357 for(
size_t t2 = 0; t2 < t; t2++)
1359 jaIs.push_back(jaIs_arr[t2]);
1360 joIs.push_back(joIs_arr[t2]);
1370 Index jaohIwithinThisT = jaohI - _m_firstJAOHIforT.at(t);
1372 Index remainder = jaohIwithinThisT;
1373 size_t vec_size = t;
1376 size_t nrJO = GetNrJointObservations();
1377 size_t nrJA = GetNrJointActions();
1378 size_t nrJAO = nrJO*nrJA;
1380 for(
Index t2=0; t2 < vec_size ; t2++)
1396 size_t stage = (t-1) - t2;
1398 size_t b = (size_t) pow((
double)nrJAO, (double)(stage));
1400 Index jaoI_t2 = remainder / b;
1402 remainder -= jaoI_t2 * b;
1418 Index johIwithinThisT = johI - _m_firstJOHIforT.at(t);
1420 Index remainder = johIwithinThisT;
1421 size_t vec_size = t;
1422 size_t nrJO = GetNrJointObservations();
1424 for(
Index t2=0; t2 < vec_size ; t2++)
1429 size_t stage = (t-1) - t2;
1431 size_t b = (size_t) pow((
double)nrJO, (double)(stage));
1432 Index joI_t2 = remainder / b;
1434 remainder -= joI_t2 * b;
1444 Index aohIwithinThisT = aohI - _m_firstAOHIforT.at(agentI).at(t);
1446 Index remainder = aohIwithinThisT;
1447 size_t vec_size = t;
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++)
1457 size_t stage = (t-1) - t2;
1459 size_t b = (size_t) pow((
double)nrAO, (double)(stage));
1461 Index aoI_t2 = remainder / b;
1462 remainder -= aoI_t2 * b;
1478 Index ohIwithinThisT = ohI - _m_firstOHIforT.at(agentI).at(t);
1480 Index remainder = ohIwithinThisT;
1481 size_t vec_size = t;
1482 size_t nrO = GetNrObservations(agentI);
1484 for(
Index t2=0; t2 < vec_size ; t2++)
1489 size_t stage = (t-1) - t2;
1491 size_t b = (size_t) pow((
double)nrO, (double)(stage));
1492 Index oI_t2 = remainder / b;
1494 remainder -= oI_t2 * b;
1504 Index ahIwithinThisT = ahI - _m_firstAHIforT.at(agentI).at(t);
1506 Index remainder = ahIwithinThisT;
1507 size_t vec_size = t;
1508 size_t nrA = GetNrActions(agentI);
1509 for(
Index t2=0; t2 < vec_size ; t2++)
1514 size_t stage = (t-1) - t2;
1516 size_t b = (size_t) pow((
double)nrA, (double)(stage));
1517 Index aI_t2 = remainder / b;
1519 remainder -= aI_t2 * b;
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?");
1548 throw E(
"GetJointObservationHistoryIndex index not found");
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?");
1561 throw E(
"GetJointObservationHistoryIndex index not found");
1570 const Index indices [],
1571 size_t indexDomainSize
1586 for (
Index ts=0; ts<t; ts++)
1588 int exponent = t-1 - ts;
1589 size_t b = (size_t) pow((
double)indexDomainSize, (double)(exponent));
1590 HI += indices[ts] * b;
1597 Index t,
const vector<Index>& observations)
const
1629 Index t,
const vector<Index>& jointObservations)
const
1634 &jointObservations[0],
1650 Index t,
const Index jointObservations[] )
const
1672 Index t,
const vector<Index>& jointActions)
const
1716 const vector<Index>& actions,
1717 const vector<Index>& observations
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);
1754 const vector<Index>& Jactions,
1755 const vector<Index>& Jobservations
1764 size_t nrJAO = nrJA * nrJO;
1766 vector<Index> JAO_Is(t, 0);
1767 for (
Index ts=0; ts<t; ts++)
1769 Jactions.at(ts), Jobservations.at(ts), nrJA, nrJO);
1799 vector< vector<Index> > indivO_vec;
1802 indivO_vec.push_back( vector<Index>() );
1804 for (
Index ts=0; ts<t; ts++)
1806 vector<Index> indivIndicesThisT =
1809 indivO_vec.at(agentI).push_back( indivIndicesThisT.at(agentI) );
1813 vector<Index> ohI_vec;
1817 indivO_vec.at(agentI) );
1818 ohI_vec.push_back(this_agentOHI);
1823 const vector<Index>&
1832 GetIndividualObservationHistoryIndices());
1837 ss <<
"PlanningUnitMADPDiscrete::JointToIndividualObservationHistory"<<
1838 "Indices ERROR: index ("<< jObsHistI <<
") out of bounds!"<<endl;
1843 const vector<Index>&
1852 GetIndividualActionObservationHistoryIndices());
1857 ss <<
"PlanningUnitMADPDiscrete::JointToIndividualActionActObserva"<<
1858 "tionHistory Indices ERROR: index ("<< jaohI <<
1859 ") out of bounds!"<<endl;
1867 vector< vector<Index> >& indivAO_vec
1870 throw E(
"PlanningUnitMADPDiscrete::JointAOHIndexToIndividualActionObservationVectors(Index jaohI, vector< vector<Index> >& indivAO_vec ) const is not yet implemented.");
1877 vector< vector<Index> >& indivO_vec,
1878 vector< vector<Index> >& indivA_vec
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++)
1898 vector<Index> indivA_IndicesThisT =
1900 vector<Index> indivO_IndicesThisT =
1904 indivO_vec[agentI][ts] = indivO_IndicesThisT[agentI];
1905 indivA_vec[agentI][ts] = indivA_IndicesThisT[agentI];
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 );
1922 vector<Index> aohI_vec;
1926 indivA_vec.at(agentI), indivO_vec.at(agentI) );
1927 aohI_vec.push_back(this_agentAOHI);
1933 Index jObsHistI)
const
1935 throw E(
"PlanningUnitMADPDiscrete::JointToIndividualActionHistoryIndices(Index jObsHistI) NOT YET IMPLEMENTED");
1945 return(joht->GetJointActionHistory()->
1946 GetIndividualActionHistoryIndices());
1951 ss <<
"PlanningUnitMADPDiscrete::JointToIndividualActionHistoryIndices"
1952 <<
" ERROR: index ("<< jObsHistI <<
") out of bounds!"<<endl;
1976 throw(
E(
"JointToIndividualPolicyDomainIndices: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
1979 throw(
E(
"JointToIndividualPolicyDomainIndices: incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
1996 throw(
E(
"JointToIndividualPolicyDomainIndicesRef: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
1999 throw(
E(
"JointToIndividualPolicyDomainIndicesRef: incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
2015 const vector<Index>& indivIs)
2020 vector< Index* > observSeqs(nrAg);
2021 for (
Index agentI = 0; agentI < nrAg; agentI++)
2023 observSeqs[agentI] =
new Index[t];
2024 Index ohI_thisAgent = indivIs.at(agentI);
2026 observSeqs[agentI] );
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];
2035 for (
Index agentI = 0; agentI < nrAg; agentI++)
2036 delete [] observSeqs[agentI];
2039 vector<Index> jobs(t);
2040 for (
Index ts = 0; ts < t; ts++)
2050 const vector<Index>& indivIs)
2055 vector< Index* > observSeqs(nrAg);
2056 for (
Index agentI = 0; agentI < nrAg; agentI++)
2058 observSeqs[agentI] =
new Index[t];
2059 Index ahI_thisAgent = indivIs.at(agentI);
2061 observSeqs[agentI] );
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];
2070 for (
Index agentI = 0; agentI < nrAg; agentI++)
2071 delete [] observSeqs[agentI];
2074 vector<Index> jabs(t);
2075 for (
Index ts = 0; ts < t; ts++)
2086 Index t,
const vector<Index>& indivIs)
const
2090 vector< Index* > observSeqs(nrAg);
2091 vector< Index* > actionSeqs(nrAg);
2092 for (
Index agentI = 0; agentI < nrAg; agentI++)
2094 observSeqs[agentI] =
new Index[t];
2095 actionSeqs[agentI] =
new Index[t];
2096 Index aohI_thisAgent = indivIs.at(agentI);
2098 actionSeqs[agentI], observSeqs[agentI] );
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++)
2107 stageObserv[ts][agentI] = observSeqs[agentI][ts];
2108 stageAction[ts][agentI] = actionSeqs[agentI][ts];
2111 for (
Index agentI = 0; agentI < nrAg; agentI++)
2113 delete [] observSeqs[agentI];
2114 delete [] actionSeqs[agentI];
2118 vector<Index> jobs(t);
2119 vector<Index> jacs(t);
2121 for (
Index ts = 0; ts < t; ts++)
2157 throw(
"ERROR PlanningUnitMADPDiscrete::GetNrActionHistories(Index agentI) index out of bounds or _m_nrActionHistories not yet initialized?!\n");
2179 throw E(
"PlanningUnitMADPDiscrete::GetActionHistoryTree _m_actionHistoryTreeVectors.size() != nrA)");
2184 if(
DEBUG_PUD){cout <<
"_m_actionHistoryTreeVectors["<<agentI<<
2187 throw E(
"_m_actionHistoryTreeVectors[agentI].size() != nrOH)");
2191 if(
DEBUG_PUD){ cout <<
"accessing _m_actionHistoryTreeVectors["<<
2192 agentI<<
"]["<<ohI<<
"]"<<endl;}
2196 if(
DEBUG_PUD){ cout <<
"...done - returning "<<oht<<endl;}
2200 cerr <<
"WARNING PlanningUnitMADPDiscrete::GetActionHistory(Index"<<
2201 " agentI, Index a) index ohI out of bounds"<<endl;
2204 cerr <<
"WARNING PlanningUnitMADPDiscrete::GetActionHistory(Index agentI, Index ohI) index agentI out of bounds"<<endl;
2216 cerr <<
"PlanningUnitMADPDiscrete::GetJointActionHistoryTree - Warning: "<<
2217 "index (" << johI <<
")out of bound!" << endl;
2232 throw E(
"GetJointActionHistoryIndex index not found");
2270 for(
unsigned i=0;i!=jaIs.size();++i)
2296 if(p_jaohI == 0 && p_jb == NULL && jpol==NULL)
2314 #if DEBUG_PUD_JAOHPROBS
2315 vector<Index> jaIsv,joIsv;
2321 cout <<
"t " << t <<
" " << jaohI
2327 for(
Index i=0;i!=t;++i)
2328 cout <<
"jaIs["<<i<<
"]=" << jaIs[i] <<
" ";
2342 throw E(
" p_jaohI != 0 but t_p == 0 ");
2346 for(
Index tI=0; tI < t_p; tI++)
2348 if ( jaIs[tI] == p_jaIs[tI] &&
2349 joIs[tI] == p_joIs[tI] )
2353 cerr <<
"GetJAOHProbs:: Warning pred. inconsistent with requested jaohI" << endl;
2361 if(p_jaohI != 0 && p_jb == NULL)
2372 double pr_cond = pr / p_pr;
2387 else if ( p_jb == NULL )
2421 Index jaI = jaIs[t_p];
2422 Index joI = joIs[t_p];
2436 double polProb = 0.0;
2449 throw E(
"PlanningUnitMADPDiscrete::GetJAOHProbsRecursively --- policy has unsupported IndexDomainCategory");
2457 double Prob_jaohI = P_remainder * cond_P * polProb;
2458 return (Prob_jaohI);
2471 double p =
GetJAOHProbs(&dummy, jaohI, p_jaohI, p_jb, jpol );
2506 for(
Index i = 0; i < nrAgents; i++)
2508 cout <<
"ObservationHistoryTree for agent "<<i<<endl;
2516 for(
Index i = 0; i < nrAgents; i++)
2518 cout <<
"ActionHistoryTree for agent "<<i<<endl;
2527 cout<<
"PlanningUnitMADPDiscrete::PrintActionObservationHistories()"<<
2528 " called on a P.U. that did not generate action-observation"<<
2529 " histories."<<endl;
2533 for(
Index i = 0; i < nrAgents; i++)
2535 cout <<
"ActionObservationHistoryTree for agent "<<i<<endl;
2540 using namespace PrintTools;
2544 if(! _m_initialized)
2545 cerr <<
"This PlanningUnitMADPDiscrete is not initialized (yet)" <<
2549 cout <<
"PlanningUnitMADPDiscrete parameters:" << endl;
2553 cout <<
"number of individual observation histories: ";
2556 cout <<
"number of individual observation histories per time step: ";
2559 cout <<
"index of first indiv. observation histories per time step: ";
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();
2570 cout <<
"number of individual action histories: ";
2574 cout <<
"number of individual action histories per time step: ";
2578 cout <<
"index of first indiv. action histories per time step: ";
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();
2590 cout <<
"number of individual action-observation histories: ";
2593 cout <<
"number of individual action-observ. histories per time step: ";
2596 cout <<
"index of first indiv. action-obs. histories per time step: ";
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();
2611 if(_m_params.GetComputeIndividualObservationHistories())
2612 return(GetObservationHistoryTree(agI, ohI)->GetContainedElement()->
2617 size_t h=GetHorizon();
2618 while( t < h && _m_firstOHIforT.at(agI).at(t) <= ohI )
2631 if(_m_params.GetComputeJointObservationHistories())
2632 return(GetJointObservationHistoryTree(johI)->GetContainedElement()->
2637 size_t h=GetHorizon();
2638 while( t < h && _m_firstJOHIforT[t] <= johI )
2652 if(_m_params.GetComputeJointObservationHistories())
2653 return(GetJointObservationHistoryTree(johI)->GetSuccessor(joI)->
2656 Index t = GetTimeStepForJOHI(johI);
2657 if(t >= GetHorizon() - 1)
2658 throw E(
"taking successor of last time step joint observation history");
2660 Index johI_wo = johI - _m_firstJOHIforT[t];
2664 Index johIsuc_wo = johI_wo * GetNrJointObservations() + joI;
2665 return(johIsuc_wo + _m_firstJOHIforT[t+1]);
2671 if(_m_params.GetComputeIndividualObservationHistories())
2672 return(GetObservationHistoryTree(agI, ohI)->GetSuccessor(oI)->
2675 Index t = GetTimeStepForOHI(agI, ohI);
2676 if(t >= GetHorizon() - 1)
2677 throw E(
"taking successor of last time step observation history");
2679 Index ohI_wo = ohI - _m_firstOHIforT.at(agI).at(t);
2683 Index ohIsuc_wo = ohI_wo * GetNrObservations(agI) + oI;
2684 return(ohIsuc_wo + _m_firstOHIforT.at(agI).at(t+1));
2689 if(_m_params.GetComputeIndividualActionHistories())
2690 return(GetActionHistoryTree(agI, ahI)->GetContainedElement()->
2695 size_t h=GetHorizon();
2696 while( t < h && _m_firstAHIforT.at(agI).at(t) <= ahI )
2709 if(_m_params.GetComputeJointActionHistories())
2710 return(GetJointActionHistoryTree(jahI)->GetContainedElement()->
2715 size_t h=GetHorizon();
2716 while( t < h && _m_firstJAHIforT[t] <= jahI )
2729 if(_m_params.GetComputeJointActionHistories())
2730 return(GetJointActionHistoryTree(jahI)->GetSuccessor(joI)->
2733 Index t = GetTimeStepForJAHI(jahI);
2734 if(t >= GetHorizon() - 1)
2735 throw E(
"taking successor of last time step joint action history");
2737 Index jahI_wo = jahI - _m_firstJAHIforT[t];
2741 Index jahIsuc_wo = jahI_wo * GetNrJointActions() + joI;
2742 return(jahIsuc_wo + _m_firstJAHIforT[t+1]);
2748 if(_m_params.GetComputeIndividualActionHistories())
2749 return(GetActionHistoryTree(agI, ahI)->GetSuccessor(oI)->
2752 Index t = GetTimeStepForAHI(agI, ahI);
2753 if(t >= GetHorizon() - 1)
2754 throw E(
"taking successor of last time step action history");
2756 Index ahI_wo = ahI - _m_firstAHIforT.at(agI).at(t);
2760 Index ahIsuc_wo = ahI_wo * GetNrActions(agI) + oI;
2761 return(ahIsuc_wo + _m_firstAHIforT.at(agI).at(t+1));
2767 if(_m_params.GetComputeIndividualActionObservationHistories())
2768 return(GetActionObservationHistoryTree(agI, aohI)->
2769 GetContainedElement()->GetLength() );
2773 while( t < GetHorizon() && _m_firstAOHIforT.at(agI).at(t) <= aohI )
2776 if(t == GetHorizon())
2787 if(_m_params.GetComputeJointActionObservationHistories())
2788 return(GetJointActionObservationHistoryTree(jaohI)->
2789 GetContainedElement()->GetLength() );
2793 while( t < GetHorizon() && _m_firstJAOHIforT[t] <= jaohI )
2796 if(t == GetHorizon())
2809 if(_m_params.GetComputeIndividualActionObservationHistories())
2810 return(GetActionObservationHistoryTree(agI, aohI)->
2811 GetSuccessor(aI,oI)->GetIndex());
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));
2828 if(_m_params.GetComputeJointActionObservationHistories())
2829 jaohIsuc=GetJointActionObservationHistoryTree(jaohI)->
2830 GetSuccessor(jaI,joI)->GetIndex();
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];
2843 throw(
EOverflow(
"PlanningUnitMADPDiscrete::GetSuccessorJAOHI index overflow detected"));
2853 _m_jointActionObservationHistoryTreeMap[i]=jaoht;
2869 if(_m_params.GetComputeJointActionObservationHistories())
2870 return(_m_jointActionObservationHistoryTreeVector.at(jaohI));
2876 map<Index,JointActionObservationHistoryTree*>::const_iterator cit
2877 = _m_jointActionObservationHistoryTreeMap.find(jaohI);
2878 if(cit!=_m_jointActionObservationHistoryTreeMap.end())
2879 return(cit->second);
2881 throw(
E(
"GetJointActionObservationHistoryTree:: index not found in map"));
2891 return _m_params.GetComputeJointObservationHistories();
2894 return _m_params.GetComputeJointActionObservationHistories();
2897 throw(
E(
"PlanningUnitMADPDiscrete::AreCachedJointToIndivIndices: types are not defined in a PlanningUnitMADPDiscrete context"));
2901 throw(
E(
"Used a JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory unknown to PlanningUnitMADPDiscrete!"));
2913 string s = SoftPrintObservationHistory(agentI, dI);
2920 return GetActionObservationHistoryTree(agentI,dI)->
2921 GetActionObservationHistory()->SoftPrint();
2924 throw(
E(
"SoftPrintPolicyDomain: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
2927 throw(
E(
"SoftPrintPolicyDomain: incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
2930 return(
"IMPOSSIBLE!");
2935 size_t depth )
const
2941 return GetNrObservationHistories(agentI);
2943 return(_m_firstOHIforT.at(agentI).at(depth));
2947 return GetNrActionObservationHistories(agentI);
2949 return(_m_firstAOHIforT.at(agentI).at(depth));
2952 throw(
E(
" GetNrPolicyDomainElements: types are not a valid policy domain for a PlanningUnitMADPDiscrete"));
2955 throw(
E(
"incorrect JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory"));
2968 if( _m_params.GetUseSparseJointBeliefs() )
2977 if( _m_params.GetUseSparseJointBeliefs() )
2984 Index ohIndex)
const
2988 if(_m_params.GetComputeIndividualObservationHistories())
2989 s = GetObservationHistoryTree(agentI,ohIndex)->
2990 GetObservationHistory()->SoftPrint();
2993 Index t = GetTimeStepForOHI(agentI, ohIndex);
2995 GetObservationHistoryArrays(agentI, ohIndex, t, obs);
2998 for(
Index t2=0; t2<t; t2++)
3001 string oName = GetObservation(agentI, oI)->GetName();
3014 return(GetAction(agentI, actionI)->GetName());