aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
aprioriSmoothing_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 AprioriSmoothing< ALLOC >::allocator_type& alloc) :
42  Apriori< ALLOC >(database, nodeId2columns, alloc) {
43  GUM_CONSTRUCTOR(AprioriSmoothing);
44  }
45 
46 
48  template < template < typename > class ALLOC >
50  const AprioriSmoothing< ALLOC >& from,
51  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) :
52  Apriori< ALLOC >(from, alloc) {
53  GUM_CONS_CPY(AprioriSmoothing);
54  }
55 
56 
58  template < template < typename > class ALLOC >
60  const AprioriSmoothing< ALLOC >& from) :
61  AprioriSmoothing< ALLOC >(from, from.getAllocator()) {}
62 
63 
65  template < template < typename > class ALLOC >
67  AprioriSmoothing< ALLOC >&& from,
68  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) :
69  Apriori< ALLOC >(std::move(from), alloc) {
70  GUM_CONS_MOV(AprioriSmoothing);
71  }
72 
73 
75  template < template < typename > class ALLOC >
77  AprioriSmoothing< ALLOC >&& from) :
78  AprioriSmoothing< ALLOC >(std::move(from), from.getAllocator()) {}
79 
80 
82  template < template < typename > class ALLOC >
83  AprioriSmoothing< ALLOC >* AprioriSmoothing< ALLOC >::clone(
84  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) const {
85  ALLOC< AprioriSmoothing< ALLOC > > allocator(alloc);
86  AprioriSmoothing< ALLOC >* apriori = allocator.allocate(1);
87  try {
88  allocator.construct(apriori, *this, alloc);
89  } catch (...) {
90  allocator.deallocate(apriori, 1);
91  throw;
92  }
93 
94  return apriori;
95  }
96 
97 
99  template < template < typename > class ALLOC >
100  INLINE AprioriSmoothing< ALLOC >* AprioriSmoothing< ALLOC >::clone() const {
101  return clone(this->getAllocator());
102  }
103 
104 
106  template < template < typename > class ALLOC >
108  GUM_DESTRUCTOR(AprioriSmoothing);
109  }
110 
111 
113  template < template < typename > class ALLOC >
114  INLINE AprioriSmoothing< ALLOC >& AprioriSmoothing< ALLOC >::operator=(
115  const AprioriSmoothing< ALLOC >& from) {
117  return *this;
118  }
119 
120 
122  template < template < typename > class ALLOC >
123  INLINE AprioriSmoothing< ALLOC >&
124  AprioriSmoothing< ALLOC >::operator=(AprioriSmoothing< ALLOC >&& from) {
125  Apriori< ALLOC >::operator=(std::move(from));
126  return *this;
127  }
128 
129 
131  template < template < typename > class ALLOC >
132  INLINE bool AprioriSmoothing< ALLOC >::isOfType(const std::string& type) {
133  return AprioriSmoothingType::isOfType(type);
134  }
135 
136 
138  template < template < typename > class ALLOC >
139  INLINE const std::string& AprioriSmoothing< ALLOC >::getType() const {
141  }
142 
143 
145  template < template < typename > class ALLOC >
146  INLINE bool AprioriSmoothing< ALLOC >::isInformative() const {
147  return this->_weight != 0.0;
148  }
149 
150 
152  template < template < typename > class ALLOC >
154  const IdCondSet< ALLOC >& idset,
155  std::vector< double, ALLOC< double > >& counts) {
156  // if the idset is empty or the weight is zero, the apriori is also empty
157  if (idset.empty() || (this->_weight == 0.0)) return;
158 
159  // otherwise, add the weight to all the cells in the counting vector
160  for (auto& count: counts)
161  count += this->_weight;
162  }
163 
164 
166  template < template < typename > class ALLOC >
168  const IdCondSet< ALLOC >& idset,
169  std::vector< double, ALLOC< double > >& counts) {
170  // if the conditioning set is empty or the weight is equal to zero,
171  // the apriori is also empty
172  if ((idset.size() == idset.nbLHSIds()) || (this->_weight == 0.0)
173  || (idset.nbLHSIds() == std::size_t(0)))
174  return;
175 
176  // compute the weight of the conditioning set
177  double weight = this->_weight;
178  if (this->_nodeId2columns.empty()) {
179  for (std::size_t i = std::size_t(0); i < idset.nbLHSIds(); ++i) {
180  weight *= this->_database->domainSize(idset[i]);
181  }
182  } else {
183  for (std::size_t i = std::size_t(0); i < idset.nbLHSIds(); ++i) {
184  weight *=
185  this->_database->domainSize(this->_nodeId2columns.second(idset[i]));
186  }
187  }
188 
189  // add the weight to the counting vector
190  for (auto& count: counts)
191  count += weight;
192  }
193 
194 
195  } /* namespace learning */
196 
197 } /* namespace gum */
198 
199 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
static const std::string type
Definition: aprioriTypes.h:43
virtual void addConditioningApriori(const IdCondSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vectordefined over the right hand side of the idset ...
STL namespace.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
virtual bool isOfType(const std::string &type) final
indicates whether an apriori is of a certain type
virtual bool isInformative() const final
indicates whether the apriori is potentially informative
const DatabaseTable< ALLOC > * _database
a reference to the database in order to have access to its variables
Definition: apriori.h:146
virtual AprioriSmoothing< ALLOC > * clone() const
virtual copy constructor
virtual ~AprioriSmoothing()
destructor
bool empty() const noexcept
Returns true if the gum::Bijection doesn&#39;t contain any association.
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
double weight() const
returns the weight assigned to the apriori
virtual const std::string & getType() const final
returns the type of the apriori
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:44
AprioriSmoothingType type
the type of the a priori
allocator_type getAllocator() const
returns the allocator used by the internal apriori
AprioriSmoothing< ALLOC > & operator=(const AprioriSmoothing< ALLOC > &from)
copy operator
Bijection< NodeId, std::size_t, ALLOC< std::size_t > > _nodeId2columns
a mapping from the NodeIds of the variables to the indices of the columns in the database ...
Definition: apriori.h:150
AprioriSmoothing(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
virtual void addAllApriori(const IdCondSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vector corresponding to the idset
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