aGrUM  0.14.2
influenceDiagram_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  ***************************************************************************/
28 #include <cstdio>
29 #include <iostream>
30 
31 namespace gum {
32 
33  // ===========================================================================
34  // Constructors / Destructors
35  // ===========================================================================
36 
37  /*
38  * Default constructor.
39  */
40  template < typename GUM_SCALAR >
42  GUM_CONSTRUCTOR(InfluenceDiagram);
43  }
44 
45  /*
46  * Destructor.
47  */
48  template < typename GUM_SCALAR >
50  GUM_DESTRUCTOR(InfluenceDiagram);
51  _removeTables();
52  }
53 
54  /*
55  * Copy Constructor
56  */
57  template < typename GUM_SCALAR >
59  const InfluenceDiagram< GUM_SCALAR >& source) :
60  DAGmodel(source),
62  GUM_CONS_CPY(InfluenceDiagram);
63  _copyTables(source);
64  }
65 
66  /*
67  * Copy Operator
68  */
69  template < typename GUM_SCALAR >
72  if (this != &source) {
73  DAGmodel::operator=(source);
74  // Removing previous potentials
75  _removeTables();
76  __potentialMap.clear();
77  __utilityMap.clear();
78 
80 
81  // Copying tables
82  _copyTables(source);
83  }
84 
85  return *this;
86  }
87 
88  /*
89  * Removing ancient table
90  */
91  template < typename GUM_SCALAR >
93  for (const auto node : _dag.nodes()) {
94  if (isChanceNode(node))
95  delete &cpt(node);
96  else if (isUtilityNode(node))
97  delete &utility(node);
98  }
99  }
100 
101  /*
102  * Copying tables from another influence diagram
103  */
104  template < typename GUM_SCALAR >
106  const InfluenceDiagram< GUM_SCALAR >& IDsource) {
107  // Copying potentials
108  for (const auto& pot : IDsource.__potentialMap) {
109  // Instanciation of the node's CPT
110  auto potentialCpy = new Potential< GUM_SCALAR >;
111  (*potentialCpy) << variable(pot.first);
112 
113  // Addition of the parents
114  for (const auto par : _dag.parents(pot.first))
115  (*potentialCpy) << variable(par);
116 
117  // Filling up of the table
118  Instantiation srcInst(*pot.second);
119  Instantiation cpyInst(*potentialCpy);
120 
121  for (cpyInst.setFirst(); !cpyInst.end(); cpyInst.inc()) {
122  for (Idx i = 0; i < cpyInst.nbrDim(); i++) {
123  NodeId id = nodeId(cpyInst.variable(i));
124  srcInst.chgVal(IDsource.variable(id), cpyInst.val(i));
125  }
126 
127  potentialCpy->set(cpyInst, (*pot.second)[srcInst]);
128  }
129 
130  // Adding cpt to cpt map
131  __potentialMap.set(pot.first, potentialCpy);
132  }
133 
134  // Copying Utilities
135  for (const auto& uti : IDsource.__utilityMap) {
136  // Instanciation of the node's CPT
137  auto utilityCpy = new Potential< GUM_SCALAR >;
138  (*utilityCpy) << variable(uti.first);
139 
140  // Addition of the parents
141  for (const auto par : _dag.parents(uti.first))
142  (*utilityCpy) << variable(par);
143 
144  // Filling up of the table
145  Instantiation srcInst(*uti.second);
146 
147  Instantiation cpyInst(*utilityCpy);
148 
149  for (cpyInst.setFirst(); !cpyInst.end(); cpyInst.inc()) {
150  for (Idx i = 0; i < cpyInst.nbrDim(); i++) {
151  NodeId id = nodeId(cpyInst.variable(i));
152  srcInst.chgVal(IDsource.variable(id), cpyInst.val(i));
153  }
154 
155  utilityCpy->set(cpyInst, (*uti.second)[srcInst]);
156  }
157 
158  // Adding cpt to cpt map
159  __utilityMap.set(uti.first, utilityCpy);
160  }
161  }
162 
163  template < typename GUM_SCALAR >
165  std::stringstream output;
166  std::stringstream decisionNode;
167  std::stringstream utilityNode;
168  std::stringstream chanceNode;
169  std::stringstream arcstream;
170  output << "digraph \"";
171 
172  try {
173  output << this->property("name") << "\" {" << std::endl;
174  } catch (NotFound&) { output << "no_name\" {" << std::endl; }
175 
176  output << " node [bgcolor=\"#AAAAAA\", style=filled];" << std::endl;
177 
178  decisionNode << "node [shape = box];" << std::endl;
179 
180  utilityNode << "node [shape = diamond];" << std::endl;
181  chanceNode << "node [shape = ellipse];" << std::endl;
182  std::string tab = " ";
183 
184  for (const auto node : _dag.nodes()) {
185  if (isChanceNode(node))
186  chanceNode << tab << "\"" << variable(node).name() << "\""
187  << ";";
188  else if (isUtilityNode(node))
189  utilityNode << tab << "\"" << variable(node).name() << "\""
190  << ";";
191  else
192  decisionNode << tab << "\"" << variable(node).name() << "\""
193  << ";";
194 
195  if (_dag.children(node).size() > 0)
196  for (const auto chi : _dag.children(node))
197  arcstream << tab << "\"" << variable(node).name() << "\""
198  << " -> "
199  << "\"" << variable(chi).name() << "\""
200  << ";" << std::endl;
201  }
202 
203  output << decisionNode.str() << std::endl
204  << utilityNode.str() << std::endl
205  << chanceNode.str() << std::endl
206  << arcstream.str() << std::endl
207  << "}" << std::endl;
208 
209  return output.str();
210  }
211 
212  template < typename GUM_SCALAR >
214  std::stringstream output;
215 
216  output << "Influence Diagram{" << std::endl;
217  output << " chance: " << chanceNodeSize() << "," << std::endl;
218  output << " utility: " << utilityNodeSize() << "," << std::endl;
219  output << " decision: " << decisionNodeSize() << "," << std::endl;
220  output << " arcs: " << dag().sizeArcs() << "," << std::endl;
221 
222  double dSize = log10DomainSize();
223 
224  if (dSize > 6)
225  output << " domainSize: 10^" << dSize;
226  else
227  output << " domainSize: " << std::round(std::pow(10.0, dSize));
228 
229  output << std::endl << "}";
230 
231  return output.str();
232  }
233 
234  // ===========================================================================
235  // Variable manipulation methods.
236  // ===========================================================================
237 
238  /*
239  * Returns the CPT of a chance variable.
240  */
241  template < typename GUM_SCALAR >
242  INLINE const Potential< GUM_SCALAR >&
244  return *(__potentialMap[varId]);
245  }
246 
247  /*
248  * Returns the utility table of a utility node.
249  */
250  template < typename GUM_SCALAR >
251  INLINE const Potential< GUM_SCALAR >&
253  return *(__utilityMap[varId]);
254  }
255 
256  /*
257  * Return true if node is a utility one
258  */
259  template < typename GUM_SCALAR >
261  return __utilityMap.exists(varId);
262  }
263 
264  /*
265  * Return true if node is a utility one
266  */
267  template < typename GUM_SCALAR >
269  bool ret = true;
270 
271  if (isUtilityNode(varId) || isChanceNode(varId)) ret = false;
272 
273  return ret;
274  }
275 
276  /*
277  * Return true if node is a chance one
278  */
279  template < typename GUM_SCALAR >
281  return __potentialMap.exists(varId);
282  }
283 
284  /*
285  * Returns the number of utility nodes
286  */
287  template < typename GUM_SCALAR >
289  return __utilityMap.size();
290  }
291 
292  /*
293  * Returns the number of chance nodes
294  */
295  template < typename GUM_SCALAR >
297  return __potentialMap.size();
298  }
299 
300  /*
301  * Returns the number of decision nodes
302  */
303  template < typename GUM_SCALAR >
305  return (size() - __utilityMap.size() - __potentialMap.size());
306  }
307 
308  /*
309  * Returns a constant reference to the VariableNodeMap of this Influence
310  * Diagram
311  */
312  template < typename GUM_SCALAR >
313  INLINE const VariableNodeMap&
315  return __variableMap;
316  }
317 
318  /*
319  * Returns a constant reference over a variable given it's node id.
320  */
321  template < typename GUM_SCALAR >
322  INLINE const DiscreteVariable&
324  return __variableMap[id];
325  }
326 
327  /*
328  * Return id node from discrete var pointer.
329  */
330  template < typename GUM_SCALAR >
331  INLINE NodeId
333  return __variableMap.get(var);
334  }
335 
336  // Getter by name
337  template < typename GUM_SCALAR >
338  INLINE NodeId
339  InfluenceDiagram< GUM_SCALAR >::idFromName(const std::string& name) const {
340  return __variableMap.idFromName(name);
341  }
342 
343  // Getter by name
344  template < typename GUM_SCALAR >
346  const std::string& name) const {
347  return __variableMap.variableFromName(name);
348  }
349 
350  /*
351  * Add a chance variable, it's associate node and it's CPT. The id of the new
352  * variable is automatically generated.
353  */
354  template < typename GUM_SCALAR >
356  NodeId varId) {
357  return addChanceNode(var, varId);
358  }
359 
360  /*
361  * Add a utility variable, it's associate node and it's UT. The id of the new
362  * variable is automatically generated.
363  * @Throws : Gum::InvalidArgument if var has more than one state
364  */
365  template < typename GUM_SCALAR >
366  NodeId
368  NodeId varId) {
370  NodeId res = 0;
371 
372  try {
373  res = addUtilityNode(var, newMultiDim, varId);
374  } catch (Exception&) {
375  delete newMultiDim;
376  throw;
377  }
378 
379  return res;
380  }
381 
382  /*
383  * Add a decision variable. The id of the new
384  * variable is automatically generated.
385  */
386  template < typename GUM_SCALAR >
387  NodeId
389  NodeId varId) {
390  return _addNode(var, varId);
391  }
392 
393  /*
394  * Add a chance variable, it's associate node and it's CPT. The id of the new
395  * variable is automatically generated.
396  */
397  template < typename GUM_SCALAR >
399  NodeId varId) {
401  NodeId res = 0;
402 
403  try {
404  res = addChanceNode(var, newMultiDim, varId);
405  } catch (Exception&) {
406  delete newMultiDim;
407  throw;
408  }
409 
410  return res;
411  }
412 
413  /*
414  * Add a chance variable, it's associate node and it's CPT. The id of the new
415  * variable is automatically generated.
416  */
417  template < typename GUM_SCALAR >
419  const DiscreteVariable& var,
421  NodeId DesiredId) {
422  NodeId proposedId = _addNode(var, DesiredId);
423 
424  Potential< GUM_SCALAR >* varcpt = new Potential< GUM_SCALAR >(aContent);
425  (*varcpt) << variable(proposedId);
426  __potentialMap.insert(proposedId, varcpt);
427 
428  return proposedId;
429  }
430 
431  /*
432  * Add a utility variable, it's associate node and it's UT. The id of the new
433  * variable is automatically generated.
434  * @Throws : Gum::InvalidArgument if var has more than one state
435  */
436  template < typename GUM_SCALAR >
438  const DiscreteVariable& var,
440  NodeId DesiredId) {
441  if (var.domainSize() != 1) {
443  "Utility var have no state ( which implicates a "
444  "single label for data output reasons ).");
445  }
446 
447  NodeId proposedId = _addNode(var, DesiredId);
448 
449  Potential< GUM_SCALAR >* varut = new Potential< GUM_SCALAR >(aContent);
450 
451  (*varut) << variable(proposedId);
452 
453  __utilityMap.insert(proposedId, varut);
454 
455  return proposedId;
456  }
457 
458  /*
459  * Add a node
460  */
461  template < typename GUM_SCALAR >
462  NodeId
464  NodeId DesiredId) {
465  // None thread safe code!
466  NodeId proposedId;
467 
468  if (DesiredId == 0)
469  proposedId = _dag.nextNodeId();
470  else
471  proposedId = DesiredId;
472 
473  __variableMap.insert(proposedId, variableType);
474 
475  _dag.addNodeWithId(proposedId);
476 
477  // end critical section
478  return proposedId;
479  }
480 
481  /*
482  * Erase a Variable from the network and remove the variable from
483  * all childs of id.
484  * If no variable matches the id, then nothing is done.
485  */
486  template < typename GUM_SCALAR >
488  if (__variableMap.exists(varId)) {
489  // Reduce the variable child's CPT or Utility Table if necessary
490  for (const auto chi : _dag.children(varId))
491  if (isChanceNode(chi))
492  __potentialMap[chi]->erase(variable(varId));
493  else if (isUtilityNode(chi))
494  __utilityMap[chi]->erase(variable(varId));
495 
496  if (isChanceNode(varId)) {
497  delete __potentialMap[varId];
498  __potentialMap.erase(varId);
499  } else if (isUtilityNode(varId)) {
500  delete __utilityMap[varId];
501  __utilityMap.erase(varId);
502  }
503 
504  __variableMap.erase(varId);
505  _dag.eraseNode(varId);
506  }
507  }
508 
509  /*
510  * Erase a Variable from the network and remove the variable from
511  * all childs of var.
512  * If no variable matches, then nothing is done.
513  */
514  template < typename GUM_SCALAR >
516  erase(__variableMap.get(var));
517  }
518 
519  /* we allow the user to change the name of a variable
520  */
521  template < typename GUM_SCALAR >
523  NodeId id, const std::string& new_name) {
524  __variableMap.changeName(id, new_name);
525  }
526 
527  // ===========================================================================
528  // @name Arc manipulation methods.
529  // ===========================================================================
530  /*
531  * Add an arc in the ID, and update diagram's chance nodes cpt if necessary.
532  */
533  template < typename GUM_SCALAR >
535  if (isUtilityNode(tail)) {
536  GUM_ERROR(InvalidArc, "Tail cannot be a utility node");
537  }
538 
539  _dag.addArc(tail, head);
540 
541  if (isChanceNode(head))
542  // Add parent in the child's CPT
543  (*(__potentialMap[head])) << variable(tail);
544  else if (isUtilityNode(head))
545  // Add parent in the child's UT
546  (*(__utilityMap[head])) << variable(tail);
547  }
548 
549  /*
550  * Removes an arc in the ID, and update diagram chance nodes cpt if necessary.
551  *
552  * If (tail, head) doesn't exist, the nothing happens.
553  */
554  template < typename GUM_SCALAR >
556  if (_dag.existsArc(arc)) {
557  NodeId head = arc.head(), tail = arc.tail();
558  _dag.eraseArc(arc);
559 
560  if (isChanceNode(head))
561  // Removes parent in the child's CPT
562  (*(__potentialMap[head])) >> variable(tail);
563  else if (isUtilityNode(head))
564  // Removes parent in the child's UT
565  (*(__utilityMap[head])) >> variable(tail);
566  }
567  }
568 
569  /*
570  * Removes an arc in the ID, and update diagram chance nodes cpt if necessary.
571  *
572  * If (tail, head) doesn't exist, the nothing happens.
573  */
574  template < typename GUM_SCALAR >
576  eraseArc(Arc(tail, head));
577  }
578 
579  // ===========================================================================
580  // Graphical methods
581  // ===========================================================================
582 
583  /*
584  * The node's id are coherent with the variables and nodes of the topology.
585  */
586  template < typename GUM_SCALAR >
588  for (const auto node : _dag.nodes())
589  if (!isUtilityNode(node)) graph.addNodeWithId(node);
590 
591  for (const auto node : _dag.nodes()) {
592  if (!isDecisionNode(node))
593  for (const auto par : _dag.parents(node)) {
594  if (isChanceNode(node)) graph.addEdge(node, par);
595 
596  for (const auto par2 : _dag.parents(node))
597  if (par != par2) graph.addEdge(par, par2);
598  }
599  }
600  }
601 
602  /*
603  * True if a directed path exist with all decison nodes
604  */
605  template < typename GUM_SCALAR >
607  const Sequence< NodeId >& order = topologicalOrder(true);
608 
609  // Finding first decision node
610  Sequence< NodeId >::const_iterator orderIter = order.begin();
611 
612  while ((orderIter != order.end()) && (!isDecisionNode(*orderIter)))
613  ++orderIter;
614 
615  if (orderIter == order.end()) return true;
616 
617  NodeId parentDecision = (*orderIter);
618  ++orderIter;
619 
620  // Checking path between decisions nodes
621  while (orderIter != order.end()) {
622  if (isDecisionNode(*orderIter)) {
623  if (!existsPathBetween(parentDecision, *orderIter)) return false;
624 
625  parentDecision = *orderIter;
626  }
627 
628  ++orderIter;
629  }
630 
631  return true;
632  }
633 
634  /*
635  * Returns true if a path exists between source and destination
636  */
637  template < typename GUM_SCALAR >
639  NodeId dest) const {
640  List< NodeId > nodeFIFO;
641  // mark[node] contains 0 if not visited
642  // mark[node] = predecessor if visited
643  NodeProperty< int > mark = _dag.nodesProperty((int)-1);
644  NodeId current;
645 
646  mark[src] = (int)src;
647  nodeFIFO.pushBack(src);
648 
649  while (!nodeFIFO.empty()) {
650  current = nodeFIFO.front();
651  nodeFIFO.popFront();
652 
653  for (const auto new_one : _dag.children(current)) {
654  if (mark[new_one] != -1)
655  continue; // if this node is already marked, continue
656 
657  mark[new_one] = (int)current;
658 
659  if (new_one == dest) break; // if we reach *orderIter, stop.
660 
661  nodeFIFO.pushBack(new_one);
662  }
663  }
664 
665  if (mark[dest] == -1) return false;
666 
667  return true;
668  }
669 
670  /*
671  * Returns the decision graph
672  */
673  template < typename GUM_SCALAR >
675  gum::DAG* temporalGraph = new gum::DAG();
676 
677  for (const auto node : _dag.nodes()) {
678  if (isDecisionNode(node)) {
679  if (!temporalGraph->existsNode(node)) temporalGraph->addNodeWithId(node);
680 
681  for (const auto chi : _getChildrenDecision(node)) {
682  if (!temporalGraph->existsNode(chi)) temporalGraph->addNodeWithId(chi);
683 
684  temporalGraph->addArc(node, chi);
685  }
686  }
687  }
688 
689  return temporalGraph;
690  }
691 
692  /*
693  * Returns the list of children decision for a given nodeId
694  */
695  template < typename GUM_SCALAR >
697  NodeId parentDecision) const {
698  Sequence< NodeId > childrenSeq;
699 
700  List< NodeId > nodeFIFO;
701  NodeId current;
702 
703  // mark[node] contains false if not visited
704  // mark[node] contains true if visited
706 
707  mark[parentDecision] = true;
708 
709  nodeFIFO.pushBack(parentDecision);
710 
711  while (!nodeFIFO.empty()) {
712  current = nodeFIFO.front();
713  nodeFIFO.popFront();
714 
715  for (const auto new_one : _dag.children(current)) {
716  if (mark[new_one]) continue; // if this node is already marked, continue
717 
718  mark[new_one] = true;
719 
720  if (!isDecisionNode(new_one))
721  nodeFIFO.pushBack(new_one);
722  else
723  childrenSeq.insert(new_one);
724  }
725  }
726 
727  return childrenSeq;
728  }
729 
730  /*
731  * Returns the sequence of decision nodes
732  * @throw NotFound if such a sequence does not exist
733  */
734  template < typename GUM_SCALAR >
735  std::vector< NodeId >* InfluenceDiagram< GUM_SCALAR >::getDecisionOrder() const {
736  if (!decisionOrderExists()) { GUM_ERROR(NotFound, "No decision path exists"); }
737 
738  std::vector< NodeId >* decisionSequence = new std::vector< NodeId >();
739 
740  for (const auto elt : topologicalOrder(false))
741  if (isDecisionNode(elt)) decisionSequence->push_back(elt);
742 
743  return decisionSequence;
744  }
745 
746  /*
747  * Returns partial temporal ordering
748  * @throw NotFound if such a sequence does not exist
749  */
750  template < typename GUM_SCALAR >
751  const List< NodeSet >&
753  if (clear) {
754  __temporalOrder.clear();
755 
756  std::vector< NodeId >* decisionOrder = getDecisionOrder();
757  NodeSet nodeList = _dag.asNodeSet();
758 
759  for (Idx i = 0; i < decisionOrder->size(); i++) {
760  NodeSet partialOrderedSet;
761 
762  for (const auto par : _dag.parents(decisionOrder->at(i))) {
763  if (nodeList.contains(par) && isChanceNode(par)) {
764  partialOrderedSet.insert(par);
765  nodeList.erase(par);
766  }
767  }
768 
769  if (!partialOrderedSet.empty())
770  __temporalOrder.pushFront(partialOrderedSet);
771 
772  NodeSet decisionSet;
773 
774  decisionSet.insert(decisionOrder->at(i));
775 
776  __temporalOrder.pushFront(decisionSet);
777  }
778 
779  NodeSet lastSet; //= new gum::NodeSet();
780 
781  for (const auto node : nodeList)
782  if (isChanceNode(node)) lastSet.insert(node);
783 
784  if (!lastSet.empty()) __temporalOrder.pushFront(lastSet);
785  }
786 
787  return __temporalOrder;
788  }
789 } // namespace gum
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
Definition: DAGmodel.cpp:76
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
virtual const VariableNodeMap & variableNodeMap() const
Returns a constant reference to the VariableNodeMap of this Influence Diagram.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1967
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
NodeProperty< Potential< GUM_SCALAR > *> __potentialMap
Mapping between potential variable&#39;s id and their CPT.
Virtual base class for PGMs using a DAG.
Definition: DAGmodel.h:45
iterator begin() const
Returns an unsafe begin iterator.
Definition: sequence_tpl.h:654
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:704
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
void erase(NodeId id)
Erase a Variable from the network and remove the variable from all his childs.
NodeId addChanceNode(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it&#39;s associate node and it&#39;s CPT.
virtual const DiscreteVariable & variable(NodeId id) const
Returns a constant reference over a variabe given it&#39;s node id.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Class representing Influence Diagrams.
bool existsPathBetween(NodeId src, NodeId dest) const
Returns true if a path exists between two nodes.
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
std::string toString() const
gum::DAG * getDecisionGraph() const
Returns the temporal Graph.
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:32
Container used to map discrete variables with nodes.
Size decisionNodeSize() const
Returns the number of decision nodes.
virtual const Potential< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
virtual const DiscreteVariable & variableFromName(const std::string &name) const
Getter by name.
void changeVariableName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable
Size chanceNodeSize() const
Returns the number of chance nodes.
NodeProperty< VAL > nodesProperty(VAL(*f)(const NodeId &), Size size=0) const
a method to create a HashTable with key:NodeId and value:VAL
bool exists(NodeId id) const
Return true if id matches a node.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
std::vector< NodeId > * getDecisionOrder() const
Returns the sequence of decision nodes in the directed path.
VariableNodeMap __variableMap
Mapping between id and variable.
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:653
Base class for discrete random variable.
Generic doubly linked lists.
Definition: list.h:369
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Size utilityNodeSize() const
Returns the number of utility nodes.
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1961
NodeProperty< Potential< GUM_SCALAR > *> __utilityMap
Mapping between utility variable&#39;s id and their utility table.
NodeId head() const
returns the head of the arc
NodeId _addNode(const DiscreteVariable &variableType, NodeId DesiredId)
Add a node.
The class for generic Hash Tables.
Definition: hashTable.h:676
std::string toDot() const
Idx val(Idx i) const
Returns the current value of the variable at position i.
void erase(NodeId id)
Removes a var and it&#39;s id of this mapping. The pointer is deleted.
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
const List< NodeSet > & getPartialTemporalOrder(bool clear=true) const
Returns partial temporal ordering.
virtual Size domainSize() const =0
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:93
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:200
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
const Sequence< NodeId > & topologicalOrder(bool clear=true) const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
Definition: DAGmodel.cpp:115
List< NodeSet > __temporalOrder
The temporal order.
const std::string & property(const std::string &name) const
Return the value of the property name of this DAGModel.
Definition: DAGmodel_inl.h:34
void inc()
Operator increment.
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1589
NodeSet asNodeSet() const
returns a copy of the set of nodes represented by the NodeGraphPart
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
void _copyTables(const InfluenceDiagram< GUM_SCALAR > &IDsource)
Copying tables from another influence diagram.
virtual ~InfluenceDiagram()
Destructor.
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
NodeId idFromName(const std::string &name) const
Sequence< NodeId > _getChildrenDecision(NodeId parentDecision) const
Returns the list of children decision for a given nodeId.
bool decisionOrderExists() const
True if a directed path exist with all decison nodes.
virtual void _moralGraph(UndiGraph &graph) const
Returns the moral graph of this InfluenceDiagram.
virtual NodeId idFromName(const std::string &name) const
Getter by name.
InfluenceDiagram< GUM_SCALAR > & operator=(const InfluenceDiagram< GUM_SCALAR > &source)
Copy Operator.
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1828
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
void eraseArc(const Arc &arc)
Removes an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.
void setFirst()
Assign the first values to the tuple of the Instantiation.
NodeId addUtilityNode(const DiscreteVariable &variable, NodeId id=0)
Add a utility variable, it&#39;s associate node and it&#39;s UT.
void changeName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable
Base class for undirected graphs.
Definition: undiGraph.h:106
InfluenceDiagram()
Default constructor.
NodeId add(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it&#39;s associate node and it&#39;s CPT.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
NodeId nextNodeId() const
returns a new node id, not yet used by any node
NodeId addDecisionNode(const DiscreteVariable &variable, NodeId id=0)
Add a decision variable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
virtual NodeId nodeId(const DiscreteVariable &var) const
Return id node from discrete var pointer.
double log10DomainSize() const
Definition: DAGmodel_inl.h:72
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:698
const DiscreteVariable & variableFromName(const std::string &name) const
const iterator & end() const noexcept
Returns the unsafe end iterator.
Definition: sequence_tpl.h:661
const std::string & name() const
returns the name of the variable
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Base class for dag.
Definition: DAG.h:99
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
Definition: diGraph_inl.h:66
void _removeTables()
Removing ancient table.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a potential variable.
NodeId tail() const
returns the tail of the arc
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.
bool end() const
Returns true if the Instantiation reached the end.
Class representing an Influence Diagram.
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:405