aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
IMarkovNet.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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&
155  variableFromName(const std::string& name) const = 0;
156  /// @}
157 
158  /**
159  * This operator compares 2 BNs !
160  * @warning To identify nodes between BNs, it is assumed that they share the
161  *same name.
162  *
163  * @return true if the src and this are equal.
164  */
165  bool operator==(const IMarkovNet< GUM_SCALAR >& from) const;
166 
167  /// @return Returns false if the src and this are equal.
168  bool operator!=(const IMarkovNet< GUM_SCALAR >& from) const;
169 
170  /**
171  * Returns the dimension (the number of free parameters) in this bayes net.
172  *
173  * \f$ dim(G)=\sum_{i \in nodes} ((r_i-1)\cdot q_i) \f$ where \f$ r_i \f$ is
174  * the number of instantiations of node \f$ i \f$ and \f$ q_i \f$ is the number
175  * of instantiations of its parents.
176  */
177  Size dim() const;
178 
179  /**
180  *
181  * @return the biggest domainSize among the variables of *this
182  */
183 
184  Size maxVarDomainSize() const;
185 
186  /**
187  *
188  * @return the smallest value in the CPTs of *this
189  */
190  GUM_SCALAR minParam() const;
191 
192  /**
193  *
194  * @return the biggest value in the CPTs of *this
195  */
196  GUM_SCALAR maxParam() const;
197 
198  /**
199  *
200  * @return the smallest value (not equal to 0) in the CPTs of *this
201  */
202  GUM_SCALAR minNonZeroParam() const;
203 
204  /**
205  *
206  * @return the biggest value (not equal to 1) in the CPTs of *this
207  */
208  GUM_SCALAR maxNonOneParam() const;
209 
210  /// @return Returns a dot representation of this IMarkovNet.
211  virtual std::string toDot() const;
212 
213  /// @return Returns a dot representation of this IMarkovNet.
214  virtual std::string toDotAsFactorGraph() const;
215 
216  /// @return Returns a string representation of this IMarkovNet.
217  std::string toString() const;
218 
219  /***
220  * @return the minimal subset of soids that conditions the target
221  *
222  * i.e. P(target| soids)=P(target|@return)
223  */
225 
226 
227  /***
228  * @return the minimal subset of soids that conditions the targets
229  *
230  * i.e. P(targets| soids)=P(targets|@return)
231  */
232  NodeSet minimalCondSet(const NodeSet& targets, const NodeSet& soids) const;
233 
234  private:
235  void minimalCondSetVisit__(NodeId node,
236  const NodeSet& soids,
237  NodeSet& minimal,
238  NodeSet& alreadyVisited) const;
239  };
240 
241 
242 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
243  extern template class IMarkovNet< double >;
244 #endif
245 
246 
247  /// Prints map's graph in output using the Graphviz-dot format.
248  template < typename GUM_SCALAR >
250  const IMarkovNet< GUM_SCALAR >& mn);
251 
252 } /* namespace gum */
253 
254 #include <agrum/MN/IMarkovNet_tpl.h>
255 
256 #endif /* GUM_SIMPLE_MARKOV_NET_H */
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
GUM_SCALAR maxParam() 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:669
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
Size maxVarDomainSize() const
GUM_SCALAR minNonZeroParam() const
void minimalCondSetVisit__(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisited) 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 !