MultiAgentDecisionProcess  Release 0.2.1
Belief.cpp
Go to the documentation of this file.
1 
28 #include "Belief.h"
29 #include "BeliefIterator.h"
30 #include "BeliefIteratorGeneric.h"
31 #include <float.h>
32 
34 
35 using namespace std;
36 
37 #define Belief_CheckAndAbort 0
38 
39 Belief::Belief(size_t size)
40 {
41  _m_b.assign(size,0);
42 }
43 
44 Belief::Belief(const vector<double> &belief)
45 {
46  _m_b=belief;
47 #if Belief_CheckAndAbort
48  if(!SanityCheck())
49  abort();
50 #endif
51 }
52 
54 {
55  _m_b.resize(belief.Size());
56  for(unsigned int i=0; i!=belief.Size(); ++i)
57  _m_b[i]=belief.Get(i);
58 
59 #if BeliefSparse_CheckAndAbort
60  if(!SanityCheck())
61  abort();
62 #endif
63 }
65 {
66  this->Set(belief);
67 }
68 
69 
70 //Destructor
72 {
73 }
74 
75 Belief&
77 {
78  if (this == &o) return *this; // Gracefully handle self assignment
79  // Put the normal assignment duties here...
80  _m_b = o._m_b;
81  return *this;
82 }
83 
86 {
87  if (this == &o) return *this; // Gracefully handle self assignment
88  const Belief& casted_o = dynamic_cast<const Belief&>(o);
89  return(operator=(casted_o));// call the operator= for Belief
90 }
91 
92 void Belief::Set(const vector<double> &belief)
93 {
94  _m_b.resize(belief.size());
95  _m_b=belief;
96 #if Belief_CheckAndAbort
97  if(!SanityCheck())
98  abort();
99 #endif
100 }
101 
102 void Belief::Set(const StateDistribution& belief)
103 {
104  const StateDistributionVector *b;
105  if((b=dynamic_cast<const StateDistributionVector*>( &belief )))
106  _m_b=*b;
107  else
108  Set(belief.ToVectorOfDoubles());
109 }
110 
111 void Belief::Set(const BeliefInterface &belief)
112 {
113  _m_b.resize(belief.Size());
114  _m_b.clear();
115 
116  for(unsigned int i=0; i!=belief.Size(); ++i)
117  _m_b[i]=belief.Get(i);
118 
119 #if Belief_CheckAndAbort
120  if(!SanityCheck())
121  abort();
122 #endif
123 }
124 
126 {
127  _m_b.clear();
128 }
129 
130 string Belief::SoftPrint() const
131 {
132 #if Belief_CheckAndAbort
133  if(!SanityCheck())
134  abort();
135 #endif
136  return(SoftPrintVector( (vector<double>) _m_b ));
137 }
138 
140 {
141  // check for negative and entries>1
142  double sum=0;
143  for(vector<double>::const_iterator it=_m_b.begin();
144  it!=_m_b.end(); ++it)
145  {
146  if(*it<0)
147  return(false);
148  if(*it>1)
149  return(false);
150  if(isnan(*it))
151  return(false);
152  sum+=*it;
153  }
154 
155  // check if sums to 1
156  if(abs(sum-1)>PROB_PRECISION)
157  return(false);
158 
159  // check whether the size is not zero
160  if(_m_b.size()==0)
161  return(false);
162 
163  // if we haven't returned yet, the belief is fine
164  return(true);
165 }
166 
167 double Belief::InnerProduct(const vector<double> &values) const
168 {
169 #if Belief_CheckAndAbort
170  if(!SanityCheck())
171  abort();
172 #endif
173 
174  double x=0;
175  for(unsigned int s=0; s!=_m_b.size(); ++s)
176  x+=_m_b[s]*values[s];
177 
178  return(x);
179 }
180 
181 vector<double> Belief::InnerProduct(const VectorSet &v) const
182 {
183  vector<double> values(v.size1());
184 
185  double x;
186  for(unsigned int k=0;k!=v.size1();++k)
187  {
188  x=0;
189  for(unsigned int s=0; s!=_m_b.size(); ++s)
190  x+=_m_b[s]*v(k,s);
191  values[k]=x;
192  }
193 
194  return(values);
195 }
196 
197 vector<double> Belief::InnerProduct(const VectorSet &v,
198  const vector<bool> &mask) const
199 {
200  vector<double> values(v.size1(),-DBL_MAX);
201 
202  double x;
203  for(unsigned int k=0;k!=v.size1();++k)
204  {
205  if(mask[k])
206  {
207  x=0;
208  for(unsigned int s=0; s!=_m_b.size(); ++s)
209  x+=_m_b[s]*v(k,s);
210  values[k]=x;
211  }
212  }
213 
214  return(values);
215 }
216 
218 {
219  return(BeliefIteratorGeneric(new BeliefIterator(this)));
220 }