faif
Belief.hpp
1 #ifndef FAIF_BELIEF_HPP_
2 #define FAIF_BELIEF_HPP_
3 
4 #include <ostream>
5 
6 #include <boost/serialization/serialization.hpp>
7 #include <boost/serialization/nvp.hpp>
8 
9 #include "../Value.hpp"
10 
11 namespace faif {
12 
13  /** \brief the probability type */
14  typedef double Probability;
15 
16  namespace ml {
17 
18  /**
19  \brief the belief concept
20  */
21  template<typename Bel>
22  struct BeliefConcept : boost::DefaultConstructible<Bel>, boost::CopyConstructible<Bel>,
23  boost::Assignable<Bel>
24  {
25 
26  typedef typename Bel::Value Value; //the Value type is required
27  typedef typename Bel::ValueId ValueId; //the ValueId type is required
28  typedef typename Bel::Beliefs Beliefs; //the Belief collection type is required
29 
30  BOOST_CONCEPT_USAGE(BeliefConcept)
31  {
32  BOOST_CONCEPT_ASSERT((ValueConcept<Value>));
33  Bel b; //default constructable is required
34  b.getValue(); //the getValue() method is required
35  b.getProbability(); //the getProbability() method is required
36  }
37 
38  };
39 
40  /** \brief belief is value id with probability */
41  template <typename Val> class Belief {
42  BOOST_CONCEPT_ASSERT((ValueConcept<Val>));
43  public:
44  typedef Val Value;
45 
46  typedef typename Val::DomainType::ValueId ValueId;
47 
48  //collection of pair (AttrIdd, Probability)
49  typedef typename std::vector<Belief<Val> > Beliefs;
50 
51  Belief() : value_(Val::DomainType::getUnknownId() ), probability_(0.0) {}
52 
53  Belief(ValueId value, Probability probability) : value_(value), probability_(probability) {}
54 
55  ValueId getValue() const { return value_; }
56  Probability getProbability() const { return probability_; }
57 
58  /** \brief serialization using boost::serialization */
59  friend class boost::serialization::access;
60 
61  template<class Archive>
62  void save(Archive & ar, const unsigned int /* file_version */) const {
63  ar & boost::serialization::make_nvp("Value", value_ );
64  ar & boost::serialization::make_nvp("Probability", probability_ );
65  }
66 
67  template<class Archive>
68  void load(Archive & ar, const unsigned int /* file_version */) {
69  typename Val::DomainType::ValueIdSerialize i;
70  ar >> boost::serialization::make_nvp("Value", i);
71  value_ = const_cast<ValueId>(i);
72  ar & boost::serialization::make_nvp("Probability", probability_ );
73  }
74 
75  template<class Archive>
76  void serialize( Archive &ar, const unsigned int file_version ){
77  boost::serialization::split_member(ar, *this, file_version);
78  }
79 
80  //ordering from the best towards week
81  bool operator<( const Belief &c) const { return probability_ > c.probability_; }
82  private:
83  ValueId value_; //public member
84  Probability probability_; //public member
85  };
86 
87  //ostream iterator for debugging
88  template <typename Val> std::ostream& operator<<(std::ostream& os, const Belief<Val>& b) {
89  os << "Value:" << b.getValue() << " Prob:" << b.getProbability();
90  return os;
91  }
92 
93  /** stream operator - for debugging */
94  template <typename Val> std::ostream& operator<<(std::ostream& os, const std::vector<Belief<Val> >& c) {
95  std::copy(c.begin(), c.end(), std::ostream_iterator<Belief<Val> >(os,";") );
96  return os;
97  }
98 
99  } //namespace ml
100 
101 } // namespace faif
102 
103 
104 #endif //FAIF_BELIEF_HPP_
Definition: Chain.h:17
the value concept
Definition: Value.hpp:41
the belief concept
Definition: Belief.hpp:22
belief is value id with probability
Definition: Belief.hpp:41