aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
instanceBayesNet_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
49
50#include <agrum/PRM/instanceBayesNet.h> // to ease IDE parser
51
52namespace gum {
53 namespace prm {
54
55 template < typename GUM_SCALAR >
57 for (const auto node: i.type().containerDag().nodes()) {
58 try {
59 // Adding the attribute
60 const PRMAttribute< GUM_SCALAR >& attr = i.get(node);
61 this->dag_.addNodeWithId(attr.id());
62 _varNodeMap_.insert(&(attr.type().variable()), &attr);
63 } catch (NotFound const&) {
64 // Not an attribute
65 }
66 }
67
68 for (const auto& arc: i.type().containerDag().arcs()) {
69 try {
70 this->dag_.addArc(arc.tail(), arc.head());
71 } catch (InvalidNode const&) {
72 // Not added means not an attribute
73 }
74 }
75 }
76
77 template < typename GUM_SCALAR >
79 IBayesNet< GUM_SCALAR >(), _inst_(&i) {
80 GUM_CONSTRUCTOR(InstanceBayesNet);
81 _init_(i);
82 }
83
84 template < typename GUM_SCALAR >
86 IBayesNet< GUM_SCALAR >(from), _varNodeMap_(from._varNodeMap_), _inst_(from._inst_) {
87 GUM_CONS_CPY(InstanceBayesNet);
88 }
89
90 template < typename GUM_SCALAR >
94
95 template < typename GUM_SCALAR >
98 if (this != &from) {
100
102 }
103
104 return *this;
105 }
106
107 template < typename GUM_SCALAR >
108 INLINE const Tensor< GUM_SCALAR >& InstanceBayesNet< GUM_SCALAR >::cpt(NodeId varId) const {
109 return _get_(varId).cpf();
110 }
111
112 template < typename GUM_SCALAR >
114 GUM_ERROR(NotFound, "no VariableNodeMap in an InstanceBayesNet")
115 }
116
117 template < typename GUM_SCALAR >
119 return _get_(id).type().variable();
120 }
121
122 template < typename GUM_SCALAR >
124 return _varNodeMap_[&var]->id();
125 }
126
127 template < typename GUM_SCALAR >
128 INLINE NodeId InstanceBayesNet< GUM_SCALAR >::idFromName(const std::string& name) const {
129 return _get_(name).id();
130 }
131
132 template < typename GUM_SCALAR >
133 INLINE const DiscreteVariable&
135 return _get_(name).type().variable();
136 }
137
138 template < typename GUM_SCALAR >
141 return _inst_->get(id);
142 }
143
144 template < typename GUM_SCALAR >
146 InstanceBayesNet< GUM_SCALAR >::_get_(const std::string& name) const {
147 try {
148 return _inst_->get(name);
149 } catch (NotFound const&) { GUM_ERROR(NotFound, "no element found with that name") }
150 }
151
152 template < typename GUM_SCALAR >
154 if (_modalities_.empty()) {
155 for (const auto node: this->nodes()) {
156 _modalities_.insert(node, variable(node).domainSize());
157 }
158 }
159
160 return _modalities_;
161 }
162
163 template < typename GUM_SCALAR >
164 INLINE std::string InstanceBayesNet< GUM_SCALAR >::toDot() const {
165 std::string tab = " ";
166 std::stringstream output;
167 output << "digraph \"";
168 output << _inst_->name() << "\" {" << std::endl;
169
170 for (const auto node: this->nodes()) {
171 if (this->children(node).size() > 0) {
172 const NodeSet& children = this->children(node);
173
174 for (const auto chi: children) {
175 output << tab << "\"" << variable(node).name() << "\" -> ";
176 output << "\"" << variable(chi).name() << "\";" << std::endl;
177 }
178 } else if (this->parents(node).size() == 0) {
179 output << tab << "\"" << variable(node).name() << "\";" << std::endl;
180 }
181 }
182
183 output << "}" << std::endl;
184 return output.str();
185 }
186
187 } /* namespace prm */
188} /* namespace gum */
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.
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
IBayesNet()
Default constructor.
Exception : node does not exist.
Exception : the element we looked for cannot be found.
Container used to map discrete variables with nodes.
This class decorates an PRMInstance<GUM_SCALAR> as an IBaseBayesNet.
NodeProperty< Size > _modalities_
const PRMClassElement< GUM_SCALAR > & _get_(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute<GUM_SCALAR>.
virtual ~InstanceBayesNet()
Destructor.
virtual const VariableNodeMap & variableNodeMap() const
See gum::IBaseBayesNet::variableNodeMap().
void _init_(const PRMInstance< GUM_SCALAR > &i)
InstanceBayesNet(const PRMInstance< GUM_SCALAR > &i)
Default constructor.
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const
See gum::IBaseBayesNet::cpt().
virtual std::string toDot() const
virtual NodeId nodeId(const DiscreteVariable &var) const
See gum::IBaseBayesNet::nodeId().
HashTable< const DiscreteVariable *, const PRMAttribute< GUM_SCALAR > * > _varNodeMap_
Mapping between DiscreteVariable and their NodeId.
virtual const DiscreteVariable & variableFromName(const std::string &name) const
See gum::IBaseBayesNet::variableFromName().
virtual NodeId idFromName(const std::string &name) const
See gum::IBaseBayesNet::idFromName().
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
InstanceBayesNet & operator=(const InstanceBayesNet &from)
Copy operator.
const PRMInstance< GUM_SCALAR > * _inst_
The PRMClassElementContainer decorated by this.
PRMAttribute is a member of a Class in a PRM.
virtual PRMType & type()=0
See gum::PRMClassElement::type().
Abstract class representing an element of PRM class.
NodeId id() const
Returns the NodeId of this element in it's class DAG.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
Definition PRMInstance.h:79
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
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
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Headers of InstanceBayesNet.
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46