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

Portion of a BN identified by the list of nodes and a BayesNet. More...

#include <agrum/BN/BayesNetFragment.h>

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

Public Member Functions

gum::BayesNet< GUM_SCALAR > toBN () const
 create a brand new BayesNet from a fragment.
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
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
void _attachSignal_ (__sig__::ISignaler *sender)
void _detachSignal_ (__sig__::ISignaler *sender)
Constructors / Destructors
 BayesNetFragment ()=delete
 BayesNetFragment (const BayesNetFragment< GUM_SCALAR > &fragment)=delete
 BayesNetFragment (BayesNetFragment< GUM_SCALAR > &&fragment)=delete
 BayesNetFragment (const IBayesNet< GUM_SCALAR > &bn)
virtual ~BayesNetFragment ()
signals
virtual void whenNodeAdded (const void *src, NodeId id) final
 the action to take when a new node is inserted into the graph
virtual void whenNodeDeleted (const void *src, NodeId id) final
 the action to take when a node has just been removed from the graph
virtual void whenArcAdded (const void *src, NodeId from, NodeId to) final
 the action to take when a new arc is inserted into the graph
virtual void whenArcDeleted (const void *src, NodeId from, NodeId to) final
 the action to take when an arc has just been removed from the graph
IBayesNet interface
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 constant reference to the VariableNodeMap of this BN.
virtual const DiscreteVariablevariable (NodeId id) const final
 Returns a constant reference over a variabe given it's node id.
virtual const DiscreteVariablevariable (const std::string &name) const final
 Returns the CPT of a variable.
virtual NodeId nodeId (const DiscreteVariable &var) const final
 Return id node from discrete var pointer.
virtual NodeId idFromName (const std::string &name) const final
 Getter by name.
virtual const DiscreteVariablevariableFromName (const std::string &name) const final
 Getter by name.
virtual std::string toDot () const final
 creates a dot representing the whole referred BN hilighting the fragment.
API for Fragment
bool isInstalledNode (NodeId id) const
 check if a certain NodeId exists in the fragment
bool isInstalledNode (const std::string &name) const
 check if a certain NodeId exists in the fragment
void installNode (NodeId id)
 install a node referenced by its nodeId
void installNode (const std::string &name)
 check if a certain NodeId exists in the fragment
void installAscendants (NodeId id)
 install a node and all its ascendants
void installAscendants (const std::string &name)
 check if a certain NodeId exists in the fragment
void uninstallNode (NodeId id)
 uninstall a node referenced by its nodeId
void uninstallNode (const std::string &name)
 check if a certain NodeId exists in the fragment
void installMarginal (NodeId id, const Tensor< GUM_SCALAR > &pot)
 install a local marginal BY COPY for a node into the fragment.
void installMarginal (const std::string &name, const Tensor< GUM_SCALAR > &pot)
 check if a certain NodeId exists in the fragment
void installCPT (NodeId id, const Tensor< GUM_SCALAR > &pot)
 install a local cpt BY COPYfor a node into the fragment.
void installCPT (const std::string &name, const Tensor< GUM_SCALAR > &pot)
 check if a certain NodeId exists in the fragment
void uninstallCPT (NodeId id)
 uninstall a local CPT.
void uninstallCPT (const std::string &name)
 check if a certain NodeId exists in the fragment
bool checkConsistency (NodeId id) const
 returns true if the nodeId's (local or not) cpt is consistent with its parents in the fragment
bool checkConsistency (const std::string &name) const
 check if a certain NodeId exists in the fragment
bool checkConsistency () const
 returns true if all nodes in the fragment are consistent
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 void spaceCplxToStream (std::stringstream &s, double dSize, int dim, Size usedMem)
 send to the stream the space complexity with 3 parametrs

Protected Member Functions

void uninstallArc_ (NodeId from, NodeId to)
void installArc_ (NodeId from, NodeId to)
void installCPT_ (NodeId id, const Tensor< GUM_SCALAR > &pot)
void uninstallCPT_ (NodeId id)
 uninstall a local CPT.

Protected Attributes

DAG dag_
 The DAG of this Directed Graphical Model.
DiGraphgraph_
 the graph to listen to

Private Types

using Senders_list = std::vector< __sig__::ISignaler* >
 Alias for the list of signal senders.

Private Member Functions

const HashTable< std::string, std::string > & _properties_ () const
 Return the properties of this Directed Graphical Model.

Private Attributes

const IBayesNet< GUM_SCALAR > & _bn_
 The referred BayesNet.
NodeProperty< const Tensor< GUM_SCALAR > * > _localCPTs_
 Mapping between the variable's id and their CPT specific to this Fragment.
HashTable< std::string, std::string > _propertiesMap_
 The properties of this Directed Graphical Model.
Senders_list _senders_

Detailed Description

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

Portion of a BN identified by the list of nodes and a BayesNet.

Author
Pierre-Henri WUILLEMIN(_at_LIP6)

This class is a decorator of a BayesNet implementing the IBayesNet interface. CPTs can be shared with the BN or can be specific to the Fragment if different.

BayesNetFragment is a DiGraphListener in order to be synchronized (especially when removing nodes or arcs).

In a BayesNetFragment, one can install or remove nodes. An arc can be in the fragment if and only if its head and tail are installed in the fragment. *When installing a node, all the arcs that can be added in the fragment are *effectively installed (resp. *when uninstalling a node, etc.).

A BayesNetFragment can redefine tensor for node. The main reason is to be able to install a node without installing all its parents (and its ascendants). So local CPT to the node can be installed. However, it is not done automatically.

If a cpt is not locally defined, the fragment uses the cpt defined in the referred BN. The checkConsistency() method verifies that, for all installed nodes, either all the parents are installed or a local CPT is defined.

Definition at line 90 of file BayesNetFragment.h.

Member Typedef Documentation

◆ Senders_list

using gum::Listener::Senders_list = std::vector< __sig__::ISignaler* >
privateinherited

Alias for the list of signal senders.

Definition at line 96 of file listener.h.

Constructor & Destructor Documentation

◆ BayesNetFragment() [1/4]

template<typename GUM_SCALAR>
gum::BayesNetFragment< GUM_SCALAR >::BayesNetFragment ( )
delete

Referenced by BayesNetFragment(), BayesNetFragment(), BayesNetFragment(), and ~BayesNetFragment().

Here is the caller graph for this function:

◆ BayesNetFragment() [2/4]

template<typename GUM_SCALAR>
gum::BayesNetFragment< GUM_SCALAR >::BayesNetFragment ( const BayesNetFragment< GUM_SCALAR > & fragment)
delete

References BayesNetFragment().

Here is the call graph for this function:

◆ BayesNetFragment() [3/4]

template<typename GUM_SCALAR>
gum::BayesNetFragment< GUM_SCALAR >::BayesNetFragment ( BayesNetFragment< GUM_SCALAR > && fragment)
delete

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

Here is the call graph for this function:

◆ BayesNetFragment() [4/4]

template<typename GUM_SCALAR>
gum::BayesNetFragment< GUM_SCALAR >::BayesNetFragment ( const IBayesNet< GUM_SCALAR > & bn)
explicit

Definition at line 55 of file BayesNetFragment_tpl.h.

55 :
58 }
Portion of a BN identified by the list of nodes and a BayesNet.
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
DiGraphListener(const DiGraph *g)
default constructor

References BayesNetFragment(), gum::DiGraphListener::DiGraphListener(), gum::IBayesNet< GUM_SCALAR >::IBayesNet(), _bn_, and gum::DAGmodel::dag().

Here is the call graph for this function:

◆ ~BayesNetFragment()

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

Definition at line 61 of file BayesNetFragment_tpl.h.

61 {
63
64 for (auto node: nodes())
65 if (_localCPTs_.exists(node)) uninstallCPT_(node);
66 }
NodeProperty< const Tensor< GUM_SCALAR > * > _localCPTs_
Mapping between the variable's id and their CPT specific to this Fragment.
void uninstallCPT_(NodeId id)
uninstall a local CPT.
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.

References BayesNetFragment(), _localCPTs_, gum::DAGmodel::nodes(), and uninstallCPT_().

Here is the call graph for this function:

Member Function Documentation

◆ _attachSignal_()

INLINE void gum::Listener::_attachSignal_ ( __sig__::ISignaler * sender)
inherited

Definition at line 56 of file listener_inl.h.

56{ _senders_.push_back(sender); }
Senders_list _senders_
Definition listener.h:113

References _senders_.

◆ _detachSignal_()

INLINE void gum::Listener::_detachSignal_ ( __sig__::ISignaler * sender)
inherited

Definition at line 58 of file listener_inl.h.

58 {
59 auto del = std::remove(_senders_.begin(), _senders_.end(), sender);
60
61 if (del != _senders_.end()) _senders_.erase(del, _senders_.end());
62 }

References _senders_.

◆ _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:

◆ 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(); }
DAG dag_
The DAG of this Directed Graphical Model.
Definition DAGmodel.h:272

References dag_.

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

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.
const std::string & name() const
returns the name of the variable

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

Here is the call graph for this function:

◆ checkConsistency() [1/3]

template<typename GUM_SCALAR>
INLINE bool gum::BayesNetFragment< GUM_SCALAR >::checkConsistency ( ) const

returns true if all nodes in the fragment are consistent

Exceptions
gum::OperatioNotAllowedif the fragment is not consistent.

Definition at line 295 of file BayesNetFragment_tpl.h.

295 {
296 for (auto node: nodes())
297 if (!checkConsistency(node)) return false;
298
299 return true;
300 }
bool checkConsistency() const
returns true if all nodes in the fragment are consistent

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

Referenced by checkConsistency().

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

◆ checkConsistency() [2/3]

template<typename GUM_SCALAR>
bool gum::BayesNetFragment< GUM_SCALAR >::checkConsistency ( const std::string & name) const
inline

check if a certain NodeId exists in the fragment

Definition at line 289 of file BayesNetFragment.h.

289 {
291 }
virtual NodeId idFromName(const std::string &name) const final
Getter by name.

References checkConsistency(), and idFromName().

Here is the call graph for this function:

◆ checkConsistency() [3/3]

template<typename GUM_SCALAR>
bool gum::BayesNetFragment< GUM_SCALAR >::checkConsistency ( NodeId id) const

returns true if the nodeId's (local or not) cpt is consistent with its parents in the fragment

Exceptions
NotFoundif the id is not in the fragment

Definition at line 280 of file BayesNetFragment_tpl.h.

280 {
281 if (!isInstalledNode(id))
282 GUM_ERROR(NotFound, "The node " << id << " is not part of this fragment")
283
284 const auto& cpt = this->cpt(id);
286
287 for (Idx i = 1; i < cpt.nbrDim(); i++) {
288 cpt_parents.insert(_bn_.idFromName(cpt.variable(i).name()));
289 }
290
291 return (this->parents(id) == cpt_parents);
292 }
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _bn_, cpt(), GUM_ERROR, gum::Set< Key >::insert(), isInstalledNode(), and gum::DAGmodel::parents().

Referenced by checkConsistency(), toBN(), and toDot().

Here is the call graph for this function:
Here is the caller 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:

◆ 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:

◆ cpt() [1/2]

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

Returns the CPT of a variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 146 of file BayesNetFragment.h.

146 {
147 return cpt(idFromName(name));
148 };

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::BayesNetFragment< GUM_SCALAR >::cpt ( NodeId varId) const
finalvirtual

Returns the CPT of a variable.

Exceptions
NotFoundIf no variable's id matches varId.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 96 of file BayesNetFragment_tpl.h.

96 {
97 if (!isInstalledNode(id)) GUM_ERROR(NotFound, "NodeId " << id << " is not installed")
98
100 else return _bn_.cpt(id);
101 }
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.

References _bn_, _localCPTs_, GUM_ERROR, and isInstalledNode().

Referenced by checkConsistency(), cpt(), toBN(), uninstallCPT(), and gum::ImportanceSampling< GUM_SCALAR >::unsharpenBN_().

Here is the call graph for this function:
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:

◆ 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 }

◆ 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 }
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel

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:

◆ 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::BayesNetFragment< GUM_SCALAR >::idFromName ( const std::string & name) const
finalvirtual

Getter by name.

Exceptions
NotFoundif no such name exists in the graph.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 125 of file BayesNetFragment_tpl.h.

125 {
126 NodeId id = _bn_.idFromName(name);
127
128 if (!isInstalledNode(id)) GUM_ERROR(NotFound, "variable " << name << " is not installed")
129
130 return id;
131 }

References _bn_, GUM_ERROR, and isInstalledNode().

Referenced by checkConsistency(), cpt(), isInstalledNode(), uninstallCPT(), uninstallNode(), variable(), and variableFromName().

Here is the call graph for this function:
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:

◆ installArc_()

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::installArc_ ( NodeId from,
NodeId to )
protected

Definition at line 192 of file BayesNetFragment_tpl.h.

192 {
193 this->dag_.addArc(from, to);
194 }

References gum::DAGmodel::dag_.

Referenced by installCPT_(), and uninstallCPT().

Here is the caller graph for this function:

◆ installAscendants() [1/2]

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

check if a certain NodeId exists in the fragment

Definition at line 225 of file BayesNetFragment.h.

225{ installAscendants(_bn_.idFromName(name)); }
void installAscendants(NodeId id)
install a node and all its ascendants

References _bn_, and installAscendants().

Referenced by installAscendants().

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

◆ installAscendants() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::installAscendants ( NodeId id)

install a node and all its ascendants

Exceptions
NotFoundif the node does not exist in the referred BN
Warning
nothing happens if the node is already installed

Definition at line 170 of file BayesNetFragment_tpl.h.

170 {
171 installNode(id);
172
173 // bn is a dag => this will have an end ...
174 for (auto pa: this->_bn_.parents(id))
176 }
void installNode(NodeId id)
install a node referenced by its nodeId

References _bn_, installAscendants(), and installNode().

Referenced by installAscendants().

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

◆ installCPT() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNetFragment< GUM_SCALAR >::installCPT ( const std::string & name,
const Tensor< GUM_SCALAR > & pot )
inline

check if a certain NodeId exists in the fragment

Definition at line 266 of file BayesNetFragment.h.

266 {
267 installCPT(_bn_.idFromName(name), pot);
268 };
void installCPT(NodeId id, const Tensor< GUM_SCALAR > &pot)
install a local cpt BY COPYfor a node into the fragment.

References _bn_, and installCPT().

Here is the call graph for this function:

◆ installCPT() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNetFragment< GUM_SCALAR >::installCPT ( NodeId id,
const Tensor< GUM_SCALAR > & pot )

install a local cpt BY COPYfor a node into the fragment.

This function will change the arcs from the parents to the node in order to be consistent with the new local tensor.

Parameters
idthe nodeId
potthe tensor to be copied
Exceptions
NotFoundif the id is not in the fragment
OperationNotAllowedif the tensor is not compliant with the variable or if a variable in the CPT is not a parent in the referred bn.

Definition at line 217 of file BayesNetFragment_tpl.h.

217 {
218 if (!dag().existsNode(id))
219 GUM_ERROR(NotFound, "Node " << id << " is not installed in the fragment")
220
221 if (&(pot.variable(0)) != &(variable(id))) {
223 "The tensor is not a marginal for _bn_.variable <" << variable(id).name() << ">")
224 }
225
226 const NodeSet& parents = _bn_.parents(id);
227
228 for (Idx i = 1; i < pot.nbrDim(); i++) {
229 if (!parents.contains(_bn_.idFromName(pot.variable(i).name())))
231 "Variable <" << pot.variable(i).name() << "> is not in the parents of node "
232 << id)
233 }
234
235 installCPT_(id, pot);
236 }
void installCPT_(NodeId id, const Tensor< GUM_SCALAR > &pot)
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it's node id.

References _bn_, gum::DAGmodel::dag(), GUM_ERROR, installCPT_(), gum::DAGmodel::parents(), and variable().

Referenced by installCPT(), gum::ImportanceSampling< GUM_SCALAR >::onContextualize_(), and gum::ImportanceSampling< GUM_SCALAR >::unsharpenBN_().

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

◆ installCPT_()

template<typename GUM_SCALAR>
void gum::BayesNetFragment< GUM_SCALAR >::installCPT_ ( NodeId id,
const Tensor< GUM_SCALAR > & pot )
protected

Definition at line 197 of file BayesNetFragment_tpl.h.

197 {
198 // topology
199 const auto& parents = this->parents(id);
200 for (auto node_it = parents.beginSafe(); node_it != parents.endSafe();
201 ++node_it) // safe iterator needed here
203
204 for (Idx i = 1; i < pot.nbrDim(); i++) {
205 NodeId parent = _bn_.idFromName(pot.variable(i).name());
206
208 }
209
210 // local cpt
211 if (_localCPTs_.exists(id)) uninstallCPT_(id);
212
214 }
void installArc_(NodeId from, NodeId to)
void uninstallArc_(NodeId from, NodeId to)

References _bn_, _localCPTs_, installArc_(), isInstalledNode(), gum::DAGmodel::parents(), uninstallArc_(), and uninstallCPT_().

Referenced by installCPT(), and installMarginal().

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

◆ installMarginal() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNetFragment< GUM_SCALAR >::installMarginal ( const std::string & name,
const Tensor< GUM_SCALAR > & pot )
inline

check if a certain NodeId exists in the fragment

Definition at line 248 of file BayesNetFragment.h.

248 {
249 installMarginal(_bn_.idFromName(name), pot);
250 }
void installMarginal(NodeId id, const Tensor< GUM_SCALAR > &pot)
install a local marginal BY COPY for a node into the fragment.

References _bn_, and installMarginal().

Here is the call graph for this function:

◆ installMarginal() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNetFragment< GUM_SCALAR >::installMarginal ( NodeId id,
const Tensor< GUM_SCALAR > & pot )

install a local marginal BY COPY for a node into the fragment.

This function will remove all the arcs from the parents to the node.

Parameters
idthe nodeId
potthe tensor
Exceptions
NotFoundif the id is not in the fragment
OperationNotAllowedif the tensor is not compliant with the variable (or is not a marginal)

Definition at line 261 of file BayesNetFragment_tpl.h.

261 {
262 if (!isInstalledNode(id)) {
263 GUM_ERROR(NotFound, "The node " << id << " is not part of this fragment")
264 }
265
266 if (pot.nbrDim() > 1) {
267 GUM_ERROR(OperationNotAllowed, "The tensor is not a marginal :" << pot)
268 }
269
270 if (&(pot.variable(0)) != &(_bn_.variable(id))) {
272 "The tensor is not a marginal for _bn_.variable <" << _bn_.variable(id).name()
273 << ">")
274 }
275
276 installCPT_(id, pot);
277 }

References _bn_, GUM_ERROR, installCPT_(), and isInstalledNode().

Referenced by installMarginal().

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

◆ installNode() [1/2]

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

check if a certain NodeId exists in the fragment

Definition at line 215 of file BayesNetFragment.h.

215{ installNode(_bn_.idFromName(name)); }

References _bn_, and installNode().

Referenced by installNode().

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

◆ installNode() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNetFragment< GUM_SCALAR >::installNode ( NodeId id)

install a node referenced by its nodeId

Exceptions
NotFoundif the node does not exist in the referred BN
Warning
nothing happens if the node is already installed

Definition at line 151 of file BayesNetFragment_tpl.h.

151 {
152 if (!_bn_.dag().existsNode(id))
153 GUM_ERROR(NotFound, "Node " << id << " does not exist in referred BayesNet")
154
155 if (!isInstalledNode(id)) {
156 this->dag_.addNodeWithId(id);
157
158 // adding arcs with id as a tail
159 for (auto pa: this->_bn_.parents(id)) {
160 if (isInstalledNode(pa)) this->dag_.addArc(pa, id);
161 }
162
163 // adding arcs with id as a head
164 for (auto son: this->_bn_.children(id))
165 if (isInstalledNode(son)) this->dag_.addArc(id, son);
166 }
167 }

References _bn_, gum::DAGmodel::dag_, GUM_ERROR, and isInstalledNode().

Referenced by installAscendants().

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:

◆ isInstalledNode() [1/2]

template<typename GUM_SCALAR>
bool gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode ( const std::string & name) const
inline

check if a certain NodeId exists in the fragment

Definition at line 203 of file BayesNetFragment.h.

203 {
205 };

References idFromName(), and isInstalledNode().

Here is the call graph for this function:

◆ isInstalledNode() [2/2]

template<typename GUM_SCALAR>
INLINE bool gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode ( NodeId id) const

check if a certain NodeId exists in the fragment

Definition at line 146 of file BayesNetFragment_tpl.h.

146 {
147 return dag().existsNode(id);
148 }
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId

References gum::DAGmodel::dag().

Referenced by checkConsistency(), cpt(), idFromName(), installCPT_(), installMarginal(), installNode(), isInstalledNode(), nodeId(), toDot(), uninstallCPT(), uninstallNode(), variable(), and variableFromName().

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::BayesNetFragment< GUM_SCALAR >::nodeId ( const DiscreteVariable & var) const
finalvirtual

Return id node from discrete var pointer.

Exceptions
NotFoundIf no variable match var.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 116 of file BayesNetFragment_tpl.h.

116 {
117 NodeId id = _bn_.nodeId(var);
118
119 if (!isInstalledNode(id)) GUM_ERROR(NotFound, "variable " << var.name() << " is not installed")
120
121 return id;
122 }

References _bn_, GUM_ERROR, isInstalledNode(), and gum::Variable::name().

Here is the call graph for this function:

◆ 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>
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_.

◆ 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:

◆ toBN()

template<typename GUM_SCALAR>
gum::BayesNet< GUM_SCALAR > gum::BayesNetFragment< GUM_SCALAR >::toBN ( ) const

create a brand new BayesNet from a fragment.

Returns
the new BayesNet<GUM_SCALAR>

Definition at line 370 of file BayesNetFragment_tpl.h.

370 {
371 if (!checkConsistency()) {
372 GUM_ERROR(OperationNotAllowed, "The fragment contains un-consistent node(s)")
373 }
375 for (const auto nod: nodes()) {
376 res.add(variable(nod), nod);
377 }
378 for (const auto& arc: dag().arcs()) {
379 res.addArc(arc.tail(), arc.head());
380 }
381 for (const auto nod: nodes()) {
382 res.cpt(nod).fillWith(cpt(nod));
383 }
384
385 return res;
386 }

References gum::BayesNet< GUM_SCALAR >::add(), gum::BayesNet< GUM_SCALAR >::addArc(), gum::DAGmodel::arcs(), checkConsistency(), gum::BayesNet< GUM_SCALAR >::cpt(), cpt(), gum::DAGmodel::dag(), GUM_ERROR, gum::DAGmodel::nodes(), and variable().

Here is the call graph for this function:

◆ toDot()

template<typename GUM_SCALAR>
std::string gum::BayesNetFragment< GUM_SCALAR >::toDot ( ) const
finalvirtual

creates a dot representing the whole referred BN hilighting the fragment.

Returns
Returns a dot representation of this fragment

Reimplemented from gum::IBayesNet< GUM_SCALAR >.

Definition at line 303 of file BayesNetFragment_tpl.h.

303 {
305 output << "digraph \"";
306
308
309 static std::string inFragmentStyle = "fillcolor=\"#ffffaa\","
310 "color=\"#000000\","
311 "fontcolor=\"#000000\"";
312 static std::string styleWithLocalCPT = "fillcolor=\"#ffddaa\","
313 "color=\"#000000\","
314 "fontcolor=\"#000000\"";
315 static std::string notConsistantStyle = "fillcolor=\"#ff0000\","
316 "color=\"#000000\","
317 "fontcolor=\"#ffff00\"";
318 static std::string outFragmentStyle = "fillcolor=\"#f0f0f0\","
319 "color=\"#f0f0f0\","
320 "fontcolor=\"#000000\"";
321
322 try {
323 bn_name = _bn_.property("name");
324 } catch (NotFound const&) { bn_name = "no_name"; }
325
326 bn_name = "Fragment of " + bn_name;
327
328 output << bn_name << "\" {" << std::endl;
329 output << " graph [bgcolor=transparent,label=\"" << bn_name << "\"];" << std::endl;
330 output << " node [style=filled];" << std::endl << std::endl;
331
332 for (auto node: _bn_.nodes()) {
333 output << "\"" << _bn_.variable(node).name() << "\" [comment=\"" << node << ":"
334 << _bn_.variable(node) << ", \"";
335
336 if (isInstalledNode(node)) {
337 if (!checkConsistency(node)) {
339 } else if (_localCPTs_.exists(node)) output << styleWithLocalCPT;
340 else output << inFragmentStyle;
341 } else output << outFragmentStyle;
342
343 output << "];" << std::endl;
344 }
345
346 output << std::endl;
347
348 std::string tab = " ";
349
350 for (auto node: _bn_.nodes()) {
351 if (_bn_.children(node).size() > 0) {
352 for (auto child: _bn_.children(node)) {
353 output << tab << "\"" << _bn_.variable(node).name() << "\" -> "
354 << "\"" << _bn_.variable(child).name() << "\" [";
355
357 else output << outFragmentStyle;
358
359 output << "];" << std::endl;
360 }
361 }
362 }
363
364 output << "}" << std::endl;
365
366 return output.str();
367 }

References _bn_, _localCPTs_, checkConsistency(), gum::DAGmodel::dag(), gum::DAGmodel::existsArc(), and isInstalledNode().

Here is the call graph for this function:

◆ 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:

◆ uninstallArc_()

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::uninstallArc_ ( NodeId from,
NodeId to )
protected

Definition at line 187 of file BayesNetFragment_tpl.h.

187 {
188 this->dag_.eraseArc(Arc(from, to));
189 }

References gum::DAGmodel::dag_.

Referenced by installCPT_(), and whenArcDeleted().

Here is the caller graph for this function:

◆ uninstallCPT() [1/2]

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

check if a certain NodeId exists in the fragment

Definition at line 279 of file BayesNetFragment.h.

void uninstallCPT(NodeId id)
uninstall a local CPT.

References idFromName(), and uninstallCPT().

Referenced by uninstallCPT().

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

◆ uninstallCPT() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT ( NodeId id)

uninstall a local CPT.

Warning
Nothing happens if no local CPT for this nodeId or if the node is not installed.

Definition at line 245 of file BayesNetFragment_tpl.h.

245 {
246 if (_localCPTs_.exists(id)) {
247 uninstallCPT_(id);
248
249 // re-create arcs from referred tensor
250 const Tensor< GUM_SCALAR >& pot = cpt(id);
251
252 for (Idx i = 1; i < pot.nbrDim(); i++) {
253 NodeId parent = _bn_.idFromName(pot.variable(i).name());
254
256 }
257 }
258 }

References _bn_, _localCPTs_, cpt(), installArc_(), isInstalledNode(), and uninstallCPT_().

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

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

◆ uninstallCPT_()

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT_ ( NodeId id)
protected

uninstall a local CPT.

Does nothing if no local CPT for this nodeId No check. No change in the topology. Checks are made in public methods.

Definition at line 239 of file BayesNetFragment_tpl.h.

239 {
240 delete _localCPTs_[id];
241 _localCPTs_.erase(id);
242 }

References _localCPTs_.

Referenced by ~BayesNetFragment(), installCPT_(), and uninstallCPT().

Here is the caller graph for this function:

◆ uninstallNode() [1/2]

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

check if a certain NodeId exists in the fragment

Definition at line 234 of file BayesNetFragment.h.

void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId

References idFromName(), and uninstallNode().

Referenced by uninstallNode().

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

◆ uninstallNode() [2/2]

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::uninstallNode ( NodeId id)

uninstall a node referenced by its nodeId

Warning
nothing happens if the node is not installed

Definition at line 179 of file BayesNetFragment_tpl.h.

179 {
180 if (isInstalledNode(id)) {
181 uninstallCPT(id);
182 this->dag_.eraseNode(id);
183 }
184 }

References gum::DAGmodel::dag_, isInstalledNode(), and uninstallCPT().

Referenced by whenNodeDeleted().

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

◆ variable() [1/2]

template<typename GUM_SCALAR>
virtual const DiscreteVariable & gum::BayesNetFragment< GUM_SCALAR >::variable ( const std::string & name) const
inlinefinalvirtual

Returns the CPT of a variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 162 of file BayesNetFragment.h.

162 {
163 return variable(idFromName(name));
164 };

References idFromName(), and variable().

Here is the call graph for this function:

◆ variable() [2/2]

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

Returns a constant reference over a variabe given it's node id.

Exceptions
NotFoundIf no variable's id matches varId.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 109 of file BayesNetFragment_tpl.h.

109 {
110 if (!isInstalledNode(id)) GUM_ERROR(NotFound, "NodeId " << id << " is not installed")
111
112 return _bn_.variable(id);
113 }

References _bn_, GUM_ERROR, and isInstalledNode().

Referenced by installCPT(), toBN(), and variable().

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

◆ variableFromName()

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

Getter by name.

Exceptions
NotFoundif no such name exists in the graph.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 135 of file BayesNetFragment_tpl.h.

135 {
136 NodeId id = idFromName(name);
137
138 if (!isInstalledNode(id)) GUM_ERROR(NotFound, "variable " << name << " is not installed")
139
140 return _bn_.variable(id);
141 }

References _bn_, GUM_ERROR, idFromName(), and isInstalledNode().

Here is the call graph for this function:

◆ variableNodeMap()

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

Returns a constant reference to the VariableNodeMap of this BN.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 104 of file BayesNetFragment_tpl.h.

104 {
105 return this->_bn_.variableNodeMap();
106 }

References _bn_.

◆ 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:

◆ whenArcAdded()

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::whenArcAdded ( const void * src,
NodeId from,
NodeId to )
finalvirtual

the action to take when a new arc is inserted into the graph

Parameters
srcthe object that sent the signal
fromthe id of tail of the new arc inserted into the graph
tothe id of head of the new arc inserted into the graph

Implements gum::DiGraphListener.

Definition at line 82 of file BayesNetFragment_tpl.h.

82 {
83 // nothing to do
84 }

◆ whenArcDeleted()

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::whenArcDeleted ( const void * src,
NodeId from,
NodeId to )
finalvirtual

the action to take when an arc has just been removed from the graph

Parameters
srcthe object that sent the signal
fromthe id of tail of the arc removed from the graph
tothe id of head of the arc removed from the graph

Implements gum::DiGraphListener.

Definition at line 88 of file BayesNetFragment_tpl.h.

88 {
90 }

References gum::DAGmodel::dag(), gum::DAGmodel::existsArc(), and uninstallArc_().

Here is the call graph for this function:

◆ whenNodeAdded()

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::whenNodeAdded ( const void * src,
NodeId id )
finalvirtual

the action to take when a new node is inserted into the graph

Parameters
srcthe object that sent the signal
idthe id of the new node inserted into the graph

Implements gum::DiGraphListener.

Definition at line 71 of file BayesNetFragment_tpl.h.

71 {
72 // nothing to do
73 }

◆ whenNodeDeleted()

template<typename GUM_SCALAR>
INLINE void gum::BayesNetFragment< GUM_SCALAR >::whenNodeDeleted ( const void * src,
NodeId id )
finalvirtual

the action to take when a node has just been removed from the graph

Parameters
srcthe object that sent the signal
idthe id of the node has just been removed from the graph

Implements gum::DiGraphListener.

Definition at line 76 of file BayesNetFragment_tpl.h.

76 {
77 uninstallNode(id);
78 }

References uninstallNode().

Here is the call graph for this function:

Member Data Documentation

◆ _bn_

◆ _localCPTs_

template<typename GUM_SCALAR>
NodeProperty< const Tensor< GUM_SCALAR >* > gum::BayesNetFragment< GUM_SCALAR >::_localCPTs_
private

Mapping between the variable's id and their CPT specific to this Fragment.

Definition at line 96 of file BayesNetFragment.h.

Referenced by ~BayesNetFragment(), cpt(), installCPT_(), toDot(), uninstallCPT(), and uninstallCPT_().

◆ _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().

◆ _senders_

Senders_list gum::Listener::_senders_
privateinherited

Definition at line 113 of file listener.h.

Referenced by Listener(), ~Listener(), _attachSignal_(), and _detachSignal_().

◆ dag_

◆ graph_

DiGraph* gum::DiGraphListener::graph_
protectedinherited

the graph to listen to

Definition at line 117 of file diGraphListener.h.

Referenced by DiGraphListener().


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