aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
influenceDiagram.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
50
51#ifndef GUM_INF_DIAG_H
52#define GUM_INF_DIAG_H
53
54#include <string>
55#include <utility>
56
57#include <agrum/agrum.h>
58
61
62namespace gum {
63
71 template < GUM_Numeric GUM_SCALAR >
72 class InfluenceDiagram: public DAGmodel {
73 // friend class InfluenceDiagramFactory<GUM_SCALAR>;
74
75 public:
102 static InfluenceDiagram< GUM_SCALAR > fastPrototype(std::string_view dotlike, Size domainSize);
103 static InfluenceDiagram< GUM_SCALAR > fastPrototype(std::string_view dotlike,
104 std::string_view domain = "[2]");
105
106 // ===========================================================================
108 // ===========================================================================
110
115
119 ~InfluenceDiagram() override;
120
125
130
133
135 bool operator==(const InfluenceDiagram< GUM_SCALAR >& other) const;
136
138 std::string toDot() const;
139
141 std::string toString() const;
142
143 void clear();
144
145 // ===========================================================================
147 // ===========================================================================
149
154 virtual const Tensor< GUM_SCALAR >& cpt(NodeId varId) const;
155
156 virtual const Tensor< GUM_SCALAR >& cpt(std::string_view name) const final;
157
162 virtual const Tensor< GUM_SCALAR >& utility(NodeId varId) const;
163
164 virtual const Tensor< GUM_SCALAR >& utility(std::string_view name) const final;
165
169 bool isUtilityNode(NodeId varId) const;
170
171 bool isUtilityNode(std::string_view name) const;
172
176 bool isDecisionNode(NodeId varId) const;
177
178 bool isDecisionNode(std::string_view name) const;
179
183 bool isChanceNode(NodeId varId) const;
184
185 bool isChanceNode(std::string_view name) const;
186
190 Size utilityNodeSize() const;
191
195 Size chanceNodeSize() const;
196
200 Size decisionNodeSize() const;
201
202 const DiscreteVariable& variable(std::string_view name) const;
203
217 NodeId add(const DiscreteVariable& variable, NodeId id = 0);
218
234
250
263
279 NodeId id = 0);
280
296 NodeId id = 0);
297
298
316 NodeId add(std::string_view fast_description, unsigned int default_nbrmod = 2);
317
337 NodeId addChanceNode(std::string_view fast_description, unsigned int default_nbrmod = 2);
338
347 NodeId addUtilityNode(std::string_view fast_description);
348
365 NodeId addDecisionNode(std::string_view fast_description, unsigned int default_nbrmod = 2);
366
367
375 void erase(NodeId id);
376
377 void erase(std::string_view name);
378
386 void erase(const DiscreteVariable& var);
387
392 void changeVariableName(NodeId id, std::string_view new_name);
393
394 void changeVariableName(std::string_view name, std::string_view new_name);
395
397 // ===========================================================================
399 // ===========================================================================
401
411 void addArc(NodeId tail, NodeId head);
412
413 void addArc(std::string_view tail, std::string_view head);
414
422 void eraseArc(const Arc& arc);
423
432 void eraseArc(NodeId tail, NodeId head);
433
434 void eraseArc(std::string_view tail, std::string_view head);
435
437
438 // ===========================================================================
440 // ===========================================================================
442
446 bool decisionOrderExists() const;
447
452
457 std::vector< NodeId > decisionOrder() const;
458
462 bool existsPathBetween(NodeId src, NodeId dest) const;
463
464 bool existsPathBetween(std::string_view src, std::string_view dest) const;
465
470 const List< NodeSet >& getPartialTemporalOrder(bool clear = true) const;
471
473
488
489 protected:
491 virtual void moralGraph_(UndiGraph& graph) const;
492
496 void removeTables_();
497
502
506 NodeId addNode_(const DiscreteVariable& variableType, NodeId DesiredId);
507
511 Sequence< NodeId > getChildrenDecision_(NodeId parentDecision) const;
512
513 public:
519
520 private:
525
528 };
529
530} /* namespace gum */
531
533
534#endif /* GUM_INF_DIAG_H */
Class representing probabilistic DAG model.
The base class for all directed edges.
Base class for dag.
Definition DAG.h:121
DAGmodel()
Default constructor.
Definition DAGmodel.cpp:49
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const VariableNodeMap & variableNodeMap() const override
Returns a constant reference to the VariableNodeMap of this model.
const DiscreteVariable & variableFromName(std::string_view name) const override
Returns a constant reference over a variable given its name.
NodeId nodeId(const DiscreteVariable &var) const override
Returns the NodeId of a variable.
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
Base class for discrete random variable.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs/utilities change their dimension with a cost in time.
List< NodeSet > _temporalOrder_
The temporal order.
Size chanceNodeSize() const
Returns the number of chance nodes.
InfluenceDiagram()
Default constructor.
const List< NodeSet > & getPartialTemporalOrder(bool clear=true) const
Returns partial temporal ordering.
NodeId addChanceNode(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it's associate node and it's CPT.
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram's tensor nodes cpt if necessary.
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs/utilities dimensions.
static InfluenceDiagram< GUM_SCALAR > fastPrototype(std::string_view dotlike, Size domainSize)
Create an Influence Diagram with a dot-like syntax which specifies:
void removeTables_()
Removing ancient table.
void eraseArc(const Arc &arc)
Removes an arc in the ID, and update diagram's tensor nodes cpt if necessary.
NodeProperty< Tensor< GUM_SCALAR > * > _tensorMap_
Mapping between tensor variable's id and their CPT.
virtual void moralGraph_(UndiGraph &graph) const
Returns the moral graph of this InfluenceDiagram.
NodeId addNode_(const DiscreteVariable &variableType, NodeId DesiredId)
Add a node.
std::string toDot() const
bool decisionOrderExists() const
True if a directed path exist with all decision nodes.
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
InfluenceDiagram< GUM_SCALAR > & operator=(const InfluenceDiagram< GUM_SCALAR > &source)
Copy Operator.
NodeId add(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it's associate node and it's CPT.
NodeId addUtilityNode(const DiscreteVariable &variable, NodeId id=0)
Add a utility variable, it's associate node and it's UT.
std::string toString() const
void copyStructureAndTables_(const InfluenceDiagram< GUM_SCALAR > &IDsource)
Copying tables from another influence diagram.
NodeId addDecisionNode(const DiscreteVariable &variable, NodeId id=0)
Add a decision variable.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
bool operator==(const InfluenceDiagram< GUM_SCALAR > &other) const
Size utilityNodeSize() const
Returns the number of utility nodes.
NodeProperty< Tensor< GUM_SCALAR > * > _utilityMap_
Mapping between utility variable's id and their utility table.
gum::DAG getDecisionGraph() const
Returns the temporal Graph.
void changeVariableName(NodeId id, std::string_view new_name)
we allow the user to change the name of a variable
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
const DiscreteVariable & variable(std::string_view name) const
Returns the CPT of a tensor variable.
Sequence< NodeId > getChildrenDecision_(NodeId parentDecision) const
Returns the list of children decision for a given nodeId.
Size decisionNodeSize() const
Returns the number of decision nodes.
void erase(NodeId id)
Erase a Variable from the network and remove the variable from all his children.
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a tensor variable.
std::vector< NodeId > decisionOrder() const
Returns the sequence of decision nodes in the directed path.
virtual const Tensor< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
bool existsPathBetween(NodeId src, NodeId dest) const
Returns true if a path exists between two nodes.
~InfluenceDiagram() override
Destructor.
Generic doubly linked lists.
Definition list.h:378
Base class for undirected graphs.
Definition undiGraph.h:130
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Template implementation of InfluenceDiagram/InfluenceDiagram.h classes.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Header of the Tensor class.