aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
influenceDiagramGenerator_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
52
54
55namespace gum {
56 // Default constructor.
57 // Use the SimpleCPTGenerator for generating the IDs CPT.
58 template < GUM_Numeric GUM_SCALAR >
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 < GUM_Numeric GUM_SCALAR >
71 ICPTGenerator< GUM_SCALAR >* cptGenerator) {
72 GUM_CONSTRUCTOR(InfluenceDiagramGenerator)
73 _cptGenerator_ = cptGenerator;
75 }
76
77 // Use this constructor if you want to use a different policy for generating
78 // UT than the default one.
79 // The utGenerator will be erased when the destructor is called.
80 // @param utGenerator The policy used to generate UT.
81 template < GUM_Numeric GUM_SCALAR >
87
88 // Use this constructor if you want to use a different policy for generating
89 // both CPT & UT than the defaults ones.
90 // The cptGenerator and utGenerator will be erased when the destructor is
91 // called.
92 // @param cptGenerator The policy used to generate CPT.
93 // @param utGenerator The policy used to generate UT.
94 template < GUM_Numeric GUM_SCALAR >
96 ICPTGenerator< GUM_SCALAR >* cptGenerator,
97 UTGenerator* utGenerator) {
98 GUM_CONSTRUCTOR(InfluenceDiagramGenerator)
99 _cptGenerator_ = cptGenerator;
100 _utGenerator_ = utGenerator;
101 }
102
103 // Destructor.
104 template < GUM_Numeric GUM_SCALAR >
110
111 // Generates an influence diagram using floats.
112 // @param nbrNodes The number of nodes in the generated ID.
113 // @param arcdensity The probability of adding an arc between two nodes.
114 // @param chanceNodeDensity The proportion of chance node
115 // @param utilityNodeDensity The proportion of utility node
116 // @param max_modality Each DRV has from 2 to max_modality modalities
117 // @return A IDs randomly generated.
118 template < GUM_Numeric GUM_SCALAR >
121 GUM_SCALAR arcDensity,
122 GUM_SCALAR chanceNodeDensity,
123 GUM_SCALAR utilityNodeDensity,
124 Size max_modality) {
125 auto influenceDiagram = new InfluenceDiagram< GUM_SCALAR >();
126 // First we add nodes
128 Size nb_mod;
129
130 for (Idx i = 0; i < nbrNodes; ++i) {
131 const auto varName = std::format("{}", i);
132 nb_mod = (max_modality == 2) ? 2 : 2 + randomValue(max_modality - 1);
133
134 GUM_SCALAR cnd = chanceNodeDensity;
135 GUM_SCALAR und = utilityNodeDensity;
136
137 auto d = (GUM_SCALAR)randomProba();
138
139 if (d < cnd)
140 map.insert(i, influenceDiagram->addChanceNode(RangeVariable(varName, "", 0, nb_mod - 1)));
141 else if (d < (cnd + und))
142 map.insert(i, influenceDiagram->addUtilityNode(RangeVariable(varName, "", 0, 0)));
143 else
144 map.insert(i, influenceDiagram->addDecisionNode(RangeVariable(varName, "", 0, nb_mod - 1)));
145 }
146
147 // We add arcs
148 GUM_SCALAR p = arcDensity;
149
150 for (Size i = 0; i < nbrNodes; ++i)
151 if (!influenceDiagram->isUtilityNode(map[i]))
152 for (Size j = i + 1; j < nbrNodes; ++j)
153 if (((GUM_SCALAR)randomProba()) < p) { influenceDiagram->addArc(map[i], map[j]); }
154
155 // And fill the CPTs and UTs
156 for (Size i = 0; i < nbrNodes; ++i)
157 if (influenceDiagram->isChanceNode(map[i]))
158 _cptGenerator_->generateCPT(
159 influenceDiagram->cpt(map[i]).pos(influenceDiagram->variable(map[i])),
160 influenceDiagram->cpt(map[i]));
161 else if (influenceDiagram->isUtilityNode(map[i]))
162 _utGenerator_->generateUT(
163 influenceDiagram->utility(map[i]).pos(influenceDiagram->variable(map[i])),
164 influenceDiagram->utility(map[i]));
165
166 _checkTemporalOrder_(influenceDiagram);
167
168 return influenceDiagram;
169 }
170
171 template < GUM_Numeric GUM_SCALAR >
174 if (!infdiag->decisionOrderExists()) {
175 Sequence< NodeId > order = infdiag->topologicalOrder();
176
177 auto orderIter = order.begin();
178
179 while ((orderIter != order.end()) && (!infdiag->isDecisionNode(*orderIter)))
180 ++orderIter;
181
182 if (orderIter == order.end()) return;
183
184 NodeId parentDecision = (*orderIter);
185
186 ++orderIter;
187
188 for (; orderIter != order.end(); ++orderIter)
189 if (infdiag->isDecisionNode(*orderIter)) {
190 infdiag->addArc(parentDecision, (*orderIter));
191 parentDecision = (*orderIter);
192 }
193 }
194 }
195} /* namespace gum */
Sequence< NodeId > topologicalOrder() const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
The class for generic Hash Tables.
Definition hashTable.h:640
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
InfluenceDiagram< GUM_SCALAR > * generateID(Size nbrNodes, GUM_SCALAR arcDensity, GUM_SCALAR chanceNodeDensity, GUM_SCALAR utilityNodeDensity, Size max_modality=2)
Generates an influence diagram using floats.
void _checkTemporalOrder_(InfluenceDiagram< GUM_SCALAR > *infdiag)
ICPTGenerator< GUM_SCALAR > * _cptGenerator_
Class representing an Influence Diagram.
InfluenceDiagram()
Default constructor.
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram's tensor nodes cpt if necessary.
bool decisionOrderExists() const
True if a directed path exist with all decision nodes.
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
Defines a discrete random variable over an integer interval.
SimpleCPTGenerator()
Default constructor.
SimpleUTGenerator()
Default constructor.
Abstract class for generating Utility Tables.
Definition UTGenerator.h:63
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.
GUM_SHARED_PUBLIC Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
GUM_SHARED_PUBLIC double randomProba()
Returns a random double between 0 and 1 included (i.e.
Class for generating Bayesian networks.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Contains useful methods for random stuff.