aGrUM 3.1.1
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-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
50
51#include <agrum/PRM/instanceBayesNet.h> // to ease IDE parser
52
53namespace gum {
54 namespace prm {
55
56 template < GUM_Numeric GUM_SCALAR >
58 for (const auto node: i.type().containerDag().nodes()) {
59 if (i.exists(node)) {
60 // Adding the attribute
61 const PRMAttribute< GUM_SCALAR >& attr = i.get(node);
62 this->dag_.addNodeWithId(attr.id());
63 _varNodeMap_.insert(&(attr.type().variable()), &attr);
64 }
65 }
66
67 for (const auto& arc: i.type().containerDag().arcs()) {
68 try {
69 this->dag_.addArc(arc.tail(), arc.head());
70 } catch (InvalidNode const&) {
71 // Not added means not an attribute
72 }
73 }
74 }
75
76 template < GUM_Numeric GUM_SCALAR >
78 IBayesNet< GUM_SCALAR >(), _inst_(&i) {
79 GUM_CONSTRUCTOR(InstanceBayesNet);
80 _init_(i);
81 }
82
83 template < GUM_Numeric GUM_SCALAR >
85 IBayesNet< GUM_SCALAR >(from), _varNodeMap_(from._varNodeMap_), _inst_(from._inst_) {
86 GUM_CONS_CPY(InstanceBayesNet);
87 }
88
89 template < GUM_Numeric GUM_SCALAR >
93
94 template < GUM_Numeric GUM_SCALAR >
97 if (this != &from) {
99
101 }
102
103 return *this;
104 }
105
106 template < GUM_Numeric GUM_SCALAR >
107 const Tensor< GUM_SCALAR >& InstanceBayesNet< GUM_SCALAR >::cpt(NodeId varId) const {
108 return _get_(varId).cpf();
109 }
110
111 template < GUM_Numeric GUM_SCALAR >
113 GUM_ERROR(NotFound, "no VariableNodeMap in an InstanceBayesNet")
114 }
115
116 template < GUM_Numeric GUM_SCALAR >
118 return _get_(id).type().variable();
119 }
120
121 template < GUM_Numeric GUM_SCALAR >
123 return _varNodeMap_[&var]->id();
124 }
125
126 template < GUM_Numeric GUM_SCALAR >
128 return _get_(name).id();
129 }
130
131 template < GUM_Numeric GUM_SCALAR >
132 const DiscreteVariable&
134 return _get_(name).type().variable();
135 }
136
137 template < GUM_Numeric GUM_SCALAR >
141
142 template < GUM_Numeric GUM_SCALAR >
144 InstanceBayesNet< GUM_SCALAR >::_get_(std::string_view name) const {
145 if (!_inst_->exists(name)) GUM_ERROR(NotFound, "no element found with that name")
146 return _inst_->get(name);
147 }
148
149 template < GUM_Numeric GUM_SCALAR >
151 if (_modalities_.empty()) {
152 for (const auto node: this->nodes()) {
153 _modalities_.insert(node, variable(node).domainSize());
154 }
155 }
156
157 return _modalities_;
158 }
159
160 template < GUM_Numeric GUM_SCALAR >
162 std::string tab = " ";
163 std::stringstream output;
164 output << std::format("digraph \"{}\" {{\n", _inst_->name());
165
166 for (const auto node: this->nodes()) {
167 if (this->children(node).size() > 0) {
168 const NodeSet& children = this->children(node);
169
170 for (const auto chi: children) {
171 output << std::format("{}\"{}\" -> \"{}\";\n",
172 tab,
173 variable(node).name(),
174 variable(chi).name());
175 }
176 } else if (this->parents(node).size() == 0) {
177 output << std::format("{}\"{}\";\n", tab, variable(node).name());
178 }
179 }
180
181 output << "}\n";
182 return output.str();
183 }
184
185 } /* namespace prm */
186} /* namespace gum */
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.
IBayesNet()
Default constructor.
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
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.
std::string toDot() const override
NodeProperty< Size > _modalities_
InstanceBayesNet(const PRMInstance< GUM_SCALAR > &i)
Default constructor.
void _init_(const PRMInstance< GUM_SCALAR > &i)
const VariableNodeMap & variableNodeMap() const override
See gum::IBaseBayesNet::variableNodeMap().
~InstanceBayesNet() override
Destructor.
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
const DiscreteVariable & variable(NodeId id) const override
See gum::IBaseBayesNet::variable().
HashTable< const DiscreteVariable *, const PRMAttribute< GUM_SCALAR > * > _varNodeMap_
Mapping between DiscreteVariable and their NodeId.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const override
See gum::IBaseBayesNet::cpt().
const PRMClassElement< GUM_SCALAR > & _get_(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute<GUM_SCALAR>.
const DiscreteVariable & variableFromName(std::string_view name) const override
See gum::IBaseBayesNet::variableFromName().
InstanceBayesNet & operator=(const InstanceBayesNet &from)
Copy operator.
NodeId idFromName(std::string_view name) const override
See gum::IBaseBayesNet::idFromName().
NodeId nodeId(const DiscreteVariable &var) const override
See gum::IBaseBayesNet::nodeId().
const PRMInstance< GUM_SCALAR > * _inst_
The PRMClassElementContainer decorated by this.
PRMAttribute is a member of a Class in a PRM.
PRMType & type() override=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
bool exists(NodeId id) const
Returns true if id matches an PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
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
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