aGrUM  0.14.2
aprioriSmoothing_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 AprioriSmoothing< ALLOC >::allocator_type& alloc) :
39  Apriori< ALLOC >(database, nodeId2columns, alloc) {
40  GUM_CONSTRUCTOR(AprioriSmoothing);
41  }
42 
43 
45  template < template < typename > class ALLOC >
47  const AprioriSmoothing< ALLOC >& from,
48  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) :
49  Apriori< ALLOC >(from, alloc) {
50  GUM_CONS_CPY(AprioriSmoothing);
51  }
52 
53 
55  template < template < typename > class ALLOC >
57  const AprioriSmoothing< ALLOC >& from) :
58  AprioriSmoothing< ALLOC >(from, from.getAllocator()) {}
59 
60 
62  template < template < typename > class ALLOC >
64  AprioriSmoothing< ALLOC >&& from,
65  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) :
66  Apriori< ALLOC >(std::move(from), alloc) {
67  GUM_CONS_MOV(AprioriSmoothing);
68  }
69 
70 
72  template < template < typename > class ALLOC >
74  AprioriSmoothing< ALLOC >&& from) :
75  AprioriSmoothing< ALLOC >(std::move(from), from.getAllocator()) {}
76 
77 
79  template < template < typename > class ALLOC >
80  AprioriSmoothing< ALLOC >* AprioriSmoothing< ALLOC >::clone(
81  const typename AprioriSmoothing< ALLOC >::allocator_type& alloc) const {
82  ALLOC< AprioriSmoothing< ALLOC > > allocator(alloc);
83  AprioriSmoothing< ALLOC >* apriori = allocator.allocate(1);
84  try {
85  allocator.construct(apriori, *this, alloc);
86  } catch (...) {
87  allocator.deallocate(apriori, 1);
88  throw;
89  }
90 
91  return apriori;
92  }
93 
94 
96  template < template < typename > class ALLOC >
97  INLINE AprioriSmoothing< ALLOC >* AprioriSmoothing< ALLOC >::clone() const {
98  return clone(this->getAllocator());
99  }
100 
101 
103  template < template < typename > class ALLOC >
105  GUM_DESTRUCTOR(AprioriSmoothing);
106  }
107 
108 
110  template < template < typename > class ALLOC >
111  INLINE AprioriSmoothing< ALLOC >& AprioriSmoothing< ALLOC >::
112  operator=(const AprioriSmoothing< ALLOC >& from) {
114  return *this;
115  }
116 
117 
119  template < template < typename > class ALLOC >
120  INLINE AprioriSmoothing< ALLOC >& AprioriSmoothing< ALLOC >::
121  operator=(AprioriSmoothing< ALLOC >&& from) {
122  Apriori< ALLOC >::operator=(std::move(from));
123  return *this;
124  }
125 
126 
128  template < template < typename > class ALLOC >
129  INLINE bool AprioriSmoothing< ALLOC >::isOfType(const std::string& type) {
130  return AprioriSmoothingType::isOfType(type);
131  }
132 
133 
135  template < template < typename > class ALLOC >
136  INLINE const std::string& AprioriSmoothing< ALLOC >::getType() const {
138  }
139 
140 
142  template < template < typename > class ALLOC >
143  INLINE bool AprioriSmoothing< ALLOC >::isInformative() const {
144  return this->_weight != 0.0;
145  }
146 
147 
149  template < template < typename > class ALLOC >
151  const IdSet< ALLOC >& idset,
152  std::vector< double, ALLOC< double > >& counts) {
153  // if the idset is empty or the weight is zero, the apriori is also empty
154  if (idset.empty() || (this->_weight == 0.0)) return;
155 
156  // otherwise, add the weight to all the cells in the counting vector
157  for (auto& count : counts)
158  count += this->_weight;
159  }
160 
161 
163  template < template < typename > class ALLOC >
165  const IdSet< ALLOC >& idset,
166  std::vector< double, ALLOC< double > >& counts) {
167  // if the conditioning set is empty or the weight is equal to zero,
168  // the apriori is also empty
169  if ((idset.size() == idset.nbLHSIds()) || (this->_weight == 0.0)
170  || (idset.nbLHSIds() == std::size_t(0)))
171  return;
172 
173  // compute the weight of the conditioning set
174  double weight = this->_weight;
175  if (this->_nodeId2columns.empty()) {
176  for (std::size_t i = std::size_t(0); i < idset.nbLHSIds(); ++i) {
177  weight *= this->_database->domainSize(idset[i]);
178  }
179  } else {
180  for (std::size_t i = std::size_t(0); i < idset.nbLHSIds(); ++i) {
181  weight *=
182  this->_database->domainSize(this->_nodeId2columns.second(idset[i]));
183  }
184  }
185 
186  // add the weight to the counting vector
187  for (auto& count : counts)
188  count += weight;
189  }
190 
191 
192  } /* namespace learning */
193 
194 } /* namespace gum */
195 
196 #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:40
STL namespace.
gum is the global namespace for all aGrUM entities
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:143
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:140
Apriori< ALLOC > & operator=(const Apriori< ALLOC > &from)
copy operator
static bool isOfType(const std::string &t)
Definition: aprioriTypes.h:41
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:147
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
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
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
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 ...