aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphicalModel.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 {
113  return exists(idFromName(name));
114  };
115 
116  /**
117  * transform a vector of NodeId in a vector of names
118  * @return the vector of names
119  */
120  std::vector< std::string > names(const std::vector< NodeId >& ids) const;
121 
122  /**
123  * transform a NodeSet in a vector of names
124  * @return the vector of names
125  */
126  std::vector< std::string > names(const NodeSet& ids) const;
127 
128  /**
129  * transform a vector of names into a vector of nodeId
130  * @return the vector of names
131  */
132  std::vector< NodeId > ids(const std::vector< std::string >& names) const;
133 
134  /**
135  * transform a vector of names into a NodeSet
136  * @return NodeSet
137  */
138  NodeSet nodeset(const std::vector< std::string >& names) const;
139 
140  virtual const NodeGraphPart& nodes() const = 0;
141 
142  /// Get an instantiation over all the variables of the model
144 
145  /**
146  * Returns a constant reference over a variable given it's node id.
147  * @throw NotFound If no variable's id matches varId.
148  */
149  virtual const DiscreteVariable& variable(NodeId id) const = 0;
150 
151  /**
152  * Return id node src discrete var pointer.
153  * @throw NotFound If no variable matches var.
154  */
155  virtual NodeId nodeId(const DiscreteVariable& var) const = 0;
156 
157  /// Getter by name
158  /// @throw NotFound if no such name exists in the graph.
159  virtual NodeId idFromName(const std::string& name) const = 0;
160 
161  /// Getter by name
162  /// @throw NotFound if no such name exists in the graph.
163  virtual const DiscreteVariable&
164  variableFromName(const std::string& name) const = 0;
165 
166  /// @}
167 
168  double log10DomainSize() const;
169 
170  /** check if node X and node Y are independent given nodes Z
171  */
172  virtual bool isIndependent(NodeId X, NodeId Y, const NodeSet& Z) const = 0;
173 
174  /** check if nodes X and nodes Y are independent given nodes Z
175  */
176  virtual bool isIndependent(const NodeSet& X,
177  const NodeSet& Y,
178  const NodeSet& Z) const = 0;
179 
180  protected:
181  /**
182  * Private copy operator.
183  */
184  GraphicalModel& operator=(const GraphicalModel& source);
185 
186  private:
187  /// The properties of this Directed Graphical Model.
188  /// Initialized using a lazy instantiation.
190 
191  /// Return the properties of this Directed Graphical Model and initialize
192  /// the hash table is necessary.
193  HashTable< std::string, std::string >& properties__() const;
194  };
195 } // namespace gum
196 
197 #ifndef GUM_NO_INLINE
198 # include <agrum/tools/graphicalModels/graphicalModel_inl.h>
199 #endif /* GUM_NO_INLINE */
200 
201 #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:669
HashTable< std::string, std::string > * propertiesMap__
The properties of this Directed Graphical Model. Initialized using a lazy instantiation.
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.
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
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
HashTable< std::string, std::string > & properties__() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...