MultiAgentDecisionProcess  Release 0.2.1
PolicyPureVector.cpp
Go to the documentation of this file.
1 
28 #include "PolicyPureVector.h"
29 #include "IndexTools.h"
30 #include <stdlib.h>
31 
32 using namespace std;
33 
34 #define DEBUG_PPV 0
35 
36 using std::stringstream;
37 
40  Index agentI,
42  size_t depth
43  ) :
44  PolicyDiscretePure(pu,idc, agentI)
45 {
47  GetNrPolicyDomainElements(agentI, GetIndexDomainCategory(), depth);
48  if(DEBUG_PPV)
49  cout << "PolicyPureVector(): creating policy for agent "<<agentI
50  << ", depth " << depth << " (nr domain elements "
51  << nrDE << ")" << endl;
52 
53  _m_agentI = agentI;
54  _m_domainToActionIndices = vector<Index>(nrDE, 0);
55 }
56 
57 //Copy assignment constructor.
60 {
61  if(DEBUG_PPV) cout << " clone PolicyPureVector ";
62  _m_agentI = o._m_agentI;
64 }
65 
66 //Destructor
68 {
70 }
71 
73 {
74  if (this == &o) return *this; // Gracefully handle self assignment
75  // Put the normal assignment duties here...
77  _m_agentI = o._m_agentI;
79 
80  return *this;
81 }
83 {
84 if(DEBUG_PPV)cout << ">>>PolicyPureVector::ZeroInitialization(): for agent "
85  << _m_agentI << endl;
86  vector<Index>::iterator it = _m_domainToActionIndices.begin();
87  vector<Index>::iterator last = _m_domainToActionIndices.end();
88 
89  while(it != last)
90  {
91  *it = 0;
92  it++;
93  }
94 }
95 
97 {
98  if(DEBUG_PPV)
99  cout << ">>>PolicyPureVector::RandomInitialization(): for agent "
100  << _m_agentI << endl;
101 
103  vector<Index>::iterator it = _m_domainToActionIndices.begin();
104  vector<Index>::iterator last = _m_domainToActionIndices.end();
105 
106  while(it != last)
107  {
108  int r = rand();
109  if(DEBUG_PPV)
110  cout << "rand() = " <<r<<endl;
111  *it = r% nrA;
112  it++;
113  }
114 }
115 
117 {
118  bool carry_over = true;
120  size_t nrOH = GetInterfacePTPDiscretePure()->
121  GetNrPolicyDomainElements(_m_agentI, GetIndexDomainCategory(),
122  GetDepth());
123 
124  //i is an integer index counting from nrObservationHistories-1 (=the last
125  // observation history) to 0
126  //(corresponding to the first (empty) observation sequence.
127  Index i = nrOH - 1;
128  while(carry_over)
129  {
131  carry_over = (_m_domainToActionIndices[i] == 0);
132  if(i > 0)
133  i--;
134  else
135  break;
136  }
137  return(carry_over);
138 }
139 
141 {
142  int nrO=GetInterfacePTPDiscretePure()->
143  GetNrPolicyDomainElements(_m_agentI,
145  GetDepth() ),
147  LIndex i=0;
148 
149  vector<LIndex> nrElems(2);
150  nrElems[0]=1;
151  nrElems[1]=nrA;
152 
153  vector<LIndex> indexVec(2);
154 
155  for(int o=0;o!=nrO;++o)
156  {
157  nrElems[0]*=nrO;
158  indexVec[0]=i;
159  indexVec[1]=_m_domainToActionIndices[o];
160  i=IndexTools::IndividualToJointIndices(indexVec,nrElems);
161  }
162  return(i);
163 }
164 
166 {
167  int nrO=GetInterfacePTPDiscretePure()->
168  GetNrPolicyDomainElements(_m_agentI,
170  GetDepth()),
172 
173  vector<LIndex> nrElems(2);
174  nrElems[0]=static_cast<LIndex>(pow(static_cast<double>(nrA),nrO));
175  nrElems[1]=nrA;
176  vector<LIndex> indexVec(2);
177 
178  for(int o=nrO-1;o>=0;--o)
179  {
180  nrElems[0]/=nrO;
181  indexVec=IndexTools::JointToIndividualIndices(i,nrElems);
182  SetAction(o,indexVec[1]);
183  i=indexVec[0];
184  }
185 }
186 
188 {
189  Policy::SetDepth(d);
191  GetInterfacePTPDiscretePure()->GetNrPolicyDomainElements(
192  _m_agentI,
194  d));
195 }
196 
198 {
199  vector<Index>::const_iterator it = _m_domainToActionIndices.begin();
200  vector<Index>::const_iterator last = _m_domainToActionIndices.end();
201 
202  //const ObservationHistoryTree* oht;
203  Index dIndex = 0;
204 
205  stringstream ss;
206 
207  while(it != last)
208  {
210  (_m_agentI, dIndex, GetIndexDomainCategory() );
211  ss << " --> ";
213  ss << endl;
214 
215  it++;
216  dIndex++;
217  }
218  return(ss.str());
219 }
220