MultiAgentDecisionProcess  Release 0.2.1
PerseusPOMDPPlanner.cpp
Go to the documentation of this file.
1 
28 #include "PerseusPOMDPPlanner.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_PerseusPOMDPPlanner 0
37 
38 //Default constructor
42  AlphaVectorPOMDP(pu),
44 {
47 }
48 
49 //Destructor
51 {
52 }
53 
55 {
56  PlanLeadIn();
57 
59  vector<double> VB,VBnew;
60 
61  // get initial value function
64 
65  int iter=0;
66  bool done=false;
67  while(!done)
68  {
69  // print out some info
70  PlanStartOfIteration(iter,VB,V1);
71 
72  // the real thing: compute the next stage value function
73  V0=V1;
74  V1=BackupStage(*_m_beliefs,V0);
75 
76  // kind of hack to fix a problem of an persisting initial
77  // alpha vector (which has the INT_MAX action)
78  if(V1.size()>1)
79  {
80  ValueFunctionPOMDPDiscrete::iterator it=V1.begin();
81  while(it!=V1.end())
82  {
83  if(it->GetAction()==INT_MAX)
84  {
85  V1.erase(it);
86  break;
87  }
88  it++;
89  }
90  }
91 
92  // compute the maximum difference in the values for all
93  // beliefs: for the convergence test
95 
96  // test for convergence
97  if(CheckConvergence(VB,VBnew,iter))
98  done=true;
99 
100  VB=VBnew;
101  iter++;
102 
103  PlanEndOfIteration(V1);
104  }
105 
106  PlanLeadOut();
107 }
108 
112 {
113  vector<double> VB=BeliefValue::GetValues(S,V),
114  VBalpha;
115  int nrB=VB.size(),
116  nrNotImproved=nrB,
117  nrS=GetPU()->GetNrStates(),
118  k;
119  vector<bool> stillNeedToBeImproved(nrB,true);
121  AlphaVector alpha(nrS);
122 
124 
126  k=-1;
127 
128  while(nrNotImproved!=0)
129  {
131  k++;
132  else // sample a belief index from the number of not improved beliefs
133  k=SampleNotImprovedBeliefIndex(stillNeedToBeImproved,nrNotImproved);
134 
135  alpha=BeliefBackup(*S[k],Gao);
136 
138  {
139  // check whether alpha improves the value of S[k]
140  double x=S[k]->InnerProduct(alpha.GetValues());
141  // if not, get copy from old value function
142  if(x<VB[k])
143  {
145 
146 #if DEBUG_PerseusPOMDPPlanner
147  cout << "Getting n-1 vector for " << k << endl;
148 #endif
149  }
150  }
151 
152  // add alpha to V1
153  V1.push_back(alpha);
154 
156  nrNotImproved--;
157  else
158  {
159  // update which beliefs have been improved
160  VBalpha=BeliefValue::GetValues(S,alpha);
161  int nrImprovedByAlpha=0;
162  for(int b=0;b!=nrB;b++)
163  if(stillNeedToBeImproved[b] && VBalpha[b]>=VB[b])
164  {
165  stillNeedToBeImproved[b]=false;
166  nrNotImproved--;
167  nrImprovedByAlpha++;
168  }
169 
170  if(GetVerbose() >= 0)
171  cout << "Added vector for " << k << " (V " << VBalpha[k]
172  << " improved " << nrImprovedByAlpha << ")" << endl;
173 #if DEBUG_PerseusPOMDPPlanner
174  alpha.Print();
175  if(nrImprovedByAlpha==0)
176  abort();
177 #endif
178  }
179  }
180 
181  BackupStageLeadOut(Gao);
182 
183  return(V1);
184 }