aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
BayesNet.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
104  Size domainSize = 2);
105 
106  // ===========================================================================
107  /// @name Constructors and Destructor
108  // ===========================================================================
109  /// @{
110 
111  /**
112  * @brief Default constructor.
113  */
114  BayesNet();
115 
116  /**
117  * @brief Default constructor.
118  *
119  * @param name The BayesNet's name.
120  */
121  explicit BayesNet(std::string name);
122 
123  /**
124  * @brief Destructor.
125  */
126  virtual ~BayesNet() final;
127 
128  /**
129  * @brief Copy constructor.
130  */
131  BayesNet(const BayesNet< GUM_SCALAR >& source);
132 
133  /// @}
134  // ===========================================================================
135  /// @name Operators
136  // ===========================================================================
137  /// @{
138 
139  /**
140  * @brief Copy operator.
141  *
142  * @param source The copied BayesNet.
143  * @return The copy of source.
144  */
145  BayesNet< GUM_SCALAR >& operator=(const BayesNet< GUM_SCALAR >& source);
146 
147  /// @}
148  // ===========================================================================
149  /// @name Variable manipulation methods
150  // ===========================================================================
151  /// @{
152 
153  /**
154  * @brief Returns the CPT of a variable.
155  *
156  * @param varId A variable's id in the gum::BayesNet.
157  * @return The variable's CPT.
158  * @throw NotFound If no variable's id matches varId.
159  */
160  const Potential< GUM_SCALAR >& cpt(NodeId varId) const final;
161 
162  /**
163  * @brief Returns the CPT of a variable.
164  */
165  const Potential< GUM_SCALAR >& cpt(const std::string& name) const {
166  return cpt(idFromName(name));
167  };
168 
169  /**
170  * @brief Returns a map between variables and nodes of this gum::BayesNet.
171  *
172  * @return Returns a constant reference to the gum::VariableNodeMap.
173  */
174  const VariableNodeMap& variableNodeMap() const final;
175 
176  /**
177  * @brief Add a variable to the gum::BayesNet.
178  *
179  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
180  * gum::Potential.
181  *
182  * The variable is added by copy to the gum::BayesNet.
183  * The variable's gum::Potential implementation will be a
184  * gum::MultiDimArray.
185  *
186  * @param var The variable added by copy.
187  * @return Returns the variable's id in the gum::BayesNet.
188  * @throws DuplicateLabel Raised if variable.name() is already used in this
189  * gum::BayesNet.
190  */
191  NodeId add(const DiscreteVariable& var);
192 
193  /**
194  * @brief Shortcut for add(gum::LabelizedVariable(name,name,nbrmod))
195  *
196  * Add a gum::LabelizedVariable to the gum::BayesNet
197  *
198  * This method is just a shortcut for a often used pattern
199  *
200  * @throws DuplicateLabel Raised if variable.name() is already used in this
201  * gum::BayesNet.
202  * @throws NotAllowed if nbrmod<2
203  */
204  NodeId add(const std::string& name, unsigned int nbrmod);
205 
206  /**
207  * @brief Add a variable to the gum::BayesNet.
208  *
209  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
210  * gum::Potential.
211  *
212  * The variable is added by copy to the gum::BayesNet.
213  *
214  * @param var The variable added by copy.
215  * @param aContent The gum::MultiDimImplementation to use for this
216  * variable's gum::Potential implementation.
217  * @return Returns the variable's id in the gum::BayesNet.
218  * @throws DuplicateLabel Raised if variable.name() is already used in this
219  * gum::BayesNet.
220  */
223 
224  /**
225  * @brief Add a variable to the gum::BayesNet.
226  *
227  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
228  * gum::Potential.
229  *
230  * The variable is added by copy to the gum::BayesNet.
231  * The variable's gum::Potential implementation will be a
232  * gum::MultiDimArray.
233  *
234  * @param var The variable added by copy.
235  * @param id The variable's forced gum::NodeId in the gum::BayesNet.
236  * @return Returns the variable's id in the gum::BayesNet.
237  * @throws DuplicateElement Raised id is already used.
238  * @throws DuplicateLabel Raised if variable.name() is already used in this
239  * gum::BayesNet.
240  */
242 
243  /**
244  * @brief Add a variable to the gum::BayesNet.
245  *
246  * Add a gum::DiscreteVariable, it's associated gum::NodeId and it's
247  * gum::Potential.
248  *
249  * @param var The variable added by copy.
250  * @param aContent The gum::MultiDimImplementation to use for this
251  * variable's gum::Potential implementation.
252  * @param id The variable's forced gum::NodeId in the gum::BayesNet.
253  * @return Returns the variable's id in the gum::BayesNet.
254  * @throws DuplicateElement Raised id is already used.
255  * @throws DuplicateLabel Raised if variable.name() is already used in this
256  * gum::BayesNet.
257  */
260  NodeId id);
261 
262  /**
263  * @brief clear the whole Bayes net *
264  */
265  void clear();
266 
267  /**
268  * @brief Remove a variable from the gum::BayesNet.
269  *
270  * Removes the corresponding variable from the gum::BayesNet and from
271  * all of it's children gum::Potential.
272  *
273  * If no variable matches the given id, then nothing is done.
274  *
275  * @param varId The variable's id to remove.
276  */
277  void erase(NodeId varId);
278 
279  /**
280  * @brief Removes a variable from the gum::BayesNet.
281  */
282  void erase(const std::string& name) { erase(idFromName(name)); };
283 
284  /**
285  * @brief Remove a variable from the gum::BayesNet.
286  *
287  * Removes the corresponding variable from the gum::BayesNet and from
288  * all of it's children gum::Potential.
289  *
290  * If no variable matches the given variable, then nothing is done.
291  *
292  * @param var A reference on the variable to remove.
293  */
294  void erase(const DiscreteVariable& var);
295 
296  /**
297  * @brief Returns a gum::DiscreteVariable given its gum::NodeId in the
298  * gum::BayesNet.
299  *
300  * @param id The variable's id to return.
301  * @returns Returns a constant reference of the gum::DiscreteVariable
302  * corresponding to id in the gum::BayesNet.
303  * @throw NotFound Raised if id does not match a a variable in the
304  * gum::BayesNet.
305  */
306  const DiscreteVariable& variable(NodeId id) const final;
307 
308  /**
309  * @brief Returns a gum::DiscreteVariable given its gum::NodeId in the
310  * gum::BayesNet.
311  */
312  const DiscreteVariable& variable(const std::string& name) const {
313  return variable(idFromName(name));
314  };
315 
316  /**
317  * @brief Changes a variable's name in the gum::BayesNet.
318  *
319  * This will change the gum::DiscreteVariable names in the gum::BayesNet.
320  *
321  * @throws DuplicateLabel Raised if newName is already used in this
322  * gum::BayesNet.
323  * @throws NotFound Raised if no variable matches id.
324  */
325  void changeVariableName(NodeId id, const std::string& new_name);
326 
327  /**
328  * @brief Changes a variable's name.
329  */
330  void changeVariableName(const std::string& name, const std::string& new_name) {
331  changeVariableName(idFromName(name), new_name);
332  }
333 
334  /**
335  * @brief Changes a variable's label in the gum::BayesNet.
336  *
337  * This will change the gum::LabelizedVariable names in the gum::BayesNet.
338  *
339  * @throws DuplicateLabel Raised if new_label is already used in this
340  * gum::LabelizedVariable.
341  * @throws NotFound Raised if no variable matches id or if the variable is not
342  * a LabelizedVariable
343  */
344  void changeVariableLabel(NodeId id,
345  const std::string& old_label,
346  const std::string& new_label);
347 
348  /**
349  * @brief Changes a variable's name.
350  */
351  void changeVariableLabel(const std::string& name,
352  const std::string& old_label,
353  const std::string& new_label) {
354  changeVariableLabel(idFromName(name), old_label, new_label);
355  }
356 
357  /**
358  * @brief Returns a variable's id in the gum::BayesNet.
359  *
360  * @param var The variable from which the gum::NodeId is returned.
361  * @return Returns the gum::DiscreteVariable gum::NodeId in the
362  * gum::BayesNet.
363  * @throw NotFound If var is not in the gum::BayesNet.
364  */
365  NodeId nodeId(const DiscreteVariable& var) const final;
366 
367  /**
368  * @brief Returns a variable's id given its name in the gum::BayesNet.
369  *
370  * @param name The variable's name from which the gum::NodeId is returned.
371  * @return Returns the variable gum::NodeId in the gum::BayesNet.
372  * @throw NotFound Raised if name does not match a variable in the
373  * gum::BayesNet.
374  */
375  NodeId idFromName(const std::string& name) const final;
376 
377  /**
378  * @brief Returns a variable given its name in the gum::BayesNet.
379  *
380  * @param name The variable's name in the gum::BayesNet.
381  * @return Returns the gum::DiscreteVariable named name in the
382  * gum::BayesNet.
383  * @throw NotFound Raised if name does not match a variable in the
384  * gum::BayesNet.
385  */
386  const DiscreteVariable& variableFromName(const std::string& name) const final;
387  /// @}
388 
389  // ===========================================================================
390  /// @name Arc manipulation methods.
391  // ===========================================================================
392  /// @{
393 
394  /**
395  * Add an arc in the BN, and update arc.head's CPT.
396  *
397  * @param head and
398  * @param tail as NodeId
399  * @throw InvalidEdge If arc.tail and/or arc.head are not in the BN.
400  * @throw DuplicateElement if the arc already exists
401  */
402  void addArc(NodeId tail, NodeId head);
403 
404  /**
405  * Add an arc in the BN, and update arc.head's CPT
406  *
407  * @throw gum::DuplicateElement if the arc already exists
408  */
409  void addArc(const std::string& tail, const std::string& head);
410 
411  /**
412  * Removes an arc in the BN, and update head's CTP.
413  *
414  * If (tail, head) doesn't exist, the nothing happens.
415  * @param arc The arc removed.
416  */
417  void eraseArc(const Arc& arc);
418 
419  /**
420  * Removes an arc in the BN, and update head's CTP.
421  *
422  * If (tail, head) doesn't exist, the nothing happens.
423  * @param head and
424  * @param tail as NodeId
425  */
426  void eraseArc(NodeId tail, NodeId head);
427 
428  /**
429  * Removes an arc in the BN, and update head's CTP.
430  */
431  void eraseArc(const std::string& tail, const std::string& head) {
432  eraseArc(idFromName(tail), idFromName(head));
433  }
434 
435  /**
436  * When inserting/removing arcs, node CPTs change their dimension with a
437  * cost in
438  * time.
439  * These functions delay the CPTs change to be done just once at the end of
440  * a
441  * sequence of topology modification.
442  */
443  ///@{
444  /// begins a sequence of insertions/deletions of arcs without changing the
445  /// dimensions of the CPTs.
447  /// terminates a sequence of insertions/deletions of arcs by adjusting all
448  /// CPTs
449  /// dimensions.
451  ///@}
452 
453  /**
454  * @brief Reverses an arc while preserving the same joint distribution.
455  *
456  * This method uses Shachter's 1986 algorithm for reversing an arc in
457  * the Bayes net while preserving the same joint distribution. By
458  * performing this reversal, we also add new arcs (required to not alter
459  * the joint distribution)
460  * @throws InvalidArc exception if the arc does not exist or if its reversal
461  * would induce a directed cycle. */
462  /// @{
463  void reverseArc(NodeId tail, NodeId head);
464  void reverseArc(const std::string& tail, const std::string& head) {
465  reverseArc(idFromName(tail), idFromName(head));
466  }
467  void reverseArc(const Arc& arc);
468  /// @}
469 
470  /// @}
471 
472  // ===========================================================================
473  /// @name Accessors for nodes with CI or logical implementation
474  // ===========================================================================
475  /// @{
476 
477  /**
478  * Add a variable, it's associate node and a gum::noisyOR implementation.
479  *The id
480  *of the new
481  * variable is automatically generated. Since it seems that the 'classical'
482  *noisyOR is the Compound noisyOR, we keep
483  * the gum::BayesNet::addNoisyOR as an alias for
484  *gum::BayesNet::addNoisyORCompound
485  *
486  * @param var The variable added by copy.
487  * @param external_weight see ref
488  *gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
489  * @return the id of the added variable.
490  * @{
491  */
492 
497  /** @} */
498 
499  /**
500  * Add a variable, its associate node and a noisyOR implementation. Since
501  * it seems that the 'classical' noisyOR is the Compound noisyOR, we keep
502  * the addNoisyOR as an alias for addNoisyORCompound.
503  *
504  * @param var The variable added by copy.
505  * @param external_weight see gum::MultiDimNoisyORNet,
506  *gum::MultiDimNoisyORCompound
507  * @param id The chosen id
508  * @warning give an id should be reserved for rare and specific situations
509  *!!!
510  * @return the id of the added variable.
511  * @throws DuplicateElement if id is already used
512  *
513  * @{
514  */
517  NodeId id);
520  NodeId id);
523  NodeId id);
524  /** @} */
525 
526  /**
527  * Add a variable, its associate node and a noisyAND implementation.
528  *
529  * @param var The variable added by copy
530  * @param external_weight see gum::MultiDimNoisyAND
531  * @param id proposed gum::nodeId for the variable
532  * @warning give an id should be reserved for rare and specific situations
533  *!!!
534  * @return the id of the added variable.
535  */
538  NodeId id);
539 
540  /**
541  * Add a variable, its associate node and a noisyAND implementation. The id
542  *of the new variable is automatically generated.
543  *
544  * @param var The variable added by copy.
545  * @param external_weight see gum::MultiDimNoisyAND
546  * @return the id of the added variable.
547  */
549 
550  /**
551  * Add a variable, its associate node and a Logit implementation.
552  *
553  * @param var The variable added by copy
554  * @param external_weight see gum::MultiDimLogit
555  * @param id proposed gum::nodeId for the variable
556  * @warning give an id should be reserved for rare and specific situations
557  *!!!
558  * @return the id of the added variable.
559  */
562  NodeId id);
563 
564  /**
565  * Add a variable, its associate node and a Logit implementation. The id of
566  *the new variable is automatically generated.
567  *
568  * @param var The variable added by copy.
569  * @param external_weight see gum::MultiDimLogit
570  * @return the id of the added variable.
571  */
573 
574  /**
575  * Add a variable, it's associate node and an OR implementation. The id of
576  *the new variable is automatically generated.
577  *
578  * @warning OR is implemented as a gum::aggregator::Or which means that if
579  *parents are not boolean, all value>1 is True
580  *
581  * @param var The variable added by copy.
582  * @return the id of the added variable.
583  * @throws SizeError if variable.domainSize()>2
584  */
586 
587  /**
588  * Add a variable, it's associate node and an AND implementation. The id of
589  *the new variable is automatically generated.
590  *
591  * @warning AND is implemented as a gum::aggregator::And which means that if
592  *parents are not boolean, all value>1 is True
593  *
594  * @param var The variable added by copy.
595  * @return the id of the added variable.
596  * @throws SizeError if variable.domainSize()>2
597  */
599 
600  /**
601  * Others aggregators
602  * @{
603  */
612  /**
613  * @}
614  */
615  /**
616  * Add an arc in the BN, and update arc.head's CPT.
617  *
618  * @param head and
619  * @param tail as NodeId
620  * @param causalWeight see gum::MultiDimICIModel
621  * @throw InvalidArc If arc.tail and/or arc.head are not in the BN.
622  * @throw InvalidArc If variable in arc.head is not a NoisyOR variable.
623  */
624  void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
625 
626  /**
627  * Add an arc in the BN, and update arc.head's CPT.
628  *
629  * @param head and
630  * @param tail as std::string
631  * @param causalWeight see gum::MultiDimICIModel
632  * @param NotFound if no node with sun names is found
633  * @throw InvalidArc If arc.tail and/or arc.head are not in the BN.
634  * @throw InvalidArc If variable in arc.head is not a NoisyOR variable.
635  */
636  void addWeightedArc(const std::string& tail,
637  const std::string& head,
638  GUM_SCALAR causalWeight) {
639  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
640  };
641  /// @}
642 
643  /// randomly generates CPTs for a given structure
644  void generateCPTs() const;
645 
646  /// randomly generate CPT for a given node in a given structure
647  void generateCPT(NodeId node) const;
648  void generateCPT(const std::string& name) const {
649  generateCPT(idFromName(name));
650  };
651 
652  /// change the CPT associated to nodeId to newPot
653  /// delete the old CPT associated to nodeId.
654  /// @throw NotAllowed if newPot has not the same signature as
655  /// probaMap__[NodeId]
656  void changePotential(NodeId id, Potential< GUM_SCALAR >* newPot);
657  void changePotential(const std::string& name, Potential< GUM_SCALAR >* newPot);
658 
659  private:
660  /// clear all potentials
661  void clearPotentials__();
662 
663  /// copy of potentials from a BN to another, using names of vars as ref.
664  void copyPotentials__(const BayesNet< GUM_SCALAR >& source);
665 
666  /// the map between variable and id
668 
669  /// Mapping between the variable's id and their CPT.
671  // HashTable<NodeId, Potential<GUM_SCALAR>* > probaMap__;
672 
673  /// change the CPT associated to nodeId to newPot
674  /// delete the old CPT associated to nodeId.
675  /// @warning no verification of dimensions are performer
676  /// @see changePotential
677  void unsafeChangePotential_(NodeId id, Potential< GUM_SCALAR >* newPot);
678 
679  public:
680  using IBayesNet< GUM_SCALAR >::dag;
681  using IBayesNet< GUM_SCALAR >::size;
682  using IBayesNet< GUM_SCALAR >::nodes;
684  };
685 
686  /// Prints map's DAG in output using the Graphviz-dot format.
687  template < typename GUM_SCALAR >
689 
690 
691 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
692  extern template class BayesNet< double >;
693 #endif
694 
695 } /* namespace gum */
696 
697 #include <agrum/BN/BayesNet_tpl.h>
698 
699 #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:372
void changeVariableName(const std::string &name, const std::string &new_name)
Changes a variable&#39;s name.
Definition: BayesNet.h:330
BayesNet< GUM_SCALAR > & operator=(const BayesNet< GUM_SCALAR > &source)
Copy operator.
Definition: BayesNet_tpl.h:191
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:497
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:549
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:219
NodeId addLogit(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, its associate node and a Logit implementation.
Definition: BayesNet_tpl.h:574
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:213
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:384
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void reverseArc(const std::string &tail, const std::string &head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet.h:464
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:245
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:485
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:171
NodeId add(const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:275
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:594
NodeId addAND(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an AND implementation.
Definition: BayesNet_tpl.h:490
void clear()
clear the whole Bayes net *
Definition: BayesNet_tpl.h:362
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:696
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:602
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:511
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:580
BayesNet(const BayesNet< GUM_SCALAR > &source)
Copy constructor.
Definition: BayesNet_tpl.h:182
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:138
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:431
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:618
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:562
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:645
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:323
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:670
void eraseArc(NodeId tail, NodeId head)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:405
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:637
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
Definition: BayesNet_tpl.h:334
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:587
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:724
NodeId addNoisyAND(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, its associate node and a noisyAND implementation.
Definition: BayesNet_tpl.h:568
BayesNet(std::string name)
Default constructor.
Definition: BayesNet_tpl.h:176
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:670
NodeId addOR(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an OR implementation.
Definition: BayesNet_tpl.h:534
void generateCPT(const std::string &name) const
Definition: BayesNet.h:648
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:690
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:503
void clearPotentials__()
clear all potentials
Definition: BayesNet_tpl.h:659
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:329
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:667
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:636
void erase(const DiscreteVariable &var)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:339
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:226
NodeId add(const std::string &name, unsigned int nbrmod)
Shortcut for add(gum::LabelizedVariable(name,name,nbrmod))
Definition: BayesNet_tpl.h:261
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:519
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable&#39;s id in the gum::BayesNet.
Definition: BayesNet_tpl.h:240
virtual ~BayesNet() final
Destructor.
Definition: BayesNet_tpl.h:204
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:652
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:611
const Potential< GUM_SCALAR > & cpt(const std::string &name) const
Returns the CPT of a variable.
Definition: BayesNet.h:165
NodeId add(const DiscreteVariable &var, NodeId id)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:286
NodeId add(const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent, NodeId id)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:304
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:703
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:351
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:524
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:556
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:317
const DiscreteVariable & variable(const std::string &name) const
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet.h:312
NodeId addSUM(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:541
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:344
void changePotential(const std::string &name, Potential< GUM_SCALAR > *newPot)
Definition: BayesNet_tpl.h:732
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:529
void reverseArc(const Arc &arc)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:410
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:476
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:395