aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::BayesNet< GUM_SCALAR > Class Template Reference

Class representing a Bayesian network. More...

#include <agrum/BN/BayesNet.h>

Inheritance diagram for gum::BayesNet< GUM_SCALAR >:
Collaboration diagram for gum::BayesNet< GUM_SCALAR >:

Public Member Functions

NodeId addNoisyAND (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a noisyAND implementation.
NodeId addNoisyAND (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, its associate node and a noisyAND implementation.
NodeId addLogit (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a Logit implementation.
NodeId addLogit (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, its associate node and a Logit implementation.
NodeId addOR (const DiscreteVariable &var)
 Add a variable, it's associate node and an OR implementation.
NodeId addAND (const DiscreteVariable &var)
 Add a variable, it's associate node and an AND implementation.
void addWeightedArc (NodeId tail, NodeId head, GUM_SCALAR causalWeight)
 Add an arc in the BN, and update arc.head's CPT.
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.
void generateCPTs () const
 randomly generates CPTs for a given structure
void generateCPT (NodeId node) const
 randomly generate CPT for a given node in a given structure
void generateCPT (const std::string &name) const
void changeTensor (NodeId id, Tensor< GUM_SCALAR > *newPot)
 change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
void changeTensor (const std::string &name, Tensor< GUM_SCALAR > *newPot)
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.
std::vector< std::string > check () const
 Check if the BayesNet is consistent (variables, CPT).
bool operator== (const IBayesNet< GUM_SCALAR > &from) const
 This operator compares 2 BNs !
bool operator!= (const IBayesNet< GUM_SCALAR > &from) const
Size dim () const
 Returns the dimension (the number of free parameters) in this bayes net.
Size maxVarDomainSize () const
GUM_SCALAR minParam () const
GUM_SCALAR maxParam () const
GUM_SCALAR minNonZeroParam () const
GUM_SCALAR maxNonOneParam () const
virtual std::string toDot () const
std::string toString () const
Tensor< GUM_SCALAR > evEq (const std::string &name, double value) const
Tensor< GUM_SCALAR > evIn (const std::string &name, double val1, double val2) const
Tensor< GUM_SCALAR > evLt (const std::string &name, double value) const
Tensor< GUM_SCALAR > evGt (const std::string &name, double value) const
Size memoryFootprint () const
 compute the (approximated) footprint in memory of the model (the footprints of CPTs)
bool hasSameStructure (const DAGmodel &other)
NodeSet minimalCondSet (NodeId target, const NodeSet &soids) const
NodeSet minimalCondSet (const NodeSet &targets, const NodeSet &soids) const
NodeSet minimalCondSet (const std::string &target, const std::vector< std::string > &soids) const
NodeSet minimalCondSet (const std::vector< std::string > &targets, const std::vector< std::string > &soids) const
double log10DomainSize () const
Constructors and Destructor
 BayesNet ()
 Default constructor.
 BayesNet (std::string name)
 Default constructor.
virtual ~BayesNet ()
 Destructor.
 BayesNet (const BayesNet< GUM_SCALAR > &source)
 Copy constructor.
Operators
BayesNet< GUM_SCALAR > & operator= (const BayesNet< GUM_SCALAR > &source)
 Copy operator.
Variable manipulation methods
const Tensor< GUM_SCALAR > & cpt (NodeId varId) const final
 Returns the CPT of a variable.
const Tensor< GUM_SCALAR > & cpt (const std::string &name) const
 Returns the CPT of a variable.
const VariableNodeMapvariableNodeMap () const final
 Returns a map between variables and nodes of this gum::BayesNet.
NodeId add (const DiscreteVariable &var)
 Add a variable to the gum::BayesNet.
NodeId add (const std::string &fast_description, unsigned int default_nbrmod=2)
 Use "fast" syntax to add a variable in the BayesNet.
NodeId add (const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent)
 Add a variable to the gum::BayesNet.
NodeId add (const DiscreteVariable &var, NodeId id)
 Add a variable to the gum::BayesNet.
NodeId add (const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent, NodeId id)
 Add a variable to the gum::BayesNet.
void clear ()
 clear the whole Bayes net *
void erase (NodeId varId)
 Remove a variable from the gum::BayesNet.
void erase (const std::string &name)
 Removes a variable from the gum::BayesNet.
void erase (const DiscreteVariable &var)
 Remove a variable from the gum::BayesNet.
const DiscreteVariablevariable (NodeId id) const final
 Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
const DiscreteVariablevariable (const std::string &name) const
 Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
void changeVariableName (NodeId id, const std::string &new_name)
 Changes a variable's name in the gum::BayesNet.
void changeVariableName (const std::string &name, const std::string &new_name)
 Changes a variable's name.
void changeVariableLabel (NodeId id, const std::string &old_label, const std::string &new_label)
 Changes a variable's label in the gum::BayesNet.
void changeVariableLabel (const std::string &name, const std::string &old_label, const std::string &new_label)
 Changes a variable's name.
NodeId nodeId (const DiscreteVariable &var) const final
 Returns a variable's id in the gum::BayesNet.
NodeId idFromName (const std::string &name) const final
 Returns a variable's id given its name in the gum::BayesNet.
const DiscreteVariablevariableFromName (const std::string &name) const final
 Returns a variable given its name in the gum::BayesNet.
Arc manipulation methods.
void addArc (NodeId tail, NodeId head)
 Add an arc in the BN, and update arc.head's CPT.
void addArc (const std::string &tail, const std::string &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.
void eraseArc (NodeId tail, NodeId head)
 Removes an arc in the BN, and update head's CTP.
void eraseArc (const std::string &tail, const std::string &head)
 Removes an arc in the BN, and update head's CTP.
void beginTopologyTransformation ()
 When inserting/removing arcs, node CPTs change their dimension with a cost in time.
void endTopologyTransformation ()
 terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
void reverseArc (NodeId tail, NodeId head)
 Reverses an arc while preserving the same joint distribution.
void reverseArc (const std::string &tail, const std::string &head)
 Reverses an arc while preserving the same joint distribution.
void reverseArc (const Arc &arc)
 Reverses an arc while preserving the same joint distribution.
Accessors for nodes with CI or logical implementation
NodeId addNoisyOR (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, it's associate node and a gum::noisyOR implementation.
NodeId addNoisyORNet (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, it's associate node and a gum::noisyOR implementation.
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, NodeId id)
 Add a variable, its associate node and a noisyOR implementation.
NodeId addNoisyORNet (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a noisyOR implementation.
NodeId addNoisyORCompound (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a noisyOR implementation.
NodeId addAMPLITUDE (const DiscreteVariable &var)
 Others aggregators.
NodeId addCOUNT (const DiscreteVariable &var, Idx value=1)
 Others aggregators.
NodeId addEXISTS (const DiscreteVariable &var, Idx value=1)
 Others aggregators.
NodeId addFORALL (const DiscreteVariable &var, Idx value=1)
 Others aggregators.
NodeId addMAX (const DiscreteVariable &var)
 Others aggregators.
NodeId addMEDIAN (const DiscreteVariable &var)
 Others aggregators.
NodeId addMIN (const DiscreteVariable &var)
 Others aggregators.
NodeId addSUM (const DiscreteVariable &var)
 Others aggregators.
Joint Probability manipulation methods
GUM_SCALAR jointProbability (const Instantiation &i) const
 Compute a parameter of the joint probability for the BN (given an instantiation of the vars).
GUM_SCALAR log2JointProbability (const Instantiation &i) const
 Compute a parameter of the log joint probability for the BN (given an instantiation of the vars).
Variable manipulation methods.
const DAGdag () 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.
Size sizeArcs () const
 Returns the number of arcs in this Directed Graphical Model.
const NodeGraphPartnodes () const final
 Returns a constant reference to the dag of this Bayes Net.
bool exists (NodeId node) const final
 Return true if this node exists in this graphical model.
bool exists (const std::string &name) const final
 Returns a constant reference to the dag of this Bayes Net.
Arc manipulation methods.
const ArcSetarcs () const
 return true if the arc tail->head exists in the DAGmodel
bool existsArc (const NodeId tail, const NodeId head) const
 return true if the arc tail->head exists in the DAGmodel
bool existsArc (const std::string &nametail, const std::string &namehead) const
 return true if the arc tail->head exists in the DAGmodel
const NodeSetparents (const NodeId id) const
 returns the set of nodes with arc ingoing to a given node
const NodeSetparents (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel
NodeSet parents (const NodeSet &ids) const
 returns the parents of a set of nodes
NodeSet parents (const std::vector< std::string > &names) const
 return true if the arc tail->head exists in the DAGmodel
NodeSet family (const NodeId id) const final
 returns the parents of a node and the node
NodeSet family (const std::string &name) const final
 return true if the arc tail->head exists in the DAGmodel
const NodeSetchildren (const NodeId id) const
 returns the set of nodes with arc outgoing from a given node
const NodeSetchildren (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel
NodeSet children (const NodeSet &ids) const
 returns the children of a set of nodes
NodeSet children (const std::vector< std::string > &names) const
 return true if the arc tail->head exists in the DAGmodel
NodeSet descendants (const NodeId id) const
 returns the set of nodes with directed path outgoing from a given node
NodeSet descendants (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel
NodeSet ancestors (const NodeId id) const
 returns the set of nodes with directed path ingoing to a given node
NodeSet ancestors (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel
Graphical methods
UndiGraph moralizedAncestralGraph (const NodeSet &nodes) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
UndiGraph moralizedAncestralGraph (const std::vector< std::string > &nodenames) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
bool isIndependent (NodeId X, NodeId Y, const NodeSet &Z) const final
 check if node X and node Y are independent given nodes Z
bool isIndependent (const NodeSet &X, const NodeSet &Y, const NodeSet &Z) const final
 check if nodes X and nodes Y are independent given nodes Z
bool isIndependent (const std::string &Xname, const std::string &Yname, const std::vector< std::string > &Znames) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
bool isIndependent (const std::vector< std::string > &Xnames, const std::vector< std::string > &Ynames, const std::vector< std::string > &Znames) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
UndiGraph moralGraph () const
 The node's id are coherent with the variables and nodes of the topology.
Sequence< NodeIdtopologicalOrder () const
 The topological order stays the same as long as no variable or arcs are added or erased src the topology.
Getter and setters
const std::string & property (const std::string &name) const
 Return the value of the property name of this GraphicalModel.
const std::string & propertyWithDefault (const std::string &name, const std::string &byDefault) const
 Return the value of the property name of this GraphicalModel.
void setProperty (const std::string &name, const std::string &value)
 Add or change a property of this GraphicalModel.
std::vector< std::string > properties () const
 List of all the properties.
Variable manipulation methods.
virtual bool empty () const
 Return true if this graphical model is empty.
std::vector< std::string > names (const std::vector< NodeId > &ids) const
 transform a vector of NodeId in a vector of names
std::vector< std::string > names (const NodeSet &ids) const
 transform a NodeSet in a vector of names
std::vector< NodeIdids (const std::vector< std::string > &names) const
 transform a vector of names into a vector of nodeId
NodeSet nodeset (const std::vector< std::string > &names) const
 transform a vector of names into a NodeSet
gum::VariableSet variables (const std::vector< std::string > &l) const
 transform a vector of names into a VariableeSet
gum::VariableSet variables (const NodeSet &ids) const
 transform a vector of NodeId into a VariableeSet
Instantiation completeInstantiation () const
 Get an instantiation over all the variables of the model.

Static Public Member Functions

static BayesNet< GUM_SCALAR > fastPrototype (const std::string &dotlike, Size domainSize)
 Create a Bayesian network with a dot-like syntax which specifies:
static BayesNet< GUM_SCALAR > fastPrototype (const std::string &dotlike, const std::string &domainSize="[2]")
static void spaceCplxToStream (std::stringstream &s, double dSize, int dim, Size usedMem)
 send to the stream the space complexity with 3 parametrs

Protected Attributes

DAG dag_
 The DAG of this Directed Graphical Model.

Private Member Functions

void _clearTensors_ ()
 clear all tensors
void _copyTensors_ (const BayesNet< GUM_SCALAR > &source)
 copy of tensors from a BN to another, using names of vars as ref.
void _unsafeChangeTensor_ (NodeId id, Tensor< GUM_SCALAR > *newPot)
 change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
const HashTable< std::string, std::string > & _properties_ () const
 Return the properties of this Directed Graphical Model.

Private Attributes

VariableNodeMap _varMap_
 the map between variable and id
NodeProperty< Tensor< GUM_SCALAR > * > _probaMap_
 Mapping between the variable's id and their CPT.
HashTable< std::string, std::string > _propertiesMap_
 The properties of this Directed Graphical Model.

Friends

class BayesNetFactory< GUM_SCALAR >

Detailed Description

template<typename GUM_SCALAR>
class gum::BayesNet< GUM_SCALAR >

Class representing a Bayesian network.

Bayesian networks are a probabilistic graphical model in which nodes are random variables and the probability distribution is defined by the product:

\(P(X_1, \ldots, X_n) = \prod_{i=1}^{n} P(X_i | \pi(X_i))\),

where \(\pi(X_i)\) is the parent of \(X_i\).

The probability distribution can be represented as a directed acyclic graph (DAG) where:

  • Nodes are discrete random variables.
  • An arc A -> B represent a dependency between variables A and B, i.e. B conditional probability distribution is defined as \(P(B| \pi(B)\).

After a variable is added to the BN, it's domain cannot change. But it arcs are added, the data in its CPT are lost.

You should look a the gum::BayesNetFactory class which can help build Bayesian networks.

You can print a BayesNet using gum::operator<<(std::ostream&, const BayesNet<GUM_SCALAR>&).

Definition at line 93 of file BayesNet.h.

Constructor & Destructor Documentation

◆ BayesNet() [1/3]

template<typename GUM_SCALAR>
INLINE gum::BayesNet< GUM_SCALAR >::BayesNet ( )

Default constructor.

Definition at line 128 of file BayesNet_tpl.h.

130 }
Class representing a Bayesian network.
Definition BayesNet.h:93
BayesNet()
Default constructor.
IBayesNet()
Default constructor.

References BayesNet(), and gum::IBayesNet< GUM_SCALAR >::IBayesNet().

Referenced by BayesNet(), BayesNet(), BayesNet(), ~BayesNet(), and add().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BayesNet() [2/3]

template<typename GUM_SCALAR>
INLINE gum::BayesNet< GUM_SCALAR >::BayesNet ( std::string name)
explicit

Default constructor.

Parameters
nameThe BayesNet's name.

Definition at line 133 of file BayesNet_tpl.h.

References BayesNet(), and gum::IBayesNet< GUM_SCALAR >::IBayesNet().

Here is the call graph for this function:

◆ ~BayesNet()

template<typename GUM_SCALAR>
gum::BayesNet< GUM_SCALAR >::~BayesNet ( )
virtual

Destructor.

Definition at line 159 of file BayesNet_tpl.h.

159 {
161 for (const auto& p: _probaMap_) {
162 delete p.second;
163 }
164 }
NodeProperty< Tensor< GUM_SCALAR > * > _probaMap_
Mapping between the variable's id and their CPT.
Definition BayesNet.h:693

References BayesNet(), and _probaMap_.

Here is the call graph for this function:

◆ BayesNet() [3/3]

template<typename GUM_SCALAR>
gum::BayesNet< GUM_SCALAR >::BayesNet ( const BayesNet< GUM_SCALAR > & source)

Copy constructor.

Definition at line 138 of file BayesNet_tpl.h.

138 :
141
143 }
void _copyTensors_(const BayesNet< GUM_SCALAR > &source)
copy of tensors from a BN to another, using names of vars as ref.
VariableNodeMap _varMap_
the map between variable and id
Definition BayesNet.h:690

References BayesNet(), gum::IBayesNet< GUM_SCALAR >::IBayesNet(), _copyTensors_(), and _varMap_.

Here is the call graph for this function:

Member Function Documentation

◆ _clearTensors_()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::_clearTensors_ ( )
private

clear all tensors

Definition at line 568 of file BayesNet_tpl.h.

568 {
569 // Removing previous tensors
570 for (const auto& elt: _probaMap_) {
571 delete elt.second;
572 }
573
574 _probaMap_.clear();
575 }

References _probaMap_.

Referenced by operator=().

Here is the caller graph for this function:

◆ _copyTensors_()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::_copyTensors_ ( const BayesNet< GUM_SCALAR > & source)
private

copy of tensors from a BN to another, using names of vars as ref.

Definition at line 579 of file BayesNet_tpl.h.

579 {
580 // Copying tensors
581
582 for (const auto& src: source._probaMap_) {
583 // First we build the node's CPT
584 auto copy_array = new Tensor< GUM_SCALAR >();
585 copy_array->beginMultipleChanges();
586 for (gum::Idx i = 0; i < src.second->nbrDim(); i++) {
587 (*copy_array) << variableFromName(src.second->variable(i).name());
588 }
589 copy_array->endMultipleChanges();
590 copy_array->copyFrom(*(src.second));
591
592 // We add the CPT to the CPT hashmap
593 _probaMap_.insert(src.first, copy_array);
594 }
595 }
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
const std::string & name() const
returns the name of the variable

References _probaMap_, and variableFromName().

Referenced by BayesNet(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _properties_()

INLINE const HashTable< std::string, std::string > & gum::GraphicalModel::_properties_ ( ) const
privateinherited

Return the properties of this Directed Graphical Model.

Definition at line 70 of file graphicalModel_inl.h.

70 {
71 return _propertiesMap_;
72 }
HashTable< std::string, std::string > _propertiesMap_
The properties of this Directed Graphical Model.

References _propertiesMap_.

Referenced by property().

Here is the caller graph for this function:

◆ _unsafeChangeTensor_()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::_unsafeChangeTensor_ ( NodeId id,
Tensor< GUM_SCALAR > * newPot )
private

change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

Warning
no verification of dimensions are performer
See also
changeTensor

Definition at line 631 of file BayesNet_tpl.h.

631 {
632 delete _probaMap_[id];
634 }

References _probaMap_.

◆ add() [1/5]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable & var)

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Tensor.

The variable is added by copy to the gum::BayesNet. The variable's gum::Tensor implementation will be a gum::MultiDimArray.

Parameters
varThe variable added by copy.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 195 of file BayesNet_tpl.h.

195 {
196 auto ptr = new MultiDimArray< GUM_SCALAR >();
197 try {
198 return add(var, ptr);
199 } catch (Exception const&) {
200 delete ptr;
201 throw;
202 }
203 }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.

References add().

Referenced by add(), add(), add(), addAMPLITUDE(), addAND(), addCOUNT(), addEXISTS(), addFORALL(), addLogit(), addLogit(), addMAX(), addMEDIAN(), addMIN(), addNoisyAND(), addNoisyAND(), addNoisyORCompound(), addNoisyORCompound(), addNoisyORNet(), addNoisyORNet(), addOR(), addSUM(), gum::build_node(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add() [2/5]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable & var,
MultiDimImplementation< GUM_SCALAR > * aContent )

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Tensor.

The variable is added by copy to the gum::BayesNet.

Parameters
varThe variable added by copy.
aContentThe gum::MultiDimImplementation to use for this variable's gum::Tensor implementation.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 214 of file BayesNet_tpl.h.

215 {
216 NodeId proposedId = dag().nextNodeId();
217
218 return add(var, aContent, proposedId);
219 }
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.

References BayesNet(), add(), and gum::DAGmodel::dag().

Here is the call graph for this function:

◆ add() [3/5]

template<typename GUM_SCALAR>
NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable & var,
MultiDimImplementation< GUM_SCALAR > * aContent,
NodeId id )

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Tensor.

Parameters
varThe variable added by copy.
aContentThe gum::MultiDimImplementation to use for this variable's gum::Tensor implementation.
idThe variable's forced gum::NodeId in the gum::BayesNet.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateElementRaised id is already used.
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 234 of file BayesNet_tpl.h.

236 {
237 _varMap_.insert(id, var);
238 this->dag_.addNodeWithId(id);
239
241 (*cpt) << variable(id);
242 _probaMap_.insert(id, cpt);
243 return id;
244 }
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
DAG dag_
The DAG of this Directed Graphical Model.
Definition DAGmodel.h:272

References _probaMap_, _varMap_, cpt(), gum::DAGmodel::dag_, and variable().

Here is the call graph for this function:

◆ add() [4/5]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable & var,
NodeId id )

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Tensor.

The variable is added by copy to the gum::BayesNet. The variable's gum::Tensor implementation will be a gum::MultiDimArray.

Parameters
varThe variable added by copy.
idThe variable's forced gum::NodeId in the gum::BayesNet.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateElementRaised id is already used.
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 222 of file BayesNet_tpl.h.

222 {
223 auto ptr = new MultiDimArray< GUM_SCALAR >();
224
225 try {
226 return add(var, ptr, id);
227 } catch (Exception const&) {
228 delete ptr;
229 throw;
230 }
231 }

References add().

Here is the call graph for this function:

◆ add() [5/5]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const std::string & fast_description,
unsigned int default_nbrmod = 2 )

Use "fast" syntax to add a variable in the BayesNet.

  • a : range variable from 0 to default_nbrmod-1
  • a[5] : range variable from 0 to 5
  • a[-3,5] : range variable from -3 to 5
  • a[1,3.14,5,3] : discretized variable
  • a{x|y|z} : labelized variable
  • a{-3|0|3|100} : integer variable
Parameters
fast_description(str) following "fast" syntax description
default_nbrmod(int) nbr of modality if fast_description do not indicate it. default_nbrmod=1 is the way to create a variable with only one value (for instance for reward in influence diagram).
Exceptions
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.
NotAllowedif nbrmod<2

Definition at line 206 of file BayesNet_tpl.h.

207 {
209 if (v->domainSize() < 2) GUM_ERROR(OperationNotAllowed, v->name() << " has a domain size <2")
210 return add(*v);
211 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

◆ addAMPLITUDE()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addAMPLITUDE ( const DiscreteVariable & var)

Others aggregators.

Definition at line 406 of file BayesNet_tpl.h.

406 {
408 }

References add().

Here is the call graph for this function:

◆ addAND()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addAND ( const DiscreteVariable & var)

Add a variable, it's associate node and an AND implementation.

The id of the new variable is automatically generated.

Warning
AND is implemented as a gum::aggregator::And which means that if parents are not boolean, all value>1 is True
Parameters
varThe variable added by copy.
Returns
the id of the added variable.
Exceptions
SizeErrorif variable.domainSize()>2

Definition at line 411 of file BayesNet_tpl.h.

411 {
412 if (var.domainSize() > 2) GUM_ERROR(SizeError, "an AND has to be boolean")
413
415 }

References add(), gum::DiscreteVariable::domainSize(), and GUM_ERROR.

Here is the call graph for this function:

◆ addArc() [1/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::addArc ( const std::string & tail,
const std::string & head )

Add an arc in the BN, and update arc.head's CPT.

Exceptions
gum::DuplicateElementif the arc already exists

Definition at line 310 of file BayesNet_tpl.h.

310 {
311 try {
312 addArc(this->idFromName(tail), this->idFromName(head));
313 } catch (DuplicateElement const&) {
314 GUM_ERROR(DuplicateElement, "The arc " << tail << "->" << head << " already exists.")
315 }
316 }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
NodeId idFromName(const std::string &name) const final
Returns a variable's id given its name in the gum::BayesNet.

References addArc(), GUM_ERROR, and idFromName().

Here is the call graph for this function:

◆ addArc() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::addArc ( NodeId tail,
NodeId head )

Add an arc in the BN, and update arc.head's CPT.

Parameters
headand
tailas NodeId
Exceptions
InvalidEdgeIf arc.tail and/or arc.head are not in the BN.
DuplicateElementif the arc already exists

Definition at line 299 of file BayesNet_tpl.h.

299 {
300 if (this->dag_.existsArc(tail, head)) {
301 GUM_ERROR(DuplicateElement, "The arc (" << tail << "," << head << ") already exists.")
302 }
303
304 this->dag_.addArc(tail, head);
305 // Add parent in the child's CPT
306 (*(_probaMap_[head])) << variable(tail);
307 }

References _probaMap_, gum::DAGmodel::dag_, GUM_ERROR, and variable().

Referenced by addArc(), addWeightedArc(), fastPrototype(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addCOUNT()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addCOUNT ( const DiscreteVariable & var,
Idx value = 1 )

Others aggregators.

Definition at line 418 of file BayesNet_tpl.h.

418 {
420 }

References add().

Here is the call graph for this function:

◆ addEXISTS()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addEXISTS ( const DiscreteVariable & var,
Idx value = 1 )

Others aggregators.

Definition at line 423 of file BayesNet_tpl.h.

423 {
424 if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean")
425
427 }

References add(), gum::DiscreteVariable::domainSize(), and GUM_ERROR.

Here is the call graph for this function:

◆ addFORALL()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addFORALL ( const DiscreteVariable & var,
Idx value = 1 )

Others aggregators.

Definition at line 430 of file BayesNet_tpl.h.

430 {
431 if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean")
432
434 }

References add(), gum::DiscreteVariable::domainSize(), and GUM_ERROR.

Here is the call graph for this function:

◆ addLogit() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addLogit ( const DiscreteVariable & var,
GUM_SCALAR external_weight )

Add a variable, its associate node and a Logit implementation.

The id of the new variable is automatically generated.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimLogit
Returns
the id of the added variable.

Definition at line 491 of file BayesNet_tpl.h.

492 {
494 }

References add().

Here is the call graph for this function:

◆ addLogit() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addLogit ( const DiscreteVariable & var,
GUM_SCALAR external_weight,
NodeId id )

Add a variable, its associate node and a Logit implementation.

Parameters
varThe variable added by copy
external_weightsee gum::MultiDimLogit
idproposed gum::nodeId for the variable
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.

Definition at line 511 of file BayesNet_tpl.h.

513 {
515 }

References add().

Here is the call graph for this function:

◆ addMAX()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addMAX ( const DiscreteVariable & var)

Others aggregators.

Definition at line 437 of file BayesNet_tpl.h.

437 {
439 }

References add().

Here is the call graph for this function:

◆ addMEDIAN()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addMEDIAN ( const DiscreteVariable & var)

Others aggregators.

Definition at line 442 of file BayesNet_tpl.h.

442 {
444 }

References add().

Here is the call graph for this function:

◆ addMIN()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addMIN ( const DiscreteVariable & var)

Others aggregators.

Definition at line 447 of file BayesNet_tpl.h.

447 {
449 }

References add().

Here is the call graph for this function:

◆ addNoisyAND() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyAND ( const DiscreteVariable & var,
GUM_SCALAR external_weight )

Add a variable, its associate node and a noisyAND implementation.

The id of the new variable is automatically generated.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyAND
Returns
the id of the added variable.

Definition at line 485 of file BayesNet_tpl.h.

486 {
488 }

References add().

Here is the call graph for this function:

◆ addNoisyAND() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyAND ( const DiscreteVariable & var,
GUM_SCALAR external_weight,
NodeId id )

Add a variable, its associate node and a noisyAND implementation.

Parameters
varThe variable added by copy
external_weightsee gum::MultiDimNoisyAND
idproposed gum::nodeId for the variable
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.

Definition at line 504 of file BayesNet_tpl.h.

506 {
508 }

References add().

Here is the call graph for this function:

◆ addNoisyOR() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyOR ( const DiscreteVariable & var,
GUM_SCALAR external_weight )

Add a variable, it's associate node and a gum::noisyOR implementation.

The id of the new variable is automatically generated. Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the gum::BayesNet::addNoisyOR as an alias for gum::BayesNet::addNoisyORCompound

Parameters
varThe variable added by copy.
external_weightsee ref gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
Returns
the id of the added variable.

Definition at line 467 of file BayesNet_tpl.h.

468 {
470 }
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.

References addNoisyORCompound().

Here is the call graph for this function:

◆ addNoisyOR() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyOR ( const DiscreteVariable & var,
GUM_SCALAR external_weight,
NodeId id )

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyORNet, gum::MultiDimNoisyORCompound
idThe chosen id
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id is already used

Definition at line 497 of file BayesNet_tpl.h.

499 {
501 }

References addNoisyORCompound().

Here is the call graph for this function:

◆ addNoisyORCompound() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORCompound ( const DiscreteVariable & var,
GUM_SCALAR external_weight )

Add a variable, it's associate node and a gum::noisyOR implementation.

The id of the new variable is automatically generated. Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the gum::BayesNet::addNoisyOR as an alias for gum::BayesNet::addNoisyORCompound

Parameters
varThe variable added by copy.
external_weightsee ref gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
Returns
the id of the added variable.

Definition at line 473 of file BayesNet_tpl.h.

References add().

Referenced by addNoisyOR(), and addNoisyOR().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addNoisyORCompound() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORCompound ( const DiscreteVariable & var,
GUM_SCALAR external_weight,
NodeId id )

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyORNet, gum::MultiDimNoisyORCompound
idThe chosen id
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id is already used

Definition at line 518 of file BayesNet_tpl.h.

520 {
522 }

References add().

Here is the call graph for this function:

◆ addNoisyORNet() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORNet ( const DiscreteVariable & var,
GUM_SCALAR external_weight )

Add a variable, it's associate node and a gum::noisyOR implementation.

The id of the new variable is automatically generated. Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the gum::BayesNet::addNoisyOR as an alias for gum::BayesNet::addNoisyORCompound

Parameters
varThe variable added by copy.
external_weightsee ref gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
Returns
the id of the added variable.

Definition at line 479 of file BayesNet_tpl.h.

480 {
482 }

References add().

Here is the call graph for this function:

◆ addNoisyORNet() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORNet ( const DiscreteVariable & var,
GUM_SCALAR external_weight,
NodeId id )

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyORNet, gum::MultiDimNoisyORCompound
idThe chosen id
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id is already used

Definition at line 525 of file BayesNet_tpl.h.

527 {
529 }

References add().

Here is the call graph for this function:

◆ addOR()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addOR ( const DiscreteVariable & var)

Add a variable, it's associate node and an OR implementation.

The id of the new variable is automatically generated.

Warning
OR is implemented as a gum::aggregator::Or which means that if parents are not boolean, all value>1 is True
Parameters
varThe variable added by copy.
Returns
the id of the added variable.
Exceptions
SizeErrorif variable.domainSize()>2

Definition at line 452 of file BayesNet_tpl.h.

452 {
453 if (var.domainSize() > 2) GUM_ERROR(SizeError, "an OR has to be boolean")
454
456 }

References add(), gum::DiscreteVariable::domainSize(), and GUM_ERROR.

Here is the call graph for this function:

◆ addSUM()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addSUM ( const DiscreteVariable & var)

Others aggregators.

Definition at line 459 of file BayesNet_tpl.h.

459 {
461 }

References add().

Here is the call graph for this function:

◆ addWeightedArc() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::addWeightedArc ( const std::string & tail,
const std::string & head,
GUM_SCALAR causalWeight )
inline

Add an arc in the BN, and update arc.head's CPT.

Parameters
headand
tailas std::string
causalWeightsee gum::MultiDimICIModel
Exceptions
NotFoundif no node with sun names is found
InvalidArcIf arc.tail and/or arc.head are not in the BN.
InvalidArcIf variable in arc.head is not a NoisyOR variable.

Definition at line 647 of file BayesNet.h.

647 {
649 };
void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head's CPT.

References addWeightedArc(), and idFromName().

Here is the call graph for this function:

◆ addWeightedArc() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::addWeightedArc ( NodeId tail,
NodeId head,
GUM_SCALAR causalWeight )

Add an arc in the BN, and update arc.head's CPT.

Parameters
headand
tailas NodeId
causalWeightsee gum::MultiDimICIModel
Exceptions
InvalidArcIf arc.tail and/or arc.head are not in the BN.
InvalidArcIf variable in arc.head is not a NoisyOR variable.

Definition at line 532 of file BayesNet_tpl.h.

532 {
533 auto* CImodel = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(cpt(head).content());
534
535 if (CImodel != 0) {
536 // or is OK
537 addArc(tail, head);
538
539 CImodel->causalWeight(variable(tail), causalWeight);
540 } else {
542 "Head variable (" << variable(head).name() << ") is not a CIModel variable !")
543 }
544 }

References addArc(), cpt(), GUM_ERROR, and variable().

Referenced by addWeightedArc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ancestors() [1/2]

INLINE NodeSet gum::DAGmodel::ancestors ( const NodeId id) const
inherited

returns the set of nodes with directed path ingoing to a given node

Note that the set of nodes returned may be empty if no path within the ArcGraphPart is ingoing to the given node.

Parameters
idthe node which is the head of a directed path with the returned nodes
namethe name of the node which is the head of a directed path with the returned nodes

Definition at line 123 of file DAGmodel_inl.h.

123{ return dag().ancestors(id); }
NodeSet ancestors(NodeId id) const
returns the set of nodes with directed path ingoing to a given node

References gum::ArcGraphPart::ancestors(), and dag().

Referenced by ancestors().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ancestors() [2/2]

INLINE NodeSet gum::DAGmodel::ancestors ( const std::string & name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 125 of file DAGmodel_inl.h.

125 {
126 return ancestors(idFromName(name));
127 }
NodeSet ancestors(const NodeId id) const
returns the set of nodes with directed path ingoing to a given node
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.

References ancestors(), and gum::GraphicalModel::idFromName().

Here is the call graph for this function:

◆ arcs()

INLINE const ArcSet & gum::DAGmodel::arcs ( ) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 65 of file DAGmodel_inl.h.

65{ return dag_.arcs(); }

References dag_.

Referenced by hasSameStructure(), gum::MarkovBlanket::hasSameStructure(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

Here is the caller graph for this function:

◆ beginTopologyTransformation()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::beginTopologyTransformation ( )

When inserting/removing arcs, node CPTs change their dimension with a cost in time.

begin Multiple Change for all CPTs

These functions delay the CPTs change to be done just once at the end of a sequence of topology modification. begins a sequence of insertions/deletions of arcs without changing the dimensions of the CPTs.

Definition at line 554 of file BayesNet_tpl.h.

554 {
555 for (const auto node: nodes())
556 _probaMap_[node]->beginMultipleChanges();
557 }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.

References _probaMap_, and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ changeTensor() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::changeTensor ( const std::string & name,
Tensor< GUM_SCALAR > * newPot )

Definition at line 637 of file BayesNet_tpl.h.

637 {
639 }
void changeTensor(NodeId id, Tensor< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

References changeTensor(), and idFromName().

Here is the call graph for this function:

◆ changeTensor() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::changeTensor ( NodeId id,
Tensor< GUM_SCALAR > * newPot )

change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

Exceptions
NotAllowedif newPot has not the same signature as probaMap[NodeId]

Definition at line 611 of file BayesNet_tpl.h.

611 {
612 if (cpt(id).nbrDim() != newPot->nbrDim()) {
614 "cannot exchange tensors with different "
615 "dimensions for variable with id "
616 << id)
617 }
618
619 for (Idx i = 0; i < cpt(id).nbrDim(); i++) {
620 if (&cpt(id).variable(i) != &(newPot->variable(i))) {
622 "cannot exchange tensors because, for variable with id " << id << ", dimension "
623 << i << " differs. ")
624 }
625 }
626
628 }
void _unsafeChangeTensor_(NodeId id, Tensor< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

References cpt(), and GUM_ERROR.

Referenced by changeTensor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ changeVariableLabel() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::changeVariableLabel ( const std::string & name,
const std::string & old_label,
const std::string & new_label )
inline

Changes a variable's name.

Definition at line 371 of file BayesNet.h.

373 {
375 }
void changeVariableLabel(NodeId id, const std::string &old_label, const std::string &new_label)
Changes a variable's label in the gum::BayesNet.

References changeVariableLabel(), and idFromName().

Here is the call graph for this function:

◆ changeVariableLabel() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::changeVariableLabel ( NodeId id,
const std::string & old_label,
const std::string & new_label )

Changes a variable's label in the gum::BayesNet.

This will change the gum::LabelizedVariable names in the gum::BayesNet.

Exceptions
DuplicateLabelRaised if new_label is already used in this gum::LabelizedVariable.
NotFoundRaised if no variable matches id or if the variable is not a LabelizedVariable

Definition at line 177 of file BayesNet_tpl.h.

179 {
181 GUM_ERROR(NotFound, "Variable " << id << " is not a LabelizedVariable.")
182
185
187 }

References gum::LabelizedVariable::changeLabel(), GUM_ERROR, gum::LABELIZED, gum::LabelizedVariable::posLabel(), and variable().

Referenced by changeVariableLabel().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ changeVariableName() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::changeVariableName ( const std::string & name,
const std::string & new_name )
inline

Changes a variable's name.

Definition at line 352 of file BayesNet.h.

352 {
354 }
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable's name in the gum::BayesNet.

References changeVariableName(), and idFromName().

Here is the call graph for this function:

◆ changeVariableName() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::changeVariableName ( NodeId id,
const std::string & new_name )

Changes a variable's name in the gum::BayesNet.

This will change the gum::DiscreteVariable names in the gum::BayesNet.

Exceptions
DuplicateLabelRaised if newName is already used in this gum::BayesNet.
NotFoundRaised if no variable matches id.

Definition at line 172 of file BayesNet_tpl.h.

172 {
173 _varMap_.changeName(id, new_name);
174 }

References _varMap_.

Referenced by changeVariableName().

Here is the caller graph for this function:

◆ check()

template<typename GUM_SCALAR>
std::vector< std::string > gum::IBayesNet< GUM_SCALAR >::check ( ) const
inherited

Check if the BayesNet is consistent (variables, CPT).

Returns
a list of comments on the consistency of the Bayes Net

Definition at line 314 of file IBayesNet_tpl.h.

314 {
316
317 const double epsilon = 1e-8;
318 const double error_epsilon = 1e-1;
319
320 // CHECKING domain
321 for (const auto i: nodes())
322 if (variable(i).domainSize() < 2) {
324 s << "Variable " << variable(i).name() << ": not consistent (domainSize=1).";
325 comments.push_back(s.str());
326 }
327
328 // CHECKING parameters are probabilities
329 // >0
330 for (const auto i: nodes()) {
331 const auto [amin, minval] = cpt(i).argmin();
332 if (minval < (GUM_SCALAR)0.0) {
334 s << "Variable " << variable(i).name() << " : P(" << *(amin.begin()) << ") < 0.0";
335 comments.push_back(s.str());
336 }
337 }
338 // <1
339 for (const auto i: nodes()) {
340 const auto [amax, maxval] = cpt(i).argmax();
341 if (maxval > (GUM_SCALAR)1.0) {
343 s << "Variable " << variable(i).name() << " : P(" << *(amax.begin()) << ") > 1.0";
344 comments.push_back(s.str());
345 }
346 }
347
348 // CHECKING distributions sum to 1
349 for (const auto i: nodes()) {
350 const auto p = cpt(i).sumOut({&variable(i)});
351 const auto [amin, minval] = p.argmin();
352 if (minval < (GUM_SCALAR)(1.0 - epsilon)) {
354 s << "Variable " << variable(i).name() << " : ";
355 if (!parents(i).empty()) s << "with (at least) parents " << *(amin.begin()) << ", ";
356 s << "the CPT sum to less than 1";
357 if (minval > (GUM_SCALAR)(1.0 - error_epsilon)) s << " (normalization problem ?)";
358 s << ".";
359 comments.push_back(s.str());
360 continue;
361 }
362 const auto [amax, maxval] = p.argmax();
363 if (maxval > (GUM_SCALAR)(1.0 + epsilon)) {
365 s << "Variable " << variable(i).name() << " : ";
366 if (!parents(i).empty()) s << "with (at least) parents " << *(amax.begin()) << ", ";
367 s << "the CPT sum to more than 1";
368 if (maxval < (GUM_SCALAR)(1.0 + error_epsilon)) s << " (normalization problem ?)";
369 s << ".";
370 comments.push_back(s.str());
371 }
372 }
373
374 return comments;
375 }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
virtual bool empty() const
Return true if this graphical model is empty.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it's node id.
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.

References cpt(), gum::GraphicalModel::empty(), gum::DAGmodel::nodes(), gum::DAGmodel::parents(), and variable().

Here is the call graph for this function:

◆ children() [1/4]

INLINE const NodeSet & gum::DAGmodel::children ( const NodeId id) const
inherited

returns the set of nodes with arc outgoing from a given node

Note that the set of nodes returned may be empty if no node is outgoing from the given node.

Parameters
idthe node which is the tail of an arc with the returned nodes
namethe name of the node which is the tail of an arc with the returned nodes

Definition at line 87 of file DAGmodel_inl.h.

87{ return dag_.children(id); }

References dag_.

Referenced by gum::BayesNet< GUM_SCALAR >::erase(), gum::prm::ClassBayesNet< GUM_SCALAR >::toDot(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot().

Here is the caller graph for this function:

◆ children() [2/4]

INLINE NodeSet gum::DAGmodel::children ( const NodeSet & ids) const
inherited

returns the children of a set of nodes

Definition at line 93 of file DAGmodel_inl.h.

93{ return dag_.children(ids); }
std::vector< NodeId > ids(const std::vector< std::string > &names) const
transform a vector of names into a vector of nodeId

References dag_, and gum::GraphicalModel::ids().

Here is the call graph for this function:

◆ children() [3/4]

INLINE const NodeSet & gum::DAGmodel::children ( const std::string & name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 89 of file DAGmodel_inl.h.

89 {
90 return dag_.children(idFromName(name));
91 }

References dag_, and gum::GraphicalModel::idFromName().

Here is the call graph for this function:

◆ children() [4/4]

INLINE NodeSet gum::DAGmodel::children ( const std::vector< std::string > & names) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 95 of file DAGmodel_inl.h.

95 {
96 return children(nodeset(names));
97 }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
std::vector< std::string > names(const std::vector< NodeId > &ids) const
transform a vector of NodeId in a vector of names
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet

References gum::GraphicalModel::names().

Here is the call graph for this function:

◆ clear()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::clear ( )

clear the whole Bayes net *

Definition at line 289 of file BayesNet_tpl.h.

289 {
290 if (!this->empty()) {
291 auto l = this->nodes();
292 for (const auto no: l) {
293 this->erase(no);
294 }
295 }
296 }
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.

References gum::GraphicalModel::empty(), erase(), and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ completeInstantiation()

INLINE Instantiation gum::GraphicalModel::completeInstantiation ( ) const
inherited

Get an instantiation over all the variables of the model.

Definition at line 106 of file graphicalModel_inl.h.

106 {
107 Instantiation I;
108
109 for (const auto node: nodes())
110 I << variable(node);
111
112 return I;
113 }
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it's node id.
virtual const NodeGraphPart & nodes() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.

References nodes().

Here is the call graph for this function:

◆ contextualize()

template<typename GUM_SCALAR>
BayesNet< GUM_SCALAR > gum::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.

Parameters
observationsthe hard observations
interventionsthe hard interventions
Returns
a new BN with the same structure as this, but with the CPTs modified to reflect the observations and interventions
Warning
The original BN is not modified. The returned BN is not a copy of the original BN, but a new BN with copied variables and modified structure and CPTs.
Exceptions
ArgumentErrorif the observations and interventions are not mutually exclusive

Definition at line 643 of file BayesNet_tpl.h.

644 {
646 for (gum::Idx i = 0; i < observations.nbrDim(); i++) {
647 if (interventions.contains(observations.variable(i))) {
649 "Cannot have both an observation and an intervention on the same variable")
650 }
652 }
653 for (gum::Idx i = 0; i < observations.nbrDim(); i++) {
655 }
656 all.setVals(observations);
657 all.setVals(interventions);
658
660
662
664 for (gum::Idx i = 0; i < observations.nbrDim(); i++) {
666 const gum::NodeId nod = this->idFromName(nam);
667 for (gum::NodeId child: this->children(nod)) {
669 }
670 }
671 for (gum::Idx i = 0; i < interventions.nbrDim(); i++) {
673 const gum::NodeId nod = this->idFromName(nam);
674 for (gum::NodeId child: this->children(nod)) {
676 }
677 for (gum::NodeId par: this->parents(nod)) {
678 const auto v1 = bn.idFromName(this->variable(par).name());
679 const auto v2 = bn.idFromName(nam);
680 if (bn.existsArc(v1, v2)) bn.eraseArc(v1, v2);
681 }
682 cpt_changed.insert(bn.idFromName(nam));
685 interventions.val(i)));
686 }
688
689 for (gum::Idx i = 0; i < all.nbrDim(); i++) {
690 const gum::NodeId nod = this->idFromName(all.variable(i).name());
691 for (gum::NodeId child: this->children(nod)) {
692 if (!cpt_changed.contains(child)) {
693 cpt_changed.insert(child);
695 .fillWith(this->cpt(child).extract(all));
696 }
697 }
698 }
699
700 return bn;
701 }
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head's CTP.
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel

References gum::Instantiation::add(), gum::Instantiation::contains(), GUM_ERROR, gum::Instantiation::nbrDim(), and gum::Instantiation::variable().

Here is the call graph for this function:

◆ cpt() [1/2]

template<typename GUM_SCALAR>
const Tensor< GUM_SCALAR > & gum::BayesNet< GUM_SCALAR >::cpt ( const std::string & name) const
inline

Returns the CPT of a variable.

Definition at line 182 of file BayesNet.h.

182 {
183 return cpt(idFromName(name));
184 };

References cpt(), and idFromName().

Here is the call graph for this function:

◆ cpt() [2/2]

template<typename GUM_SCALAR>
INLINE const Tensor< GUM_SCALAR > & gum::BayesNet< GUM_SCALAR >::cpt ( NodeId varId) const
finalvirtual

Returns the CPT of a variable.

Parameters
varIdA variable's id in the gum::BayesNet.
Returns
The variable's CPT.
Exceptions
NotFoundIf no variable's id matches varId.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 258 of file BayesNet_tpl.h.

258 {
259 return *(_probaMap_[varId]);
260 }

References _probaMap_.

Referenced by gum::BayesNet< double >::_copyTensors_(), add(), addWeightedArc(), changeTensor(), cpt(), generateCPT(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

Here is the caller graph for this function:

◆ dag()

◆ descendants() [1/2]

INLINE NodeSet gum::DAGmodel::descendants ( const NodeId id) const
inherited

returns the set of nodes with directed path outgoing from a given node

Note that the set of nodes returned may be empty if no path within the ArcGraphPart is outgoing from the given node.

Parameters
idthe node which is the tail of a directed path with the returned nodes
namethe name of the node which is the tail of a directed path with the returned nodes

Definition at line 117 of file DAGmodel_inl.h.

117{ return dag().descendants(id); }
NodeSet descendants(NodeId id) const
returns the set of nodes with directed path outgoing from a given node

References dag(), and gum::ArcGraphPart::descendants().

Referenced by descendants().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ descendants() [2/2]

INLINE NodeSet gum::DAGmodel::descendants ( const std::string & name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 119 of file DAGmodel_inl.h.

119 {
120 return descendants(idFromName(name));
121 }
NodeSet descendants(const NodeId id) const
returns the set of nodes with directed path outgoing from a given node

References descendants(), and gum::GraphicalModel::idFromName().

Here is the call graph for this function:

◆ dim()

template<typename GUM_SCALAR>
Size gum::IBayesNet< GUM_SCALAR >::dim ( ) const
inherited

Returns the dimension (the number of free parameters) in this bayes net.

\( dim(G)=\sum_{i \in nodes} ((r_i-1)\cdot q_i) \) where \( r_i \) is the number of instantiations of node \( i \) and \( q_i \) is the number of instantiations of its parents.

Definition at line 95 of file IBayesNet_tpl.h.

95 {
96 Size dim = 0;
97
98 for (auto node: nodes()) {
99 Size q = 1;
100
101 for (auto parent: parents(node))
103
104 dim += (variable(node).domainSize() - 1) * q;
105 }
106
107 return dim;
108 }
virtual Size domainSize() const =0
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.

References dim(), and gum::DAGmodel::nodes().

Referenced by dim(), and toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ empty()

INLINE bool gum::GraphicalModel::empty ( ) const
virtualinherited

Return true if this graphical model is empty.

Definition at line 116 of file graphicalModel_inl.h.

116{ return size() == 0; }
virtual Size size() const =0
Returns the number of variables in this Directed Graphical Model.

References size().

Referenced by gum::IBayesNet< GUM_SCALAR >::check(), and gum::BayesNet< GUM_SCALAR >::clear().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ endTopologyTransformation()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::endTopologyTransformation ( )

terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.

end Multiple Change for all CPTs

Definition at line 561 of file BayesNet_tpl.h.

561 {
562 for (const auto node: nodes())
563 _probaMap_[node]->endMultipleChanges();
564 }

References _probaMap_, and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ erase() [1/3]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::erase ( const DiscreteVariable & var)

Remove a variable from the gum::BayesNet.

Removes the corresponding variable from the gum::BayesNet and from all of it's children gum::Tensor.

If no variable matches the given variable, then nothing is done.

Parameters
varA reference on the variable to remove.

Definition at line 268 of file BayesNet_tpl.h.

268 {
269 erase(_varMap_.get(var));
270 }

References _varMap_, and erase().

Here is the call graph for this function:

◆ erase() [2/3]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::erase ( const std::string & name)
inline

Removes a variable from the gum::BayesNet.

Definition at line 304 of file BayesNet.h.

304{ erase(idFromName(name)); };

References erase(), erase(), and idFromName().

Referenced by erase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ erase() [3/3]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::erase ( NodeId varId)

Remove a variable from the gum::BayesNet.

Removes the corresponding variable from the gum::BayesNet and from all of it's children gum::Tensor.

If no variable matches the given id, then nothing is done.

Parameters
varIdThe variable's id to remove.

Definition at line 273 of file BayesNet_tpl.h.

273 {
274 if (_varMap_.exists(varId)) {
275 // Reduce the variable child's CPT
276 for (const NodeSet& children = this->children(varId); const auto c: children) {
277 _probaMap_[c]->erase(variable(varId));
278 }
279
280 delete _probaMap_[varId];
281
282 _probaMap_.erase(varId);
283 _varMap_.erase(varId);
284 this->dag_.eraseNode(varId);
285 }
286 }

References _probaMap_, _varMap_, gum::DAGmodel::children(), gum::DAGmodel::dag_, and variable().

Referenced by clear(), erase(), and erase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ eraseArc() [1/3]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::eraseArc ( const Arc & arc)

Removes an arc in the BN, and update head's CTP.

If (tail, head) doesn't exist, the nothing happens.

Parameters
arcThe arc removed.

Definition at line 319 of file BayesNet_tpl.h.

319 {
320 if (_varMap_.exists(arc.tail()) && _varMap_.exists(arc.head())) {
321 NodeId head = arc.head();
322 NodeId tail = arc.tail();
323 this->dag_.eraseArc(arc);
324 // Remove parent from child's CPT
325 (*(_probaMap_[head])) >> variable(tail);
326 }
327 }

References _probaMap_, _varMap_, gum::DAGmodel::dag_, gum::Arc::head(), gum::Arc::tail(), and variable().

Referenced by eraseArc(), and eraseArc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ eraseArc() [2/3]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::eraseArc ( const std::string & tail,
const std::string & head )
inline

Removes an arc in the BN, and update head's CTP.

Definition at line 451 of file BayesNet.h.

451 {
453 }

References eraseArc(), and idFromName().

Here is the call graph for this function:

◆ eraseArc() [3/3]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::eraseArc ( NodeId tail,
NodeId head )

Removes an arc in the BN, and update head's CTP.

If (tail, head) doesn't exist, the nothing happens.

Parameters
headand
tailas NodeId

Definition at line 330 of file BayesNet_tpl.h.

330 {
332 }

References eraseArc().

Here is the call graph for this function:

◆ evEq()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::IBayesNet< GUM_SCALAR >::evEq ( const std::string & name,
double value ) const
inherited
Returns
a Tensor for a (numerical) discrete variable representing an evidence with a float as observed value

Definition at line 378 of file IBayesNet_tpl.h.

378 {
380 }
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
static Tensor< GUM_SCALAR > evEq(const DiscreteVariable &v, double val)
numerical evidence generator

References gum::Tensor< GUM_SCALAR >::evEq(), and variableFromName().

Here is the call graph for this function:

◆ evGt()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::IBayesNet< GUM_SCALAR >::evGt ( const std::string & name,
double value ) const
inherited
Returns
a Tensor for a (numerical) discrete variable representing an evidence with an observed value greater than the parameter

Definition at line 389 of file IBayesNet_tpl.h.

389 {
391 }
static Tensor< GUM_SCALAR > evGt(const DiscreteVariable &v, double val)
numerical evidence generator

References gum::Tensor< GUM_SCALAR >::evGt(), and variableFromName().

Here is the call graph for this function:

◆ evIn()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::IBayesNet< GUM_SCALAR >::evIn ( const std::string & name,
double val1,
double val2 ) const
inherited
Returns
a Tensor for a (numerical) discrete variable representing an evidence with a interval of float as observed value

Definition at line 384 of file IBayesNet_tpl.h.

384 {
386 }
static Tensor< GUM_SCALAR > evIn(const DiscreteVariable &v, double val1, double val2)
numerical evidence generator

References gum::Tensor< GUM_SCALAR >::evIn(), and variableFromName().

Here is the call graph for this function:

◆ evLt()

template<typename GUM_SCALAR>
Tensor< GUM_SCALAR > gum::IBayesNet< GUM_SCALAR >::evLt ( const std::string & name,
double value ) const
inherited
Returns
a Tensor for a (numerical) discrete variable representing an evidence with an observed value less than the parameter

Definition at line 394 of file IBayesNet_tpl.h.

394 {
396 }
static Tensor< GUM_SCALAR > evLt(const DiscreteVariable &v, double val)
numerical evidence generator

References gum::Tensor< GUM_SCALAR >::evLt(), and variableFromName().

Here is the call graph for this function:

◆ exists() [1/2]

INLINE bool gum::DAGmodel::exists ( const std::string & name) const
finalvirtualinherited

Returns a constant reference to the dag of this Bayes Net.

Implements gum::GraphicalModel.

Definition at line 107 of file DAGmodel_inl.h.

107 {
108 try {
109 return exists(idFromName(name));
110 } catch ([[maybe_unused]] gum::NotFound& e) { return false; }
111 }
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.

◆ exists() [2/2]

INLINE bool gum::DAGmodel::exists ( NodeId node) const
finalvirtualinherited

Return true if this node exists in this graphical model.

Implements gum::GraphicalModel.

Definition at line 105 of file DAGmodel_inl.h.

105{ return dag_.exists(node); }

References dag_.

◆ existsArc() [1/2]

INLINE bool gum::DAGmodel::existsArc ( const NodeId tail,
const NodeId head ) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 67 of file DAGmodel_inl.h.

67 {
68 return dag_.existsArc(tail, head);
69 }

References dag_.

Referenced by existsArc(), gum::BayesNet< GUM_SCALAR >::reverseArc(), gum::BayesNetFragment< GUM_SCALAR >::toDot(), and gum::BayesNetFragment< GUM_SCALAR >::whenArcDeleted().

Here is the caller graph for this function:

◆ existsArc() [2/2]

INLINE bool gum::DAGmodel::existsArc ( const std::string & nametail,
const std::string & namehead ) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 71 of file DAGmodel_inl.h.

71 {
72 return existsArc(idFromName(nametail), idFromName(namehead));
73 }

References existsArc(), and gum::GraphicalModel::idFromName().

Here is the call graph for this function:

◆ family() [1/2]

INLINE NodeSet gum::DAGmodel::family ( const NodeId id) const
finalvirtualinherited

returns the parents of a node and the node

Parameters
idthe node which is the head of an arc with the returned nodes
namethe name of the node the node which is the head of an arc with the returned nodes

Implements gum::GraphicalModel.

Definition at line 81 of file DAGmodel_inl.h.

81{ return dag_.family(id); }

References dag_.

◆ family() [2/2]

INLINE NodeSet gum::DAGmodel::family ( const std::string & name) const
finalvirtualinherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Implements gum::GraphicalModel.

Definition at line 83 of file DAGmodel_inl.h.

83 {
84 return dag_.family(idFromName(name));
85 }

References dag_, and gum::GraphicalModel::idFromName().

Here is the call graph for this function:

◆ fastPrototype() [1/2]

template<typename GUM_SCALAR>
BayesNet< GUM_SCALAR > gum::BayesNet< GUM_SCALAR >::fastPrototype ( const std::string & dotlike,
const std::string & domainSize = "[2]" )
static

Definition at line 96 of file BayesNet_tpl.h.

97 {
99
100 for (const auto& chaine: split(remove_newline(dotlike), ";")) {
101 NodeId lastId = 0;
102 bool notfirst = false;
103 for (const auto& souschaine: split(chaine, "->")) {
104 bool forward = true;
105 for (auto& node: split(souschaine, "<-")) {
106 auto idVar = build_node(bn, node, domain);
107 if (notfirst) {
108 if (forward) {
110 forward = false;
111 } else {
113 }
114 } else {
115 notfirst = true;
116 forward = false;
117 }
118 lastId = idVar;
119 }
120 }
121 }
123 bn.setProperty("name", "fastPrototype");
124 return bn;
125 }
void generateCPTs() const
randomly generates CPTs for a given structure
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.

References addArc(), gum::build_node(), gum::remove_newline(), and gum::split().

Here is the call graph for this function:

◆ fastPrototype() [2/2]

template<typename GUM_SCALAR>
BayesNet< GUM_SCALAR > gum::BayesNet< GUM_SCALAR >::fastPrototype ( const std::string & dotlike,
Size domainSize )
static

Create a Bayesian network with a dot-like syntax which specifies:

  • the structure "a->b->c;b->d<-e;".
  • the type of the variables with different syntax:

Note that if the dot-like string contains such a specification more than once for a variable, the first specification will be used.

Parameters
dotlikethe string containing the specification
domainSizethe default domain size for variables
Returns
the resulting Bayesian network

Definition at line 90 of file BayesNet_tpl.h.

91 {
92 return fastPrototype(dotlike, "[" + std::to_string(domainSize) + "]");
93 }
static BayesNet< GUM_SCALAR > fastPrototype(const std::string &dotlike, Size domainSize)
Create a Bayesian network with a dot-like syntax which specifies:

References fastPrototype().

Referenced by fastPrototype().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateCPT() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::generateCPT ( const std::string & name) const
inline

Definition at line 659 of file BayesNet.h.

void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure

References generateCPT(), and idFromName().

Referenced by generateCPT().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateCPT() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::generateCPT ( NodeId node) const

randomly generate CPT for a given node in a given structure

Definition at line 604 of file BayesNet_tpl.h.

References cpt(), and variable().

Referenced by generateCPTs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateCPTs()

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::generateCPTs ( ) const

randomly generates CPTs for a given structure

Definition at line 598 of file BayesNet_tpl.h.

598 {
599 for (const auto node: nodes())
601 }

References generateCPT(), and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ hasSameStructure()

bool gum::DAGmodel::hasSameStructure ( const DAGmodel & other)
inherited
Returns
true if all the named node are the same and all the named arcs are the same

Definition at line 66 of file DAGmodel.cpp.

66 {
67 if (this == &other) return true;
68
69 if (size() != other.size()) return false;
70
71 if (sizeArcs() != other.sizeArcs()) return false;
72
73 for (const auto& nid: nodes()) {
74 try {
75 other.idFromName(variable(nid).name());
76 } catch (NotFound const&) { return false; }
77 }
78
79 for (const auto& arc: arcs()) {
80 if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
81 other.idFromName(variable(arc.head()).name()))))
82 return false;
83 }
84
85 return true;
86 }
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.

References DAGmodel(), arcs(), gum::Set< Key >::exists(), gum::GraphicalModel::idFromName(), nodes(), size(), sizeArcs(), and gum::GraphicalModel::variable().

Here is the call graph for this function:

◆ idFromName()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::idFromName ( const std::string & name) const
finalvirtual

Returns a variable's id given its name in the gum::BayesNet.

Parameters
nameThe variable's name from which the gum::NodeId is returned.
Returns
Returns the variable gum::NodeId in the gum::BayesNet.
Exceptions
NotFoundRaised if name does not match a variable in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 247 of file BayesNet_tpl.h.

247 {
248 return _varMap_.idFromName(name);
249 }

References _varMap_.

Referenced by gum::BayesNet< double >::_copyTensors_(), addArc(), addWeightedArc(), gum::build_node(), changeTensor(), changeVariableLabel(), changeVariableName(), cpt(), erase(), eraseArc(), generateCPT(), reverseArc(), and variable().

Here is the caller graph for this function:

◆ ids()

INLINE std::vector< NodeId > gum::GraphicalModel::ids ( const std::vector< std::string > & names) const
inherited

transform a vector of names into a vector of nodeId

Returns
the vector of names

Definition at line 139 of file graphicalModel_inl.h.

139 {
140 std::vector< NodeId > res;
141 const VariableNodeMap& v = variableNodeMap();
142 std::transform(names.cbegin(),
143 names.cend(),
144 std::back_inserter(res),
145 [v](const std::string& n) { return v.idFromName(n); });
146 return res;
147 }
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.

References names().

Referenced by gum::DAGmodel::children(), exists(), names(), and gum::DAGmodel::parents().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isIndependent() [1/4]

INLINE bool gum::DAGmodel::isIndependent ( const NodeSet & X,
const NodeSet & Y,
const NodeSet & Z ) const
finalvirtualinherited

check if nodes X and nodes Y are independent given nodes Z

Implements gum::GraphicalModel.

Definition at line 142 of file DAGmodel_inl.h.

142 {
143 return dag().dSeparation(X, Y, Z);
144 }
bool dSeparation(NodeId X, NodeId Y, const NodeSet &Z) const
check if node X and node Y are independent given nodes Z (in the sense of d-separation)
Definition DAG.cpp:117

References dag(), and gum::DAG::dSeparation().

Here is the call graph for this function:

◆ isIndependent() [2/4]

bool gum::DAGmodel::isIndependent ( const std::string & Xname,
const std::string & Yname,
const std::vector< std::string > & Znames ) const
inlineinherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 202 of file DAGmodel.h.

204 {
205 return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
206 };
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if node X and node Y are independent given nodes Z

References gum::GraphicalModel::idFromName(), isIndependent(), and gum::GraphicalModel::nodeset().

Here is the call graph for this function:

◆ isIndependent() [3/4]

bool gum::DAGmodel::isIndependent ( const std::vector< std::string > & Xnames,
const std::vector< std::string > & Ynames,
const std::vector< std::string > & Znames ) const
inlineinherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 208 of file DAGmodel.h.

210 {
211 return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
212 };

References isIndependent(), and gum::GraphicalModel::nodeset().

Here is the call graph for this function:

◆ isIndependent() [4/4]

INLINE bool gum::DAGmodel::isIndependent ( NodeId X,
NodeId Y,
const NodeSet & Z ) const
finalvirtualinherited

check if node X and node Y are independent given nodes Z

Implements gum::GraphicalModel.

Definition at line 138 of file DAGmodel_inl.h.

138 {
139 return dag().dSeparation(X, Y, Z);
140 }

References dag(), and gum::DAG::dSeparation().

Referenced by isIndependent(), and isIndependent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ jointProbability()

template<typename GUM_SCALAR>
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::jointProbability ( const Instantiation & i) const
inherited

Compute a parameter of the joint probability for the BN (given an instantiation of the vars).

Warning
a variable not present in the instantiation is assumed to be instantiated to 0.

Definition at line 221 of file IBayesNet_tpl.h.

221 {
222 auto value = (GUM_SCALAR)1.0;
223
225
226 for (auto node: nodes()) {
227 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) { return (GUM_SCALAR)0; }
228
229 value *= tmp;
230 }
231
232 return value;
233 }

References cpt(), and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ log10DomainSize()

INLINE double gum::GraphicalModel::log10DomainSize ( ) const
inherited

Definition at line 95 of file graphicalModel_inl.h.

95 {
96 double dSize = 0.0;
97
98 for (const auto node: nodes()) {
99 dSize += std::log10(variable(node).domainSize());
100 }
101
102 return dSize;
103 }

References nodes().

Referenced by gum::IBayesNet< GUM_SCALAR >::toString(), gum::IMarkovRandomField< GUM_SCALAR >::toString(), and gum::InfluenceDiagram< GUM_SCALAR >::toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ log2JointProbability()

template<typename GUM_SCALAR>
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::log2JointProbability ( const Instantiation & i) const
inherited

Compute a parameter of the log joint probability for the BN (given an instantiation of the vars).

Compute a parameter of the joint probability for the BN (given an instantiation of the vars).

Warning
a variable not present in the instantiation is assumed to be instantiated to 0.

Definition at line 239 of file IBayesNet_tpl.h.

239 {
240 auto value = (GUM_SCALAR)0.0;
241
243
244 for (auto node: nodes()) {
245 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
247 }
248
249 value += std::log2(cpt(node)[i]);
250 }
251
252 return value;
253 }

◆ maxNonOneParam()

template<typename GUM_SCALAR>
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::maxNonOneParam ( ) const
inherited
Returns
the biggest value (not equal to 1) in the CPTs of *this
Warning
can return one if no other value in the CPTs than one....

Definition at line 151 of file IBayesNet_tpl.h.

151 {
152 GUM_SCALAR res = 0.0;
153 for (auto node: nodes()) {
154 auto v = cpt(node).maxNonOne();
155 if (v > res) { res = v; }
156 }
157 return res;
158 }

References cpt(), and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ maxParam()

template<typename GUM_SCALAR>
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::maxParam ( ) const
inherited
Returns
the biggest value in the CPTs of *this

Definition at line 131 of file IBayesNet_tpl.h.

131 {
132 GUM_SCALAR res = 1.0;
133 for (auto node: nodes()) {
134 auto v = cpt(node).max();
135 if (v > res) { res = v; }
136 }
137 return res;
138 }

References cpt(), and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ maxVarDomainSize()

template<typename GUM_SCALAR>
Size gum::IBayesNet< GUM_SCALAR >::maxVarDomainSize ( ) const
inherited
Returns
the biggest domainSize among the variables of *this

Definition at line 111 of file IBayesNet_tpl.h.

111 {
112 Size res = 0;
113 for (auto node: nodes()) {
114 auto v = variable(node).domainSize();
115 if (v > res) { res = v; }
116 }
117 return res;
118 }

References gum::DAGmodel::nodes(), and variable().

Referenced by gum::ImportanceSampling< GUM_SCALAR >::onContextualize_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ memoryFootprint()

template<typename GUM_SCALAR>
INLINE Size gum::IBayesNet< GUM_SCALAR >::memoryFootprint ( ) const
inherited

compute the (approximated) footprint in memory of the model (the footprints of CPTs)

Returns
the size in bytes

Definition at line 161 of file IBayesNet_tpl.h.

161 {
162 Size usedMem = 0;
163
164 for (auto node: nodes())
165 usedMem += cpt(node).memoryFootprint();
166 return usedMem;
167 }

References gum::DAGmodel::nodes().

Referenced by toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ minimalCondSet() [1/4]

INLINE NodeSet gum::DAGmodel::minimalCondSet ( const NodeSet & targets,
const NodeSet & soids ) const
inherited

Definition at line 150 of file DAGmodel_inl.h.

150 {
151 return dag_.minimalCondSet(targets, soids);
152 }

References dag_.

◆ minimalCondSet() [2/4]

INLINE NodeSet gum::DAGmodel::minimalCondSet ( const std::string & target,
const std::vector< std::string > & soids ) const
inherited

Definition at line 154 of file DAGmodel_inl.h.

155 {
156 return dag_.minimalCondSet(idFromName(target), nodeset(soids));
157 }

◆ minimalCondSet() [3/4]

INLINE NodeSet gum::DAGmodel::minimalCondSet ( const std::vector< std::string > & targets,
const std::vector< std::string > & soids ) const
inherited

Definition at line 159 of file DAGmodel_inl.h.

160 {
161 return dag_.minimalCondSet(nodeset(targets), nodeset(soids));
162 }

◆ minimalCondSet() [4/4]

INLINE NodeSet gum::DAGmodel::minimalCondSet ( NodeId target,
const NodeSet & soids ) const
inherited

Definition at line 146 of file DAGmodel_inl.h.

146 {
147 return dag_.minimalCondSet(target, soids);
148 }

◆ minNonZeroParam()

template<typename GUM_SCALAR>
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::minNonZeroParam ( ) const
inherited
Returns
the smallest value (not equal to 0) in the CPTs of *this
Warning
can return 0 if no other value in the CPTs than 0...

Definition at line 141 of file IBayesNet_tpl.h.

141 {
142 GUM_SCALAR res = 1.0;
143 for (auto node: nodes()) {
144 auto v = cpt(node).minNonZero();
145 if (v < res) { res = v; }
146 }
147 return res;
148 }

References cpt(), and gum::DAGmodel::nodes().

Referenced by gum::ImportanceSampling< GUM_SCALAR >::onContextualize_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ minParam()

template<typename GUM_SCALAR>
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::minParam ( ) const
inherited
Returns
the smallest value in the CPTs of *this

Definition at line 121 of file IBayesNet_tpl.h.

121 {
122 GUM_SCALAR res = 1.0;
123 for (auto node: nodes()) {
124 auto v = cpt(node).min();
125 if (v < res) { res = v; }
126 }
127 return res;
128 }

References cpt(), and gum::DAGmodel::nodes().

Here is the call graph for this function:

◆ moralGraph()

UndiGraph gum::DAGmodel::moralGraph ( ) const
inherited

The node's id are coherent with the variables and nodes of the topology.

Definition at line 64 of file DAGmodel.cpp.

64{ return dag().moralGraph(); }
UndiGraph moralGraph() const
build a UndiGraph by moralizing the dag
Definition DAG.cpp:68

References dag(), and gum::DAG::moralGraph().

Referenced by gum::prm::SVE< GUM_SCALAR >::_eliminateNodes_(), gum::prm::SVED< GUM_SCALAR >::_eliminateNodes_(), gum::prm::SVE< GUM_SCALAR >::_eliminateNodesWithEvidence_(), gum::prm::SVED< GUM_SCALAR >::_eliminateNodesWithEvidence_(), gum::prm::SVE< GUM_SCALAR >::_initLiftedNodes_(), and gum::prm::SVED< GUM_SCALAR >::_initLiftedNodes_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ moralizedAncestralGraph() [1/2]

INLINE UndiGraph gum::DAGmodel::moralizedAncestralGraph ( const NodeSet & nodes) const
inherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 134 of file DAGmodel_inl.h.

134 {
136 }
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition DAG.cpp:91

References dag(), gum::DAG::moralizedAncestralGraph(), and nodes().

Referenced by moralizedAncestralGraph().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ moralizedAncestralGraph() [2/2]

INLINE UndiGraph gum::DAGmodel::moralizedAncestralGraph ( const std::vector< std::string > & nodenames) const
inherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 130 of file DAGmodel_inl.h.

130 {
131 return moralizedAncestralGraph(nodeset(nodenames));
132 }
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

References moralizedAncestralGraph(), and gum::GraphicalModel::nodeset().

Here is the call graph for this function:

◆ names() [1/2]

INLINE std::vector< std::string > gum::GraphicalModel::names ( const NodeSet & ids) const
inherited

transform a NodeSet in a vector of names

Returns
the vector of names

Definition at line 129 of file graphicalModel_inl.h.

129 {
130 const VariableNodeMap& v = variableNodeMap();
131 std::vector< std::string > res;
132 for (auto n: ids) {
133 res.push_back(v.name(n));
134 }
135 return res;
136 }

◆ names() [2/2]

INLINE std::vector< std::string > gum::GraphicalModel::names ( const std::vector< NodeId > & ids) const
inherited

transform a vector of NodeId in a vector of names

Returns
the vector of names

Definition at line 119 of file graphicalModel_inl.h.

119 {
120 std::vector< std::string > res;
121 const VariableNodeMap& v = variableNodeMap();
122 std::transform(ids.cbegin(), ids.cend(), std::back_inserter(res), [v](NodeId n) {
123 return v[n].name();
124 });
125 return res;
126 }
Size NodeId
Type for node ids.

References ids(), and variableNodeMap().

Referenced by gum::DAGmodel::children(), exists(), ids(), nodeset(), and gum::DAGmodel::parents().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nodeId()

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::nodeId ( const DiscreteVariable & var) const
finalvirtual

Returns a variable's id in the gum::BayesNet.

Parameters
varThe variable from which the gum::NodeId is returned.
Returns
Returns the gum::DiscreteVariable gum::NodeId in the gum::BayesNet.
Exceptions
NotFoundIf var is not in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 190 of file BayesNet_tpl.h.

190 {
191 return _varMap_.get(var);
192 }

◆ nodes()

INLINE const NodeGraphPart & gum::DAGmodel::nodes ( ) const
finalvirtualinherited

Returns a constant reference to the dag of this Bayes Net.

Implements gum::GraphicalModel.

Definition at line 113 of file DAGmodel_inl.h.

113{ return (NodeGraphPart&)dag_; }

References dag_.

Referenced by gum::Estimator< GUM_SCALAR >::Estimator(), gum::BayesNetFragment< GUM_SCALAR >::~BayesNetFragment(), gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::_verticesSampling_(), gum::BayesNet< GUM_SCALAR >::beginTopologyTransformation(), gum::InfluenceDiagram< GUM_SCALAR >::beginTopologyTransformation(), gum::IBayesNet< GUM_SCALAR >::check(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNet< GUM_SCALAR >::clear(), gum::InfluenceDiagram< GUM_SCALAR >::copyStructureAndTables_(), gum::IBayesNet< GUM_SCALAR >::dim(), gum::BayesNet< GUM_SCALAR >::endTopologyTransformation(), gum::InfluenceDiagram< GUM_SCALAR >::endTopologyTransformation(), gum::InfluenceDiagram< GUM_SCALAR >::fastPrototype(), gum::BayesNet< GUM_SCALAR >::generateCPTs(), gum::getMaxModality(), hasSameStructure(), gum::IBayesNet< GUM_SCALAR >::jointProbability(), gum::IBayesNet< GUM_SCALAR >::maxNonOneParam(), gum::IBayesNet< GUM_SCALAR >::maxParam(), gum::IBayesNet< GUM_SCALAR >::maxVarDomainSize(), gum::IBayesNet< GUM_SCALAR >::memoryFootprint(), gum::IBayesNet< GUM_SCALAR >::minNonZeroParam(), gum::IBayesNet< GUM_SCALAR >::minParam(), gum::prm::ClassBayesNet< GUM_SCALAR >::modalities(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), moralizedAncestralGraph(), gum::InfluenceDiagram< GUM_SCALAR >::operator==(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::BayesNetFragment< GUM_SCALAR >::toBN(), gum::prm::ClassBayesNet< GUM_SCALAR >::toDot(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::ImportanceSampling< GUM_SCALAR >::unsharpenBN_().

◆ nodeset()

NodeSet gum::GraphicalModel::nodeset ( const std::vector< std::string > & names) const
inherited

transform a vector of names into a NodeSet

Returns
NodeSet

Definition at line 60 of file graphicalModel.cpp.

60 {
61 NodeSet res;
62 for (const auto& name: names) {
63 res.insert(idFromName(name));
64 }
65 return res;
66 }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...

References idFromName(), gum::Set< Key >::insert(), and names().

Referenced by gum::IBayesNet< double >::children(), gum::IBayesNet< double >::children(), gum::DAGmodel::isIndependent(), gum::DAGmodel::isIndependent(), gum::UGmodel::isIndependent(), gum::UGmodel::isIndependent(), gum::DAGmodel::moralizedAncestralGraph(), and gum::DAGmodel::parents().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator!=()

template<typename GUM_SCALAR>
bool gum::IBayesNet< GUM_SCALAR >::operator!= ( const IBayesNet< GUM_SCALAR > & from) const
inherited
Returns
Returns false if the src and this are equal.

Definition at line 303 of file IBayesNet_tpl.h.

303 {
304 return !this->operator==(from);
305 }
bool operator==(const IBayesNet< GUM_SCALAR > &from) const
This operator compares 2 BNs !

References IBayesNet(), and gum::operator==().

Here is the call graph for this function:

◆ operator=()

template<typename GUM_SCALAR>
BayesNet< GUM_SCALAR > & gum::BayesNet< GUM_SCALAR >::operator= ( const BayesNet< GUM_SCALAR > & source)

Copy operator.

Parameters
sourceThe copied BayesNet.
Returns
The copy of source.

Definition at line 146 of file BayesNet_tpl.h.

146 {
147 if (this != &source) {
150
153 }
154
155 return *this;
156 }
void _clearTensors_()
clear all tensors
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.

References _clearTensors_(), _copyTensors_(), _varMap_, and gum::IBayesNet< GUM_SCALAR >::operator=().

Here is the call graph for this function:

◆ operator==()

template<typename GUM_SCALAR>
bool gum::IBayesNet< GUM_SCALAR >::operator== ( const IBayesNet< GUM_SCALAR > & from) const
inherited

This operator compares 2 BNs !

Warning
To identify nodes between BNs, it is assumed that they share the same name.
Returns
true if the src and this are equal.

Definition at line 256 of file IBayesNet_tpl.h.

256 {
257 if (size() != from.size()) { return false; }
258
259 if (sizeArcs() != from.sizeArcs()) { return false; }
260
261 for (auto node: nodes()) {
262 try {
263 const auto& v1 = variable(node);
264 const auto& v2 = from.variableFromName(variable(node).name());
265 if (v1 != v2) { return false; }
266 } catch (NotFound const&) {
267 // a name is not found in from
268 return false;
269 }
270 }
271
272 for (auto node: nodes()) {
274
275 if (cpt(node).nbrDim() != from.cpt(fromnode).nbrDim()) { return false; }
276
277 if (cpt(node).domainSize() != from.cpt(fromnode).domainSize()) { return false; }
278
279 for (Idx i = 0; i < cpt(node).nbrDim(); ++i) {
280 if (!from.cpt(fromnode).contains(from.variableFromName(cpt(node).variable(i).name()))) {
281 return false;
282 }
283 }
284
287
289 for (i.setFirst(); !i.end(); i.inc()) {
290 for (Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
291 const DiscreteVariable* p = &(i.variable(indice));
292 j.chgVal(j.pos(from.variableFromName(p->name())), i.val(*p));
293 }
294
295 if (cmp(cpt(node).get(i), from.cpt(fromnode).get(j))) { return false; }
296 }
297 }
298
299 return true;
300 }
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.

References IBayesNet(), and gum::DAGmodel::size().

Here is the call graph for this function:

◆ parents() [1/4]

INLINE const NodeSet & gum::DAGmodel::parents ( const NodeId id) const
inherited

returns the set of nodes with arc ingoing to a given node

Note that the set of nodes returned may be empty if no arc within the ArcGraphPart is ingoing into the given node.

Parameters
idthe node which is the head of an arc with the returned nodes
namethe name of the node the node which is the head of an arc with the returned nodes

Definition at line 75 of file DAGmodel_inl.h.

75{ return dag_.parents(id); }

References dag_.

Referenced by gum::IBayesNet< GUM_SCALAR >::check(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::InfluenceDiagram< GUM_SCALAR >::copyStructureAndTables_(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), gum::BayesNetFragment< GUM_SCALAR >::installCPT_(), parents(), parents(), gum::prm::ClassBayesNet< GUM_SCALAR >::toDot(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot().

Here is the caller graph for this function:

◆ parents() [2/4]

INLINE NodeSet gum::DAGmodel::parents ( const NodeSet & ids) const
inherited

returns the parents of a set of nodes

Definition at line 99 of file DAGmodel_inl.h.

99{ return dag_.children(ids); }

References dag_, and gum::GraphicalModel::ids().

Here is the call graph for this function:

◆ parents() [3/4]

INLINE const NodeSet & gum::DAGmodel::parents ( const std::string & name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 77 of file DAGmodel_inl.h.

77 {
78 return parents(idFromName(name));
79 }

References gum::GraphicalModel::idFromName(), and parents().

Here is the call graph for this function:

◆ parents() [4/4]

INLINE NodeSet gum::DAGmodel::parents ( const std::vector< std::string > & names) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 101 of file DAGmodel_inl.h.

101 {
102 return parents(nodeset(names));
103 }

References gum::GraphicalModel::names(), gum::GraphicalModel::nodeset(), and parents().

Here is the call graph for this function:

◆ properties()

INLINE std::vector< std::string > gum::GraphicalModel::properties ( ) const
inherited

List of all the properties.

Definition at line 81 of file graphicalModel_inl.h.

81 {
82 std::vector< std::string > prop;
83 for (const auto& [p, v]: _propertiesMap_)
84 prop.push_back(p);
85 return prop;
86 }

References _propertiesMap_.

◆ property()

INLINE const std::string & gum::GraphicalModel::property ( const std::string & name) const
inherited

Return the value of the property name of this GraphicalModel.

Exceptions
NotFoundRaised if no name property is found.

Definition at line 60 of file graphicalModel_inl.h.

60 {
61 try {
62 return _properties_()[name];
63 } catch (NotFound const&) {
64 std::string msg = "The following property does not exists: ";
65 GUM_ERROR(NotFound, msg + name)
66 }
67 }
const HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model.

References _properties_(), and GUM_ERROR.

Referenced by gum::IMarkovRandomField< GUM_SCALAR >::toDot(), gum::InfluenceDiagram< GUM_SCALAR >::toDot(), and gum::IMarkovRandomField< GUM_SCALAR >::toDotAsFactorGraph().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ propertyWithDefault()

INLINE const std::string & gum::GraphicalModel::propertyWithDefault ( const std::string & name,
const std::string & byDefault ) const
inherited

Return the value of the property name of this GraphicalModel.

return byDefault if the property name is not found

Definition at line 75 of file graphicalModel_inl.h.

76 {
77 return (_propertiesMap_.exists(name)) ? _propertiesMap_[name] : byDefault;
78 }

References _propertiesMap_.

◆ reverseArc() [1/3]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::reverseArc ( const Arc & arc)

Reverses an arc while preserving the same joint distribution.

This method uses Shachter's 1986 algorithm for reversing an arc in the Bayes net while preserving the same joint distribution. By performing this reversal, we also add new arcs (required to not alter the joint distribution)

Exceptions
InvalidArcexception if the arc does not exist or if its reversal would induce a directed cycle.

Definition at line 335 of file BayesNet_tpl.h.

335 {
336 // check that the arc exists
337 if (!_varMap_.exists(arc.tail()) || !_varMap_.exists(arc.head()) || !dag().existsArc(arc)) {
338 GUM_ERROR(InvalidArc, "a non-existing arc cannot be reversed")
339 }
340
341 NodeId tail = arc.tail();
342 NodeId head = arc.head();
343
344 // check that the reversal does not induce a cycle
345 try {
346 DAG d = dag();
347 d.eraseArc(arc);
348 d.addArc(head, tail);
349 } catch (Exception const&) {
350 GUM_ERROR(InvalidArc, "this arc reversal would induce a directed cycle")
351 }
352
353 // with the same notations as Shachter (1986), "evaluating influence
354 // diagrams", p.878, we shall first compute the product of probabilities:
355 // pi_j^old (x_j | x_c^old(j) ) * pi_i^old (x_i | x_c^old(i) )
357
358 // modify the topology of the graph: add to tail all the parents of head
359 // and add to head all the parents of tail
362 for (const auto node: this->parents(tail))
363 new_parents.insert(node);
364 for (const auto node: this->parents(head))
365 new_parents.insert(node);
366 // remove arc (head, tail)
367 eraseArc(arc);
368
369 // add the necessary arcs to the tail
370 for (const auto p: new_parents) {
371 if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
372 }
373
374 addArc(head, tail);
375 // add the necessary arcs to the head
377
378 for (const auto p: new_parents) {
379 if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
380 }
381
383
384 // update the conditional distributions of head and tail
386 del_vars << &(variable(tail));
388
389 auto& cpt_head = const_cast< Tensor< GUM_SCALAR >& >(cpt(head));
391
393 auto& cpt_tail = const_cast< Tensor< GUM_SCALAR >& >(cpt(tail));
395 }

References _varMap_, gum::DAGmodel::dag(), gum::DAGmodel::existsArc(), GUM_ERROR, gum::Arc::head(), and gum::Arc::tail().

Here is the call graph for this function:

◆ reverseArc() [2/3]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::reverseArc ( const std::string & tail,
const std::string & head )
inline

Reverses an arc while preserving the same joint distribution.

This method uses Shachter's 1986 algorithm for reversing an arc in the Bayes net while preserving the same joint distribution. By performing this reversal, we also add new arcs (required to not alter the joint distribution)

Exceptions
InvalidArcexception if the arc does not exist or if its reversal would induce a directed cycle.

Definition at line 485 of file BayesNet.h.

485 {
487 }
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.

References idFromName(), and reverseArc().

Here is the call graph for this function:

◆ reverseArc() [3/3]

template<typename GUM_SCALAR>
INLINE void gum::BayesNet< GUM_SCALAR >::reverseArc ( NodeId tail,
NodeId head )

Reverses an arc while preserving the same joint distribution.

This method uses Shachter's 1986 algorithm for reversing an arc in the Bayes net while preserving the same joint distribution. By performing this reversal, we also add new arcs (required to not alter the joint distribution)

Exceptions
InvalidArcexception if the arc does not exist or if its reversal would induce a directed cycle.

Definition at line 398 of file BayesNet_tpl.h.

398 {
400 }

References reverseArc().

Referenced by reverseArc(), and reverseArc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setProperty()

INLINE void gum::GraphicalModel::setProperty ( const std::string & name,
const std::string & value )
inherited

Add or change a property of this GraphicalModel.

Definition at line 89 of file graphicalModel_inl.h.

89 {
90 if (_propertiesMap_.exists(name)) _propertiesMap_[name] = value;
91 else _propertiesMap_.insert(name, value);
92 }

References _propertiesMap_.

Referenced by gum::IBayesNet< GUM_SCALAR >::IBayesNet(), gum::IMarkovRandomField< GUM_SCALAR >::IMarkovRandomField(), and gum::InfluenceDiagram< GUM_SCALAR >::fastPrototype().

Here is the caller graph for this function:

◆ size()

INLINE Size gum::DAGmodel::size ( ) const
finalvirtualinherited

Returns the number of variables in this Directed Graphical Model.

Implements gum::GraphicalModel.

Definition at line 60 of file DAGmodel_inl.h.

60{ return dag().size(); }
Size size() const
alias for sizeNodes

References dag(), and gum::NodeGraphPart::size().

Referenced by gum::InfluenceDiagram< GUM_SCALAR >::copyStructureAndTables_(), gum::InfluenceDiagram< GUM_SCALAR >::decisionNodeSize(), hasSameStructure(), gum::MarkovBlanket::hasSameStructure(), gum::IBayesNet< GUM_SCALAR >::operator==(), gum::InfluenceDiagram< GUM_SCALAR >::operator==(), gum::prm::ClassBayesNet< GUM_SCALAR >::toDot(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::IBayesNet< GUM_SCALAR >::toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sizeArcs()

INLINE Size gum::DAGmodel::sizeArcs ( ) const
inherited

Returns the number of arcs in this Directed Graphical Model.

Definition at line 63 of file DAGmodel_inl.h.

63{ return dag_.sizeArcs(); }

References dag_.

Referenced by hasSameStructure(), gum::MarkovBlanket::hasSameStructure(), and gum::InfluenceDiagram< GUM_SCALAR >::operator==().

Here is the caller graph for this function:

◆ spaceCplxToStream()

void gum::GraphicalModel::spaceCplxToStream ( std::stringstream & s,
double dSize,
int dim,
Size usedMem )
staticinherited

send to the stream the space complexity with 3 parametrs

Parameters
sthe stream
dSizethe log10domainSize
dimthe dimension
usedMemthe memory needed for the params

Definition at line 69 of file graphicalModel.cpp.

69 {
70 if (dSize > 6) s << "domainSize: 10^" << dSize;
71 else s << "domainSize: " << std::round(std::pow(10.0, dSize));
72
73 s << ", dim: " << dim << ", mem: ";
74
75 if (const Size go = usedMem / (1024 * 1024 * 1024); go > 0) s << go << "Go ";
76 if (const Size mo = (usedMem / (1024 * 1024)) % 1024; mo > 0) s << mo << "Mo ";
77 if (const Size ko = (usedMem / 1024) % 1024; ko > 0) s << ko << "Ko ";
78 s << usedMem % 1024 << "o";
79 }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

Referenced by gum::IBayesNet< GUM_SCALAR >::toString().

Here is the caller graph for this function:

◆ toDot()

template<typename GUM_SCALAR>
std::string gum::IBayesNet< GUM_SCALAR >::toDot ( ) const
virtualinherited
Returns
Returns a dot representation of this IBayesNet.

Reimplemented in gum::BayesNetFragment< GUM_SCALAR >, gum::prm::ClassBayesNet< GUM_SCALAR >, and gum::prm::InstanceBayesNet< GUM_SCALAR >.

Definition at line 179 of file IBayesNet_tpl.h.

179 {
181 output << "digraph \"";
182
184
185 try {
186 bn_name = this->property("name");
187 } catch (NotFound const&) { bn_name = "no_name"; }
188
189 output << bn_name << "\" {" << std::endl;
190 output << " graph [bgcolor=transparent,label=\"" << bn_name << "\"];" << std::endl;
191 output << " node [style=filled fillcolor=\"#ffffaa\"];" << std::endl << std::endl;
192
193 for (auto node: nodes())
194 output << "\"" << variable(node).name() << "\" [comment=\"" << node << ":"
196
197 output << std::endl;
198
199 std::string tab = " ";
200
201 for (auto node: nodes()) {
202 if (children(node).size() > 0) {
203 for (auto child: children(node)) {
204 output << tab << "\"" << variable(node).name() << "\" -> " << "\""
205 << variable(child).name() << "\";" << std::endl;
206 }
207 } else if (parents(node).size() == 0) {
208 output << tab << "\"" << variable(node).name() << "\";" << std::endl;
209 }
210 }
211
212 output << "}" << std::endl;
213
214 return output.str();
215 }
std::string toStringWithDescription() const
string version of *this using description attribute instead of name.
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.

◆ topologicalOrder()

INLINE Sequence< NodeId > gum::DAGmodel::topologicalOrder ( ) const
inherited

The topological order stays the same as long as no variable or arcs are added or erased src the topology.

Parameters
clearIf false returns the previously created topology.

Definition at line 115 of file DAGmodel_inl.h.

115{ return dag().topologicalOrder(); }
Sequence< NodeId > topologicalOrder() const
Build and return a topological order.
Definition diGraph.cpp:111

Referenced by gum::InfluenceDiagramGenerator< GUM_SCALAR >::_checkTemporalOrder_(), gum::InfluenceDiagram< GUM_SCALAR >::decisionOrder(), and gum::InfluenceDiagram< GUM_SCALAR >::decisionOrderExists().

Here is the caller graph for this function:

◆ toString()

template<typename GUM_SCALAR>
INLINE std::string gum::IBayesNet< GUM_SCALAR >::toString ( ) const
inherited
Returns
Returns a string representation of this IBayesNet.

Definition at line 170 of file IBayesNet_tpl.h.

170 {
172 s << "BN{nodes: " << size() << ", arcs: " << dag().sizeArcs() << ", ";
174 s << "}";
175 return s.str();
176 }
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
static void spaceCplxToStream(std::stringstream &s, double dSize, int dim, Size usedMem)
send to the stream the space complexity with 3 parametrs
double log10DomainSize() const
Size memoryFootprint() const
compute the (approximated) footprint in memory of the model (the footprints of CPTs)

References gum::DAGmodel::dag(), dim(), gum::GraphicalModel::log10DomainSize(), memoryFootprint(), gum::DAGmodel::size(), and gum::GraphicalModel::spaceCplxToStream().

Referenced by gum::operator<<().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ variable() [1/2]

template<typename GUM_SCALAR>
const DiscreteVariable & gum::BayesNet< GUM_SCALAR >::variable ( const std::string & name) const
inline

Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.

Definition at line 334 of file BayesNet.h.

334 {
335 return variable(idFromName(name));
336 };

References idFromName(), and variable().

Here is the call graph for this function:

◆ variable() [2/2]

template<typename GUM_SCALAR>
INLINE const DiscreteVariable & gum::BayesNet< GUM_SCALAR >::variable ( NodeId id) const
finalvirtual

Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.

Parameters
idThe variable's id to return.
Returns
Returns a constant reference of the gum::DiscreteVariable corresponding to id in the gum::BayesNet.
Exceptions
NotFoundRaised if id does not match a a variable in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 167 of file BayesNet_tpl.h.

167 {
168 return _varMap_.get(id);
169 }

References _varMap_.

Referenced by add(), addArc(), addWeightedArc(), changeVariableLabel(), erase(), eraseArc(), generateCPT(), gum::getMaxModality(), and variable().

Here is the caller graph for this function:

◆ variableFromName()

template<typename GUM_SCALAR>
INLINE const DiscreteVariable & gum::BayesNet< GUM_SCALAR >::variableFromName ( const std::string & name) const
finalvirtual

Returns a variable given its name in the gum::BayesNet.

Parameters
nameThe variable's name in the gum::BayesNet.
Returns
Returns the gum::DiscreteVariable named name in the gum::BayesNet.
Exceptions
NotFoundRaised if name does not match a variable in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 253 of file BayesNet_tpl.h.

253 {
254 return _varMap_.variableFromName(name);
255 }

References _varMap_.

Referenced by _copyTensors_().

Here is the caller graph for this function:

◆ variableNodeMap()

template<typename GUM_SCALAR>
INLINE const VariableNodeMap & gum::BayesNet< GUM_SCALAR >::variableNodeMap ( ) const
finalvirtual

Returns a map between variables and nodes of this gum::BayesNet.

Returns
Returns a constant reference to the gum::VariableNodeMap.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 263 of file BayesNet_tpl.h.

263 {
264 return _varMap_;
265 }

References _varMap_.

◆ variables() [1/2]

INLINE VariableSet gum::GraphicalModel::variables ( const NodeSet & ids) const
inherited

transform a vector of NodeId into a VariableeSet

Returns
NodeSet

Definition at line 160 of file graphicalModel_inl.h.

160 {
161 VariableSet s;
162 const VariableNodeMap& v = variableNodeMap();
163 for (const auto& node: l) {
164 s.insert(&v.get(node));
165 }
166 return s;
167 }
Set< const DiscreteVariable * > VariableSet

References gum::VariableNodeMap::get(), gum::Set< Key >::insert(), and variableNodeMap().

Here is the call graph for this function:

◆ variables() [2/2]

INLINE VariableSet gum::GraphicalModel::variables ( const std::vector< std::string > & l) const
inherited

transform a vector of names into a VariableeSet

Returns
NodeSet

Definition at line 150 of file graphicalModel_inl.h.

150 {
151 VariableSet s;
152 const VariableNodeMap& v = variableNodeMap();
153 for (const auto& name: l) {
154 s.insert(&v.variableFromName(name));
155 }
156 return s;
157 }

References variableNodeMap().

Here is the call graph for this function:

◆ BayesNetFactory< GUM_SCALAR >

template<typename GUM_SCALAR>
friend class BayesNetFactory< GUM_SCALAR >
friend

Definition at line 714 of file BayesNet.h.

Member Data Documentation

◆ _probaMap_

template<typename GUM_SCALAR>
NodeProperty< Tensor< GUM_SCALAR >* > gum::BayesNet< GUM_SCALAR >::_probaMap_
private

Mapping between the variable's id and their CPT.

Definition at line 693 of file BayesNet.h.

Referenced by ~BayesNet(), _clearTensors_(), _copyTensors_(), _unsafeChangeTensor_(), add(), addArc(), beginTopologyTransformation(), cpt(), endTopologyTransformation(), erase(), and eraseArc().

◆ _propertiesMap_

HashTable< std::string, std::string > gum::GraphicalModel::_propertiesMap_
privateinherited

The properties of this Directed Graphical Model.

Definition at line 236 of file graphicalModel.h.

Referenced by _properties_(), operator=(), properties(), propertyWithDefault(), and setProperty().

◆ _varMap_

template<typename GUM_SCALAR>
VariableNodeMap gum::BayesNet< GUM_SCALAR >::_varMap_
private

the map between variable and id

Definition at line 690 of file BayesNet.h.

Referenced by BayesNet(), add(), changeVariableName(), erase(), erase(), eraseArc(), idFromName(), operator=(), reverseArc(), variable(), variableFromName(), and variableNodeMap().

◆ dag_


The documentation for this class was generated from the following files: