MultiAgentDecisionProcess  Release 0.2.1
TransitionObservationIndependentMADPDiscrete.cpp
Go to the documentation of this file.
1 
30 #include "TransitionModelMapping.h"
33 #include "TGet.h"
34 #include "OGet.h"
35 
36 #include "VectorTools.h"
37 
38 using namespace std;
39 
40 #define DEBUG_TOIMADPD 0
41 #define DEBUG_CJA 0
42 #define DEBUG_CJO 0
43 
46  string name, string descr, string pf) :
47  MultiAgentDecisionProcess(name, descr, pf)
48 {
49  _m_initialized = false;
50  _m_sparse = false;
51  _m_noJointModels = false;
55  _m_p_tModel = 0;
56  _m_p_oModel = 0;
57  // these are pointers to get around the const-ness of their Get functions
58  _m_jointStatesMap=new map<vector<Index>, State*>;
59  _m_indivStateIndicesMap = new map< Index, vector<Index> >;
60  _m_jointActionMap=new map<Index, JointActionDiscrete*>;
61  _m_jointObsMap=new map<Index, JointObservationDiscrete*>;
63 }
64 
68 {
69  throw(E("TransitionObservationIndependentMADPDiscrete: copy ctor not implemented"));
70 }
71 
74 {
75  // this destructor leaks memory
76 
77  vector<MultiAgentDecisionProcessDiscrete*>::iterator it =
78  _m_individualMADPDs.begin();
79  vector<MultiAgentDecisionProcessDiscrete*>::iterator last =
80  _m_individualMADPDs.end();
81  while(it != last)
82  {
83  delete *it;//pointer to MultiAgentDecisionProcessDiscrete
84  it++;
85  }
86  _m_individualMADPDs.clear();
87 
88  vector<JointActionDiscrete*>::iterator it2 =
89  _m_jointActionVec.begin();
90  vector<JointActionDiscrete*>::iterator last2 =
91  _m_jointActionVec.end();
92  while(it2 != last2)
93  {
94  delete *it2; //pointer to MultiAgentDecisionProcessDiscrete
95  it2++;
96  }
97  _m_jointActionVec.clear();
98 }
99 
100 
102 {
104  vector<MultiAgentDecisionProcessDiscrete*>::iterator it =
105  _m_individualMADPDs.begin();
106  vector<MultiAgentDecisionProcessDiscrete*>::iterator last =
107  _m_individualMADPDs.end();
108  while(it != last)
109  {
110  delete (*it);
111  it++;
112  }
113 
114  _m_individualMADPDs.clear();
115  for(Index i=0; i < n; i++)
116  {
117  stringstream name, descr, pf;
118  name << "Indiv.MADPDiscr.Agent"<<i;
119  descr << "The individual MADPDiscrete used to model the transition \
120 and observation dynamics for agent "<<i;
121  pf << GetProblemFile() << ".agent" << i;
122 
124  MultiAgentDecisionProcessDiscrete(1, 0, name.str(), descr.str(),
125  pf.str() );
126  m->SetSparse(_m_sparse);
127  _m_individualMADPDs.push_back( m );
128  }
129 
130  SetInitialized(false);
131 }
132 
134 {
135  Index i = GetNrAgents();//the old number of agents
137 
138  stringstream name, descr, pf;
139  name << "Indiv.MADPDiscr.Agent"<<i;
140  descr << "The individual MADPDiscrete used to model the transition \
141 and observation dynamics for agent "<<namestring <<"(agent "<<i<<")";
142  pf << GetProblemFile() << ".agent" << i;
143 
145  MultiAgentDecisionProcessDiscrete(1, 0, name.str(), descr.str(),
146  pf.str() );
147  m->SetSparse(_m_sparse);
148  _m_individualMADPDs.push_back( m );
149 
150  SetInitialized(false);
151 }
152 
154 {
155  if(agentI >= _m_individualMADPDs.size())
156  throw EInvalidIndex("TransitionObservationIndependentMADPDiscrete::SetNrStates - agentI out of bounds...");
157  _m_individualMADPDs[agentI]->SetNrStates(nr);
158 }
160 {
161  if(agentI >= _m_individualMADPDs.size())
162  throw EInvalidIndex("TransitionObservationIndependentMADPDiscrete::AddState - agentI out of bounds...");
163  _m_individualMADPDs[agentI]->AddState(name);
164 }
165 
167 {
168  if(agentI >= _m_individualMADPDs.size())
169  throw EInvalidIndex("TransitionObservationIndependentMADPDiscrete::SetNrActions - agentI out of bounds...");
170  _m_individualMADPDs[agentI]->SetNrActions(0, nr);
171 }
172 
174 {
175  if(agentI >= _m_individualMADPDs.size())
176  throw EInvalidIndex("TransitionObservationIndependentMADPDiscrete::AddAction - agentI out of bounds...");
177  _m_individualMADPDs[agentI]->AddAction(0, name);
178 }
179 
181 {
182  if(agentI >= _m_individualMADPDs.size())
183  throw EInvalidIndex("TransitionObservationIndependentMADPDiscrete::SetNrObservations - agentI out of bounds...");
184  _m_individualMADPDs[agentI]->SetNrObservations(0, nr);
185 }
186 
188 {
189  if(agentI >= _m_individualMADPDs.size())
190  throw EInvalidIndex("TransitionObservationIndependentMADPDiscrete::AddObservation - agentI out of bounds...");
191  _m_individualMADPDs[agentI]->AddObservation(0, name);
192 }
193 
194 #if 0 // will be computed from individual ISDs
195 void TransitionObservationIndependentMADPDiscrete::SetUniformISD()
196 {
197  size_t nrJS = GetNrJointStates();
198  if(_m_initialStateDistribution->size() != nrJS)
199  _m_initialStateDistribution->resize(nrJS);
200  double uprob = 1.0 / nrJS;
201  vector<double>::iterator it = _m_initialStateDistribution->begin();
202  vector<double>::iterator last = _m_initialStateDistribution->end();
203  while(it!=last)
204  {
205  *it = uprob;
206  it++;
207  }
208 }
209 #endif
210 
212 {
213  if(_m_nrJointStates==0)
214  throw(E("TransitionObservationIndependentMADPDiscrete::CreateISD() joint states should have been created already"));
215 
216  if(v.size() != _m_nrJointStates)
217  throw E("TransitionObservationIndependentMADPDiscrete::SetISD - ERROR: nrStates don't match!");
218 
221 }
222 
224 {
225  _m_jointActionVec.clear();
226  if(_m_noJointModels)
227  {
229  for(Index agI = 0; agI < GetNrAgents(); agI++)
231  }
232  else
233  {
236 
238  throw(E("TransitionObservationIndependentMADPDiscrete::CreateJointActions() sizes do not match"));
239  }
240 
241  _m_nrIndivActions.clear();
242  for(Index agI = 0; agI < GetNrAgents(); agI++)
244 
245  for(Index ja=0;ja!=_m_nrJointActions;++ja)
246  _m_jointToIndActionCache.push_back(
248 }
249 
252  Index jaI)
253 {
254 if(DEBUG_CJA) cerr << "TransitionObservationIndependentMADPDiscrete::CreateJointActions(Index "<<curAgentI<<", JointActionDiscrete& ja, Index "<< jaI<<") called"<<endl;
255 
256  bool lastAgent=false;
257  if(curAgentI == _m_nrAgents-1)
258  {
259  lastAgent = true;
260  if(DEBUG_CJA) cerr << "\nlast agent\n";
261  }
262 
263  //we are going to walk through this agents action vector
264  size_t nrA = GetNrActions(curAgentI);
265 
266  //first action extends the received ja
267  JointActionDiscrete* p_jaReceivedArgCopy = new JointActionDiscrete(ja);
268  JointActionDiscrete* p_ja;
269 
270  for(Index a=0; a < nrA; a++)
271  {
272  const ActionDiscrete * adp = GetIndividualMADPD(curAgentI)->
273  GetActionDiscrete(0, a);
274  if(DEBUG_CJA) cerr << "\nnext action";
275 // if(it == first) //
276  if(a == 0)
277  {
278  if(DEBUG_CJA) cerr << "(first action - not making copy)\n";
279  p_ja = &ja;
280  }
281  else if ( a == nrA-1 )//it == beforelast)
282  //this is the last last action
283  {
284  if(DEBUG_CJA) cerr << "(last action - not making copy)\n";
285  p_ja = p_jaReceivedArgCopy; //don't make a new copy
286  }
287  else //make a new copy
288  {
289  if(DEBUG_CJA) cerr << "(intermed. action - making copy)\n";
290  p_ja = new JointActionDiscrete(*p_jaReceivedArgCopy);
291  }
292  if(lastAgent)
293  {
294  p_ja->SetIndex(jaI);
295  if(DEBUG_CJA)cerr << "setting index of this joint action to: "<<
296  jaI <<endl;
297  }
298 // ActionDiscrete* adp = /*(ActionDiscrete*)*/ &(*it);
299  if(DEBUG_CJA)
300  cerr <<"Adding agent's indiv. action to joint action..."<<endl;
301  p_ja->AddIndividualAction(adp, curAgentI);
302  if(lastAgent) //jointAction is now completed: add it to jointAction set.
303  {
304  if(DEBUG_CJA){cerr << "INSERTING the joint action:";
305  p_ja->Print();cerr<<endl;}
306  _m_jointActionVec.push_back(p_ja);
307  if(DEBUG_CJA){cerr << "\nINSERTED the joint action."<<endl;
308  cerr << "_m_jointActionVec now containts "<<
309  _m_jointActionVec.size() << " joint actions." << endl;}
310  jaI++;
311  }
312  else
313  jaI = CreateJointActionsRecursively(curAgentI+1,*p_ja, jaI);
314 
315 // it++;
316  }
317  if(DEBUG_CJA) cerr << ">>ProblemDecTiger::CreateJointActionsRecursively(Index "<<curAgentI<<", JointActionDiscrete& ja, Index "<< jaI<<") FINISHED"<<endl;
318  return jaI;
319 }
320 
321 
323 {
324  _m_jointStates.clear();
325  size_t nrAg = GetNrAgents();
326  _m_nrIndivStates.clear();
328  for(Index agI = 0; agI < nrAg; agI++)
329  {
330  size_t nrS = GetIndividualMADPD(agI)->GetNrStates();
331  _m_nrIndivStates.push_back(nrS);
332  _m_nrJointStates*=nrS;
333  }
334  if(!_m_noJointModels)
335  {
336  vector<Index> ind_sI(nrAg, 0);
337  size_t i=0;
338  do
339  {
340  _m_indivStateIndices.push_back(ind_sI);
341  State *state=new StateDiscrete(i);
342  string name="";
343  for(Index agI = 0; agI < nrAg; agI++)
344  {
345  if(agI>0)
346  name+="_";
347  name+=GetIndividualMADPD(agI)->GetState(ind_sI[agI])->GetName();
348  }
349  state->SetName(name);
350  state->SetDescription("");
351  _m_jointStates.push_back(state);
352  i++;
353  } while(! IndexTools::Increment(ind_sI, _m_nrIndivStates) );
354 
355  if(_m_nrJointStates!=_m_jointStates.size())
356  throw(E("TransitionObservationIndependentMADPDiscrete::CreateJointStates() sizes do not match"));
357  }
358 }
359 
360 const State*
362 {
363  if(!_m_noJointModels) // we cached all joint states
364  return(_m_jointStates.at(i));
365  else
367 }
368 
370  const std::vector<Index> &sIs) const
371 {
372  // we cached the ones already asked for
373  if(_m_jointStatesMap->find(sIs)!=_m_jointStatesMap->end())
374  return(_m_jointStatesMap->find(sIs)->second);
375  else // create new joint state and add it to cache
376  {
377  State *state=new State; // not a StateDiscrete, since the
378  // index might overflow
379  string name="";
380  for(Index agI = 0; agI < GetNrAgents(); agI++)
381  {
382  if(agI>0)
383  name+="_";
384  name+=GetIndividualMADPD(agI)->GetState(sIs[agI])->GetName();
385  }
386  state->SetName(name);
387  state->SetDescription("");
388  _m_jointStatesMap->insert(make_pair(sIs,state));
389  return(state);
390  }
391 
392 }
393 
394 const JointActionDiscrete*
396 {
397  if(!_m_noJointModels) // we cached all joint actions
398  return(_m_jointActionVec.at(i));
399  // we cached the ones already asked for
400  else if(_m_jointActionMap->find(i)!=_m_jointActionMap->end())
401  return(_m_jointActionMap->find(i)->second);
402  else // create new joint action and add it to cache
403  {
405  vector<Index> ind_sI=
407  for(Index agI = 0; agI < GetNrAgents(); agI++)
408  action->AddIndividualAction(
409  GetIndividualMADPD(agI)->GetActionDiscrete(0,ind_sI[agI]),
410  agI);
411 
412  _m_jointActionMap->insert(make_pair(i,action));
413  return(action);
414  }
415 }
416 
417 const JointObservation*
419 {
420  if(!_m_noJointModels) // we cached all joint obs
421  return(_m_jointObs.at(i));
422  // we cached the ones already asked for
423  else if(_m_jointObsMap->find(i)!=_m_jointObsMap->end())
424  return(_m_jointObsMap->find(i)->second);
425  else // create new joint obs and add it to cache
426  {
428  vector<Index> ind_sI=
430  for(Index agI = 0; agI < GetNrAgents(); agI++)
431  observation->AddIndividualObservation(
432  GetIndividualMADPD(agI)->GetObservationDiscrete(0,ind_sI[agI]),
433  agI);
434 
435  _m_jointObsMap->insert(make_pair(i,observation));
436  return(observation);
437  }
438 }
439 
440 const std::vector<Index>&
443 {
444  if(!_m_noJointModels)
445  return _m_indivStateIndices[jointSI];
446  else if (_m_indivStateIndicesMap->find(jointSI)!=
448  return(_m_indivStateIndicesMap->find(jointSI)->second);
449  else
450  {
451  vector<Index> ind_sI=
453  _m_indivStateIndicesMap->insert(make_pair(jointSI,ind_sI));
454  return(_m_indivStateIndicesMap->find(jointSI)->second);
455  }
456 }
457 
459 {
460  _m_jointObs.clear();
461  size_t nrAg = GetNrAgents();
462  _m_nrIndivObs.clear();
463  for(Index agI = 0; agI < nrAg; agI++)
464  {
465  size_t nrO = GetIndividualMADPD(agI)->GetNrObservations(0);
466  _m_nrIndivObs.push_back(nrO);
467  }
468 
469  for(Index agI = 0; agI < nrAg; agI++)
470  {
471  vector<ObservationDiscrete> indObs;
472  for(Index o=0; o!=GetIndividualMADPD(agI)->GetNrObservations(0); ++o)
473  indObs.push_back(*GetIndividualMADPD(agI)->
474  GetObservationDiscrete(0,o));
475  _m_indivObs.push_back(indObs);
476  }
477 
478  if(_m_noJointModels)
479  {
481  for(Index agI = 0; agI < nrAg; agI++)
484  }
485  else
486  {
490  throw(E("TransitionObservationIndependentMADPDiscrete::CreateJointObservations() sizes do not match"));
491 
492  for(Index jo=0;jo!=_m_nrJointObservations;++jo)
493  _m_jointToIndObsCache.push_back(
495  }
496 }
497 
500  Index curAgentI, JointObservationDiscrete& jo, Index joI)
501 {
502  bool lastAgent=false;
503  if(curAgentI == GetNrAgents()-1)
504  {
505  lastAgent = true;
506  }
507  if(curAgentI >= _m_indivObs.size())
508  {
509  stringstream ss;
510  ss << "ConstructJointObservationsRecursively - current Agent index ("<<
511  curAgentI<<") out of bounds! (_m_indivObs contains "<<
512  "observations for "<<_m_indivObs.size() << " agents...)\n";
513  throw E(ss.str().c_str());
514  }
515  ObservationDVec::iterator first = _m_indivObs[curAgentI].begin();
516  ObservationDVec::iterator it = _m_indivObs[curAgentI].begin();
517  ObservationDVec::iterator last = _m_indivObs[curAgentI].end();
518  ObservationDVec::iterator beforelast = _m_indivObs[curAgentI].end();
519  beforelast--;
520 
521  if(it == last)
522  {
523  stringstream ss;
524  ss << " empty observation set for agent " << curAgentI;
525  throw E(ss);
526  }
527  //first observation extends the received jo
528  JointObservationDiscrete* p_joReceivedArgCopy =
529  new JointObservationDiscrete(jo);
531 
532  while( it != last) // other observations extend duplicates of jo
533  {
534  if(it == first) //
535  {
536  p_jo = &jo;
537  }
538  else if (it == beforelast)//this is the last valid it -> last observation
539  {
540  p_jo = p_joReceivedArgCopy; //don't make a new copy
541  }
542  else //make a new copy
543  {
544  p_jo = new JointObservationDiscrete(*p_joReceivedArgCopy);
545  }
546  if(lastAgent)
547  {
548  p_jo->SetIndex(joI);
549  if(DEBUG_CJO)
550  cerr << "setting index of this observation to: "<< joI <<endl;
551  }
552  ObservationDiscrete* ai = /*(ObservationDiscrete*)*/ &(*it);
553  if(DEBUG_CJO)
554  cerr << "Adding agent's indiv. observation to joint observation..."<<endl;
555  p_jo->AddIndividualObservation(ai, curAgentI);
556 
557  if(lastAgent)//jointObservation is now completed:add it to the jointObservation set.
558  {
559  if(DEBUG_CJO)
560  {cerr<<"INSERTING the joint observation:"; p_jo->Print();cerr<<endl;}
561  _m_jointObs.push_back(p_jo);
562  if(DEBUG_CJO){cerr << "\nINSERTED the joint observation"<<endl<< "_m_jointObservationVec now containts "<< _m_jointObs.size() << " joint actions." << endl;}
563  joI++;
564  }
565  else
566  joI = ConstructJointObservationsRecursively(curAgentI+1,*p_jo, joI);
567  it++;
568  }
569  if(DEBUG_CJO) cerr << ">> TransitionObservationIndependentMADPDiscrete::ConstructJointObservationsRecursively(Index "<<curAgentI<<", JointObservationDiscrete& jo, Index "<< joI<<") FINISHED"<<endl;
570  return joI;
571 
572 }
573 
575 {
576  size_t nrJS = _m_nrJointStates;
577  if(nrJS==0)
578  throw(E("TransitionObservationIndependentMADPDiscrete::CreateISD() joint states should have been created already"));
579 
580  //vector<vector<double> > isdIs;
581  //vector< const StateDistributionVector* > isdIs;
582  vector< const StateDistribution* > isdIs;
583 
584  for(Index i=0;i!=GetNrAgents();++i)
585  isdIs.push_back(GetIndividualMADPD(i)->GetISD());
586 
587  if(!_m_noJointModels)
588  {
589  vector<double> ISD(nrJS,1);
590  for(Index s=0;s!=nrJS;++s)
591  for(Index i=0;i!=GetNrAgents();++i)
592  ISD[s]*= isdIs[i]->GetProbability(_m_indivStateIndices[s][i] );
593 
594  SetISD(ISD);
595  }
596 }
597 
599 {
600  if(b == false)
601  {
602  if(_m_initialized == true)
603  delete [] _m_actionStepSize;
604  _m_initialized = false;
605  return(true);
606  }
607  if(_m_initialized == true && b == true)
608  {
609  //first free mem before re-initialize:
610  delete [] _m_actionStepSize;
611  }
612 
613  if(DEBUG_TOIMADPD)
614  cerr << "TransitionObservationIndependentMADPDiscrete::SetInitialized"<<
615  " called - GetNrAgents()="<< GetNrAgents()<<endl;
616 
617  if(GetNrAgents() < 1 )
618  throw E("TransitionObservationIndependentMADPDiscrete::SetInitialized(bool b) called, but the number of agents has not been set yet.. (is 0 at the moment...)");
619 
620  if(b == true)
621  {
622  //first initialize the individual models...
623  vector<MultiAgentDecisionProcessDiscrete*>::iterator it =
624  _m_individualMADPDs.begin();
625  vector<MultiAgentDecisionProcessDiscrete*>::iterator last =
626  _m_individualMADPDs.end();
627  while(it != last)
628  {
629  (*it)->Initialize();
630  it++;
631  }
632 
633  // most likely we will not have the memory space to store all this...
634  if(GetNrAgents()>2)
635  _m_noJointModels=true;
636 
637  //now perform the necessary actions for this class...
641 
642  CreateISD();
643 
646 
647  _m_initialized = b;
648  }
649  return(true);
650 }
651 
654 {
657  return(a);
658 }
659 
661 IndividualToJointActionIndices(const vector<Index>& indivActionIndices) const
662 {
663  if(!_m_initialized)
664  {
665  stringstream ss;
666  ss << "TransitionObservationIndependentMADPDiscrete::"<<
667  "IndividualToJointActionIndices(vector<Index>&)"<<
668  " - Error: not initialized. "<<endl;
669  throw E(ss);
670  }
671 
672  Index i = IndexTools::IndividualToJointIndices(indivActionIndices,
674  return(i);
675 }
676 
679 {
680 #if 0
681  if(!_m_initialized)
682  {
683  stringstream ss;
684  ss << "TransitionObservationIndependentMADPDiscrete::"<<
685  "JointToIndividualActionIndices("<< jaI<<
686  ") - Error: not initialized. "<<endl;
687  throw E(ss);
688  }
689 #endif
690 #if 0
692  vector<Index> vai = jai->GetIndividualActionDiscretesIndices();
693 #endif
694  vector<Index> vai=IndexTools::JointToIndividualIndices(jaI,
696 
697  if(DEBUG_TOIMADPD)
698  {
699  cerr << "TransitionObservationIndependentMADPDiscrete::"<<
700  "JointToIndividualActionIndices"<< "(Index "<<jaI<<")" << endl <<
701  "vai.size() = " <<vai.size() << " - elements: "<<endl;
702  vector<Index>::iterator vai_it = vai.begin();
703  vector<Index>::iterator vai_last = vai.end();
704  while(vai_it != vai_last)
705  {
706  cerr << ", " << *vai_it;
707  vai_it++;
708  }
709  cerr << endl;
710  }
711  return(vai);
712 }
713 
716  indivObsIndices) const
717 {
718  if(!_m_initialized)
719  {
720  stringstream ss;
721  ss << "TransitionObservationIndependentMADPDiscrete::"<<
722  "IndividualToJointObservationIndices(const vector<Index>)"<<
723  " - Error: not initialized. "<<endl;
724  throw E(ss);
725  }
726 
727  vector<size_t> nrO;
728  for(Index agI=0; agI < GetNrAgents(); agI++)
729  nrO.push_back(GetNrObservations(agI));
730 
731  Index i = IndexTools::IndividualToJointIndices(indivObsIndices,nrO);
732 
733  return(i);
734 }
735 
738 {
739 #if 0
740  if(!_m_initialized)
741  {
742  stringstream ss;
743  ss << "TransitionObservationIndependentMADPDiscrete::"<<
744  "JointToIndividualObservationIndices("<< joI<<
745  ") - Error: not initialized. "<<endl;
746  throw E(ss);
747  }
748 #endif
749  vector<size_t> nrO;
750  for(Index agI=0; agI < GetNrAgents(); agI++)
751  nrO.push_back(GetNrObservations(agI));
752  vector<Index> voi =
754  if(DEBUG_TOIMADPD)
755  {
756  cerr << "TransitionObservationIndependentMADPDiscrete::"<<
757  "JointToIndividualActionIndices"<< "(Index "<<joI<<")" << endl <<
758  "voi.size() = " <<voi.size() << " - elements: "<<endl;
759  vector<Index>::iterator voi_it = voi.begin();
760  vector<Index>::iterator voi_last = voi.end();
761  while(voi_it != voi_last)
762  {
763  cerr << ", " << *voi_it;
764  voi_it++;
765  }
766  cerr << endl;
767  }
768  return(voi);
769 }
770 
772 IndividualToJointStateIndices(const vector<Index>& indivStateIndices) const
773 {
774  return(IndexTools::IndividualToJointIndices(indivStateIndices,
776 }
777 
779 {
780  stringstream str;
782  if(!_m_initialized)
783  {
784  stringstream ss;
785  ss << "TransitionObservationIndependentMADPDiscrete::Print("<<
786  ") - Error: not initialized. "<<endl;
787  throw E(ss);
788  }
789  str << "#joint states=" << GetNrJointStates() << endl;
790  str << "#joint actions="<<GetNrJointActions()<<endl;
791  str << "#joint observations="<<GetNrJointObservations()<<endl;
792  str << SoftPrintActionSets();
793  str << "Joint Actions:"<<endl;
794  str << SoftPrintJointActionSet();
795  //print the individual MADPs
796  str << "Individual MADPs for each agent:"<<endl;
797  for(Index agI=0; agI < GetNrAgents(); agI++)
798  str << "Agent "<< agI << ":" <<endl <<
800 
801  return(str.str());
802 }
803 
805 {
806  stringstream str;
807  if(!_m_initialized)
808  {
809  stringstream ss;
810  ss << "TransitionObservationIndependentMADPDiscrete::PrintAction"<<
811  "Sets() - Error: not initialized. "<<endl;
812  throw E(ss);
813  }
814  str << "Actions:"<<endl;
815  for(Index agentIndex=0; agentIndex < _m_nrAgents; agentIndex++)
816  {
817  str << "agentI " << agentIndex << " - nrActions " <<
818  GetNrActions(agentIndex)<<endl;
819  for(Index actionI=0; actionI < GetNrActions(agentIndex); actionI++)
820  {
821 
822  const ActionDiscrete * adp = GetIndividualMADPD(agentIndex)->
823  GetActionDiscrete(0, actionI);
824  str << adp->SoftPrint();
825  str << endl;
826  }
827  }
828  return(str.str());
829 }
830 
832  const
833 {
834  stringstream str;
835  if(!_m_initialized)
836  {
837  stringstream ss;
838  ss << "TransitionObservationIndependentMADPDiscrete::PrintJoint"<<
839  "ActionSets() - Error: not initialized. "<<endl;
840  throw E(ss);
841  }
842  vector<JointActionDiscrete*>::const_iterator ja_it =
843  _m_jointActionVec.begin();
844  vector<JointActionDiscrete*>::const_iterator ja_last =
845  _m_jointActionVec.end();
846  while(ja_it != ja_last)
847  {
848  str << (*ja_it)->SoftPrint();
849  str<<endl;
850  ja_it++;
851  }
852  return(str.str());
853 }
854 
855 string
857 {
858  vector<Index> sIs=JointToIndividualStateIndices(sI);
859  stringstream ss;
860  for(Index agI = 0; agI < sIs.size(); agI++)
861  ss << GetIndividualMADPD(agI)->SoftPrintState(sIs[agI]);
862  return(ss.str());
863 }
864 
865 
868  Index jaI,
869  Index sucSI) const
870 {
871  double p=1;
872  if(_m_noJointModels)
873  {
874  vector<Index> sIs=JointToIndividualStateIndices(sI),
875  sucSIs=JointToIndividualStateIndices(sucSI);
876  vector<Index> aIs=JointToIndividualActionIndices(jaI);
877  for(Index agI = 0; agI < GetNrAgents(); agI++)
878  {
880  sIs[agI],
881  aIs[agI],
882  sucSIs[agI]);
883  if(p==0)
884  break;
885  }
886  }
887  else
888  {
889  for(Index agI = 0; agI < GetNrAgents(); agI++)
890  {
892  _m_indivStateIndices[sI][agI],
893  _m_jointToIndActionCache[jaI][agI],
894  _m_indivStateIndices[sucSI][agI]);
895  if(p==0)
896  break;
897  }
898  }
899  return(p);
900 }
901 
903 GetTransitionProbability(const vector<Index> &sIs,
904  const vector<Index> &aIs,
905  const vector<Index> &sucSIs) const
906 {
907  double p=1;
908  for(Index agI = 0; agI < GetNrAgents(); agI++)
909  {
911  sIs[agI],
912  aIs[agI],
913  sucSIs[agI]);
914  if(p==0)
915  break;
916  }
917  return(p);
918 }
919 
922  Index sucSI,
923  Index joI) const
924 {
925  double p=1;
926  if(_m_noJointModels)
927  {
928  vector<Index> sucSIs=JointToIndividualStateIndices(sucSI);
929  vector<Index> aIs=JointToIndividualActionIndices(jaI);
930  vector<Index> oIs=JointToIndividualObservationIndices(joI);
931  for(Index agI = 0; agI < GetNrAgents(); agI++)
932  {
934  aIs[agI],
935  sucSIs[agI],
936  oIs[agI]);
937  if(p==0)
938  break;
939  }
940  }
941  else
942  {
943  for(Index agI = 0; agI < GetNrAgents(); agI++)
944  {
946  _m_jointToIndActionCache[jaI][agI],
947  _m_indivStateIndices[sucSI][agI],
948  _m_jointToIndObsCache[joI][agI]);
949  if(p==0)
950  break;
951  }
952  }
953  return(p);
954 }
955 
957 GetObservationProbability(const vector<Index> &aIs,
958  const vector<Index> &sucSIs,
959  const vector<Index> &oIs) const
960 {
961  double p=1;
962  for(Index agI = 0; agI < GetNrAgents(); agI++)
963  {
965  aIs[agI],
966  sucSIs[agI],
967  oIs[agI]);
968  if(p==0)
969  break;
970  }
971  return(p);
972 }
973 
976 {
977  vector<Index> sIs=JointToIndividualStateIndices(sI);
978  vector<Index> aIs=JointToIndividualActionIndices(jaI);
980 }
981 
982 vector<Index>
984  const vector<Index> &sIs,
985  const vector<Index> &aIs) const
986 {
987  vector<Index> sucSIs(GetNrAgents());
988  for(Index agI = 0; agI < GetNrAgents(); agI++)
989  sucSIs[agI]=GetIndividualMADPD(agI)->SampleSuccessorState(sIs[agI],
990  aIs[agI]);
991  return(sucSIs);
992 }
993 
996  Index sucI) const
997 {
998  vector<Index> sucIs=JointToIndividualStateIndices(sucI);
999  vector<Index> aIs=JointToIndividualActionIndices(jaI);
1000 
1002  sucIs)));
1003 }
1004 
1005 vector<Index>
1007  const vector<Index> &aIs,
1008  const vector<Index> &sucIs)
1009  const
1010 {
1011  vector<Index> oIs(GetNrAgents());
1012 
1013  for(Index agI = 0; agI < GetNrAgents(); agI++)
1014  oIs[agI]=GetIndividualMADPD(agI)->SampleJointObservation(aIs[agI],
1015  sucIs[agI]);
1016  return(oIs);
1017 }
1018 
1019 
1022 {
1024 }
1025 
1026 vector<Index>
1028 {
1029  vector<Index> sIs(GetNrAgents());
1030 
1031  for(Index agI = 0; agI < GetNrAgents(); agI++)
1032  sIs[agI]=GetIndividualMADPD(agI)->SampleInitialState();
1033 
1034  return(sIs);
1035 }
1036 
1037 //vector<double> TransitionObservationIndependentMADPDiscrete::GetISD() const
1039 {
1040  if(_m_noJointModels)
1041  {
1042  throw(E("TransitionObservationIndependentMADPDiscrete::GetISD initial state distribution has not been generated as a double vector."));
1043  }
1044  else
1046 }
1047 
1049 {
1050  _m_sparse=sparse;
1051 
1052  vector<MultiAgentDecisionProcessDiscrete*>::iterator it =
1053  _m_individualMADPDs.begin();
1054  vector<MultiAgentDecisionProcessDiscrete*>::iterator last =
1055  _m_individualMADPDs.end();
1056  while(it != last)
1057  {
1058  (*it)->SetSparse(sparse);
1059  it++;
1060  }
1061 }
1062 
1064 {
1065  if(!_m_initialized)
1066  throw(E("TransitionObservationIndependentMADPDiscrete::GetNrJointStates() not yet initialized"));
1067 
1068  return(_m_nrJointStates);
1069 }
1070 
1072 {
1073  if(!_m_initialized)
1074  throw(E("TransitionObservationIndependentMADPDiscrete::GetNrJointActions() not yet initialized"));
1075 
1076  return(_m_nrJointActions);
1077 }
1078 
1080 {
1081  if(!_m_initialized)
1082  throw(E("TransitionObservationIndependentMADPDiscrete::GetNrJointObservations() not yet initialized"));
1083 
1084  return(_m_nrJointObservations);
1085 }
1086 
1087 void
1089 {
1090  if(!_m_initialized)
1091  {
1092  throw(E("TransitionObservationIndependentMADPDiscrete::CreateCentralizedSparseModels not initialized yet"));
1093  return;
1094  }
1095 
1101  double p=0;
1102  for(Index a=0;a!=_m_nrJointActions;++a)
1103  for(Index s=0;s!=_m_nrJointStates;++s)
1104  {
1105  for(Index s1=0;s1!=_m_nrJointStates;++s1)
1106  {
1107  p=GetTransitionProbability(s,a,s1);
1108  if(p>0)
1109  _m_p_tModel->Set(s,a,s1,p);
1110  }
1111 
1112  for(Index o=0;o!=_m_nrJointObservations;++o)
1113  {
1114  p=GetObservationProbability(a,s,o);
1115  if(p>0)
1116  _m_p_oModel->Set(a,s,o,p);
1117  }
1118  }
1119 }
1120 
1121 void
1123 {
1124  // todo: remove the overlap with CreateCentralizedSparseModels()
1125  if(!_m_initialized)
1126  {
1127  throw(E("TransitionObservationIndependentMADPDiscrete::CreateCentralizedFullModels not initialized yet"));
1128  return;
1129  }
1130 
1136  double p=0;
1137  for(Index a=0;a!=_m_nrJointActions;++a)
1138  for(Index s=0;s!=_m_nrJointStates;++s)
1139  {
1140  for(Index s1=0;s1!=_m_nrJointStates;++s1)
1141  {
1142  p=GetTransitionProbability(s,a,s1);
1143  if(p>0)
1144  _m_p_tModel->Set(s,a,s1,p);
1145  }
1146 
1147  for(Index o=0;o!=_m_nrJointObservations;++o)
1148  {
1149  p=GetObservationProbability(a,s,o);
1150  if(p>0)
1151  _m_p_oModel->Set(a,s,o,p);
1152  }
1153  }
1154 }
1155 
1157 GetTGet() const
1158 {
1159  if(_m_noJointModels)
1160  return 0;
1161 
1162  if(_m_sparse)
1165  else
1166  return new TGet_TransitionModelMapping(
1168 }
1169 
1171 GetOGet() const
1172 {
1173  if(_m_noJointModels)
1174  return 0;
1175 
1176  if(_m_sparse)
1179  else
1180  return new OGet_ObservationModelMapping(
1182 }