aGrUM  0.20.3
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. More...
 
bool operator== (const IBayesNet< GUM_SCALAR > &from) const
 This operator compares 2 BNs ! More...
 
bool operator!= (const IBayesNet< GUM_SCALAR > &from) const
 
Size dim () const
 Returns the dimension (the number of free parameters) in this bayes net. More...
 
Size maxVarDomainSize () const
 
GUM_SCALAR minParam () const
 
GUM_SCALAR maxParam () const
 
GUM_SCALAR minNonZeroParam () const
 
GUM_SCALAR maxNonOneParam () const
 
std::string toString () const
 
NodeSet minimalCondSet (NodeId target, const NodeSet &soids) const
 
NodeSet minimalCondSet (const NodeSet &targets, const NodeSet &soids) const
 
bool hasSameStructure (const DAGmodel &other)
 
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 More...
 
virtual void whenNodeDeleted (const void *src, NodeId id) final
 the action to take when a node has just been removed from the graph More...
 
virtual void whenArcAdded (const void *src, NodeId from, NodeId to) final
 the action to take when a new arc is inserted into the graph More...
 
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 More...
 
IBayesNet interface
const Potential< GUM_SCALAR > & cpt (NodeId varId) const final
 Returns the CPT of a variable. More...
 
const Potential< GUM_SCALAR > & cpt (const std::string &name) const
 Returns the CPT of a variable. More...
 
const VariableNodeMapvariableNodeMap () const final
 Returns a constant reference to the VariableNodeMap of this BN. More...
 
virtual const DiscreteVariablevariable (NodeId id) const final
 Returns a constant reference over a variabe given it's node id. More...
 
virtual const DiscreteVariablevariable (const std::string &name) const final
 Returns the CPT of a variable. More...
 
virtual NodeId nodeId (const DiscreteVariable &var) const final
 Return id node from discrete var pointer. More...
 
virtual NodeId idFromName (const std::string &name) const final
 Getter by name. More...
 
virtual const DiscreteVariablevariableFromName (const std::string &name) const final
 Getter by name. More...
 
virtual std::string toDot () const final
 creates a dot representing the whole referred BN hilighting the fragment. More...
 
API for Fragment
bool isInstalledNode (NodeId id) const
 check if a certain NodeId exists in the fragment More...
 
bool isInstalledNode (const std::string &name) const
 check if a certain NodeId exists in the fragment More...
 
void installNode (NodeId id)
 install a node referenced by its nodeId More...
 
void installNode (const std::string &name)
 check if a certain NodeId exists in the fragment More...
 
void installAscendants (NodeId id)
 install a node and all its ascendants More...
 
void installAscendants (const std::string &name)
 check if a certain NodeId exists in the fragment More...
 
void uninstallNode (NodeId id)
 uninstall a node referenced by its nodeId More...
 
void uninstallNode (const std::string &name)
 check if a certain NodeId exists in the fragment More...
 
void installMarginal (NodeId id, const Potential< GUM_SCALAR > &pot)
 install a local marginal BY COPY for a node into the fragment. More...
 
void installMarginal (const std::string &name, const Potential< GUM_SCALAR > &pot)
 check if a certain NodeId exists in the fragment More...
 
void installCPT (NodeId id, const Potential< GUM_SCALAR > &pot)
 install a local cpt BY COPYfor a node into the fragment. More...
 
void installCPT (const std::string &name, const Potential< GUM_SCALAR > &pot)
 check if a certain NodeId exists in the fragment More...
 
void uninstallCPT (NodeId id)
 uninstall a local CPT. More...
 
void uninstallCPT (const std::string &name)
 check if a certain NodeId exists in the fragment More...
 
bool checkConsistency (NodeId id) const
 returns true if the nodeId's (local or not) cpt is consistent with its parents in the fragment More...
 
bool checkConsistency (const std::string &name) const
 check if a certain NodeId exists in the fragment More...
 
bool checkConsistency () const
 returns true if all nodes in the fragment are consistent More...
 
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) More...
 
GUM_SCALAR log2JointProbability (const Instantiation &i) const
 Compute a parameter of the log joint probability for the BN (given an instantiation of the vars) More...
 
Variable manipulation methods.
const DAGdag () const
 Returns a constant reference to the dag of this Bayes Net. More...
 
virtual Size size () const final
 Returns the number of variables in this Directed Graphical Model. More...
 
Size sizeArcs () const
 Returns the number of arcs in this Directed Graphical Model. More...
 
const NodeGraphPartnodes () const final
 Returns a constant reference to the dag of this Bayes Net. More...
 
bool exists (NodeId node) const final
 Return true if this node exists in this graphical model. More...
 
Variable manipulation methods.
bool exists (const std::string &name) const
 Return true if this graphical model is empty. More...
 
virtual bool empty () const
 Return true if this graphical model is empty. More...
 
std::vector< std::string > names (const std::vector< NodeId > &ids) const
 transform a vector of NodeId in a vector of names More...
 
std::vector< std::string > names (const NodeSet &ids) const
 transform a NodeSet in a vector of names More...
 
std::vector< NodeIdids (const std::vector< std::string > &names) const
 transform a vector of names into a vector of nodeId More...
 
NodeSet nodeset (const std::vector< std::string > &names) const
 transform a vector of names into a NodeSet More...
 
Instantiation completeInstantiation () const
 Get an instantiation over all the variables of the model. More...
 
Arc manipulation methods.
const ArcSetarcs () const
 return true if the arc tail->head exists in the DAGmodel More...
 
bool existsArc (const NodeId tail, const NodeId head) const
 return true if the arc tail->head exists in the DAGmodel More...
 
bool existsArc (const std::string &nametail, const std::string &namehead) const
 return true if the arc tail->head exists in the DAGmodel More...
 
const NodeSetparents (const NodeId id) const
 returns the set of nodes with arc ingoing to a given node More...
 
const NodeSetparents (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet parents (const NodeSet &ids) const
 returns the parents of a set of nodes More...
 
NodeSet parents (const std::vector< std::string > &names) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet family (const NodeId id) const
 returns the parents of a node and the node More...
 
NodeSet family (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
const NodeSetchildren (const NodeId id) const
 returns the set of nodes with arc outgoing from a given node More...
 
const NodeSetchildren (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet children (const NodeSet &ids) const
 returns the children of a set of nodes More...
 
NodeSet children (const std::vector< std::string > &names) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet descendants (const NodeId id) const
 returns the set of nodes with directed path outgoing from a given node More...
 
NodeSet descendants (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet ancestors (const NodeId id) const
 returns the set of nodes with directed path ingoing to a given node More...
 
NodeSet ancestors (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
Graphical methods
UndiGraph moralizedAncestralGraph (const NodeSet &nodes) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes More...
 
UndiGraph moralizedAncestralGraph (const std::vector< std::string > &nodenames) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes More...
 
bool isIndependent (NodeId X, NodeId Y, const NodeSet &Z) const final
 check if node X and node Y are independent given nodes Z More...
 
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 More...
 
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 More...
 
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 More...
 
const UndiGraphmoralGraph (bool clear=true) const
 The node's id are coherent with the variables and nodes of the topology. More...
 
const Sequence< NodeId > & topologicalOrder (bool clear=true) const
 The topological order stays the same as long as no variable or arcs are added or erased src the topology. More...
 
Getter and setters
const std::string & property (const std::string &name) const
 Return the value of the property name of this GraphicalModel. More...
 
const std::string & propertyWithDefault (const std::string &name, const std::string &byDefault) const
 Return the value of the property name of this GraphicalModel. More...
 
void setProperty (const std::string &name, const std::string &value)
 Add or change a property of this GraphicalModel. More...
 

Protected Attributes

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

Protected Member Functions

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

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()

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 potential 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 72 of file BayesNetFragment.h.

Constructor & Destructor Documentation

◆ BayesNetFragment() [1/4]

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

◆ BayesNetFragment() [2/4]

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

◆ BayesNetFragment() [3/4]

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

◆ BayesNetFragment() [4/4]

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

Definition at line 34 of file BayesNetFragment_tpl.h.

34  :
35  DiGraphListener(&bn.dag()), _bn_(bn) {
36  GUM_CONSTRUCTOR(BayesNetFragment);
37  }
DiGraphListener(const DiGraph *g)
default constructor
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.

◆ ~BayesNetFragment()

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

Definition at line 40 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

40  {
41  GUM_DESTRUCTOR(BayesNetFragment);
42 
43  for (auto node: nodes())
44  if (_localCPTs_.exists(node)) uninstallCPT_(node);
45  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
void uninstallCPT_(NodeId id)
uninstall a local CPT.
NodeProperty< const Potential< GUM_SCALAR > *> _localCPTs_
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
+ Here is the call graph for this function:

Member Function Documentation

◆ _attachSignal_()

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

Definition at line 35 of file listener_inl.h.

References gum::Set< Key, Alloc >::emplace().

35 { _senders_.push_back(sender); }
Senders_list _senders_
Definition: listener.h:92
+ Here is the call graph for this function:

◆ _detachSignal_()

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

Definition at line 37 of file listener_inl.h.

References gum::Set< Key, Alloc >::emplace().

37  {
38  auto del = std::remove(_senders_.begin(), _senders_.end(), sender);
39 
40  if (del != _senders_.end()) _senders_.erase(del, _senders_.end());
41  }
Senders_list _senders_
Definition: listener.h:92
+ Here is the call 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 96 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

96 { return dag().ancestors(id); }
NodeSet ancestors(NodeId id) const
returns the set of nodes with directed path ingoing to a given node
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call 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 98 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

98  {
99  return ancestors(idFromName(name));
100  }
NodeSet ancestors(const NodeId id) const
returns the set of nodes with directed path ingoing to a given node
Definition: DAGmodel_inl.h:96
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ 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 43 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

43 { return dag_.arcs(); }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
+ Here is the call graph for this function:

◆ checkConsistency() [1/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 260 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

260  {
261  if (!isInstalledNode(id))
262  GUM_ERROR(NotFound, "The node " << id << " is not part of this fragment")
263 
264  const auto& cpt = this->cpt(id);
265  NodeSet cpt_parents;
266 
267  for (Idx i = 1; i < cpt.nbrDim(); i++) {
268  cpt_parents.insert(_bn_.idFromName(cpt.variable(i).name()));
269  }
270 
271  return (this->parents(id) == cpt_parents);
272  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 262 of file BayesNetFragment.h.

262  {
263  return checkConsistency(idFromName(name));
264  }
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
bool checkConsistency() const
returns true if all nodes in the fragment are consistent

◆ checkConsistency() [3/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 275 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

275  {
276  for (auto node: nodes())
277  if (!checkConsistency(node)) return false;
278 
279  return true;
280  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
+ 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 65 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

65 { return dag_.children(id); }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ children() [2/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 66 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

66  {
67  return dag_.children(idFromName(name));
68  }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ children() [3/4]

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

returns the children of a set of nodes

Definition at line 70 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

70 { 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
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ 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 72 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

72  {
73  return children(nodeset(names));
74  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:65
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
+ 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 84 of file graphicalModel_inl.h.

84  {
85  Instantiation I;
86 
87  for (const auto node: nodes())
88  I << variable(node);
89 
90  return I;
91  }
virtual const NodeGraphPart & nodes() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.

◆ cpt() [1/2]

template<typename GUM_SCALAR >
INLINE const Potential< 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 72 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

72  {
73  if (!isInstalledNode(id)) GUM_ERROR(NotFound, "NodeId " << id << " is not installed")
74 
75  if (_localCPTs_.exists(id))
76  return *_localCPTs_[id];
77  else
78  return _bn_.cpt(id);
79  }
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
NodeProperty< const Potential< GUM_SCALAR > *> _localCPTs_
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.
Definition: DAGmodel_inl.h:82
+ Here is the call graph for this function:

◆ cpt() [2/2]

template<typename GUM_SCALAR >
const Potential< 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 128 of file BayesNetFragment.h.

128  {
129  return cpt(idFromName(name));
130  };
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.

◆ dag()

INLINE const DAG & gum::DAGmodel::dag ( ) const
inherited

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

Definition at line 35 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

35 { return dag_; }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ 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 90 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

90 { return dag().descendants(id); }
NodeSet descendants(NodeId id) const
returns the set of nodes with directed path outgoing from a given node
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call 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 92 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

92  {
93  return descendants(idFromName(name));
94  }
NodeSet descendants(const NodeId id) const
returns the set of nodes with directed path outgoing from a given node
Definition: DAGmodel_inl.h:90
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ 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 78 of file IBayesNet_tpl.h.

78  {
79  Size dim = 0;
80 
81  for (auto node: nodes()) {
82  Size q = 1;
83 
84  for (auto parent: parents(node))
85  q *= variable(parent).domainSize();
86 
87  dim += (variable(node).domainSize() - 1) * q;
88  }
89 
90  return dim;
91  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
virtual Size domainSize() const =0
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
Definition: IBayesNet_tpl.h:78
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ empty()

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

Return true if this graphical model is empty.

Definition at line 94 of file graphicalModel_inl.h.

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

◆ exists() [1/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 82 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

82 { return dag_.exists(node); }
bool exists(const NodeId id) const
alias for existsNode
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ exists() [2/2]

bool gum::GraphicalModel::exists ( const std::string &  name) const
inlineinherited

Return true if this graphical model is empty.

Definition at line 112 of file graphicalModel.h.

112 { return exists(idFromName(name)); };
virtual bool exists(NodeId node) const =0
Return true if this node exists in this graphical model.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.

◆ 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 45 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

45  {
46  return dag_.existsArc(tail, head);
47  }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
+ Here is the call 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 49 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

49  {
50  return existsArc(idFromName(nametail), idFromName(namehead));
51  }
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:45
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ family() [1/2]

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

returns the parents of a node and the 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 59 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

59 { return dag_.family(id); }
NodeSet family(NodeId id) const
returns the set of nodes which consists in the node and its parents
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ family() [2/2]

INLINE NodeSet gum::DAGmodel::family ( 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 61 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

61  {
62  return dag_.family(idFromName(name));
63  }
NodeSet family(NodeId id) const
returns the set of nodes which consists in the node and its parents
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ 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 69 of file DAGmodel.cpp.

References gum::Set< Key, Alloc >::emplace().

69  {
70  if (this == &other) return true;
71 
72  if (size() != other.size()) return false;
73 
74  if (sizeArcs() != other.sizeArcs()) return false;
75 
76  for (const auto& nid: nodes()) {
77  try {
78  other.idFromName(variable(nid).name());
79  } catch (NotFound) { return false; }
80  }
81 
82  for (const auto& arc: arcs()) {
83  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
84  other.idFromName(variable(arc.head()).name()))))
85  return false;
86  }
87 
88  return true;
89  }
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:43
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:41
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:38
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
+ 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 103 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

103  {
104  NodeId id = _bn_.idFromName(name);
105 
106  if (!isInstalledNode(id)) GUM_ERROR(NotFound, "variable " << name << " is not installed")
107 
108  return id;
109  }
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 117 of file graphicalModel_inl.h.

117  {
118  std::vector< NodeId > res;
119  const VariableNodeMap& v = variableNodeMap();
120  std::transform(names.cbegin(),
121  names.cend(),
122  std::back_inserter(res),
123  [v](const std::string& n) { return v.idFromName(n); });
124  return res;
125  }
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.

◆ installArc_()

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

Definition at line 170 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

170  {
171  this->dag_.addArc(from, to);
172  }
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition: DAG_inl.h:42
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ installAscendants() [1/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 148 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

148  {
149  installNode(id);
150 
151  // bn is a dag => this will have an end ...
152  for (auto pa: this->_bn_.parents(id))
153  installAscendants(pa);
154  }
void installAscendants(NodeId id)
install a node and all its ascendants
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
void installNode(NodeId id)
install a node referenced by its nodeId
+ Here is the call graph for this function:

◆ installAscendants() [2/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 203 of file BayesNetFragment.h.

203 { installAscendants(_bn_.idFromName(name)); }
void installAscendants(NodeId id)
install a node and all its ascendants
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.

◆ installCPT() [1/2]

template<typename GUM_SCALAR >
void gum::BayesNetFragment< GUM_SCALAR >::installCPT ( NodeId  id,
const Potential< 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 potential.

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

Definition at line 195 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

195  {
196  if (!dag().existsNode(id))
197  GUM_ERROR(NotFound, "Node " << id << " is not installed in the fragment")
198 
199  if (&(pot.variable(0)) != &(variable(id))) {
200  GUM_ERROR(OperationNotAllowed,
201  "The potential is not a marginal for _bn_.variable <" << variable(id).name()
202  << ">");
203  }
204 
205  const NodeSet& parents = _bn_.parents(id);
206 
207  for (Idx i = 1; i < pot.nbrDim(); i++) {
208  if (!parents.contains(_bn_.idFromName(pot.variable(i).name())))
209  GUM_ERROR(OperationNotAllowed,
210  "Variable <" << pot.variable(i).name() << "> is not in the parents of node "
211  << id);
212  }
213 
214  installCPT_(id, pot);
215  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
void installCPT_(NodeId id, const Potential< GUM_SCALAR > &pot)
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it&#39;s node id.
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ installCPT() [2/2]

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

check if a certain NodeId exists in the fragment

Definition at line 241 of file BayesNetFragment.h.

241  {
242  installCPT(_bn_.idFromName(name), pot);
243  };
void installCPT(NodeId id, const Potential< GUM_SCALAR > &pot)
install a local cpt BY COPYfor a node into the fragment.
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.

◆ installCPT_()

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

Definition at line 175 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

175  {
176  // topology
177  const auto& parents = this->parents(id);
178  for (auto node_it = parents.beginSafe(); node_it != parents.endSafe();
179  ++node_it) // safe iterator needed here
180  uninstallArc_(*node_it, id);
181 
182  for (Idx i = 1; i < pot.nbrDim(); i++) {
183  NodeId parent = _bn_.idFromName(pot.variable(i).name());
184 
185  if (isInstalledNode(parent)) installArc_(parent, id);
186  }
187 
188  // local cpt
189  if (_localCPTs_.exists(id)) uninstallCPT_(id);
190 
191  _localCPTs_.insert(id, new gum::Potential< GUM_SCALAR >(pot));
192  }
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:59
void uninstallArc_(NodeId from, NodeId to)
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
void uninstallCPT_(NodeId id)
uninstall a local CPT.
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
Definition: set_tpl.h:483
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
void installArc_(NodeId from, NodeId to)
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition: set_tpl.h:470
NodeProperty< const Potential< GUM_SCALAR > *> _localCPTs_
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ installMarginal() [1/2]

template<typename GUM_SCALAR >
void gum::BayesNetFragment< GUM_SCALAR >::installMarginal ( NodeId  id,
const Potential< 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 potential
Exceptions
NotFoundif the id is not in the fragment
OperationNotAllowedif the potential is not compliant with the variable (or is not a marginal)

Definition at line 240 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

241  {
242  if (!isInstalledNode(id)) {
243  GUM_ERROR(NotFound, "The node " << id << " is not part of this fragment")
244  }
245 
246  if (pot.nbrDim() > 1) {
247  GUM_ERROR(OperationNotAllowed, "The potential is not a marginal :" << pot)
248  }
249 
250  if (&(pot.variable(0)) != &(_bn_.variable(id))) {
251  GUM_ERROR(OperationNotAllowed,
252  "The potential is not a marginal for _bn_.variable <" << _bn_.variable(id).name()
253  << ">");
254  }
255 
256  installCPT_(id, pot);
257  }
void installCPT_(NodeId id, const Potential< GUM_SCALAR > &pot)
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ installMarginal() [2/2]

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

check if a certain NodeId exists in the fragment

Definition at line 224 of file BayesNetFragment.h.

224  {
225  installMarginal(_bn_.idFromName(name), pot);
226  }
void installMarginal(NodeId id, const Potential< GUM_SCALAR > &pot)
install a local marginal BY COPY for a node into the fragment.
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.

◆ installNode() [1/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 129 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

129  {
130  if (!_bn_.dag().existsNode(id))
131  GUM_ERROR(NotFound, "Node " << id << " does not exist in referred BayesNet")
132 
133  if (!isInstalledNode(id)) {
134  this->dag_.addNodeWithId(id);
135 
136  // adding arcs with id as a tail
137  for (auto pa: this->_bn_.parents(id)) {
138  if (isInstalledNode(pa)) this->dag_.addArc(pa, id);
139  }
140 
141  // adding arcs with id as a head
142  for (auto son: this->_bn_.children(id))
143  if (isInstalledNode(son)) this->dag_.addArc(id, son);
144  }
145  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition: DAG_inl.h:42
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ installNode() [2/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 194 of file BayesNetFragment.h.

194 { installNode(_bn_.idFromName(name)); }
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
void installNode(NodeId id)
install a node referenced by its nodeId

◆ isIndependent() [1/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 113 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

113  {
114  return dag().dSeparation(X, Y, Z);
115  }
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:105
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ isIndependent() [2/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 117 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

117  {
118  return dag().dSeparation(X, Y, Z);
119  }
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:105
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ isIndependent() [3/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 185 of file DAGmodel.h.

187  {
188  return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
189  };
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if node X and node Y are independent given nodes Z
Definition: DAGmodel_inl.h:113

◆ isIndependent() [4/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 191 of file DAGmodel.h.

193  {
194  return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
195  };
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if node X and node Y are independent given nodes Z
Definition: DAGmodel_inl.h:113

◆ isInstalledNode() [1/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 124 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

124  {
125  return dag().existsNode(id);
126  }
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ isInstalledNode() [2/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 183 of file BayesNetFragment.h.

183  {
184  return isInstalledNode(idFromName(name));
185  };
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment

◆ 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 207 of file IBayesNet_tpl.h.

207  {
208  auto value = (GUM_SCALAR)1.0;
209 
210  GUM_SCALAR tmp;
211 
212  for (auto node: nodes()) {
213  if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) { return (GUM_SCALAR)0; }
214 
215  value *= tmp;
216  }
217 
218  return value;
219  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.

◆ log10DomainSize()

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

Definition at line 73 of file graphicalModel_inl.h.

73  {
74  double dSize = 0.0;
75 
76  for (const auto node: nodes()) {
77  dSize += std::log10(variable(node).domainSize());
78  }
79 
80  return dSize;
81  }
virtual const NodeGraphPart & nodes() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.

◆ 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 225 of file IBayesNet_tpl.h.

225  {
226  auto value = (GUM_SCALAR)0.0;
227 
228  GUM_SCALAR tmp;
229 
230  for (auto node: nodes()) {
231  if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
232  return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
233  }
234 
235  value += std::log2(cpt(node)[i]);
236  }
237 
238  return value;
239  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:590

◆ 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 134 of file IBayesNet_tpl.h.

134  {
135  GUM_SCALAR res = 0.0;
136  for (auto node: nodes()) {
137  auto v = cpt(node).maxNonOne();
138  if (v > res) { res = v; }
139  }
140  return res;
141  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.

◆ 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 114 of file IBayesNet_tpl.h.

114  {
115  GUM_SCALAR res = 1.0;
116  for (auto node: nodes()) {
117  auto v = cpt(node).max();
118  if (v > res) { res = v; }
119  }
120  return res;
121  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.

◆ 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 94 of file IBayesNet_tpl.h.

94  {
95  Size res = 0;
96  for (auto node: nodes()) {
97  auto v = variable(node).domainSize();
98  if (v > res) { res = v; }
99  }
100  return res;
101  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual Size domainSize() const =0
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ minimalCondSet() [1/2]

template<typename GUM_SCALAR >
NodeSet gum::IBayesNet< GUM_SCALAR >::minimalCondSet ( NodeId  target,
const NodeSet soids 
) const
inherited

Definition at line 332 of file IBayesNet_tpl.h.

332  {
333  if (soids.contains(target)) return NodeSet({target});
334 
335  NodeSet res;
336  NodeSet alreadyVisitedUp;
337  NodeSet alreadyVisitedDn;
338  alreadyVisitedDn << target;
339  alreadyVisitedUp << target;
340 
341  for (auto fath: dag_.parents(target))
342  _minimalCondSetVisitUp_(fath, soids, res, alreadyVisitedUp, alreadyVisitedDn);
343  for (auto chil: dag_.children(target))
344  _minimalCondSetVisitDn_(chil, soids, res, alreadyVisitedUp, alreadyVisitedDn);
345  return res;
346  }
void _minimalCondSetVisitUp_(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisitedUp, NodeSet &alreadyVisitedDn) const
void _minimalCondSetVisitDn_(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisitedUp, NodeSet &alreadyVisitedDn) const
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222

◆ minimalCondSet() [2/2]

template<typename GUM_SCALAR >
NodeSet gum::IBayesNet< GUM_SCALAR >::minimalCondSet ( const NodeSet targets,
const NodeSet soids 
) const
inherited

Definition at line 349 of file IBayesNet_tpl.h.

350  {
351  NodeSet res;
352  for (auto node: targets) {
353  res += minimalCondSet(node, soids);
354  }
355  return res;
356  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const

◆ 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 124 of file IBayesNet_tpl.h.

124  {
125  GUM_SCALAR res = 1.0;
126  for (auto node: nodes()) {
127  auto v = cpt(node).minNonZero();
128  if (v < res) { res = v; }
129  }
130  return res;
131  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.

◆ 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 104 of file IBayesNet_tpl.h.

104  {
105  GUM_SCALAR res = 1.0;
106  for (auto node: nodes()) {
107  auto v = cpt(node).min();
108  if (v < res) { res = v; }
109  }
110  return res;
111  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.

◆ moralGraph()

const UndiGraph & gum::DAGmodel::moralGraph ( bool  clear = true) const
inherited

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

Parameters
clearIf false returns the previously created moral graph.

Definition at line 55 of file DAGmodel.cpp.

References gum::Set< Key, Alloc >::emplace().

55  {
56  if (clear || (_mutableMoralGraph_ == nullptr)) { // we have to call dag().moralGraph()
57  if (_mutableMoralGraph_ == nullptr) {
58  _mutableMoralGraph_ = new UndiGraph();
59  } else {
60  // clear is True , __mutableMoralGraph exists
62  }
64  }
65 
66  return *_mutableMoralGraph_;
67  }
void clear() override
removes all the nodes and edges from the graph
Definition: undiGraph_inl.h:42
UndiGraph moralGraph() const
build a UndiGraph by moralizing the dag
Definition: DAG.cpp:55
UndiGraph * _mutableMoralGraph_
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:230
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call 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 109 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

109  {
111  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAG.cpp:79
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call 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 104 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

104  {
105  return moralizedAncestralGraph(nodeset(nodenames));
106  }
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAGmodel_inl.h:109
+ Here is the call graph for this function:

◆ names() [1/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 97 of file graphicalModel_inl.h.

97  {
98  std::vector< std::string > res;
99  const VariableNodeMap& v = variableNodeMap();
100  std::transform(ids.cbegin(), ids.cend(), std::back_inserter(res), [v](NodeId n) {
101  return v[n].name();
102  });
103  return res;
104  }
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ names() [2/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 107 of file graphicalModel_inl.h.

107  {
108  const VariableNodeMap& v = variableNodeMap();
109  std::vector< std::string > res;
110  for (auto n: ids) {
111  res.push_back(v.name(n));
112  }
113  return res;
114  }
std::vector< NodeId > ids(const std::vector< std::string > &names) const
transform a vector of names into a vector of nodeId
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.

◆ 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 matches var.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 94 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

94  {
95  NodeId id = _bn_.nodeId(var);
96 
97  if (!isInstalledNode(id)) GUM_ERROR(NotFound, "variable " << var.name() << " is not installed")
98 
99  return id;
100  }
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 84 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

84 { return (NodeGraphPart&)dag_; }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ 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 58 of file graphicalModel.cpp.

References gum::Set< Key, Alloc >::emplace().

58  {
59  NodeSet res;
60  for (const auto& name: names) {
61  res.insert(idFromName(name));
62  }
63  return res;
64  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
+ 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
Returns
Returns false if the src and this are equal.

Definition at line 286 of file IBayesNet_tpl.h.

286  {
287  return !this->operator==(from);
288  }
bool operator==(const IBayesNet< GUM_SCALAR > &from) const
This operator compares 2 BNs !

◆ 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 242 of file IBayesNet_tpl.h.

242  {
243  if (size() != from.size()) { return false; }
244 
245  if (sizeArcs() != from.sizeArcs()) { return false; }
246 
247  // alignment of variables between the 2 BNs
248  Bijection< const DiscreteVariable*, const DiscreteVariable* > alignment;
249 
250  for (auto node: nodes()) {
251  try {
252  alignment.insert(&variable(node), &from.variableFromName(variable(node).name()));
253  } catch (NotFound&) {
254  // a name is not found in from
255  return false;
256  }
257  }
258 
259  for (auto node: nodes()) {
260  NodeId fromnode = from.idFromName(variable(node).name());
261 
262  if (cpt(node).nbrDim() != from.cpt(fromnode).nbrDim()) { return false; }
263 
264  if (cpt(node).domainSize() != from.cpt(fromnode).domainSize()) { return false; }
265 
266  Instantiation i(cpt(node));
267  Instantiation j(from.cpt(fromnode));
268 
269  for (i.setFirst(); !i.end(); i.inc()) {
270  for (Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
271  const DiscreteVariable* p = &(i.variable(indice));
272  j.chgVal(*(alignment.second(p)), i.val(*p));
273  }
274 
275  if (std::pow(cpt(node).get(i) - from.cpt(fromnode).get(j), (GUM_SCALAR)2)
276  > (GUM_SCALAR)1e-6) {
277  return false;
278  }
279  }
280  }
281 
282  return true;
283  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:41
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:38
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ 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 53 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

53 { return dag_.parents(id); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ parents() [2/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 55 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

55  {
56  return parents(idFromName(name));
57  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ parents() [3/4]

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

returns the parents of a set of nodes

Definition at line 76 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

76 { 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
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ 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 78 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

78  {
79  return parents(nodeset(names));
80  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
+ Here is the call graph for this function:

◆ 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 38 of file graphicalModel_inl.h.

38  {
39  try {
40  return _properties_()[name];
41  } catch (NotFound&) {
42  std::string msg = "The following property does not exists: ";
43  GUM_ERROR(NotFound, msg + name)
44  }
45  }
HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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 57 of file graphicalModel_inl.h.

58  {
59  try {
60  return _properties_()[name];
61  } catch (NotFound&) { return byDefault; }
62  }
HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...

◆ 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 65 of file graphicalModel_inl.h.

65  {
66  try {
67  _properties_()[name] = value;
68  } catch (NotFound&) { _properties_().insert(name, value); }
69  }
HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ size()

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

Returns the number of variables in this Directed Graphical Model.

Implements gum::GraphicalModel.

Definition at line 38 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

38 { return dag().size(); }
Size size() const
alias for sizeNodes
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call 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 41 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

41 { return dag_.sizeArcs(); }
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call 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 355 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

355  {
356  if (!checkConsistency()) {
357  GUM_ERROR(OperationNotAllowed, "The fragment contains un-consistent node(s)")
358  }
360  for (const auto nod: nodes()) {
361  res.add(variable(nod), nod);
362  }
363  for (const auto& arc: dag().arcs()) {
364  res.addArc(arc.tail(), arc.head());
365  }
366  for (const auto nod: nodes()) {
367  res.cpt(nod).fillWith(cpt(nod));
368  }
369 
370  return res;
371  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:344
Class representing a Bayesian network.
Definition: BayesNet.h:77
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:43
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:234
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it&#39;s node id.
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:301
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 283 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

283  {
284  std::stringstream output;
285  output << "digraph \"";
286 
287  std::string bn_name;
288 
289  static std::string inFragmentStyle = "fillcolor=\"#ffffaa\","
290  "color=\"#000000\","
291  "fontcolor=\"#000000\"";
292  static std::string styleWithLocalCPT = "fillcolor=\"#ffddaa\","
293  "color=\"#000000\","
294  "fontcolor=\"#000000\"";
295  static std::string notConsistantStyle = "fillcolor=\"#ff0000\","
296  "color=\"#000000\","
297  "fontcolor=\"#ffff00\"";
298  static std::string outFragmentStyle = "fillcolor=\"#f0f0f0\","
299  "color=\"#f0f0f0\","
300  "fontcolor=\"#000000\"";
301 
302  try {
303  bn_name = _bn_.property("name");
304  } catch (NotFound&) { bn_name = "no_name"; }
305 
306  bn_name = "Fragment of " + bn_name;
307 
308  output << bn_name << "\" {" << std::endl;
309  output << " graph [bgcolor=transparent,label=\"" << bn_name << "\"];" << std::endl;
310  output << " node [style=filled];" << std::endl << std::endl;
311 
312  for (auto node: _bn_.nodes()) {
313  output << "\"" << _bn_.variable(node).name() << "\" [comment=\"" << node << ":"
314  << _bn_.variable(node) << ", \"";
315 
316  if (isInstalledNode(node)) {
317  if (!checkConsistency(node)) {
318  output << notConsistantStyle;
319  } else if (_localCPTs_.exists(node))
320  output << styleWithLocalCPT;
321  else
322  output << inFragmentStyle;
323  } else
324  output << outFragmentStyle;
325 
326  output << "];" << std::endl;
327  }
328 
329  output << std::endl;
330 
331  std::string tab = " ";
332 
333  for (auto node: _bn_.nodes()) {
334  if (_bn_.children(node).size() > 0) {
335  for (auto child: _bn_.children(node)) {
336  output << tab << "\"" << _bn_.variable(node).name() << "\" -> "
337  << "\"" << _bn_.variable(child).name() << "\" [";
338 
339  if (dag().existsArc(Arc(node, child)))
340  output << inFragmentStyle;
341  else
342  output << outFragmentStyle;
343 
344  output << "];" << std::endl;
345  }
346  }
347  }
348 
349  output << "}" << std::endl;
350 
351  return output.str();
352  }
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:45
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
NodeProperty< const Potential< GUM_SCALAR > *> _localCPTs_
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ topologicalOrder()

INLINE const Sequence< NodeId > & gum::DAGmodel::topologicalOrder ( bool  clear = true) 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 86 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

86  {
87  return dag().topologicalOrder(clear);
88  }
const Sequence< NodeId > & topologicalOrder(bool clear=true) const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
Definition: diGraph.cpp:88
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call 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 144 of file IBayesNet_tpl.h.

144  {
145  Size param = 0;
146  double dSize = log10DomainSize();
147 
148  for (auto node: nodes())
149  param += cpt(node).content()->realSize();
150 
151  std::stringstream s;
152  s << "BN{nodes: " << size() << ", arcs: " << dag().sizeArcs() << ", ";
153 
154  if (dSize > 6)
155  s << "domainSize: 10^" << dSize;
156  else
157  s << "domainSize: " << std::round(std::pow(10.0, dSize));
158 
159  s << ", dim: " << param << "}";
160 
161  return s.str();
162  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:38
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
double log10DomainSize() const
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35

◆ uninstallArc_()

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

Definition at line 165 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

165  {
166  this->dag_.eraseArc(Arc(from, to));
167  }
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ uninstallCPT() [1/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 224 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

224  {
225  if (_localCPTs_.exists(id)) {
226  uninstallCPT_(id);
227 
228  // re-create arcs from referred potential
229  const Potential< GUM_SCALAR >& pot = cpt(id);
230 
231  for (Idx i = 1; i < pot.nbrDim(); i++) {
232  NodeId parent = _bn_.idFromName(pot.variable(i).name());
233 
234  if (isInstalledNode(parent)) installArc_(parent, id);
235  }
236  }
237  }
void uninstallCPT_(NodeId id)
uninstall a local CPT.
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
void installArc_(NodeId from, NodeId to)
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
NodeProperty< const Potential< GUM_SCALAR > *> _localCPTs_
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ uninstallCPT() [2/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 253 of file BayesNetFragment.h.

253 { uninstallCPT(idFromName(name)); }
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
void uninstallCPT(NodeId id)
uninstall a local CPT.

◆ 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 218 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

218  {
219  delete _localCPTs_[id];
220  _localCPTs_.erase(id);
221  }
NodeProperty< const Potential< GUM_SCALAR > *> _localCPTs_
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
+ Here is the call graph for this function:

◆ uninstallNode() [1/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 157 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

157  {
158  if (isInstalledNode(id)) {
159  uninstallCPT(id);
160  this->dag_.eraseNode(id);
161  }
162  }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
void uninstallCPT(NodeId id)
uninstall a local CPT.
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
Definition: diGraph_inl.h:67
+ Here is the call graph for this function:

◆ uninstallNode() [2/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 211 of file BayesNetFragment.h.

211 { uninstallNode(idFromName(name)); }
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
virtual NodeId idFromName(const std::string &name) const final
Getter by name.

◆ variable() [1/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 87 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

87  {
88  if (!isInstalledNode(id)) GUM_ERROR(NotFound, "NodeId " << id << " is not installed")
89 
90  return _bn_.variable(id);
91  }
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it&#39;s node id.
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ variable() [2/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 143 of file BayesNetFragment.h.

143  {
144  return variable(idFromName(name));
145  };
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it&#39;s node id.
virtual NodeId idFromName(const std::string &name) const final
Getter by name.

◆ 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 113 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

113  {
114  NodeId id = idFromName(name);
115 
116  if (!isInstalledNode(id)) GUM_ERROR(NotFound, "variable " << name << " is not installed")
117 
118  return _bn_.variable(id);
119  }
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it&#39;s node id.
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 82 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

82  {
83  return this->_bn_.variableNodeMap();
84  }
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
+ 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 59 of file BayesNetFragment_tpl.h.

59  {
60  // nothing to do
61  }

◆ 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 64 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

64  {
65  if (dag().existsArc(from, to)) uninstallArc_(from, to);
66  }
void uninstallArc_(NodeId from, NodeId to)
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:45
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ 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 50 of file BayesNetFragment_tpl.h.

50  {
51  // nothing to do
52  }

◆ 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 54 of file BayesNetFragment_tpl.h.

References gum::Set< Key, Alloc >::emplace().

54  {
55  uninstallNode(id);
56  }
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
+ Here is the call graph for this function:

Member Data Documentation

◆ _bn_

template<typename GUM_SCALAR >
const IBayesNet< GUM_SCALAR >& gum::BayesNetFragment< GUM_SCALAR >::_bn_
private

The referred BayesNet.

Definition at line 75 of file BayesNetFragment.h.

◆ _localCPTs_

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

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

Definition at line 79 of file BayesNetFragment.h.

◆ dag_

DAG gum::DAGmodel::dag_
protectedinherited

The DAG of this Directed Graphical Model.

Definition at line 222 of file DAGmodel.h.

◆ graph_

DiGraph* gum::DiGraphListener::graph_
protectedinherited

the graph to listen to

Definition at line 98 of file diGraphListener.h.


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