33 #define DEBUG_GETJA_COPYVEC 0
35 #define DEBUG_ADD_DA 0
36 #define DEBUG_MADP_DA 0
39 #define RUNTIME_CHECKS 0
46 _m_initialized =
false;
47 _m_cachedAllJointActions =
false;
48 _m_actionStepSize = 0;
49 _m_jointActionIndices=0;
50 _m_jointIndicesValid=
true;
57 vector<vector<ActionDiscrete> >::iterator it = _m_actionVecs.begin();
58 vector<vector<ActionDiscrete> >::iterator last = _m_actionVecs.end();
65 _m_actionVecs.clear();
66 vector<JointActionDiscrete*>::iterator it2 = _m_jointActionVec.begin();
67 vector<JointActionDiscrete*>::iterator last2 = _m_jointActionVec.end();
73 _m_jointActionVec.clear();
74 if(_m_jointActionIndices)
76 while(!_m_jointActionIndices->empty())
78 delete (*_m_jointActionIndices->begin()).second;
79 _m_jointActionIndices->erase( _m_jointActionIndices->begin() );
81 delete _m_jointActionIndices;
94 delete[] _m_actionStepSize;
102 if(_m_nrActions.size() != AI)
105 ss <<
"MADPComponentDiscreteActions::SetNrAction("<<AI<<
","<<
107 ") - error, actions of agents should be specified in order!"<<
108 " (the vector _m_nrActions should contain entries for all "<<
109 "preceeding agents.)";
114 _m_nrActions.push_back(nrA);
116 vector<ActionDiscrete> thisAgentsActions;
117 for(
Index i=0;i<nrA;i++)
124 _m_actionVecs.push_back(thisAgentsActions);
131 cerr <<
"MADPComponentDiscreteActions::AddAction("<<AI<<
","<<name<<
")"<<endl;
133 if(_m_nrActions.size() != AI && _m_nrActions.size() != AI+1)
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.)";
144 if(_m_nrActions.size() == AI )
147 _m_nrActions.push_back(1);
149 vector<ActionDiscrete> thisAgentsActions;
151 thisAgentsActions.push_back(ad);
152 _m_actionVecs.push_back(thisAgentsActions);
157 Index newActionIndex = _m_nrActions[AI]++;
159 _m_actionVecs[AI].push_back(ad);
168 size_t NRJA = ConstructJointActionsRecursively(0, *ja, 0);
169 _m_cachedAllJointActions=
true;
179 bool lastAgent=
false;
180 if(curAgentI == GetNrAgents()-1)
184 if(curAgentI >= _m_actionVecs.size())
187 ss <<
"ConstructJointActionsRecursively - current Agent index ("<<
188 curAgentI<<
") out of bounds! (_m_actionVecs contains actions for "<<
189 _m_actionVecs.size() <<
" agents...)\n";
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();
201 stringstream ss; ss <<
"ERROR empty action set for agent " << curAgentI;
214 cerr <<
"(first action - not making copy)\n";
217 else if (it == beforelast)
219 if(
DEBUG_CJA) cerr <<
"(last action - not making copy)\n";
220 p_ja = p_jaReceivedArgCopy;
224 if(
DEBUG_CJA) cerr <<
"(intermed. action - making copy)\n";
230 if(
DEBUG_CJA)cerr <<
"setting index of this joint action to: "
234 if(
DEBUG_CJA)cerr <<
"Adding agent's indiv. action to joint action..."
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;}
249 jaI = ConstructJointActionsRecursively(curAgentI+1,*p_ja, jaI);
254 cerr <<
">>ProblemDecTiger::ConstructJointActionsRecursively(Index "<<
255 curAgentI<<
", JointActionDiscrete& ja, Index "<< jaI<<
") FINISHED"
268 if(_m_initialized ==
true)
269 delete [] _m_actionStepSize;
273 if(_m_initialized ==
true && b ==
true)
276 delete [] _m_actionStepSize;
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();
285 if(!_m_cachedAllJointActions)
288 size_t prevNrJA=nrJA;
289 for(
Index i=0;i!=_m_nrActions.size();++i)
291 nrJA*=_m_nrActions[i];
294 _m_jointIndicesValid=
false;
297 _m_nrJointActions=nrJA;
298 _m_jointActionIndices=
299 new map<Index, vector<Index> *>();
302 _m_nrJointActions=_m_jointActionVec.size();
318 ss <<
"MADPComponentDiscreteActions::GetNrActions("<<
319 agentI<<
") - Error: not initialized. "<<endl;
323 size_t nrA = GetNrAgents();
324 if(nrA != _m_nrActions.size())
327 ss <<
"MADPComponentDiscreteActions::GetNrActions("<<
328 agentI<<
") - Error: nrA != _m_nrActions.size() "<<endl;
332 return _m_nrActions[agentI];
336 ss <<
"Warning: MADPComponentDiscreteActions::GetNrActions(Index agentI) - index out of bounds"<<endl;
345 ss <<
"MADPComponentDiscreteActions::GetNrJointActions("<<
346 ") - Error: not initialized. "<<endl;
349 if(!_m_jointIndicesValid)
351 throw(
E(
"MADPComponentDiscreteActions::GetNrJointActions() joint indices are not available, overflow detected"));
353 return(_m_nrJointActions);
361 throw E(
"MADPComponentDiscreteActions::GetActionIndexByName - not initialized!");
363 if(agentI >= _m_actionVecs.size())
366 ss <<
"GetActionIndexByName - Agent index ("<<
367 agentI<<
") out of bounds! (_m_actionVecs contains actions for "<<
368 _m_actionVecs.size() <<
" agents...)\n";
371 vector<ActionDiscrete>::const_iterator it = _m_actionVecs[agentI].begin();
372 vector<ActionDiscrete>::const_iterator last = _m_actionVecs[agentI].end();
375 string s2 = (*it).GetName();
378 return( (*it).GetIndex() );
383 ss <<
"GetActionIndexByName - action \"" << s <<
"\" of agent " << agentI <<
384 " not found." << endl;
385 throw E(ss.str().c_str());
391 return ((
Action*) GetActionDiscrete( agentI, a) );
398 ss <<
"MADPComponentDiscreteActions::GetAction("<<
399 agentI<<
") - Error: not initialized. "<<endl;
402 if(agentI < GetNrAgents() )
404 if(a < GetNrActions(agentI) )
406 return (&_m_actionVecs[agentI][a]);
410 ss <<
"WARNING MADPComponentDiscreteActions::GetAction(Index agentI, Index a) index a out of bounds"<<endl;
415 ss <<
"WARNING MADPComponentDiscreteActions::GetAction(Index agentI, Index a) index agentI out of bounds"<<endl;
424 ss <<
"MADPComponentDiscreteActions::GetJointActionDiscrete("<<
425 i<<
") - Error: not initialized. "<<endl;
428 if(!_m_cachedAllJointActions)
430 throw E(
"MADPComponentDiscreteActions::GetJointActionDiscrete: joint actions have not been created");
432 if(i < _m_jointActionVec.size() )
439 ss <<
"WARNING MADPComponentDiscreteActions::GetJointActionDiscrete(Index i) index a out of bounds"<<endl;
448 ss <<
"MADPComponentDiscreteActions::GetJointAction("<<
449 i<<
") - Error: not initialized. "<<endl;
452 if(!_m_cachedAllJointActions)
454 throw E(
"MADPComponentDiscreteActions::GetJointActionDiscrete: joint actions have not been created");
456 if(i < _m_jointActionVec.size() )
457 return( (
const JointAction*) _m_jointActionVec[i] );
460 ss <<
"WARNING MADPComponentDiscreteActions::GetJointAction(Index i) index a out of bounds"<<endl;
465 const vector<Index>& indivActionIndices)
const
471 ss <<
"MADPComponentDiscreteActions::GetJointActionIndex("<<
472 "vector<Index>& indivActionIndices) -Error: not initialized."<<endl;
485 if(
DEBUG_MADP_DA){ss <<
"MADPComponentDiscreteActions::Print()" << endl;}
489 ss <<
"MADPComponentDiscreteActions::Print("<<
490 ") - Error: not initialized. "<<endl;
493 ss <<
"#joint actions="<<GetNrJointActions()<<endl;
494 ss << SoftPrintActionSets();
495 ss <<
"Joint Actions:"<<endl;
496 ss << SoftPrintJointActionSet();
506 ss <<
"MADPComponentDiscreteActions::PrintActionSets("<<
507 ") - Error: not initialized. "<<endl;
510 ss <<
"Actions:"<<endl;
511 for(
Index agentIndex=0; agentIndex < GetNrAgents(); agentIndex++)
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();
521 ss << (*f).SoftPrint() << endl;
535 ss <<
"MADPComponentDiscreteActions::PrintJointActionSets("<<
536 ") - Error: not initialized. "<<endl;
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)
545 ss << (*ja_it)->SoftPrint()<<endl;