aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
IMarkovNet.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) et Christophe GONZALES(@AMU)
4  * (@AMU) 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 Bayesian networks
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Lionel TORTI
27  *
28  */
29 #ifndef GUM_SIMPLE_MARKOV_NET_H
30 #define GUM_SIMPLE_MARKOV_NET_H
31 
32 #include <utility>
33 
34 #include <agrum/agrum.h>
35 
36 #include <agrum/tools/core/hashTable.h>
37 
38 #include <agrum/tools/graphicalModels/UGmodel.h>
39 #include <agrum/tools/multidim/potential.h>
40 
41 namespace gum {
42  template < typename GUM_SCALAR >
44 
45  // template < typename GUM_SCALAR >
46  // class MarkovNetFactory;
47 
48  /**
49  * @class IMarkovNet
50  * @headerfile IMarkovNet.h <agrum/BN/IMarkovNet.h>
51  * @brief Class representing the minimal interface for Markov Network.
52  * @ingroup mn_group
53  *
54  * This class is used as a base class for different versions of Markov Networks.
55  *No data (except the graph herited from UGModel are included in this class. Many
56  *algorithms (inference for instance) may use this class when a simple MN is
57  *needed.
58  */
59  template < typename GUM_SCALAR >
60  class IMarkovNet: public UGmodel {
61  public:
62  // ===========================================================================
63  /// @name Constructors / Destructors
64  // ===========================================================================
65  /// @{
66 
67  /**
68  * Default constructor.
69  */
70  IMarkovNet();
71  explicit IMarkovNet(std::string name);
72 
73  /**
74  * Destructor.
75  */
76  virtual ~IMarkovNet();
77 
78  /**
79  * Copy constructor.
80  */
81  IMarkovNet(const IMarkovNet< GUM_SCALAR >& source);
82 
83  /**
84  * Copy operator.
85  */
86  IMarkovNet< GUM_SCALAR >& operator=(const IMarkovNet< GUM_SCALAR >& source);
87 
88  /// @}
89 
90 
91  // ===========================================================================
92  /// @name Pure Virtual methods
93  // ===========================================================================
94  /// @{
95 
96  /**
97  * Returns the factor of a set of variable.
98  *
99  * @throw NotFound If no variable's id matches varId.
100  */
101  virtual const Potential< GUM_SCALAR >& factor(const NodeSet& varIds) const = 0;
102 
103  /**
104  * Returns the smallest factor that contains this variable
105  *
106  * @throw NotFound If no variable's id matches varId.
107  */
108  virtual const NodeSet& smallestFactorFromNode(NodeId node) const = 0;
109 
110  /**
111  * Returns the smallest factor that contains this variable
112  *
113  * @throw NotFound If no variable's id matches varId.
114  */
115  virtual const NodeSet& smallestFactorFromNode(const std::string& name) const;
116 
117  /**
118  * Returns the set of factors as a IMarkovNet::FactorTable
119  *
120  */
121  virtual const FactorTable< GUM_SCALAR >& factors() const = 0;
122 
123  /**
124  * Returns a constant reference to the VariableNodeMap of thisBN
125  */
126  virtual const VariableNodeMap& variableNodeMap() const = 0;
127 
128  /**
129  * Returns a constant reference over a variable given it's node id.
130  *
131  * @throw NotFound If no variable's id matches varId.
132  */
133  virtual const DiscreteVariable& variable(NodeId id) const = 0;
134 
135  /**
136  * Return id node from discrete var pointer.
137  *
138  * @throw NotFound If no variable matches var.
139  */
140  virtual NodeId nodeId(const DiscreteVariable& var) const = 0;
141 
142  /**
143  * Getter by name
144  *
145  * @throw NotFound if no such name exists in the graph.
146  */
147  virtual NodeId idFromName(const std::string& name) const = 0;
148 
149  /**
150  * Getter by name
151  *
152  * @throw NotFound if no such name exists in the graph.
153  */
154  virtual const DiscreteVariable& variableFromName(const std::string& name) const = 0;
155  /// @}
156 
157  /**
158  * This operator compares 2 BNs !
159  * @warning To identify nodes between BNs, it is assumed that they share the
160  *same name.
161  *
162  * @return true if the src and this are equal.
163  */
164  bool operator==(const IMarkovNet< GUM_SCALAR >& from) const;
165 
166  /// @return Returns false if the src and this are equal.
167  bool operator!=(const IMarkovNet< GUM_SCALAR >& from) const;
168 
169  /**
170  * Returns the dimension (the number of free parameters) in this bayes net.
171  *
172  * \f$ dim(G)=\sum_{i \in nodes} ((r_i-1)\cdot q_i) \f$ where \f$ r_i \f$ is
173  * the number of instantiations of node \f$ i \f$ and \f$ q_i \f$ is the number
174  * of instantiations of its parents.
175  */
176  Size dim() const;
177 
178  /**
179  *
180  * @return the biggest domainSize among the variables of *this
181  */
182 
183  Size maxVarDomainSize() const;
184 
185  /**
186  *
187  * @return the smallest value in the CPTs of *this
188  */
189  GUM_SCALAR minParam() const;
190 
191  /**
192  *
193  * @return the biggest value in the CPTs of *this
194  */
195  GUM_SCALAR maxParam() const;
196 
197  /**
198  *
199  * @return the smallest value (not equal to 0) in the CPTs of *this
200  */
201  GUM_SCALAR minNonZeroParam() const;
202 
203  /**
204  *
205  * @return the biggest value (not equal to 1) in the CPTs of *this
206  */
207  GUM_SCALAR maxNonOneParam() const;
208 
209  /// @return Returns a dot representation of this IMarkovNet.
210  virtual std::string toDot() const;
211 
212  /// @return Returns a dot representation of this IMarkovNet.
213  virtual std::string toDotAsFactorGraph() const;
214 
215  /// @return Returns a string representation of this IMarkovNet.
216  std::string toString() const;
217 
218  /***
219  * @return the minimal subset of soids that conditions the target
220  *
221  * i.e. P(target| soids)=P(target|@return)
222  */
224 
225 
226  /***
227  * @return the minimal subset of soids that conditions the targets
228  *
229  * i.e. P(targets| soids)=P(targets|@return)
230  */
231  NodeSet minimalCondSet(const NodeSet& targets, const NodeSet& soids) const;
232 
233  private:
234  void _minimalCondSetVisit_(NodeId node,
235  const NodeSet& soids,
236  NodeSet& minimal,
237  NodeSet& alreadyVisited) const;
238  };
239 
240 
241 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
242  extern template class IMarkovNet< double >;
243 #endif
244 
245 
246  /// Prints map's graph in output using the Graphviz-dot format.
247  template < typename GUM_SCALAR >
249 
250 } /* namespace gum */
251 
252 #include <agrum/MN/IMarkovNet_tpl.h>
253 
254 #endif /* GUM_SIMPLE_MARKOV_NET_H */
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
GUM_SCALAR maxParam() const
void _minimalCondSetVisit_(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisited) const
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of thisBN.
virtual ~IMarkovNet()
Destructor.
GUM_SCALAR maxNonOneParam() const
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
Size maxVarDomainSize() const
GUM_SCALAR minNonZeroParam() const
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
virtual const NodeSet & smallestFactorFromNode(NodeId node) const =0
Returns the smallest factor that contains this variable.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
virtual std::string toDotAsFactorGraph() const
IMarkovNet(const IMarkovNet< GUM_SCALAR > &source)
Copy constructor.
virtual std::string toDot() const
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
IMarkovNet()
Default constructor.
IMarkovNet< GUM_SCALAR > & operator=(const IMarkovNet< GUM_SCALAR > &source)
Copy operator.
IMarkovNet(std::string name)
Default constructor.
bool operator!=(const IMarkovNet< GUM_SCALAR > &from) const
NodeSet minimalCondSet(const NodeSet &targets, const NodeSet &soids) const
GUM_SCALAR minParam() const
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node from discrete var pointer.
std::string toString() const
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
Class representing the minimal interface for Markov Network.
Definition: IMarkovNet.h:60
virtual const NodeSet & smallestFactorFromNode(const std::string &name) const
Returns the smallest factor that contains this variable.
std::ostream & operator<<(std::ostream &output, const IMarkovNet< GUM_SCALAR > &mn)
Prints map&#39;s graph in output using the Graphviz-dot format.
virtual const Potential< GUM_SCALAR > & factor(const NodeSet &varIds) const =0
Returns the factor of a set of variable.
bool operator==(const IMarkovNet< GUM_SCALAR > &from) const
This operator compares 2 BNs !