aGrUM 2.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-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
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 < typename 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 virtual ~BayesNetFragment();
109
112
114
116 virtual void whenNodeAdded(const void* src, NodeId id) final;
117
119
121 virtual void whenNodeDeleted(const void* src, NodeId id) final;
122
124
127 virtual void whenArcAdded(const void* src, NodeId from, NodeId to) final;
128
130
133 virtual 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(const std::string& name) const {
147 return cpt(idFromName(name));
148 };
149
153 const VariableNodeMap& variableNodeMap() const final;
154
160 virtual const DiscreteVariable& variable(NodeId id) const final;
161
162 virtual const DiscreteVariable& variable(const std::string& name) const final {
163 return variable(idFromName(name));
164 };
165
171 virtual NodeId nodeId(const DiscreteVariable& var) const final;
172
178 virtual NodeId idFromName(const std::string& name) const final;
179
185 virtual const DiscreteVariable& variableFromName(const std::string& name) const final;
186
191 virtual std::string toDot() const final;
192
194
197
201 bool isInstalledNode(NodeId id) const;
202
203 bool isInstalledNode(const std::string& name) const {
204 return isInstalledNode(idFromName(name));
205 };
206
213 void installNode(NodeId id);
214
215 void installNode(const std::string& name) { installNode(_bn_.idFromName(name)); }
216
223 void installAscendants(NodeId id);
224
225 void installAscendants(const std::string& name) { installAscendants(_bn_.idFromName(name)); }
226
232 void uninstallNode(NodeId id);
233
234 void uninstallNode(const std::string& name) { uninstallNode(idFromName(name)); }
235
246 void installMarginal(NodeId id, const Tensor< GUM_SCALAR >& pot);
247
248 void installMarginal(const std::string& name, const Tensor< GUM_SCALAR >& pot) {
249 installMarginal(_bn_.idFromName(name), pot);
250 }
251
264 void installCPT(NodeId id, const Tensor< GUM_SCALAR >& pot);
265
266 void installCPT(const std::string& name, const Tensor< GUM_SCALAR >& pot) {
267 installCPT(_bn_.idFromName(name), pot);
268 };
269
277 void uninstallCPT(NodeId id);
278
279 void uninstallCPT(const std::string& name) { uninstallCPT(idFromName(name)); }
280
287 bool checkConsistency(NodeId id) const;
288
289 bool checkConsistency(const std::string& name) const {
290 return checkConsistency(idFromName(name));
291 }
292
298 bool checkConsistency() const;
299
301
302
308
309 using IBayesNet< GUM_SCALAR >::nodes;
310 using IBayesNet< GUM_SCALAR >::dag;
311
312 protected:
313 // remove an arc
314 void uninstallArc_(NodeId from, NodeId to);
315
316 // add an arc
317 void installArc_(NodeId from, NodeId to);
318
319 // install a CPT BY COPY, create or delete arcs. Checks are made in public
320 // methods In particular, it is assumed that all the variables in the pot are
321 // in the fragment
322 void installCPT_(NodeId id, const Tensor< GUM_SCALAR >& pot);
323
328 void uninstallCPT_(NodeId id);
329 };
330
331
332#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
333 extern template class BayesNetFragment< double >;
334#endif
335
336} // namespace gum
337
339
340#endif // GUM_BAYES_NET_FRAGMENT_H
Template implementation of BN/BayesNetFragment.h classes.
Class representing Bayesian networks.
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
virtual void whenNodeDeleted(const void *src, NodeId id) final
the action to take when a node has just been removed from the graph
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.
virtual void whenNodeAdded(const void *src, NodeId id) final
the action to take when a new node is inserted into the graph
const Tensor< GUM_SCALAR > & cpt(const std::string &name) const
Returns the CPT of a variable.
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.
virtual void whenArcDeleted(const void *src, NodeId from, NodeId to) final
the action to take when an arc has just been removed from the graph
bool checkConsistency(const std::string &name) const
check if a certain NodeId exists in the fragment
virtual void whenArcAdded(const void *src, NodeId from, NodeId to) final
the action to take when a new arc is inserted into the graph
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it's node id.
BayesNetFragment(BayesNetFragment< GUM_SCALAR > &&fragment)=delete
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
NodeProperty< const Tensor< GUM_SCALAR > * > _localCPTs_
Mapping between the variable's id and their CPT specific to this Fragment.
virtual NodeId nodeId(const DiscreteVariable &var) const final
Return id node from discrete var pointer.
const VariableNodeMap & variableNodeMap() const final
Returns a constant reference to the VariableNodeMap of this BN.
void installNode(const std::string &name)
check if a certain NodeId exists in the fragment
void uninstallCPT_(NodeId id)
uninstall a local CPT.
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
virtual std::string toDot() const final
creates a dot representing the whole referred BN hilighting the fragment.
void installMarginal(const std::string &name, const Tensor< GUM_SCALAR > &pot)
check if a certain NodeId exists in the fragment
virtual const DiscreteVariable & variableFromName(const std::string &name) const final
Getter by name.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
void installAscendants(const std::string &name)
check if a certain NodeId exists in the fragment
void installCPT(const std::string &name, const Tensor< GUM_SCALAR > &pot)
check if a certain NodeId exists in the fragment
BayesNetFragment(const BayesNetFragment< GUM_SCALAR > &fragment)=delete
void installArc_(NodeId from, NodeId to)
void installAscendants(NodeId id)
install a node and all its ascendants
void uninstallCPT(const std::string &name)
check if a certain NodeId exists in the fragment
void uninstallArc_(NodeId from, NodeId to)
void uninstallNode(const std::string &name)
check if a certain NodeId exists in the fragment
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:93
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Abstract Base class for all diGraph Listener.
Base class for discrete random variable.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
IBayesNet()
Default constructor.
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.