MultiAgentDecisionProcess  Release 0.2.1
PerseusBGPlanner.cpp
Go to the documentation of this file.
1 
28 #include "PerseusBGPlanner.h"
29 #include "BeliefValue.h"
30 #include <float.h>
31 #include <fstream>
32 #include <limits.h>
33 
34 using namespace std;
35 
36 #define DEBUG_PerseusBGPlanner 0
37 #define PerseusBGPlanner_alternativeSampling 0
38 
39 //Default constructor
41  const QAVParameters& params) :
43  AlphaVectorBG(pu),
45  _m_backupType(params.bgBackupType)
46 {
49 }
50 
53  AlphaVectorBG(pu),
55  _m_backupType(BGIP_SOLVER_EXHAUSTIVE)
56 {
59 }
60 
61 //Destructor
63 {
64 }
65 
66 
69  const QFunctionsDiscrete &Q) const
70 {
72  return(BackupStageAll(S,Q));
73  else
74  {
75 #if PerseusBGPlanner_alternativeSampling
76  return(BackupStageSamplingAlt(S,Q));
77 #else
78  return(BackupStageSampling(S,Q));
79 #endif
80  }
81 }
82 
83 
86  const QFunctionsDiscrete &Q) const
87 {
88  vector<double> VB=BeliefValue::GetValues(S,Q),
89  VBalpha;
90 
91  int nrB=VB.size(),
92  nrS=GetPU()->GetNrStates();
93  QFunctionsDiscrete Q1(Q.size());
94  AlphaVector alpha(nrS);
95 
98 
100 
101  for(int k=0;k<nrB;++k)
102  {
103  Qalphas.clear();
104  for(unsigned int a=0;a!=GetPU()->GetNrJointActions();++a)
105  {
106  // backup the belief
107  alpha=BeliefBackup(*S[k],a,Gao,V,_m_backupType);
108 
109  // add alpha to Q1
110  if(!VectorIsInValueFunction(alpha,Q1[a]))
111  Q1[a].push_back(alpha);
112  Qalphas.push_back(alpha);
113  }
114 
115  if(GetVerbose())
116  {
117  VBalpha=BeliefValue::GetValues(S,Qalphas);
118  cout << "Added vectors for " << k << " (V " << VBalpha[k]
119  << ")" << endl;
120  }
121  }
122 
123  BackupStageLeadOut(Gao);
124 
125  return(Q1);
126 }
127 
130  const QFunctionsDiscrete &Q) const
131 {
132 
133  vector<double> VB=BeliefValue::GetValues(S,Q),
134  VBalpha;
135  vector<vector<double> > QB;
136  for(unsigned int a=0;a!=Q.size();++a)
137  QB.push_back(BeliefValue::GetValues(S,Q[a]));
138 
139  int nrB=VB.size(),
140  nrNotImproved=nrB,
141  nrS=GetPU()->GetNrStates(),
142  k;
143  double oldValue;
144  vector<bool> stillNeedToBeImproved(nrB,true);
145  QFunctionsDiscrete Q1(Q.size());
146  AlphaVector alpha(nrS);
147 
150 
152 
153  while(nrNotImproved!=0)
154  {
155  // sample a belief index from the number of not improved beliefs
156  k=SampleNotImprovedBeliefIndex(stillNeedToBeImproved,nrNotImproved);
157 
158  Qalphas.clear();
159  for(unsigned int a=0;a!=GetPU()->GetNrJointActions();++a)
160  {
161  // backup the belief
162  alpha=BeliefBackup(*S[k],a,Gao,V,_m_backupType);
163 
164  // check whether alpha improves the value of S[k]
165  double x=S[k]->InnerProduct(alpha.GetValues());
166 
167  // if not, get copy from old value function
168  oldValue=QB[a][k];//VB[k]
169  if(x<oldValue)
170  {
171 #if DEBUG_PerseusBGPlanner
172  cout << "Getting n-1 vector for action " << a << ", belief "
173  << k << " (" << x << " < " <<oldValue << ")" << endl;
174 #endif
175  alpha=BeliefValue::GetMaximizingVector(S,k,Q[a]);
176  }
177  else
178  {
179  if(GetVerbose())
180  cout << "Added vector for action " << a << ", belief "
181  << k << " (Q " << x << " >= " << oldValue << ")"
182  << endl;
183  }
184  // add alpha to Q1
185  if(!VectorIsInValueFunction(alpha,Q1[a]))
186  Q1[a].push_back(alpha);
187  Qalphas.push_back(alpha);
188  }
189 
190  // update which beliefs have been improved
191  VBalpha=BeliefValue::GetValues(S,Qalphas);
192  int nrImprovedByAlpha=0;
193  for(int b=0;b!=nrB;b++)
194  if(stillNeedToBeImproved[b] && VBalpha[b]>=VB[b])
195  {
196  stillNeedToBeImproved[b]=false;
197  nrNotImproved--;
198  nrImprovedByAlpha++;
199  }
200 
201  if(GetVerbose())
202  cout << "Added vectors for " << k << " (V " << VBalpha[k]
203  << " improved " << nrImprovedByAlpha << ")" << endl;
204  }
205 
206  BackupStageLeadOut(Gao);
207 
208  return(Q1);
209 }
210 
213  const QFunctionsDiscrete &Q) const
214 {
215  vector<double> VB=BeliefValue::GetValues(S,Q),
216  VBalpha;
217 
218  int nrB=VB.size(),
219  nrNotImproved=nrB,
220  nrS=GetPU()->GetNrStates(),
221  k;
222  vector<bool> stillNeedToBeImproved(nrB,true);
223  QFunctionsDiscrete Q1(Q.size());
224  AlphaVector alpha(nrS);
225 
228 
230 
231  k=-1;
232 
233  while(nrNotImproved!=0)
234  {
235  Qalphas.clear();
236  for(unsigned int a=0;a!=GetPU()->GetNrJointActions();++a)
237  {
238  // sample a belief index from the number of not improved beliefs
239  k=SampleNotImprovedBeliefIndex(stillNeedToBeImproved,nrNotImproved);
240  // backup the belief
241  alpha=BeliefBackup(*S[k],a,Gao,V,_m_backupType);
242 
243  // check whether alpha improves the value of S[k]
244  double x=S[k]->InnerProduct(alpha.GetValues());
245  // if not, get copy from old value function
246  if(x<VB[k]) //QB[a][k])//VB[k])
247  {
248 #if DEBUG_PerseusBGPlanner
249  cout << "Getting n-1 vector for action " << a << ", belief "
250  << k << " (" << x << " < " << VB[k] << ")" << endl;
251 #endif
252  alpha=BeliefValue::GetMaximizingVector(S,k,Q[a]);
253  }
254  else
255  {
256  if(GetVerbose())
257  cout << "Added vector for action " << a << ", belief "
258  << k << " (Q " << x << " >= " << VB[k] << ")"
259  << endl;
260  }
261 
262  // add alpha to Q1
263  if(!VectorIsInValueFunction(alpha,Q1[a]))
264  Q1[a].push_back(alpha);
265  Qalphas.push_back(alpha);
266  }
267 
268  // update which beliefs have been improved
269  VBalpha=BeliefValue::GetValues(S,Qalphas);
270  int nrImprovedByAlpha=0;
271  for(int b=0;b!=nrB;b++)
272  if(stillNeedToBeImproved[b] && VBalpha[b]>=VB[b])
273  {
274  stillNeedToBeImproved[b]=false;
275  nrNotImproved--;
276  nrImprovedByAlpha++;
277  }
278 
279  if(GetVerbose())
280  cout << "Added vectors for " << k << " (V " << VBalpha[k]
281  << " improved " << nrImprovedByAlpha << ")" << endl;
282  }
283 
284  BackupStageLeadOut(Gao);
285 
286  return(Q1);
287 }