aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
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-2026 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-2026 *
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
#pragma once
42
43
50
#include <
agrum/BN/generator/simpleBayesNetGenerator.h
>
51
52
#include <
agrum/base/core/utils_random.h
>
53
54
namespace
gum
{
55
56
// Use the SimpleCPTGenerator for generating the BNs CPT.
57
template
< GUM_Numeric GUM_SCALAR,
template
<
typename
>
class
ICPTGenerator >
58
SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator
(
Size
nbrNodes
,
59
Size
maxArcs
,
60
Size
maxModality
) :
61
IBayesNetGenerator
< GUM_SCALAR, ICPTGenerator >(
nbrNodes
,
maxArcs
,
maxModality
) {
62
GUM_CONSTRUCTOR(
SimpleBayesNetGenerator
);
63
}
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 <GumScalar 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
< GUM_Numeric GUM_SCALAR,
template
<
typename
>
class
ICPTGenerator >
79
SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::~SimpleBayesNetGenerator
() {
80
GUM_DESTRUCTOR(
SimpleBayesNetGenerator
);
81
}
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
< GUM_Numeric GUM_SCALAR,
template
<
typename
>
class
ICPTGenerator >
89
void
SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN
(
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 */
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes
Size nbrNodes() const
Definition
IBayesNetGenerator_tpl.h:93
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fromDAG
void fromDAG(BayesNet< GUM_SCALAR > &bayesNet)
Definition
IBayesNetGenerator_tpl.h:127
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_
DAG dag_
Definition
IBayesNetGenerator.h:187
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fillCPT
void fillCPT(BayesNet< GUM_SCALAR > &bn) const
Definition
IBayesNetGenerator_tpl.h:82
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes_
Size nbrNodes_
Definition
IBayesNetGenerator.h:184
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs
Size maxArcs() const
Definition
IBayesNetGenerator_tpl.h:98
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxModality
Size maxModality() const
Definition
IBayesNetGenerator_tpl.h:88
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs_
Size maxArcs_
Definition
IBayesNetGenerator.h:185
gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::IBayesNetGenerator
IBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality)
Definition
IBayesNetGenerator_tpl.h:59
gum::SimpleBayesNetGenerator::SimpleBayesNetGenerator
SimpleBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality=2)
Constructor.
Definition
simpleBayesNetGenerator_tpl.h:58
gum::SimpleBayesNetGenerator::~SimpleBayesNetGenerator
~SimpleBayesNetGenerator() override
Destructor.
Definition
simpleBayesNetGenerator_tpl.h:79
gum::SimpleBayesNetGenerator::generateBN
void generateBN(BayesNet< GUM_SCALAR > &bayesNet) override
function that generates a Bayesian networks.
Definition
simpleBayesNetGenerator_tpl.h:89
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
gum::randomValue
GUM_SHARED_PUBLIC Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
Definition
utils_random_inl.h:66
gum::randomProba
GUM_SHARED_PUBLIC double randomProba()
Returns a random double between 0 and 1 included (i.e.
Definition
utils_random_inl.h:73
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
simpleBayesNetGenerator.h
Class for generating Bayesian networks.
utils_random.h
Contains useful methods for random stuff.
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0