MultiAgentDecisionProcess  Release 0.2.1
Perseus.h
Go to the documentation of this file.
1 
28 /* Only include this header file once. */
29 #ifndef _PERSEUS_H_
30 #define _PERSEUS_H_ 1
31 
32 /* the include directives */
33 #include <iostream>
34 #include <iomanip>
35 #include "Globals.h"
36 
37 #include "BeliefValue.h"
38 #include "AlphaVectorPlanning.h"
39 #include "directories.h"
41 #include "QAVParameters.h"
42 #include "argumentHandlers.h"
43 
44 class BeliefSetNonStationary;
45 
47 class Perseus :
48  virtual public AlphaVectorPlanning
49 {
50 private:
51 
53 
56 
58 
61 
62  size_t GetSize(const ValueFunctionPOMDPDiscrete &V) const
63  { return(V.size()); }
64  size_t GetSize(const QFunctionsDiscrete &Q) const
65  {
66  size_t nr=0;
67  for(QFDcit i=Q.begin();i!=Q.end();++i)
68  nr+=i->size();
69  return(nr);
70  }
72  {
73  size_t nr=0;
74  for(QFDNScit i=Q.begin();i!=Q.end();++i)
75  nr+=GetSize(*i);
76  return(nr);
77  }
78 
79 protected:
80 
81  double _m_bestValue;
82 
85 
87 
89 
91 
93 
95 
97 
99 
100  bool _m_dryrun;
101 
102  template <class VF>
103  void PlanStartOfIteration(int iter,
104  const std::vector<double> &VB,
105  const VF &V) const
106  {
107  double x=0;
108  for(unsigned int i=0;i!=VB.size();i++)
109  x+=VB[i];
111  if(GetVerbose() >= -1)
112  std::cout
113  << GetIdentification() << ": iteration " << std::setw(6)
114  << iter
115  << " |V| " << GetSize(V)
116  << " sumV/nrB " << x/VB.size() << " V0 "
117  << BeliefValue::GetValue( *jb0, V)
118  << " (best " << _m_bestValue << ")" << std::endl;
119  delete jb0;
120 
122  {
123  std::string resultsDir=directories::MADPGetResultsDir("POMDP",
124  _m_pu);
125  std::stringstream valueFunctionFilename;
126  valueFunctionFilename << resultsDir << "/intermediate/"
127  << GetIdentification()
128  << "ValueFunction_h"
129  << _m_pu->GetHorizon() << "_iter_"
130  << std::setw(4) << std::setfill('0')
131  << iter;
133  ExportValueFunction(valueFunctionFilename.str(),V);
134  }
135  }
136 
137  template <class VF>
138  void PlanEndOfIteration(const VF &V)
139  {
141 
143  double value=BeliefValue::GetValue(*jb0, V);
144  delete jb0;
145 
146  if(value>_m_bestValue)
147  {
148  _m_bestValue=value;
150  if(!_m_dryrun)
152  }
153  }
154 
155  void PlanEndOfIteration() const;
156 
158  void PrintMaxRewardInBeliefSet() const;
159 
160  void PlanLeadIn();
161 
162  void PlanLeadOut();
163 
165 
166  void BackupStageLeadOut(GaoVectorSet Gao) const;
167 
169 
170  virtual std::vector<double> GetImmediateRewardBeliefSet() const = 0;
171 
172  virtual void InitializeBeliefs(int nrB, bool uniquify) = 0;
173 
174  virtual void StoreValueFunction(const ValueFunctionPOMDPDiscrete &V);
175  virtual void StoreValueFunction(const QFunctionsDiscrete &Q);
177 
178 public:
179  // Constructor, destructor and copy assignment.
183  virtual ~Perseus();
184 
185  void Initialize();
186 
189  virtual void ExportValueFunction(const std::string &filename) const = 0;
190 
191  virtual double GetQ(const JointBeliefInterface &b, Index jaI) const = 0;
192 
193  virtual double GetQ(const JointBeliefInterface &b, Index jaI,
194  int &betaMaxI) const = 0;
195 
196  virtual double GetQ(const JointBeliefInterface &b, Index t,
197  Index jaI) const = 0;
198 
199  virtual double GetQ(const JointBeliefInterface &b, Index t, Index jaI,
200  int &betaMaxI) const = 0;
201 
202  virtual void SetValueFunction(const std::string &filename) = 0;
203 
205  int SampleNotImprovedBeliefIndex(std::vector<bool> stillNeedToBeImproved,
206  int nrNotImproved) const;
207 
208  bool CheckConvergence(const std::vector<double> &VB,
209  const std::vector<double> &VBnew,
210  int iter) const;
211 
212  virtual void Plan() = 0;
213 
216 
217  void SetSaveTimings(bool save)
218  { _m_storeTimings=save; }
219 
220  void SetIdentification(std::string identification);
221 
222  void SetResultsFilename(std::string filename);
223 
224  std::string GetIdentification() const { return(_m_identification); }
225 
226  static std::string BackupTypeToString(const QAVParameters &params);
227 
229  &args);
230 
231  void SetComputeVectorForEachBelief(bool compute)
232  { _m_computeVectorForEachBelief = compute; }
233 
238 
239  void SetInitializeWithImmediateReward(bool initReward)
240  { _m_initializeWithImmediateReward=initReward; }
241 
242  void SetInitializeWithZero(bool initZero)
243  { _m_initializeWithZero=initZero; }
244 
245  void SetDryrun(bool dryrun)
246  { _m_dryrun=dryrun; }
247  void SetVerbose(int verbose) { _m_verbose=verbose; }
248  int GetVerbose() const { return(_m_verbose); }
249 
250 };
251 
252 #endif /* !_PERSEUS_H_ */
253 
254 // Local Variables: ***
255 // mode:c++ ***
256 // End: ***