aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::GraphicalModel Class Referenceabstract

Virtual base class for probabilistic graphical models. More...

#include <graphicalModel.h>

Inheritance diagram for gum::GraphicalModel:
Collaboration diagram for gum::GraphicalModel:

Public Member Functions

double log10DomainSize () const
virtual bool isIndependent (NodeId X, NodeId Y, const NodeSet &Z) const =0
 check if node X and node Y are independent given nodes Z
virtual bool isIndependent (const NodeSet &X, const NodeSet &Y, const NodeSet &Z) const =0
 check if nodes X and nodes Y are independent given nodes Z
virtual NodeSet family (const NodeId id) const =0
 returns the family of a noe (parents or neighbours) of a node and the node
virtual NodeSet family (std::string_view name) const =0
Constructors / Destructors
 GraphicalModel ()
 Default constructor.
virtual ~GraphicalModel ()
 Destructor.
 GraphicalModel (const GraphicalModel &source)
 Copy constructor.
 GraphicalModel (GraphicalModel &&source) noexcept
 Move constructor.
Getter and setters
const std::string & property (std::string_view name) const
 Return the value of the property name of this GraphicalModel.
const std::string & propertyWithDefault (std::string_view name, const std::string &byDefault) const
 Return the value of the property name of this GraphicalModel.
void setProperty (std::string_view name, std::string_view value)
 Add or change a property of this GraphicalModel.
std::vector< std::string > properties () const
 List of all the names of property in the Graphical model.
bool existsProperty (std::string_view name) const
 check wether a property exists in this GraphicalModel
void updateMetaData ()
 update the meta data of this Graphical Model (version, creation date, last modification date) This method is called by the writers ONLY before writing the model to a file.
Variable manipulation methods.
virtual const VariableNodeMapvariableNodeMap () const =0
 Returns a constant reference to the VariableNodeMap of this Graphical Model.
virtual Size size () const =0
 Returns the number of variables in this Directed Graphical Model.
virtual bool empty () const
 Return true if this graphical model is empty.
virtual bool exists (NodeId node) const =0
 Return true if this node exists in this graphical model.
virtual bool exists (std::string_view name) const =0
 Returns the number of variables in this Directed Graphical Model.
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
virtual const NodeGraphPartnodes () const =0
 Returns the number of variables in this Directed Graphical Model.
Instantiation completeInstantiation () const
 Get an instantiation over all the variables of the model.
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 src discrete var pointer.
virtual NodeId idFromName (std::string_view name) const =0
 Getter by name.
virtual const DiscreteVariablevariableFromName (std::string_view name) const =0
 Getter by name.

Static Public Member Functions

static std::string spaceCplxToString (double dSize, int dim, Size usedMem)
 send to the stream the space complexity with 3 parametrs

Protected Member Functions

GraphicalModeloperator= (const GraphicalModel &source)
 Private copy operator.
GraphicalModeloperator= (GraphicalModel &&source) noexcept
 Move assignment operator.
void _nameNodes_ (NodeGraphPart &g) const
 Names every node of g using variable(id).name() for each node id in g.

Private Member Functions

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

Virtual base class for probabilistic graphical models.

Definition at line 64 of file graphicalModel.h.

Constructor & Destructor Documentation

◆ GraphicalModel() [1/3]

gum::GraphicalModel::GraphicalModel ( )

Default constructor.

Definition at line 51 of file graphicalModel.cpp.

51{ GUM_CONSTRUCTOR(GraphicalModel) }
GraphicalModel()
Default constructor.

References GraphicalModel().

Referenced by gum::DiscreteGraphicalModel::DiscreteGraphicalModel(), gum::DiscreteGraphicalModel::DiscreteGraphicalModel(), GraphicalModel(), GraphicalModel(), GraphicalModel(), ~GraphicalModel(), family(), operator=(), and operator=().

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

◆ ~GraphicalModel()

gum::GraphicalModel::~GraphicalModel ( )
virtual

Destructor.

Definition at line 63 of file graphicalModel.cpp.

63{ GUM_DESTRUCTOR(GraphicalModel); }

References GraphicalModel().

Here is the call graph for this function:

◆ GraphicalModel() [2/3]

gum::GraphicalModel::GraphicalModel ( const GraphicalModel & source)

Copy constructor.

Do nothing.

Definition at line 53 of file graphicalModel.cpp.

53 {
54 GUM_CONS_CPY(GraphicalModel)
55 _propertiesMap_ = from._propertiesMap_;
56 }
HashTable< std::string, std::string > _propertiesMap_
The properties of this Directed Graphical Model.

References GraphicalModel(), and _propertiesMap_.

Here is the call graph for this function:

◆ GraphicalModel() [3/3]

gum::GraphicalModel::GraphicalModel ( GraphicalModel && source)
noexcept

Move constructor.

Do nothing.

Definition at line 58 of file graphicalModel.cpp.

58 :
59 _propertiesMap_(std::move(from._propertiesMap_)) {
60 GUM_CONS_MOV(GraphicalModel);
61 }

References GraphicalModel(), and _propertiesMap_.

Here is the call graph for this function:

Member Function Documentation

◆ _nameNodes_()

INLINE void gum::GraphicalModel::_nameNodes_ ( NodeGraphPart & g) const
protected

Names every node of g using variable(id).name() for each node id in g.

Call this before returning any newly constructed graph from a model method.

Definition at line 175 of file graphicalModel_inl.h.

175 {
176 for (auto id: g)
177 g.setName(id, variable(id).name());
178 }
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it's node id.

References gum::NodeGraphPart::setName(), and variable().

Referenced by gum::DAGmodel::dag(), gum::UGmodel::graph(), gum::DAGmodel::moralGraph(), and gum::DAGmodel::moralizedAncestralGraph().

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
private

Return the properties of this Directed Graphical Model.

Definition at line 67 of file graphicalModel_inl.h.

67 {
68 return _propertiesMap_;
69 }

References _propertiesMap_.

Referenced by property().

Here is the caller graph for this function:

◆ completeInstantiation()

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

Get an instantiation over all the variables of the model.

Definition at line 104 of file graphicalModel_inl.h.

104 {
105 Instantiation I;
106
107 for (const auto node: nodes())
108 I << variable(node);
109
110 return I;
111 }
virtual const NodeGraphPart & nodes() const =0
Returns the number of variables in this Directed Graphical Model.

References nodes(), and variable().

Here is the call graph for this function:

◆ empty()

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

Return true if this graphical model is empty.

Definition at line 114 of file graphicalModel_inl.h.

114{ 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]

virtual bool gum::GraphicalModel::exists ( NodeId node) const
pure virtual

Return true if this node exists in this graphical model.

Implemented in gum::DAGmodel, and gum::UGmodel.

◆ exists() [2/2]

virtual bool gum::GraphicalModel::exists ( std::string_view name) const
pure virtual

Returns the number of variables in this Directed Graphical Model.

Implemented in gum::DAGmodel, and gum::UGmodel.

References ids(), and names().

Here is the call graph for this function:

◆ existsProperty()

INLINE bool gum::GraphicalModel::existsProperty ( std::string_view name) const

check wether a property exists in this GraphicalModel

Definition at line 170 of file graphicalModel_inl.h.

170 {
171 return _propertiesMap_.exists(name);
172 }

References _propertiesMap_.

◆ family() [1/2]

virtual NodeSet gum::GraphicalModel::family ( const NodeId id) const
pure virtual

returns the family of a noe (parents or neighbours) of a node and the node

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

Implemented in gum::DAGmodel, and gum::UGmodel.

◆ family() [2/2]

virtual NodeSet gum::GraphicalModel::family ( std::string_view name) const
pure virtual

Implemented in gum::DAGmodel, and gum::UGmodel.

References GraphicalModel().

Here is the call graph for this function:

◆ idFromName()

virtual NodeId gum::GraphicalModel::idFromName ( std::string_view name) const
pure virtual

Getter by name.

Exceptions
NotFoundif no such name exists in the graph.

Implemented in gum::BayesNet< GUM_SCALAR >, gum::BayesNet< double >, gum::BayesNetFragment< GUM_SCALAR >, gum::DiscreteGraphicalModel, gum::InfluenceDiagram< GUM_SCALAR >, gum::prm::ClassBayesNet< GUM_SCALAR >, and gum::prm::InstanceBayesNet< GUM_SCALAR >.

Referenced by nodeset().

Here is the caller graph for this function:

◆ ids()

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

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(), and variableNodeMap().

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

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

◆ isIndependent() [1/2]

virtual bool gum::GraphicalModel::isIndependent ( const NodeSet & X,
const NodeSet & Y,
const NodeSet & Z ) const
pure virtual

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

Implemented in gum::DAGmodel, and gum::UGmodel.

◆ isIndependent() [2/2]

virtual bool gum::GraphicalModel::isIndependent ( NodeId X,
NodeId Y,
const NodeSet & Z ) const
pure virtual

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

Implemented in gum::DAGmodel, and gum::UGmodel.

◆ log10DomainSize()

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

Definition at line 93 of file graphicalModel_inl.h.

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

References nodes().

Referenced by 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:

◆ names() [1/2]

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

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

References ids(), gum::VariableNodeMap::name(), and variableNodeMap().

Here is the call graph for this function:

◆ names() [2/2]

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

transform a vector of NodeId in a vector of names

Returns
the vector of names

Definition at line 117 of file graphicalModel_inl.h.

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

References ids().

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:

◆ nodeId()

virtual NodeId gum::GraphicalModel::nodeId ( const DiscreteVariable & var) const
pure virtual

◆ nodes()

virtual const NodeGraphPart & gum::GraphicalModel::nodes ( ) const
pure virtual

Returns the number of variables in this Directed Graphical Model.

Implemented in gum::DAGmodel, and gum::UGmodel.

Referenced by completeInstantiation(), and log10DomainSize().

Here is the caller graph for this function:

◆ nodeset()

NodeSet gum::GraphicalModel::nodeset ( const std::vector< std::string > & names) const

transform a vector of names into a NodeSet

Returns
NodeSet

Definition at line 102 of file graphicalModel.cpp.

102 {
103 NodeSet res;
104 for (const auto& name: names) {
105 res.insert(idFromName(name));
106 }
107 return res;
108 }
virtual NodeId idFromName(std::string_view name) const =0
Getter by name.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...

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

Referenced by gum::BayesNet< double >::ancestors(), gum::DAGmodel::children(), gum::DAGmodel::isIndependent(), gum::UGmodel::isIndependent(), gum::UGmodel::isIndependent(), gum::DAGmodel::minimalCondSet(), and gum::DAGmodel::moralizedAncestralGraph().

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

◆ operator=() [1/2]

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

Private copy operator.

Definition at line 65 of file graphicalModel.cpp.

65 {
66 if (this != &source) {
67 _propertiesMap_ = source._propertiesMap_;
68 GUM_OP_CPY(GraphicalModel);
69 }
70 return *this;
71 }

References GraphicalModel(), and _propertiesMap_.

Referenced by gum::DiscreteGraphicalModel::operator=(), and gum::DiscreteGraphicalModel::operator=().

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

◆ operator=() [2/2]

GraphicalModel & gum::GraphicalModel::operator= ( GraphicalModel && source)
protectednoexcept

Move assignment operator.

Definition at line 73 of file graphicalModel.cpp.

73 {
74 if (this != &source) {
75 _propertiesMap_ = std::move(source._propertiesMap_);
76 GUM_OP_MOV(GraphicalModel);
77 }
78 return *this;
79 }

References GraphicalModel(), and _propertiesMap_.

Here is the call graph for this function:

◆ properties()

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

List of all the names of property in the Graphical model.

Definition at line 79 of file graphicalModel_inl.h.

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

References _propertiesMap_.

◆ property()

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

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 auto p = _properties_().tryGet(name);
62 if (!p) { GUM_ERROR(NotFound, "The following property does not exists: " << name) }
63 return *p;
64 }
const HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model.
optional_ref< Val > tryGet(const Key &key)
Returns a pointer to the value associated with a given key, or nullptr if the key does not exist.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _properties_(), GUM_ERROR, and gum::HashTable< Key, Val >::tryGet().

Here is the call graph for this function:

◆ propertyWithDefault()

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

Return the value of the property name of this GraphicalModel.

return byDefault if the property name is not found

Definition at line 72 of file graphicalModel_inl.h.

73 {
74 auto p = _propertiesMap_.tryGet(name);
75 return p ? *p : byDefault;
76 }

References _propertiesMap_.

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

Here is the caller graph for this function:

◆ setProperty()

INLINE void gum::GraphicalModel::setProperty ( std::string_view name,
std::string_view value )

Add or change a property of this GraphicalModel.

Definition at line 87 of file graphicalModel_inl.h.

87 {
88 if (auto p = _propertiesMap_.tryGet(name)) *p = value;
89 else _propertiesMap_.insert(std::string(name), std::string(value));
90 }

References _propertiesMap_.

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

Here is the caller graph for this function:

◆ size()

virtual Size gum::GraphicalModel::size ( ) const
pure virtual

Returns the number of variables in this Directed Graphical Model.

Implemented in gum::DAGmodel, and gum::UGmodel.

Referenced by empty().

Here is the caller graph for this function:

◆ spaceCplxToString()

std::string gum::GraphicalModel::spaceCplxToString ( double dSize,
int dim,
Size usedMem )
static

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

110 {
111 std::string result;
112 if (dSize > 6) result = std::format("domainSize: 10^{:.6g}", dSize);
113 else result = std::format("domainSize: {}", (long long)std::round(std::pow(10.0, dSize)));
114
115 result += std::format(", dim: {}, mem: ", dim);
116
117 if (const Size go = usedMem / (1024 * 1024 * 1024); go > 0) result += std::format("{}Go ", go);
118 if (const Size mo = (usedMem / (1024 * 1024)) % 1024; mo > 0)
119 result += std::format("{}Mo ", mo);
120 if (const Size ko = (usedMem / 1024) % 1024; ko > 0) result += std::format("{}Ko ", ko);
121 result += std::format("{}o", usedMem % 1024);
122 return result;
123 }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

◆ updateMetaData()

void gum::GraphicalModel::updateMetaData ( )

update the meta data of this Graphical Model (version, creation date, last modification date) This method is called by the writers ONLY before writing the model to a file.

Definition at line 81 of file graphicalModel.cpp.

81 {
82 auto const time = std::chrono::time_point_cast< std::chrono::milliseconds >(
83 std::chrono::system_clock::now());
84 auto const currentdate = std::format("{:%Y-%m-%d %T}", time);
85
86 const std::string currentSoftware = "aGrUM " GUM_VERSION;
87 if (auto software = _propertiesMap_.tryGet("software")) {
88 *software = currentSoftware;
89 } else {
90 _propertiesMap_.insert("software", currentSoftware);
91 }
92
93 if (!_propertiesMap_.tryGet("creation")) { _propertiesMap_.insert("creation", currentdate); }
94
95 if (auto lastModification = _propertiesMap_.tryGet("lastModification")) {
96 *lastModification = currentdate;
97 } else {
98 _propertiesMap_.insert("lastModification", currentdate);
99 }
100 }

References _propertiesMap_.

◆ variable()

virtual const DiscreteVariable & gum::GraphicalModel::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.

Implemented in gum::BayesNet< GUM_SCALAR >, gum::BayesNet< double >, gum::BayesNetFragment< GUM_SCALAR >, gum::DiscreteGraphicalModel, gum::InfluenceDiagram< GUM_SCALAR >, gum::prm::ClassBayesNet< GUM_SCALAR >, and gum::prm::InstanceBayesNet< GUM_SCALAR >.

Referenced by _nameNodes_(), and completeInstantiation().

Here is the caller graph for this function:

◆ variableFromName()

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

◆ variableNodeMap()

virtual const VariableNodeMap & gum::GraphicalModel::variableNodeMap ( ) const
pure virtual

Returns a constant reference to the VariableNodeMap of this Graphical Model.

Implemented in gum::BayesNet< GUM_SCALAR >, gum::BayesNet< double >, gum::BayesNetFragment< GUM_SCALAR >, gum::DiscreteGraphicalModel, gum::InfluenceDiagram< GUM_SCALAR >, gum::prm::ClassBayesNet< GUM_SCALAR >, and gum::prm::InstanceBayesNet< GUM_SCALAR >.

Referenced by ids(), names(), and variables().

Here is the caller graph for this function:

◆ variables() [1/2]

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

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

◆ variables() [2/2]

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

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 gum::Set< Key >::insert(), gum::VariableNodeMap::variableFromName(), and variableNodeMap().

Here is the call graph for this function:

Member Data Documentation

◆ _propertiesMap_

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

The properties of this Directed Graphical Model.

Definition at line 262 of file graphicalModel.h.

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


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