aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
BayesNet.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_H
50#define GUM_BAYES_NET_H
51
52#include <utility>
53
54#include <agrum/agrum.h>
55
56#include <agrum/BN/IBayesNet.h>
57
58namespace gum {
59 template < GUM_Numeric GUM_SCALAR >
60 class BayesNetFactory;
61
62 template < GUM_Numeric GUM_SCALAR >
64
65 template < GUM_Numeric GUM_SCALAR >
66 class GumBNReader;
67
98 template < GUM_Numeric GUM_SCALAR >
99 class BayesNet: public IBayesNet< GUM_SCALAR > {
100 friend class BayesNetFactory< GUM_SCALAR >;
101 friend class GumBNReader< GUM_SCALAR >;
102 friend class AggregatorDecomposition< GUM_SCALAR >;
103
104 public:
127 static BayesNet< GUM_SCALAR > fastPrototype(std::string_view dotlike, Size domainSize);
128 static BayesNet< GUM_SCALAR > fastPrototype(std::string_view dotlike,
129 std::string_view domainSize = "[2]");
130
131 // ===========================================================================
133 // ===========================================================================
135
139 BayesNet();
140
146 explicit BayesNet(std::string_view name);
147
151 ~BayesNet() override;
152
156 BayesNet(const BayesNet< GUM_SCALAR >& source);
157
161 BayesNet(BayesNet< GUM_SCALAR >&& source);
162
164 // ===========================================================================
166 // ===========================================================================
168
175 BayesNet< GUM_SCALAR >& operator=(const BayesNet< GUM_SCALAR >& source);
176
183 BayesNet< GUM_SCALAR >& operator=(BayesNet< GUM_SCALAR >&& source);
184
186 // ===========================================================================
188 // ===========================================================================
190
198 const Tensor< GUM_SCALAR >& cpt(NodeId varId) const final;
199
203 const Tensor< GUM_SCALAR >& cpt(std::string_view name) const;
204
220 NodeId add(const DiscreteVariable& var);
221
240 NodeId add(std::string_view fast_description, unsigned int default_nbrmod = 2);
241
258
276 NodeId add(const DiscreteVariable& var, NodeId id);
277
293 NodeId
295
299 void clear();
300
311 void erase(NodeId varId);
312
316 void erase(std::string_view name);
317
328 void erase(const DiscreteVariable& var);
329
333 const DiscreteVariable& variable(std::string_view name) const;
334
344 void changeVariableName(NodeId id, std::string_view new_name);
345
349 void changeVariableName(std::string_view name, std::string_view new_name);
350
361 void changeVariableLabel(NodeId id, std::string_view old_label, std::string_view new_label);
362
366 void changeVariableLabel(std::string_view name,
367 std::string_view old_label,
368 std::string_view new_label);
369
371
372 // ===========================================================================
374 // ===========================================================================
376
387 void addArc(NodeId tail, NodeId head);
388
394 void addArc(std::string_view tail, std::string_view head);
395
402 void eraseArc(const Arc& arc);
403
411 void eraseArc(NodeId tail, NodeId head);
412
416 void eraseArc(std::string_view tail, std::string_view head);
417
435
446 void reverseArc(NodeId tail, NodeId head);
447
448 void reverseArc(std::string_view tail, std::string_view head);
449
450 void reverseArc(const Arc& arc);
452
454
455 // ===========================================================================
457 // ===========================================================================
459
475
476 NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight);
477 NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight);
478 NodeId addNoisyORCompound(const DiscreteVariable& var, GUM_SCALAR external_weight);
480
497 NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
498 NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
499 NodeId addNoisyORCompound(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
501
512 NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
513
522 NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight);
523
534 NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
535
544 NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
545
557 NodeId addOR(const DiscreteVariable& var);
558
570 NodeId addAND(const DiscreteVariable& var);
571
584 NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
585 NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
586 NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
587 NodeId addMAX(const DiscreteVariable& var);
589 NodeId addMIN(const DiscreteVariable& var);
590 NodeId addSUM(const DiscreteVariable& var);
591
604 void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
605
616 void addWeightedArc(std::string_view tail, std::string_view head, GUM_SCALAR causalWeight);
617
619
621 void generateCPTs() const;
622
624 void generateCPT(NodeId node) const;
625
626 void generateCPT(std::string_view name) const;
627
632 void changeTensor(NodeId id, Tensor< GUM_SCALAR >* newPot);
633 void changeTensor(std::string_view name, Tensor< GUM_SCALAR >* newPot);
634
646 BayesNet< GUM_SCALAR > contextualize(const gum::Instantiation& observations,
647 const gum::Instantiation& interventions) const;
648
649 private:
651 void _clearTensors_();
652
654 void _copyTensors_(const BayesNet< GUM_SCALAR >& source);
655
658
663 void _unsafeChangeTensor_(NodeId id, Tensor< GUM_SCALAR >* newPot);
664
668 NodeId _addAggregator_(std::string_view aggregatorType,
669 const DiscreteVariable& var,
670 Idx value = 1);
671
675 NodeId _addICIModel_(std::string_view iciType,
676 const DiscreteVariable& var,
677 GUM_SCALAR externalWeight);
678
679 public:
680 using IBayesNet< GUM_SCALAR >::dag;
681 using IBayesNet< GUM_SCALAR >::size;
682 using IBayesNet< GUM_SCALAR >::nodes;
683 using IBayesNet< GUM_SCALAR >::log10DomainSize;
689 };
690
692 template < GUM_Numeric GUM_SCALAR >
693 std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
694
695
696#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
697 extern template class GUM_PUBLIC_BN BayesNet< double >;
698#endif
699} /* namespace gum */
700
702
703#endif /* GUM_BAYES_NET_H */
Template implementation of BN/BayesNet.h class.
Class representing the minimal interface for Bayesian network with no numerical data.
<agrum/BN/inference/tools/aggregatorDecomposition.h>
The base class for all directed edges.
A factory class to ease BayesNet construction.
NodeId addLogit(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a Logit implementation.
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head's CTP.
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
static BayesNet< GUM_SCALAR > fastPrototype(std::string_view dotlike, Size domainSize)
Create a Bayesian network with a dot-like syntax which specifies:
NodeId addAND(const DiscreteVariable &var)
Add a variable, it's associate node and an AND implementation.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
void clear()
clear the whole Bayes net *
void changeVariableLabel(NodeId id, std::string_view old_label, std::string_view new_label)
Changes a variable's label in the gum::BayesNet.
const DiscreteVariable & variable(std::string_view name) const
Returns a gum::DiscreteVariable given its name in the gum::BayesNet.
void changeTensor(NodeId id, Tensor< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head's CPT.
NodeId addSUM(const DiscreteVariable &var)
Others aggregators.
BayesNet< GUM_SCALAR > contextualize(const gum::Instantiation &observations, const gum::Instantiation &interventions) const
create a contextual BN from this and a set of hard observations and hard interventions.
NodeId _addAggregator_(std::string_view aggregatorType, const DiscreteVariable &var, Idx value=1)
Generic factory for aggregator nodes, dispatching on the aggregator's name.
NodeId addNoisyORNet(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
void _copyTensors_(const BayesNet< GUM_SCALAR > &source)
copy of tensors from a BN to another, using names of vars as ref.
NodeId addNoisyAND(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a noisyAND implementation.
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
NodeId addNoisyOR(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
NodeProperty< Tensor< GUM_SCALAR > * > _probaMap_
Mapping between the variable's id and their CPT.
Definition BayesNet.h:657
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
void changeVariableName(NodeId id, std::string_view new_name)
Changes a variable's name in the gum::BayesNet.
void _clearTensors_()
clear all tensors
NodeId addOR(const DiscreteVariable &var)
Add a variable, it's associate node and an OR implementation.
void _unsafeChangeTensor_(NodeId id, Tensor< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
void generateCPTs() const
randomly generates CPTs for a given structure
BayesNet()
Default constructor.
NodeId _addICIModel_(std::string_view iciType, const DiscreteVariable &var, GUM_SCALAR externalWeight)
Generic factory for ICI model nodes, dispatching on the implementation's name.
BayesNet< GUM_SCALAR > & operator=(const BayesNet< GUM_SCALAR > &source)
Copy operator.
Size size() const final
Returns the number of variables in this Directed Graphical Model.
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...
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const VariableNodeMap & variableNodeMap() const override
Returns a constant reference to the VariableNodeMap of this model.
const DiscreteVariable & variableFromName(std::string_view name) const override
Returns a constant reference over a variable given its name.
NodeId nodeId(const DiscreteVariable &var) const override
Returns the NodeId of a variable.
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
Base class for discrete random variable.
double log10DomainSize() const
IBayesNet()
Default constructor.
Class for assigning/browsing values to tuples of discrete variables.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
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
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
template class GUM_PUBLIC_BN BayesNet< double >
Definition BayesNet.cpp:48