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