aGrUM  0.13.2
BayesNet_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
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  ***************************************************************************/
27 #include <limits>
28 #include <set>
29 
30 #include <agrum/BN/BayesNet.h>
31 #include <agrum/BN/IBayesNet.h>
32 #include <agrum/BN/IBayesNet.h>
33 
35 
45 
49 
51 
54 
55 namespace gum {
56  template < typename GUM_SCALAR >
58  std::string node,
59  gum::Size domainSize) {
60  std::string name = node;
61  auto ds = domainSize;
62  std::vector< std::string > labels;
63 
64  // node like "n[5]"
65  auto posBrack = node.find('[');
66  if (posBrack != std::string::npos) {
67  if (*(node.rbegin()) != ']')
68  name = node; // a name with '[' inside but no ']' at the end
69  else {
70  name = node.substr(0, posBrack);
71  ds = static_cast< Size >(
72  std::stoi(node.substr(posBrack + 1, node.size() - posBrack - 2)));
73  }
74  }
75 
76  // node like "n{one|two|three}"
77  posBrack = node.find('{');
78  if (posBrack != std::string::npos) {
79  if (*(node.rbegin()) != '}')
80  name = node; // a name with '{' inside but no '}' at the end
81  else {
82  name = node.substr(0, posBrack);
83  labels = split(node.substr(posBrack + 1, node.size() - posBrack - 2), "|");
84  if (labels.size() < 2) {
85  GUM_ERROR(InvalidArgument, "Not enough labels in node " << node);
86  }
87  if (!hasUniqueElts(labels)) {
88  GUM_ERROR(InvalidArgument, "Duplicate labels in node " << node);
89  }
90  ds = static_cast< Size >(labels.size());
91  }
92  }
93 
94  // now we add the node in the BN
95  NodeId idVar;
96  try {
97  idVar = bn.idFromName(name);
98  } catch (gum::NotFound&) {
99  if (labels.empty()) {
100  idVar = bn.add(LabelizedVariable(name, name, ds));
101  } else {
102  auto l = LabelizedVariable(name, name, 0);
103  for (const auto& label : labels) {
104  l.addLabel(label);
105  }
106  idVar = bn.add(l);
107  }
108  }
109 
110  return idVar;
111  }
112 
113  template < typename GUM_SCALAR >
115  BayesNet< GUM_SCALAR >::fastPrototype(const std::string& dotlike,
116  Size domainSize) {
118 
119 
120  for (const auto& chaine : split(dotlike, ";")) {
121  NodeId lastId = 0;
122  bool notfirst = false;
123  for (const auto& souschaine : split(chaine, "->")) {
124  bool forward = true;
125  for (const auto& node : split(souschaine, "<-")) {
126  auto idVar = build_node(bn, node, domainSize);
127  if (notfirst) {
128  if (forward) {
129  bn.addArc(lastId, idVar);
130  forward = false;
131  } else {
132  bn.addArc(idVar, lastId);
133  }
134  } else {
135  notfirst = true;
136  forward = false;
137  }
138  lastId = idVar;
139  }
140  }
141  }
142  bn.generateCPTs();
143  return bn;
144  }
145 
146  template < typename GUM_SCALAR >
147  INLINE BayesNet< GUM_SCALAR >::BayesNet() : IBayesNet< GUM_SCALAR >() {
148  GUM_CONSTRUCTOR(BayesNet);
149  }
150 
151  template < typename GUM_SCALAR >
152  INLINE BayesNet< GUM_SCALAR >::BayesNet(std::string name) :
153  IBayesNet< GUM_SCALAR >(name) {
154  GUM_CONSTRUCTOR(BayesNet);
155  }
156 
157  template < typename GUM_SCALAR >
159  IBayesNet< GUM_SCALAR >(source), __varMap(source.__varMap) {
160  GUM_CONS_CPY(BayesNet);
161 
162  __copyPotentials(source);
163  }
164 
165  template < typename GUM_SCALAR >
168  if (this != &source) {
170  __varMap = source.__varMap;
171 
173  __copyPotentials(source);
174  }
175 
176  return *this;
177  }
178 
179  template < typename GUM_SCALAR >
181  GUM_DESTRUCTOR(BayesNet);
182  for (const auto p : __probaMap) {
183  delete p.second;
184  }
185  }
186 
187  template < typename GUM_SCALAR >
188  INLINE const DiscreteVariable&
190  return __varMap.get(id);
191  }
192 
193  template < typename GUM_SCALAR >
194  INLINE void
196  const std::string& new_name) {
197  __varMap.changeName(id, new_name);
198  }
199 
200  template < typename GUM_SCALAR >
202  NodeId id, const std::string& old_label, const std::string& new_label) {
203  if (variable(id).varType() != VarType::Labelized) {
204  GUM_ERROR(NotFound, "Variable " << id << " is not a LabelizedVariable.");
205  }
206  LabelizedVariable* var = dynamic_cast< LabelizedVariable* >(
207  const_cast< DiscreteVariable* >(&variable(id)));
208 
209  var->changeLabel(var->posLabel(old_label), new_label);
210  }
211 
212 
213  template < typename GUM_SCALAR >
215  return __varMap.get(var);
216  }
217 
218  template < typename GUM_SCALAR >
220  auto ptr = new MultiDimArray< GUM_SCALAR >();
221  NodeId res = 0;
222 
223  try {
224  res = add(var, ptr);
225 
226  } catch (Exception&) {
227  delete ptr;
228  throw;
229  }
230 
231  return res;
232  }
233 
234  template < typename GUM_SCALAR >
235  INLINE NodeId BayesNet< GUM_SCALAR >::add(const std::string& name,
236  unsigned int nbrmod) {
237  if (nbrmod < 2) {
239  "Variable " << name << "needs more than " << nbrmod
240  << " modalities");
241  }
242 
243  LabelizedVariable v(name, name, nbrmod);
244  return add(v);
245  }
246 
247  template < typename GUM_SCALAR >
250  NodeId proposedId = dag().nextNodeId();
251  NodeId res = 0;
252 
253  res = add(var, aContent, proposedId);
254 
255  return res;
256  }
257 
258  template < typename GUM_SCALAR >
260  NodeId id) {
261  auto ptr = new MultiDimArray< GUM_SCALAR >();
262  NodeId res = 0;
263 
264  try {
265  res = add(var, ptr, id);
266 
267  } catch (Exception&) {
268  delete ptr;
269  throw;
270  }
271 
272  return res;
273  }
274 
275  template < typename GUM_SCALAR >
276  NodeId
279  NodeId id) {
280  __varMap.insert(id, var);
281  this->_dag.addNodeWithId(id);
282 
283  auto cpt = new Potential< GUM_SCALAR >(aContent);
284  (*cpt) << variable(id);
285  __probaMap.insert(id, cpt);
286  return id;
287  }
288 
289  template < typename GUM_SCALAR >
290  INLINE NodeId BayesNet< GUM_SCALAR >::idFromName(const std::string& name) const {
291  return __varMap.idFromName(name);
292  }
293 
294  template < typename GUM_SCALAR >
295  INLINE const DiscreteVariable&
296  BayesNet< GUM_SCALAR >::variableFromName(const std::string& name) const {
297  return __varMap.variableFromName(name);
298  }
299 
300  template < typename GUM_SCALAR >
301  INLINE const Potential< GUM_SCALAR >&
303  return *(__probaMap[varId]);
304  }
305 
306  template < typename GUM_SCALAR >
308  return __varMap;
309  }
310 
311  template < typename GUM_SCALAR >
313  erase(__varMap.get(var));
314  }
315 
316  template < typename GUM_SCALAR >
318  if (__varMap.exists(varId)) {
319  // Reduce the variable child's CPT
320  const NodeSet& children = this->children(varId);
321 
322  for (const auto c : children) {
323  __probaMap[c]->erase(variable(varId));
324  }
325 
326  delete __probaMap[varId];
327 
328  __probaMap.erase(varId);
329  __varMap.erase(varId);
330  this->_dag.eraseNode(varId);
331  }
332  }
333 
334  template < typename GUM_SCALAR >
335  INLINE void BayesNet< GUM_SCALAR >::addArc(NodeId tail, NodeId head) {
336  this->_dag.addArc(tail, head);
337  // Add parent in the child's CPT
338  (*(__probaMap[head])) << variable(tail);
339  }
340 
341  template < typename GUM_SCALAR >
342  INLINE void BayesNet< GUM_SCALAR >::eraseArc(const Arc& arc) {
343  if (__varMap.exists(arc.tail()) && __varMap.exists(arc.head())) {
344  NodeId head = arc.head(), tail = arc.tail();
345  this->_dag.eraseArc(arc);
346  // Remove parent froms child's CPT
347  (*(__probaMap[head])) >> variable(tail);
348  }
349  }
350 
351  template < typename GUM_SCALAR >
353  eraseArc(Arc(tail, head));
354  }
355 
356  template < typename GUM_SCALAR >
358  // check that the arc exsists
359  if (!__varMap.exists(arc.tail()) || !__varMap.exists(arc.head())
360  || !dag().existsArc(arc)) {
361  GUM_ERROR(InvalidArc, "a nonexisting arc cannot be reversed");
362  }
363 
364  NodeId tail = arc.tail(), head = arc.head();
365 
366  // check that the reversal does not induce a cycle
367  try {
368  DAG d = dag();
369  d.eraseArc(arc);
370  d.addArc(head, tail);
371  } catch (Exception&) {
372  GUM_ERROR(InvalidArc, "this arc reversal would induce a directed cycle");
373  }
374 
375  // with the same notations as Shachter (1986), "evaluating influence
376  // diagrams",
377  // p.878, we shall first compute the product of probabilities:
378  // pi_j^old (x_j | x_c^old(j) ) * pi_i^old (x_i | x_c^old(i) )
379  Potential< GUM_SCALAR > prod{cpt(tail) * cpt(head)};
380 
381  // modify the topology of the graph: add to tail all the parents of head
382  // and add to head all the parents of tail
384  NodeSet new_parents;
385  for (const auto node : this->parents(tail))
386  new_parents.insert(node);
387  for (const auto node : this->parents(head))
388  new_parents.insert(node);
389  // remove arc (head, tail)
390  eraseArc(arc);
391 
392  // add the necessary arcs to the tail
393  for (const auto p : new_parents) {
394  if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
395  }
396 
397  addArc(head, tail);
398  // add the necessary arcs to the head
399  new_parents.erase(tail);
400 
401  for (const auto p : new_parents) {
402  if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
403  }
404 
406 
407  // update the conditional distributions of head and tail
409  del_vars << &(variable(tail));
410  Potential< GUM_SCALAR > new_cpt_head = prod.margSumOut(del_vars);
411  auto& cpt_head = const_cast< Potential< GUM_SCALAR >& >(cpt(head));
412  cpt_head = std::move(new_cpt_head);
413 
414  Potential< GUM_SCALAR > new_cpt_tail{prod / cpt_head};
415  auto& cpt_tail = const_cast< Potential< GUM_SCALAR >& >(cpt(tail));
416  cpt_tail = std::move(new_cpt_tail);
417  }
418 
419  template < typename GUM_SCALAR >
421  reverseArc(Arc(tail, head));
422  }
423 
424 
425  //==============================================
426  // Aggregators
427  //=============================================
428  template < typename GUM_SCALAR >
430  return add(var, new aggregator::Amplitude< GUM_SCALAR >());
431  }
432 
433  template < typename GUM_SCALAR >
435  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an AND has to be boolean");
436 
437  return add(var, new aggregator::And< GUM_SCALAR >());
438  }
439 
440  template < typename GUM_SCALAR >
442  Idx value) {
443  return add(var, new aggregator::Count< GUM_SCALAR >(value));
444  }
445 
446  template < typename GUM_SCALAR >
448  Idx value) {
449  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean");
450 
451  return add(var, new aggregator::Exists< GUM_SCALAR >(value));
452  }
453 
454  template < typename GUM_SCALAR >
456  Idx value) {
457  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean");
458 
459  return add(var, new aggregator::Forall< GUM_SCALAR >(value));
460  }
461 
462  template < typename GUM_SCALAR >
464  return add(var, new aggregator::Max< GUM_SCALAR >());
465  }
466 
467  template < typename GUM_SCALAR >
469  return add(var, new aggregator::Median< GUM_SCALAR >());
470  }
471 
472  template < typename GUM_SCALAR >
474  return add(var, new aggregator::Min< GUM_SCALAR >());
475  }
476 
477  template < typename GUM_SCALAR >
479  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an OR has to be boolean");
480 
481  return add(var, new aggregator::Or< GUM_SCALAR >());
482  }
483 
484 
485  //================================
486  // ICIModels
487  //================================
488  template < typename GUM_SCALAR >
490  GUM_SCALAR external_weight) {
491  return addNoisyORCompound(var, external_weight);
492  }
493 
494  template < typename GUM_SCALAR >
496  const DiscreteVariable& var, GUM_SCALAR external_weight) {
497  return add(var, new MultiDimNoisyORCompound< GUM_SCALAR >(external_weight));
498  }
499 
500  template < typename GUM_SCALAR >
502  GUM_SCALAR external_weight) {
503  return add(var, new MultiDimNoisyORNet< GUM_SCALAR >(external_weight));
504  }
505 
506  template < typename GUM_SCALAR >
508  GUM_SCALAR external_weight) {
509  return add(var, new MultiDimNoisyAND< GUM_SCALAR >(external_weight));
510  }
511 
512  template < typename GUM_SCALAR >
514  GUM_SCALAR external_weight) {
515  return add(var, new MultiDimLogit< GUM_SCALAR >(external_weight));
516  }
517 
518  template < typename GUM_SCALAR >
520  GUM_SCALAR external_weight,
521  NodeId id) {
522  return addNoisyORCompound(var, external_weight, id);
523  }
524 
525  template < typename GUM_SCALAR >
527  GUM_SCALAR external_weight,
528  NodeId id) {
529  return add(var, new MultiDimNoisyAND< GUM_SCALAR >(external_weight), id);
530  }
531 
532  template < typename GUM_SCALAR >
534  GUM_SCALAR external_weight,
535  NodeId id) {
536  return add(var, new MultiDimLogit< GUM_SCALAR >(external_weight), id);
537  }
538 
539  template < typename GUM_SCALAR >
541  const DiscreteVariable& var, GUM_SCALAR external_weight, NodeId id) {
542  return add(
543  var, new MultiDimNoisyORCompound< GUM_SCALAR >(external_weight), id);
544  }
545 
546  template < typename GUM_SCALAR >
548  GUM_SCALAR external_weight,
549  NodeId id) {
550  return add(var, new MultiDimNoisyORNet< GUM_SCALAR >(external_weight), id);
551  }
552 
553  template < typename GUM_SCALAR >
555  NodeId head,
556  GUM_SCALAR causalWeight) {
557  auto* CImodel =
558  dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(cpt(head).content());
559 
560  if (CImodel != 0) {
561  // or is OK
562  addArc(tail, head);
563 
564  CImodel->causalWeight(variable(tail), causalWeight);
565  } else {
567  "Head variable (" << variable(head).name()
568  << ") is not a CIModel variable !");
569  }
570  }
571 
572  template < typename GUM_SCALAR >
573  INLINE std::ostream& operator<<(std::ostream& output,
574  const BayesNet< GUM_SCALAR >& bn) {
575  output << bn.toString();
576  return output;
577  }
578 
580  template < typename GUM_SCALAR >
582  for (const auto node : nodes())
583  __probaMap[node]->beginMultipleChanges();
584  }
585 
587  template < typename GUM_SCALAR >
589  for (const auto node : nodes())
590  __probaMap[node]->endMultipleChanges();
591  }
592 
594  template < typename GUM_SCALAR >
596  // Removing previous potentials
597  for (const auto& elt : __probaMap) {
598  delete elt.second;
599  }
600 
601  __probaMap.clear();
602  }
603 
605  template < typename GUM_SCALAR >
607  const BayesNet< GUM_SCALAR >& source) {
608  // Copying potentials
609 
610  for (const auto src : source.__probaMap) {
611  // First we build the node's CPT
613  copy_array->beginMultipleChanges();
614  for (gum::Idx i = 0; i < src.second->nbrDim(); i++) {
615  (*copy_array) << variableFromName(src.second->variable(i).name());
616  }
617  copy_array->endMultipleChanges();
618  copy_array->copyFrom(*(src.second));
619 
620  // We add the CPT to the CPT's hashmap
621  __probaMap.insert(src.first, copy_array);
622  }
623  }
624 
625  template < typename GUM_SCALAR >
627  for (const auto node : nodes())
628  generateCPT(node);
629  }
630  template < typename GUM_SCALAR >
631  INLINE void BayesNet< GUM_SCALAR >::generateCPT(NodeId node) const {
633 
634  generator.generateCPT(cpt(node).pos(variable(node)), cpt(node));
635  }
636 
637  template < typename GUM_SCALAR >
639  Potential< GUM_SCALAR >* newPot) {
640  if (cpt(id).nbrDim() != newPot->nbrDim()) {
642  "cannot exchange potentials with different "
643  "dimensions for variable with id "
644  << id);
645  }
646 
647  for (Idx i = 0; i < cpt(id).nbrDim(); i++) {
648  if (&cpt(id).variable(i) != &(newPot->variable(i))) {
650  "cannot exchange potentials because, for variable with id "
651  << id << ", dimension " << i << " differs. ");
652  }
653  }
654 
655  _unsafeChangePotential(id, newPot);
656  }
657 
658  template < typename GUM_SCALAR >
660  NodeId id, Potential< GUM_SCALAR >* newPot) {
661  delete __probaMap[id];
662  __probaMap[id] = newPot;
663  }
664 
665  template < typename GUM_SCALAR >
666  void BayesNet< GUM_SCALAR >::changePotential(const std::string& name,
667  Potential< GUM_SCALAR >* newPot) {
668  changePotential(idFromName(name), newPot);
669  }
670 
671 } /* namespace gum */
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:335
Noisy OR representation.
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
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
BayesNet< GUM_SCALAR > & operator=(const BayesNet< GUM_SCALAR > &source)
Copy operator.
Definition: BayesNet_tpl.h:167
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
Abstract class for generating Conditional Probability Tables.
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:61
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
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:195
bool hasUniqueElts(std::vector< T > const &x)
check if a vector consists in unique values (no duplicate).
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, std::string node, gum::Size domainSize)
Definition: BayesNet_tpl.h:57
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:189
class LabelizedVariable
NodeId tail() const
returns the tail of the arc
class for NoisyOR-net implementation as multiDim
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
or aggregator
Definition: or.h:53
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:219
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:647
forall aggregator
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:429
Container used to map discrete variables with nodes.
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:147
Class representing Bayesian networks.
median aggregator
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
class for LOGIT implementation as multiDim
Class representing Bayesian networks.
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
Definition: IBayesNet_tpl.h:64
class for multiDimNoisyORCompound
Base class for discrete random variable.
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:109
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
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
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
min aggregator
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
forall aggregator
Definition: forall.h:52
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 erase(NodeId id)
Removes a var and it&#39;s id of this mapping. The pointer is deleted.
virtual Size domainSize() const =0
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
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:200
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:573
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
Definition: BayesNet_tpl.h:307
And aggregator.
Definition: and.h:52
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
Logit representation.
Definition: multiDimLogit.h:50
or aggregator
exists aggregator
Definition: exists.h:51
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
abstract class for Conditional Indepency Models
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:478
std::string toString() const
forall aggregator
max aggregator
Idx posLabel(const std::string &label) const
return the pos from label
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:103
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:51
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:40
<agrum/BN/generator/simpleCPTGenerator.h>
NodeProperty< Potential< GUM_SCALAR > * > __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:650
Noisy AND representation.
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
NodeId head() const
returns the head of the arc
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:104
bool exists(NodeId id) const
Return true if id matches a node.
count aggregator
NodeId nextNodeId() const
returns a new node id, not yet used by any node
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
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
count aggregator
class for NoisyAND-net implementation as multiDim
NodeId idFromName(const std::string &name) const
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
median aggregator
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:588
void changeName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable
<agrum/multidim/multiDimImplementation.h>
Utilities for manipulating strings.
max aggregator
Definition: max.h:51
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:631
const DiscreteVariable & variableFromName(const std::string &name) const
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
median aggregator
Definition: median.h:57
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
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:113
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
Base class for labelized discrete random variables.
Base class for dag.
Definition: DAG.h:98
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
Definition: diGraph_inl.h:66
amplitude aggregator
Definition: amplitude.h:52
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
void generateCPT(const Idx &varId, const Potential< GUM_SCALAR > &cpt) override
Generates a CPT using floats.
min aggregator
Definition: min.h:50
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:626
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
Definition: BayesNet_tpl.h:473
count aggregator
Definition: count.h:54
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