aGrUM  0.14.2
aprioriBDeu_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #ifndef DOXYGEN_SHOULD_SKIP_THIS
26 
27 namespace gum {
28 
29  namespace learning {
30 
31 
33  template < template < typename > class ALLOC >
35  const DatabaseTable< ALLOC >& database,
36  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
37  nodeId2columns,
38  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
39  Apriori< ALLOC >(database, nodeId2columns, alloc) {
40  GUM_CONSTRUCTOR(AprioriBDeu);
41  }
42 
43 
45  template < template < typename > class ALLOC >
47  const AprioriBDeu< ALLOC >& from,
48  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
49  Apriori< ALLOC >(from, alloc) {
50  GUM_CONS_CPY(AprioriBDeu);
51  }
52 
53 
55  template < template < typename > class ALLOC >
56  INLINE AprioriBDeu< ALLOC >::AprioriBDeu(const AprioriBDeu< ALLOC >& from) :
57  AprioriBDeu< ALLOC >(from, from.getAllocator()) {}
58 
59 
61  template < template < typename > class ALLOC >
63  AprioriBDeu< ALLOC >&& from,
64  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
65  Apriori< ALLOC >(std::move(from), alloc) {
66  GUM_CONS_MOV(AprioriBDeu);
67  }
68 
69 
71  template < template < typename > class ALLOC >
72  INLINE AprioriBDeu< ALLOC >::AprioriBDeu(AprioriBDeu< ALLOC >&& from) :
73  AprioriBDeu< ALLOC >(std::move(from), from.getAllocator()) {}
74 
75 
77  template < template < typename > class ALLOC >
78  AprioriBDeu< ALLOC >* AprioriBDeu< ALLOC >::clone(
79  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) const {
80  ALLOC< AprioriBDeu< ALLOC > > allocator(alloc);
81  AprioriBDeu< ALLOC >* apriori = allocator.allocate(1);
82  try {
83  allocator.construct(apriori, *this, alloc);
84  } catch (...) {
85  allocator.deallocate(apriori, 1);
86  throw;
87  }
88 
89  return apriori;
90  }
91 
92 
94  template < template < typename > class ALLOC >
95  INLINE AprioriBDeu< ALLOC >* AprioriBDeu< ALLOC >::clone() const {
96  return clone(this->getAllocator());
97  }
98 
99 
101  template < template < typename > class ALLOC >
103  GUM_DESTRUCTOR(AprioriBDeu);
104  }
105 
106 
108  template < template < typename > class ALLOC >
109  INLINE AprioriBDeu< ALLOC >& AprioriBDeu< ALLOC >::
110  operator=(const AprioriBDeu< ALLOC >& from) {
112  return *this;
113  }
114 
115 
117  template < template < typename > class ALLOC >
118  INLINE AprioriBDeu< ALLOC >& AprioriBDeu< ALLOC >::
119  operator=(AprioriBDeu< ALLOC >&& from) {
120  Apriori< ALLOC >::operator=(std::move(from));
121  return *this;
122  }
123 
124 
126  template < template < typename > class ALLOC >
127  INLINE void AprioriBDeu< ALLOC >::setWeight(const double weight) {
128  if (weight < 0.0) {
129  GUM_ERROR(OutOfBounds,
130  "A negative weight (" << weight
131  << ") is forbidden for the BDeu apriori");
132  }
133  this->_weight = weight;
134  }
135 
136 
138  template < template < typename > class ALLOC >
139  INLINE void AprioriBDeu< ALLOC >::setEffectiveSampleSize(const double weight) {
140  setWeight(weight);
141  }
142 
143 
145  template < template < typename > class ALLOC >
146  INLINE bool AprioriBDeu< ALLOC >::isOfType(const std::string& type) {
147  return AprioriBDeuType::isOfType(type);
148  }
149 
150 
152  template < template < typename > class ALLOC >
153  INLINE const std::string& AprioriBDeu< ALLOC >::getType() const {
154  return AprioriBDeuType::type;
155  }
156 
157 
159  template < template < typename > class ALLOC >
160  INLINE bool AprioriBDeu< ALLOC >::isInformative() const {
161  return this->_weight != 0.0;
162  }
163 
164 
166  template < template < typename > class ALLOC >
168  const IdSet< ALLOC >& idset,
169  std::vector< double, ALLOC< double > >& counts) {
170  // if the idset is empty or the weight is zero, the apriori is also empty
171  if (idset.empty() || (this->_weight == 0.0)) return;
172 
173  // otherwise, add the weight to all the cells in the counting vector
174  const double weight = this->_weight / counts.size();
175  for (auto& count : counts)
176  count += weight;
177  }
178 
179 
181  template < template < typename > class ALLOC >
183  const IdSet< ALLOC >& idset,
184  std::vector< double, ALLOC< double > >& counts) {
185  // if the conditioning set is empty or the weight is equal to zero,
186  // the apriori is also empty
187  if ((idset.size() == idset.nbLHSIds()) || (this->_weight == 0.0)
188  || (idset.nbLHSIds() == std::size_t(0)))
189  return;
190 
191  // add the weight to the counting vector
192  const double weight = this->_weight / counts.size();
193  for (auto& count : counts)
194  count += weight;
195  }
196 
197 
198  } /* namespace learning */
199 
200 } /* namespace gum */
201 
202 #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:50
gum is the global namespace for all aGrUM entities
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:140
Apriori< ALLOC > & operator=(const Apriori< ALLOC > &from)
copy operator
static bool isOfType(const std::string &t)
Definition: aprioriTypes.h:51
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:57
Size NodeId
Type for node ids.
Definition: graphElements.h:97
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:54
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
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;