aGrUM  0.14.2
BayesNet.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
28 #ifndef GUM_BAYES_NET_H
29 #define GUM_BAYES_NET_H
30 
31 #include <utility>
32 
33 #include <agrum/agrum.h>
34 
35 #include <agrum/core/hashTable.h>
36 
37 #include <agrum/BN/IBayesNet.h>
39 
40 namespace gum {
41 
42  template < typename GUM_SCALAR >
44 
75  template < typename GUM_SCALAR >
76  class BayesNet : public IBayesNet< GUM_SCALAR > {
77  friend class BayesNetFactory< GUM_SCALAR >;
78 
79  public:
91  static BayesNet< GUM_SCALAR > fastPrototype(const std::string& dotlike,
92  Size domainSize = 2);
93 
94  // ===========================================================================
96  // ===========================================================================
98 
102  BayesNet();
103 
109  explicit BayesNet(std::string name);
110 
114  ~BayesNet() final;
115 
119  BayesNet(const BayesNet< GUM_SCALAR >& source);
120 
122  // ===========================================================================
124  // ===========================================================================
126 
133  BayesNet< GUM_SCALAR >& operator=(const BayesNet< GUM_SCALAR >& source);
134 
136  // ===========================================================================
138  // ===========================================================================
140 
148  const Potential< GUM_SCALAR >& cpt(NodeId varId) const final;
149 
153  const Potential< GUM_SCALAR >& cpt(const std::string& name) const {
154  return cpt(idFromName(name));
155  };
156 
162  const VariableNodeMap& variableNodeMap() const final;
163 
179  NodeId add(const DiscreteVariable& var);
180 
192  NodeId add(const std::string& name, unsigned int nbrmod);
193 
209  NodeId add(const DiscreteVariable& var,
211 
229  NodeId add(const DiscreteVariable& var, NodeId id);
230 
246  NodeId add(const DiscreteVariable& var,
248  NodeId id);
249 
260  void erase(NodeId varId);
261 
265  void erase(const std::string& name) { erase(idFromName(name)); };
266 
277  void erase(const DiscreteVariable& var);
278 
289  const DiscreteVariable& variable(NodeId id) const final;
290 
295  const DiscreteVariable& variable(const std::string& name) const {
296  return variable(idFromName(name));
297  };
298 
308  void changeVariableName(NodeId id, const std::string& new_name);
309 
313  void changeVariableName(const std::string& name, const std::string& new_name) {
314  changeVariableName(idFromName(name), new_name);
315  }
316 
327  void changeVariableLabel(NodeId id,
328  const std::string& old_label,
329  const std::string& new_label);
330 
334  void changeVariableLabel(const std::string& name,
335  const std::string& old_label,
336  const std::string& new_label) {
337  changeVariableLabel(idFromName(name), old_label, new_label);
338  }
339 
348  NodeId nodeId(const DiscreteVariable& var) const final;
349 
358  NodeId idFromName(const std::string& name) const final;
359 
369  const DiscreteVariable& variableFromName(const std::string& name) const final;
371 
372  // ===========================================================================
374  // ===========================================================================
376 
384  void addArc(NodeId tail, NodeId head);
385 
389  void addArc(const std::string& tail, const std::string& head) {
390  addArc(idFromName(tail), idFromName(head));
391  }
392 
399  void eraseArc(const Arc& arc);
400 
408  void eraseArc(NodeId tail, NodeId head);
409 
413  void eraseArc(const std::string& tail, const std::string& head) {
414  eraseArc(idFromName(tail), idFromName(head));
415  }
416 
434 
444  void reverseArc(NodeId tail, NodeId head);
446  void reverseArc(const std::string& tail, const std::string& head) {
447  reverseArc(idFromName(tail), idFromName(head));
448  }
449  void reverseArc(const Arc& arc);
451 
453 
454  // ===========================================================================
456  // ===========================================================================
458 
475  NodeId addNoisyOR(const DiscreteVariable& var, GUM_SCALAR external_weight);
476  NodeId addNoisyORNet(const DiscreteVariable& var, GUM_SCALAR external_weight);
478  GUM_SCALAR external_weight);
498  GUM_SCALAR external_weight,
499  NodeId id);
501  GUM_SCALAR external_weight,
502  NodeId id);
504  GUM_SCALAR external_weight,
505  NodeId id);
519  GUM_SCALAR external_weight,
520  NodeId id);
521 
530  NodeId addNoisyAND(const DiscreteVariable& var, GUM_SCALAR external_weight);
531 
542  NodeId addLogit(const DiscreteVariable& var,
543  GUM_SCALAR external_weight,
544  NodeId id);
545 
554  NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
555 
567  NodeId addOR(const DiscreteVariable& var);
568 
580  NodeId addAND(const DiscreteVariable& var);
581 
587  NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
588  NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
589  NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
590  NodeId addMAX(const DiscreteVariable& var);
591  NodeId addMEDIAN(const DiscreteVariable& var);
592  NodeId addMIN(const DiscreteVariable& var);
605  void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
606 
617  void addWeightedArc(const std::string& tail,
618  const std::string& head,
619  GUM_SCALAR causalWeight) {
620  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
621  };
623 
625  void generateCPTs() const;
626 
628  void generateCPT(NodeId node) const;
629  void generateCPT(const std::string& name) const {
630  generateCPT(idFromName(name));
631  };
632 
638  void changePotential(const std::string& name, Potential< GUM_SCALAR >* newPot);
639 
640  private:
642  void __clearPotentials();
643 
645  void __copyPotentials(const BayesNet< GUM_SCALAR >& source);
646 
649 
652  // HashTable<NodeId, Potential<GUM_SCALAR>* > __probaMap;
653 
659 
660  public:
665  };
666 
668  template < typename GUM_SCALAR >
669  std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
670 
671 
672 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
673  extern template class BayesNet< double >;
674 #endif
675 
676 } /* namespace gum */
677 
678 #include <agrum/BN/BayesNet_tpl.h>
679 
680 #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:345
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
Class representing a Bayesian Network.
Definition: BayesNet.h:76
void changeVariableName(const std::string &name, const std::string &new_name)
Changes a variable&#39;s name.
Definition: BayesNet.h:313
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:451
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:499
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:205
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:199
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:389
void reverseArc(const std::string &tail, const std::string &head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet.h:446
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:229
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:648
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:439
Container used to map discrete variables with nodes.
STL namespace.
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:157
void erase(const std::string &name)
Removes a variable from the gum::BayesNet.
Definition: BayesNet.h:265
Template implementation of BN/BayesNet.h class.
Class representing Bayesian networks.
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:543
NodeId addAND(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an AND implementation.
Definition: BayesNet_tpl.h:444
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:641
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:465
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:124
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:413
The class for generic Hash Tables.
Definition: hashTable.h:676
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:564
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:511
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:591
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:306
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:669
Header of the Potential class.
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
Definition: BayesNet_tpl.h:317
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:536
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:488
void generateCPT(const std::string &name) const
Definition: BayesNet.h:629
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:651
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:636
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:457
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:312
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:617
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:211
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:473
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:616
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable&#39;s id in the gum::BayesNet.
Definition: BayesNet_tpl.h:224
~BayesNet() final
Destructor.
Definition: BayesNet_tpl.h:190
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:598
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
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:648
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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:334
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:478
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:505
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:300
const DiscreteVariable & variable(const std::string &name) const
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet.h:295
void __clearPotentials()
clear all potentials
Definition: BayesNet_tpl.h:605
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:327
Class hash tables iterators.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:483
A factory class to ease BayesNet construction.
Definition: BayesNet.h:43
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:430
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:352