aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
DBRowGeneratorWithBN.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 Base class for DBRowGenerator classes that use a BN for computing
24  * their outputs
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLWithBNIN
27  */
28 #ifndef GUM_LEARNING_DBROW_GENERATOR_WITH_BN_H
29 #define GUM_LEARNING_DBROW_GENERATOR_WITH_BN_H
30 
31 #include <vector>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/tools/core/bijection.h>
35 #include <agrum/BN/BayesNet.h>
36 #include <agrum/BN/inference/variableElimination.h>
37 #include <agrum/tools/database/DBRowGenerator.h>
38 
39 namespace gum {
40 
41  namespace learning {
42 
43  /** @class DBRowGeneratorWithBN
44  * @headerfile DBRowGeneratorWithBN.h <agrum/tools/database/DBRowGeneratorWithBN.h>
45  * @brief Base class for DBRowGenerator classes that use a BN for computing
46  * their outputs
47  *
48  * @ingroup learning_database
49  *
50  * This class is a DBRowGenerator that fills the unobserved values of the
51  * nodes of interest as the WithBN algorithm does, i.e., by returning all the
52  * possible completed rows with a weight corresponding to the probability
53  * of the completion.
54  * The standard usage of a DBRowGenerator is the following:
55  * @code
56  * // create a DatabaseTable and fill it
57  * gum::learning::DBTranslatorSet<> set;
58  * for ( int i = 0; i < 10; ++i )
59  * set.insertTranslator(gum::learning::DBTranslator4LabelizedVariable<>(),i);
60  * gum::learning::DatabaseTable<> database ( set );
61  * // fill the database
62  *
63  * // keep in a vector the types of the columns in the database
64  * const std::vector<gum::learning::DBTranslatedValueType>
65  * column_types ( 10, gum::learning::DBTranslatedValueType::DISCRETE );
66  *
67  * // create the generator
68  * gum::learning::DBRowGeneratorWithBN<> generator ( col_types );
69  *
70  * // parse the database and produce output rows
71  * for ( auto dbrow : database ) {
72  * generator.setInputRow ( dbrow );
73  * while ( generator.hasRows() ) {
74  * const auto& output_dbrow = generator.generate ();
75  * // do something with the output dbrow
76  * }
77  * }
78  * @endcode
79  */
80  template < typename GUM_SCALAR = double,
81  template < typename > class ALLOC = std::allocator >
83  public:
84  /// type for the allocators passed in arguments of methods
86 
87  // ##########################################################################
88  /// @name Constructors / Destructors
89  // ##########################################################################
90 
91  /// @{
92 
93  /// default constructor
97  const BayesNet< GUM_SCALAR >& bn,
99  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
101  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
102  const allocator_type& alloc = allocator_type());
103 
104  /// copy constructor
105  DBRowGeneratorWithBN(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >& from);
106 
107  /// copy constructor with a given allocator
108  DBRowGeneratorWithBN(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >& from,
109  const allocator_type& alloc);
110 
111  /// move constructor
112  DBRowGeneratorWithBN(DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >&& from);
113 
114  /// move constructor with a given allocator
115  DBRowGeneratorWithBN(DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >&& from,
116  const allocator_type& alloc);
117 
118  /// destructor
120 
121  /// @}
122 
123 
124  // ##########################################################################
125  /// @name Accessors / Modifiers
126  // ##########################################################################
127 
128  /// @{
129 
130  /// assign a new Bayes net to the generator
131  virtual void setBayesNet(const BayesNet< GUM_SCALAR >& new_bn);
132 
133  /// returns the Bayes net used by the generator
134  const BayesNet< GUM_SCALAR >& getBayesNet() const;
135 
136  /// returns the allocator used
138 
139  /// @}
140 
141 
142  protected:
143  /// the Bayesian network used to fill the unobserved values
145 
146  /// the mapping betwen the BN's node ids and the database's columns
148 
149 
150  /// copy operator
153 
154  /// move operator
157  };
158 
159  } /* namespace learning */
160 
161 } /* namespace gum */
162 
163 
164 // always include the template implementation
165 #include <agrum/tools/database/DBRowGeneratorWithBN_tpl.h>
166 
167 #endif /* GUM_LEARNING_DBROW_GENERATOR_WITH_BN_H */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Bijection< NodeId, std::size_t, ALLOC< std::size_t > > nodeId2columns_
the mapping betwen the BN&#39;s node ids and the database&#39;s columns
DBRowGeneratorWithBN(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > &from)
copy constructor
DBRowGeneratorWithBN(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > & operator=(DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > &&from)
move operator
DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > & operator=(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > &from)
copy operator
allocator_type getAllocator() const
returns the allocator used
virtual void setBayesNet(const BayesNet< GUM_SCALAR > &new_bn)
assign a new Bayes net to the generator
DBRowGeneratorWithBN(const std::vector< DBTranslatedValueType, ALLOC< DBTranslatedValueType > > column_types, const BayesNet< GUM_SCALAR > &bn, const DBRowGeneratorGoal goal, 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
DBRowGeneratorWithBN(DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
const BayesNet< GUM_SCALAR > & getBayesNet() const
returns the Bayes net used by the generator
const BayesNet< GUM_SCALAR > * bn_
the Bayesian network used to fill the unobserved values
DBRowGeneratorWithBN(DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > &&from)
move constructor
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)