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

Class representing the minimal interface for Markov random field. More...

#include <agrum/BN/IMarkovRandomField.h>

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

Public Member Functions

bool operator== (const IMarkovRandomField< GUM_SCALAR > &from) const
 This operator compares 2 BNs !
bool operator!= (const IMarkovRandomField< GUM_SCALAR > &from) const
Size dim () const
 Returns the dimension (the number of free parameters) in this bayes net.
Size maxVarDomainSize () const
GUM_SCALAR minParam () const
GUM_SCALAR maxParam () const
GUM_SCALAR minNonZeroParam () const
GUM_SCALAR maxNonOneParam () const
virtual std::string toDot () const
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
NodeSet family (const NodeId id) const final
 returns the the node as a NodeSet (in mixed graph : family is the node and its parents
NodeSet family (const std::string &name) const final
double log10DomainSize () const
Constructors / Destructors
 IMarkovRandomField ()
 Default constructor.
 IMarkovRandomField (std::string name)
 Default constructor.
virtual ~IMarkovRandomField ()
 Destructor.
 IMarkovRandomField (const IMarkovRandomField< GUM_SCALAR > &source)
 Copy constructor.
IMarkovRandomField< GUM_SCALAR > & operator= (const IMarkovRandomField< GUM_SCALAR > &source)
 Copy operator.
Pure Virtual methods
virtual const Tensor< GUM_SCALAR > & factor (const NodeSet &varIds) const =0
 Returns the factor of a set of variable.
virtual const NodeSetsmallestFactorFromNode (NodeId node) const =0
 Returns the smallest factor that contains this variable.
virtual const NodeSetsmallestFactorFromNode (const std::string &name) const
 Returns the smallest factor that contains this variable.
virtual const FactorTable< GUM_SCALAR > & factors () const =0
 Returns the set of factors as a IMarkovRandomField::FactorTable.
virtual const VariableNodeMapvariableNodeMap () const =0
 Returns a constant reference to the VariableNodeMap of thisBN.
virtual const DiscreteVariablevariable (NodeId id) const =0
 Returns a constant reference over a variable given it's node id.
virtual NodeId nodeId (const DiscreteVariable &var) const =0
 Return id node from discrete var pointer.
virtual NodeId idFromName (const std::string &name) const =0
 Getter by name.
virtual const DiscreteVariablevariableFromName (const std::string &name) const =0
 Getter by name.
Variable manipulation methods.
const UndiGraphgraph () const
 Returns a constant reference to the dag of this Bayes Net.
virtual Size size () const final
 Returns the number of variables in this Directed Graphical Model.
Size sizeEdges () const
 Returns the number of arcs in this Directed Graphical Model.
const NodeGraphPartnodes () const final
 Returns a constant reference to the dag of this Bayes Net.
bool exists (NodeId node) const final
 Return true if this node exists in this graphical model.
bool exists (const std::string &name) const final
 Returns a constant reference to the dag of this Bayes Net.
Edge manipulation methods.
const EdgeSetedges () const
 return true if the edge node1-node2 exists in the UGModel
bool existsEdge (const NodeId node1, const NodeId node2) const
 return true if the edge node1-node2 exists in the UGModel
bool existsEdge (const std::string &name1, const std::string &name2) const
 return true if the edge node1-node2 exists in the UGModel
const NodeSetneighbours (const NodeId id) const
 returns the neighbours of a node as set of nodes
const NodeSetneighbours (const std::string &name) const
 return true if the edge node1-node2 exists in the UGModel
bool isIndependent (NodeId X, NodeId Y, const NodeSet &Z) const final
 check if X and Y are independent given Z
bool isIndependent (const NodeSet &X, const NodeSet &Y, const NodeSet &Z) const final
 check if nodes X and nodes Y are independent given nodes Z
bool isIndependent (const std::string &Xname, const std::string &Yname, const std::vector< std::string > &Znames) const
 return true if the edge node1-node2 exists in the UGModel
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
Getter and setters
const std::string & property (const std::string &name) const
 Return the value of the property name of this GraphicalModel.
const std::string & propertyWithDefault (const std::string &name, const std::string &byDefault) const
 Return the value of the property name of this GraphicalModel.
void setProperty (const std::string &name, const std::string &value)
 Add or change a property of this GraphicalModel.
std::vector< std::string > properties () const
 List of all the properties.
Variable manipulation methods.
virtual bool empty () const
 Return true if this graphical model is empty.
std::vector< std::string > names (const std::vector< NodeId > &ids) const
 transform a vector of NodeId in a vector of names
std::vector< std::string > names (const NodeSet &ids) const
 transform a NodeSet in a vector of names
std::vector< NodeIdids (const std::vector< std::string > &names) const
 transform a vector of names into a vector of nodeId
NodeSet nodeset (const std::vector< std::string > &names) const
 transform a vector of names into a NodeSet
gum::VariableSet variables (const std::vector< std::string > &l) const
 transform a vector of names into a VariableeSet
gum::VariableSet variables (const NodeSet &ids) const
 transform a vector of NodeId into a VariableeSet
Instantiation completeInstantiation () const
 Get an instantiation over all the variables of the model.

Static Public Member Functions

static void spaceCplxToStream (std::stringstream &s, double dSize, int dim, Size usedMem)
 send to the stream the space complexity with 3 parametrs

Protected Attributes

UndiGraph graph_
 The DAG of this Directed Graphical Model.

Private Member Functions

void _minimalCondSetVisit_ (NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisited) const
const HashTable< std::string, std::string > & _properties_ () const
 Return the properties of this Directed Graphical Model.

Private Attributes

HashTable< std::string, std::string > _propertiesMap_
 The properties of this Directed Graphical Model.

Detailed Description

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

Class representing the minimal interface for Markov random field.

This class is used as a base class for different versions of Markov random fields. 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 MRF is needed.

Definition at line 75 of file IMarkovRandomField.h.

Constructor & Destructor Documentation

◆ IMarkovRandomField() [1/3]

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

Default constructor.

Definition at line 62 of file IMarkovRandomField_tpl.h.

62 : UGmodel() {
64 }
Class representing the minimal interface for Markov random field.
IMarkovRandomField()
Default constructor.
UGmodel()
Default constructor.
Definition UGmodel.cpp:49

References IMarkovRandomField(), and gum::UGmodel::UGmodel().

Referenced by IMarkovRandomField(), IMarkovRandomField(), IMarkovRandomField(), ~IMarkovRandomField(), operator!=(), operator=(), operator==(), and variableFromName().

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

◆ IMarkovRandomField() [2/3]

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

Default constructor.

Definition at line 67 of file IMarkovRandomField_tpl.h.

67 : UGmodel() {
69 this->setProperty("name", name);
70 }
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.

References IMarkovRandomField(), gum::UGmodel::UGmodel(), and gum::GraphicalModel::setProperty().

Here is the call graph for this function:

◆ ~IMarkovRandomField()

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

Destructor.

Definition at line 87 of file IMarkovRandomField_tpl.h.

References IMarkovRandomField().

Here is the call graph for this function:

◆ IMarkovRandomField() [3/3]

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

Copy constructor.

Definition at line 73 of file IMarkovRandomField_tpl.h.

References IMarkovRandomField(), and gum::UGmodel::UGmodel().

Here is the call graph for this function:

Member Function Documentation

◆ _minimalCondSetVisit_()

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

Definition at line 336 of file IMarkovRandomField_tpl.h.

339 {
340 if (alreadyVisited.contains(node)) return;
342
343 if (soids.contains(node)) {
344 minimal << node;
345 } else {
346 for (auto neig: graph_.neighbours(node))
348 }
349 }
void _minimalCondSetVisit_(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisited) const
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition UGmodel.h:182

References _minimalCondSetVisit_(), gum::Set< Key >::contains(), and gum::UGmodel::graph_.

Referenced by _minimalCondSetVisit_(), and minimalCondSet().

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

◆ _properties_()

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

Return the properties of this Directed Graphical Model.

Definition at line 70 of file graphicalModel_inl.h.

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

References _propertiesMap_.

Referenced by property().

Here is the caller graph for this function:

◆ completeInstantiation()

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

Get an instantiation over all the variables of the model.

Definition at line 106 of file graphicalModel_inl.h.

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

References nodes().

Here is the call graph for this function:

◆ dim()

template<typename GUM_SCALAR>
INLINE Size gum::IMarkovRandomField< 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 92 of file IMarkovRandomField_tpl.h.

92 {
93 Size res = 0;
94 for (auto f: factors()) {
95 res += f.second->domainSize();
96 }
97 return res;
98 }
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovRandomField::FactorTable.

References factors().

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

65{ return graph_.edges(); }

References graph_.

Referenced by hasSameStructure().

Here is the caller graph for this function:

◆ empty()

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

Return true if this graphical model is empty.

Definition at line 116 of file graphicalModel_inl.h.

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

References size().

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

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

◆ exists() [1/2]

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

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

Implements gum::GraphicalModel.

Definition at line 83 of file UGmodel_inl.h.

83 {
84 try {
85 return exists(idFromName(name));
86 } catch ([[maybe_unused]] gum::NotFound& e) { return false; }
87 }
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.
Definition UGmodel_inl.h:81

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

Here is the call graph for this function:

◆ exists() [2/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 81 of file UGmodel_inl.h.

81{ return graph_.exists(node); }

References graph_.

Referenced by exists().

Here is the caller graph for this function:

◆ 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 67 of file UGmodel_inl.h.

67 {
68 return graph_.existsEdge(node1, node2);
69 }

References graph_.

Referenced by existsEdge().

Here is the caller 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 71 of file UGmodel_inl.h.

71 {
72 return existsEdge(idFromName(name1), idFromName(name2));
73 }
bool existsEdge(const NodeId node1, const NodeId node2) const
return true if the edge node1-node2 exists in the UGModel
Definition UGmodel_inl.h:67

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

Here is the call graph for this function:

◆ factor()

template<typename GUM_SCALAR>
virtual const Tensor< GUM_SCALAR > & gum::IMarkovRandomField< 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.

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

Here is the caller graph for this function:

◆ factors()

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

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

Referenced by dim(), maxNonOneParam(), maxParam(), minNonZeroParam(), minParam(), operator==(), toDotAsFactorGraph(), and toString().

Here is the caller graph for this function:

◆ family() [1/2]

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

returns the the node as a NodeSet (in mixed graph : family is the node and its parents

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

Implements gum::GraphicalModel.

Definition at line 99 of file UGmodel_inl.h.

99 {
100 // family is the set of the note and its parent. So for a completely undirected model, it is
101 // just the node.
102 return gum::NodeSet{id};
103 }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...

Referenced by family().

Here is the caller graph for this function:

◆ family() [2/2]

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

Implements gum::GraphicalModel.

Definition at line 105 of file UGmodel_inl.h.

105{ return family(idFromName(name)); }
NodeSet family(const NodeId id) const final
returns the the node as a NodeSet (in mixed graph : family is the node and its parents
Definition UGmodel_inl.h:99

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

Here is the call graph for this function:

◆ graph()

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

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

Definition at line 57 of file UGmodel_inl.h.

57{ return graph_; }

References graph_.

Referenced by gum::MarginalTargetedMRFInference< GUM_SCALAR >::MarginalTargetedMRFInference(), isIndependent(), isIndependent(), size(), and gum::IMarkovRandomField< GUM_SCALAR >::toString().

Here is the caller 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 70 of file UGmodel.cpp.

70 {
71 if (this == &other) return true;
72
73 if (size() != other.size()) return false;
74
75 if (sizeEdges() != other.sizeEdges()) return false;
76
77 for (const auto& nid: nodes()) {
78 try {
79 other.idFromName(variable(nid).name());
80 } catch (NotFound const&) { return false; }
81 }
82
83 for (const auto& edge: edges()) {
84 if (!other.edges().exists(Edge(other.idFromName(variable(edge.first()).name()),
85 other.idFromName(variable(edge.second()).name()))))
86 return false;
87 }
88
89 return true;
90 }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition UGmodel_inl.h:89
const EdgeSet & edges() const
return true if the edge node1-node2 exists in the UGModel
Definition UGmodel_inl.h:65
Size sizeEdges() const
Returns the number of arcs in this Directed Graphical Model.
Definition UGmodel_inl.h:63
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition UGmodel_inl.h:60

References UGmodel(), edges(), gum::Set< Key >::exists(), gum::GraphicalModel::idFromName(), nodes(), size(), sizeEdges(), and gum::GraphicalModel::variable().

Here is the call graph for this function:

◆ idFromName()

template<typename GUM_SCALAR>
virtual NodeId gum::IMarkovRandomField< 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.

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

Here is the caller graph for this function:

◆ ids()

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

transform a vector of names into a vector of nodeId

Returns
the vector of names

Definition at line 139 of file graphicalModel_inl.h.

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

References names().

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

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

◆ isIndependent() [1/4]

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

95 {
96 return !graph().hasUndirectedPath(X, Y, Z);
97 }
bool hasUndirectedPath(NodeId n1, 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:57

References graph(), and gum::EdgeGraphPart::hasUndirectedPath().

Here is the call graph for this function:

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

146 {
147 return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
148 };
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if X and Y are independent given Z
Definition UGmodel_inl.h:91

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

Here is the call graph for this function:

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

152 {
153 return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
154 };

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

Here is the call graph for this function:

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

91 {
92 return !graph().hasUndirectedPath(X, Y, Z);
93 }

References graph(), and gum::EdgeGraphPart::hasUndirectedPath().

Referenced by isIndependent(), and isIndependent().

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

◆ log10DomainSize()

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

Definition at line 95 of file graphicalModel_inl.h.

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

References nodes().

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

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

◆ maxNonOneParam()

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

Definition at line 141 of file IMarkovRandomField_tpl.h.

141 {
142 GUM_SCALAR res = 0.0;
143 for (auto elt: factors()) {
144 auto v = elt.second->maxNonOne();
145 if (v > res) { res = v; }
146 }
147 return res;
148 }

References factors().

Here is the call graph for this function:

◆ maxParam()

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

Definition at line 121 of file IMarkovRandomField_tpl.h.

121 {
122 GUM_SCALAR res = 1.0;
123 for (auto elt: factors()) {
124 auto v = elt.second->max();
125 if (v > res) { res = v; }
126 }
127 return res;
128 }

References factors().

Here is the call graph for this function:

◆ maxVarDomainSize()

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

Definition at line 101 of file IMarkovRandomField_tpl.h.

101 {
102 Size res = 0;
103 for (auto node: nodes()) {
104 auto v = variable(node).domainSize();
105 if (v > res) { res = v; }
106 }
107 return res;
108 }
virtual Size domainSize() const =0
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it's node id.

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

Here is the call graph for this function:

◆ minimalCondSet() [1/2]

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

Definition at line 366 of file IMarkovRandomField_tpl.h.

367 {
368 NodeSet res;
369 for (auto node: targets) {
371 }
372 return res;
373 }
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const

References minimalCondSet().

Here is the call graph for this function:

◆ minimalCondSet() [2/2]

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

Definition at line 352 of file IMarkovRandomField_tpl.h.

353 {
354 if (soids.contains(target)) return NodeSet({target});
355
356 NodeSet res;
359
360 for (auto neig: graph_.neighbours(target))
362 return res;
363 }

References _minimalCondSetVisit_(), gum::Set< Key >::contains(), and gum::UGmodel::graph_.

Referenced by minimalCondSet().

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

◆ minNonZeroParam()

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

Definition at line 131 of file IMarkovRandomField_tpl.h.

131 {
132 GUM_SCALAR res = 1.0;
133 for (auto elt: factors()) {
134 auto v = elt.second->minNonZero();
135 if (v < res) { res = v; }
136 }
137 return res;
138 }

References factors().

Here is the call graph for this function:

◆ minParam()

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

Definition at line 111 of file IMarkovRandomField_tpl.h.

111 {
112 GUM_SCALAR res = 1.0;
113 for (auto elt: factors()) {
114 auto v = elt.second->min();
115 if (v < res) { res = v; }
116 }
117 return res;
118 }

References factors().

Here is the call graph for this function:

◆ names() [1/2]

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

transform a NodeSet in a vector of names

Returns
the vector of names

Definition at line 129 of file graphicalModel_inl.h.

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

◆ names() [2/2]

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

transform a vector of NodeId in a vector of names

Returns
the vector of names

Definition at line 119 of file graphicalModel_inl.h.

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

References ids(), and variableNodeMap().

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

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

◆ 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 75 of file UGmodel_inl.h.

75{ return graph_.neighbours(id); }

References graph_.

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

Here is the caller 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 77 of file UGmodel_inl.h.

77 {
78 return neighbours(idFromName(name));
79 }
const NodeSet & neighbours(const NodeId id) const
returns the neighbours of a node as set of nodes
Definition UGmodel_inl.h:75

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

Here is the call graph for this function:

◆ nodeId()

template<typename GUM_SCALAR>
virtual NodeId gum::IMarkovRandomField< 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 89 of file UGmodel_inl.h.

89{ return (NodeGraphPart&)graph_; }

References graph_.

Referenced by hasSameStructure(), gum::IMarkovRandomField< GUM_SCALAR >::maxVarDomainSize(), gum::IMarkovRandomField< GUM_SCALAR >::operator==(), gum::IMarkovRandomField< GUM_SCALAR >::toDot(), and gum::IMarkovRandomField< GUM_SCALAR >::toDotAsFactorGraph().

Here is the caller 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 60 of file graphicalModel.cpp.

60 {
61 NodeSet res;
62 for (const auto& name: names) {
63 res.insert(idFromName(name));
64 }
65 return res;
66 }

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

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

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

◆ operator!=()

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

Definition at line 313 of file IMarkovRandomField_tpl.h.

313 {
314 return !this->operator==(from);
315 }
bool operator==(const IMarkovRandomField< GUM_SCALAR > &from) const
This operator compares 2 BNs !

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

Here is the call graph for this function:

◆ operator=()

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

Copy operator.

Definition at line 80 of file IMarkovRandomField_tpl.h.

80 {
81 if (this != &source) { UGmodel::operator=(source); }
82
83 return *this;
84 }
UGmodel & operator=(const UGmodel &source)
Private copy operator.
Definition UGmodel.cpp:61

References IMarkovRandomField(), and gum::UGmodel::operator=().

Here is the call graph for this function:

◆ operator==()

template<typename GUM_SCALAR>
bool gum::IMarkovRandomField< GUM_SCALAR >::operator== ( const IMarkovRandomField< 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 263 of file IMarkovRandomField_tpl.h.

263 {
264 if (size() != from.size()) { return false; }
265
266 if (sizeEdges() != from.sizeEdges()) { return false; }
267
268 // alignment of variables between the 2 BNs
270
271 for (auto node: nodes()) {
272 try {
273 const auto& v1 = variable(node);
274 const auto& v2 = from.variableFromName(variable(node).name());
275 if (v1 != v2) return false;
276
278 } catch (NotFound const&) {
279 // a name is not found in from
280 return false;
281 }
282 }
283
284 for (const auto& elt: factors()) {
285 const auto& key = elt.first;
286 const auto& factor = *elt.second;
287
289 for (const auto n: key)
290 fromkey.insert(from.idFromName(variable(n).name()));
291
292 if (!from.factors().exists(fromkey)) { return false; }
293
294 const auto& fromfactor = from.factor(fromkey);
295
298 for (i.setFirst(); !i.end(); i.inc()) {
299 for (Idx indice = 0; indice < factor.nbrDim(); ++indice) {
300 const DiscreteVariable* p = &(i.variable(indice));
301 j.chgVal(*(alignment.second(p)), i.val(*p));
302 }
303
304 if (std::pow(factor.get(i) - fromfactor.get(j), (GUM_SCALAR)2) > (GUM_SCALAR)1e-6) {
305 return false;
306 }
307 }
308 }
309 return true;
310 }
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
virtual const Tensor< GUM_SCALAR > & factor(const NodeSet &varIds) const =0
Returns the factor of a set of variable.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.

References IMarkovRandomField(), gum::Instantiation::chgVal(), gum::Instantiation::end(), factor(), factors(), idFromName(), gum::Instantiation::inc(), gum::BijectionImplementation< T1, T2, Gen >::insert(), gum::Set< Key >::insert(), gum::UGmodel::nodes(), gum::BijectionImplementation< T1, T2, Gen >::second(), gum::Instantiation::setFirst(), gum::UGmodel::size(), gum::UGmodel::sizeEdges(), gum::Instantiation::val(), variable(), gum::Instantiation::variable(), and variableFromName().

Here is the call graph for this function:

◆ properties()

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

List of all the properties.

Definition at line 81 of file graphicalModel_inl.h.

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

References _propertiesMap_.

◆ property()

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

Return the value of the property name of this GraphicalModel.

Exceptions
NotFoundRaised if no name property is found.

Definition at line 60 of file graphicalModel_inl.h.

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

References _properties_(), and GUM_ERROR.

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

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

◆ propertyWithDefault()

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

Return the value of the property name of this GraphicalModel.

return byDefault if the property name is not found

Definition at line 75 of file graphicalModel_inl.h.

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

References _propertiesMap_.

◆ setProperty()

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

Add or change a property of this GraphicalModel.

Definition at line 89 of file graphicalModel_inl.h.

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

References _propertiesMap_.

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

Here is the caller graph for this function:

◆ size()

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

Returns the number of variables in this Directed Graphical Model.

Implements gum::GraphicalModel.

Definition at line 60 of file UGmodel_inl.h.

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

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

Referenced by hasSameStructure(), gum::IMarkovRandomField< GUM_SCALAR >::operator==(), gum::IMarkovRandomField< GUM_SCALAR >::toDot(), and gum::IMarkovRandomField< GUM_SCALAR >::toString().

Here is the call graph for this function:
Here is the caller 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 63 of file UGmodel_inl.h.

63{ return graph_.sizeEdges(); }

References graph_.

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

Here is the caller graph for this function:

◆ smallestFactorFromNode() [1/2]

template<typename GUM_SCALAR>
INLINE const NodeSet & gum::IMarkovRandomField< 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 326 of file IMarkovRandomField_tpl.h.

326 {
327 try {
329 } catch (NotFound const&) {
330 GUM_ERROR(NotFound, "No factor containing the variable <" << name << ">")
331 }
332 }
virtual const NodeSet & smallestFactorFromNode(NodeId node) const =0
Returns the smallest factor that contains this variable.

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

Here is the call graph for this function:

◆ smallestFactorFromNode() [2/2]

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

Returns the smallest factor that contains this variable.

Exceptions
NotFoundIf no variable's id matches varId.

Referenced by smallestFactorFromNode().

Here is the caller graph for this function:

◆ spaceCplxToStream()

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

send to the stream the space complexity with 3 parametrs

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

Definition at line 69 of file graphicalModel.cpp.

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

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

Here is the caller graph for this function:

◆ toDot()

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

Definition at line 170 of file IMarkovRandomField_tpl.h.

170 {
172 output << "graph \"";
173
175
176 try {
177 mn_name = this->property("name");
178 } catch (NotFound const&) { mn_name = "no_name"; }
179
180 output << mn_name << "\" {" << std::endl;
181 output << " graph [bgcolor=transparent,label=\"" << mn_name << "\"];" << std::endl;
182 output << " node [style=filled fillcolor=\"#ffffaa\"];" << std::endl << std::endl;
183
184 for (auto node: nodes())
185 output << " \"" << variable(node).name() << "\" [comment=\"" << node << ":"
187
188 output << std::endl;
189
190 std::string tab = " ";
191
192 for (auto node: nodes()) {
193 if (neighbours(node).size() > 0) {
194 for (auto nei: neighbours(node)) {
195 if (variable(node).name() < variable(nei).name()) {
196 output << tab << "\"" << variable(node).name() << "\" -- "
197 << "\"" << variable(nei).name() << "\";" << std::endl;
198 }
199 }
200 } else {
201 output << tab << "\"" << variable(node).name() << "\";" << std::endl;
202 }
203 }
204
205 output << "}" << std::endl;
206
207 return output.str();
208 }
std::string toStringWithDescription() const
string version of *this using description attribute instead of name.
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.
const std::string & name() const
returns the name of the variable

References gum::UGmodel::neighbours(), gum::UGmodel::nodes(), gum::GraphicalModel::property(), gum::UGmodel::size(), and variable().

Here is the call graph for this function:

◆ toDotAsFactorGraph()

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

Definition at line 211 of file IMarkovRandomField_tpl.h.

211 {
214 try {
215 mn_name = this->property("name");
216 } catch (NotFound const&) { mn_name = "no_name"; }
217
218 output << "graph FG_" << mn_name << " {" << std::endl;
219 output << " layout=neato;" << std::endl;
220 output << " graph [bgcolor=transparent,label=\"factor graph for " << mn_name << "\"];"
221 << std::endl;
222
223 // the variables
224 output << " node [shape=rectangle,margin=0.04,width=0,height=0, "
225 "style=filled,color=\"coral\"];"
226 << std::endl;
227 for (auto nod: nodes()) {
228 output << "\"" << variable(nod).name() << "\";" << std::endl;
229 }
230 output << std::endl;
231
232 // the factor
233 output << "node[shape = point,width = 0.1,height = 0.1,style = filled,color = "
234 "\"burlywood\"];"
235 << std::endl;
236 for (const auto& kv: factors()) {
237 output << " \"f";
238 for (NodeId nod: kv.first) {
239 output << "#" << variable(nod).name();
240 }
241 output << "\";" << std::endl;
242 }
243
244 // the link variable--factors
245 output << " edge[len = 0.7];" << std::endl;
246 for (const auto& kv: factors()) {
247 std::string clicname = "\"f";
248 for (NodeId nod: kv.first) {
249 clicname += "#";
251 }
252 clicname += "\"";
253
254 for (NodeId nod: kv.first)
255 output << " " << clicname << " -- \"" << variable(nod).name() << "\";" << std::endl;
256 }
257 output << "}" << std::endl;
258
259 return output.str();
260 }

References factors(), gum::UGmodel::nodes(), gum::GraphicalModel::property(), and variable().

Here is the call graph for this function:

◆ toString()

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

Definition at line 151 of file IMarkovRandomField_tpl.h.

151 {
152 Size param = 0;
153 double dSize = log10DomainSize();
154
155 for (auto factor: factors())
156 param += factor.second->content()->realSize();
157
159 s << "MRF{nodes: " << size() << ", edges: " << graph().sizeEdges() << ", ";
160
161 if (dSize > 6) s << "domainSize: 10^" << dSize;
162 else s << "domainSize: " << std::round(std::pow(10.0, dSize));
163
164 s << ", dim: " << param << "}";
165
166 return s.str();
167 }
Size sizeEdges() const
indicates the number of edges stored within the EdgeGraphPart
double log10DomainSize() const

References factor(), factors(), gum::UGmodel::graph(), gum::GraphicalModel::log10DomainSize(), and gum::UGmodel::size().

Referenced by gum::operator<<().

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

◆ variable()

template<typename GUM_SCALAR>
virtual const DiscreteVariable & gum::IMarkovRandomField< 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.

Referenced by maxVarDomainSize(), operator==(), toDot(), and toDotAsFactorGraph().

Here is the caller graph for this function:

◆ variableFromName()

template<typename GUM_SCALAR>
virtual const DiscreteVariable & gum::IMarkovRandomField< 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.

References IMarkovRandomField().

Referenced by operator==().

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

◆ variableNodeMap()

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

Returns a constant reference to the VariableNodeMap of thisBN.

Implements gum::GraphicalModel.

◆ variables() [1/2]

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

transform a vector of NodeId into a VariableeSet

Returns
NodeSet

Definition at line 160 of file graphicalModel_inl.h.

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

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

Here is the call graph for this function:

◆ variables() [2/2]

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

transform a vector of names into a VariableeSet

Returns
NodeSet

Definition at line 150 of file graphicalModel_inl.h.

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

References variableNodeMap().

Here is the call graph for this function:

Member Data Documentation

◆ _propertiesMap_

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

The properties of this Directed Graphical Model.

Definition at line 236 of file graphicalModel.h.

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

◆ graph_

UndiGraph gum::UGmodel::graph_
protectedinherited

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