contrib/brl/bbas/bsl/bsl_opinion.h
Go to the documentation of this file.
00001 #ifndef bsl_opinion_h_
00002 #define bsl_opinion_h_
00003 //:
00004 // \file
00005 // \brief  A binomial opinion, x = (b_,d_,a_,u_).
00006 // * b_: belief
00007 // * d_: disbelief
00008 // * u_: uncertainty about belief
00009 // * a_: atomicity is the prior probability of the two outcomes, usually 0.5
00010 //
00011 // \author Ozge C. Ozcanli
00012 // \date   Jan 26, 2012
00013 //
00014 // \verbatim
00015 //  Modifications
00016 //   <none yet>
00017 // \endverbatim
00018 
00019 #include <vcl_iostream.h>
00020 
00021 #include <vcl_limits.h>
00022 #include <vcl_cmath.h>
00023 
00024 class bsl_opinion
00025 {
00026  public:
00027   bsl_opinion() : u_(1.0f), b_(0.0f), a_(0.5f) {}
00028   bsl_opinion(float u, float b) : u_(u), b_(b), a_(0.5) {}
00029   bsl_opinion(float u, float b, float a) : u_(u), b_(b), a_(a) {}
00030 
00031   //: constructor where uncertainty is assumed to go away when a belief value is set
00032   bsl_opinion(float b) { b_=b; u_=1.0f-b; a_ = 0.5f;}
00033 
00034   //: copy constructor
00035   bsl_opinion(bsl_opinion const& o) : u_(o.u()), b_(o.b()), a_(o.a()) {}
00036 
00037   //: destructor
00038   ~bsl_opinion() {}
00039 
00040   float u() const { return u_; }
00041 
00042   float b() const { return b_; }
00043 
00044   float a() const { return a_; }
00045 
00046   float d() const { return 1.0f-b_-u_; }
00047 
00048   float expectation() const { return b_+u_*a_; }
00049 
00050   void set(float u, float b) { u_=u; b_=b; }
00051 
00052   void set_u(float u) { u_=u; }
00053 
00054   void set_b(float b) { b_=b; u_=1.0f-b; }
00055 
00056   void set_a(float a) { a_ = a; }
00057 
00058   bool operator==(bsl_opinion const& o) const { return b_==o.b() && u_==o.u() && a_==o.a(); }
00059 
00060  private:
00061   float u_;  // uncertainty value [0.0,1.0], 1.0 if total uncertainty, 0.0 if total certainty
00062   float b_;  // probability as a belief value [0.0,1.0]
00063   float a_;  // atomicity, default is .5f;
00064 };
00065 
00066 inline bsl_opinion fuse(bsl_opinion const& lhs, bsl_opinion const& rhs)
00067 {
00068   float bA = lhs.b();
00069   float bB = rhs.b();
00070   float uA = lhs.u();
00071   float uB = rhs.u();
00072   // then, dA = 1.0f-bA-uA
00073   // and   dB = 1.0f-bB-uB
00074   float kappa = uA + uB - uA*uB; // = 1 - (1-uA)*(1-uB)
00075 
00076   // if one of the uncertainties is non-zero
00077   if (vcl_abs(uA) > vcl_numeric_limits<float>::epsilon() || vcl_abs(uB) > vcl_numeric_limits<float>::epsilon()) {
00078     float b = (bA*uB + bB*uA)/kappa;
00079     float u = uA*uB/kappa;
00080     return bsl_opinion(u,b,lhs.a()); // atomicity should be the same for lhs & rhs
00081   }
00082   else {
00083     bsl_opinion op((bA+bB)/2.0f); op.set_a(lhs.a());
00084     return op;
00085   }
00086 }
00087 
00088 inline bool operator> (bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() >  rhs.b(); }
00089 inline bool operator>=(bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() >= rhs.b(); }
00090 inline bool operator< (bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() <  rhs.b(); }
00091 
00092 inline bsl_opinion operator+(bsl_opinion const& lhs, bsl_opinion const& rhs) { return bsl_opinion(lhs.b()+rhs.b()); }
00093 inline float operator+(bsl_opinion const& lhs, float rhs) { return lhs.b() + rhs; }
00094 inline float operator*(bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() * rhs.b(); }
00095 
00096 inline bsl_opinion operator/(float o1, bsl_opinion const& o2) { return bsl_opinion(o1/o2.b()); }
00097 inline bsl_opinion operator/(bsl_opinion const& o1, bsl_opinion const& o2) { return bsl_opinion(o1.b()/o2.b()); }
00098 
00099 inline vcl_ostream& operator<< (vcl_ostream& s, bsl_opinion const& o) { s << "bsl_opinion [b=" << o.b() << " u=" << o.u() << ']' << vcl_endl; return s; }
00100 
00101 
00102 #endif
00103