aGrUM  0.16.0
aprioriBDeu_tpl.h
Go to the documentation of this file.
1 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32  namespace learning {
33 
34 
36  template < template < typename > class ALLOC >
38  const DatabaseTable< ALLOC >& database,
39  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
40  nodeId2columns,
41  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
42  Apriori< ALLOC >(database, nodeId2columns, alloc) {
43  GUM_CONSTRUCTOR(AprioriBDeu);
44  }
45 
46 
48  template < template < typename > class ALLOC >
50  const AprioriBDeu< ALLOC >& from,
51  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
52  Apriori< ALLOC >(from, alloc) {
53  GUM_CONS_CPY(AprioriBDeu);
54  }
55 
56 
58  template < template < typename > class ALLOC >
59  INLINE AprioriBDeu< ALLOC >::AprioriBDeu(const AprioriBDeu< ALLOC >& from) :
60  AprioriBDeu< ALLOC >(from, from.getAllocator()) {}
61 
62 
64  template < template < typename > class ALLOC >
66  AprioriBDeu< ALLOC >&& from,
67  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
68  Apriori< ALLOC >(std::move(from), alloc) {
69  GUM_CONS_MOV(AprioriBDeu);
70  }
71 
72 
74  template < template < typename > class ALLOC >
75  INLINE AprioriBDeu< ALLOC >::AprioriBDeu(AprioriBDeu< ALLOC >&& from) :
76  AprioriBDeu< ALLOC >(std::move(from), from.getAllocator()) {}
77 
78 
80  template < template < typename > class ALLOC >
81  AprioriBDeu< ALLOC >* AprioriBDeu< ALLOC >::clone(
82  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) const {
83  ALLOC< AprioriBDeu< ALLOC > > allocator(alloc);
84  AprioriBDeu< ALLOC >* apriori = allocator.allocate(1);
85  try {
86  allocator.construct(apriori, *this, alloc);
87  } catch (...) {
88  allocator.deallocate(apriori, 1);
89  throw;
90  }
91 
92  return apriori;
93  }
94 
95 
97  template < template < typename > class ALLOC >
98  INLINE AprioriBDeu< ALLOC >* AprioriBDeu< ALLOC >::clone() const {
99  return clone(this->getAllocator());
100  }
101 
102 
104  template < template < typename > class ALLOC >
106  GUM_DESTRUCTOR(AprioriBDeu);
107  }
108 
109 
111  template < template < typename > class ALLOC >
112  INLINE AprioriBDeu< ALLOC >& AprioriBDeu< ALLOC >::
113  operator=(const AprioriBDeu< ALLOC >& from) {
115  return *this;
116  }
117 
118 
120  template < template < typename > class ALLOC >
121  INLINE AprioriBDeu< ALLOC >& AprioriBDeu< ALLOC >::
122  operator=(AprioriBDeu< ALLOC >&& from) {
123  Apriori< ALLOC >::operator=(std::move(from));
124  return *this;
125  }
126 
127 
129  template < template < typename > class ALLOC >
130  INLINE void AprioriBDeu< ALLOC >::setWeight(const double weight) {
131  if (weight < 0.0) {
132  GUM_ERROR(OutOfBounds,
133  "A negative weight (" << weight
134  << ") is forbidden for the BDeu apriori");
135  }
136  this->_weight = weight;
137  }
138 
139 
141  template < template < typename > class ALLOC >
142  INLINE void AprioriBDeu< ALLOC >::setEffectiveSampleSize(const double weight) {
143  setWeight(weight);
144  }
145 
146 
148  template < template < typename > class ALLOC >
149  INLINE bool AprioriBDeu< ALLOC >::isOfType(const std::string& type) {
150  return AprioriBDeuType::isOfType(type);
151  }
152 
153 
155  template < template < typename > class ALLOC >
156  INLINE const std::string& AprioriBDeu< ALLOC >::getType() const {
157  return AprioriBDeuType::type;
158  }
159 
160 
162  template < template < typename > class ALLOC >
163  INLINE bool AprioriBDeu< ALLOC >::isInformative() const {
164  return this->_weight != 0.0;
165  }
166 
167 
169  template < template < typename > class ALLOC >
171  const IdSet< ALLOC >& idset,
172  std::vector< double, ALLOC< double > >& counts) {
173  // if the idset is empty or the weight is zero, the apriori is also empty
174  if (idset.empty() || (this->_weight == 0.0)) return;
175 
176  // otherwise, add the weight to all the cells in the counting vector
177  const double weight = this->_weight / counts.size();
178  for (auto& count : counts)
179  count += weight;
180  }
181 
182 
184  template < template < typename > class ALLOC >
186  const IdSet< ALLOC >& idset,
187  std::vector< double, ALLOC< double > >& counts) {
188  // if the conditioning set is empty or the weight is equal to zero,
189  // the apriori is also empty
190  if ((idset.size() == idset.nbLHSIds()) || (this->_weight == 0.0)
191  || (idset.nbLHSIds() == std::size_t(0)))
192  return;
193 
194  // add the weight to the counting vector
195  const double weight = this->_weight / counts.size();
196  for (auto& count : counts)
197  count += weight;
198  }
199 
200 
201  } /* namespace learning */
202 
203 } /* namespace gum */
204 
205 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
virtual void addConditioningApriori(const IdSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vectordefined over the right hand side of the idset ...
virtual AprioriBDeu< ALLOC > * clone() const
virtual copy constructor
AprioriBDeu< ALLOC > & operator=(const AprioriBDeu< ALLOC > &from)
copy operator
virtual ~AprioriBDeu()
destructor
virtual const std::string & getType() const final
returns the type of the apriori
STL namespace.
static const std::string type
Definition: aprioriTypes.h:53
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual void setWeight(const double weight) final
sets the effective sample size N&#39; (alias of setEffectiveSampleSize ())
virtual void addAllApriori(const IdSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vector corresponding to the idset
double weight() const
returns the weight assigned to the apriori
virtual bool isInformative() const final
indicates whether the apriori is potentially informative
double _weight
the weight of the apriori
Definition: apriori.h:143
Apriori< ALLOC > & operator=(const Apriori< ALLOC > &from)
copy operator
static bool isOfType(const std::string &t)
Definition: aprioriTypes.h:54
allocator_type getAllocator() const
returns the allocator used by the internal apriori
virtual bool isOfType(const std::string &type) final
indicates whether an apriori is of a certain type
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
Definition: aprioriBDeu.h:60
Size NodeId
Type for node ids.
Definition: graphElements.h:98
Apriori(const DatabaseTable< ALLOC > &database, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
AprioriBDeuType type
the type of the a priori
Definition: aprioriBDeu.h:57
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
AprioriBDeu(const DatabaseTable< ALLOC > &database, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
void setEffectiveSampleSize(const double weight)
sets the effective sample size N&#39;