aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
simpleBayesNetGenerator_tpl.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#pragma once
41
42
50
52
53namespace gum {
54
55 // Use the SimpleCPTGenerator for generating the BNs CPT.
56 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
64
65 // Use this constructor if you want to use a different policy for generating
66 // CPT than the default one.
67 // The cptGenerator will be erased when the destructor is called.
68 // @param cptGenerator The policy used to generate CPT.
69 /*template <typename GUM_SCALAR, template<class> class ICPTGenerator>
70 SimpleBayesNetGenerator<GUM_SCALAR,ICPTGenerator>::SimpleBayesNetGenerator(CPTGenerator*
71 cptGenerator ,Size nbrNodes, float density, Size maxModality):
72 IBayesNetGenerator<GUM_SCALAR,ICPTGenerator>(cptGenerator
73 ,nbrNodes,density,maxModality) {
74 GUM_CONSTRUCTOR ( SimpleBayesNetGenerator )
75 }*/
76
77 // Destructor.
78 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
82
83 // Generates a Bayesian network using floats.
84 // @param nbrNodes The number of nodes in the generated BN.
85 // @param density The probability of adding an arc between two nodes.
86 // @return A BNs randomly generated.
87
88 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
90 BayesNet< GUM_SCALAR >& bayesNet) {
91 this->dag_.clear();
92 this->dag_.addNodes(this->nbrNodes_);
93
94 // We add arcs
95 float density = (float)(this->maxArcs_ * 2) / (float)(this->nbrNodes_ * (this->nbrNodes_ - 1));
96 for (Size i = 0; i < this->nbrNodes_; ++i)
97 for (Size j = i + 1; j < this->nbrNodes_; ++j)
98 if (randomProba() < density) this->dag_.addArc(i, j);
99
100 // Adding arcs until we reach the maxArcs_ number
101 while (this->dag_.sizeArcs() < this->maxArcs_) {
102 Size i = randomValue(this->nbrNodes_);
103 Size j = randomValue(this->nbrNodes_);
104 if (i != j) {
105 if (i > j) std::swap(i, j);
106
107 if (!this->dag_.existsArc(i, j)) this->dag_.addArc(i, j);
108 }
109 }
110
111 this->fromDAG(bayesNet);
112 this->fillCPT(bayesNet);
113 }
114} /* namespace gum */
IBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality)
SimpleBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality=2)
Constructor.
void generateBN(BayesNet< GUM_SCALAR > &bayesNet) override
function that generates a Bayesian networks.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
double randomProba()
Returns a random double between 0 and 1 included (i.e.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Class for generating Bayesian networks.
Contains useful methods for random stuff.