aGrUM 3.1.1
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
385 void addArc(NodeId tail, NodeId head);
386
392 void addArc(std::string_view tail, std::string_view head);
393
400 void eraseArc(const Arc& arc);
401
409 void eraseArc(NodeId tail, NodeId head);
410
414 void eraseArc(std::string_view tail, std::string_view head);
415
433
444 void reverseArc(NodeId tail, NodeId head);
445
446 void reverseArc(std::string_view tail, std::string_view head);
447
448 void reverseArc(const Arc& arc);
450
452
453 // ===========================================================================
455 // ===========================================================================
457
473
474 NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight);
475 NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight);
476 NodeId addNoisyORCompound(const DiscreteVariable& var, GUM_SCALAR external_weight);
478
495 NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
496 NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
497 NodeId addNoisyORCompound(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
499
510 NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
511
520 NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight);
521
532 NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
533
542 NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
543
555 NodeId addOR(const DiscreteVariable& var);
556
568 NodeId addAND(const DiscreteVariable& var);
569
575 NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
576 NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
577 NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
578 NodeId addMAX(const DiscreteVariable& var);
580 NodeId addMIN(const DiscreteVariable& var);
581 NodeId addSUM(const DiscreteVariable& var);
582
595 void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
596
607 void addWeightedArc(std::string_view tail, std::string_view head, GUM_SCALAR causalWeight);
608
610
612 void generateCPTs() const;
613
615 void generateCPT(NodeId node) const;
616
617 void generateCPT(std::string_view name) const;
618
623 void changeTensor(NodeId id, Tensor< GUM_SCALAR >* newPot);
624 void changeTensor(std::string_view name, Tensor< GUM_SCALAR >* newPot);
625
637 BayesNet< GUM_SCALAR > contextualize(const gum::Instantiation& observations,
638 const gum::Instantiation& interventions) const;
639
640 private:
642 void _clearTensors_();
643
645 void _copyTensors_(const BayesNet< GUM_SCALAR >& source);
646
649
654 void _unsafeChangeTensor_(NodeId id, Tensor< GUM_SCALAR >* newPot);
655
659 NodeId
660 _addAggregator_(std::string_view aggregatorType, const DiscreteVariable& var, Idx value = 1);
661
665 NodeId
666 _addICIModel_(std::string_view iciType, const DiscreteVariable& var, GUM_SCALAR externalWeight);
667
668 public:
669 using IBayesNet< GUM_SCALAR >::dag;
670 using IBayesNet< GUM_SCALAR >::size;
671 using IBayesNet< GUM_SCALAR >::nodes;
672 using IBayesNet< GUM_SCALAR >::log10DomainSize;
678 };
679
681 template < GUM_Numeric GUM_SCALAR >
682 std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
683
684
685#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
686 extern template class BayesNet< double >;
687#endif
688} /* namespace gum */
689
691
692#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:648
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.
~BayesNet() override
Destructor.
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