aGrUM 2.3.2
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-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
50#include <agrum/PRM/classBayesNet.h> // to ease IDE parser
51
52namespace gum {
53 namespace prm {
54
55 template < typename GUM_SCALAR >
57 for (const auto node: c.containerDag().nodes()) {
58 try {
59 // Adding the attribute
62 const PRMClassElement< GUM_SCALAR >& elt = c.get(node);
63 this->dag_.addNodeWithId(elt.id());
64 this->_varNodeMap_.insert(&(elt.type().variable()), &elt);
65 }
66 } catch (NotFound const&) {
67 // Not an attribute
68 }
69 }
70
71 for (const auto& arc: c.containerDag().arcs()) {
72 try {
73 this->dag_.addArc(arc.tail(), arc.head());
74 } catch (InvalidNode const&) {
75 // Not added means not an attribute
76 }
77 }
78 }
79
80 template < typename GUM_SCALAR >
82 IBayesNet< GUM_SCALAR >(), _class_(&c) {
83 GUM_CONSTRUCTOR(ClassBayesNet);
84 _init_(c);
85 }
86
87 template < typename GUM_SCALAR >
89 IBayesNet< GUM_SCALAR >(from), _class_(from._class_) {
90 GUM_CONS_CPY(ClassBayesNet);
91 }
92
93 template < typename GUM_SCALAR >
97
98 template < typename GUM_SCALAR >
101 if (this != &from) {
103
104 _class_ = from._class_;
105 }
106
107 return *this;
108 }
109
110 template < typename GUM_SCALAR >
111 INLINE const Tensor< GUM_SCALAR >& ClassBayesNet< GUM_SCALAR >::cpt(NodeId varId) const {
112 return _get_(varId).cpf();
113 }
114
115 template < typename GUM_SCALAR >
117 GUM_ERROR(FatalError, "Sorry no VarMap in a ClassBayesNet.")
118 }
119
120 template < typename GUM_SCALAR >
122 return _get_(id).type().variable();
123 }
124
125 template < typename GUM_SCALAR >
127 return _varNodeMap_[&var]->id();
128 }
129
130 template < typename GUM_SCALAR >
131 INLINE NodeId ClassBayesNet< GUM_SCALAR >::idFromName(const std::string& name) const {
132 return _get_(name).id();
133 }
134
135 template < typename GUM_SCALAR >
136 INLINE const DiscreteVariable&
137 ClassBayesNet< GUM_SCALAR >::variableFromName(const std::string& name) const {
138 return _get_(name).type().variable();
139 }
140
141 template < typename GUM_SCALAR >
144 if (this->dag_.exists(id)) {
145 return _class_->get(id);
146 } else {
147 GUM_ERROR(NotFound, "no element found with that id.")
148 }
149 }
150
151 template < typename GUM_SCALAR >
153 ClassBayesNet< GUM_SCALAR >::_get_(const std::string& name) const {
154 try {
155 return _class_->get(name);
156 } catch (NotFound const&) { GUM_ERROR(NotFound, "no element found with that id.") }
157 }
158
159 template < typename GUM_SCALAR >
161 if (_modalities_.empty()) {
162 for (const auto node: this->nodes()) {
163 _modalities_.insert(node, (Size)variable(node).domainSize());
164 }
165 }
166
167 return _modalities_;
168 }
169
170 template < typename GUM_SCALAR >
171 INLINE std::string ClassBayesNet< GUM_SCALAR >::toDot() const {
172 std::string tab = " ";
173 std::stringstream output;
174 output << "digraph \"";
175 output << _class_->name() << "\" {" << std::endl;
176
177 for (const auto node: this->nodes()) {
178 if (this->children(node).size() > 0)
179 for (const auto chi: this->children(node)) {
180 output << tab << "\"" << variable(node).name() << "\" -> ";
181 output << "\"" << variable(chi).name() << "\";" << std::endl;
182 }
183 else if (this->parents(node).size() == 0) {
184 output << tab << "\"" << variable(node).name() << "\";" << std::endl;
185 }
186 }
187
188 output << "}" << std::endl;
189 return output.str();
190 }
191
192 } /* namespace prm */
193} /* 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:272
virtual 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 constant reference to the dag of this Bayes Net.
Base class for discrete random variable.
Exception : fatal (unknown ?) error.
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
IBayesNet()
Default constructor.
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.
virtual ~ClassBayesNet()
Destructor.
virtual const DiscreteVariable & variableFromName(const std::string &name) const
See gum::IBaseBayesNet::variableFromName().
virtual NodeId nodeId(const DiscreteVariable &var) const
See gum::IBaseBayesNet::nodeId().
const PRMClassElement< GUM_SCALAR > & _get_(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute.
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.
ClassBayesNet(const PRMClass< GUM_SCALAR > &c)
Default constructor.
NodeProperty< Size > _modalities_
HashTable< const DiscreteVariable *, const PRMClassElement< GUM_SCALAR > * > _varNodeMap_
Mapping between DiscreteVariable and their NodeId.
virtual std::string toDot() const
virtual const VariableNodeMap & variableNodeMap() const
See gum::IBaseBayesNet::variableNodeMap().
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
virtual NodeId idFromName(const std::string &name) const
See gum::IBaseBayesNet::idFromName().
void _init_(const PRMClass< GUM_SCALAR > &c)
const PRMClass< GUM_SCALAR > * _class_
The PRMClassElementContainer decorated by this.
virtual const DiscreteVariable & variable(NodeId id) const
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.
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.
NodeId id() const
Returns the NodeId of this element in it's class DAG.
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.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:75
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition PRMType_inl.h:64
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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