aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
graphicalModel.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Class representing probabilistic DAG model
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  *
28  */
29 #ifndef GUM_GRAPHICALMODEL_H
30 #define GUM_GRAPHICALMODEL_H
31 #include <agrum/agrum.h>
32 #include <agrum/tools/multidim/instantiation.h>
33 
34 #include <agrum/tools/graphs/DAG.h>
35 #include <agrum/tools/graphs/undiGraph.h>
36 
37 #include <agrum/tools/graphicalModels/variableNodeMap.h>
38 
39 namespace gum {
40 
41  /**
42  * @class GraphicalModel
43  * @headerfile GraphicalModel.h <agrum/tools/graphicalModels/GraphicalModel.h>
44  * Virtual base class for probabilistic graphical models
45  *
46  */
48  public:
49  /// @name Constructors / Destructors
50  /// @{
51 
52  /**
53  * Default constructor.
54  */
56 
57  /**
58  * Destructor.
59  */
60  virtual ~GraphicalModel();
61 
62  /**
63  * Copy constructor. Do nothing.
64  */
65  GraphicalModel(const GraphicalModel& source);
66 
67  /// @}
68  /// @name Getter and setters
69  /// @{
70 
71  /**
72  * Return the value of the property name of this GraphicalModel.
73  * @throw NotFound Raised if no name property is found.
74  */
75  const std::string& property(const std::string& name) const;
76 
77  /**
78  * Return the value of the property name of this GraphicalModel.
79  * return byDefault if the property name is not found
80  */
81  const std::string& propertyWithDefault(const std::string& name,
82  const std::string& byDefault) const;
83 
84  /**
85  * Add or change a property of this GraphicalModel.
86  */
87  void setProperty(const std::string& name, const std::string& value);
88 
89  /// @}
90  /// @name Variable manipulation methods.
91  /// @{
92 
93  /**
94  * Returns a constant reference to the VariableNodeMap of this Graphical Model
95  */
96  virtual const VariableNodeMap& variableNodeMap() const = 0;
97 
98  /**
99  * Returns the number of variables in this Directed Graphical Model.
100  */
101  virtual Size size() const = 0;
102 
103  /**
104  * Return true if this graphical model is empty.
105  */
106  virtual bool empty() const;
107 
108  /**
109  * Return true if this node exists in this graphical model.
110  */
111  virtual bool exists(NodeId node) const = 0;
112  bool exists(const std::string& name) const { return exists(idFromName(name)); };
113 
114  /**
115  * transform a vector of NodeId in a vector of names
116  * @return the vector of names
117  */
118  std::vector< std::string > names(const std::vector< NodeId >& ids) const;
119 
120  /**
121  * transform a NodeSet in a vector of names
122  * @return the vector of names
123  */
124  std::vector< std::string > names(const NodeSet& ids) const;
125 
126  /**
127  * transform a vector of names into a vector of nodeId
128  * @return the vector of names
129  */
130  std::vector< NodeId > ids(const std::vector< std::string >& names) const;
131 
132  /**
133  * transform a vector of names into a NodeSet
134  * @return NodeSet
135  */
136  NodeSet nodeset(const std::vector< std::string >& names) const;
137 
138  virtual const NodeGraphPart& nodes() const = 0;
139 
140  /// Get an instantiation over all the variables of the model
142 
143  /**
144  * Returns a constant reference over a variable given it's node id.
145  * @throw NotFound If no variable's id matches varId.
146  */
147  virtual const DiscreteVariable& variable(NodeId id) const = 0;
148 
149  /**
150  * Return id node src discrete var pointer.
151  * @throw NotFound If no variable matches var.
152  */
153  virtual NodeId nodeId(const DiscreteVariable& var) const = 0;
154 
155  /// Getter by name
156  /// @throw NotFound if no such name exists in the graph.
157  virtual NodeId idFromName(const std::string& name) const = 0;
158 
159  /// Getter by name
160  /// @throw NotFound if no such name exists in the graph.
161  virtual const DiscreteVariable& variableFromName(const std::string& name) const = 0;
162 
163  /// @}
164 
165  double log10DomainSize() const;
166 
167  /** check if node X and node Y are independent given nodes Z
168  */
169  virtual bool isIndependent(NodeId X, NodeId Y, const NodeSet& Z) const = 0;
170 
171  /** check if nodes X and nodes Y are independent given nodes Z
172  */
173  virtual bool isIndependent(const NodeSet& X, const NodeSet& Y, const NodeSet& Z) const = 0;
174 
175  protected:
176  /**
177  * Private copy operator.
178  */
179  GraphicalModel& operator=(const GraphicalModel& source);
180 
181  private:
182  /// The properties of this Directed Graphical Model.
183  /// Initialized using a lazy instantiation.
185 
186  /// Return the properties of this Directed Graphical Model and initialize
187  /// the hash table is necessary.
188  HashTable< std::string, std::string >& _properties_() const;
189  };
190 } // namespace gum
191 
192 #ifndef GUM_NO_INLINE
193 # include <agrum/tools/graphicalModels/graphicalModel_inl.h>
194 #endif /* GUM_NO_INLINE */
195 
196 #endif /* GUM_GRAPHICALMODEL_H */
Virtual base class for probabilistic graphical models.
Instantiation completeInstantiation() const
Get an instantiation over all the variables of the model.
const std::string & propertyWithDefault(const std::string &name, const std::string &byDefault) const
Return the value of the property name of this GraphicalModel.
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.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual const NodeGraphPart & nodes() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
bool exists(const std::string &name) const
Return true if this graphical model is empty.
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 Size size() const =0
Returns the number of variables in this Directed Graphical Model.
HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
virtual bool empty() const
Return true if this graphical model is empty.
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
virtual ~GraphicalModel()
Destructor.
double log10DomainSize() const
HashTable< std::string, std::string > * _propertiesMap_
The properties of this Directed Graphical Model. Initialized using a lazy instantiation.
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node src discrete var pointer.
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
GraphicalModel & operator=(const GraphicalModel &source)
Private copy operator.
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.
GraphicalModel()
Default constructor.
GraphicalModel(const GraphicalModel &source)
Copy constructor.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.
std::vector< std::string > names(const NodeSet &ids) const
transform a NodeSet in a vector of names