aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
classBayesNet_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
51#include <agrum/PRM/classBayesNet.h> // to ease IDE parser
52
53namespace gum {
54 namespace prm {
55
56 template < GUM_Numeric GUM_SCALAR >
58 for (const auto node: c.containerDag().nodes()) {
59 if (c.exists(node)) {
60 // Adding the attribute
63 const PRMClassElement< GUM_SCALAR >& elt = c.get(node);
64 this->dag_.addNodeWithId(elt.id());
65 this->_varNodeMap_.insert(&(elt.type().variable()), &elt);
66 }
67 }
68 }
69
70 for (const auto& arc: c.containerDag().arcs()) {
71 try {
72 this->dag_.addArc(arc.tail(), arc.head());
73 } catch (InvalidNode const&) {
74 // Not added means not an attribute
75 }
76 }
77 }
78
79 template < GUM_Numeric GUM_SCALAR >
81 IBayesNet< GUM_SCALAR >(), _class_(&c) {
82 GUM_CONSTRUCTOR(ClassBayesNet);
83 _init_(c);
84 }
85
86 template < GUM_Numeric GUM_SCALAR >
88 IBayesNet< GUM_SCALAR >(from), _class_(from._class_) {
89 GUM_CONS_CPY(ClassBayesNet);
90 }
91
92 template < GUM_Numeric GUM_SCALAR >
96
97 template < GUM_Numeric GUM_SCALAR >
100 if (this != &from) {
102
103 _class_ = from._class_;
104 }
105
106 return *this;
107 }
108
109 template < GUM_Numeric GUM_SCALAR >
110 const Tensor< GUM_SCALAR >& ClassBayesNet< GUM_SCALAR >::cpt(NodeId varId) const {
111 return _get_(varId).cpf();
112 }
113
114 template < GUM_Numeric GUM_SCALAR >
116 GUM_ERROR(FatalError, "Sorry no VarMap in a ClassBayesNet.")
117 }
118
119 template < GUM_Numeric GUM_SCALAR >
121 return _get_(id).type().variable();
122 }
123
124 template < GUM_Numeric GUM_SCALAR >
126 return _varNodeMap_[&var]->id();
127 }
128
129 template < GUM_Numeric GUM_SCALAR >
130 NodeId ClassBayesNet< GUM_SCALAR >::idFromName(std::string_view name) const {
131 return _get_(name).id();
132 }
133
134 template < GUM_Numeric GUM_SCALAR >
135 const DiscreteVariable&
137 return _get_(name).type().variable();
138 }
139
140 template < GUM_Numeric GUM_SCALAR >
142 if (this->dag_.exists(id)) {
143 return _class_->get(id);
144 } else {
145 GUM_ERROR(NotFound, "no element found with that id.")
146 }
147 }
148
149 template < GUM_Numeric GUM_SCALAR >
151 ClassBayesNet< GUM_SCALAR >::_get_(std::string_view name) const {
152 if (!_class_->exists(name)) GUM_ERROR(NotFound, "no element found with that id.")
153 return _class_->get(name);
154 }
155
156 template < GUM_Numeric GUM_SCALAR >
158 if (_modalities_.empty()) {
159 for (const auto node: this->nodes()) {
160 _modalities_.insert(node, (Size)variable(node).domainSize());
161 }
162 }
163
164 return _modalities_;
165 }
166
167 template < GUM_Numeric GUM_SCALAR >
169 std::string tab = " ";
170 std::stringstream output;
171 output << std::format("digraph \"{}\" {{\n", _class_->name());
172
173 for (const auto node: this->nodes()) {
174 if (this->children(node).size() > 0)
175 for (const auto chi: this->children(node)) {
176 output << std::format("{}\"{}\" -> \"{}\";\n",
177 tab,
178 variable(node).name(),
179 variable(chi).name());
180 }
181 else if (this->parents(node).size() == 0) {
182 output << std::format("{}\"{}\";\n", tab, variable(node).name());
183 }
184 }
185
186 output << "}\n";
187 return output.str();
188 }
189
190 } /* namespace prm */
191} /* namespace gum */
Headers of ClassBayesNet<GUM_SCALAR>.
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition DAGmodel.h:284
Size size() const final
Returns the number of variables in this Directed Graphical Model.
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
const NodeGraphPart & nodes() const final
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
Base class for discrete random variable.
Exception : fatal (unknown ?) error.
IBayesNet()
Default constructor.
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
Exception : node does not exist.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Exception : the element we looked for cannot be found.
Container used to map discrete variables with nodes.
This class decorates a gum::prm::Class<GUM_SCALAR> has an IBaseBayesNet.
~ClassBayesNet() override
Destructor.
ClassBayesNet(const PRMClass< GUM_SCALAR > &c)
Default constructor.
const DiscreteVariable & variableFromName(std::string_view name) const override
See gum::IBaseBayesNet::variableFromName().
std::string toDot() const override
NodeId nodeId(const DiscreteVariable &var) const override
See gum::IBaseBayesNet::nodeId().
NodeProperty< Size > _modalities_
NodeId idFromName(std::string_view name) const override
See gum::IBaseBayesNet::idFromName().
const VariableNodeMap & variableNodeMap() const override
See gum::IBaseBayesNet::variableNodeMap().
HashTable< const DiscreteVariable *, const PRMClassElement< GUM_SCALAR > * > _varNodeMap_
Mapping between DiscreteVariable and their NodeId.
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const override
Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.
const PRMClassElement< GUM_SCALAR > & _get_(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute.
void _init_(const PRMClass< GUM_SCALAR > &c)
const PRMClass< GUM_SCALAR > * _class_
The PRMClassElementContainer decorated by this.
const DiscreteVariable & variable(NodeId id) const override
See gum::IBaseBayesNet::variable().
ClassBayesNet< GUM_SCALAR > & operator=(const ClassBayesNet< GUM_SCALAR > &from)
Copy operator.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
virtual bool exists(std::string_view name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
Abstract class representing an element of PRM class.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
virtual PRMType & type()=0
Return a reference over the gum::PRMType of this class element.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
NodeId id() const
Returns the NodeId of this element in it's class DAG.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:77
PRMClassElement< GUM_SCALAR > & get(NodeId id) override
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition PRMType_inl.h:65
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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.
aGrUM's inline/outline selection
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46