aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DBRowGeneratorWithBN.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 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, template < typename > class ALLOC = std::allocator >
82  public:
83  /// type for the allocators passed in arguments of methods
85 
86  // ##########################################################################
87  /// @name Constructors / Destructors
88  // ##########################################################################
89 
90  /// @{
91 
92  /// default constructor
95  const BayesNet< GUM_SCALAR >& bn,
98  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
100 
101  /// copy constructor
102  DBRowGeneratorWithBN(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >& from);
103 
104  /// copy constructor with a given allocator
105  DBRowGeneratorWithBN(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >& from,
106  const allocator_type& alloc);
107 
108  /// move constructor
109  DBRowGeneratorWithBN(DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >&& from);
110 
111  /// move constructor with a given allocator
112  DBRowGeneratorWithBN(DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >&& from,
113  const allocator_type& alloc);
114 
115  /// destructor
117 
118  /// @}
119 
120 
121  // ##########################################################################
122  /// @name Accessors / Modifiers
123  // ##########################################################################
124 
125  /// @{
126 
127  /// assign a new Bayes net to the generator
128  virtual void setBayesNet(const BayesNet< GUM_SCALAR >& new_bn);
129 
130  /// returns the Bayes net used by the generator
131  const BayesNet< GUM_SCALAR >& getBayesNet() const;
132 
133  /// returns the allocator used
135 
136  /// @}
137 
138 
139  protected:
140  /// the Bayesian network used to fill the unobserved values
142 
143  /// the mapping betwen the BN's node ids and the database's columns
145 
146 
147  /// copy operator
150 
151  /// move operator
154  };
155 
156  } /* namespace learning */
157 
158 } /* namespace gum */
159 
160 
161 // always include the template implementation
162 #include <agrum/tools/database/DBRowGeneratorWithBN_tpl.h>
163 
164 #endif /* GUM_LEARNING_DBROW_GENERATOR_WITH_BN_H */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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)