aGrUM 3.1.1
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
187 std::string toDot() const final;
188
190
193
197 bool isInstalledNode(NodeId id) const;
198
199 bool isInstalledNode(std::string_view name) const;
200
207 void installNode(NodeId id);
208
209 void installNode(std::string_view name);
210
217 void installAscendants(NodeId id);
218
219 void installAscendants(std::string_view name);
220
226 void uninstallNode(NodeId id);
227
228 void uninstallNode(std::string_view name);
229
240 void installMarginal(NodeId id, const Tensor< GUM_SCALAR >& pot);
241
242 void installMarginal(std::string_view name, const Tensor< GUM_SCALAR >& pot);
243
256 void installCPT(NodeId id, const Tensor< GUM_SCALAR >& pot);
257
258 void installCPT(std::string_view name, const Tensor< GUM_SCALAR >& pot);
259
267 void uninstallCPT(NodeId id);
268
269 void uninstallCPT(std::string_view name);
270
277 bool checkConsistency(NodeId id) const;
278
279 bool checkConsistency(std::string_view name) const;
280
286 bool checkConsistency() const;
287
289
290
295 gum::BayesNet< GUM_SCALAR > toBN() const;
296
297 using IBayesNet< GUM_SCALAR >::nodes;
298 using IBayesNet< GUM_SCALAR >::dag;
299
300 protected:
301 // remove an arc
302 void uninstallArc_(NodeId from, NodeId to);
303
304 // add an arc
305 void installArc_(NodeId from, NodeId to);
306
307 // install a CPT BY COPY, create or delete arcs. Checks are made in public
308 // methods In particular, it is assumed that all the variables in the pot are
309 // in the fragment
310 void installCPT_(NodeId id, const Tensor< GUM_SCALAR >& pot);
311
316 void uninstallCPT_(NodeId id);
317 };
318
319
320#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
321 extern template class BayesNetFragment< double >;
322#endif
323
324} // namespace gum
325
327
328#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 final
creates a dot representing the whole referred BN hilighting the fragment.
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
STL namespace.