aGrUM  0.18.1
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 
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:
104  static BayesNet< GUM_SCALAR > fastPrototype(const std::string& dotlike,
105  Size domainSize = 2);
106 
107  // ===========================================================================
109  // ===========================================================================
111 
115  BayesNet();
116 
122  explicit BayesNet(std::string name);
123 
127  virtual ~BayesNet() final;
128 
132  BayesNet(const BayesNet< GUM_SCALAR >& source);
133 
135  // ===========================================================================
137  // ===========================================================================
139 
146  BayesNet< GUM_SCALAR >& operator=(const BayesNet< GUM_SCALAR >& source);
147 
149  // ===========================================================================
151  // ===========================================================================
153 
161  const Potential< GUM_SCALAR >& cpt(NodeId varId) const final;
162 
166  const Potential< GUM_SCALAR >& cpt(const std::string& name) const {
167  return cpt(idFromName(name));
168  };
169 
175  const VariableNodeMap& variableNodeMap() const final;
176 
192  NodeId add(const DiscreteVariable& var);
193 
205  NodeId add(const std::string& name, unsigned int nbrmod);
206 
222  NodeId add(const DiscreteVariable& var,
224 
242  NodeId add(const DiscreteVariable& var, NodeId id);
243 
259  NodeId add(const DiscreteVariable& var,
261  NodeId id);
262 
266  void clear();
267 
278  void erase(NodeId varId);
279 
283  void erase(const std::string& name) { erase(idFromName(name)); };
284 
295  void erase(const DiscreteVariable& var);
296 
307  const DiscreteVariable& variable(NodeId id) const final;
308 
313  const DiscreteVariable& variable(const std::string& name) const {
314  return variable(idFromName(name));
315  };
316 
326  void changeVariableName(NodeId id, const std::string& new_name);
327 
331  void changeVariableName(const std::string& name, const std::string& new_name) {
332  changeVariableName(idFromName(name), new_name);
333  }
334 
345  void changeVariableLabel(NodeId id,
346  const std::string& old_label,
347  const std::string& new_label);
348 
352  void changeVariableLabel(const std::string& name,
353  const std::string& old_label,
354  const std::string& new_label) {
355  changeVariableLabel(idFromName(name), old_label, new_label);
356  }
357 
366  NodeId nodeId(const DiscreteVariable& var) const final;
367 
376  NodeId idFromName(const std::string& name) const final;
377 
387  const DiscreteVariable& variableFromName(const std::string& name) const final;
389 
390  // ===========================================================================
392  // ===========================================================================
394 
403  void addArc(NodeId tail, NodeId head);
404 
410  void addArc(const std::string& tail, const std::string& head);
411 
418  void eraseArc(const Arc& arc);
419 
427  void eraseArc(NodeId tail, NodeId head);
428 
432  void eraseArc(const std::string& tail, const std::string& head) {
433  eraseArc(idFromName(tail), idFromName(head));
434  }
435 
453 
463  void reverseArc(NodeId tail, NodeId head);
465  void reverseArc(const std::string& tail, const std::string& head) {
466  reverseArc(idFromName(tail), idFromName(head));
467  }
468  void reverseArc(const Arc& arc);
470 
472 
473  // ===========================================================================
475  // ===========================================================================
477 
494  NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight);
495  NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight);
497  GUM_SCALAR external_weight);
517  GUM_SCALAR external_weight,
518  NodeId id);
520  GUM_SCALAR external_weight,
521  NodeId id);
523  GUM_SCALAR external_weight,
524  NodeId id);
538  GUM_SCALAR external_weight,
539  NodeId id);
540 
549  NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight);
550 
561  NodeId addLogit(const DiscreteVariable& var,
562  GUM_SCALAR external_weight,
563  NodeId id);
564 
573  NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
574 
586  NodeId addOR(const DiscreteVariable& var);
587 
599  NodeId addAND(const DiscreteVariable& var);
600 
606  NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
607  NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
608  NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
609  NodeId addMAX(const DiscreteVariable& var);
610  NodeId addMEDIAN(const DiscreteVariable& var);
611  NodeId addMIN(const DiscreteVariable& var);
612  NodeId addSUM(const DiscreteVariable& var);
625  void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
626 
637  void addWeightedArc(const std::string& tail,
638  const std::string& head,
639  GUM_SCALAR causalWeight) {
640  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
641  };
643 
645  void generateCPTs() const;
646 
648  void generateCPT(NodeId node) const;
649  void generateCPT(const std::string& name) const {
650  generateCPT(idFromName(name));
651  };
652 
658  void changePotential(const std::string& name, Potential< GUM_SCALAR >* newPot);
659 
660  private:
662  void clearPotentials__();
663 
665  void copyPotentials__(const BayesNet< GUM_SCALAR >& source);
666 
669 
672  // HashTable<NodeId, Potential<GUM_SCALAR>* > probaMap__;
673 
679 
680  public:
685  };
686 
688  template < typename GUM_SCALAR >
689  std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
690 
691 
692 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
693  extern template class BayesNet< double >;
694 #endif
695 
696 } /* namespace gum */
697 
698 #include <agrum/BN/BayesNet_tpl.h>
699 
700 #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:370
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:331
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:495
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:547
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:220
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
void reverseArc(const std::string &tail, const std::string &head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet.h:465
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:483
Container used to map discrete variables with nodes.
STL namespace.
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:172
void erase(const std::string &name)
Removes a variable from the gum::BayesNet.
Definition: BayesNet.h:283
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
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:591
NodeId addAND(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an AND implementation.
Definition: BayesNet_tpl.h:488
void clear()
clear the whole Bayes net *
Definition: BayesNet_tpl.h:360
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:690
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:509
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:139
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:432
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:612
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:559
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:639
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:321
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:664
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
Definition: BayesNet_tpl.h:332
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:584
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:718
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
NodeId addOR(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an OR implementation.
Definition: BayesNet_tpl.h:532
void generateCPT(const std::string &name) const
Definition: BayesNet.h:649
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:684
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:501
void clearPotentials__()
clear all potentials
Definition: BayesNet_tpl.h:653
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:327
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
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:637
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 addMAX(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:517
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable&#39;s id in the gum::BayesNet.
Definition: BayesNet_tpl.h:239
virtual ~BayesNet() final
Destructor.
Definition: BayesNet_tpl.h:205
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:646
<agrum/tools/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:697
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:352
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:522
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:553
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:315
const DiscreteVariable & variable(const std::string &name) const
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet.h:313
NodeId addSUM(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:539
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:342
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:527
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:474
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:393