aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::IMarkovNet< GUM_SCALAR > Class Template Referenceabstract

Class representing the minimal interface for Markov Network. More...

#include <agrum/BN/IMarkovNet.h>

+ Inheritance diagram for gum::IMarkovNet< GUM_SCALAR >:
+ Collaboration diagram for gum::IMarkovNet< GUM_SCALAR >:

Public Member Functions

bool operator== (const IMarkovNet< GUM_SCALAR > &from) const
 This operator compares 2 BNs ! More...
 
bool operator!= (const IMarkovNet< 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
 
virtual std::string toDot () const
 
virtual std::string toDotAsFactorGraph () 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 UGmodel &other)
 check if nodes X and nodes Y are independent given nodes Z More...
 
double log10DomainSize () const
 
Constructors / Destructors
 IMarkovNet ()
 Default constructor. More...
 
 IMarkovNet (std::string name)
 Default constructor. More...
 
virtual ~IMarkovNet ()
 Destructor. More...
 
 IMarkovNet (const IMarkovNet< GUM_SCALAR > &source)
 Copy constructor. More...
 
IMarkovNet< GUM_SCALAR > & operator= (const IMarkovNet< GUM_SCALAR > &source)
 Copy operator. More...
 
Pure Virtual methods
virtual const Potential< GUM_SCALAR > & factor (const NodeSet &varIds) const =0
 Returns the factor of a set of variable. More...
 
virtual const NodeSetsmallestFactorFromNode (NodeId node) const =0
 Returns the smallest factor that contains this variable. More...
 
virtual const NodeSetsmallestFactorFromNode (const std::string &name) const
 Returns the smallest factor that contains this variable. More...
 
virtual const FactorTable< GUM_SCALAR > & factors () const =0
 Returns the set of factors as a IMarkovNet::FactorTable. More...
 
virtual const VariableNodeMapvariableNodeMap () const =0
 Returns a constant reference to the VariableNodeMap of thisBN. More...
 
virtual const DiscreteVariablevariable (NodeId id) const =0
 Returns a constant reference over a variable given it's node id. More...
 
virtual NodeId nodeId (const DiscreteVariable &var) const =0
 Return id node from discrete var pointer. More...
 
virtual NodeId idFromName (const std::string &name) const =0
 Getter by name. More...
 
virtual const DiscreteVariablevariableFromName (const std::string &name) const =0
 Getter by name. More...
 
Variable manipulation methods.
const UndiGraphgraph () 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 sizeEdges () 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...
 
Edge manipulation methods.
const EdgeSetedges () const
 return true if the edge node1-node2 exists in the UGModel More...
 
bool existsEdge (const NodeId node1, const NodeId node2) const
 return true if the edge node1-node2 exists in the UGModel More...
 
bool existsEdge (const std::string &name1, const std::string &name2) const
 return true if the edge node1-node2 exists in the UGModel More...
 
const NodeSetneighbours (const NodeId id) const
 returns the neighbours of a node as set of nodes More...
 
const NodeSetneighbours (const std::string &name) const
 return true if the edge node1-node2 exists in the UGModel More...
 
virtual bool isIndependent (NodeId X, NodeId Y, const NodeSet &Z) const final
 check if X and Y are independent given 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
 return true if the edge node1-node2 exists in the UGModel More...
 
bool isIndependent (const std::vector< std::string > &Xnames, const std::vector< std::string > &Ynames, const std::vector< std::string > &Znames) const
 return true if the edge node1-node2 exists in the UGModel 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

UndiGraph graph_
 The DAG of this Directed Graphical Model. More...
 

Detailed Description

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

Class representing the minimal interface for Markov Network.

This class is used as a base class for different versions of Markov Networks. No data (except the graph herited from UGModel are included in this class. Many algorithms (inference for instance) may use this class when a simple MN is needed.

Definition at line 60 of file IMarkovNet.h.

Constructor & Destructor Documentation

◆ IMarkovNet() [1/3]

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

Default constructor.

Definition at line 40 of file IMarkovNet_tpl.h.

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

40  : UGmodel() {
41  GUM_CONSTRUCTOR(IMarkovNet);
42  }
UGmodel()
Default constructor.
Definition: UGmodel.cpp:29
IMarkovNet()
Default constructor.
+ Here is the call graph for this function:

◆ IMarkovNet() [2/3]

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

Default constructor.

Definition at line 45 of file IMarkovNet_tpl.h.

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

45  : UGmodel() {
46  GUM_CONSTRUCTOR(IMarkovNet);
47  this->setProperty("name", name);
48  }
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
UGmodel()
Default constructor.
Definition: UGmodel.cpp:29
IMarkovNet()
Default constructor.
+ Here is the call graph for this function:

◆ ~IMarkovNet()

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

Destructor.

Definition at line 64 of file IMarkovNet_tpl.h.

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

64  {
65  GUM_DESTRUCTOR(IMarkovNet);
66  }
IMarkovNet()
Default constructor.
+ Here is the call graph for this function:

◆ IMarkovNet() [3/3]

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

Copy constructor.

Definition at line 51 of file IMarkovNet_tpl.h.

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

51  : UGmodel(source) {
52  GUM_CONS_CPY(IMarkovNet);
53  }
UGmodel()
Default constructor.
Definition: UGmodel.cpp:29
IMarkovNet()
Default constructor.
+ Here is the call graph for this function:

Member Function Documentation

◆ _minimalCondSetVisit_()

template<typename GUM_SCALAR >
void gum::IMarkovNet< GUM_SCALAR >::_minimalCondSetVisit_ ( NodeId  node,
const NodeSet soids,
NodeSet minimal,
NodeSet alreadyVisited 
) const
private

Definition at line 309 of file IMarkovNet_tpl.h.

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

312  {
313  if (alreadyVisited.contains(node)) return;
314  alreadyVisited << node;
315 
316  if (soids.contains(node)) {
317  minimal << node;
318  } else {
319  for (auto neig: graph_.neighbours(node))
320  _minimalCondSetVisit_(neig, soids, minimal, alreadyVisited);
321  }
322  }
void _minimalCondSetVisit_(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisited) const
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
const NodeSet & neighbours(const NodeId id) const
returns the set of node neighbours to 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 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.

◆ dim()

template<typename GUM_SCALAR >
INLINE Size gum::IMarkovNet< GUM_SCALAR >::dim ( ) const

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

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

69  {
70  Size res = 0;
71  for (auto f: factors()) {
72  res += f.second->domainSize();
73  }
74  return res;
75  }
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ edges()

INLINE const EdgeSet & gum::UGmodel::edges ( ) const
inherited

return true if the edge node1-node2 exists in the UGModel

Parameters
node1the nodeId (or the name) of the node1
node2the nodeId (or the name) of the node2
Returns
true if the edge exists

Definition at line 43 of file UGmodel_inl.h.

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

43 { return graph_.edges(); }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
+ Here is the call graph for this function:

◆ 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::UGmodel::exists ( NodeId  node) const
finalvirtualinherited

Return true if this node exists in this graphical model.

Implements gum::GraphicalModel.

Definition at line 58 of file UGmodel_inl.h.

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

58 { return graph_.exists(node); }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
bool exists(const NodeId id) const
alias for existsNode
+ 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.

◆ existsEdge() [1/2]

INLINE bool gum::UGmodel::existsEdge ( const NodeId  node1,
const NodeId  node2 
) const
inherited

return true if the edge node1-node2 exists in the UGModel

Parameters
node1the nodeId (or the name) of the node1
node2the nodeId (or the name) of the node2
Returns
true if the edge exists

Definition at line 45 of file UGmodel_inl.h.

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

45  {
46  return graph_.existsEdge(node1, node2);
47  }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
bool existsEdge(const Edge &edge) const
indicates whether a given edge exists
+ Here is the call graph for this function:

◆ existsEdge() [2/2]

INLINE bool gum::UGmodel::existsEdge ( const std::string &  name1,
const std::string &  name2 
) const
inherited

return true if the edge node1-node2 exists in the UGModel

Parameters
node1the nodeId (or the name) of the node1
node2the nodeId (or the name) of the node2
Returns
true if the edge exists

Definition at line 49 of file UGmodel_inl.h.

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

49  {
50  return existsEdge(idFromName(name1), idFromName(name2));
51  }
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool existsEdge(const NodeId node1, const NodeId node2) const
return true if the edge node1-node2 exists in the UGModel
Definition: UGmodel_inl.h:45
+ Here is the call graph for this function:

◆ factor()

template<typename GUM_SCALAR >
virtual const Potential< GUM_SCALAR >& gum::IMarkovNet< GUM_SCALAR >::factor ( const NodeSet varIds) const
pure virtual

Returns the factor of a set of variable.

Exceptions
NotFoundIf no variable's id matches varId.

◆ factors()

template<typename GUM_SCALAR >
virtual const FactorTable< GUM_SCALAR >& gum::IMarkovNet< GUM_SCALAR >::factors ( ) const
pure virtual

Returns the set of factors as a IMarkovNet::FactorTable.

◆ graph()

INLINE const UndiGraph & gum::UGmodel::graph ( ) const
inherited

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

Definition at line 35 of file UGmodel_inl.h.

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

35 { return graph_; }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
+ Here is the call graph for this function:

◆ hasSameStructure()

bool gum::UGmodel::hasSameStructure ( const UGmodel other)
inherited

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

Returns
true if all the named node are the same and all the named arcs are the same

Definition at line 50 of file UGmodel.cpp.

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

50  {
51  if (this == &other) return true;
52 
53  if (size() != other.size()) return false;
54 
55  if (sizeEdges() != other.sizeEdges()) return false;
56 
57  for (const auto& nid: nodes()) {
58  try {
59  other.idFromName(variable(nid).name());
60  } catch (NotFound) { return false; }
61  }
62 
63  for (const auto& edge: edges()) {
64  if (!other.edges().exists(Edge(other.idFromName(variable(edge.first()).name()),
65  other.idFromName(variable(edge.second()).name()))))
66  return false;
67  }
68 
69  return true;
70  }
const EdgeSet & edges() const
return true if the edge node1-node2 exists in the UGModel
Definition: UGmodel_inl.h:43
Size sizeEdges() const
Returns the number of arcs in this Directed Graphical Model.
Definition: UGmodel_inl.h:41
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:60
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: UGmodel_inl.h:38
+ Here is the call graph for this function:

◆ idFromName()

template<typename GUM_SCALAR >
virtual NodeId gum::IMarkovNet< GUM_SCALAR >::idFromName ( const std::string &  name) const
pure virtual

Getter by name.

Exceptions
NotFoundif no such name exists in the graph.

Implements gum::GraphicalModel.

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

◆ isIndependent() [1/4]

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

check if X and Y are independent given Z

Implements gum::GraphicalModel.

Definition at line 62 of file UGmodel_inl.h.

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

62  {
63  return !graph().hasUndirectedPath(X, Y, Z);
64  }
bool hasUndirectedPath(const NodeId n1, const NodeId n2) const
return true if n1 and n2 are connected (by an undirected path) in the graph.
const UndiGraph & graph() const
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:35
+ Here is the call graph for this function:

◆ isIndependent() [2/4]

INLINE bool gum::UGmodel::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 66 of file UGmodel_inl.h.

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

66  {
67  return !graph().hasUndirectedPath(X, Y, Z);
68  }
bool hasUndirectedPath(const NodeId n1, const NodeId n2) const
return true if n1 and n2 are connected (by an undirected path) in the graph.
const UndiGraph & graph() const
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:35
+ Here is the call graph for this function:

◆ isIndependent() [3/4]

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

return true if the edge node1-node2 exists in the UGModel

Parameters
node1the nodeId (or the name) of the node1
node2the nodeId (or the name) of the node2
Returns
true if the edge exists

Definition at line 125 of file UGmodel.h.

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

◆ isIndependent() [4/4]

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

return true if the edge node1-node2 exists in the UGModel

Parameters
node1the nodeId (or the name) of the node1
node2the nodeId (or the name) of the node2
Returns
true if the edge exists

Definition at line 130 of file UGmodel.h.

132  {
133  return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
134  };
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
virtual bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if X and Y are independent given Z
Definition: UGmodel_inl.h:62

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

◆ maxNonOneParam()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::IMarkovNet< GUM_SCALAR >::maxNonOneParam ( ) const
Returns
the biggest value (not equal to 1) in the CPTs of *this

Definition at line 118 of file IMarkovNet_tpl.h.

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

118  {
119  GUM_SCALAR res = 0.0;
120  for (auto elt: factors()) {
121  auto v = elt.second->maxNonOne();
122  if (v > res) { res = v; }
123  }
124  return res;
125  }
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
+ Here is the call graph for this function:

◆ maxParam()

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

Definition at line 98 of file IMarkovNet_tpl.h.

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

98  {
99  GUM_SCALAR res = 1.0;
100  for (auto elt: factors()) {
101  auto v = elt.second->max();
102  if (v > res) { res = v; }
103  }
104  return res;
105  }
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
+ Here is the call graph for this function:

◆ maxVarDomainSize()

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

Definition at line 78 of file IMarkovNet_tpl.h.

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

78  {
79  Size res = 0;
80  for (auto node: nodes()) {
81  auto v = variable(node).domainSize();
82  if (v > res) { res = v; }
83  }
84  return res;
85  }
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
virtual Size domainSize() const =0
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:60
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ minimalCondSet() [1/2]

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

Definition at line 326 of file IMarkovNet_tpl.h.

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

326  {
327  if (soids.contains(target)) return NodeSet({target});
328 
329  NodeSet res;
330  NodeSet alreadyVisited;
331  alreadyVisited << target;
332 
333  for (auto neig: graph_.neighbours(target))
334  _minimalCondSetVisit_(neig, soids, res, alreadyVisited);
335  return res;
336  }
void _minimalCondSetVisit_(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisited) const
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
const NodeSet & neighbours(const NodeId id) const
returns the set of node neighbours to a given node
+ Here is the call graph for this function:

◆ minimalCondSet() [2/2]

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

Definition at line 339 of file IMarkovNet_tpl.h.

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

340  {
341  NodeSet res;
342  for (auto node: targets) {
343  res += minimalCondSet(node, soids);
344  }
345  return res;
346  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
+ Here is the call graph for this function:

◆ minNonZeroParam()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::IMarkovNet< GUM_SCALAR >::minNonZeroParam ( ) const
Returns
the smallest value (not equal to 0) in the CPTs of *this

Definition at line 108 of file IMarkovNet_tpl.h.

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

108  {
109  GUM_SCALAR res = 1.0;
110  for (auto elt: factors()) {
111  auto v = elt.second->minNonZero();
112  if (v < res) { res = v; }
113  }
114  return res;
115  }
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
+ Here is the call graph for this function:

◆ minParam()

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

Definition at line 88 of file IMarkovNet_tpl.h.

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

88  {
89  GUM_SCALAR res = 1.0;
90  for (auto elt: factors()) {
91  auto v = elt.second->min();
92  if (v < res) { res = v; }
93  }
94  return res;
95  }
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
+ 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.

◆ neighbours() [1/2]

INLINE const NodeSet & gum::UGmodel::neighbours ( const NodeId  id) const
inherited

returns the neighbours of a node as set of nodes

Note that the set of nodes returned may be empty if no edge within the EdgeGraphPart contains the given node.

Parameters
idthe node toward which the edge returned are pointing

Definition at line 53 of file UGmodel_inl.h.

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

53 { return graph_.neighbours(id); }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
const NodeSet & neighbours(const NodeId id) const
returns the set of node neighbours to a given node
+ Here is the call graph for this function:

◆ neighbours() [2/2]

INLINE const NodeSet & gum::UGmodel::neighbours ( const std::string &  name) const
inherited

return true if the edge node1-node2 exists in the UGModel

Parameters
node1the nodeId (or the name) of the node1
node2the nodeId (or the name) of the node2
Returns
true if the edge exists

Definition at line 54 of file UGmodel_inl.h.

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

54  {
55  return neighbours(idFromName(name));
56  }
const NodeSet & neighbours(const NodeId id) const
returns the neighbours of a node as set of nodes
Definition: UGmodel_inl.h:53
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ nodeId()

template<typename GUM_SCALAR >
virtual NodeId gum::IMarkovNet< GUM_SCALAR >::nodeId ( const DiscreteVariable var) const
pure virtual

Return id node from discrete var pointer.

Exceptions
NotFoundIf no variable matches var.

Implements gum::GraphicalModel.

◆ nodes()

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

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

Implements gum::GraphicalModel.

Definition at line 60 of file UGmodel_inl.h.

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

60 { return (NodeGraphPart&)graph_; }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
+ 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 >
INLINE bool gum::IMarkovNet< GUM_SCALAR >::operator!= ( const IMarkovNet< GUM_SCALAR > &  from) const
Returns
Returns false if the src and this are equal.

Definition at line 289 of file IMarkovNet_tpl.h.

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

289  {
290  return !this->operator==(from);
291  }
bool operator==(const IMarkovNet< GUM_SCALAR > &from) const
This operator compares 2 BNs !
+ Here is the call graph for this function:

◆ operator=()

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

Copy operator.

Definition at line 57 of file IMarkovNet_tpl.h.

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

57  {
58  if (this != &source) { UGmodel::operator=(source); }
59 
60  return *this;
61  }
UGmodel & operator=(const UGmodel &source)
Private copy operator.
Definition: UGmodel.cpp:41
+ Here is the call graph for this function:

◆ operator==()

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

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 243 of file IMarkovNet_tpl.h.

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

243  {
244  if (size() != from.size()) { return false; }
245 
246  if (sizeEdges() != from.sizeEdges()) { return false; }
247 
248  // alignment of variables between the 2 BNs
249  Bijection< const DiscreteVariable*, const DiscreteVariable* > alignment;
250 
251  for (auto node: nodes()) {
252  try {
253  alignment.insert(&variable(node), &from.variableFromName(variable(node).name()));
254  } catch (NotFound&) {
255  // a name is not found in from
256  return false;
257  }
258  }
259 
260  for (const auto& elt: factors()) {
261  const auto& key = elt.first;
262  const auto& factor = *elt.second;
263 
264  NodeSet fromkey;
265  for (const auto n: key)
266  fromkey.insert(from.idFromName(variable(n).name()));
267 
268  if (!from.factors().exists(fromkey)) { return false; }
269 
270  const auto& fromfactor = from.factor(fromkey);
271 
272  Instantiation i(factor);
273  Instantiation j(fromfactor);
274  for (i.setFirst(); !i.end(); i.inc()) {
275  for (Idx indice = 0; indice < factor.nbrDim(); ++indice) {
276  const DiscreteVariable* p = &(i.variable(indice));
277  j.chgVal(*(alignment.second(p)), i.val(*p));
278  }
279 
280  if (std::pow(factor.get(i) - fromfactor.get(j), (GUM_SCALAR)2) > (GUM_SCALAR)1e-6) {
281  return false;
282  }
283  }
284  }
285  return true;
286  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
Size sizeEdges() const
Returns the number of arcs in this Directed Graphical Model.
Definition: UGmodel_inl.h:41
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:60
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: UGmodel_inl.h:38
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
virtual const Potential< GUM_SCALAR > & factor(const NodeSet &varIds) const =0
Returns the factor of a set of variable.
+ 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::UGmodel::size ( ) const
finalvirtualinherited

Returns the number of variables in this Directed Graphical Model.

Implements gum::GraphicalModel.

Definition at line 38 of file UGmodel_inl.h.

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

38 { return graph().size(); }
Size size() const
alias for sizeNodes
const UndiGraph & graph() const
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:35
+ Here is the call graph for this function:

◆ sizeEdges()

INLINE Size gum::UGmodel::sizeEdges ( ) const
inherited

Returns the number of arcs in this Directed Graphical Model.

Definition at line 41 of file UGmodel_inl.h.

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

41 { return graph_.sizeEdges(); }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
Size sizeEdges() const
indicates the number of edges stored within the EdgeGraphPart
+ Here is the call graph for this function:

◆ smallestFactorFromNode() [1/2]

template<typename GUM_SCALAR >
virtual const NodeSet& gum::IMarkovNet< GUM_SCALAR >::smallestFactorFromNode ( NodeId  node) const
pure virtual

Returns the smallest factor that contains this variable.

Exceptions
NotFoundIf no variable's id matches varId.

◆ smallestFactorFromNode() [2/2]

template<typename GUM_SCALAR >
INLINE const NodeSet & gum::IMarkovNet< GUM_SCALAR >::smallestFactorFromNode ( const std::string &  name) const
virtual

Returns the smallest factor that contains this variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 301 of file IMarkovNet_tpl.h.

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

301  {
302  try {
303  return smallestFactorFromNode(idFromName(name));
304  } catch (NotFound) { GUM_ERROR(NotFound, "No factor containing the variable <" << name << ">") }
305  }
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
virtual const NodeSet & smallestFactorFromNode(NodeId node) const =0
Returns the smallest factor that contains this variable.
#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::IMarkovNet< GUM_SCALAR >::toDot ( ) const
virtual
Returns
Returns a dot representation of this IMarkovNet.

Definition at line 149 of file IMarkovNet_tpl.h.

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

149  {
150  std::stringstream output;
151  output << "graph \"";
152 
153  std::string mn_name;
154 
155  try {
156  mn_name = this->property("name");
157  } catch (NotFound&) { mn_name = "no_name"; }
158 
159  output << mn_name << "\" {" << std::endl;
160  output << " graph [bgcolor=transparent,label=\"" << mn_name << "\"];" << std::endl;
161  output << " node [style=filled fillcolor=\"#ffffaa\"];" << std::endl << std::endl;
162 
163  for (auto node: nodes())
164  output << " \"" << variable(node).name() << "\" [comment=\"" << node << ":"
165  << variable(node).toStringWithDescription() << "\"];" << std::endl;
166 
167  output << std::endl;
168 
169  std::string tab = " ";
170 
171  for (auto node: nodes()) {
172  if (neighbours(node).size() > 0) {
173  for (auto nei: neighbours(node)) {
174  if (variable(node).name() < variable(nei).name()) {
175  output << tab << "\"" << variable(node).name() << "\" -- "
176  << "\"" << variable(nei).name() << "\";" << std::endl;
177  }
178  }
179  } else {
180  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
181  }
182  }
183 
184  output << "}" << std::endl;
185 
186  return output.str();
187  }
const NodeSet & neighbours(const NodeId id) const
returns the neighbours of a node as set of nodes
Definition: UGmodel_inl.h:53
std::string toStringWithDescription() const
string version of *this using description attribute instead of name.
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 final
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:60
const std::string & name() const
returns the name of the variable
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: UGmodel_inl.h:38
+ Here is the call graph for this function:

◆ toDotAsFactorGraph()

template<typename GUM_SCALAR >
std::string gum::IMarkovNet< GUM_SCALAR >::toDotAsFactorGraph ( ) const
virtual
Returns
Returns a dot representation of this IMarkovNet.

Definition at line 191 of file IMarkovNet_tpl.h.

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

191  {
192  std::stringstream output;
193  std::string mn_name;
194  try {
195  mn_name = this->property("name");
196  } catch (NotFound&) { mn_name = "no_name"; }
197 
198  output << "graph FG_" << mn_name << " {" << std::endl;
199  output << " layout=neato;" << std::endl;
200  output << " graph [bgcolor=transparent,label=\"factor graph for " << mn_name << "\"];"
201  << std::endl;
202 
203  // the variables
204  output << " node [shape=rectangle,margin=0.04,width=0,height=0, "
205  "style=filled,color=\"coral\"];"
206  << std::endl;
207  for (auto nod: nodes()) {
208  output << "\"" << variable(nod).name() << "\";" << std::endl;
209  }
210  output << std::endl;
211 
212  // the factor
213  output << "node[shape = point,width = 0.1,height = 0.1,style = filled,color = "
214  "\"burlywood\"];"
215  << std::endl;
216  for (const auto& kv: factors()) {
217  output << " \"f";
218  for (NodeId nod: kv.first) {
219  output << "#" << variable(nod).name();
220  }
221  output << "\";" << std::endl;
222  }
223 
224  // the link variable--factors
225  output << " edge[len = 0.7];" << std::endl;
226  for (const auto& kv: factors()) {
227  std::string clicname = "\"f";
228  for (NodeId nod: kv.first) {
229  clicname += "#";
230  clicname += variable(nod).name();
231  }
232  clicname += "\"";
233 
234  for (NodeId nod: kv.first)
235  output << " " << clicname << " -- \"" << variable(nod).name() << "\";" << std::endl;
236  }
237  output << "}" << std::endl;
238 
239  return output.str();
240  }
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:60
const std::string & name() const
returns the name of the variable
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ toString()

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

Definition at line 128 of file IMarkovNet_tpl.h.

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

128  {
129  Size param = 0;
130  double dSize = log10DomainSize();
131 
132  for (auto factor: factors())
133  param += factor.second->content()->realSize();
134 
135  std::stringstream s;
136  s << "MN{nodes: " << size() << ", edges: " << graph().sizeEdges() << ", ";
137 
138  if (dSize > 6)
139  s << "domainSize: 10^" << dSize;
140  else
141  s << "domainSize: " << std::round(std::pow(10.0, dSize));
142 
143  s << ", dim: " << param << "}";
144 
145  return s.str();
146  }
double log10DomainSize() const
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
const UndiGraph & graph() const
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:35
Size sizeEdges() const
indicates the number of edges stored within the EdgeGraphPart
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: UGmodel_inl.h:38
virtual const Potential< GUM_SCALAR > & factor(const NodeSet &varIds) const =0
Returns the factor of a set of variable.
+ Here is the call graph for this function:

◆ variable()

template<typename GUM_SCALAR >
virtual const DiscreteVariable& gum::IMarkovNet< GUM_SCALAR >::variable ( NodeId  id) const
pure virtual

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

Exceptions
NotFoundIf no variable's id matches varId.

Implements gum::GraphicalModel.

◆ variableFromName()

template<typename GUM_SCALAR >
virtual const DiscreteVariable& gum::IMarkovNet< GUM_SCALAR >::variableFromName ( const std::string &  name) const
pure virtual

Getter by name.

Exceptions
NotFoundif no such name exists in the graph.

Implements gum::GraphicalModel.

◆ variableNodeMap()

template<typename GUM_SCALAR >
virtual const VariableNodeMap& gum::IMarkovNet< GUM_SCALAR >::variableNodeMap ( ) const
pure virtual

Returns a constant reference to the VariableNodeMap of thisBN.

Implements gum::GraphicalModel.

Member Data Documentation

◆ graph_

UndiGraph gum::UGmodel::graph_
protectedinherited

The DAG of this Directed Graphical Model.

Definition at line 154 of file UGmodel.h.


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