aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
IBayesNetGenerator_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
52
53namespace gum {
54
55 // Default constructor.
56 // Use the SimpleCPTGenerator for generating the BNs CPT.
57 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
61 dag_() {
62 GUM_CONSTRUCTOR(IBayesNetGenerator);
64
66 GUM_ERROR(OperationNotAllowed, " maxArcs value not possible ")
67
68 if (maxModality < 2) GUM_ERROR(OperationNotAllowed, " maxModality must be at least equal to 2 ")
69
72 }
73
74 // Destructor.
75 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
77 GUM_DESTRUCTOR(IBayesNetGenerator);
78 // delete cptGenerator_;
79 }
80
81 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
82 void IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::fillCPT(BayesNet< GUM_SCALAR >& bn) const {
83 for (auto node: bn.nodes())
84 this->generateCPT(bn.cpt(node).pos(bn.variable(node)), bn.cpt(node));
85 }
86
87 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
91
92 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
96
97 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
101
102 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
104 if (maxModality < 2)
105 GUM_ERROR(OperationNotAllowed, " maxModality must be at least equal to two ")
108 }
109
110 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
111 INLINE void IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::setNbrNodes(Size nbrNodes) {
112 if ((maxArcs_ < nbrNodes - 1) || (maxArcs_ > (nbrNodes * (nbrNodes - 1)) / 2))
113 GUM_ERROR(OperationNotAllowed, " nbrNodes value not possible ")
114
116 }
117
118 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
120 if (maxArcs < nbrNodes_ - 1 || maxArcs > (nbrNodes_ * (nbrNodes_ - 1)) / 2)
121 GUM_ERROR(OperationNotAllowed, " maxArcs value not possible ")
124 }
125
126 template < typename GUM_SCALAR, template < typename > class ICPTGenerator >
127 INLINE void IBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::fromDAG(BayesNet< GUM_SCALAR >& bn) {
128 bn.clear();
129
130 const auto width = (this->dag_.size() >= 100) ? 3 : 2;
131 int n = 0;
132 const auto& topo = this->dag_.topologicalOrder();
133 for (const auto node: topo) {
134 std::stringstream strBuff;
135 strBuff << "X" << std::setfill('0') << std::setw(width) << n++;
136 bn.add(RangeVariable(strBuff.str(), "", 0, long(1 + randomValue(this->maxModality_ - 1))),
137 node);
138 }
139 bn.beginTopologyTransformation();
140 for (auto arc: this->dag_.arcs()) {
141 bn.addArc(arc.tail(), arc.head());
142 }
143 bn.endTopologyTransformation();
144 }
146
147} /* namespace gum */
Interface-like class for generating Bayesian networks.
Class representing a Bayesian network.
Definition BayesNet.h:93
void setMaxArcs(Size maxArcs)
Modifies the value of the number of nodes imposed on the BayesGenerator.
void setNbrNodes(Size nbrNodes)
Modifies the value of the number of nodes imposed on the BayesGenerator.
void fromDAG(BayesNet< GUM_SCALAR > &bayesNet)
Virtual function that Generates a Bayesian networks.
Size maxModality() const
Return a constant reference to the maximum modality imposed on the IBayesNetGenerator.
void setMaxModality(Size maxModality)
Modifies the value of the number of nodes imposed on the BayesGenerator.
Size nbrNodes() const
Return a constant reference to the number of nodes imposed on the IBayesNetGenerator.
void fillCPT(BayesNet< GUM_SCALAR > &bn) const
function that insert random values in the CPT of each nodes according to the CPTGenerator.
virtual ~IBayesNetGenerator()
Destructor.
Size maxArcs() const
Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator.
IBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality)
constructor.
Exception : operation not allowed.
Defines a discrete random variable over an integer interval.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Header of gumRangeVariable.