33 #define DEBUG_GETJO_COPYVEC 0
35 #define DEBUG_ADD_DO 0
40 _m_initialized =
false;
41 _m_observationStepSize = 0;
42 _m_cachedAllJointObservations=
false;
43 _m_jointObservationIndices=0;
44 _m_jointIndicesValid=
true;
50 _m_nrObservations.clear();
51 vector<vector<ObservationDiscrete> >::iterator it =
52 _m_observationVecs.begin();
53 vector<vector<ObservationDiscrete> >::iterator last =
54 _m_observationVecs.end();
61 _m_observationVecs.clear();
62 vector<JointObservationDiscrete*>::iterator it2 =
63 _m_jointObservationVec.begin();
64 vector<JointObservationDiscrete*>::iterator last2 =
65 _m_jointObservationVec.end();
72 _m_jointObservationVec.clear();
74 if(_m_jointObservationIndices)
76 while(!_m_jointObservationIndices->empty())
78 delete (*_m_jointObservationIndices->begin()).second;
79 _m_jointObservationIndices->erase( _m_jointObservationIndices->begin() );
81 delete _m_jointObservationIndices;
84 if(_m_jointObservationIndices)
86 vector<vector<Index>*>::iterator it3 = _m_jointObservationIndices->begin();
87 vector<vector<Index>*>::iterator last3 = _m_jointObservationIndices->end();
95 delete[] _m_observationStepSize;
105 if(
DEBUG_CJO) cerr <<
"ConstructJointObservations() - called";
107 size_t NRJO = ConstructJointObservationsRecursively(0, *jo, 0);
108 _m_cachedAllJointObservations=
true;
117 if(
DEBUG_CJO) cerr <<
">> MADPComponentDiscreteObservations::ConstructJointObservations(Index "<<curAgentI<<
", JointObservationDiscrete& jo, Index "<< joI<<
") called"<<endl;
119 bool lastAgent=
false;
120 if(curAgentI == GetNrAgents()-1)
125 if(curAgentI >= _m_observationVecs.size())
128 ss <<
"ConstructJointObservationsRecursively - current Agent index ("<<
129 curAgentI<<
") out of bounds! (_m_observationVecs contains "<<
130 "observations for "<<_m_observationVecs.size() <<
" agents...)\n";
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();
142 ss <<
" empty observation set for agent " << curAgentI;
153 cerr <<
"\nnext observation";
156 if(
DEBUG_CJO) cerr <<
"(first observation - not making copy)\n";
159 else if(it == beforelast)
161 if(
DEBUG_CJO) cerr <<
"(last observation - not making copy)\n";
162 p_jo = p_joReceivedArgCopy;
166 if(
DEBUG_CJO) cerr <<
"(intermed. observation - making copy)\n";
173 cerr <<
"setting index of this joint observation to: "<< joI
178 cerr <<
"Adding agent's indiv. observation to joint observation..."<<endl;
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;}
190 joI = ConstructJointObservationsRecursively(curAgentI+1,*p_jo, joI);
193 if(
DEBUG_CJO) cerr <<
">> MADPComponentDiscreteObservations::ConstructJointObservationsRecursively(Index "<<curAgentI<<
", JointObservationDiscrete& jo, Index "<< joI<<
") FINISHED"<<endl;
202 if(_m_nrObservations.size() != AI)
205 ss <<
"MADPComponentDiscreteObservations::SetNrObservation("<<AI<<
","<<
207 ") - error, observations of agents should be specified in order!"<<
208 " (the vector _m_nrObservations should contain entries for all "<<
209 "preceeding agents.)";
214 _m_nrObservations.push_back(nrO);
216 vector<ObservationDiscrete> thisAgentsObservations;
217 for(
Index i=0;i<nrO;i++)
224 _m_observationVecs.push_back(thisAgentsObservations);
231 cerr<<
"MADPComponentDiscreteObservation::AddObservation("<<AI<<
","<<name<<
")"
234 if(_m_nrObservations.size() != AI && _m_nrObservations.size() != AI+1)
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.)";
245 if(_m_nrObservations.size() == AI )
248 _m_nrObservations.push_back(1);
250 vector<ObservationDiscrete> thisAgentsObservations;
252 thisAgentsObservations.push_back(ad);
253 _m_observationVecs.push_back(thisAgentsObservations);
258 Index newObservationIndex = _m_nrObservations[AI]++;
260 _m_observationVecs[AI].push_back(ad);
271 if(_m_initialized ==
true)
272 delete [] _m_observationStepSize;
276 if(_m_initialized ==
true && b ==
true)
279 delete [] _m_observationStepSize;
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=
290 if(!_m_cachedAllJointObservations)
293 size_t prevNrJO=nrJO;
294 for(
Index i=0;i!=_m_nrObservations.size();++i)
296 nrJO*=_m_nrObservations[i];
299 _m_jointIndicesValid=
false;
302 _m_nrJointObservations=nrJO;
303 _m_jointObservationIndices=
304 new map<Index, vector<Index> *>();
307 _m_nrJointObservations=_m_jointObservationVec.size();
318 ss <<
"MADPComponentDiscreteObservations::GetNrObservations("<<
319 agentI<<
") - Error: not initialized. "<<endl;
322 size_t nrA = GetNrAgents();
323 if(nrA != _m_nrObservations.size())
326 ss <<
"MADPComponentDiscreteObservations::GetNrObservations("<<
327 agentI<<
") - Error: nrA != _m_nrObservations.size() "<<endl;
331 return _m_nrObservations[agentI];
335 ss <<
"Warning: MADPComponentDiscreteObservations::GetNrObservations(Index agentI) - index out of bounds"<<endl;
345 ss <<
"MADPComponentDiscreteObservations::GetNrJointObservations("<<
346 ") - Error: not initialized. "<<endl;
349 return _m_nrJointObservations;
356 throw E(
"MADPComponentDiscreteObservations::GetObservationIndexByName - not initialized!");
357 if(agentI >= _m_observationVecs.size())
360 ss <<
"GetObservationIndexByName - Agent index ("<<
361 agentI<<
") out of bounds! (_m_observationVecs contains observations"
362 <<
" for " << _m_observationVecs.size() <<
" agents...)\n";
365 vector<ObservationDiscrete>::const_iterator it =
366 _m_observationVecs[agentI].begin();
367 vector<ObservationDiscrete>::const_iterator last =
368 _m_observationVecs[agentI].end();
371 string s2 = (*it).GetName();
373 return( (*it).GetIndex() );
379 ss <<
"GetObservationIndexByName - observation \"" << s <<
"\" of agent " <<
380 agentI <<
" not found." << endl;
390 ss <<
"MADPComponentDiscreteObservations::GetObservationDiscrete("<<
391 agentI<<
","<< a <<
") - Error: not initialized. "<<endl;
395 if(agentI < GetNrAgents() )
397 if(a < GetNrObservations(agentI) )
399 return ( &_m_observationVecs[agentI][a]);
402 cerr <<
"WARNING MADPComponentDiscreteObservations::GetObservationDiscrete("<< agentI<<
","<< a <<
") - observation (2nd) index out of bounds"<<endl;
405 cerr <<
"WARNING MADPComponentDiscreteObservations::GetObservationDiscrete("<< agentI<<
","<< a <<
") - agent (1st) index out of bounds"<<endl;
416 ss <<
"MADPComponentDiscreteObservations::GetJointObservationDiscrete("
417 << i<<
") - Error: not initialized. "<<endl;
420 if(!_m_cachedAllJointObservations)
422 throw E(
"MADPComponentDiscreteObservations::GetJointObservationDiscrete: joint observations have not been created");
424 if(i < _m_jointObservationVec.size() )
431 ss <<
"WARNING MADPComponentDiscreteObservations::GetJointObservationDiscrete(Index i) index out of bounds (i="<< i <<
")"<<endl;
440 ss <<
"MADPComponentDiscreteObservations::GetJointObservation("<<
441 i<<
") - Error: not initialized. "<<endl;
444 if(!_m_cachedAllJointObservations)
446 throw E(
"MADPComponentDiscreteObservations::GetJointObservationDiscrete: joint observations have not been created");
448 if(i < _m_jointObservationVec.size() )
452 ss <<
"WARNING MADPComponentDiscreteObservations::GetJointObservation(Index i) index out of bounds (i="<< i <<
")"<<endl;
456 const vector<Index>& indivObservationIndices)
const
461 ss <<
"MADPComponentDiscreteObservations::GetJointObservationIndex("<<
462 "vector<Index>& indivObservationIndices) - Error: not initialized."
467 indivObservationIndices, _m_observationStepSize);
475 ss <<
"MADPComponentDiscreteObservations::Print()" << endl;
476 ss <<
"#joint observations="<<GetNrJointObservations()<<endl;
477 ss << SoftPrintObservationSets();
478 ss <<
"Joint Observations:"<<endl;
479 ss << SoftPrintJointObservationSet();
489 ss <<
"MADPComponentDiscreteObservations::PrintObservationSets("<<
490 ") - Error: not initialized. "<<endl;
493 ss <<
"Observations:"<<endl;
494 for(
Index agentIndex=0; agentIndex < GetNrAgents(); agentIndex++)
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();
504 ss << (*f).SoftPrint() << endl;
518 ss <<
"MADPComponentDiscreteObservations::PrintJointObservationSet("<<
519 ") - Error: not initialized. "<<endl;
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)
528 ss << (*jo_it)->SoftPrint()<<endl;