aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
maxParentsMCBayesNetGenerator.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 Class for generating Bayesian networks using MC algorithm
24  * cf. [Ide and Cozman, 2002]
25  *
26  * @author Ariele MAESANO and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_MAX_PARENTS_MC_BAYES_NET_GENERATOR
30 #define GUM_MAX_PARENTS_MC_BAYES_NET_GENERATOR
31 
32 #include <agrum/agrum.h>
33 
34 #include <fstream>
35 #include <iostream>
36 #include <set>
37 #include <vector>
38 
39 #include <sstream>
40 
41 #ifdef HAVE_DIRENT_H
42 # include <dirent.h>
43 #else
44 # include <agrum/tools/core/mvsc/dirent.h>
45 #endif
46 
47 
48 #include <agrum/BN/BayesNet.h>
49 #include <agrum/BN/generator/MCBayesNetGenerator.h>
50 #include <agrum/BN/generator/simpleCPTDisturber.h>
51 #include <agrum/BN/generator/simpleCPTGenerator.h>
52 #include <agrum/tools/variables/labelizedVariable.h>
53 
54 namespace gum {
55  /**
56  * @class MaxParentsMCBayesNetGenerator maxParentsMCBayesNetGenerator.h
57  *<agrum/BN/generator/SimpleMCayesNetGenerator.h>
58  * @brief Class for generating Bayesian networks with Markov chains.
59  * @ingroup bn_generator
60  *
61  * This class is inherited from MCBayesNetGenerator and is an example of
62  *Markov
63  *Chain Bayesian network Generator that can be implemented.
64  * Here a constraint is added which is the maximum number of parents that a
65  *node
66  *can have.
67  * To create our own MCBayesNetGenerator with a specific constraint you
68  *must:\n
69  * 1) Create the class
70  *XXXXXXMCBayesNetGenerator<GUM_SCALAR,ICPTGenerator,ICPTDisturber>.\n
71  * 2) Add a variable specific to the constraint (here maxParents_). \n
72  * 3) Verify in the function checkCondition_() the wanted constraint and then
73  *launch
74  * MCBayesNetGenerator<GUM_SCALAR,ICPTGenerator,ICPTDisturber>:: _checkConditions_()
75  *to verify the default constraint.\n
76  * 4) Define the getters and setters of the constraint variable.\n
77  * 5) And verify in the constructor that the constraint is not in
78  *contradiction
79  *with the generator here maxParent must be >= 1.\n
80  *
81  * @warning Be Careful when entering the parameters, high Values may cause
82  *the
83  *density of the Bayesian network to be too high
84  * resulting in the failure of most of the inference Methods. \n
85  * */
86  template < typename GUM_SCALAR,
87  template < typename > class ICPTGenerator = SimpleCPTGenerator,
88  template < typename > class ICPTDisturber = SimpleCPTDisturber >
89  class MaxParentsMCBayesNetGenerator:
90  public MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber > {
91  public:
92  // ############################################################################
93  /// @name Constructors / Destructor
94  // ############################################################################
95  /// @{
96 
97  /**
98  * Constructor.
99  * Use by default the SimpleCPTGenerator for generating the BNs CPT
100  * and the SimpleCPTDisturber to tweak the CPT when the dimension of the
101  * table
102  * changes.
103  * @param nbrNodes The number of nodes in the generated BN.
104  * @param maxArcs The maximum number of Arcs.
105  * @param maxModality Each DRV has from 2 to maxModality modalities
106  * @param maxParents The number of maximum parents for each node imposed on
107  * the
108  * generator
109  * @param iteration The number of iterations wanted to repeat the algorithm
110  * @param p probability for the change of the state (see \ref probability_p_q
111  * "use
112  * of p and q" )
113  * @param q probability for the change of the state (see \ref probability_p_q
114  * "use
115  * of p and q" )
116  */
117  MaxParentsMCBayesNetGenerator(Size nbrNodes,
118  Size maxArcs,
119  Size maxModality = 2,
120  Size maxParents = 1,
121  Idx iteration = 5000,
122  Idx p = 30,
123  Idx q = 40);
124 
125  /**
126  * Constructor.
127  * Use by default the SimpleCPTGenerator for generating the BNs CPT
128  * and the SimpleCPTDisturber to tweak the CPT when the dimension of the
129  * table
130  * changes.
131  * @param bayesNet the IBayesNet used as reference to fill the parameters
132  * nbrNodes, maxArcs and maxModality
133  * @param maxParents The number of maximum parents for each node imposed on
134  * the
135  * generator
136  * @param iteration The number of iterations wanted to repeat the algorithm
137  * @param p probability for the change of the state (see \ref probability_p_q
138  * "use
139  * of p and q" )
140  * @param q probability for the change of the state (see \ref probability_p_q
141  * "use
142  * of p and q" )
143  */
144  explicit MaxParentsMCBayesNetGenerator(BayesNet< GUM_SCALAR > bayesNet,
145  Size maxParents = 2,
146  Idx iteration = 5000,
147  Idx p = 30,
148  Idx q = 40);
149 
150  /**
151  * Destructor.
152  */
153  ~MaxParentsMCBayesNetGenerator() final;
154 
155  /// @}
156 
157  // ############################################################################
158  /// @name Getters and Setters
159  // ############################################################################
160  /// @{
161  ///@name Getters
162 
163  /**
164  * Return a constant reference to the number of maximum parents imposed on
165  * the
166  * Markov Chain BayesNetGenerator.
167  */
168  Size maxParents() const;
169 
170  ///@name Setters
171  /**
172  * Modifies the value of the number of maximum parents imposed on the
173  * BayesNetGenerator
174  */
175  void setMaxParents(Size maxParents);
176  /// @}
177 
178  protected:
179  Size maxParents_;
180 
181  private:
182  /**
183  * function to holding the the specification wanted for the Bayesian markov.
184  * @return boolean state that verify the conditions
185  */
186 
187  bool _checkConditions_() final;
188  };
189 
190 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
191  extern template class MaxParentsMCBayesNetGenerator< double >;
192 #endif
193 
194 } /*namespace gum*/
195 
196 #include <agrum/BN/generator/maxParentsMCBayesNetGenerator_tpl.h>
197 #endif // MCBAYESNETGENERATOR