aGrUM  0.16.3
a C++ library for (probabilistic) graphical models
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 
387  void addArc(NodeId tail, NodeId head);
388 
394  void addArc(const std::string& tail, const std::string& head);
395 
402  void eraseArc(const Arc& arc);
403 
411  void eraseArc(NodeId tail, NodeId head);
412 
416  void eraseArc(const std::string& tail, const std::string& head) {
417  eraseArc(idFromName(tail), idFromName(head));
418  }
419 
437 
447  void reverseArc(NodeId tail, NodeId head);
449  void reverseArc(const std::string& tail, const std::string& head) {
450  reverseArc(idFromName(tail), idFromName(head));
451  }
452  void reverseArc(const Arc& arc);
454 
456 
457  // ===========================================================================
459  // ===========================================================================
461 
478  NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight);
479  NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight);
481  GUM_SCALAR external_weight);
501  GUM_SCALAR external_weight,
502  NodeId id);
504  GUM_SCALAR external_weight,
505  NodeId id);
507  GUM_SCALAR external_weight,
508  NodeId id);
522  GUM_SCALAR external_weight,
523  NodeId id);
524 
533  NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight);
534 
545  NodeId addLogit(const DiscreteVariable& var,
546  GUM_SCALAR external_weight,
547  NodeId id);
548 
557  NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
558 
570  NodeId addOR(const DiscreteVariable& var);
571 
583  NodeId addAND(const DiscreteVariable& var);
584 
590  NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
591  NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
592  NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
593  NodeId addMAX(const DiscreteVariable& var);
594  NodeId addMEDIAN(const DiscreteVariable& var);
595  NodeId addMIN(const DiscreteVariable& var);
608  void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
609 
620  void addWeightedArc(const std::string& tail,
621  const std::string& head,
622  GUM_SCALAR causalWeight) {
623  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
624  };
626 
628  void generateCPTs() const;
629 
631  void generateCPT(NodeId node) const;
632  void generateCPT(const std::string& name) const {
633  generateCPT(idFromName(name));
634  };
635 
641  void changePotential(const std::string& name, Potential< GUM_SCALAR >* newPot);
642 
643  private:
645  void __clearPotentials();
646 
648  void __copyPotentials(const BayesNet< GUM_SCALAR >& source);
649 
652 
655  // HashTable<NodeId, Potential<GUM_SCALAR>* > __probaMap;
656 
662 
663  public:
668  };
669 
671  template < typename GUM_SCALAR >
672  std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
673 
674 
675 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
676  extern template class BayesNet< double >;
677 #endif
678 
679 } /* namespace gum */
680 
681 #include <agrum/BN/BayesNet_tpl.h>
682 
683 #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:361
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:486
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:534
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:221
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:215
void reverseArc(const std::string &tail, const std::string &head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet.h:449
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:245
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:474
Container used to map discrete variables with nodes.
STL namespace.
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:173
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:578
NodeId addAND(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an AND implementation.
Definition: BayesNet_tpl.h:479
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:677
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:500
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:140
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:416
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:599
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:546
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:626
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:322
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:705
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:333
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:571
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:523
void generateCPT(const std::string &name) const
Definition: BayesNet.h:632
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:671
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:492
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:328
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 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:227
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:508
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:651
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable&#39;s id in the gum::BayesNet.
Definition: BayesNet_tpl.h:240
~BayesNet() final
Destructor.
Definition: BayesNet_tpl.h:206
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:633
<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:684
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:513
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:540
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:316
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:640
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:343
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:518
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:465
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:384