aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
DAGmodel.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_DAGMODEL_H
30 #define GUM_DAGMODEL_H
31 
32 #include <agrum/agrum.h>
33 #include <agrum/tools/multidim/instantiation.h>
34 #include <agrum/tools/graphicalModels/graphicalModel.h>
35 
36 #include <agrum/tools/graphs/DAG.h>
37 #include <agrum/tools/graphs/undiGraph.h>
38 
39 namespace gum {
40 
41  /**
42  * @class DAGmodel
43  * @headerfile DAGmodel.h <agrum/tools/graphicalModels/DAGmodel.h>
44  * Virtual base class for PGMs using a DAG
45  *
46  */
47  class DAGmodel: public GraphicalModel {
48  public:
49  /// @name Constructors / Destructors
50  /// @{
51 
52  /**
53  * Default constructor.
54  */
55  DAGmodel();
56 
57  /**
58  * Destructor.
59  */
60  virtual ~DAGmodel();
61 
62  /**
63  * Copy constructor. Do nothing.
64  */
65  DAGmodel(const DAGmodel& source);
66 
67  /// @}
68 
69  /// @name Variable manipulation methods.
70  /// @{
71  /**
72  * Returns a constant reference to the dag of this Bayes Net.
73  */
74  const DAG& dag() const;
75 
76  /**
77  * Returns the number of variables in this Directed Graphical Model.
78  */
79  virtual Size size() const final;
80 
81  /**
82  * Returns the number of arcs in this Directed Graphical Model.
83  */
84  Size sizeArcs() const;
85 
86  const NodeGraphPart& nodes() const final;
87 
88  /**
89  * Return true if this node exists in this graphical model.
90  */
91  bool exists(NodeId node) const final;
92 
93  /// @}
94 
95  /// @name Arc manipulation methods.
96  /// @{
97  const ArcSet& arcs() const;
98 
99  /// return true if the arc tail->head exists in the DAGmodel
100  /**
101  *
102  * @param tail the nodeId (or the name) of the tail in tail->head
103  * @param head the nodeId (or the name) of the head in tail->head
104  * @return true if the arc exists
105  */
106  bool existsArc(const NodeId tail, const NodeId head) const;
107  bool existsArc(const std::string& nametail, const std::string& namehead) const;
108 
109  /// returns the set of nodes with arc ingoing to a given node
110  /** Note that the set of nodes returned may be empty if no arc within the
111  * ArcGraphPart is ingoing into the given node.
112  * @param id the node which is the head of an arc with the returned nodes
113  * @param name the name of the node the node which is the head of an arc with
114  * the returned nodes*/
115  const NodeSet& parents(const NodeId id) const;
116  const NodeSet& parents(const std::string& name) const;
117 
118  /// returns the parents of a set of nodes
119  NodeSet parents(const NodeSet& ids) const;
120  NodeSet parents(const std::vector< std::string >& names) const;
121 
122  /// returns the parents of a node and the node
123  /** Note that the set of nodes returned may be empty if no arc within the
124  * ArcGraphPart is ingoing into the given node.
125  * @param id the node which is the head of an arc with the returned nodes
126  * @param name the name of the node the node which is the head of an arc with
127  * the returned nodes*/
128  NodeSet family(const NodeId id) const;
129  NodeSet family(const std::string& name) const;
130 
131  /// returns the set of nodes with arc outgoing from a given node
132  /** Note that the set of nodes returned may be empty if no node
133  * is outgoing from the given node.
134  * @param id the node which is the tail of an arc with the returned nodes
135  * @param name the name of the node which is the tail of an arc with the
136  * returned nodes */
137  const NodeSet& children(const NodeId id) const;
138  const NodeSet& children(const std::string& name) const;
139 
140  /// returns the children of a set of nodes
141  NodeSet children(const NodeSet& ids) const;
142  NodeSet children(const std::vector< std::string >& names) const;
143 
144  /// returns the set of nodes with directed path outgoing from a given node
145  /** Note that the set of nodes returned may be empty if no path within the
146  * ArcGraphPart is outgoing from the given node.
147  * @param id the node which is the tail of a directed path with the returned
148  * nodes
149  * @param name the name of the node which is the tail of a directed path with
150  * the returned nodes */
151  NodeSet descendants(const NodeId id) const;
152  NodeSet descendants(const std::string& name) const;
153 
154  /// returns the set of nodes with directed path ingoing to a given node
155  /** Note that the set of nodes returned may be empty if no path within the
156  * ArcGraphPart is ingoing to the given node.
157  * @param id the node which is the head of a directed path with the returned
158  * nodes
159  * @param name the name of the node which is the head of a directed path with
160  * the returned nodes */
161  NodeSet ancestors(const NodeId id) const;
162  NodeSet ancestors(const std::string& name) const;
163  /// @}
164 
165  /// @name Graphical methods
166  /// @{
167 
168  /** build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
169  *
170  * @param nodes the set of nodeId
171  * @param nodenames the vector of names of nodes
172  * @return the moralized ancestral graph
173  */
175  UndiGraph
177 
178  /** check if node X and node Y are independent given nodes Z
179  */
180  bool isIndependent(NodeId X, NodeId Y, const NodeSet& Z) const final;
181 
182  /** check if nodes X and nodes Y are independent given nodes Z
183  */
184  bool isIndependent(const NodeSet& X,
185  const NodeSet& Y,
186  const NodeSet& Z) const final;
187 
188  bool isIndependent(const std::string& Xname,
189  const std::string& Yname,
190  const std::vector< std::string >& Znames) const {
191  return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
192  };
193 
194  bool isIndependent(const std::vector< std::string >& Xnames,
195  const std::vector< std::string >& Ynames,
196  const std::vector< std::string >& Znames) const {
197  return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
198  };
199  /**
200  * The node's id are coherent with the variables and nodes of the topology.
201  * @param clear If false returns the previously created moral graph.
202  */
203  const UndiGraph& moralGraph(bool clear = true) const;
204 
205  /**
206  * The topological order stays the same as long as no variable or arcs are
207  * added or erased src the topology.
208  * @param clear If false returns the previously created topology.
209  */
210  const Sequence< NodeId >& topologicalOrder(bool clear = true) const;
211 
212  /// @}
213 
214  /// @return true if all the named node are the same and all the named arcs are
215  /// the same
216  bool hasSameStructure(const DAGmodel& other);
217 
218  protected:
219  /**
220  * Private copy operator.
221  */
222  DAGmodel& operator=(const DAGmodel& source);
223 
224  /// The DAG of this Directed Graphical Model.
226 
227  private:
228  /// Returns the moral graph of this DAGModel.
229  /// @warning mutableMoralGraph__ is assumed to be valid and empty
230  void moralGraph__() const;
231 
232  /// The moral graph of this Directed Graphical Model.
234  };
235 } // namespace gum
236 
237 #ifndef GUM_NO_INLINE
238 # include <agrum/tools/graphicalModels/DAGmodel_inl.h>
239 #endif /* GUM_NO_INLINE */
240 
241 #endif /* GUM_DAGMODEL_H */
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
Definition: DAGmodel.cpp:44
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:43
Virtual base class for PGMs using a DAG.
Definition: DAGmodel.h:47
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:70
UndiGraph moralizedAncestralGraph(const std::vector< std::string > &nodenames) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAGmodel_inl.h:121
NodeSet ancestors(const NodeId id) const
returns the set of nodes with directed path ingoing to a given node
Definition: DAGmodel_inl.h:112
NodeSet descendants(const NodeId id) const
returns the set of nodes with directed path outgoing from a given node
Definition: DAGmodel_inl.h:104
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:96
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:54
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:41
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:38
virtual ~DAGmodel()
Destructor.
Definition: DAGmodel.cpp:38
NodeSet ancestors(const std::string &name) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:116
NodeSet descendants(const std::string &name) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:108
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:45
bool hasSameStructure(const DAGmodel &other)
Definition: DAGmodel.cpp:74
DAGmodel()
Default constructor.
Definition: DAGmodel.cpp:29
UndiGraph * mutableMoralGraph__
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:233
bool isIndependent(const std::vector< std::string > &Xnames, const std::vector< std::string > &Ynames, const std::vector< std::string > &Znames) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAGmodel.h:194
NodeSet family(const NodeId id) const
returns the parents of a node and the node
Definition: DAGmodel_inl.h:62
DAGmodel(const DAGmodel &source)
Copy constructor.
Definition: DAGmodel.cpp:33
NodeSet family(const std::string &name) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:66
void moralGraph__() const
Returns the moral graph of this DAGModel.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:225
NodeSet children(const std::vector< std::string > &names) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:82
const UndiGraph & moralGraph(bool clear=true) const
The node&#39;s id are coherent with the variables and nodes of the topology.
Definition: DAGmodel.cpp:58
const Sequence< NodeId > & topologicalOrder(bool clear=true) const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
Definition: DAGmodel_inl.h:100
NodeSet parents(const std::vector< std::string > &names) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:90
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if node X and node Y are independent given nodes Z
Definition: DAGmodel_inl.h:131
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.
Definition: DAGmodel_inl.h:94
bool existsArc(const std::string &nametail, const std::string &namehead) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:49