MultiAgentDecisionProcess  Release 0.2.1
BayesianGameBase.cpp
Go to the documentation of this file.
1 
28 #include "BayesianGameBase.h"
29 #include "E.h"
30 
31 using namespace std;
32 
33 //Default constructor
35  : _m_verboseness(0)
36 {
37  cerr << "Warning BayesianGameBase called without arguments"<< endl;
38  _m_initialized=false;
39  _m_nrAgents = 0;
40  _m_nrActions.clear();
41  _m_nrTypes.clear();
42  _m_nrJTypes = 0;
43  _m_nrJA = 0;
44  _m_jTypeProbs.clear();
46  _m_stepSizeTypes = 0;
48 }
49 
51  const size_t nrAgents,
52  const vector<size_t> & nrActions,
53  const vector<size_t> & nrTypes,
54  int verb)
55  : _m_verboseness(verb)
56 {
57  if( nrActions.size() != nrAgents || nrTypes.size() != nrAgents)
58  throw E("Dimension mismatches in creating a Bayesian game");
59 
60  _m_nrAgents = nrAgents;
61  _m_nrActions = nrActions;
62  _m_nrTypes = nrTypes;
65 
66  //calculate # joint types and actions
67  _m_nrJTypes = 1;
68  _m_nrJA = 1;
69  for(Index i = 0; i < nrAgents; i++)
70  {
71  _m_nrJTypes *= _m_nrTypes[i];
72  _m_nrJA *= _m_nrActions[i];
73  }
74 
75  if(_m_nrJTypes>1e6)
76  {
77  cout << "BayesianGameBase: using sparse models" << endl;
78  _m_useSparse=true;
79  }
80  else
81  {
82  _m_useSparse=false;
83  try {
84  _m_jTypeProbs = vector<double>(_m_nrJTypes, 0.0);
85  _m_jointToIndTypes = new vector<vector<Index> >(_m_nrJTypes);
86  } catch(std::bad_alloc)
87  {
88  cout << "BayesianGameBase: too many joint types ("
89  << _m_nrJTypes
90  << ") to have a full vector in memory, switching to sparse models..." << endl;
91  _m_useSparse=true;
92  }
93  }
94 
95  //initialize the probability distribution over joint types:
96  if(_m_useSparse)
97  {
99  _m_jTypeProbsSparse.clear();
100  _m_jointToIndTypesMap = new map<Index, vector<Index> >();
102  }
103  else
104  {
106  }
107 
108  _m_initialized=true;
109 }
110 
111 //Copy assignment constructor.
113 {
114  throw E("trying to copy construct a BayesianGameBase - not implemented. (is this necessary?)");
115 }
116 
117 //Destructor
119 {
120  delete [] _m_stepSizeTypes;
121  delete [] _m_stepSizeActions;
122 }
123 
125 {
126  //TODO implement checks
127  _m_initialized = b;
128  return(true);
129 }
130 
132 {
133  stringstream ss;
134  ss << "Bayesian game with "<<_m_nrAgents<<" agents"<<endl;
135  ss << "Number of actions ";
137  ss << " ("<< _m_nrJA <<" joint actions)";
138  ss << "\nNumber of types ";
140  ss << " ("<< _m_nrJTypes <<" joint types)"<<endl;
141  ss << "joint type probs: ";
143  ss << endl;
144  return(ss.str());
145 }
146 
148  Index typeIndex,
150 {
151  if(cat != PolicyGlobals::TYPE_INDEX)
152  throw E("BGs only work with types as the policy domain");
153  stringstream ss;
154  ss << "BGType"<<typeIndex;
155  return(ss.str());
156 }
157 string BayesianGameBase::SoftPrintAction(Index agentI, Index actionI) const
158 {
159  stringstream ss;
160  ss << "BGAction"<<actionI;
161  return(ss.str());
162 }
163 void BayesianGameBase::PrintPolicyDomain(Index agentI, Index typeIndex) const
164 {
165  throw E("BayesianGameBase::PrintPolicyDomainNot (yet? deprecate?) implemented");
166 }
167 void BayesianGameBase::PrintAction(Index agentI, Index actionI) const
168 {
169  throw E("BayesianGameBase::PrintAction(Not (yet? deprecate?) implemented");
170 }
171 
173  const PolicyGlobals::IndexDomainCategory pdc) const
174 {
175  switch ( pdc )
176  {
179  break;
182  break;
185  break;
186  default:
187  throw(E("Used a JointPolicyDiscrete::PolicyGlobals::IndexDomainCategory unknown to BayesianGameBase!"));
188  break;
189  } /* ----- end switch ----- */
190 };
191 
193  const
194 {
196 }
197 
199  Index agentI,
201  size_t depth) const
202 {
203 #if 0
204  if(depth!=MAXHORIZON)
205  throw(E("BayesianGameBase::GetNrPolicyDomainElements depth argument not supported for BGs"));
206 #endif
207  return _m_nrTypes[agentI];
208 }
209 
210 bool
212 {
213  double p=0.0;
214  for(Index jtI=0; jtI < _m_nrJTypes; jtI++)
215  p += GetProbability(jtI);
216  if(! Globals::EqualProbability(p, 1.0) )
217  {
218  stringstream ss;
219  ss << "Warning, total probability of joint types does not sum to 1.0, "
220  << "but to "<<p<<"!";
221  throw(E(ss));
222  }
223  return true;
224 }
225 
227 {
228  double psum = 0.0;
229  for(Index jtI=0; jtI < _m_jTypeProbs.size(); jtI++)
230  psum += GetProbability(jtI);
231 
232  if(!Globals::EqualProbability(psum, 1.0))
233  cout << "Warning! - BayesianGameBase::SanityCheck does not sum to 1.0" << endl;
234  if( abs(psum - 1.0) > 1e-12 )
235  cout << "Warning - - BayesianGameBase::SanityCheck could perhaps use renormalization?"<<endl;
236 
237 }