aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BayesNet.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_BAYES_NET_H
30 #define GUM_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/BN/IBayesNet.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 BayesNet
48  * @headerfile BayesNet.h <agrum/BN/BayesNet.h>
49  * @brief Class representing a Bayesian network.
50  * @ingroup bn_group
51  *
52  * Bayesian networks are a probabilistic graphical model in which nodes are
53  * random variables and the probability distribution is defined by the
54  * product:
55  *
56  * <center>\f$P(X_1, \ldots, X_n) = \prod_{i=1}^{n} P(X_i |
57  * \pi(X_i))\f$,</center>
58  *
59  * where \f$\pi(X_i)\f$ is the parent of \f$X_i\f$.
60  *
61  * The probability distribution can be represented as a directed acyclic
62  * graph (DAG) where:
63  * - Nodes are discrete random variables.
64  * - An arc A -> B represent a dependency between variables A and B, i.e. B
65  * conditional probability distribution is defined as \f$P(B| \pi(B)\f$.
66  *
67  * After a variable is added to the BN, it's domain cannot change. But it arcs
68  * are added, the data in its CPT are lost.
69  *
70  * You should look a the gum::BayesNetFactory class which can help build
71  * Bayesian networks.
72  *
73  * You can print a BayesNet using
74  * gum::operator<<(std::ostream&, const BayesNet<GUM_SCALAR>&).
75  */
76  template < typename GUM_SCALAR >
77  class BayesNet: public IBayesNet< GUM_SCALAR > {
78  friend class BayesNetFactory< GUM_SCALAR >;
79 
80  public:
81  /**
82  * Create a Bayesian network with a dot-like syntax which specifies:
83  * - the structure "a->b->c;b->d<-e;".
84  * - the type of the variables with different syntax:
85  * + by default, a variable is a gum::RangeVariable using the default
86  * domainSize (second argument)
87  * + with "a[10]", the variable is a gum::RangeVariable using 10 as
88  * domainSize (from 0 to 9)
89  * + with "a[3,7]", the variable is a gum::RangeVariable using a domainSize
90  * from 3 to 7
91  * + with "a[1,3.14,5,6.2]", the variable is a gum::DiscretizedVariable
92  * using the given ticks (at least 3 values)
93  * + with "a{top|middle|bottom}", the variable is a gum::LabelizedVariable
94  * using the given labels.
95  *
96  * Note that if the dot-like string contains such a specification more than
97  * once for a variable, the first specification will be used.
98  *
99  * @param dotlike the string containing the specification
100  * @param domainSize the default domain size for variables
101  * @return the resulting Bayesian network
102  */
103  static BayesNet< GUM_SCALAR > fastPrototype(const std::string& dotlike, Size domainSize = 2);
104 
105  // ===========================================================================
106  /// @name Constructors and Destructor
107  // ===========================================================================
108  /// @{
109 
110  /**
111  * @brief Default constructor.
112  */
113  BayesNet();
114 
115  /**
116  * @brief Default constructor.
117  *
118  * @param name The BayesNet's name.
119  */
120  explicit BayesNet(std::string name);
121 
122  /**
123  * @brief Destructor.
124  */
125  virtual ~BayesNet() final;
126 
127  /**
128  * @brief Copy constructor.
129  */
130  BayesNet(const BayesNet< GUM_SCALAR >& source);
131 
132  /// @}
133  // ===========================================================================
134  /// @name Operators
135  // ===========================================================================
136  /// @{
137 
138  /**
139  * @brief Copy operator.
140  *
141  * @param source The copied BayesNet.
142  * @return The copy of source.
143  */
144  BayesNet< GUM_SCALAR >& operator=(const BayesNet< GUM_SCALAR >& source);
145 
146  /// @}
147  // ===========================================================================
148  /// @name Variable manipulation methods
149  // ===========================================================================
150  /// @{
151 
152  /**
153  * @brief Returns the CPT of a variable.
154  *
155  * @param varId A variable's id in the gum::BayesNet.
156  * @return The variable's CPT.
157  * @throw NotFound If no variable's id matches varId.
158  */
159  const Potential< GUM_SCALAR >& cpt(NodeId varId) const final;
160 
161  /**
162  * @brief Returns the CPT of a variable.
163  */
164  const Potential< GUM_SCALAR >& cpt(const std::string& name) const {
165  return cpt(idFromName(name));
166  };
167 
168  /**
169  * @brief Returns a map between variables and nodes of this gum::BayesNet.
170  *
171  * @return Returns a constant reference to the gum::VariableNodeMap.
172  */
173  const VariableNodeMap& variableNodeMap() const final;
174 
175  /**
176  * @brief Add a variable to the gum::BayesNet.
177  *
178  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
179  * gum::Potential.
180  *
181  * The variable is added by copy to the gum::BayesNet.
182  * The variable's gum::Potential implementation will be a
183  * gum::MultiDimArray.
184  *
185  * @param var The variable added by copy.
186  * @return Returns the variable's id in the gum::BayesNet.
187  * @throws DuplicateLabel Raised if variable.name() is already used in this
188  * gum::BayesNet.
189  */
190  NodeId add(const DiscreteVariable& var);
191 
192  /**
193  * @brief Shortcut for add(gum::LabelizedVariable(name,name,nbrmod))
194  *
195  * Add a gum::LabelizedVariable to the gum::BayesNet
196  *
197  * This method is just a shortcut for a often used pattern
198  *
199  * @throws DuplicateLabel Raised if variable.name() is already used in this
200  * gum::BayesNet.
201  * @throws NotAllowed if nbrmod<2
202  */
203  NodeId add(const std::string& name, unsigned int nbrmod);
204 
205  /**
206  * @brief Add a variable to the gum::BayesNet.
207  *
208  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
209  * gum::Potential.
210  *
211  * The variable is added by copy to the gum::BayesNet.
212  *
213  * @param var The variable added by copy.
214  * @param aContent The gum::MultiDimImplementation to use for this
215  * variable's gum::Potential implementation.
216  * @return Returns the variable's id in the gum::BayesNet.
217  * @throws DuplicateLabel Raised if variable.name() is already used in this
218  * gum::BayesNet.
219  */
221 
222  /**
223  * @brief Add a variable to the gum::BayesNet.
224  *
225  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
226  * gum::Potential.
227  *
228  * The variable is added by copy to the gum::BayesNet.
229  * The variable's gum::Potential implementation will be a
230  * gum::MultiDimArray.
231  *
232  * @param var The variable added by copy.
233  * @param id The variable's forced gum::NodeId in the gum::BayesNet.
234  * @return Returns the variable's id in the gum::BayesNet.
235  * @throws DuplicateElement Raised id is already used.
236  * @throws DuplicateLabel Raised if variable.name() is already used in this
237  * gum::BayesNet.
238  */
240 
241  /**
242  * @brief Add a variable to the gum::BayesNet.
243  *
244  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
245  * gum::Potential.
246  *
247  * @param var The variable added by copy.
248  * @param aContent The gum::MultiDimImplementation to use for this
249  * variable's gum::Potential implementation.
250  * @param id The variable's forced gum::NodeId in the gum::BayesNet.
251  * @return Returns the variable's id in the gum::BayesNet.
252  * @throws DuplicateElement Raised id is already used.
253  * @throws DuplicateLabel Raised if variable.name() is already used in this
254  * gum::BayesNet.
255  */
256  NodeId
258 
259  /**
260  * @brief clear the whole Bayes net *
261  */
262  void clear();
263 
264  /**
265  * @brief Remove a variable from the gum::BayesNet.
266  *
267  * Removes the corresponding variable from the gum::BayesNet and from
268  * all of it's children gum::Potential.
269  *
270  * If no variable matches the given id, then nothing is done.
271  *
272  * @param varId The variable's id to remove.
273  */
274  void erase(NodeId varId);
275 
276  /**
277  * @brief Removes a variable from the gum::BayesNet.
278  */
279  void erase(const std::string& name) { erase(idFromName(name)); };
280 
281  /**
282  * @brief Remove a variable from the gum::BayesNet.
283  *
284  * Removes the corresponding variable from the gum::BayesNet and from
285  * all of it's children gum::Potential.
286  *
287  * If no variable matches the given variable, then nothing is done.
288  *
289  * @param var A reference on the variable to remove.
290  */
291  void erase(const DiscreteVariable& var);
292 
293  /**
294  * @brief Returns a gum::DiscreteVariable given its gum::NodeId in the
295  * gum::BayesNet.
296  *
297  * @param id The variable's id to return.
298  * @returns Returns a constant reference of the gum::DiscreteVariable
299  * corresponding to id in the gum::BayesNet.
300  * @throw NotFound Raised if id does not match a a variable in the
301  * gum::BayesNet.
302  */
303  const DiscreteVariable& variable(NodeId id) const final;
304 
305  /**
306  * @brief Returns a gum::DiscreteVariable given its gum::NodeId in the
307  * gum::BayesNet.
308  */
309  const DiscreteVariable& variable(const std::string& name) const {
310  return variable(idFromName(name));
311  };
312 
313  /**
314  * @brief Changes a variable's name in the gum::BayesNet.
315  *
316  * This will change the gum::DiscreteVariable names in the gum::BayesNet.
317  *
318  * @throws DuplicateLabel Raised if newName is already used in this
319  * gum::BayesNet.
320  * @throws NotFound Raised if no variable matches id.
321  */
322  void changeVariableName(NodeId id, const std::string& new_name);
323 
324  /**
325  * @brief Changes a variable's name.
326  */
327  void changeVariableName(const std::string& name, const std::string& new_name) {
328  changeVariableName(idFromName(name), new_name);
329  }
330 
331  /**
332  * @brief Changes a variable's label in the gum::BayesNet.
333  *
334  * This will change the gum::LabelizedVariable names in the gum::BayesNet.
335  *
336  * @throws DuplicateLabel Raised if new_label is already used in this
337  * gum::LabelizedVariable.
338  * @throws NotFound Raised if no variable matches id or if the variable is not
339  * a LabelizedVariable
340  */
341  void changeVariableLabel(NodeId id, const std::string& old_label, const std::string& new_label);
342 
343  /**
344  * @brief Changes a variable's name.
345  */
346  void changeVariableLabel(const std::string& name,
347  const std::string& old_label,
348  const std::string& new_label) {
349  changeVariableLabel(idFromName(name), old_label, new_label);
350  }
351 
352  /**
353  * @brief Returns a variable's id in the gum::BayesNet.
354  *
355  * @param var The variable from which the gum::NodeId is returned.
356  * @return Returns the gum::DiscreteVariable gum::NodeId in the
357  * gum::BayesNet.
358  * @throw NotFound If var is not in the gum::BayesNet.
359  */
360  NodeId nodeId(const DiscreteVariable& var) const final;
361 
362  /**
363  * @brief Returns a variable's id given its name in the gum::BayesNet.
364  *
365  * @param name The variable's name from which the gum::NodeId is returned.
366  * @return Returns the variable gum::NodeId in the gum::BayesNet.
367  * @throw NotFound Raised if name does not match a variable in the
368  * gum::BayesNet.
369  */
370  NodeId idFromName(const std::string& name) const final;
371 
372  /**
373  * @brief Returns a variable given its name in the gum::BayesNet.
374  *
375  * @param name The variable's name in the gum::BayesNet.
376  * @return Returns the gum::DiscreteVariable named name in the
377  * gum::BayesNet.
378  * @throw NotFound Raised if name does not match a variable in the
379  * gum::BayesNet.
380  */
381  const DiscreteVariable& variableFromName(const std::string& name) const final;
382  /// @}
383 
384  // ===========================================================================
385  /// @name Arc manipulation methods.
386  // ===========================================================================
387  /// @{
388 
389  /**
390  * Add an arc in the BN, and update arc.head's CPT.
391  *
392  * @param head and
393  * @param tail as NodeId
394  * @throw InvalidEdge If arc.tail and/or arc.head are not in the BN.
395  * @throw DuplicateElement if the arc already exists
396  */
397  void addArc(NodeId tail, NodeId head);
398 
399  /**
400  * Add an arc in the BN, and update arc.head's CPT
401  *
402  * @throw gum::DuplicateElement if the arc already exists
403  */
404  void addArc(const std::string& tail, const std::string& head);
405 
406  /**
407  * Removes an arc in the BN, and update head's CTP.
408  *
409  * If (tail, head) doesn't exist, the nothing happens.
410  * @param arc The arc removed.
411  */
412  void eraseArc(const Arc& arc);
413 
414  /**
415  * Removes an arc in the BN, and update head's CTP.
416  *
417  * If (tail, head) doesn't exist, the nothing happens.
418  * @param head and
419  * @param tail as NodeId
420  */
421  void eraseArc(NodeId tail, NodeId head);
422 
423  /**
424  * Removes an arc in the BN, and update head's CTP.
425  */
426  void eraseArc(const std::string& tail, const std::string& head) {
427  eraseArc(idFromName(tail), idFromName(head));
428  }
429 
430  /**
431  * When inserting/removing arcs, node CPTs change their dimension with a
432  * cost in
433  * time.
434  * These functions delay the CPTs change to be done just once at the end of
435  * a
436  * sequence of topology modification.
437  */
438  ///@{
439  /// begins a sequence of insertions/deletions of arcs without changing the
440  /// dimensions of the CPTs.
442  /// terminates a sequence of insertions/deletions of arcs by adjusting all
443  /// CPTs
444  /// dimensions.
446  ///@}
447 
448  /**
449  * @brief Reverses an arc while preserving the same joint distribution.
450  *
451  * This method uses Shachter's 1986 algorithm for reversing an arc in
452  * the Bayes net while preserving the same joint distribution. By
453  * performing this reversal, we also add new arcs (required to not alter
454  * the joint distribution)
455  * @throws InvalidArc exception if the arc does not exist or if its reversal
456  * would induce a directed cycle. */
457  /// @{
458  void reverseArc(NodeId tail, NodeId head);
459  void reverseArc(const std::string& tail, const std::string& head) {
460  reverseArc(idFromName(tail), idFromName(head));
461  }
462  void reverseArc(const Arc& arc);
463  /// @}
464 
465  /// @}
466 
467  // ===========================================================================
468  /// @name Accessors for nodes with CI or logical implementation
469  // ===========================================================================
470  /// @{
471 
472  /**
473  * Add a variable, it's associate node and a gum::noisyOR implementation.
474  *The id
475  *of the new
476  * variable is automatically generated. Since it seems that the 'classical'
477  *noisyOR is the Compound noisyOR, we keep
478  * the gum::BayesNet::addNoisyOR as an alias for
479  *gum::BayesNet::addNoisyORCompound
480  *
481  * @param var The variable added by copy.
482  * @param external_weight see ref
483  *gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
484  * @return the id of the added variable.
485  * @{
486  */
487 
491  /** @} */
492 
493  /**
494  * Add a variable, its associate node and a noisyOR implementation. Since
495  * it seems that the 'classical' noisyOR is the Compound noisyOR, we keep
496  * the addNoisyOR as an alias for addNoisyORCompound.
497  *
498  * @param var The variable added by copy.
499  * @param external_weight see gum::MultiDimNoisyORNet,
500  *gum::MultiDimNoisyORCompound
501  * @param id The chosen id
502  * @warning give an id should be reserved for rare and specific situations
503  *!!!
504  * @return the id of the added variable.
505  * @throws DuplicateElement if id is already used
506  *
507  * @{
508  */
512  /** @} */
513 
514  /**
515  * Add a variable, its associate node and a noisyAND implementation.
516  *
517  * @param var The variable added by copy
518  * @param external_weight see gum::MultiDimNoisyAND
519  * @param id proposed gum::nodeId for the variable
520  * @warning give an id should be reserved for rare and specific situations
521  *!!!
522  * @return the id of the added variable.
523  */
525 
526  /**
527  * Add a variable, its associate node and a noisyAND implementation. The id
528  *of the new variable is automatically generated.
529  *
530  * @param var The variable added by copy.
531  * @param external_weight see gum::MultiDimNoisyAND
532  * @return the id of the added variable.
533  */
535 
536  /**
537  * Add a variable, its associate node and a Logit implementation.
538  *
539  * @param var The variable added by copy
540  * @param external_weight see gum::MultiDimLogit
541  * @param id proposed gum::nodeId for the variable
542  * @warning give an id should be reserved for rare and specific situations
543  *!!!
544  * @return the id of the added variable.
545  */
547 
548  /**
549  * Add a variable, its associate node and a Logit implementation. The id of
550  *the new variable is automatically generated.
551  *
552  * @param var The variable added by copy.
553  * @param external_weight see gum::MultiDimLogit
554  * @return the id of the added variable.
555  */
557 
558  /**
559  * Add a variable, it's associate node and an OR implementation. The id of
560  *the new variable is automatically generated.
561  *
562  * @warning OR is implemented as a gum::aggregator::Or which means that if
563  *parents are not boolean, all value>1 is True
564  *
565  * @param var The variable added by copy.
566  * @return the id of the added variable.
567  * @throws SizeError if variable.domainSize()>2
568  */
570 
571  /**
572  * Add a variable, it's associate node and an AND implementation. The id of
573  *the new variable is automatically generated.
574  *
575  * @warning AND is implemented as a gum::aggregator::And which means that if
576  *parents are not boolean, all value>1 is True
577  *
578  * @param var The variable added by copy.
579  * @return the id of the added variable.
580  * @throws SizeError if variable.domainSize()>2
581  */
583 
584  /**
585  * Others aggregators
586  * @{
587  */
596  /**
597  * @}
598  */
599  /**
600  * Add an arc in the BN, and update arc.head's CPT.
601  *
602  * @param head and
603  * @param tail as NodeId
604  * @param causalWeight see gum::MultiDimICIModel
605  * @throw InvalidArc If arc.tail and/or arc.head are not in the BN.
606  * @throw InvalidArc If variable in arc.head is not a NoisyOR variable.
607  */
608  void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
609 
610  /**
611  * Add an arc in the BN, and update arc.head's CPT.
612  *
613  * @param head and
614  * @param tail as std::string
615  * @param causalWeight see gum::MultiDimICIModel
616  * @param NotFound if no node with sun names is found
617  * @throw InvalidArc If arc.tail and/or arc.head are not in the BN.
618  * @throw InvalidArc If variable in arc.head is not a NoisyOR variable.
619  */
620  void addWeightedArc(const std::string& tail, const std::string& head, GUM_SCALAR causalWeight) {
621  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
622  };
623  /// @}
624 
625  /// randomly generates CPTs for a given structure
626  void generateCPTs() const;
627 
628  /// randomly generate CPT for a given node in a given structure
629  void generateCPT(NodeId node) const;
630  void generateCPT(const std::string& name) const { generateCPT(idFromName(name)); };
631 
632  /// change the CPT associated to nodeId to newPot
633  /// delete the old CPT associated to nodeId.
634  /// @throw NotAllowed if newPot has not the same signature as
635  /// _probaMap_[NodeId]
636  void changePotential(NodeId id, Potential< GUM_SCALAR >* newPot);
637  void changePotential(const std::string& name, Potential< GUM_SCALAR >* newPot);
638 
639  private:
640  /// clear all potentials
641  void _clearPotentials_();
642 
643  /// copy of potentials from a BN to another, using names of vars as ref.
644  void _copyPotentials_(const BayesNet< GUM_SCALAR >& source);
645 
646  /// the map between variable and id
648 
649  /// Mapping between the variable's id and their CPT.
651  // HashTable<NodeId, Potential<GUM_SCALAR>* > _probaMap_;
652 
653  /// change the CPT associated to nodeId to newPot
654  /// delete the old CPT associated to nodeId.
655  /// @warning no verification of dimensions are performer
656  /// @see changePotential
657  void _unsafeChangePotential_(NodeId id, Potential< GUM_SCALAR >* newPot);
658 
659  public:
660  using IBayesNet< GUM_SCALAR >::dag;
661  using IBayesNet< GUM_SCALAR >::size;
662  using IBayesNet< GUM_SCALAR >::nodes;
664  };
665 
666  /// Prints map's DAG in output using the Graphviz-dot format.
667  template < typename GUM_SCALAR >
669 
670 
671 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
672  extern template class BayesNet< double >;
673 #endif
674 
675 } /* namespace gum */
676 
677 #include <agrum/BN/BayesNet_tpl.h>
678 
679 #endif /* GUM_BAYES_NET_H */
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:344
void changeVariableName(const std::string &name, const std::string &new_name)
Changes a variable&#39;s name.
Definition: BayesNet.h:327
BayesNet< GUM_SCALAR > & operator=(const BayesNet< GUM_SCALAR > &source)
Copy operator.
Definition: BayesNet_tpl.h:184
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:462
NodeId addNoisyOR(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it&#39;s associate node and a gum::noisyOR implementation.
Definition: BayesNet_tpl.h:511
void _clearPotentials_()
clear all potentials
Definition: BayesNet_tpl.h:612
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:210
NodeId addLogit(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, its associate node and a Logit implementation.
Definition: BayesNet_tpl.h:535
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:205
void addArc(const std::string &tail, const std::string &head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:355
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void reverseArc(const std::string &tail, const std::string &head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet.h:459
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:234
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:450
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:166
NodeId add(const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:256
NodeId addLogit(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a Logit implementation.
Definition: BayesNet_tpl.h:555
NodeId addAND(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an AND implementation.
Definition: BayesNet_tpl.h:455
void clear()
clear the whole Bayes net *
Definition: BayesNet_tpl.h:334
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:648
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a noisyOR implementation.
Definition: BayesNet_tpl.h:562
void _copyPotentials_(const BayesNet< GUM_SCALAR > &source)
copy of potentials from a BN to another, using names of vars as ref.
Definition: BayesNet_tpl.h:623
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:474
NodeId addNoisyOR(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a noisyOR implementation.
Definition: BayesNet_tpl.h:541
BayesNet(const BayesNet< GUM_SCALAR > &source)
Copy constructor.
Definition: BayesNet_tpl.h:176
static BayesNet< GUM_SCALAR > fastPrototype(const std::string &dotlike, Size domainSize=2)
Create a Bayesian network with a dot-like syntax which specifies:
Definition: BayesNet_tpl.h:133
void eraseArc(const std::string &tail, const std::string &head)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet.h:426
void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:576
NodeId addNoisyORNet(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it&#39;s associate node and a gum::noisyOR implementation.
Definition: BayesNet_tpl.h:523
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:598
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:296
NodeProperty< Potential< GUM_SCALAR > *> _probaMap_
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:650
void eraseArc(NodeId tail, NodeId head)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:374
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:591
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
Definition: BayesNet_tpl.h:306
NodeId addNoisyAND(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a noisyAND implementation.
Definition: BayesNet_tpl.h:548
NodeId addNoisyAND(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, its associate node and a noisyAND implementation.
Definition: BayesNet_tpl.h:529
BayesNet(std::string name)
Default constructor.
Definition: BayesNet_tpl.h:171
NodeId addOR(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an OR implementation.
Definition: BayesNet_tpl.h:496
void generateCPT(const std::string &name) const
Definition: BayesNet.h:630
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:642
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:467
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:301
void addWeightedArc(const std::string &tail, const std::string &head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet.h:620
void erase(const DiscreteVariable &var)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:311
void changeVariableLabel(NodeId id, const std::string &old_label, const std::string &new_label)
Changes a variable&#39;s label in the gum::BayesNet.
Definition: BayesNet_tpl.h:215
NodeId add(const std::string &name, unsigned int nbrmod)
Shortcut for add(gum::LabelizedVariable(name,name,nbrmod))
Definition: BayesNet_tpl.h:245
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:481
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable&#39;s id in the gum::BayesNet.
Definition: BayesNet_tpl.h:229
virtual ~BayesNet() final
Destructor.
Definition: BayesNet_tpl.h:197
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:605
VariableNodeMap _varMap_
the map between variable and id
Definition: BayesNet.h:647
NodeId addNoisyORNet(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a noisyOR implementation.
Definition: BayesNet_tpl.h:569
const Potential< GUM_SCALAR > & cpt(const std::string &name) const
Returns the CPT of a variable.
Definition: BayesNet.h:164
NodeId add(const DiscreteVariable &var, NodeId id)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:264
NodeId add(const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent, NodeId id)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:277
void changePotential(NodeId id, Potential< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
Definition: BayesNet_tpl.h:655
void changeVariableLabel(const std::string &name, const std::string &old_label, const std::string &new_label)
Changes a variable&#39;s name.
Definition: BayesNet.h:346
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:486
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it&#39;s associate node and a gum::noisyOR implementation.
Definition: BayesNet_tpl.h:517
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:290
const DiscreteVariable & variable(const std::string &name) const
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet.h:309
NodeId addSUM(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:503
void _unsafeChangePotential_(NodeId id, Potential< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
Definition: BayesNet_tpl.h:675
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:316
void changePotential(const std::string &name, Potential< GUM_SCALAR > *newPot)
Definition: BayesNet_tpl.h:681
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:491
void reverseArc(const Arc &arc)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:379
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:441
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:364