aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
aprioriSmoothing.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 GUM_LEARNING_A_PRIORI_SMOOTHING_H
28 #define GUM_LEARNING_A_PRIORI_SMOOTHING_H
29 
30 #include <vector>
31 
32 #include <agrum/agrum.h>
33 #include <agrum/BN/learning/aprioris/apriori.h>
34 
35 namespace gum {
36 
37  namespace learning {
38 
39  /** @class AprioriSmoothing
40  * @brief the smooth a priori: adds a weight w to all the countings
41  * @headerfile aprioriSmoothing.h <agrum/tools/database/aprioriSmoothing.h>
42  * @ingroup learning_apriori
43  */
44  template < template < typename > class ALLOC = std::allocator >
45  class AprioriSmoothing: public Apriori< ALLOC > {
46  public:
47  /// the type of the a priori
49 
50  /// type for the allocators passed in arguments of methods
52 
53 
54  // ##########################################################################
55  /// @name Constructors / Destructors
56  // ##########################################################################
57  /// @{
58 
59  /// default constructor
60  /** @param database the database from which learning is performed. This is
61  * useful to get access to the random variables
62  * @param nodeId2Columns a mapping from the ids of the nodes in the
63  * graphical model to the corresponding column in the DatabaseTable.
64  * This enables estimating from a database in which variable A corresponds
65  * to the 2nd column the parameters of a BN in which variable A has a
66  * NodeId of 5. An empty nodeId2Columns bijection means that the mapping
67  * is an identity, i.e., the value of a NodeId is equal to the index of
68  * the column in the DatabaseTable.
69  * @param alloc the allocator used to allocate the structures within the
70  * RecordCounter.*/
71  AprioriSmoothing(const DatabaseTable< ALLOC >& database,
73  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
75 
76  /// copy constructor
77  AprioriSmoothing(const AprioriSmoothing< ALLOC >& from);
78 
79  /// copy constructor with a given allocator
80  AprioriSmoothing(const AprioriSmoothing< ALLOC >& from, const allocator_type& alloc);
81 
82  /// move constructor
83  AprioriSmoothing(AprioriSmoothing< ALLOC >&& from);
84 
85  /// move constructor with a given allocator
86  AprioriSmoothing(AprioriSmoothing< ALLOC >&& from, const allocator_type& alloc);
87 
88  /// virtual copy constructor
89  virtual AprioriSmoothing< ALLOC >* clone() const;
90 
91  /// virtual copy constructor with a given allocator
92  virtual AprioriSmoothing< ALLOC >* clone(const allocator_type& alloc) const;
93 
94  /// destructor
95  virtual ~AprioriSmoothing();
96 
97  /// @}
98 
99 
100  // ##########################################################################
101  /// @name Operators
102  // ##########################################################################
103  /// @{
104 
105  /// copy operator
107 
108  /// move operator
110 
111  /// @}
112 
113 
114  // ##########################################################################
115  /// @name Accessors / Modifiers
116  // ##########################################################################
117  /// @{
118 
119  /// indicates whether an apriori is of a certain type
120  virtual bool isOfType(const std::string& type) final;
121 
122  /// returns the type of the apriori
123  virtual const std::string& getType() const final;
124 
125  /// indicates whether the apriori is potentially informative
126  /** Basically, only the NoApriori is uninformative. However, it may happen
127  * that, under some circonstances, an apriori, which is usually not equal
128  * to the NoApriori, becomes equal to it (e.g., when the weight is equal
129  * to zero). In this case, if the apriori can detect this case, it shall
130  * inform the classes that use it that it is temporarily uninformative.
131  * These classes will then be able to speed-up their code by avoiding to
132  * take into account the apriori in their computations. */
133  virtual bool isInformative() const final;
134 
135  /// adds the apriori to a counting vector corresponding to the idset
136  /** adds the apriori to an already created counting vector defined over
137  * the union of the variables on both the left and right hand side of the
138  * conditioning bar of the idset.
139  * @warning the method assumes that the size of the vector is exactly
140  * the domain size of the joint variables set. */
141  virtual void addAllApriori(const IdCondSet< ALLOC >& idset,
142  std::vector< double, ALLOC< double > >& counts) final;
143 
144  /** @brief adds the apriori to a counting vectordefined over the right
145  * hand side of the idset
146  *
147  * @warning the method assumes that the size of the vector is exactly
148  * the domain size of the joint RHS variables of the idset. */
149  virtual void addConditioningApriori(const IdCondSet< ALLOC >& idset,
150  std::vector< double, ALLOC< double > >& counts) final;
151 
152  /// @}
153  };
154 
155  } /* namespace learning */
156 
157 } /* namespace gum */
158 
159 /// include the template implementation
160 #include <agrum/BN/learning/aprioris/aprioriSmoothing_tpl.h>
161 
162 #endif /* GUM_LEARNING_A_PRIORI_SMOOTHING_H */
virtual AprioriSmoothing< ALLOC > * clone(const allocator_type &alloc) const
virtual copy constructor with a given allocator
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 ...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
AprioriSmoothing(AprioriSmoothing< ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
AprioriSmoothing(const AprioriSmoothing< ALLOC > &from)
copy constructor
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
AprioriSmoothing(AprioriSmoothing< ALLOC > &&from)
move constructor
virtual AprioriSmoothing< ALLOC > * clone() const
virtual copy constructor
virtual ~AprioriSmoothing()
destructor
virtual const std::string & getType() const final
returns the type of the apriori
AprioriSmoothing(const AprioriSmoothing< ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
AprioriSmoothing< ALLOC > & operator=(AprioriSmoothing< ALLOC > &&from)
move operator
AprioriSmoothing< ALLOC > & operator=(const AprioriSmoothing< ALLOC > &from)
copy operator
the smooth a priori: adds a weight w to all the countings
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
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
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