aGrUM  0.17.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. 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
virtual const Potential< GUM_SCALAR > & cpt (NodeId varId) const final
 Returns the CPT of a variable. More...
 
virtual const Potential< GUM_SCALAR > & cpt (const std::string &name) const
 Returns the CPT of a variable. More...
 
virtual 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
 Returns a constant reference to the dag of this Bayes Net. More...
 
Arc manipulation methods.
const ArcSetarcs () const
 returns the set of nodes with arc ingoing to a given node 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
 returns the set of nodes with arc ingoing to a given node 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
 returns the set of nodes with arc ingoing to a given node More...
 
Graphical methods
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...
 
Variable manipulation methods.
virtual bool empty () const
 Retursn true if this Directed Graphical Model is empty. More...
 
Instantiation completeInstantiation () const
 Get an instantiation over all the variables of the model. More...
 

Protected Attributes

DAG _dag
 The DAG of this Directed Graphical Model. More...
 
DiGraph_graph
 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 73 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 35 of file BayesNetFragment_tpl.h.

36  :
37  DiGraphListener(&bn.dag()),
38  __bn(bn) {
39  GUM_CONSTRUCTOR(BayesNetFragment);
40  }
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 43 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__localCPTs, gum::BayesNetFragment< GUM_SCALAR >::_uninstallCPT(), and gum::DAGmodel::nodes().

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

Member Function Documentation

◆ _installArc()

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

Definition at line 189 of file BayesNetFragment_tpl.h.

References gum::DAGmodel::_dag, and gum::DAG::addArc().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), and gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT().

189  {
190  this->_dag.addArc(from, to);
191  }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:43
+ 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 Potential< GUM_SCALAR > &  pot 
)
protected

Definition at line 194 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::BayesNetFragment< GUM_SCALAR >::__localCPTs, gum::BayesNetFragment< GUM_SCALAR >::_installArc(), gum::BayesNetFragment< GUM_SCALAR >::_uninstallArc(), gum::BayesNetFragment< GUM_SCALAR >::_uninstallCPT(), gum::Set< Key, Alloc >::beginSafe(), gum::Set< Key, Alloc >::endSafe(), gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::DAGmodel::parents(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), and gum::BayesNetFragment< GUM_SCALAR >::installMarginal().

195  {
196  // topology
197  const auto& parents = this->parents(id);
198  for (auto node_it = parents.beginSafe(); node_it != parents.endSafe();
199  ++node_it) // safe iterator needed here
200  _uninstallArc(*node_it, id);
201 
202  for (Idx i = 1; i < pot.nbrDim(); i++) {
203  NodeId parent = __bn.idFromName(pot.variable(i).name());
204 
205  if (isInstalledNode(parent)) _installArc(parent, id);
206  }
207 
208  // local cpt
209  if (__localCPTs.exists(id)) _uninstallCPT(id);
210 
211  __localCPTs.insert(id, new gum::Potential< GUM_SCALAR >(pot));
212  }
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
void _installArc(NodeId from, NodeId to)
NodeProperty< const Potential< GUM_SCALAR > *> __localCPTs
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:46
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
Definition: set_tpl.h:504
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
void _uninstallCPT(NodeId id)
uninstall a local CPT.
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:490
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void _uninstallArc(NodeId from, NodeId to)
+ 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 183 of file BayesNetFragment_tpl.h.

References gum::DAGmodel::_dag, and gum::ArcGraphPart::eraseArc().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), and gum::BayesNetFragment< GUM_SCALAR >::whenArcDeleted().

184  {
185  this->_dag.eraseArc(Arc(from, to));
186  }
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
+ 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.

References gum::BayesNetFragment< GUM_SCALAR >::__localCPTs.

Referenced by gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT(), and gum::BayesNetFragment< GUM_SCALAR >::~BayesNetFragment().

239  {
240  delete __localCPTs[id];
241  __localCPTs.erase(id);
242  }
NodeProperty< const Potential< GUM_SCALAR > *> __localCPTs
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
+ Here is the caller graph for this function:

◆ arcs()

INLINE const ArcSet & gum::DAGmodel::arcs ( ) 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 toward which the nodes returned share an arc

Definition at line 44 of file DAGmodel_inl.h.

References gum::DAGmodel::_dag, and gum::ArcGraphPart::arcs().

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

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

◆ attachSignal__()

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

Definition at line 36 of file listener_inl.h.

References gum::Listener::__senders.

36  {
37  __senders.push_back(sender);
38  }
Senders_list __senders
Definition: listener.h:94

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

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::BayesNetFragment< GUM_SCALAR >::cpt(), GUM_ERROR, gum::Set< Key, Alloc >::insert(), gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), and gum::DAGmodel::parents().

281  {
282  if (!isInstalledNode(id))
283  GUM_ERROR(NotFound, "The node " << id << " is not part of this fragment");
284 
285  const auto& cpt = this->cpt(id);
286  NodeSet cpt_parents;
287 
288  for (Idx i = 1; i < cpt.nbrDim(); i++) {
289  cpt_parents.insert(__bn.idFromName(cpt.variable(i).name()));
290  }
291 
292  return (this->parents(id) == cpt_parents);
293  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:46
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
virtual 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:615
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 273 of file BayesNetFragment.h.

References gum::BayesNetFragment< GUM_SCALAR >::_installArc(), gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::BayesNetFragment< GUM_SCALAR >::_uninstallArc(), gum::BayesNetFragment< GUM_SCALAR >::_uninstallCPT(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::idFromName(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

273  {
274  return checkConsistency(idFromName(name));
275  }
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
+ Here is the call graph for this function:

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

References gum::DAGmodel::nodes().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::toBN(), gum::BayesNetFragment< GUM_SCALAR >::toDot(), and gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT().

296  {
297  for (auto node: nodes())
298  if (!checkConsistency(node)) return false;
299 
300  return true;
301  }
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ children() [1/2]

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 arcs returned may be empty if no arc within the ArcGraphPart is outgoing from the given node.

Parameters
idthe node which is the tail of the arcs returned

Definition at line 53 of file DAGmodel_inl.h.

References gum::DAGmodel::_dag, and gum::ArcGraphPart::children().

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

53  {
54  return _dag.children(id);
55  }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ children() [2/2]

INLINE const NodeSet & gum::DAGmodel::children ( const std::string &  name) 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 toward which the nodes returned share an arc

Definition at line 56 of file DAGmodel_inl.h.

References gum::DAGmodel::_dag, gum::ArcGraphPart::children(), and gum::DAGmodel::idFromName().

56  {
57  return _dag.children(idFromName(name));
58  }
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
+ 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 85 of file graphicalModel_inl.h.

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

85  {
86  Instantiation I;
87 
88  for (const auto node: nodes())
89  I << variable(node);
90 
91  return I;
92  }
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.
+ Here is the call graph for this function:

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

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::BayesNetFragment< GUM_SCALAR >::__localCPTs, GUM_ERROR, and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

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

80  {
81  if (!isInstalledNode(id))
82  GUM_ERROR(NotFound, "NodeId " << id << " is not installed");
83 
84  if (__localCPTs.exists(id))
85  return *__localCPTs[id];
86  else
87  return __bn.cpt(id);
88  }
NodeProperty< const Potential< GUM_SCALAR > *> __localCPTs
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
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:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cpt() [2/2]

template<typename GUM_SCALAR>
virtual const Potential< GUM_SCALAR >& gum::BayesNetFragment< GUM_SCALAR >::cpt ( const std::string &  name) const
inlinevirtual

Returns the CPT of a variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 131 of file BayesNetFragment.h.

References gum::BayesNetFragment< GUM_SCALAR >::cpt(), gum::BayesNetFragment< GUM_SCALAR >::idFromName(), gum::BayesNetFragment< GUM_SCALAR >::variable(), and gum::BayesNetFragment< GUM_SCALAR >::variableNodeMap().

131  {
132  return cpt(idFromName(name));
133  };
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
+ Here is the call graph for this function:

◆ dag()

◆ detachSignal__()

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

Definition at line 40 of file listener_inl.h.

References gum::Listener::__senders.

40  {
41  auto del = std::remove(__senders.begin(), __senders.end(), sender);
42 
43  if (del != __senders.end()) __senders.erase(del, __senders.end());
44  }
Senders_list __senders
Definition: listener.h:94

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

79  {
80  Size dim = 0;
81 
82  for (auto node: nodes()) {
83  Size q = 1;
84 
85  for (auto parent: parents(node))
86  q *= variable(parent).domainSize();
87 
88  dim += (variable(node).domainSize() - 1) * q;
89  }
90 
91  return dim;
92  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:46
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.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
Definition: IBayesNet_tpl.h:79
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ empty()

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

Retursn true if this Directed Graphical Model is empty.

Definition at line 95 of file graphicalModel_inl.h.

References gum::GraphicalModel::size().

95 { return size() == 0; }
virtual Size size() const =0
Returns the number of variables in this Directed Graphical Model.
+ 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 103 of file DAGmodel.cpp.

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

103  {
104  if (this == &other) return true;
105 
106  if (size() != other.size()) return false;
107 
108  if (sizeArcs() != other.sizeArcs()) return false;
109 
110  for (const auto& nid: nodes()) {
111  try {
112  other.idFromName(variable(nid).name());
113  } catch (NotFound) { return false; }
114  }
115 
116  for (const auto& arc: arcs()) {
117  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
118  other.idFromName(variable(arc.head()).name()))))
119  return false;
120  }
121 
122  return true;
123  }
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:44
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:42
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:39
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
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 119 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__bn, GUM_ERROR, and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::cpt(), gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT(), gum::BayesNetFragment< GUM_SCALAR >::uninstallNode(), gum::BayesNetFragment< GUM_SCALAR >::variable(), and gum::BayesNetFragment< GUM_SCALAR >::variableFromName().

119  {
120  NodeId id = __bn.idFromName(name);
121 
122  if (!isInstalledNode(id))
123  GUM_ERROR(NotFound, "variable " << name << " is not installed");
124 
125  return id;
126  }
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:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller 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 166 of file BayesNetFragment_tpl.h.

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

Referenced by gum::BayesNetFragment< GUM_SCALAR >::installAscendants(), and gum::BayesNetFragment< GUM_SCALAR >::installNode().

166  {
167  installNode(id);
168 
169  // bn is a dag => this will have an end ...
170  for (auto pa: this->__bn.parents(id))
171  installAscendants(pa);
172  }
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:
+ Here is the caller 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 209 of file BayesNetFragment.h.

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

209  {
210  installAscendants(__bn.idFromName(name));
211  }
void installAscendants(NodeId id)
install a node and all its ascendants
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
+ Here is the call graph for this function:

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

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::Set< Key, Alloc >::contains(), gum::DAGmodel::dag(), GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::DAGmodel::parents(), gum::MultiDimDecorator< GUM_SCALAR >::variable(), and gum::BayesNetFragment< GUM_SCALAR >::variable().

Referenced by gum::ImportanceSampling< GUM_SCALAR >::_onContextualize(), gum::ImportanceSampling< GUM_SCALAR >::_unsharpenBN(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), and gum::BayesNetFragment< GUM_SCALAR >::installMarginal().

216  {
217  if (!dag().existsNode(id))
218  GUM_ERROR(NotFound, "Node " << id << " is not installed in the fragment");
219 
220  if (&(pot.variable(0)) != &(variable(id))) {
221  GUM_ERROR(OperationNotAllowed,
222  "The potential is not a marginal for __bn.variable <"
223  << 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())))
230  GUM_ERROR(OperationNotAllowed,
231  "Variable <" << pot.variable(i).name()
232  << "> is not in the parents of node " << id);
233  }
234 
235  _installCPT(id, pot);
236  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:46
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
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.
void _installCPT(NodeId id, const Potential< GUM_SCALAR > &pot)
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller 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 252 of file BayesNetFragment.h.

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

252  {
253  installCPT(__bn.idFromName(name), pot);
254  };
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.
+ 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 261 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), GUM_ERROR, gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::installMarginal(), and gum::BayesNetFragment< GUM_SCALAR >::uninstallNode().

262  {
263  if (!isInstalledNode(id)) {
264  GUM_ERROR(NotFound, "The node " << id << " is not part of this fragment");
265  }
266 
267  if (pot.nbrDim() > 1) {
268  GUM_ERROR(OperationNotAllowed, "The potential is not a marginal :" << pot);
269  }
270 
271  if (&(pot.variable(0)) != &(__bn.variable(id))) {
272  GUM_ERROR(OperationNotAllowed,
273  "The potential is not a marginal for __bn.variable <"
274  << __bn.variable(id).name() << ">");
275  }
276 
277  _installCPT(id, pot);
278  }
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
void _installCPT(NodeId id, const Potential< GUM_SCALAR > &pot)
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller 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 234 of file BayesNetFragment.h.

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

235  {
236  installMarginal(__bn.idFromName(name), pot);
237  }
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
void installMarginal(NodeId id, const Potential< GUM_SCALAR > &pot)
install a local marginal BY COPY for a node into the fragment.
+ Here is the call graph for this function:

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

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::DAGmodel::_dag, gum::DAG::addArc(), gum::NodeGraphPart::addNodeWithId(), GUM_ERROR, and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::installAscendants(), gum::BayesNetFragment< GUM_SCALAR >::installNode(), and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

147  {
148  if (!__bn.dag().existsNode(id))
149  GUM_ERROR(NotFound, "Node " << id << " does not exist in referred BayesNet");
150 
151  if (!isInstalledNode(id)) {
152  this->_dag.addNodeWithId(id);
153 
154  // adding arcs with id as a tail
155  for (auto pa: this->__bn.parents(id)) {
156  if (isInstalledNode(pa)) this->_dag.addArc(pa, id);
157  }
158 
159  // addin arcs with id as a head
160  for (auto son: this->__bn.children(id))
161  if (isInstalledNode(son)) this->_dag.addArc(id, son);
162  }
163  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:43
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 ( const std::string &  name)
inline

check if a certain NodeId exists in the fragment

Definition at line 198 of file BayesNetFragment.h.

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

198  {
199  installNode(__bn.idFromName(name));
200  }
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:

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

References gum::DAGmodel::dag(), and gum::NodeGraphPart::existsNode().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::cpt(), gum::BayesNetFragment< GUM_SCALAR >::idFromName(), gum::BayesNetFragment< GUM_SCALAR >::installMarginal(), gum::BayesNetFragment< GUM_SCALAR >::installNode(), gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), gum::BayesNetFragment< GUM_SCALAR >::nodeId(), gum::BayesNetFragment< GUM_SCALAR >::toDot(), gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT(), gum::BayesNetFragment< GUM_SCALAR >::uninstallNode(), gum::BayesNetFragment< GUM_SCALAR >::variable(), and gum::BayesNetFragment< GUM_SCALAR >::variableFromName().

142  {
143  return dag().existsNode(id);
144  }
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:36
+ Here is the call graph for this function:
+ Here is the caller 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 187 of file BayesNetFragment.h.

References gum::BayesNetFragment< GUM_SCALAR >::idFromName(), gum::BayesNetFragment< GUM_SCALAR >::installNode(), and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

187  {
188  return isInstalledNode(idFromName(name));
189  };
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
+ Here is the call 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 211 of file IBayesNet_tpl.h.

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

◆ log10DomainSize()

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

Definition at line 74 of file graphicalModel_inl.h.

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

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

74  {
75  double dSize = 0.0;
76 
77  for (const auto node: nodes()) {
78  dSize += std::log10(variable(node).domainSize());
79  }
80 
81  return dSize;
82  }
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.
+ 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 230 of file IBayesNet_tpl.h.

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

◆ 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

Definition at line 135 of file IBayesNet_tpl.h.

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

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

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

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

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

95  {
96  Size res = 0;
97  for (auto node: nodes()) {
98  auto v = variable(node).domainSize();
99  if (v > res) { res = v; }
100  }
101  return res;
102  }
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.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ Here is the caller graph for this function:

◆ minimalCondSet() [1/2]

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

Definition at line 346 of file IBayesNet_tpl.h.

347  {
348  if (soids.contains(target)) return NodeSet({target});
349 
350  NodeSet res;
351  NodeSet alreadyVisitedUp;
352  NodeSet alreadyVisitedDn;
353  alreadyVisitedDn << target;
354  alreadyVisitedUp << target;
355 
356  for (auto fath: _dag.parents(target))
358  fath, soids, res, alreadyVisitedUp, alreadyVisitedDn);
359  for (auto chil: _dag.children(target))
361  chil, soids, res, alreadyVisitedUp, alreadyVisitedDn);
362  return res;
363  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
void __minimalCondSetVisitUp(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisitedUp, NodeSet &alreadyVisitedDn) const
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
void __minimalCondSetVisitDn(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisitedUp, NodeSet &alreadyVisitedDn) const

◆ minimalCondSet() [2/2]

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

Definition at line 366 of file IBayesNet_tpl.h.

367  {
368  NodeSet res;
369  for (auto node: targets) {
370  res += minimalCondSet(node, soids);
371  }
372  return res;
373  }
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

Definition at line 125 of file IBayesNet_tpl.h.

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

125  {
126  GUM_SCALAR res = 1.0;
127  for (auto node: nodes()) {
128  auto v = cpt(node).minNonZero();
129  if (v < res) { res = v; }
130  }
131  return res;
132  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
+ 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 105 of file IBayesNet_tpl.h.

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

◆ 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 83 of file DAGmodel.cpp.

References gum::DAGmodel::__moralGraph(), gum::DAGmodel::__mutableMoralGraph, and gum::UndiGraph::clear().

Referenced by gum::prm::SVED< GUM_SCALAR >::__eliminateNodes(), gum::prm::SVE< GUM_SCALAR >::__eliminateNodes(), gum::prm::SVED< GUM_SCALAR >::__eliminateNodesWithEvidence(), gum::prm::SVE< GUM_SCALAR >::__eliminateNodesWithEvidence(), gum::prm::SVED< GUM_SCALAR >::__initLiftedNodes(), and gum::prm::SVE< GUM_SCALAR >::__initLiftedNodes().

83  {
84  if (clear
85  || (__mutableMoralGraph == nullptr)) { // we have to call _moralGraph
86  if (__mutableMoralGraph == nullptr) {
87  __mutableMoralGraph = new UndiGraph();
88  } else {
89  // clear is True ,__mutableMoralGraph exists
91  }
92 
93  __moralGraph();
94  }
95 
96  return *__mutableMoralGraph;
97  }
virtual void clear()
removes all the nodes and edges from the graph
Definition: undiGraph_inl.h:43
UndiGraph * __mutableMoralGraph
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:170
void __moralGraph() const
Returns the moral graph of this DAGModel.
Definition: DAGmodel.cpp:44
+ 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 matches var.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 108 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__bn, GUM_ERROR, gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), and gum::Variable::name().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::variable().

108  {
109  NodeId id = __bn.nodeId(var);
110 
111  if (!isInstalledNode(id))
112  GUM_ERROR(NotFound, "variable " << var.name() << " is not installed");
113 
114  return id;
115  }
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:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nodes()

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

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

Implements gum::GraphicalModel.

Definition at line 60 of file DAGmodel_inl.h.

References gum::DAGmodel::_dag.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::__bnCopy(), gum::EssentialGraph::__buildEssentialGraph(), gum::MarkovBlanket::__buildMarkovBlanket(), gum::DAGmodel::__moralGraph(), gum::credal::CredalNet< GUM_SCALAR >::__sort_varType(), gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__verticesSampling(), gum::ImportanceSampling< GUM_SCALAR >::_unsharpenBN(), gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::disturbBN(), gum::Estimator< GUM_SCALAR >::Estimator(), gum::getMaxModality(), gum::DAGmodel::hasSameStructure(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), gum::prm::ClassBayesNet< GUM_SCALAR >::modalities(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::BayesNetFragment< GUM_SCALAR >::toBN(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), gum::prm::ClassBayesNet< GUM_SCALAR >::toDot(), gum::credal::CredalNet< GUM_SCALAR >::toString(), and gum::BayesNetFragment< GUM_SCALAR >::~BayesNetFragment().

60  {
61  return (NodeGraphPart&)_dag;
62  }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
+ 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 294 of file IBayesNet_tpl.h.

294  {
295  return !this->operator==(from);
296  }
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 247 of file IBayesNet_tpl.h.

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

◆ parents() [1/2]

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 toward which the nodes returned share an arc

Definition at line 46 of file DAGmodel_inl.h.

References gum::DAGmodel::_dag, and gum::ArcGraphPart::parents().

Referenced by gum::MarkovBlanket::__buildMarkovBlanket(), gum::DAGmodel::__moralGraph(), gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), gum::DAGmodel::parents(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::prm::ClassBayesNet< GUM_SCALAR >::toDot().

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

◆ parents() [2/2]

INLINE const NodeSet & gum::DAGmodel::parents ( const std::string &  name) 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 toward which the nodes returned share an arc

Definition at line 49 of file DAGmodel_inl.h.

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

49  {
50  return parents(idFromName(name));
51  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:46
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ 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 37 of file graphicalModel_inl.h.

References gum::GraphicalModel::__properties(), and GUM_ERROR.

Referenced by gum::InfluenceDiagram< GUM_SCALAR >::toDot().

37  {
38  try {
39  return __properties()[name];
40  } catch (NotFound&) {
41  std::string msg = "The following property does not exists: ";
42  GUM_ERROR(NotFound, msg + name);
43  }
44  }
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:55
+ 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 57 of file graphicalModel_inl.h.

References gum::GraphicalModel::__properties().

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

◆ setProperty()

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

Add or change a property of this GraphicalModel.

Definition at line 65 of file graphicalModel_inl.h.

References gum::GraphicalModel::__properties(), and gum::HashTable< Key, Val, Alloc >::insert().

Referenced by gum::BayesNet< double >::fastPrototype(), and gum::IMarkovNet< GUM_SCALAR >::IMarkovNet().

66  {
67  try {
68  __properties()[name] = value;
69  } catch (NotFound&) { __properties().insert(name, value); }
70  }
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
HashTable< std::string, std::string > & __properties() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
+ Here is the call graph for this function:
+ 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 39 of file DAGmodel_inl.h.

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

Referenced by gum::credal::CredalNet< GUM_SCALAR >::__initCNNets(), gum::InfluenceDiagram< GUM_SCALAR >::decisionNodeSize(), gum::MarkovBlanket::hasSameStructure(), gum::DAGmodel::hasSameStructure(), gum::IBayesNet< double >::operator==(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::prm::ClassBayesNet< GUM_SCALAR >::toDot().

39 { 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:36
+ 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 42 of file DAGmodel_inl.h.

References gum::DAGmodel::_dag, and gum::ArcGraphPart::sizeArcs().

Referenced by gum::MarkovBlanket::hasSameStructure(), gum::DAGmodel::hasSameStructure(), and gum::IBayesNet< double >::operator==().

42 { return _dag.sizeArcs(); }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
+ Here is the call graph for this function:
+ 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 377 of file BayesNetFragment_tpl.h.

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

Referenced by gum::BayesNetFragment< GUM_SCALAR >::checkConsistency().

377  {
378  if (!checkConsistency()) {
379  GUM_ERROR(OperationNotAllowed, "The fragment contains un-consistent node(s)")
380  }
382  for (const auto nod: nodes()) {
383  res.add(variable(nod), nod);
384  }
385  for (const auto arc: dag().arcs()) {
386  res.addArc(arc.tail(), arc.head());
387  }
388  for (const auto nod: nodes()) {
389  res.cpt(nod).fillWith(cpt(nod));
390  }
391 
392  return res;
393  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:369
Class representing a Bayesian Network.
Definition: BayesNet.h:78
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:44
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:243
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it&#39;s node id.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:326
virtual 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:36
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller 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 304 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::BayesNetFragment< GUM_SCALAR >::__localCPTs, gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::DAGmodel::dag(), and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::variable().

304  {
305  std::stringstream output;
306  output << "digraph \"";
307 
308  std::string bn_name;
309 
310  static std::string inFragmentStyle = "fillcolor=\"#ffffaa\","
311  "color=\"#000000\","
312  "fontcolor=\"#000000\"";
313  static std::string styleWithLocalCPT = "fillcolor=\"#ffddaa\","
314  "color=\"#000000\","
315  "fontcolor=\"#000000\"";
316  static std::string notConsistantStyle = "fillcolor=\"#ff0000\","
317  "color=\"#000000\","
318  "fontcolor=\"#ffff00\"";
319  static std::string outFragmentStyle = "fillcolor=\"#f0f0f0\","
320  "color=\"#f0f0f0\","
321  "fontcolor=\"#000000\"";
322 
323  try {
324  bn_name = __bn.property("name");
325  } catch (NotFound&) { bn_name = "no_name"; }
326 
327  bn_name = "Fragment of " + bn_name;
328 
329  output << bn_name << "\" {" << std::endl;
330  output << " graph [bgcolor=transparent,label=\"" << bn_name << "\"];"
331  << std::endl;
332  output << " node [style=filled];" << std::endl << std::endl;
333 
334  for (auto node: __bn.nodes()) {
335  output << "\"" << __bn.variable(node).name() << "\" [comment=\"" << node
336  << ":" << __bn.variable(node) << ", \"";
337 
338  if (isInstalledNode(node)) {
339  if (!checkConsistency(node)) {
340  output << notConsistantStyle;
341  } else if (__localCPTs.exists(node))
342  output << styleWithLocalCPT;
343  else
344  output << inFragmentStyle;
345  } else
346  output << outFragmentStyle;
347 
348  output << "];" << std::endl;
349  }
350 
351  output << std::endl;
352 
353  std::string tab = " ";
354 
355  for (auto node: __bn.nodes()) {
356  if (__bn.children(node).size() > 0) {
357  for (auto child: __bn.children(node)) {
358  output << tab << "\"" << __bn.variable(node).name() << "\" -> "
359  << "\"" << __bn.variable(child).name() << "\" [";
360 
361  if (dag().existsArc(Arc(node, child)))
362  output << inFragmentStyle;
363  else
364  output << outFragmentStyle;
365 
366  output << "];" << std::endl;
367  }
368  }
369  }
370 
371  output << "}" << std::endl;
372 
373  return output.str();
374  }
NodeProperty< const Potential< GUM_SCALAR > *> __localCPTs
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
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
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ topologicalOrder()

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 99 of file DAGmodel.cpp.

References gum::DAGmodel::dag(), and gum::DiGraph::topologicalOrder().

Referenced by gum::EssentialGraph::__buildEssentialGraph(), gum::InfluenceDiagramGenerator< GUM_SCALAR >::__checkTemporalOrder(), gum::InfluenceDiagram< GUM_SCALAR >::decisionOrderExists(), and gum::InfluenceDiagram< GUM_SCALAR >::getDecisionOrder().

99  {
100  return this->dag().topologicalOrder(clear);
101  }
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:91
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
+ Here is the call graph for this function:
+ 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 145 of file IBayesNet_tpl.h.

Referenced by gum::operator<<().

145  {
146  Size param = 0;
147  double dSize = log10DomainSize();
148 
149  for (auto node: nodes())
150  param += cpt(node).content()->realSize();
151 
152  std::stringstream s;
153  s << "BN{nodes: " << size() << ", arcs: " << dag().sizeArcs() << ", ";
154 
155  if (dSize > 6)
156  s << "domainSize: 10^" << dSize;
157  else
158  s << "domainSize: " << std::round(std::pow(10.0, dSize));
159 
160  s << ", dim: " << param << "}";
161 
162  return s.str();
163  }
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:39
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
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
+ Here is the caller 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 245 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__bn, gum::BayesNetFragment< GUM_SCALAR >::__localCPTs, gum::BayesNetFragment< GUM_SCALAR >::_installArc(), gum::BayesNetFragment< GUM_SCALAR >::_uninstallCPT(), gum::BayesNetFragment< GUM_SCALAR >::cpt(), gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

Referenced by gum::ImportanceSampling< GUM_SCALAR >::_onContextualize(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), and gum::BayesNetFragment< GUM_SCALAR >::uninstallNode().

245  {
246  if (__localCPTs.exists(id)) {
247  _uninstallCPT(id);
248 
249  // re-create arcs from referred potential
250  const Potential< 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 
255  if (isInstalledNode(parent)) _installArc(parent, id);
256  }
257  }
258  }
void _installArc(NodeId from, NodeId to)
NodeProperty< const Potential< GUM_SCALAR > *> __localCPTs
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
void _uninstallCPT(NodeId id)
uninstall a local CPT.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
+ Here is the call graph for this function:
+ Here is the caller 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 264 of file BayesNetFragment.h.

References gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::BayesNetFragment< GUM_SCALAR >::idFromName(), and gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT().

264 { uninstallCPT(idFromName(name)); }
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
void uninstallCPT(NodeId id)
uninstall a local CPT.
+ Here is the call graph for this function:
+ Here is the caller 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 175 of file BayesNetFragment_tpl.h.

References gum::DAGmodel::_dag, gum::DiGraph::eraseNode(), gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), and gum::BayesNetFragment< GUM_SCALAR >::uninstallCPT().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::installAscendants(), gum::BayesNetFragment< GUM_SCALAR >::uninstallNode(), and gum::BayesNetFragment< GUM_SCALAR >::whenNodeDeleted().

175  {
176  if (isInstalledNode(id)) {
177  uninstallCPT(id);
178  this->_dag.eraseNode(id);
179  }
180  }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
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:69
+ Here is the call graph for this function:
+ Here is the caller 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 219 of file BayesNetFragment.h.

References gum::BayesNetFragment< GUM_SCALAR >::idFromName(), gum::BayesNetFragment< GUM_SCALAR >::installMarginal(), and gum::BayesNetFragment< GUM_SCALAR >::uninstallNode().

219  {
220  uninstallNode(idFromName(name));
221  }
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
+ Here is the call graph for this function:

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

References gum::BayesNetFragment< GUM_SCALAR >::__bn, GUM_ERROR, and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::cpt(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), gum::BayesNetFragment< GUM_SCALAR >::toBN(), and gum::BayesNetFragment< GUM_SCALAR >::variable().

99  {
100  if (!isInstalledNode(id))
101  GUM_ERROR(NotFound, "NodeId " << id << " is not installed");
102 
103  return __bn.variable(id);
104  }
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:55
+ Here is the call graph for this function:
+ Here is the caller 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 146 of file BayesNetFragment.h.

References gum::BayesNetFragment< GUM_SCALAR >::idFromName(), gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode(), gum::BayesNetFragment< GUM_SCALAR >::nodeId(), gum::BayesNetFragment< GUM_SCALAR >::toDot(), gum::BayesNetFragment< GUM_SCALAR >::variable(), and gum::BayesNetFragment< GUM_SCALAR >::variableFromName().

146  {
147  return variable(idFromName(name));
148  };
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.
+ Here is the call 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 129 of file BayesNetFragment_tpl.h.

References gum::BayesNetFragment< GUM_SCALAR >::__bn, GUM_ERROR, gum::BayesNetFragment< GUM_SCALAR >::idFromName(), and gum::BayesNetFragment< GUM_SCALAR >::isInstalledNode().

Referenced by gum::BayesNetFragment< GUM_SCALAR >::variable().

130  {
131  NodeId id = idFromName(name);
132 
133  if (!isInstalledNode(id))
134  GUM_ERROR(NotFound, "variable " << name << " is not installed");
135 
136  return __bn.variable(id);
137  }
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:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller 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 92 of file BayesNetFragment_tpl.h.

References GUM_ERROR.

Referenced by gum::BayesNetFragment< GUM_SCALAR >::cpt().

92  {
93  GUM_ERROR(FatalError,
94  "Not implemented yet. please use referent bayesnet method");
95  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller 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 63 of file BayesNetFragment_tpl.h.

65  {
66  // nothing to do
67  }

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

References gum::BayesNetFragment< GUM_SCALAR >::_uninstallArc(), and gum::DAGmodel::dag().

71  {
72  if (dag().existsArc(from, to)) _uninstallArc(from, to);
73  }
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
void _uninstallArc(NodeId from, NodeId to)
+ 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 53 of file BayesNetFragment_tpl.h.

54  {
55  // nothing to do
56  }

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

References gum::BayesNetFragment< GUM_SCALAR >::uninstallNode().

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

Member Data Documentation

◆ __bn

◆ __localCPTs

◆ _dag

◆ _graph

DiGraph* gum::DiGraphListener::_graph
protectedinherited

the graph to listen to

Definition at line 99 of file diGraphListener.h.

Referenced by gum::DiGraphListener::DiGraphListener().


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