aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
BayesNetFragment.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
49#ifndef GUM_BAYES_NET_FRAGMENT_H
50#define GUM_BAYES_NET_FRAGMENT_H
51
52#include <agrum/agrum.h>
53
55#include <agrum/BN/BayesNet.h>
56
57namespace gum {
89 template < GUM_Numeric GUM_SCALAR >
90 class BayesNetFragment: public IBayesNet< GUM_SCALAR >, public gum::DiGraphListener {
93
97
98 public:
104
105 explicit BayesNetFragment(const IBayesNet< GUM_SCALAR >& bn);
106
107 ~BayesNetFragment() override;
109
112
114
116 void whenNodeAdded(const void* src, NodeId id) final;
117
119
121 void whenNodeDeleted(const void* src, NodeId id) final;
122
124
127 void whenArcAdded(const void* src, NodeId from, NodeId to) final;
128
130
133 void whenArcDeleted(const void* src, NodeId from, NodeId to) final;
135
138
144 const Tensor< GUM_SCALAR >& cpt(NodeId varId) const final;
145
146 const Tensor< GUM_SCALAR >& cpt(std::string_view name) const;
147
151 const VariableNodeMap& variableNodeMap() const final;
152
158 const DiscreteVariable& variable(NodeId id) const final;
159
160 virtual const DiscreteVariable& variable(std::string_view name) const final;
161
167 NodeId nodeId(const DiscreteVariable& var) const final;
168
174 NodeId idFromName(std::string_view name) const final;
175
181 const DiscreteVariable& variableFromName(std::string_view name) const final;
182
189 std::string toDot(const dot::DotOptions& options) const final;
190 using IBayesNet< GUM_SCALAR >::toDot;
191
193
196
200 bool isInstalledNode(NodeId id) const;
201
202 bool isInstalledNode(std::string_view name) const;
203
210 void installNode(NodeId id);
211
212 void installNode(std::string_view name);
213
220 void installAscendants(NodeId id);
221
222 void installAscendants(std::string_view name);
223
229 void uninstallNode(NodeId id);
230
231 void uninstallNode(std::string_view name);
232
243 void installMarginal(NodeId id, const Tensor< GUM_SCALAR >& pot);
244
245 void installMarginal(std::string_view name, const Tensor< GUM_SCALAR >& pot);
246
259 void installCPT(NodeId id, const Tensor< GUM_SCALAR >& pot);
260
261 void installCPT(std::string_view name, const Tensor< GUM_SCALAR >& pot);
262
270 void uninstallCPT(NodeId id);
271
272 void uninstallCPT(std::string_view name);
273
280 bool checkConsistency(NodeId id) const;
281
282 bool checkConsistency(std::string_view name) const;
283
289 bool checkConsistency() const;
290
292
293
298 gum::BayesNet< GUM_SCALAR > toBN() const;
299
300 using IBayesNet< GUM_SCALAR >::nodes;
301 using IBayesNet< GUM_SCALAR >::dag;
302
303 protected:
304 // remove an arc
305 void uninstallArc_(NodeId from, NodeId to);
306
307 // add an arc
308 void installArc_(NodeId from, NodeId to);
309
310 // install a CPT BY COPY, create or delete arcs. Checks are made in public
311 // methods In particular, it is assumed that all the variables in the pot are
312 // in the fragment
313 void installCPT_(NodeId id, const Tensor< GUM_SCALAR >& pot);
314
319 void uninstallCPT_(NodeId id);
320 };
321
322
323#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
324 extern template class GUM_PUBLIC_BN BayesNetFragment< double >;
325#endif
326
327} // namespace gum
328
330
331#endif // GUM_BAYES_NET_FRAGMENT_H
Template implementation of BN/BayesNetFragment.h classes.
Class representing Bayesian networks.
void installNode(NodeId id)
install a node referenced by its nodeId
void installMarginal(NodeId id, const Tensor< GUM_SCALAR > &pot)
install a local marginal BY COPY for a node into the fragment.
const DiscreteVariable & variableFromName(std::string_view name) const final
Getter by name.
void uninstallArc_(NodeId from, NodeId to)
void whenNodeDeleted(const void *src, NodeId id) final
the action to take when a node has just been removed from the graph
void installCPT_(NodeId id, const Tensor< GUM_SCALAR > &pot)
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
gum::BayesNet< GUM_SCALAR > toBN() const
create a brand new BayesNet from a fragment.
void installArc_(NodeId from, NodeId to)
void whenNodeAdded(const void *src, NodeId id) final
the action to take when a new node is inserted into the graph
BayesNetFragment(BayesNetFragment< GUM_SCALAR > &&fragment)=delete
void whenArcDeleted(const void *src, NodeId from, NodeId to) final
the action to take when an arc has just been removed from the graph
NodeId idFromName(std::string_view name) const final
Getter by name.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it's node id.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
void whenArcAdded(const void *src, NodeId from, NodeId to) final
the action to take when a new arc is inserted into the graph
NodeProperty< const Tensor< GUM_SCALAR > * > _localCPTs_
Mapping between the variable's id and their CPT specific to this Fragment.
bool checkConsistency(NodeId id) const
returns true if the nodeId's (local or not) cpt is consistent with its parents in the fragment
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
std::string toDot(const dot::DotOptions &options) const final
creates a dot representing the whole referred BN hilighting the fragment : the nodes and the arcs whi...
BayesNetFragment(const BayesNetFragment< GUM_SCALAR > &fragment)=delete
void installAscendants(NodeId id)
install a node and all its ascendants
NodeId nodeId(const DiscreteVariable &var) const final
Return id node from discrete var pointer.
void uninstallCPT(NodeId id)
uninstall a local CPT.
const VariableNodeMap & variableNodeMap() const final
Returns a constant reference to the VariableNodeMap of this BN.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void uninstallCPT_(NodeId id)
uninstall a local CPT.
void installCPT(NodeId id, const Tensor< GUM_SCALAR > &pot)
install a local cpt BY COPYfor a node into the fragment.
Class representing a Bayesian network.
Definition BayesNet.h:99
DAG dag() const
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
const NodeGraphPart & nodes() const final
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
Abstract Base class for all diGraph Listener.
Base class for discrete random variable.
IBayesNet()
Default constructor.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Container used to map discrete variables with nodes.
Base classes for oriented graph listeners.
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
template class GUM_PUBLIC_BN BayesNetFragment< double >
STL namespace.