aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
MCBayesNetGenerator.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_MC_BAYES_NET_GENERATOR
50#define GUM_MC_BAYES_NET_GENERATOR
51
52#define NB_INIT_ITERATIONS 5000
53
54#include <fstream>
55#include <iostream>
56#include <set>
57#include <sstream>
58#include <vector>
59
60#include <agrum/agrum.h>
61
65
66namespace gum {
166 template < typename GUM_SCALAR,
167 template < typename > class ICPTGenerator = SimpleCPTGenerator,
168 template < typename > class ICPTDisturber = SimpleCPTDisturber >
170 public IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >,
171 public ICPTDisturber< GUM_SCALAR > {
172 public:
173 // ############################################################################
175 // ############################################################################
177
196 Idx maxModality = 2,
198 Idx p = 30,
199 Idx q = 40);
200
215 explicit MCBayesNetGenerator(BayesNet< GUM_SCALAR > bayesNet,
217 Idx p = 30,
218 Idx q = 40);
219
223
224 ~MCBayesNetGenerator() override;
225
227
228 // ############################################################################
230 // ############################################################################
232
237 void generateBN(BayesNet< GUM_SCALAR >& bayesNet) override;
238
252
254
255 // ############################################################################
257 // ############################################################################
260
266 Size iteration() const;
272 Idx p() const;
277 Idx q() const;
278
280
289 void setP(Idx p);
293 void setQ(Idx q);
294
296
297 protected:
300 /*
301 bool disturbing_;
302 BayesNet< GUM_SCALAR > bayesNettemp_;
303 HashTable< NodeId, Tensor< GUM_SCALAR >* > hashMarginal_;*/
304
308 bool _isPolytree_();
312 bool _connect_(NodeId i, NodeId j);
317 bool _directedPath_(NodeId tail, NodeId head);
323 void _insertArc_(NodeId i, NodeId j);
330 void _eraseArc_(NodeId i, NodeId j, bool mustbeconnex = true);
331
338 void _PMMx_poly_();
339
345 void _PMMx_multi_();
346
350 void _jump_poly_();
351
357 void _jump_multi_();
358
363 void _Add_or_Remove_();
364
369 void _Add_and_Remove_();
370
374 virtual bool _checkConditions_();
375
383 bool _is_connected_(const NodeId i, const NodeId j, NodeSet& excluded);
384
390 bool _directedPath_(NodeId tail, NodeId head, NodeSet& excluded);
391
395
396 void _chooseNodes_(NodeId& i, NodeId& j);
397
401 void _chooseCloseNodes_(NodeId& i, NodeId& j);
402
406
407 void _transformPoly_(Idx nbiter);
408
412 void _createTree_(Size BNSize);
413
420 NodeId _createPartTree_(Size BNSize, Idx& n);
421 };
422
423
424#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
425 extern template class MCBayesNetGenerator< double >;
426#endif
427
428} /*namespace gum*/
429
431#endif // MCBAYESNETGENERATOR
Interface-like class for generating Bayesian networks.
#define NB_INIT_ITERATIONS
Source implementation of MCBayesNetGenerator.
IBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality)
<agrum/BN/generator/MCayesNetGenerator.h>
bool _isPolytree_()
The function that verify if graph is a polytree.
void _jump_multi_()
In the case that the graph is a multiconnect graph, the function will choose randomly two nodes and w...
void _createTree_(Size BNSize)
The function that randomly generate a simple tree.
bool _is_connected_(const NodeId i, const NodeId j, NodeSet &excluded)
The internal function used by the previous is_connected.
NodeId _createPartTree_(Size BNSize, Idx &n)
The internal function used by createTree that randomly generate a simple tree.
void setQ(Idx q)
Modifies the value of the probability q imposed on the BayesNetGenerator.
void _PMMx_multi_()
In the case that the graph is a multiconnected graph, the function will, according to the probability...
void _transformPoly_(Idx nbiter)
The function that randomly change the simple tree into a polytree.
void _jump_poly_()
In the case that the graph is a polytree, the function will add a random arc by the use of the functi...
void _chooseCloseNodes_(NodeId &i, NodeId &j)
The function that randomly choose two neighbours nodes of the graph.
bool _connect_(NodeId i, NodeId j)
The function that verify if node i and j are connected.
void _insertArc_(NodeId i, NodeId j)
The function that will insert an arc between node i to node j, but only if there isn't any cycle crea...
void _PMMx_poly_()
In the case that the graph is a polytree, the function will, according to the probability p and q,...
bool _directedPath_(NodeId tail, NodeId head)
The function that verify if there is a oriented path from node i to node j.
void _eraseArc_(NodeId i, NodeId j, bool mustbeconnex=true)
The function that will remove the arc between node i and node j.
void setIteration(Size iteration)
Modifies the value of the number of iterations impose on the BayesNetGenerator.
Idx q() const
Return a constant reference to the probabilité imposed on the Markov Chain BayesNetGenerator.
void generateBN(BayesNet< GUM_SCALAR > &bayesNet) override
Generates a random Bayesian network.
virtual bool _checkConditions_()
The boolean function that will assert the respect of the constraint.
MCBayesNetGenerator(Size nbrNodes, Size maxArcs, Idx maxModality=2, Size iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
Constructor.
void setP(Idx p)
Modifies the value of the probability p imposed on the BayesNetGenerator.
Size iteration() const
Return a constant reference to the number of iteration imposed on the Markov Chain BayesNetGenerator.
void _Add_and_Remove_()
The function will remove and add a random arc changing the topology of the graph but asserting its co...
void _chooseNodes_(NodeId &i, NodeId &j)
The function that randomly choose two nodes of the graph.
Idx p() const
Return a constant reference to the probabilité p imposed on the Markov Chain BayesNetGenerator.
void _Add_or_Remove_()
The function will add or remove a random arc in the graph using the functions insertArc and removeArc...
~MCBayesNetGenerator() override
Destructor.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Implementation of a Shafer-Shenoy's-like version of lazy propagation for inference in Bayesian networ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Source implementation of SimpleCPTDisturber.