aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
gum::UGmodel Class Referenceabstract

Virtual base class for PGMs using a undirected graph. More...

#include <agrum/tools/graphicalModels/UGmodel.h>

+ Inheritance diagram for gum::UGmodel:
+ Collaboration diagram for gum::UGmodel:

Public Member Functions

bool hasSameStructure (const UGmodel &other)
 check if nodes X and nodes Y are independent given nodes Z More...
 
double log10DomainSize () const
 
Constructors / Destructors
 UGmodel ()
 Default constructor. More...
 
virtual ~UGmodel ()
 Destructor. More...
 
 UGmodel (const UGmodel &source)
 Copy constructor. 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...
 
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...
 
Variable manipulation methods.
virtual const VariableNodeMapvariableNodeMap () const =0
 Returns a constant reference to the VariableNodeMap of this Graphical Model. More...
 
virtual bool empty () const
 Return true if this graphical model is empty. More...
 
bool exists (const std::string &name) 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...
 
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 src 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...
 

Protected Attributes

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

Protected Member Functions

UGmodeloperator= (const UGmodel &source)
 Private copy operator. More...
 

Detailed Description

Virtual base class for PGMs using a undirected graph.

Definition at line 46 of file UGmodel.h.

Constructor & Destructor Documentation

◆ UGmodel() [1/2]

gum::UGmodel::UGmodel ( )

Default constructor.

Definition at line 29 of file UGmodel.cpp.

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

29  {
30  GUM_CONSTRUCTOR(UGmodel);
31  ;
32  }
UGmodel()
Default constructor.
Definition: UGmodel.cpp:29
+ Here is the call graph for this function:

◆ ~UGmodel()

gum::UGmodel::~UGmodel ( )
virtual

Destructor.

Definition at line 36 of file UGmodel.cpp.

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

36  {
37  GUM_DESTRUCTOR(UGmodel);
38  ;
39  }
UGmodel()
Default constructor.
Definition: UGmodel.cpp:29
+ Here is the call graph for this function:

◆ UGmodel() [2/2]

gum::UGmodel::UGmodel ( const UGmodel source)

Copy constructor.

Do nothing.

Definition at line 34 of file UGmodel.cpp.

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

34 : graph_(from.graph_) { GUM_CONS_CPY(UGmodel); }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
UGmodel()
Default constructor.
Definition: UGmodel.cpp:29
+ Here is the call graph for this function:

Member Function Documentation

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

◆ edges()

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

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
finalvirtual

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

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

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:

◆ graph()

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

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)

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

virtual NodeId gum::GraphicalModel::idFromName ( const std::string &  name) const
pure virtualinherited

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

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
finalvirtual

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
inline

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
inline

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.

◆ 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

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

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

◆ nodes()

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

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

UGmodel & gum::UGmodel::operator= ( const UGmodel source)
protected

Private copy operator.

Definition at line 41 of file UGmodel.cpp.

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

41  {
42  if (this != &source) {
44  graph_ = source.graph_;
45  }
46 
47  return *this;
48  }
UndiGraph graph_
The DAG of this Directed Graphical Model.
Definition: UGmodel.h:154
GraphicalModel & operator=(const GraphicalModel &source)
Private copy operator.
+ 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
finalvirtual

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

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:

◆ variable()

virtual const DiscreteVariable& gum::GraphicalModel::variable ( NodeId  id) const
pure virtualinherited

◆ variableFromName()

virtual const DiscreteVariable& gum::GraphicalModel::variableFromName ( const std::string &  name) const
pure virtualinherited

◆ variableNodeMap()

Member Data Documentation

◆ graph_

UndiGraph gum::UGmodel::graph_
protected

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: