MultiAgentDecisionProcess  Release 0.2.1
BayesianGameBase.h
Go to the documentation of this file.
1 
28 /* Only include this header file once. */
29 #ifndef _BAYESIANGAMEBASE_H_
30 #define _BAYESIANGAMEBASE_H_ 1
31 
32 /* the include directives */
33 #include <iostream>
34 #include <vector>
35 #include <map>
36 #include "Globals.h"
38 #include "IndexTools.h"
39 #include <boost/numeric/ublas/vector.hpp>
40 #include <boost/numeric/ublas/vector_sparse.hpp>
41 
52  public Interface_ProblemToPolicyDiscretePure //this class implements the interface
53  //used by PolicyPureVector
54 {
55  private:
56 
60  std::vector<double> _m_jTypeProbs;
61  typedef boost::numeric::ublas::mapped_vector<double> SparseVector;
63 
65  std::vector< std::vector<Index> > *_m_jointToIndTypes;
67  std::map<Index, std::vector<Index> > *_m_jointToIndTypesMap;
68 
69  protected:
72 
75 
77  size_t _m_nrAgents;
79  std::vector<size_t> _m_nrActions;
81  std::vector<size_t> _m_nrTypes;
83  size_t _m_nrJTypes;
85  size_t _m_nrJA;
86 
90  size_t * _m_stepSizeTypes;
91 
92  public:
93  // Constructor, destructor and copy assignment.
94  // (default) Constructor
96  BayesianGameBase(const size_t nrAgents,
97  const std::vector<size_t> & nrActions,
98  const std::vector<size_t> & nrTypes,
99  int verboseness=0);
104  //operators:
105 
106 
107  //data manipulation (set) functions:
108 
111  bool SetInitialized(bool b);
113  void SetProbability(const Index i, const double p)
115  else _m_jTypeProbs[i]=p;}
118  void SetProbability(const std::vector<Index>& indIndices, const double p)
121  void AddProbability(const Index i, const double p)
123  else _m_jTypeProbs[i]+=p;}
126  void AddProbability(const std::vector<Index>& indIndices, const double p)
128 
129 
130  //get (data) functions:
131  size_t GetNrJointActions() const {return _m_nrJA;};
132  size_t GetNrJointTypes() const {return _m_nrJTypes;};
133  const std::vector<size_t>& GetNrTypes() const {return _m_nrTypes;}
134  size_t GetNrTypes(Index agI) const {return _m_nrTypes.at(agI);}
135 
137  virtual double GetProbability(const Index i) const
138  {if (_m_useSparse) return(_m_jTypeProbsSparse[i]);
139  else return(_m_jTypeProbs[i]);}
142  double GetProbability(const std::vector<Index>& indIndices)
143  {return(GetProbability(IndividualToJointTypeIndices(indIndices)));}
144 
145 
146  //indices
147 
148  //\todo TODO: Bayesian game caches the step_size etc. so the following
149  //should be changed? TODO: <--- check this
153  Index IndividualToJointActionIndices(const std::vector<Index>& indices)
154  const
157 
158  Index IndividualToJointTypeIndices(const std::vector<Index>& indices) const
160  _m_stepSizeTypes);};
161  std::vector<Index> JointToIndividualActionIndices(Index jaI) const
164 /* std::vector<Index> JointToIndividualTypeIndices(Index jTypeI) const
165  { return IndexTools::JointToIndividualIndicesStepSize(jTypeI,
166  _m_stepSizeTypes, _m_nrAgents);};
167 */
168  const std::vector<Index>& JointToIndividualTypeIndices(Index jTypeI) const
169  {
170  if(_m_useSparse)
171  {
172  if((*_m_jointToIndTypesMap).find(jTypeI)==
173  (*_m_jointToIndTypesMap).end())
174  (*_m_jointToIndTypesMap)[jTypeI]=
176  jTypeI, _m_nrTypes);
177  return((*_m_jointToIndTypesMap)[jTypeI]);
178 
179  }
180  else
181  {
182  if((*_m_jointToIndTypes)[jTypeI].size()==0)
183  (*_m_jointToIndTypes)[jTypeI]=
185  jTypeI, _m_nrTypes);
186  return((*_m_jointToIndTypes)[jTypeI]);
187  }
188  }
189 
190 
191 
193  std::string SoftPrint() const;
195  void Print() const
196  {std::cout << SoftPrint();}
197 
198  bool SanityCheck() const;
199 
201  //\todo TODO adapt
202  size_t GetNrAgents() const
203  {return _m_nrAgents;}
204  size_t GetNrActions(Index agentI) const
205  {return _m_nrActions[agentI];}
206  const std::vector<size_t>& GetNrActions() const
207  {return _m_nrActions;}
209  Index agentI,
211  size_t depth=MAXHORIZON) const;
218  //Index GetJointActionIndex(std::vector<Index>& indivIndices) const
219  // {return IndividualToJointActionIndices(indivIndices);}
220 
225  std::vector<Index> JointToIndividualPolicyDomainIndices
227  {
228  if(cat != PolicyGlobals::TYPE_INDEX)
229  throw(E("BG only supports type as the domain of the policy"));
230  return(JointToIndividualTypeIndices(jdI));
231  }
236  const std::vector<Index>& JointToIndividualPolicyDomainIndicesRef
238  {
239 #if 0 // disable check for speed
240  if(cat != PolicyGlobals::TYPE_INDEX)
241  throw(E("BG only supports type as the domain of the policy"));
242 #endif
243 // return( _m_jointToIndTypes[jdI]);
244  return(JointToIndividualTypeIndices(jdI));
245  }
246 
247  // A BG has types, not (A)OH histories.
248  // (and the joint -> individual type conversions are cached)
249  bool CacheJointToIndivType_Indices() const {return true;};
250  bool CacheJointToIndivOH_Indices() const {return false;};
251  bool CacheJointToIndivAOH_Indices() const {return false;};
252  virtual bool AreCachedJointToIndivIndices(
253  const PolicyGlobals::IndexDomainCategory pdc) const;
254 
257  std::string SoftPrintPolicyDomainElement(Index agentI, Index typeIndex,
261  std::string SoftPrintAction(Index agentI, Index actionI) const;
264  void PrintPolicyDomain(Index agentI, Index typeIndex) const;
267  void PrintAction(Index agentI, Index actionI) const;
268 
269  /*\brief the default IndexDomainCategory for the planning unit.
270  *
271  * as specified by Interface_ProblemToPolicyDiscrete. This can be
272  * overriden in derived classes.
273  */
275  const;
276 
277  void SanityCheckBGBase();
278 
279  virtual void SanityCheck()
280  { SanityCheckBGBase(); }
281 };
282 
283 #endif /* !_BAYESIANGAMEBASE_H_ */
284 
285 // Local Variables: ***
286 // mode:c++ ***
287 // End: ***