MultiAgentDecisionProcess  Release 0.2.1
MultiAgentDecisionProcessDiscrete.cpp
Go to the documentation of this file.
1 
29 #include "ObservationModelMapping.h"
31 #include "TransitionModelMapping.h"
33 #include <stdio.h>
34 
35 #include "TGet.h"
36 #include "OGet.h"
37 
38 
39 using namespace std;
40 
41 #define DEBUG_MADPD 0
42 
44 {
45  _m_initialized = false;
46  _m_sparse = false;
47  _m_p_tModel = 0;
48  _m_p_oModel = 0;
49 }
51  string name, string descr, string pf) :
52  MultiAgentDecisionProcess(name, descr, pf),
56 {
57  _m_initialized = false;
58  _m_sparse = false;
59  _m_p_tModel = 0;
60  _m_p_oModel = 0;
61 }
62 
63 
65  nrAgents, int nrS, string name, string descr, string pf) :
66  MultiAgentDecisionProcess(nrAgents,name, descr, pf),
70 {
71  _m_initialized = false;
72  _m_sparse = false;
73  _m_p_tModel = 0;
74  _m_p_oModel = 0;
75 }
76 
78 {
79  if(DEBUG_MADPD)
80  cout << "deleting MultiAgentDecisionProcessDiscrete "
81  << "(deleting T and O model )"<<endl;
82  delete (_m_p_tModel);
83  delete (_m_p_oModel);
84 }
85 
87 {
88  if(_m_initialized)
89  delete(_m_p_tModel);
90 
91  if(_m_sparse)
94  else
97 
98 }
99 
101 {
102  if(_m_sparse)
105  else
106  return new TGet_TransitionModelMapping(
108 }
109 
111 {
112  if(_m_sparse)
115  else
116  return new OGet_ObservationModelMapping(
118 }
119 
121 {
122  if(_m_initialized)
123  delete(_m_p_oModel);
124 
125  if(_m_sparse)
126  _m_p_oModel = new
130  else
134 }
135 
136 
138 {
139 
140  if(b == true)
141  {
145  {
146  //error in initialization of sub-components.
147  _m_initialized = false;
148  return(false);
149  }
150  //check if transition- and observation model are present...
151  if(_m_p_tModel == 0)
152  {
153  throw E("MultiAgentDecisionProcessDiscrete::SetInitialized() -initializing a MultiAgentDecisionProcessDiscrete which has no transition model! - make sure that CreateNewTransitionModel() has been called before SetInitialized()");
154  }
155  if(_m_p_oModel == 0)
156  {
157  throw E("MultiAgentDecisionProcessDiscrete::SetInitialized() -initializing a MultiAgentDecisionProcessDiscrete which has no observation model! - make sure that CreateNewObservationModel() has been called before SetInitialized()");
158 
159  }
160 
161  if( SanityCheck() )
162  {
163  _m_initialized = true;
164  return(true);
165  }
166  else
167  {
168  _m_initialized = false;
169  return(false);
170  }
171  }
172  else
173  {
177  _m_initialized = false;
178  return(true);
179  }
180 
181 }
182 
183 
185 {
186  stringstream ss;
187 
192 
193  if(_m_initialized)
194  {
195  ss << "Transition model: " << endl;
196  ss << _m_p_tModel->SoftPrint();
197  ss << "Observation model: " << endl;
198  ss << _m_p_oModel->SoftPrint();
199  }
200  return(ss.str());
201 }
202 
204 {
205  size_t nrJA=GetNrJointActions(),
206  nrS=GetNrStates(),
207  nrJO=GetNrJointObservations();
208 
209  double sum,p;
210  bool sane=true;
211 
212  // check transition model
213  for(Index a=0;a<nrJA;a++)
214  {
215  for(Index from=0;from<nrS;from++)
216  {
217  sum=0.0;
218  for(Index to=0;to<nrS;to++)
219  {
220  p=GetTransitionProbability(from,a,to);
221  if(p<0)
222  {
223  sane=false;
224  stringstream ss;
225  ss << "MultiAgentDecisionProcessDiscrete::SanityCheck "<<
226  "failed: negative probability " << p <<
227  " for p(s'|s,a)==p(" << GetStateName(to) << "|" <<
228  GetStateName(from)<<"," << GetJointActionName(a) << ")";
229  throw E(ss);
230  }
231  if(isnan(p))
232  {
233  sane=false;
234  stringstream ss;
235  ss << "MultiAgentDecisionProcessDiscrete::SanityCheck "<<
236  "failed: NaN " <<
237  " for p(s'|s,a)==p(" << GetStateName(to) << "|" <<
238  GetStateName(from)<<"," << GetJointActionName(a) << ")";
239  throw E(ss);
240  }
241  sum+=p;
242  }
243  if((sum>(1.0 + PROB_PRECISION/2)) ||
244  (sum < (1.0 - PROB_PRECISION/2)))
245  {
246  sane=false;
247  stringstream ss;
248  //string float_str;
249  char float_str[30];
250  sprintf(float_str, "%10.10f", sum);
251  ss << "MultiAgentDecisionProcessDiscrete::SanityCheck failed:"<<
252  " transition does not sum to 1 but to:\n" << float_str <<
253  "\n for (s,a)==(" << GetStateName(from) << "[" << from <<
254  "]," << GetJointActionName(a) << "[" << a << "])";
255  throw E(ss);
256  }
257  }
258  }
259 
260  // check observation model
261  for(Index a=0;a<nrJA;a++)
262  {
263  for(Index to=0;to<nrS;to++)
264  {
265  sum=0;
266  for(Index o=0;o<nrJO;o++)
267  {
268  p=GetObservationProbability(a,to,o);
269  if(p<0)
270  {
271  stringstream ss;
272  ss << "MultiAgentDecisionProcessDiscrete::SanityCheck "
273  << "failed: negative probability " << p
274  << " for p(o|s',a)==p("
276  << "|" << GetStateName(to)
277  << ","
278  << GetJointActionName(a)
279  << ")";
280  throw E(ss);
281  }
282  if(isnan(p))
283  {
284  stringstream ss;
285  ss << "MultiAgentDecisionProcessDiscrete::SanityCheck "
286  << "failed: NaN for p(o|s',a)==p("
288  << "|" << GetStateName(to)
289  << ","
290  << GetJointActionName(a)
291  << ")";
292  throw E(ss);
293  }
294  sum+=p;
295  }
296  if((sum>(1.0 + PROB_PRECISION/2)) ||
297  (sum < (1.0 - PROB_PRECISION/2)))
298  {
299  char float_str[30];
300  sprintf(float_str, "%10.10f", sum);
301  sane=false;
302  stringstream ss;
303  ss << "MultiAgentDecisionProcessDiscrete::SanityCheck "
304  << "failed: observation does not sum to 1 but to \n"
305  << float_str << "\n for (s',a)==(" << GetStateName(to)
306  << "[" << to << "],"
307  << GetJointActionName(a)
308  << "[" << a << "])";
309  throw E(ss);
310  }
311  }
312  }
313 
314  return(sane);
315 }
316 
318 {
319  _m_sparse=sparse;
320 }