aGrUM 2.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-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_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 < typename GUM_SCALAR >
60 class BayesNetFactory;
61
92 template < typename GUM_SCALAR >
93 class BayesNet: public IBayesNet< GUM_SCALAR > {
94 friend class BayesNetFactory< GUM_SCALAR >;
95
96 public:
119 static BayesNet< GUM_SCALAR > fastPrototype(const std::string& dotlike, Size domainSize);
120 static BayesNet< GUM_SCALAR > fastPrototype(const std::string& dotlike,
121 const std::string& domainSize = "[2]");
122
123 // ===========================================================================
125 // ===========================================================================
127
131 BayesNet();
132
138 explicit BayesNet(std::string name);
139
143 virtual ~BayesNet();
144
148 BayesNet(const BayesNet< GUM_SCALAR >& source);
149
151 // ===========================================================================
153 // ===========================================================================
155
162 BayesNet< GUM_SCALAR >& operator=(const BayesNet< GUM_SCALAR >& source);
163
165 // ===========================================================================
167 // ===========================================================================
169
177 const Tensor< GUM_SCALAR >& cpt(NodeId varId) const final;
178
182 const Tensor< GUM_SCALAR >& cpt(const std::string& name) const {
183 return cpt(idFromName(name));
184 };
185
191 const VariableNodeMap& variableNodeMap() const final;
192
208 NodeId add(const DiscreteVariable& var);
209
228 NodeId add(const std::string& fast_description, unsigned int default_nbrmod = 2);
229
245 NodeId add(const DiscreteVariable& var, MultiDimImplementation< GUM_SCALAR >* aContent);
246
264 NodeId add(const DiscreteVariable& var, NodeId id);
265
281 NodeId
282 add(const DiscreteVariable& var, MultiDimImplementation< GUM_SCALAR >* aContent, NodeId id);
283
287 void clear();
288
299 void erase(NodeId varId);
300
304 void erase(const std::string& name) { erase(idFromName(name)); };
305
316 void erase(const DiscreteVariable& var);
317
328 const DiscreteVariable& variable(NodeId id) const final;
329
334 const DiscreteVariable& variable(const std::string& name) const {
335 return variable(idFromName(name));
336 };
337
347 void changeVariableName(NodeId id, const std::string& new_name);
348
352 void changeVariableName(const std::string& name, const std::string& new_name) {
353 changeVariableName(idFromName(name), new_name);
354 }
355
366 void changeVariableLabel(NodeId id, const std::string& old_label, const std::string& new_label);
367
371 void changeVariableLabel(const std::string& name,
372 const std::string& old_label,
373 const std::string& new_label) {
374 changeVariableLabel(idFromName(name), old_label, new_label);
375 }
376
385 NodeId nodeId(const DiscreteVariable& var) const final;
386
395 NodeId idFromName(const std::string& name) const final;
396
406 const DiscreteVariable& variableFromName(const std::string& name) const final;
408
409 // ===========================================================================
411 // ===========================================================================
413
422 void addArc(NodeId tail, NodeId head);
423
429 void addArc(const std::string& tail, const std::string& head);
430
437 void eraseArc(const Arc& arc);
438
446 void eraseArc(NodeId tail, NodeId head);
447
451 void eraseArc(const std::string& tail, const std::string& head) {
452 eraseArc(idFromName(tail), idFromName(head));
453 }
454
472
483 void reverseArc(NodeId tail, NodeId head);
484
485 void reverseArc(const std::string& tail, const std::string& head) {
486 reverseArc(idFromName(tail), idFromName(head));
487 }
488
489 void reverseArc(const Arc& arc);
491
493
494 // ===========================================================================
496 // ===========================================================================
498
514
515 NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight);
516 NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight);
517 NodeId addNoisyORCompound(const DiscreteVariable& var, GUM_SCALAR external_weight);
519
536 NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
537 NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
538 NodeId addNoisyORCompound(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
540
551 NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
552
561 NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight);
562
573 NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
574
583 NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
584
596 NodeId addOR(const DiscreteVariable& var);
597
609 NodeId addAND(const DiscreteVariable& var);
610
616 NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
617 NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
618 NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
619 NodeId addMAX(const DiscreteVariable& var);
621 NodeId addMIN(const DiscreteVariable& var);
622 NodeId addSUM(const DiscreteVariable& var);
635 void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
636
647 void addWeightedArc(const std::string& tail, const std::string& head, GUM_SCALAR causalWeight) {
648 addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
649 };
650
652
654 void generateCPTs() const;
655
657 void generateCPT(NodeId node) const;
658
659 void generateCPT(const std::string& name) const { generateCPT(idFromName(name)); };
660
665 void changeTensor(NodeId id, Tensor< GUM_SCALAR >* newPot);
666 void changeTensor(const std::string& name, Tensor< GUM_SCALAR >* newPot);
667
679 BayesNet< GUM_SCALAR > contextualize(const gum::Instantiation& observations,
680 const gum::Instantiation& interventions) const;
681
682 private:
684 void _clearTensors_();
685
687 void _copyTensors_(const BayesNet< GUM_SCALAR >& source);
688
691
694
699 void _unsafeChangeTensor_(NodeId id, Tensor< GUM_SCALAR >* newPot);
700
701 public:
702 using IBayesNet< GUM_SCALAR >::dag;
703 using IBayesNet< GUM_SCALAR >::size;
704 using IBayesNet< GUM_SCALAR >::nodes;
705 using IBayesNet< GUM_SCALAR >::log10DomainSize;
706 };
707
709 template < typename GUM_SCALAR >
710 std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
711
712
713#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
714 extern template class BayesNet< double >;
715#endif
716} /* namespace gum */
717
719
720#endif /* GUM_BAYES_NET_H */
Template implementation of BN/BayesNet.h class.
Class representing the minimal interface for Bayesian network with no numerical data.
The base class for all directed edges.
A factory class to ease BayesNet construction.
NodeId addNoisyORNet(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
NodeId addLogit(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a Logit implementation.
const Tensor< GUM_SCALAR > & cpt(const std::string &name) const
Returns the CPT of a variable.
Definition BayesNet.h:182
void changeVariableLabel(const std::string &name, const std::string &old_label, const std::string &new_label)
Changes a variable's name.
Definition BayesNet.h:371
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void changeVariableLabel(NodeId id, const std::string &old_label, const std::string &new_label)
Changes a variable's label in the gum::BayesNet.
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable's name in the gum::BayesNet.
NodeId addNoisyOR(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
NodeId addOR(const DiscreteVariable &var)
Add a variable, it's associate node and an OR implementation.
const DiscreteVariable & variable(const std::string &name) const
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition BayesNet.h:334
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
NodeId addAND(const DiscreteVariable &var)
Add a variable, it's associate node and an AND implementation.
void generateCPTs() const
randomly generates CPTs for a given structure
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
void clear()
clear the whole Bayes net *
void changeVariableName(const std::string &name, const std::string &new_name)
Changes a variable's name.
Definition BayesNet.h:352
BayesNet()
Default constructor.
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId 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.
void reverseArc(const std::string &tail, const std::string &head)
Reverses an arc while preserving the same joint distribution.
Definition BayesNet.h:485
void eraseArc(const std::string &tail, const std::string &head)
Removes an arc in the BN, and update head's CTP.
Definition BayesNet.h:451
void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head's CPT.
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
NodeId addMIN(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.
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
void erase(const std::string &name)
Removes a variable from the gum::BayesNet.
Definition BayesNet.h:304
void _copyTensors_(const BayesNet< GUM_SCALAR > &source)
copy of tensors from a BN to another, using names of vars as ref.
void generateCPT(const std::string &name) const
Definition BayesNet.h:659
static BayesNet< GUM_SCALAR > fastPrototype(const std::string &dotlike, Size domainSize)
Create a Bayesian network with a dot-like syntax which specifies:
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head's CTP.
NodeId addSUM(const DiscreteVariable &var)
Others aggregators.
virtual ~BayesNet()
Destructor.
void addWeightedArc(const std::string &tail, const std::string &head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head's CPT.
Definition BayesNet.h:647
NodeProperty< Tensor< GUM_SCALAR > * > _probaMap_
Mapping between the variable's id and their CPT.
Definition BayesNet.h:693
VariableNodeMap _varMap_
the map between variable and id
Definition BayesNet.h:690
NodeId addNoisyAND(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a noisyAND implementation.
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable's id in the gum::BayesNet.
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
void _clearTensors_()
clear all tensors
void _unsafeChangeTensor_(NodeId id, Tensor< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
NodeId idFromName(const std::string &name) const final
Returns a variable's id given its name in the gum::BayesNet.
BayesNet< GUM_SCALAR > & operator=(const BayesNet< GUM_SCALAR > &source)
Copy operator.
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Base class for discrete random variable.
double log10DomainSize() const
IBayesNet()
Default constructor.
Class for assigning/browsing values to tuples of discrete variables.
<agrum/base/multidim/multiDimImplementation.h>
Container used to map discrete variables with nodes.
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
Definition AVLTree.h:913
STL namespace.