MultiAgentDecisionProcess  Release 0.2.1
BeliefSparse.cpp
Go to the documentation of this file.
1 
28 #include "BeliefSparse.h"
29 #include "BeliefIteratorSparse.h"
30 #include "BeliefIteratorGeneric.h"
31 #include <float.h>
33 
34 #define BeliefSparse_CheckAndAbort 0
35 
36 using namespace std;
37 
39 {
40 }
41 
43 {
44  _m_b.resize(size,false);
45 }
46 
47 BeliefSparse::BeliefSparse(const vector<double> &belief)
48 {
49  _m_b.resize(belief.size(),false);
50 
51  unsigned int i=0;
52  for(vector<double>::const_iterator it=belief.begin();
53  it!=belief.end(); ++it)
54  {
55  if(*it>0)
56  _m_b[i]=*it;
57  ++i;
58  }
59 #if BeliefSparse_CheckAndAbort
60  if(!SanityCheck())
61  abort();
62 #endif
63 }
64 
65 
67 {
68  Set(belief);
69 }
71 {
72  this->Set(belief);
73 }
74 
75 
76 //Destructor
78 {
79 }
80 
83 {
84  if (this == &o) return *this; // Gracefully handle self assignment
85  // Put the normal assignment duties here...
86  _m_b = o._m_b;
87  return *this;
88 }
89 
92 {
93  if (this == &o) return *this; // Gracefully handle self assignment
94  const BeliefSparse& casted_o =
95  dynamic_cast<const BeliefSparse&>(o);
96  return(operator=(casted_o));// call the operator= for BeliefSparse
97 }
98 
99 void BeliefSparse::Set(const BS &belief)
100 {
101  _m_b=belief;
102 #if BeliefSparse_CheckAndAbort
103  if(!SanityCheck())
104  abort();
105 #endif
106 }
107 
108 void BeliefSparse::Set(const vector<double> &belief)
109 {
110  _m_b.resize(belief.size(),false);
111  _m_b.clear();
112 
113  unsigned int i=0;
114  for(vector<double>::const_iterator it=belief.begin();
115  it!=belief.end(); ++it)
116  {
117  if(*it>0)
118  _m_b[i]=*it;
119  ++i;
120  }
121 #if BeliefSparse_CheckAndAbort
122  if(!SanityCheck())
123  abort();
124 #endif
125 }
126 
128 {
129  try{
130  const StateDistributionVector& b =
131  dynamic_cast<const StateDistributionVector&>( belief );
132  this->Set( (vector<double>) b );
133  return;
134  }
135  catch(exception e)
136  {
137  //TODO loop over all states and set probability
138  throw E("BeliefSparse::Set(const StateDistribution& belief) - inumplemented case.");
139  }
140 }
141 
142 
144 {
145  _m_b.resize(belief.Size(),false);
146  _m_b.clear();
147 
148  for(unsigned int i=0; i!=belief.Size(); ++i)
149  {
150  if(belief.Get(i)>0)
151  _m_b[i]=belief.Get(i);
152  }
153 #if BeliefSparse_CheckAndAbort
154  if(!SanityCheck())
155  abort();
156 #endif
157 }
158 
160 {
161  _m_b.clear();
162 }
163 
165 {
166 #if BeliefSparse_CheckAndAbort
167  if(!SanityCheck())
168  abort();
169 #endif
170  stringstream ss;
171  ss << "[" << _m_b.size() << "]< ";
172  for(BScit it=_m_b.begin();
173  it!=_m_b.end(); ++it)
174  ss << it.index() << ":" << *it << " ";
175  ss << ">";
176  return(ss.str());
177 }
178 
180 {
181  // check for negative and entries>1
182  double sum=0;
183  for(BScit it=_m_b.begin();
184  it!=_m_b.end(); ++it)
185  {
186  if(*it<0)
187  return(false);
188  if(*it>1)
189  return(false);
190  if(isnan(*it))
191  return(false);
192  sum+=*it;
193  }
194 
195  // check if sums to 1
196  if(abs(sum-1)>PROB_PRECISION)
197  return(false);
198 
199  // check whether the size is not zero
200  if(_m_b.size()==0)
201  return(false);
202 
203  // if we haven't returned yet, the belief is fine
204  return(true);
205 }
206 
207 double BeliefSparse::InnerProduct(const vector<double> &values) const
208 {
209 #if BeliefSparse_CheckAndAbort
210  if(!SanityCheck())
211  abort();
212 #endif
213  double x=0;
214  for(BScit it=_m_b.begin(); it!=_m_b.end(); ++it)
215  x+=(*it)*values[it.index()];
216 
217  return(x);
218 }
219 
220 vector<double> BeliefSparse::InnerProduct(const VectorSet &v) const
221 {
222  vector<double> values(v.size1());
223 
224  double x;
225  for(unsigned int k=0;k!=v.size1();++k)
226  {
227  x=0;
228  for(BScit it=_m_b.begin(); it!=_m_b.end(); ++it)
229  x+=(*it)*v(k,it.index());
230  values[k]=x;
231  }
232 
233  return(values);
234 }
235 
236 vector<double> BeliefSparse::InnerProduct(const VectorSet &v,
237  const vector<bool> &mask) const
238 {
239  vector<double> values(v.size1(),-DBL_MAX);
240 
241  double x;
242  for(unsigned int k=0;k!=v.size1();++k)
243  {
244  if(mask[k])
245  {
246  x=0;
247  for(BScit it=_m_b.begin(); it!=_m_b.end(); ++it)
248  x+=(*it)*v(k,it.index());
249  values[k]=x;
250  }
251  }
252 
253  return(values);
254 }
255 
257 {
258  return(BeliefIteratorGeneric(new BeliefIteratorSparse(this)));
259 }