aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DAGmodel.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_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  */
176 
177  /** check if node X and node Y are independent given nodes Z
178  */
179  bool isIndependent(NodeId X, NodeId Y, const NodeSet& Z) const final;
180 
181  /** check if nodes X and nodes Y are independent given nodes Z
182  */
183  bool isIndependent(const NodeSet& X, const NodeSet& Y, const NodeSet& Z) const final;
184 
185  bool isIndependent(const std::string& Xname,
186  const std::string& Yname,
187  const std::vector< std::string >& Znames) const {
188  return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
189  };
190 
191  bool isIndependent(const std::vector< std::string >& Xnames,
192  const std::vector< std::string >& Ynames,
193  const std::vector< std::string >& Znames) const {
194  return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
195  };
196  /**
197  * The node's id are coherent with the variables and nodes of the topology.
198  * @param clear If false returns the previously created moral graph.
199  */
200  const UndiGraph& moralGraph(bool clear = true) const;
201 
202  /**
203  * The topological order stays the same as long as no variable or arcs are
204  * added or erased src the topology.
205  * @param clear If false returns the previously created topology.
206  */
207  const Sequence< NodeId >& topologicalOrder(bool clear = true) const;
208 
209  /// @}
210 
211  /// @return true if all the named node are the same and all the named arcs are
212  /// the same
213  bool hasSameStructure(const DAGmodel& other);
214 
215  protected:
216  /**
217  * Private copy operator.
218  */
219  DAGmodel& operator=(const DAGmodel& source);
220 
221  /// The DAG of this Directed Graphical Model.
223 
224  private:
225  /// Returns the moral graph of this DAGModel.
226  /// @warning _mutableMoralGraph_ is assumed to be valid and empty
227  void _moralGraph_() const;
228 
229  /// The moral graph of this Directed Graphical Model.
231  };
232 } // namespace gum
233 
234 #ifndef GUM_NO_INLINE
235 # include <agrum/tools/graphicalModels/DAGmodel_inl.h>
236 #endif /* GUM_NO_INLINE */
237 
238 #endif /* GUM_DAGMODEL_H */
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
Definition: DAGmodel.cpp:41
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:65
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:104
NodeSet ancestors(const NodeId id) const
returns the set of nodes with directed path ingoing to a given node
Definition: DAGmodel_inl.h:96
NodeSet descendants(const NodeId id) const
returns the set of nodes with directed path outgoing from a given node
Definition: DAGmodel_inl.h:90
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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:35
NodeSet ancestors(const std::string &name) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:98
NodeSet descendants(const std::string &name) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:92
void _moralGraph_() const
Returns the moral graph of this DAGModel.
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:69
DAGmodel()
Default constructor.
Definition: DAGmodel.cpp:29
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:191
NodeSet family(const NodeId id) const
returns the parents of a node and the node
Definition: DAGmodel_inl.h:59
DAGmodel(const DAGmodel &source)
Copy constructor.
Definition: DAGmodel.cpp:31
NodeSet family(const std::string &name) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:61
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
NodeSet children(const std::vector< std::string > &names) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:72
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:55
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:86
NodeSet parents(const std::vector< std::string > &names) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:78
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:113
UndiGraph * _mutableMoralGraph_
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:230
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:82
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