MultiAgentDecisionProcess  Release 0.2.1
JPolComponent_VectorImplementation.cpp
Go to the documentation of this file.
1 
29 #include "IndexTools.h"
30 using namespace std;
31 
32 
36  size_t depth
37  )
38 {
39  _m_idc = idc;
40  for(Index agI = 0; agI < pu.GetNrAgents(); agI++)
41  {
42  PolicyPureVector* pol =
43  new PolicyPureVector(pu, agI, idc,
44  depth);
45  _m_indivPols_PolicyPureVector.at(agI) = pol;
46  }
47  //allocate a new array for temporarily storing indiv. actions
48  _m_indivActionIndices = new Index[pu.GetNrAgents()];
49 }
50 
53  size_t depth)
54  :
55  _m_PTPDP(&pu)
56  , _m_indivPols_PolicyPureVector(pu.GetNrAgents())
57 {
58  Construct( pu, pu.GetDefaultIndexDomCat(), depth);
59 }
60 
64  size_t depth
65  )
66  :
67  _m_PTPDP(&pu)
68  , _m_indivPols_PolicyPureVector(pu.GetNrAgents())
69 {
70  Construct( pu, idc, depth);
71 }
72 
73 //Copy constructor.
75 {
76 
77  // we want to create an actual clone of this joint policy and thus
78  // of the member (individual policies)
79  this->PrivClone(o);
80 }
81 
83 {
84  _m_idc = o._m_idc;
85  _m_PTPDP = o._m_PTPDP;
88 
89  vector<PolicyPureVector*>::const_iterator it =
91  vector<PolicyPureVector*>::const_iterator last =
93 
94  Index i=0;
95  while(it != last)
96  {
97  //make a copy of the individual policy
98  PolicyPureVector* ind_pol = new PolicyPureVector(**it);
99  //store the reference to the new copy
100  _m_indivPols_PolicyPureVector[i] = (ind_pol);
101  it++;
102  i++;
103  }
104  //allocate a new array for temporarily storing indiv. actions
106 
107 }
108 //Destructor
110 {
111  //prevent memory leakage... should delete the individual policies referred
112  //to...
114  delete [] _m_indivActionIndices;
115 }
116 
118 {
119  //NOTE: this means that when adding a ind. policy p to the joint policy (or
120  //replacing one) we should make a copy of p and insert the ref to this copy.
121  //Otherwise deleting the joint policy deletes the individual policies,
122  //whose other references become dangling.
123  vector<PolicyPureVector*>::iterator it =
125  vector<PolicyPureVector*>::iterator last =
127 
128  while(it != last)
129  {
130  delete (*it);
131  it++;
132  }
134 
135 }
136 
137 
138 
139 //Copy assignment operator
141 {
142  if (this == &o) return *this; // Gracefully handle self assignment
143  // Put the normal assignment duties here...
144 
145  //we delete the PolicyPureVectors pointed to:
146  //this means that we should not be able
147  //to add indiv. policies without copying them!!!
148  size_t s = _m_indivPols_PolicyPureVector.size() ;
149  if( s != o._m_indivPols_PolicyPureVector.size() )
150  {
153  o._m_indivPols_PolicyPureVector.size(), 0);
154  }
155 
156  for(Index i=0; i < _m_indivPols_PolicyPureVector.size() ; i++)
157  {
159  PolicyPureVector* this_pol = this->_m_indivPols_PolicyPureVector[i];
160  if(this_pol == 0) // this->_m_indivPols_PolicyPureVector[i] == 0
161  {
162  //this pol is not a jppv yet, so we need to copy
163  PolicyPureVector* copy = new PolicyPureVector(*o_pol);
164  this->_m_indivPols_PolicyPureVector[i] = copy;
165  }
166  else
167  {
168  //this_pol is an existing ppv: do assignment.
169  (*this_pol) = (*o_pol);
170  }
171  }
172  return *this;
173 }
174 
176 {
177  for(Index agI = 0; agI < _m_indivPols_PolicyPureVector.size(); agI++)
179 }
180 
181 
183 {
184  PolicyPureVector* this_pol = this->_m_indivPols_PolicyPureVector[agI];
185  if(this_pol == 0)
186  {
187  throw E(" JPolComponent_VectorImplementation::RandomInitialization(agI) this_pol is 0!?");
188  }
189  this_pol->RandomInitialization();
190 }
191 
192 
194 {
195  for(Index agI = 0; agI < _m_indivPols_PolicyPureVector.size(); agI++)
196  ZeroInitialization(agI);
197 }
198 
199 
201 {
202  PolicyPureVector* this_pol = this->_m_indivPols_PolicyPureVector[agI];
203  if(this_pol == 0)
204  {
205  throw E(" JPolComponent_VectorImplementation::ZeroInitialization(agI) this_pol is 0!?");
206  }
207  this_pol->ZeroInitialization();
208 }
209 
210 
211 
212 
214 {
215  bool carry_over = true;
216  size_t nrAgents = _m_PTPDP->GetNrAgents();
217  Index agentI = nrAgents - 1;
218  while(carry_over /*&& agentI >= 0*/)
219  {
220 // _m_obsHistToActionIndices[i] = (_m_obsHistToActionIndices[i] + 1) % nrA;
221  carry_over = ++(*_m_indivPols_PolicyPureVector[agentI]) ;
222  //(_m_obsHistToActionIndices[i] == 0);
223  if(agentI >=1)
224  agentI--;
225  else//agentI is 0; -- will get huge unsigned int leading to seg.fault
226  //if we don't jump out
227  break;
228  }
229  return(carry_over);
230 }
231 
233 {
234  int nrAgents=_m_PTPDP->GetNrAgents();
235 
236  vector<LIndex> nrElems(2);
237  nrElems[0]=1;
238  nrElems[1]=1;
239 
240  vector<LIndex> indexVec(2);
241 
242  vector<LIndex> nrIndPol(nrAgents);
243  vector<LIndex> individualIndices(nrAgents);
244  for(int k=0;k<nrAgents;++k)
245  {
246  nrIndPol[k]=_m_PTPDP->GetNrPolicies(k, _m_idc,
248  GetDepth());
249  individualIndices[k]=_m_indivPols_PolicyPureVector[k]->GetIndex();
250  }
251 
252  LIndex i=individualIndices[0];
253 
254  for(int k=1;k<nrAgents;++k)
255  {
256 
257  nrElems[0]*=nrIndPol[k-1];
258  nrElems[1]=nrIndPol[k-1];
259 
260  indexVec[0]=i;
261  indexVec[1]=individualIndices[k];
262  i=IndexTools::IndividualToJointIndices(indexVec,nrElems);
263 #if 0
264  cout << SoftPrintVector(nrIndPol) << " "
265  << SoftPrintVector(individualIndices)
266  << " " << k << " "
267  << SoftPrintVector(indexVec) << " "
268  << SoftPrintVector(nrElems) << endl;
269 #endif
270  }
271  return(i);
272 }
273 
275 {
276  int nrAgents=_m_PTPDP->GetNrAgents();
277 
278  vector<LIndex> nrElems(2);
279  nrElems[0]=1;
280  nrElems[1]=1;
281 
282  vector<LIndex> indexVec(2);
283 
284  //contains the number of individual policies per agent:
285  vector<LIndex> nrIndPol(nrAgents);
286  for(int k=0;k<nrAgents;++k)
287  {
288  nrIndPol[k]=_m_PTPDP->GetNrPolicies(k, _m_idc );
289  nrElems[0]*=nrIndPol[k];
290  }
291  //nrElems[0] now containts the number of joint policies:
292  // 1 * nrIndPol[1] * ... * nrIndPol[nrAgents-1]
293 
294  // Matthijs... wtf is happening here?!?
295  for(int k=nrAgents-1;k>0;--k)
296  {
297  nrElems[0]*=nrIndPol[k-1];
298  nrElems[1]=nrIndPol[k-1];
299 
300  indexVec=IndexTools::JointToIndividualIndices(i,nrElems);
301  _m_indivPols_PolicyPureVector[k]->SetIndex(indexVec[1]);
302  i=indexVec[0]; //this seems to be a remainder?!
303 #if 0
304  cout << SoftPrintVector(nrIndPol)
305  << " " << k << " "
306  << SoftPrintVector(indexVec) << " "
307  << SoftPrintVector(nrElems) << endl;
308 #endif
309  }
310  _m_indivPols_PolicyPureVector[0]->SetIndex(indexVec[0]);
311 }
312 
314 {
315  Index ohI, aThisAgent, ja_i;
316 
317  // it is possible to cache the joint->individual observ. history mapping
318  // If this is done we want to use it by calling
319  // JointToIndividualObservationHistoryIndicesRef
320  // as it is faster.
321  // Otherwise we have to use
322  // JointToIndividualObservationHistoryIndices
323  // to avoid a segfault.
324  //
325  // The mentioned cache is calculated, when generating joint observation
326  // histories. This is controlled by the
327  // PlanningUnitMADPDiscreteParameters of the PlanningUnitMADPDiscrete.
328  // ( _m_params.GetComputeJointObservationHistories() in particular )
329 
330  //if the "Interface_ProblemToPolicyDiscretePure"-implementing object this
331  //policy caches joint -> indiv OH indices
333  {
334  const vector<Index> &individualDomainIndices = _m_PTPDP->
335  JointToIndividualPolicyDomainIndicesRef(jdI, _m_idc );
336 
337  //vector<Index> individualActionIndices(individualDomainIndices.size());
338  for( Index agentI=0; agentI < _m_PTPDP->GetNrAgents() ; agentI++ )
339  {
340  ohI=individualDomainIndices[agentI];
341  aThisAgent = _m_indivPols_PolicyPureVector[agentI]->
342  GetActionIndex(ohI);
343  _m_indivActionIndices[agentI]=aThisAgent;
344  }
346  }
347  else
348  {
349  vector<Index> individualDomainIndices = _m_PTPDP->
350  JointToIndividualPolicyDomainIndices(jdI,_m_idc);
351 
352  size_t nrA = _m_PTPDP->GetNrAgents();
353  if( individualDomainIndices.size() !=
354  static_cast<vector<Index>::size_type>(nrA) )
355  {
356  stringstream ss;
357  ss<<" JPolComponent_VectorImplementation::GetJointActionIndex Dimension "<<
358  "mismatch, jdI has " << individualDomainIndices.size() <<
359  "individual actions, but there are "<< nrA <<" agents!\n";
360  throw E(ss);
361  }
362 
363  vector<Index> individualActionIndices(individualDomainIndices.size());
364  for( Index agentI=0; agentI < individualDomainIndices.size();
365  ++agentI )
366  {
367  ohI=individualDomainIndices[agentI];
368  aThisAgent = _m_indivPols_PolicyPureVector[agentI]->GetActionIndex(ohI);
369  individualActionIndices[agentI]=aThisAgent;
370  }
371 
373  individualActionIndices);
374 
375  }
376 
377  return(ja_i);
378 }
379 
381 {
382  vector<PolicyPureVector*>::iterator it =
384  vector<PolicyPureVector*>::iterator last =
386 
387  while(it != last)
388  {
389  (*it)->SetDepth(d);
390  it++;
391  }
392 }
393 
395 {
396  if(_m_PTPDP == 0 )
397  throw E("JPolComponent_VectorImplementation:SoftPrintBrief - Error!!! _m_PTPDP==0");
398 
399  stringstream ss;
400  ss << "JPolComponent_VectorImplementation index " << GetIndex()
401  //<< " depth " << GetDepth()
402  << endl;
403  for(
404  Index agentI = 0;
405  agentI < static_cast< vector<PolicyPureVector*>::size_type>
406  (_m_PTPDP->GetNrAgents());
407  agentI++
408  )
409  {
410  ss << "Policy for agent " << agentI << " (index "
411  << _m_indivPols_PolicyPureVector.at(agentI)->GetIndex() << "):" << endl
412  << _m_indivPols_PolicyPureVector.at(agentI)->SoftPrint();
413  }
414  return(ss.str());
415 }
416 
418 {
419  if(_m_PTPDP == 0 )
420  throw E("JPolComponent_VectorImplementation::SoftPrintBrief - Error!!! _m_PTPDP==0");
421 
422  stringstream ss;
423  ss << GetIndex();
424 
425  return(ss.str());
426 }
427