MultiAgentDecisionProcess  Release 0.2.1
MADPComponentDiscreteActions.cpp
Go to the documentation of this file.
1 
29 #include "VectorTools.h"
30 
31 using namespace std;
32 
33 #define DEBUG_GETJA_COPYVEC 0
34 #define DEBUG_CJA 0
35 #define DEBUG_ADD_DA 0
36 #define DEBUG_MADP_DA 0
37 
38 //the following variable enables some runtime checks - enable it for debugging
39 #define RUNTIME_CHECKS 0
40 
41 
42 
43 //Default constructor
45 {
46  _m_initialized = false;
47  _m_cachedAllJointActions = false;
48  _m_actionStepSize = 0;
49  _m_jointActionIndices=0;
50  _m_jointIndicesValid=true;
51 }
52 
53 //Destructor
55 {
56  _m_nrActions.clear();
57  vector<vector<ActionDiscrete> >::iterator it = _m_actionVecs.begin();
58  vector<vector<ActionDiscrete> >::iterator last = _m_actionVecs.end();
59  while(it != last)
60  {
61  // (*it) isa vector<ActionDiscrete>
62  (*it).clear();
63  it++;
64  }
65  _m_actionVecs.clear();
66  vector<JointActionDiscrete*>::iterator it2 = _m_jointActionVec.begin();
67  vector<JointActionDiscrete*>::iterator last2 = _m_jointActionVec.end();
68  while(it2 != last2)
69  {
70  delete *it2; //removes the joint action pointed to...
71  it2++;
72  }
73  _m_jointActionVec.clear();
74  if(_m_jointActionIndices)
75  {
76  while(!_m_jointActionIndices->empty())
77  {
78  delete (*_m_jointActionIndices->begin()).second;
79  _m_jointActionIndices->erase( _m_jointActionIndices->begin() );
80  }
81  delete _m_jointActionIndices;
82  }
83 // if(_m_jointActionIndices)
84 // {
85 // vector<vector<Index>*>::iterator it3 = _m_jointActionIndices->begin();
86 // vector<vector<Index>*>::iterator last3 = _m_jointActionIndices->end();
87 // while(it3 != last3)
88 // {
89 // delete *it3;
90 // it3++;
91 // }
92 // }
93 
94  delete[] _m_actionStepSize;
95 }
96 
97 //data manipulation (set) functions:
98 
101 {
102  if(_m_nrActions.size() != AI)
103  {
104  stringstream ss;
105  ss << "MADPComponentDiscreteActions::SetNrAction("<<AI<<","<<
106  nrA<<
107  ") - error, actions of agents should be specified in order!"<<
108  " (the vector _m_nrActions should contain entries for all "<<
109  "preceeding agents.)";
110  throw(E(ss));
111  }
112  else
113  {
114  _m_nrActions.push_back(nrA);
115  //create nameless actions for this agent...
116  vector<ActionDiscrete> thisAgentsActions;
117  for(Index i=0;i<nrA;i++)
118  {
119  stringstream ss;
120  ss //<< "ag"<<AI
121  <<"a"<<i;
122  thisAgentsActions.push_back(ActionDiscrete(i, ss.str() ));
123  }
124  _m_actionVecs.push_back(thisAgentsActions);
125  }
126 }
127 
129 {
130  if(DEBUG_ADD_DA)
131  cerr << "MADPComponentDiscreteActions::AddAction("<<AI<<","<<name<<")"<<endl;
132 
133  if(_m_nrActions.size() != AI && _m_nrActions.size() != AI+1)
134  {
135  stringstream ss;
136  ss << "MADPComponentDiscreteActions::AddAction("<<AI<<","<<name<<
137  ") - error, actions of agents should be specified in order!"<<
138  " first all actions of agent 1, then all of agent 2,...etc..."<<
139  " _m_nrActions.size now is: "<< _m_nrActions.size() <<
140  "\n(the vector _m_nrActions should contain entries for all "<<
141  "preceeding agents.)";
142  throw(E(ss));
143  }
144  if(_m_nrActions.size() == AI )
145  {
146  //this is the first action we add for this agent
147  _m_nrActions.push_back(1);
148 
149  vector<ActionDiscrete> thisAgentsActions;
150  ActionDiscrete ad(0, name);
151  thisAgentsActions.push_back(ad);
152  _m_actionVecs.push_back(thisAgentsActions);
153  }
154  else
155  {
156  //we add an action for this agent - increment his nr_actions
157  Index newActionIndex = _m_nrActions[AI]++;
158  ActionDiscrete ad(newActionIndex, name);
159  _m_actionVecs[AI].push_back(ad);
160  }
161 }
162 
166 {
168  size_t NRJA = ConstructJointActionsRecursively(0, *ja, 0);
169  _m_cachedAllJointActions=true;
170  return NRJA;
171 }
172 
176  Index curAgentI, JointActionDiscrete& ja, Index jaI)
177 {
178 
179  bool lastAgent=false;
180  if(curAgentI == GetNrAgents()-1)
181  {
182  lastAgent = true;
183  }
184  if(curAgentI >= _m_actionVecs.size())
185  {
186  stringstream ss;
187  ss << "ConstructJointActionsRecursively - current Agent index ("<<
188  curAgentI<<") out of bounds! (_m_actionVecs contains actions for "<<
189  _m_actionVecs.size() << " agents...)\n";
190  throw E(ss);
191  }
192 
193  ActionDVec::iterator first = _m_actionVecs[curAgentI].begin();
194  ActionDVec::iterator it = _m_actionVecs[curAgentI].begin();
195  ActionDVec::iterator last = _m_actionVecs[curAgentI].end();
196  ActionDVec::iterator beforelast = _m_actionVecs[curAgentI].end();
197  beforelast--;
198 
199  if(it == last)
200  {
201  stringstream ss; ss << "ERROR empty action set for agent " << curAgentI;
202  throw E(ss);
203  }
204  //first action extends the received ja
205  JointActionDiscrete* p_jaReceivedArgCopy = new JointActionDiscrete(ja);
206  JointActionDiscrete* p_ja;
207 
208  while( it != last) // other actions extend duplicates of ja
209  {
210  if(DEBUG_CJA) cerr << "\nnext action";
211  if(it == first) //
212  {
213  if(DEBUG_CJA)
214  cerr << "(first action - not making copy)\n";
215  p_ja = &ja;
216  }
217  else if (it == beforelast)//this is the last valid it -> last action
218  {
219  if(DEBUG_CJA) cerr << "(last action - not making copy)\n";
220  p_ja = p_jaReceivedArgCopy; //don't make a new copy
221  }
222  else //make a new copy
223  {
224  if(DEBUG_CJA) cerr << "(intermed. action - making copy)\n";
225  p_ja = new JointActionDiscrete(*p_jaReceivedArgCopy);
226  }
227  if(lastAgent)
228  {
229  p_ja->SetIndex(jaI);
230  if(DEBUG_CJA)cerr << "setting index of this joint action to: "
231  << jaI <<endl;
232  }
233  ActionDiscrete* ai = /*(ActionDiscrete*)*/ &(*it);
234  if(DEBUG_CJA)cerr <<"Adding agent's indiv. action to joint action..."
235  <<endl;
236  p_ja->AddIndividualAction(ai, curAgentI);
237 
238  if(lastAgent) //jointAction is now completed: add it to jointAction set.
239  {
240  if(DEBUG_CJA){cerr << "INSERTING the joint action:";
241  p_ja->Print();cerr<<endl;}
242  _m_jointActionVec.push_back(p_ja);
243  if(DEBUG_CJA){cerr << "\nINSERTED the joint action."<<endl;
244  cerr << "_m_jointActionVec now containts "<<
245  _m_jointActionVec.size() << " joint actions." << endl;}
246  jaI++;
247  }
248  else
249  jaI = ConstructJointActionsRecursively(curAgentI+1,*p_ja, jaI);
250 
251  it++;
252  }
253  if(DEBUG_CJA)
254  cerr << ">>ProblemDecTiger::ConstructJointActionsRecursively(Index "<<
255  curAgentI<<", JointActionDiscrete& ja, Index "<< jaI<<") FINISHED"
256  <<endl;
257  return jaI;
258 }
259 
260 
265 {
266  if(b == false)
267  {
268  if(_m_initialized == true)
269  delete [] _m_actionStepSize;
270  _m_initialized = b;
271  return true;
272  }
273  if(_m_initialized == true && b == true)
274  {
275  //first free mem before re-initialize:
276  delete [] _m_actionStepSize;
277  }
278  if(b == true)
279  {
280  if(_m_nrActions.size() != GetNrAgents())
281  throw(E("MADPComponentDiscreteActions::SetInitialized size of _m_nrActions vector does not agree with the number of agents"));
282  _m_nr_agents=GetNrAgents();
283  _m_actionStepSize=IndexTools::CalculateStepSize(_m_nrActions);
284 
285  if(!_m_cachedAllJointActions)
286  {
287  size_t nrJA=1;
288  size_t prevNrJA=nrJA;
289  for(Index i=0;i!=_m_nrActions.size();++i)
290  {
291  nrJA*=_m_nrActions[i];
292  // detect overflow
293  if(nrJA<prevNrJA)
294  _m_jointIndicesValid=false;
295  prevNrJA=nrJA;
296  }
297  _m_nrJointActions=nrJA;
298  _m_jointActionIndices=
299  new map<Index, vector<Index> *>();
300  }
301  else
302  _m_nrJointActions=_m_jointActionVec.size();
303  _m_initialized = b;
304  }
305  return(true);
306 }
307 
308 
309 
311 {
312 #if 0
313  // this is very inconvenient: we sometimes want to get the #actions
314  // before setting to initialised.
315  if(!_m_initialized)
316  {
317  stringstream ss;
318  ss << "MADPComponentDiscreteActions::GetNrActions("<<
319  agentI<<") - Error: not initialized. "<<endl;
320  throw E(ss);
321  }
322 #endif
323  size_t nrA = GetNrAgents();
324  if(nrA != _m_nrActions.size())
325  {
326  stringstream ss;
327  ss << "MADPComponentDiscreteActions::GetNrActions("<<
328  agentI<<") - Error: nrA != _m_nrActions.size() "<<endl;
329  throw E(ss);
330  }
331  if(agentI < nrA)
332  return _m_nrActions[agentI];
333  else
334  {
335  stringstream ss;
336  ss << "Warning: MADPComponentDiscreteActions::GetNrActions(Index agentI) - index out of bounds"<<endl;
337  throw E(ss);
338  }
339 }
341 {
342  if(!_m_initialized)
343  {
344  stringstream ss;
345  ss << "MADPComponentDiscreteActions::GetNrJointActions("<<
346  ") - Error: not initialized. "<<endl;
347  throw E(ss);
348  }
349  if(!_m_jointIndicesValid)
350  {
351  throw(E("MADPComponentDiscreteActions::GetNrJointActions() joint indices are not available, overflow detected"));
352  }
353  return(_m_nrJointActions);
354 }
355 
358  Index agentI) const
359 {
360  if(!_m_initialized)
361  throw E("MADPComponentDiscreteActions::GetActionIndexByName - not initialized!");
362 
363  if(agentI >= _m_actionVecs.size())
364  {
365  stringstream ss;
366  ss << "GetActionIndexByName - Agent index ("<<
367  agentI<<") out of bounds! (_m_actionVecs contains actions for "<<
368  _m_actionVecs.size() << " agents...)\n";
369  throw E(ss);
370  }
371  vector<ActionDiscrete>::const_iterator it = _m_actionVecs[agentI].begin();
372  vector<ActionDiscrete>::const_iterator last = _m_actionVecs[agentI].end();
373  while(it != last)
374  {
375  string s2 = (*it).GetName();
376  if(s == s2)
377  //if(strcmp(s,s2) == 0)//match
378  return( (*it).GetIndex() );
379  it++;
380  }
381  //not found
382  stringstream ss;
383  ss << "GetActionIndexByName - action \"" << s << "\" of agent " << agentI <<
384  " not found." << endl;
385  throw E(ss.str().c_str());
386 
387 }
388 
390 {
391  return ((Action*) GetActionDiscrete( agentI, a) );
392 }
394 {
395  if(!_m_initialized)
396  {
397  stringstream ss;
398  ss << "MADPComponentDiscreteActions::GetAction("<<
399  agentI<<") - Error: not initialized. "<<endl;
400  throw E(ss);
401  }
402  if(agentI < GetNrAgents() )
403  {
404  if(a < GetNrActions(agentI) )
405  {
406  return (&_m_actionVecs[agentI][a]);
407  }
408  //else
409  stringstream ss;
410  ss << "WARNING MADPComponentDiscreteActions::GetAction(Index agentI, Index a) index a out of bounds"<<endl;
411  throw E(ss);
412  }
413  //else
414  stringstream ss;
415  ss << "WARNING MADPComponentDiscreteActions::GetAction(Index agentI, Index a) index agentI out of bounds"<<endl;
416  throw E(ss);
417 }
418 /*return a ref to the i-th joint action.*/
420 {
421  if(!_m_initialized)
422  {
423  stringstream ss;
424  ss << "MADPComponentDiscreteActions::GetJointActionDiscrete("<<
425  i<<") - Error: not initialized. "<<endl;
426  throw E(ss);
427  }
428  if(!_m_cachedAllJointActions)
429  {
430  throw E("MADPComponentDiscreteActions::GetJointActionDiscrete: joint actions have not been created");
431  }
432  if(i < _m_jointActionVec.size() )
433  {
434  const JointActionDiscrete* j = _m_jointActionVec[i];
435  return( j );
436  }
437  //else
438  stringstream ss;
439  ss << "WARNING MADPComponentDiscreteActions::GetJointActionDiscrete(Index i) index a out of bounds"<<endl;
440  throw E(ss);
441 }
442 /*return a ref to the i-th joint action.*/
444 {
445  if(!_m_initialized)
446  {
447  stringstream ss;
448  ss << "MADPComponentDiscreteActions::GetJointAction("<<
449  i<<") - Error: not initialized. "<<endl;
450  throw E(ss);
451  }
452  if(!_m_cachedAllJointActions)
453  {
454  throw E("MADPComponentDiscreteActions::GetJointActionDiscrete: joint actions have not been created");
455  }
456  if(i < _m_jointActionVec.size() )
457  return( (const JointAction*) _m_jointActionVec[i] );
458  //else
459  stringstream ss;
460  ss << "WARNING MADPComponentDiscreteActions::GetJointAction(Index i) index a out of bounds"<<endl;
461  throw E(ss);
462 }
463 
465  const vector<Index>& indivActionIndices)const
466 {
467 #if RUNTIME_CHECKS
468  if(!_m_initialized)
469  {
470  stringstream ss;
471  ss << "MADPComponentDiscreteActions::GetJointActionIndex("<<
472  "vector<Index>& indivActionIndices) -Error: not initialized."<<endl;
473  throw E(ss);
474  }
475 #endif
477  _m_actionStepSize);
478 
479  return(i);
480 }
481 
483 {
484  stringstream ss;
485  if(DEBUG_MADP_DA){ss << "MADPComponentDiscreteActions::Print()" << endl;}
486  if(!_m_initialized)
487  {
488  stringstream ss;
489  ss << "MADPComponentDiscreteActions::Print("<<
490  ") - Error: not initialized. "<<endl;
491  throw E(ss);
492  }
493  ss << "#joint actions="<<GetNrJointActions()<<endl;
494  ss << SoftPrintActionSets();
495  ss << "Joint Actions:"<<endl;
496  ss << SoftPrintJointActionSet();
497  return(ss.str());
498 }
499 
501 {
502  stringstream ss;
503  if(!_m_initialized)
504  {
505  stringstream ss;
506  ss << "MADPComponentDiscreteActions::PrintActionSets("<<
507  ") - Error: not initialized. "<<endl;
508  throw E(ss);
509  }
510  ss << "Actions:"<<endl;
511  for(Index agentIndex=0; agentIndex < GetNrAgents(); agentIndex++)
512  {
513  ss << "agentI " << agentIndex << " - nrActions " <<
514  GetNrActions(agentIndex)<<endl;
515  vector<ActionDiscrete>::const_iterator f =
516  _m_actionVecs[agentIndex].begin();
517  vector<ActionDiscrete>::const_iterator l =
518  _m_actionVecs[agentIndex].end();
519  while(f != l)
520  {
521  ss << (*f).SoftPrint() << endl;
522  // (*f).GetName() << " - " << (*f).GetDescription()<<endl;
523  f++;
524  }
525  }
526  return(ss.str());
527 }
528 
530 {
531  stringstream ss;
532  if(!_m_initialized)
533  {
534  stringstream ss;
535  ss << "MADPComponentDiscreteActions::PrintJointActionSets("<<
536  ") - Error: not initialized. "<<endl;
537  throw E(ss);
538  }
539  vector<JointActionDiscrete*>::const_iterator ja_it =
540  _m_jointActionVec.begin();
541  vector<JointActionDiscrete*>::const_iterator ja_last =
542  _m_jointActionVec.end();
543  while(ja_it != ja_last)
544  {
545  ss << (*ja_it)->SoftPrint()<<endl;
546  ja_it++;
547  }
548  return(ss.str());
549 }