1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_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(_at_LIP6) & Christophe GONZALES(_at_AMU)
27  *
28  */
29 #ifndef GUM_UGMODEL_H
30 #define GUM_UGMODEL_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/undiGraph.h>
37 
38 namespace gum {
39 
40   /**
41    * @class UGmodel
42    * @headerfile UGmodel.h <agrum/tools/graphicalModels/UGmodel.h>
43    * Virtual base class for PGMs using a undirected graph
44    *
45    */
46   class UGmodel: public GraphicalModel {
47     public:
48     /// @name Constructors / Destructors
49     /// @{
50 
51     /**
52      * Default constructor.
53      */
54     UGmodel();
55 
56     /**
57      * Destructor.
58      */
59     virtual ~UGmodel();
60 
61     /**
62      * Copy constructor. Do nothing.
63      */
64     UGmodel(const UGmodel& source);
65 
66     /// @}
67     /// @name Getter and setters
68     /// @{
69 
70     /// @}
71     /// @name Variable manipulation methods.
72     /// @{
73     /**
74      * Returns a constant reference to the dag of this Bayes Net.
75      */
76     const UndiGraph& graph() const;
77 
78     /**
79      * Returns the number of variables in this Directed Graphical Model.
80      */
81     virtual Size size() const final;
82 
83     /**
84      * Returns the number of arcs in this Directed Graphical Model.
85      */
86     Size sizeEdges() const;
87 
88     const NodeGraphPart& nodes() const final;
89 
90     /**
91      * Return true if this node exists in this graphical model.
92      */
93     bool exists(NodeId node) const final;
94 
95     /// @}
96 
97     /// @name Edge manipulation methods.
98     /// @{
99     const EdgeSet& edges() const;
100 
101     /// return true if the edge node1-node2 exists in the UGModel
102     /**
103      *
104      * @param node1 the nodeId (or the name) of the node1
105      * @param node2 the nodeId (or the name) of the node2
106      * @return true if the edge exists
107      */
108     bool existsEdge(const NodeId node1, const NodeId node2) const;
109     bool existsEdge(const std::string& name1, const std::string& name2) const;
110 
111     /// returns the neighbours of a node as set of nodes
112     /** Note that the set of nodes returned may be empty if no edge within the
113      * EdgeGraphPart contains the given node.
114      * @param id the node toward which the edge returned are pointing */
115     const NodeSet& neighbours(const NodeId id) const;
116     const NodeSet& neighbours(const std::string& name) const;
117 
118     /** check if X and Y are independent given Z
119      */
120     virtual bool isIndependent(NodeId X, NodeId Y, const NodeSet& Z) const final;
121     /** check if nodes X and nodes Y are independent given nodes Z
122      */
123     bool isIndependent(const NodeSet& X, const NodeSet& Y, const NodeSet& Z) const final;
124 
isIndependent(const std::string & Xname,const std::string & Yname,const std::vector<std::string> & Znames)125     bool isIndependent(const std::string&                Xname,
126                        const std::string&                Yname,
127                        const std::vector< std::string >& Znames) const {
128       return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
129     };
isIndependent(const std::vector<std::string> & Xnames,const std::vector<std::string> & Ynames,const std::vector<std::string> & Znames)130     bool isIndependent(const std::vector< std::string >& Xnames,
131                        const std::vector< std::string >& Ynames,
132                        const std::vector< std::string >& Znames) const {
133       return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
134     };
135 
136     /** check if nodes X and nodes Y are independent given nodes Z
137      */
138     // virtual bool isIndependent(const NodeSet& X, const NodeSet& Y, const
139     // NodeSet& Z) const;
140 
141     /// @}
142 
143     /// @return true if all the named node are the same and all the named arcs are
144     /// the same
145     bool hasSameStructure(const UGmodel& other);
146 
147     protected:
148     /**
149      * Private copy operator.
150      */
151     UGmodel& operator=(const UGmodel& source);
152 
153     /// The DAG of this Directed Graphical Model.
154     UndiGraph graph_;
155   };
156 }   // namespace gum
157 
158 #ifndef GUM_NO_INLINE
159 #  include <agrum/tools/graphicalModels/UGmodel_inl.h>
160 #endif /* GUM_NO_INLINE */
161 
162 #endif /* GUM_UGMODEL_H */
163