aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BNLearner.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 A basic pack of learning algorithms that can easily be used
24  *
25  * The pack currently contains K2, GreedyHillClimbing and
26  *LocalSearchWithTabuList
27  *
28  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
29  */
30 #ifndef GUM_LEARNING_BN_LEARNER_H
31 #define GUM_LEARNING_BN_LEARNER_H
32 
33 #include <sstream>
34 #include <algorithm>
35 #include <vector>
36 
37 #include <agrum/BN/BayesNet.h>
38 #include <agrum/agrum.h>
39 #include <agrum/tools/core/bijection.h>
40 #include <agrum/tools/core/sequence.h>
41 #include <agrum/tools/graphs/DAG.h>
42 
43 #include <agrum/BN/learning/BNLearnUtils/genericBNLearner.h>
44 
45 namespace gum {
46 
47  namespace learning {
48 
49  class BNLearnerListener;
50 
51  /** @class BNLearner
52  * @brief A pack of learning algorithms that can easily be used
53  *
54  * The pack currently contains K2, GreedyHillClimbing and
55  * LocalSearchWithTabuList
56  * @ingroup learning_group
57  */
58  template < typename GUM_SCALAR >
59  class BNLearner: public genericBNLearner {
60  public:
61  // ##########################################################################
62  /// @name Constructors / Destructors
63  // ##########################################################################
64  /// @{
65 
66  /// default constructor
67  /**
68  * read the database file for the score / parameter estimation and var
69  * names
70  */
71  BNLearner(const std::string& filename,
72  const std::vector< std::string >& missing_symbols = {"?"});
73 
74  BNLearner(const DatabaseTable<>& db);
75 
76  /**
77  * @brief Read the database file for the score / parameter estimation and
78  * var names.
79  *
80  * If modalities = { 1 -> {True, False, Big} }, then the node of id 1 in
81  * the BN will have 3 modalities, the first one being True, the second
82  * one being False, and the third bein Big.
83  *
84  * A parsing of the database will allow to determine which ones are
85  * really necessary and will keep them in the order specified by the user
86  * (NodeProperty modalities). If parse_database is set to false (the
87  * default), then the modalities specified by the user will be considered
88  * as being exactly those of the variables of the BN (as a consequence,
89  * if we find other values in the database, an exception will be raised
90  * during learning).
91  *
92  * @param filename The file to learn from.
93  * @param modalities indicate for some nodes (not necessarily all the
94  * nodes of the BN) which modalities they should have and in which order
95  * these modalities should be stored into the nodes.
96  * @param parse_database if true, the modalities specified by the user
97  * will be considered as a superset of the modalities of the variables.
98  */
99  /*
100  BNLearner(const std::string& filename,
101  const NodeProperty< Sequence< std::string > >& modalities );
102  */
103 
104 
105  /**
106  * Wrapper for BNLearner (filename,modalities,parse_database) using a bn
107  * to find those modalities and nodeids.
108  **/
109  BNLearner(const std::string& filename,
110  const gum::BayesNet< GUM_SCALAR >& src,
111  const std::vector< std::string >& missing_symbols = {"?"});
112 
113  /// copy constructor
114  BNLearner(const BNLearner&);
115 
116  /// move constructor
117  BNLearner(BNLearner&&);
118 
119  /// destructor
120  virtual ~BNLearner();
121 
122  /// @}
123 
124  // ##########################################################################
125  /// @name Operators
126  // ##########################################################################
127  /// @{
128 
129  /// copy operator
130  BNLearner& operator=(const BNLearner&);
131 
132  /// move operator
133  BNLearner& operator=(BNLearner&&);
134 
135  /// @}
136 
137  /// learn a Bayes Net from a file (must have read the db before)
138  BayesNet< GUM_SCALAR > learnBN();
139 
140  /// learns a BN (its parameters) when its structure is known
141  /**
142  * @param dag the structure of the Bayesian network
143  * @param takeIntoAccountScore The dag passed in argument may have
144  * been learnt from a structure learning. In this case, if the score used
145  * to learn the structure has an implicit apriori (like K2 which has a
146  * 1-smoothing apriori), it is important to also take into account this
147  * implicit apriori for parameter learning. By default, if a score
148  * exists, we will learn parameters by taking into account the apriori
149  * specified by methods useAprioriXXX () + the implicit apriori of the
150  * score, else we just take into account the apriori specified by
151  * useAprioriXXX () */
152  BayesNet< GUM_SCALAR > learnParameters(const DAG& dag, bool takeIntoAccountScore = true);
153 
154  // learns a BN (its parameters) when its structure is known
155  /** @param take_into_account_score The dag of the BN which was passed in
156  * argument to the BNLearner may have
157  * been learnt from a structure learning. In this case, if the score used
158  * to learn the structure has an implicit apriori (like K2 which has a
159  * 1-smoothing apriori), it is important to also take into account this
160  * implicit apriori for parameter learning. By default, if a score
161  * exists, we will learn parameters by taking into account the apriori
162  * specified by methods useAprioriXXX () + the implicit apriori of the
163  * score, else we just take into account the apriori specified by
164  * useAprioriXXX ()
165  * @throw MissingVariableInDatabase if a variable of the BN is not found
166  * in the database.
167  * @throw UnknownLabelInDatabase if a label is found in the databast that
168  * do not correpond to the variable.
169  */
170  BayesNet< GUM_SCALAR > learnParameters(bool take_into_account_score = true);
171 
172  private:
173  /// read the first line of a file to find column names
174  NodeProperty< Sequence< std::string > > _labelsFromBN_(const std::string& filename,
175  const BayesNet< GUM_SCALAR >& src);
176  };
177 
178  } /* namespace learning */
179 
180 } /* namespace gum */
181 
182 /// always include templated methods
183 #include <agrum/BN/learning/BNLearner_tpl.h>
184 
185 #endif /* GUM_LEARNING_BN_LEARNER_H */