aGrUM 2.3.2
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-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
51
53
54namespace gum {
55 // Default constructor.
56 // Use the SimpleCPTGenerator for generating the IDs CPT.
57 template < typename GUM_SCALAR >
63
64 // Use this constructor if you want to use a different policy for generating
65 // CPT than the default one.
66 // The cptGenerator will be erased when the destructor is called.
67 // @param cptGenerator The policy used to generate CPT.
68 template < typename GUM_SCALAR >
70 ICPTGenerator< GUM_SCALAR >* cptGenerator) {
71 GUM_CONSTRUCTOR(InfluenceDiagramGenerator)
72 _cptGenerator_ = cptGenerator;
74 }
75
76 // Use this constructor if you want to use a different policy for generating
77 // UT than the default one.
78 // The utGenerator will be erased when the destructor is called.
79 // @param utGenerator The policy used to generate UT.
80 template < typename GUM_SCALAR >
86
87 // Use this constructor if you want to use a different policy for generating
88 // both CPT & UT than the defaults ones.
89 // The cptGenerator and utGenerator will be erased when the destructor is
90 // called.
91 // @param cptGenerator The policy used to generate CPT.
92 // @param utGenerator The policy used to generate UT.
93 template < typename GUM_SCALAR >
95 ICPTGenerator< GUM_SCALAR >* cptGenerator,
96 UTGenerator* utGenerator) {
97 GUM_CONSTRUCTOR(InfluenceDiagramGenerator)
98 _cptGenerator_ = cptGenerator;
99 _utGenerator_ = utGenerator;
100 }
101
102 // Destructor.
103 template < typename GUM_SCALAR >
109
110 // Generates an influence diagram using floats.
111 // @param nbrNodes The number of nodes in the generated ID.
112 // @param arcdensity The probability of adding an arc between two nodes.
113 // @param chanceNodeDensity The proportion of chance node
114 // @param utilityNodeDensity The proportion of utility node
115 // @param max_modality Each DRV has from 2 to max_modality modalities
116 // @return A IDs randomly generated.
117 template < typename GUM_SCALAR >
120 GUM_SCALAR arcDensity,
121 GUM_SCALAR chanceNodeDensity,
122 GUM_SCALAR utilityNodeDensity,
123 Size max_modality) {
124 auto influenceDiagram = new InfluenceDiagram< GUM_SCALAR >();
125 // First we add nodes
127 std::stringstream strBuff;
128 Size nb_mod;
129
130 for (Idx i = 0; i < nbrNodes; ++i) {
131 strBuff << 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(
141 i,
142 influenceDiagram->addChanceNode(RangeVariable(strBuff.str(), "", 0, nb_mod - 1)));
143 else if (d < (cnd + und))
144 map.insert(i, influenceDiagram->addUtilityNode(RangeVariable(strBuff.str(), "", 0, 0)));
145 else
146 map.insert(
147 i,
148 influenceDiagram->addDecisionNode(RangeVariable(strBuff.str(), "", 0, nb_mod - 1)));
149
150 strBuff.str("");
151 }
152
153 // We add arcs
154 GUM_SCALAR p = arcDensity;
155
156 for (Size i = 0; i < nbrNodes; ++i)
157 if (!influenceDiagram->isUtilityNode(map[i]))
158 for (Size j = i + 1; j < nbrNodes; ++j)
159 if (((GUM_SCALAR)randomProba()) < p) { influenceDiagram->addArc(map[i], map[j]); }
160
161 // And fill the CPTs and UTs
162 for (Size i = 0; i < nbrNodes; ++i)
163 if (influenceDiagram->isChanceNode(map[i]))
164 _cptGenerator_->generateCPT(
165 influenceDiagram->cpt(map[i]).pos(influenceDiagram->variable(map[i])),
166 influenceDiagram->cpt(map[i]));
167 else if (influenceDiagram->isUtilityNode(map[i]))
168 _utGenerator_->generateUT(
169 influenceDiagram->utility(map[i]).pos(influenceDiagram->variable(map[i])),
170 influenceDiagram->utility(map[i]));
171
172 _checkTemporalOrder_(influenceDiagram);
173
174 return influenceDiagram;
175 }
176
177 template < typename GUM_SCALAR >
180 if (!infdiag->decisionOrderExists()) {
181 Sequence< NodeId > order = infdiag->topologicalOrder();
182
183 auto orderIter = order.begin();
184
185 while ((orderIter != order.end()) && (!infdiag->isDecisionNode(*orderIter)))
186 ++orderIter;
187
188 if (orderIter == order.end()) return;
189
190 NodeId parentDecision = (*orderIter);
191
192 ++orderIter;
193
194 for (; orderIter != order.end(); ++orderIter)
195 if (infdiag->isDecisionNode(*orderIter)) {
196 infdiag->addArc(parentDecision, (*orderIter));
197 parentDecision = (*orderIter);
198 }
199 }
200 }
201} /* 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:637
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.
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
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.
Defines a discrete random variable over an integer interval.
<agrum/BN/generator/simpleCPTGenerator.h>
Class for generating Utility Tables.
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.
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.
Class for generating Bayesian networks.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Contains useful methods for random stuff.