MultiAgentDecisionProcess  Release 0.2.1
BGforStageCreation.cpp
Go to the documentation of this file.
1 
28 #include "BGforStageCreation.h"
31 
32 using namespace std;
33 /*
34 //Default constructor
35 BGforStageCreation::BGforStageCreation()
36 {
37 }
38 //Copy constructor.
39 BGforStageCreation::BGforStageCreation(const BGforStageCreation& o)
40 {
41 }
42 //Destructor
43 BGforStageCreation::~BGforStageCreation()
44 {
45 }
46 //Copy assignment operator
47 BGforStageCreation& BGforStageCreation::operator= (const BGforStageCreation& o)
48 {
49  if (this == &o) return *this; // Gracefully handle self assignment
50  // Put the normal assignment duties here...
51 
52  return *this;
53 }
54 */
55 
56 
57 
58 
60  const PlanningUnitMADPDiscrete* pu,
61  Index ts,
62  vector<Index>& firstOHtsI
63  )
64 {
65  firstOHtsI.clear();
66  //because the OHs are constructed breath-first, we know the OHs for agent i
67  //for this time step are numbered:
68  //firstOHtsGI[i]...firstOHtsGI[i]+nrOH[i]-1
69  //
70  //(read first-OH-for-time-step-ts its Global Index)
71  //
72  //i.e. ohGI = ohI + firstOHtsGI
73 
74  for(Index agI=0; agI < pu->GetNrAgents(); agI++)
75  {
76  Index fI = pu->GetFirstObservationHistoryIndex(agI, ts);
77  firstOHtsI.push_back(fI);
78  }
79 }
80 
81 /*
82 
83 //this function extends a previous policy jpolPrevTs for ts-1 with the behavior specified by the policy of the BayesianGame for time step ts (jpolBG).
84 PartialJointPolicyDiscretePure*
85 BGforStageCreation::ConstructExtendedPolicy(
86  PartialJointPolicyDiscretePure& jpolPrevTs,
87  JointPolicyDiscretePure& jpolBG,
88  vector<size_t>& nrOHts,
89  vector<Index>& firstOHtsI
90  )
91 {
92  //check policy types
93  if(jpolPrevTs.GetIndexDomainCategory() != PolicyGlobals::OHIST_INDEX)
94  throw E("BGforStageCreation::ConstructExtendedPolicy --- jpolPrevTs.GetIndexDomainCategory() != PolicyGlobals::OHIST_INDEX ");
95  if(jpolBG.GetIndexDomainCategory() != PolicyGlobals::TYPE_INDEX)
96  throw E("BGforStageCreation::ConstructExtendedPolicy --- jpolPrevTs.GetIndexDomainCategory() != PolicyGlobals::TYPE_INDEX ");
97  //construct a policy for the DecPOMDP:
98  //a copy of jpolPrevTs with extended to this time step (ts) by
99  //jpolBG
100  PartialJointPolicyDiscretePure* jpolTs = new
101  PartialJointPolicyPureVector(jpolPrevTs);
102  jpolTs->SetDepth( jpolTs->GetDepth()+1 );
103  for(Index agentI=0; agentI < GetNrAgents(); agentI++)
104  {
105  for(Index type = 0; type < nrOHts[agentI]; type++)
106  {
107  Index ohI = type + firstOHtsI[agentI];
108  jpolTs->SetAction(agentI, ohI,
109  jpolBG.GetActionIndex(agentI, type) );
110  }
111  }
112  return(jpolTs);
113 }
114 */
115 
116 
118  const PlanningUnitMADPDiscrete* pu,
119  const Index ts,
120  const vector<Index>& indTypes,
121  const vector<Index>& firstOHtsI,
122  Index* joI_arr)
123 {
124  //convert indiv type indices to ind. observation history indices:
125  vector<Index> indOHI = vector<Index>(indTypes);
126  // indivObservations[ti][agI] will contain the observation agI received at tI+1
127  vector< vector<Index> > indivObservations(ts,vector<Index>(
128  pu->GetNrAgents() ) );
129  for(Index agentI=0; agentI < pu->GetNrAgents(); agentI++)
130  {
131  indOHI[agentI] += firstOHtsI[agentI];
132  Index obsArr[ts];
133  pu->GetObservationHistoryArrays(agentI, indOHI[agentI], ts, obsArr);
134  //now obsArr is filled and can be copied into indivObservations
135  for(Index tI=0; tI < ts; tI++)
136  indivObservations.at(tI).at(agentI) = obsArr[tI];
137  }
138 
139  for(Index tI=0; tI < ts; tI++)
140  joI_arr[tI] = pu->IndividualToJointObservationIndices(
141  indivObservations[tI] );
142 
143 /* old code - dependent on generation of individual observation histories.
144  //convert indiv type indices to ind. observation history indices:
145  vector<Index> indOHI = vector<Index>(indTypes);
146  vector<const ObservationHistory*> indOH;
147  for(Index agentI=0; agentI < GetNrAgents(); agentI++)
148  {
149  indOHI[agentI] += firstOHtsI[agentI];
150  const ObservationHistoryTree* oht =
151  _m_pu->GetObservationHistoryTree(agentI, indOHI[agentI]);
152  const ObservationHistory* oh = oht->GetObservationHistory();
153  indOH.push_back(oh);
154  }
155  //convert the vector of ind. observation history indices to a array of
156  //joint observations
157  for(int tI=ts-1; tI >= 0; tI--)
158  {
159  vector<Index> indivObservationsTI;
160  for(Index agentI=0; agentI < GetNrAgents(); agentI++)
161  {
162  indivObservationsTI.push_back( indOH[agentI]->
163  GetLastObservationIndex() );
164  indOH[agentI] = indOH[agentI]->GetPredecessor();
165  }
166  joI_arr[tI] = _m_pu->IndividualToJointObservationIndices(
167  indivObservationsTI);
168  }
169 */
170 }
171 
172 /*
173 //compute the joint actions taken bu jpolPrevTs when joht is the true joint
174 //observation history at stage ts.
175 void BGforStageCreation::Fill_jaI_Array_OLD(
176  Index ts,
177  const JointObservationHistoryTree* joht,
178  const JointPolicyDiscretePure* jpolPrevTs,
179  Index* jaI_arr
180  )
181 {
182  if(! (ts >= 1) ) // to guarantee that t2 > 0
183  { //ts == 0, we're constructing the first time-step BG...
184  ; //TODO:check if we need to do something special here(?)
185  }
186  else // ts > 1
187  {
188  Index t2 = ts;// t2 > 0
189  do
190  {
191  t2--; //in first iter. t2 = ts-1 > 0 (because ts > 1, see above)
192  joht = joht->GetPredecessor();
193  Index johI = joht->GetIndex();
194  jaI_arr[t2] = jpolPrevTs->GetJointActionIndex(johI);
195  }
196  while(t2 > 0);
197  }
198  //t2 == 0
199 }
200 */
201 
202 //compute the joint actions taken bu jpolPrevTs when joIs is the true joint
203 //observation history at stage ts.
205  const PlanningUnitMADPDiscrete* pu,
206  Index ts,
207  Index joIs[], //the array of joint observations issued
208  const JointPolicyDiscretePure* jpolPrevTs,
209  Index* jaI_arr
210  )
211 {
212  Index johI = 0;
213  Index t = 0;
214  while(t < ts)
215  {
216  Index ja = jpolPrevTs->GetJointActionIndex(johI);
217  jaI_arr[t] = ja;
218 
219  Index next_joI = joIs[t];
220  johI = pu->GetSuccessorJOHI(johI, next_joI);
221  t++;
222  }
223 }
224 
225 /*
226 const JointObservationHistoryTree* BGforStageCreation::
227 Get_joht(PlanningUnitMADPDiscrete* pu, const Index ts, const Index* joI_arr)
228 {
229  JointObservationHistoryTree* joht =
230  pu->GetJointObservationHistoryTree(0);
231  Index tI = 0;
232  while(tI < ts)
233  {
234  joht = joht->GetSuccessor(joI_arr[tI]);
235  tI++;
236  }
237  return(joht);
238 
239 }
240 */
241