aGrUM  0.15.0
BayesNet.h
Go to the documentation of this file.
1 
30 #ifndef GUM_BAYES_NET_H
31 #define GUM_BAYES_NET_H
32 
33 #include <utility>
34 
35 #include <agrum/agrum.h>
36 
37 #include <agrum/core/hashTable.h>
38 
39 #include <agrum/BN/IBayesNet.h>
41 
42 namespace gum {
43 
44  template < typename GUM_SCALAR >
46 
77  template < typename GUM_SCALAR >
78  class BayesNet : public IBayesNet< GUM_SCALAR > {
79  friend class BayesNetFactory< GUM_SCALAR >;
80 
81  public:
93  static BayesNet< GUM_SCALAR > fastPrototype(const std::string& dotlike,
94  Size domainSize = 2);
95 
96  // ===========================================================================
98  // ===========================================================================
100 
104  BayesNet();
105 
111  explicit BayesNet(std::string name);
112 
116  ~BayesNet() final;
117 
121  BayesNet(const BayesNet< GUM_SCALAR >& source);
122 
124  // ===========================================================================
126  // ===========================================================================
128 
135  BayesNet< GUM_SCALAR >& operator=(const BayesNet< GUM_SCALAR >& source);
136 
138  // ===========================================================================
140  // ===========================================================================
142 
150  const Potential< GUM_SCALAR >& cpt(NodeId varId) const final;
151 
155  const Potential< GUM_SCALAR >& cpt(const std::string& name) const {
156  return cpt(idFromName(name));
157  };
158 
164  const VariableNodeMap& variableNodeMap() const final;
165 
181  NodeId add(const DiscreteVariable& var);
182 
194  NodeId add(const std::string& name, unsigned int nbrmod);
195 
211  NodeId add(const DiscreteVariable& var,
213 
231  NodeId add(const DiscreteVariable& var, NodeId id);
232 
248  NodeId add(const DiscreteVariable& var,
250  NodeId id);
251 
262  void erase(NodeId varId);
263 
267  void erase(const std::string& name) { erase(idFromName(name)); };
268 
279  void erase(const DiscreteVariable& var);
280 
291  const DiscreteVariable& variable(NodeId id) const final;
292 
297  const DiscreteVariable& variable(const std::string& name) const {
298  return variable(idFromName(name));
299  };
300 
310  void changeVariableName(NodeId id, const std::string& new_name);
311 
315  void changeVariableName(const std::string& name, const std::string& new_name) {
316  changeVariableName(idFromName(name), new_name);
317  }
318 
329  void changeVariableLabel(NodeId id,
330  const std::string& old_label,
331  const std::string& new_label);
332 
336  void changeVariableLabel(const std::string& name,
337  const std::string& old_label,
338  const std::string& new_label) {
339  changeVariableLabel(idFromName(name), old_label, new_label);
340  }
341 
350  NodeId nodeId(const DiscreteVariable& var) const final;
351 
360  NodeId idFromName(const std::string& name) const final;
361 
371  const DiscreteVariable& variableFromName(const std::string& name) const final;
373 
374  // ===========================================================================
376  // ===========================================================================
378 
386  void addArc(NodeId tail, NodeId head);
387 
391  void addArc(const std::string& tail, const std::string& head) {
392  addArc(idFromName(tail), idFromName(head));
393  }
394 
401  void eraseArc(const Arc& arc);
402 
410  void eraseArc(NodeId tail, NodeId head);
411 
415  void eraseArc(const std::string& tail, const std::string& head) {
416  eraseArc(idFromName(tail), idFromName(head));
417  }
418 
436 
446  void reverseArc(NodeId tail, NodeId head);
448  void reverseArc(const std::string& tail, const std::string& head) {
449  reverseArc(idFromName(tail), idFromName(head));
450  }
451  void reverseArc(const Arc& arc);
453 
455 
456  // ===========================================================================
458  // ===========================================================================
460 
477  NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight);
478  NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight);
480  GUM_SCALAR external_weight);
500  GUM_SCALAR external_weight,
501  NodeId id);
503  GUM_SCALAR external_weight,
504  NodeId id);
506  GUM_SCALAR external_weight,
507  NodeId id);
521  GUM_SCALAR external_weight,
522  NodeId id);
523 
532  NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight);
533 
544  NodeId addLogit(const DiscreteVariable& var,
545  GUM_SCALAR external_weight,
546  NodeId id);
547 
556  NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
557 
569  NodeId addOR(const DiscreteVariable& var);
570 
582  NodeId addAND(const DiscreteVariable& var);
583 
589  NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
590  NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
591  NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
592  NodeId addMAX(const DiscreteVariable& var);
593  NodeId addMEDIAN(const DiscreteVariable& var);
594  NodeId addMIN(const DiscreteVariable& var);
607  void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
608 
619  void addWeightedArc(const std::string& tail,
620  const std::string& head,
621  GUM_SCALAR causalWeight) {
622  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
623  };
625 
627  void generateCPTs() const;
628 
630  void generateCPT(NodeId node) const;
631  void generateCPT(const std::string& name) const {
632  generateCPT(idFromName(name));
633  };
634 
640  void changePotential(const std::string& name, Potential< GUM_SCALAR >* newPot);
641 
642  private:
644  void __clearPotentials();
645 
647  void __copyPotentials(const BayesNet< GUM_SCALAR >& source);
648 
651 
654  // HashTable<NodeId, Potential<GUM_SCALAR>* > __probaMap;
655 
661 
662  public:
667  };
668 
670  template < typename GUM_SCALAR >
671  std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
672 
673 
674 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
675  extern template class BayesNet< double >;
676 #endif
677 
678 } /* namespace gum */
679 
680 #include <agrum/BN/BayesNet_tpl.h>
681 
682 #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:348
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
Class representing a Bayesian Network.
Definition: BayesNet.h:78
void changeVariableName(const std::string &name, const std::string &new_name)
Changes a variable&#39;s name.
Definition: BayesNet.h:315
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:454
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:502
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:208
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
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.h:391
void reverseArc(const std::string &tail, const std::string &head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet.h:448
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:650
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:442
Container used to map discrete variables with nodes.
STL namespace.
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:160
void erase(const std::string &name)
Removes a variable from the gum::BayesNet.
Definition: BayesNet.h:267
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:546
NodeId addAND(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an AND implementation.
Definition: BayesNet_tpl.h:447
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:644
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:468
static BayesNet< GUM_SCALAR > fastPrototype(const std::string &dotlike, Size domainSize=2)
Create a bn with a dotlike syntax : &#39;a->b->c;b->d;&#39;.
Definition: BayesNet_tpl.h:127
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:415
The class for generic Hash Tables.
Definition: hashTable.h:679
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:567
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:514
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:594
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:309
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:672
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
Definition: BayesNet_tpl.h:320
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:539
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
NodeId addOR(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an OR implementation.
Definition: BayesNet_tpl.h:491
void generateCPT(const std::string &name) const
Definition: BayesNet.h:631
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:653
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:639
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:460
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:315
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:619
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:214
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:476
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:619
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable&#39;s id in the gum::BayesNet.
Definition: BayesNet_tpl.h:227
~BayesNet() final
Destructor.
Definition: BayesNet_tpl.h:193
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:601
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:53
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:651
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
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:336
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:481
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:508
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:303
const DiscreteVariable & variable(const std::string &name) const
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet.h:297
void __clearPotentials()
clear all potentials
Definition: BayesNet_tpl.h:608
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:330
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:486
A factory class to ease BayesNet construction.
Definition: BayesNet.h:45
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:433
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:355