aGrUM 2.3.2
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-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
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 < typename GUM_SCALAR >
72 class InfluenceDiagram: public DAGmodel {
73 // friend class InfluenceDiagramFactory<GUM_SCALAR>;
74
75 public:
102 static InfluenceDiagram< GUM_SCALAR > fastPrototype(const std::string& dotlike,
103 Size domainSize);
104 static InfluenceDiagram< GUM_SCALAR > fastPrototype(const std::string& dotlike,
105 const std::string& domain = "[2]");
106
107 // ===========================================================================
109 // ===========================================================================
111
116
120 ~InfluenceDiagram() override;
121
126
131
133 bool operator==(const InfluenceDiagram< GUM_SCALAR >& other) const;
134
136 return !operator==(other);
137 }
138
140 std::string toDot() const;
141
143 std::string toString() const;
144
145 void clear();
146
147 // ===========================================================================
149 // ===========================================================================
151
156 virtual const Tensor< GUM_SCALAR >& cpt(NodeId varId) const;
157
158 virtual const Tensor< GUM_SCALAR >& cpt(std::string name) const final {
159 return cpt(idFromName(name));
160 };
161
166 virtual const Tensor< GUM_SCALAR >& utility(NodeId varId) const;
167
168 virtual const Tensor< GUM_SCALAR >& utility(std::string name) const final {
169 return utility(idFromName(name));
170 };
171
176 const VariableNodeMap& variableNodeMap() const final;
177
181 bool isUtilityNode(NodeId varId) const;
182
183 bool isUtilityNode(const std::string& name) const { return isUtilityNode(idFromName(name)); };
184
188 bool isDecisionNode(NodeId varId) const;
189
190 bool isDecisionNode(const std::string& name) const { return isDecisionNode(idFromName(name)); };
191
195 bool isChanceNode(NodeId varId) const;
196
197 bool isChanceNode(const std::string& name) const { return isChanceNode(idFromName(name)); };
198
202 Size utilityNodeSize() const;
203
207 Size chanceNodeSize() const;
208
212 Size decisionNodeSize() const;
213
218 const DiscreteVariable& variable(NodeId id) const final;
219
220 const DiscreteVariable& variable(const std::string& name) const {
221 return variable(idFromName(name));
222 };
223
228 NodeId nodeId(const DiscreteVariable& var) const final;
229
232 NodeId idFromName(const std::string& name) const final;
233
236 const DiscreteVariable& variableFromName(const std::string& name) const final;
237
251 NodeId add(const DiscreteVariable& variable, NodeId id = 0);
252
268
284
297
313 NodeId id = 0);
314
330 NodeId id = 0);
331
332
350 NodeId add(const std::string& fast_description, unsigned int default_nbrmod = 2);
351
371 NodeId addChanceNode(const std::string& fast_description, unsigned int default_nbrmod = 2);
372
381 NodeId addUtilityNode(const std::string& fast_description);
382
399 NodeId addDecisionNode(const std::string& fast_description, unsigned int default_nbrmod = 2);
400
401
409 void erase(NodeId id);
410
411 void erase(const std::string& name) { erase(idFromName(name)); };
412
420 void erase(const DiscreteVariable& var);
421
426 void changeVariableName(NodeId id, const std::string& new_name);
427
428 void changeVariableName(const std::string& name, const std::string& new_name) {
429 changeVariableName(idFromName(name), new_name);
430 }
431
433 // ===========================================================================
435 // ===========================================================================
437
447 void addArc(NodeId tail, NodeId head);
448
449 void addArc(const std::string& tail, const std::string& head) {
450 addArc(idFromName(tail), idFromName(head));
451 }
452
460 void eraseArc(const Arc& arc);
461
470 void eraseArc(NodeId tail, NodeId head);
471
472 void eraseArc(const std::string& tail, const std::string& head) {
473 eraseArc(idFromName(tail), idFromName(head));
474 }
475
477
478 // ===========================================================================
480 // ===========================================================================
482
486 bool decisionOrderExists() const;
487
491 gum::DAG* getDecisionGraph() const;
492
497 std::vector< NodeId > decisionOrder() const;
498
502 bool existsPathBetween(NodeId src, NodeId dest) const;
503
504 bool existsPathBetween(const std::string& src, const std::string& dest) const {
505 return existsPathBetween(idFromName(src), idFromName(dest));
506 }
507
512 const List< NodeSet >& getPartialTemporalOrder(bool clear = true) const;
513
515
530
531 protected:
533 virtual void moralGraph_(UndiGraph& graph) const;
534
538 void removeTables_();
539
544
548 NodeId addNode_(const DiscreteVariable& variableType, NodeId DesiredId);
549
553 Sequence< NodeId > getChildrenDecision_(NodeId parentDecision) const;
554
555 private:
558
563
566 };
567
568} /* namespace gum */
569
571
572#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
Base class for discrete random variable.
Class representing an Influence Diagram.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs/utilities change their dimension with a cost in time.
List< NodeSet > _temporalOrder_
The temporal order.
gum::DAG * getDecisionGraph() const
Returns the temporal Graph.
bool isUtilityNode(const std::string &name) const
Returns the CPT of a tensor variable.
bool existsPathBetween(const std::string &src, const std::string &dest) const
True if a directed path exist with all decision nodes.
const List< NodeSet > & getPartialTemporalOrder(bool clear=true) const
Returns partial temporal ordering.
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
VariableNodeMap _variableMap_
Mapping between id and variable.
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a tensor variable.
NodeId addChanceNode(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it's associate node and it's CPT.
bool operator!=(const InfluenceDiagram< GUM_SCALAR > &other) const
Size chanceNodeSize() const
Returns the number of chance nodes.
void addArc(const std::string &tail, const std::string &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.
void removeTables_()
Removing ancient table.
NodeProperty< Tensor< GUM_SCALAR > * > _tensorMap_
Mapping between tensor variable's id and their CPT.
const VariableNodeMap & variableNodeMap() const final
Returns a constant reference to the VariableNodeMap of this Influence Diagram.
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram's tensor nodes cpt if necessary.
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.
Size utilityNodeSize() const
Returns the number of utility nodes.
InfluenceDiagram< GUM_SCALAR > & operator=(const InfluenceDiagram< GUM_SCALAR > &source)
Copy Operator.
void changeVariableName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable
NodeId add(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it's associate node and it's CPT.
static InfluenceDiagram< GUM_SCALAR > fastPrototype(const std::string &dotlike, Size domainSize)
Create an Influence Diagram with a dot-like syntax which specifies:
NodeId addUtilityNode(const DiscreteVariable &variable, NodeId id=0)
Add a utility variable, it's associate node and it's UT.
std::string toString() const
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
void copyStructureAndTables_(const InfluenceDiagram< GUM_SCALAR > &IDsource)
Copying tables from another influence diagram.
virtual const Tensor< GUM_SCALAR > & cpt(std::string name) const final
Returns the CPT of a tensor variable.
NodeId addDecisionNode(const DiscreteVariable &variable, NodeId id=0)
Add a decision variable.
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
InfluenceDiagram()
Default constructor.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it's node id.
bool isChanceNode(const std::string &name) const
Returns the CPT of a tensor variable.
bool operator==(const InfluenceDiagram< GUM_SCALAR > &other) const
void eraseArc(const Arc &arc)
Removes an arc in the ID, and update diagram's tensor nodes cpt if necessary.
NodeProperty< Tensor< GUM_SCALAR > * > _utilityMap_
Mapping between utility variable's id and their utility table.
NodeId nodeId(const DiscreteVariable &var) const final
Return id node from discrete var pointer.
void eraseArc(const std::string &tail, const std::string &head)
Add an arc in the ID, and update diagram's tensor nodes cpt if necessary.
Size decisionNodeSize() const
Returns the number of decision nodes.
const DiscreteVariable & variable(const std::string &name) const
Returns the CPT of a tensor variable.
virtual const Tensor< GUM_SCALAR > & utility(std::string name) const final
Returns the CPT of a tensor variable.
Sequence< NodeId > getChildrenDecision_(NodeId parentDecision) const
Returns the list of children decision for a given nodeId.
const DiscreteVariable & variableFromName(const std::string &name) const final
Getter by name.
void changeVariableName(const std::string &name, const std::string &new_name)
Returns the CPT of a tensor variable.
void erase(NodeId id)
Erase a Variable from the network and remove the variable from all his children.
virtual const Tensor< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
std::vector< NodeId > decisionOrder() const
Returns the sequence of decision nodes in the directed path.
NodeId idFromName(const std::string &name) const final
Getter by name.
bool existsPathBetween(NodeId src, NodeId dest) const
Returns true if a path exists between two nodes.
bool isDecisionNode(const std::string &name) const
Returns the CPT of a tensor variable.
void erase(const std::string &name)
Returns the CPT of a tensor variable.
~InfluenceDiagram() override
Destructor.
Generic doubly linked lists.
Definition list.h:379
<agrum/base/multidim/multiDimImplementation.h>
Base class for undirected graphs.
Definition undiGraph.h:128
Container used to map discrete variables with nodes.
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
STL namespace.
Header of the Tensor class.