aGrUM  0.13.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
543  addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id);
544 
553  NodeId addLogit(const DiscreteVariable& var, GUM_SCALAR external_weight);
554 
566  NodeId addOR(const DiscreteVariable& var);
567 
579  NodeId addAND(const DiscreteVariable& var);
580 
586  NodeId addCOUNT(const DiscreteVariable& var, Idx value = 1);
587  NodeId addEXISTS(const DiscreteVariable& var, Idx value = 1);
588  NodeId addFORALL(const DiscreteVariable& var, Idx value = 1);
589  NodeId addMAX(const DiscreteVariable& var);
590  NodeId addMEDIAN(const DiscreteVariable& var);
591  NodeId addMIN(const DiscreteVariable& var);
604  void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight);
605 
616  void addWeightedArc(const std::string& tail,
617  const std::string& head,
618  GUM_SCALAR causalWeight) {
619  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
620  };
622 
624  void generateCPTs() const;
625 
627  void generateCPT(NodeId node) const;
628  void generateCPT(const std::string& name) const {
629  generateCPT(idFromName(name));
630  };
631 
637  void changePotential(const std::string& name, Potential< GUM_SCALAR >* newPot);
638 
639  private:
641  void __clearPotentials();
642 
644  void __copyPotentials(const BayesNet< GUM_SCALAR >& source);
645 
648 
651  // HashTable<NodeId, Potential<GUM_SCALAR>* > __probaMap;
652 
658 
659  public:
664  };
665 
667  template < typename GUM_SCALAR >
668  std::ostream& operator<<(std::ostream& output, const BayesNet< GUM_SCALAR >& bn);
669 
670  extern template class BayesNet< float >;
671  extern template class BayesNet< double >;
672 
673 } /* namespace gum */
674 
675 #include <agrum/BN/BayesNet_tpl.h>
676 
677 #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:335
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
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:441
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
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:489
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:195
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:189
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:219
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:647
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:429
Container used to map discrete variables with nodes.
STL namespace.
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:147
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:533
NodeId addAND(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an AND implementation.
Definition: BayesNet_tpl.h:434
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:455
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:115
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:554
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:501
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:581
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:296
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:659
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:307
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:526
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
void generateCPT(const std::string &name) const
Definition: BayesNet.h:628
NodeId addOR(const DiscreteVariable &var)
Add a variable, it&#39;s associate node and an OR implementation.
Definition: BayesNet_tpl.h:478
NodeProperty< Potential< GUM_SCALAR > * > __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:650
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
Definition: BayesNet_tpl.h:447
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:302
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:616
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:201
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:463
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:606
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable&#39;s id in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
~BayesNet() final
Destructor.
Definition: BayesNet_tpl.h:180
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:588
<agrum/multidim/multiDimImplementation.h>
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:631
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:638
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:468
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:495
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:290
void __clearPotentials()
clear all potentials
Definition: BayesNet_tpl.h:595
unsigned long Idx
Type for indexes.
Definition: types.h:43
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:317
Class hash tables iterators.
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 generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:626
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:473
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:420
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:342