aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
aprioriSmoothing_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief the smooth a priori: adds a weight w to all the countings
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 
29 namespace gum {
30 
31  namespace learning {
32 
33 
34  /// default constructor
35  template < template < typename > class ALLOC >
36  INLINE AprioriSmoothing< ALLOC >::AprioriSmoothing(
37  const DatabaseTable< ALLOC >& database,
38  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
40  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) :
43  }
44 
45 
46  /// copy constructor with a given allocator
47  template < template < typename > class ALLOC >
49  const AprioriSmoothing< ALLOC >& from,
50  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) :
51  Apriori< ALLOC >(from, alloc) {
53  }
54 
55 
56  /// copy constructor
57  template < template < typename > class ALLOC >
59  const AprioriSmoothing< ALLOC >& from) :
61 
62 
63  /// move constructor with a given allocator
64  template < template < typename > class ALLOC >
67  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) :
68  Apriori< ALLOC >(std::move(from), alloc) {
70  }
71 
72 
73  /// move constructor
74  template < template < typename > class ALLOC >
78 
79 
80  /// virtual copy constructor with a given allocator
81  template < template < typename > class ALLOC >
83  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) const {
86  try {
88  } catch (...) {
90  throw;
91  }
92 
93  return apriori;
94  }
95 
96 
97  /// virtual copy constructor
98  template < template < typename > class ALLOC >
100  return clone(this->getAllocator());
101  }
102 
103 
104  /// destructor
105  template < template < typename > class ALLOC >
108  }
109 
110 
111  /// copy operator
112  template < template < typename > class ALLOC >
114  const AprioriSmoothing< ALLOC >& from) {
115  Apriori< ALLOC >::operator=(from);
116  return *this;
117  }
118 
119 
120  /// move operator
121  template < template < typename > class ALLOC >
124  Apriori< ALLOC >::operator=(std::move(from));
125  return *this;
126  }
127 
128 
129  /// indicates whether an apriori is of a certain type
130  template < template < typename > class ALLOC >
131  INLINE bool AprioriSmoothing< ALLOC >::isOfType(const std::string& type) {
133  }
134 
135 
136  /// returns the type of the apriori
137  template < template < typename > class ALLOC >
138  INLINE const std::string& AprioriSmoothing< ALLOC >::getType() const {
139  return AprioriSmoothingType::type;
140  }
141 
142 
143  /// indicates whether the apriori is potentially informative
144  template < template < typename > class ALLOC >
145  INLINE bool AprioriSmoothing< ALLOC >::isInformative() const {
146  return this->weight_ != 0.0;
147  }
148 
149 
150  /// returns the apriori vector all the variables in the idset
151  template < template < typename > class ALLOC >
153  const IdCondSet< ALLOC >& idset,
154  std::vector< double, ALLOC< double > >& counts) {
155  // if the idset is empty or the weight is zero, the apriori is also empty
156  if (idset.empty() || (this->weight_ == 0.0)) return;
157 
158  // otherwise, add the weight to all the cells in the counting vector
159  for (auto& count: counts)
160  count += this->weight_;
161  }
162 
163 
164  /// returns the apriori vector over only the conditioning set of an idset
165  template < template < typename > class ALLOC >
167  const IdCondSet< ALLOC >& idset,
168  std::vector< double, ALLOC< double > >& counts) {
169  // if the conditioning set is empty or the weight is equal to zero,
170  // the apriori is also empty
171  if ((idset.size() == idset.nbLHSIds()) || (this->weight_ == 0.0)
172  || (idset.nbLHSIds() == std::size_t(0)))
173  return;
174 
175  // compute the weight of the conditioning set
176  double weight = this->weight_;
177  if (this->nodeId2columns_.empty()) {
178  for (std::size_t i = std::size_t(0); i < idset.nbLHSIds(); ++i) {
179  weight *= this->database_->domainSize(idset[i]);
180  }
181  } else {
182  for (std::size_t i = std::size_t(0); i < idset.nbLHSIds(); ++i) {
183  weight *= this->database_->domainSize(
184  this->nodeId2columns_.second(idset[i]));
185  }
186  }
187 
188  // add the weight to the counting vector
189  for (auto& count: counts)
190  count += weight;
191  }
192 
193 
194  } /* namespace learning */
195 
196 } /* namespace gum */
197 
198 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)