MultiAgentDecisionProcess  Release 0.2.1
MADPComponentDiscreteObservations.cpp
Go to the documentation of this file.
1 
29 #include "IndexTools.h"
30 
31 using namespace std;
32 
33 #define DEBUG_GETJO_COPYVEC 0
34 #define DEBUG_CJO 0
35 #define DEBUG_ADD_DO 0
36 
37 //Default constructor
39 {
40  _m_initialized = false;
41  _m_observationStepSize = 0;
42  _m_cachedAllJointObservations=false;
43  _m_jointObservationIndices=0;
44  _m_jointIndicesValid=true;
45 }
46 
47 //Destructor
49 {
50  _m_nrObservations.clear();
51  vector<vector<ObservationDiscrete> >::iterator it =
52  _m_observationVecs.begin();
53  vector<vector<ObservationDiscrete> >::iterator last =
54  _m_observationVecs.end();
55  while(it != last)
56  {
57  // (*it) isa vector<ObservationDiscrete>
58  (*it).clear();
59  it++;
60  }
61  _m_observationVecs.clear();
62  vector<JointObservationDiscrete*>::iterator it2 =
63  _m_jointObservationVec.begin();
64  vector<JointObservationDiscrete*>::iterator last2 =
65  _m_jointObservationVec.end();
66  while(it2 != last2)
67  {
68  delete *it2; //removes the joint action pointed to...
69  it2++;
70  }
71 
72  _m_jointObservationVec.clear();
73 
74  if(_m_jointObservationIndices)
75  {
76  while(!_m_jointObservationIndices->empty())
77  {
78  delete (*_m_jointObservationIndices->begin()).second;
79  _m_jointObservationIndices->erase( _m_jointObservationIndices->begin() );
80  }
81  delete _m_jointObservationIndices;
82  }
83 #if 0
84  if(_m_jointObservationIndices)
85  {
86  vector<vector<Index>*>::iterator it3 = _m_jointObservationIndices->begin();
87  vector<vector<Index>*>::iterator last3 = _m_jointObservationIndices->end();
88  while(it3 != last3)
89  {
90  delete *it3; //removes the joint observation pointed to...
91  it3++;
92  }
93  }
94 #endif
95  delete[] _m_observationStepSize;
96 }
97 
98 //data manipulation (set) functions:
99 
104 {
105 if(DEBUG_CJO) cerr << "ConstructJointObservations() - called";
107  size_t NRJO = ConstructJointObservationsRecursively(0, *jo, 0);
108  _m_cachedAllJointObservations=true;
109  return NRJO;
110 }
111 
115  Index curAgentI, JointObservationDiscrete& jo, Index joI)
116 {
117 if(DEBUG_CJO) cerr << ">> MADPComponentDiscreteObservations::ConstructJointObservations(Index "<<curAgentI<<", JointObservationDiscrete& jo, Index "<< joI<<") called"<<endl;
118 
119  bool lastAgent=false;
120  if(curAgentI == GetNrAgents()-1)
121  {
122  lastAgent = true;
123  if(DEBUG_CJO) cerr << "\nlast agent\n";
124  }
125  if(curAgentI >= _m_observationVecs.size())
126  {
127  stringstream ss;
128  ss << "ConstructJointObservationsRecursively - current Agent index ("<<
129  curAgentI<<") out of bounds! (_m_observationVecs contains "<<
130  "observations for "<<_m_observationVecs.size() << " agents...)\n";
131  throw E(ss);
132  }
133  ObservationDVec::iterator first = _m_observationVecs[curAgentI].begin();
134  ObservationDVec::iterator it = _m_observationVecs[curAgentI].begin();
135  ObservationDVec::iterator last = _m_observationVecs[curAgentI].end();
136  ObservationDVec::iterator beforelast = _m_observationVecs[curAgentI].end();
137  beforelast--;
138 
139  if(it == last)
140  {
141  stringstream ss;
142  ss << " empty observation set for agent " << curAgentI;
143  throw E(ss);
144  }
145  //first observation extends the received jo
146  JointObservationDiscrete* p_joReceivedArgCopy = new
149 
150  while( it != last) // other observations extend duplicates of jo
151  {
152  if(DEBUG_CJO)
153  cerr << "\nnext observation";
154  if(it == first) //
155  {
156  if(DEBUG_CJO) cerr << "(first observation - not making copy)\n";
157  p_jo = &jo;
158  }
159  else if(it == beforelast)//this is the last valid it ->last observation
160  {
161  if(DEBUG_CJO) cerr << "(last observation - not making copy)\n";
162  p_jo = p_joReceivedArgCopy; //don't make a new copy
163  }
164  else //make a new copy
165  {
166  if(DEBUG_CJO) cerr << "(intermed. observation - making copy)\n";
167  p_jo = new JointObservationDiscrete(*p_joReceivedArgCopy);
168  }
169  if(lastAgent)
170  {
171  p_jo->SetIndex(joI);
172  if(DEBUG_CJO)
173  cerr << "setting index of this joint observation to: "<< joI
174  <<endl;
175  }
176  ObservationDiscrete* ai = /*(ObservationDiscrete*)*/ &(*it);
177  if(DEBUG_CJO)
178  cerr << "Adding agent's indiv. observation to joint observation..."<<endl;
179  p_jo->AddIndividualObservation(ai, curAgentI);
180 
181  if(lastAgent)//jointObservation is now completed:add it to the jointObservation set.
182  {
183  if(DEBUG_CJO)
184  {cerr<<"INSERTING the joint observation:"; p_jo->Print();cerr<<endl;}
185  _m_jointObservationVec.push_back(p_jo);
186  if(DEBUG_CJO){cerr << "\nINSERTED the joint observation"<<endl<< "_m_jointObservationVec now containts "<< _m_jointObservationVec.size() << " joint observations." << endl;}
187  joI++;
188  }
189  else
190  joI = ConstructJointObservationsRecursively(curAgentI+1,*p_jo, joI);
191  it++;
192  }
193  if(DEBUG_CJO) cerr << ">> MADPComponentDiscreteObservations::ConstructJointObservationsRecursively(Index "<<curAgentI<<", JointObservationDiscrete& jo, Index "<< joI<<") FINISHED"<<endl;
194  return joI;
195 
196 }
197 
200  size_t nrO)
201 {
202  if(_m_nrObservations.size() != AI)
203  {
204  stringstream ss;
205  ss << "MADPComponentDiscreteObservations::SetNrObservation("<<AI<<","<<
206  nrO<<
207  ") - error, observations of agents should be specified in order!"<<
208  " (the vector _m_nrObservations should contain entries for all "<<
209  "preceeding agents.)";
210  throw(E(ss));
211  }
212  else
213  {
214  _m_nrObservations.push_back(nrO);
215  //create nameless observatiobs for this agent...
216  vector<ObservationDiscrete> thisAgentsObservations;
217  for(Index i=0;i<nrO;i++)
218  {
219  stringstream ss;
220  ss //<< "ag"<<AI
221  <<"o"<<i;
222  thisAgentsObservations.push_back(ObservationDiscrete(i, ss.str()));
223  }
224  _m_observationVecs.push_back(thisAgentsObservations);
225  }
226 }
227 
229 {
230  if(DEBUG_ADD_DO)
231  cerr<<"MADPComponentDiscreteObservation::AddObservation("<<AI<<","<<name<<")"
232  <<endl;
233 
234  if(_m_nrObservations.size() != AI && _m_nrObservations.size() != AI+1)
235  {
236  stringstream ss;
237  ss << "MADPComponentDiscreteObservations::AddObservation("<<AI<<","<<name<<
238  ") - error, observations of agents should be specified in order!"<<
239  " first all observations of agent 1, then all of agent 2,...etc..."<<
240  " _m_nrObservations.size now is: "<< _m_nrObservations.size() <<
241  "\n(the vector _m_nrObservations should contain entries for all "<<
242  "preceeding agents.)";
243  throw(E(ss));
244  }
245  if(_m_nrObservations.size() == AI )
246  {
247  //this is the first observation we add for this agent
248  _m_nrObservations.push_back(1);
249 
250  vector<ObservationDiscrete> thisAgentsObservations;
251  ObservationDiscrete ad(0, name);
252  thisAgentsObservations.push_back(ad);
253  _m_observationVecs.push_back(thisAgentsObservations);
254  }
255  else
256  {
257  //we add an observation for this agent - increment his nr_observations
258  Index newObservationIndex = _m_nrObservations[AI]++;
259  ObservationDiscrete ad(newObservationIndex, name);
260  _m_observationVecs[AI].push_back(ad);
261  }
262 }
263 
268 {
269  if(b == false)
270  {
271  if(_m_initialized == true)
272  delete [] _m_observationStepSize;
273  _m_initialized = b;
274  return true;
275  }
276  if(_m_initialized == true && b == true)
277  {
278  //first free mem before re-initialize:
279  delete [] _m_observationStepSize;
280  }
281  if(b == true)
282  {
283  if(_m_nrObservations.size() == 0)
284  throw(E("MADPComponentDiscreteObservations::SetInitialized() no observations specified"));
285  if(_m_nrObservations.size() != GetNrAgents())
286  throw E("MADPComponentDiscreteObservations::SetInitialized - (_m_nrObservations.size() != GetNrAgents()");
287  _m_observationStepSize=
288  IndexTools::CalculateStepSize(_m_nrObservations);
289 
290  if(!_m_cachedAllJointObservations)
291  {
292  size_t nrJO=1;
293  size_t prevNrJO=nrJO;
294  for(Index i=0;i!=_m_nrObservations.size();++i)
295  {
296  nrJO*=_m_nrObservations[i];
297  // detect overflow
298  if(nrJO<prevNrJO)
299  _m_jointIndicesValid=false;
300  prevNrJO=nrJO;
301  }
302  _m_nrJointObservations=nrJO;
303  _m_jointObservationIndices=
304  new map<Index, vector<Index> *>();
305  }
306  else
307  _m_nrJointObservations=_m_jointObservationVec.size();
308  _m_initialized = b;
309  }
310  return(true);
311 }
312 
314 {
315  if(!_m_initialized)
316  {
317  stringstream ss;
318  ss << "MADPComponentDiscreteObservations::GetNrObservations("<<
319  agentI<<") - Error: not initialized. "<<endl;
320  throw E(ss);
321  }
322  size_t nrA = GetNrAgents();
323  if(nrA != _m_nrObservations.size())
324  {
325  stringstream ss;
326  ss << "MADPComponentDiscreteObservations::GetNrObservations("<<
327  agentI<<") - Error: nrA != _m_nrObservations.size() "<<endl;
328  throw E(ss);
329  }
330  if(agentI < nrA)
331  return _m_nrObservations[agentI];
332  else
333  {
334  stringstream ss;
335  ss << "Warning: MADPComponentDiscreteObservations::GetNrObservations(Index agentI) - index out of bounds"<<endl;
336  throw E(ss);
337  }
338 }
339 
341 {
342  if(!_m_initialized)
343  {
344  stringstream ss;
345  ss << "MADPComponentDiscreteObservations::GetNrJointObservations("<<
346  ") - Error: not initialized. "<<endl;
347  throw E(ss);
348  }
349  return _m_nrJointObservations;
350 }
351 
354 {
355  if(!_m_initialized)
356  throw E("MADPComponentDiscreteObservations::GetObservationIndexByName - not initialized!");
357  if(agentI >= _m_observationVecs.size())
358  {
359  stringstream ss;
360  ss << "GetObservationIndexByName - Agent index ("<<
361  agentI<<") out of bounds! (_m_observationVecs contains observations"
362  <<" for " << _m_observationVecs.size() << " agents...)\n";
363  throw E(ss);
364  }
365  vector<ObservationDiscrete>::const_iterator it =
366  _m_observationVecs[agentI].begin();
367  vector<ObservationDiscrete>::const_iterator last =
368  _m_observationVecs[agentI].end();
369  while(it != last)
370  {
371  string s2 = (*it).GetName();
372  if(s == s2)
373  return( (*it).GetIndex() );
374  it++;
375  }
376  //not found
377  //return(-1);
378  stringstream ss;
379  ss << "GetObservationIndexByName - observation \"" << s << "\" of agent " <<
380  agentI << " not found." << endl;
381  throw E(ss);
382 }
383 
384 
386 {
387  if(!_m_initialized)
388  {
389  stringstream ss;
390  ss << "MADPComponentDiscreteObservations::GetObservationDiscrete("<<
391  agentI<<","<< a <<") - Error: not initialized. "<<endl;
392  throw E(ss);
393  }
394 
395  if(agentI < GetNrAgents() )
396  {
397  if(a < GetNrObservations(agentI) )
398  {
399  return ( &_m_observationVecs[agentI][a]);
400  }
401  else
402  cerr << "WARNING MADPComponentDiscreteObservations::GetObservationDiscrete("<< agentI<<","<< a <<") - observation (2nd) index out of bounds"<<endl;
403  }
404  else
405  cerr << "WARNING MADPComponentDiscreteObservations::GetObservationDiscrete("<< agentI<<","<< a <<") - agent (1st) index out of bounds"<<endl;
406 
407  return(0);
408 }
409 /*return a ref to the i-th joint observation.*/
412 {
413  if(!_m_initialized)
414  {
415  stringstream ss;
416  ss << "MADPComponentDiscreteObservations::GetJointObservationDiscrete("
417  << i<<") - Error: not initialized. "<<endl;
418  throw E(ss);
419  }
420  if(!_m_cachedAllJointObservations)
421  {
422  throw E("MADPComponentDiscreteObservations::GetJointObservationDiscrete: joint observations have not been created");
423  }
424  if(i < _m_jointObservationVec.size() )
425  {
426  const JointObservationDiscrete* j = _m_jointObservationVec[i];
427  return( j );
428  }
429  //else
430  stringstream ss;
431  ss << "WARNING MADPComponentDiscreteObservations::GetJointObservationDiscrete(Index i) index out of bounds (i="<< i <<")"<<endl;
432  throw E(ss);
433 }
434 /*return a ref to the i-th joint observation.*/
436 {
437  if(!_m_initialized)
438  {
439  stringstream ss;
440  ss << "MADPComponentDiscreteObservations::GetJointObservation("<<
441  i<<") - Error: not initialized. "<<endl;
442  throw E(ss);
443  }
444  if(!_m_cachedAllJointObservations)
445  {
446  throw E("MADPComponentDiscreteObservations::GetJointObservationDiscrete: joint observations have not been created");
447  }
448  if(i < _m_jointObservationVec.size() )
449  return( (const JointObservation*) _m_jointObservationVec[i] );
450  //else
451  stringstream ss;
452  ss << "WARNING MADPComponentDiscreteObservations::GetJointObservation(Index i) index out of bounds (i="<< i <<")"<<endl;
453  throw E(ss);
454 }
456  const vector<Index>& indivObservationIndices)const
457 {
458  if(!_m_initialized)
459  {
460  stringstream ss;
461  ss << "MADPComponentDiscreteObservations::GetJointObservationIndex("<<
462  "vector<Index>& indivObservationIndices) - Error: not initialized."
463  <<endl;
464  throw E(ss);
465  }
467  indivObservationIndices, _m_observationStepSize);
468 
469  return(i);
470 }
471 
473 {
474  stringstream ss;
475  ss << "MADPComponentDiscreteObservations::Print()" << endl;
476  ss << "#joint observations="<<GetNrJointObservations()<<endl;
477  ss << SoftPrintObservationSets();
478  ss << "Joint Observations:"<<endl;
479  ss << SoftPrintJointObservationSet();
480  return(ss.str());
481 }
482 
484 {
485  stringstream ss;
486  if(!_m_initialized)
487  {
488  stringstream ss;
489  ss << "MADPComponentDiscreteObservations::PrintObservationSets("<<
490  ") - Error: not initialized. "<<endl;
491  throw E(ss);
492  }
493  ss << "Observations:"<<endl;
494  for(Index agentIndex=0; agentIndex < GetNrAgents(); agentIndex++)
495  {
496  ss << "agentI " << agentIndex << " - nrObservations " <<
497  GetNrObservations(agentIndex)<<endl;
498  vector<ObservationDiscrete>::const_iterator f =
499  _m_observationVecs[agentIndex].begin();
500  vector<ObservationDiscrete>::const_iterator l =
501  _m_observationVecs[agentIndex].end();
502  while(f != l)
503  {
504  ss << (*f).SoftPrint() << endl;
505 // GetName() << " - " << (*f).GetDescription()<<endl;
506  f++;
507  }
508  }
509  return(ss.str());
510 }
511 
513 {
514  stringstream ss;
515  if(!_m_initialized)
516  {
517  stringstream ss;
518  ss << "MADPComponentDiscreteObservations::PrintJointObservationSet("<<
519  ") - Error: not initialized. "<<endl;
520  throw E(ss);
521  }
522  vector<JointObservationDiscrete*>::const_iterator jo_it =
523  _m_jointObservationVec.begin();
524  vector<JointObservationDiscrete*>::const_iterator jo_last =
525  _m_jointObservationVec.end();
526  while(jo_it != jo_last)
527  {
528  ss << (*jo_it)->SoftPrint()<<endl;
529  jo_it++;
530  }
531  return(ss.str());
532 }
533