aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::InfluenceDiagram< GUM_SCALAR > Class Template Reference

Class representing an Influence Diagram. More...

#include <agrum/ID/influenceDiagram.h>

+ Inheritance diagram for gum::InfluenceDiagram< GUM_SCALAR >:
+ Collaboration diagram for gum::InfluenceDiagram< GUM_SCALAR >:

Public Member Functions

std::string toDot () const
 
std::string toString () const
 
void clear ()
 
bool hasSameStructure (const DAGmodel &other)
 
double log10DomainSize () const
 
Constructors / Destructors
 InfluenceDiagram ()
 Default constructor. More...
 
 ~InfluenceDiagram () override
 Destructor. More...
 
 InfluenceDiagram (const InfluenceDiagram< GUM_SCALAR > &source)
 Copy Constructor. More...
 
InfluenceDiagram< GUM_SCALAR > & operator= (const InfluenceDiagram< GUM_SCALAR > &source)
 Copy Operator. More...
 
Variable manipulation methods.
virtual const Potential< GUM_SCALAR > & cpt (NodeId varId) const
 Returns the CPT of a potential variable. More...
 
virtual const Potential< GUM_SCALAR > & cpt (std::string name) const final
 Returns the CPT of a potential variable. More...
 
virtual const Potential< GUM_SCALAR > & utility (NodeId varId) const
 Returns the utility table of a utility node. More...
 
virtual const Potential< GUM_SCALAR > & utility (std::string name) const final
 Returns the CPT of a potential variable. More...
 
const VariableNodeMapvariableNodeMap () const final
 Returns a constant reference to the VariableNodeMap of this Influence Diagram. More...
 
bool isUtilityNode (NodeId varId) const
 Returns true if node is a utility one. More...
 
bool isUtilityNode (const std::string &name) const
 Returns the CPT of a potential variable. More...
 
bool isDecisionNode (NodeId varId) const
 Returns true if node is a decision one. More...
 
bool isDecisionNode (const std::string &name) const
 Returns the CPT of a potential variable. More...
 
bool isChanceNode (NodeId varId) const
 Returns true if node is a chance one. More...
 
bool isChanceNode (const std::string &name) const
 Returns the CPT of a potential variable. More...
 
Size utilityNodeSize () const
 Returns the number of utility nodes. More...
 
Size chanceNodeSize () const
 Returns the number of chance nodes. More...
 
Size decisionNodeSize () const
 Returns the number of decision nodes. More...
 
const DiscreteVariablevariable (NodeId id) const final
 Returns a constant reference over a variable given it's node id. More...
 
NodeId nodeId (const DiscreteVariable &var) const final
 Return id node from discrete var pointer. More...
 
NodeId idFromName (const std::string &name) const final
 Getter by name. More...
 
const DiscreteVariablevariableFromName (const std::string &name) const final
 Getter by name. More...
 
NodeId add (const DiscreteVariable &variable, NodeId id=0)
 Add a chance variable, it's associate node and it's CPT. More...
 
NodeId addChanceNode (const DiscreteVariable &variable, NodeId id=0)
 Add a chance variable, it's associate node and it's CPT. More...
 
NodeId addUtilityNode (const DiscreteVariable &variable, NodeId id=0)
 Add a utility variable, it's associate node and it's UT. More...
 
NodeId addDecisionNode (const DiscreteVariable &variable, NodeId id=0)
 Add a decision variable. More...
 
NodeId addChanceNode (const DiscreteVariable &variable, MultiDimImplementation< GUM_SCALAR > *aContent, NodeId id=0)
 Add a chance variable, it's associate node and it's CPT. More...
 
NodeId addUtilityNode (const DiscreteVariable &variable, MultiDimImplementation< GUM_SCALAR > *aContent, NodeId id=0)
 Add a chance variable, it's associate node and it's CPT. More...
 
void erase (NodeId id)
 Erase a Variable from the network and remove the variable from all his children. More...
 
void erase (const std::string &name)
 Returns the CPT of a potential variable. More...
 
void erase (const DiscreteVariable &var)
 Erase a Variable from the network and remove the variable from all his children. More...
 
void changeVariableName (NodeId id, const std::string &new_name)
 we allow the user to change the name of a variable More...
 
void changeVariableName (const std::string &name, const std::string &new_name)
 Returns the CPT of a potential variable. More...
 
Arc manipulation methods.
void addArc (NodeId tail, NodeId head)
 Add an arc in the ID, and update diagram's potential nodes cpt if necessary. More...
 
void addArc (const std::string &tail, const std::string &head)
 Add an arc in the ID, and update diagram's potential nodes cpt if necessary. More...
 
void eraseArc (const Arc &arc)
 Removes an arc in the ID, and update diagram's potential nodes cpt if necessary. More...
 
void eraseArc (NodeId tail, NodeId head)
 Removes an arc in the ID, and update diagram's potential nodes cpt if necessary. More...
 
void eraseArc (const std::string &tail, const std::string &head)
 Add an arc in the ID, and update diagram's potential nodes cpt if necessary. More...
 
Decisions methods
bool decisionOrderExists () const
 True if a directed path exist with all decision nodes. More...
 
gum::DAGgetDecisionGraph () const
 Returns the temporal Graph. More...
 
std::vector< NodeIddecisionOrder () const
 Returns the sequence of decision nodes in the directed path. More...
 
bool existsPathBetween (NodeId src, NodeId dest) const
 Returns true if a path exists between two nodes. More...
 
bool existsPathBetween (const std::string &src, const std::string &dest) const
 True if a directed path exist with all decision nodes. More...
 
const List< NodeSet > & getPartialTemporalOrder (bool clear=true) const
 Returns partial temporal ordering. More...
 
Variable manipulation methods.
const DAGdag () const
 Returns a constant reference to the dag of this Bayes Net. More...
 
virtual Size size () const final
 Returns the number of variables in this Directed Graphical Model. More...
 
Size sizeArcs () const
 Returns the number of arcs in this Directed Graphical Model. More...
 
const NodeGraphPartnodes () const final
 Returns a constant reference to the dag of this Bayes Net. More...
 
bool exists (NodeId node) const final
 Return true if this node exists in this graphical model. More...
 
Variable manipulation methods.
bool exists (const std::string &name) const
 Return true if this graphical model is empty. More...
 
virtual bool empty () const
 Return true if this graphical model is empty. More...
 
std::vector< std::string > names (const std::vector< NodeId > &ids) const
 transform a vector of NodeId in a vector of names More...
 
std::vector< std::string > names (const NodeSet &ids) const
 transform a NodeSet in a vector of names More...
 
std::vector< NodeIdids (const std::vector< std::string > &names) const
 transform a vector of names into a vector of nodeId More...
 
NodeSet nodeset (const std::vector< std::string > &names) const
 transform a vector of names into a NodeSet More...
 
Instantiation completeInstantiation () const
 Get an instantiation over all the variables of the model. More...
 
Arc manipulation methods.
const ArcSetarcs () const
 return true if the arc tail->head exists in the DAGmodel More...
 
bool existsArc (const NodeId tail, const NodeId head) const
 return true if the arc tail->head exists in the DAGmodel More...
 
bool existsArc (const std::string &nametail, const std::string &namehead) const
 return true if the arc tail->head exists in the DAGmodel More...
 
const NodeSetparents (const NodeId id) const
 returns the set of nodes with arc ingoing to a given node More...
 
const NodeSetparents (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet parents (const NodeSet &ids) const
 returns the parents of a set of nodes More...
 
NodeSet parents (const std::vector< std::string > &names) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet family (const NodeId id) const
 returns the parents of a node and the node More...
 
NodeSet family (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
const NodeSetchildren (const NodeId id) const
 returns the set of nodes with arc outgoing from a given node More...
 
const NodeSetchildren (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet children (const NodeSet &ids) const
 returns the children of a set of nodes More...
 
NodeSet children (const std::vector< std::string > &names) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet descendants (const NodeId id) const
 returns the set of nodes with directed path outgoing from a given node More...
 
NodeSet descendants (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
NodeSet ancestors (const NodeId id) const
 returns the set of nodes with directed path ingoing to a given node More...
 
NodeSet ancestors (const std::string &name) const
 return true if the arc tail->head exists in the DAGmodel More...
 
Graphical methods
UndiGraph moralizedAncestralGraph (const NodeSet &nodes) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes More...
 
UndiGraph moralizedAncestralGraph (const std::vector< std::string > &nodenames) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes More...
 
bool isIndependent (NodeId X, NodeId Y, const NodeSet &Z) const final
 check if node X and node Y are independent given nodes Z More...
 
bool isIndependent (const NodeSet &X, const NodeSet &Y, const NodeSet &Z) const final
 check if nodes X and nodes Y are independent given nodes Z More...
 
bool isIndependent (const std::string &Xname, const std::string &Yname, const std::vector< std::string > &Znames) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes More...
 
bool isIndependent (const std::vector< std::string > &Xnames, const std::vector< std::string > &Ynames, const std::vector< std::string > &Znames) const
 build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes More...
 
const UndiGraphmoralGraph (bool clear=true) const
 The node's id are coherent with the variables and nodes of the topology. More...
 
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 topology. More...
 
Getter and setters
const std::string & property (const std::string &name) const
 Return the value of the property name of this GraphicalModel. More...
 
const std::string & propertyWithDefault (const std::string &name, const std::string &byDefault) const
 Return the value of the property name of this GraphicalModel. More...
 
void setProperty (const std::string &name, const std::string &value)
 Add or change a property of this GraphicalModel. More...
 

Static Public Member Functions

static InfluenceDiagram< GUM_SCALAR > fastPrototype (const std::string &dotlike, Size domainSize=2)
 Create an Influence Diagram with a dot-like syntax which specifies: More...
 

Protected Attributes

DAG dag_
 The DAG of this Directed Graphical Model. More...
 

Protected Member Functions

virtual void moralGraph_ (UndiGraph &graph) const
 Returns the moral graph of this InfluenceDiagram. More...
 
void removeTables_ ()
 Removing ancient table. More...
 
void copyStructureAndTables_ (const InfluenceDiagram< GUM_SCALAR > &IDsource)
 Copying tables from another influence diagram. More...
 
NodeId addNode_ (const DiscreteVariable &variableType, NodeId DesiredId)
 Add a node. More...
 
Sequence< NodeIdgetChildrenDecision_ (NodeId parentDecision) const
 Returns the list of children decision for a given nodeId. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::InfluenceDiagram< GUM_SCALAR >

Class representing an Influence Diagram.

Definition at line 53 of file influenceDiagram.h.

Constructor & Destructor Documentation

◆ InfluenceDiagram() [1/2]

template<typename GUM_SCALAR >
INLINE gum::InfluenceDiagram< GUM_SCALAR >::InfluenceDiagram ( )

Default constructor.

Definition at line 194 of file influenceDiagram_tpl.h.

194  : DAGmodel() {
195  GUM_CONSTRUCTOR(InfluenceDiagram);
196  }
DAGmodel()
Default constructor.
Definition: DAGmodel.cpp:29
InfluenceDiagram()
Default constructor.

◆ ~InfluenceDiagram()

template<typename GUM_SCALAR >
gum::InfluenceDiagram< GUM_SCALAR >::~InfluenceDiagram ( )
override

Destructor.

Definition at line 202 of file influenceDiagram_tpl.h.

202  {
203  GUM_DESTRUCTOR(InfluenceDiagram);
204  removeTables_();
205  }
void removeTables_()
Removing ancient table.
InfluenceDiagram()
Default constructor.

◆ InfluenceDiagram() [2/2]

template<typename GUM_SCALAR >
gum::InfluenceDiagram< GUM_SCALAR >::InfluenceDiagram ( const InfluenceDiagram< GUM_SCALAR > &  source)

Copy Constructor.

Definition at line 211 of file influenceDiagram_tpl.h.

211  {
212  GUM_CONS_CPY(InfluenceDiagram);
213  copyStructureAndTables_(source);
214  }
void copyStructureAndTables_(const InfluenceDiagram< GUM_SCALAR > &IDsource)
Copying tables from another influence diagram.
InfluenceDiagram()
Default constructor.

Member Function Documentation

◆ add()

template<typename GUM_SCALAR >
NodeId gum::InfluenceDiagram< GUM_SCALAR >::add ( const DiscreteVariable variable,
NodeId  id = 0 
)

Add a chance variable, it's associate node and it's CPT.

The id of the new variable is automatically generated.

The implementation of the Potential is by default a MultiDimArray.

Parameters
variableThe variable added by copy.
idThe chosen id. If 0, the NodeGraphPart will choose.
Warning
give an id (not 0) should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id(<>0) is already used

Definition at line 485 of file influenceDiagram_tpl.h.

485  {
486  return addChanceNode(var, varId);
487  }
NodeId addChanceNode(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it&#39;s associate node and it&#39;s CPT.

◆ addArc() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::InfluenceDiagram< GUM_SCALAR >::addArc ( NodeId  tail,
NodeId  head 
)

Add an arc in the ID, and update diagram's potential nodes cpt if necessary.

Parameters
headand
tailas NodeId
Exceptions
InvalidEdgeIf arc.tail and/or arc.head are not in the ID.
InvalidEdgeif tail is a utility node

Definition at line 658 of file influenceDiagram_tpl.h.

658  {
659  if (isUtilityNode(tail)) { GUM_ERROR(InvalidArc, "Tail cannot be a utility node") }
660 
661  dag_.addArc(tail, head);
662 
663  if (isChanceNode(head))
664  // Add parent in the child's CPT
665  (*(_potentialMap_[head])) << variable(tail);
666  else if (isUtilityNode(head)) {
667  // Add parent in the child's UT
668  (*(_utilityMap_[head])) << variable(tail);
669  }
670  }
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition: DAG_inl.h:42
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it&#39;s node id.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addArc() [2/2]

template<typename GUM_SCALAR>
void gum::InfluenceDiagram< GUM_SCALAR >::addArc ( const std::string &  tail,
const std::string &  head 
)
inline

Add an arc in the ID, and update diagram's potential nodes cpt if necessary.

Parameters
headand
tailas NodeId
Exceptions
InvalidEdgeIf arc.tail and/or arc.head are not in the ID.
InvalidEdgeif tail is a utility node

Definition at line 339 of file influenceDiagram.h.

339  {
340  addArc(idFromName(tail), idFromName(head));
341  }
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.
NodeId idFromName(const std::string &name) const final
Getter by name.

◆ addChanceNode() [1/2]

template<typename GUM_SCALAR >
NodeId gum::InfluenceDiagram< GUM_SCALAR >::addChanceNode ( const DiscreteVariable variable,
NodeId  id = 0 
)

Add a chance variable, it's associate node and it's CPT.

The id of the new variable is automatically generated.

The implementation of the Potential is by default a MultiDimArray.

Parameters
variableThe variable added by copy.
idThe chosen id. If 0, the NodeGraphPart will choose.
Warning
give an id (not 0) should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id(<>0) is already used

Definition at line 524 of file influenceDiagram_tpl.h.

524  {
525  auto newMultiDim = new MultiDimArray< GUM_SCALAR >();
526  NodeId res;
527 
528  try {
529  res = addChanceNode(var, newMultiDim, varId);
530  } catch (Exception&) {
531  delete newMultiDim;
532  throw;
533  }
534 
535  return res;
536  }
NodeId addChanceNode(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it&#39;s associate node and it&#39;s CPT.
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ addChanceNode() [2/2]

template<typename GUM_SCALAR >
NodeId gum::InfluenceDiagram< GUM_SCALAR >::addChanceNode ( const DiscreteVariable variable,
MultiDimImplementation< GUM_SCALAR > *  aContent,
NodeId  id = 0 
)

Add a chance variable, it's associate node and it's CPT.

The id of the new variable is automatically generated.

Parameters
variableThe variable added by copy.
aContentThe content used for the variable potential.
idThe chosen id. If 0, the NodeGraphPart will choose.
Warning
give an id (not 0) should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id(<>0) is already used

Definition at line 544 of file influenceDiagram_tpl.h.

546  {
547  NodeId proposedId = addNode_(var, DesiredId);
548 
549  auto varcpt = new Potential< GUM_SCALAR >(aContent);
550  (*varcpt) << variable(proposedId);
551  _potentialMap_.insert(proposedId, varcpt);
552 
553  return proposedId;
554  }
NodeId addNode_(const DiscreteVariable &variableType, NodeId DesiredId)
Add a node.
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it&#39;s node id.
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ addDecisionNode()

template<typename GUM_SCALAR >
NodeId gum::InfluenceDiagram< GUM_SCALAR >::addDecisionNode ( const DiscreteVariable variable,
NodeId  id = 0 
)

Add a decision variable.

The id of the new variable is automatically generated.

Parameters
variableThe variable added by copy.
idThe chosen id. If 0, the NodeGraphPart will choose.
Warning
give an id (not 0) should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id(<>0) is already used

Definition at line 514 of file influenceDiagram_tpl.h.

515  {
516  return addNode_(var, varId);
517  }
NodeId addNode_(const DiscreteVariable &variableType, NodeId DesiredId)
Add a node.

◆ addNode_()

template<typename GUM_SCALAR >
NodeId gum::InfluenceDiagram< GUM_SCALAR >::addNode_ ( const DiscreteVariable variableType,
NodeId  DesiredId 
)
protected

Add a node.

Definition at line 587 of file influenceDiagram_tpl.h.

588  {
589  // None thread safe code!
590  NodeId proposedId;
591 
592  if (DesiredId == 0)
593  proposedId = dag_.nextNodeId();
594  else
595  proposedId = DesiredId;
596 
597  _variableMap_.insert(proposedId, variableType);
598 
599  dag_.addNodeWithId(proposedId);
600 
601  // end critical section
602  return proposedId;
603  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
VariableNodeMap _variableMap_
Mapping between id and variable.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
NodeId nextNodeId() const
returns a new node id, not yet used by any node
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ addUtilityNode() [1/2]

template<typename GUM_SCALAR >
NodeId gum::InfluenceDiagram< GUM_SCALAR >::addUtilityNode ( const DiscreteVariable variable,
NodeId  id = 0 
)

Add a utility variable, it's associate node and it's UT.

The id of the new variable is automatically generated.

The implementation of the Utility is by default a MultiDimArray.

Parameters
variableThe variable added by copy.
idThe chosen id. If 0, the NodeGraphPart will choose.
Warning
give an id (not 0) should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
InvalidArgumentIf variable has more than one label
DuplicateElementif id(<>0) is already used

Definition at line 495 of file influenceDiagram_tpl.h.

495  {
496  auto newMultiDim = new MultiDimArray< GUM_SCALAR >();
497  NodeId res;
498 
499  try {
500  res = addUtilityNode(var, newMultiDim, varId);
501  } catch (Exception&) {
502  if (newMultiDim != nullptr) delete newMultiDim;
503  throw;
504  }
505 
506  return res;
507  }
NodeId addUtilityNode(const DiscreteVariable &variable, NodeId id=0)
Add a utility variable, it&#39;s associate node and it&#39;s UT.
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ addUtilityNode() [2/2]

template<typename GUM_SCALAR >
NodeId gum::InfluenceDiagram< GUM_SCALAR >::addUtilityNode ( const DiscreteVariable variable,
MultiDimImplementation< GUM_SCALAR > *  aContent,
NodeId  id = 0 
)

Add a chance variable, it's associate node and it's CPT.

The id of the new variable is automatically generated.

Parameters
variableThe variable added by copy.
aContentThe content used for the variable utility.
idThe chosen id. If 0, the NodeGraphPart will choose.
Warning
give an id (not 0) should be reserved for rare and specific situations !!!
Exceptions
InvalidArgumentIf variable has more than one label
DuplicateElementif id(<>0) is already used

Definition at line 563 of file influenceDiagram_tpl.h.

565  {
566  if (var.domainSize() != 1) {
567  GUM_ERROR(InvalidArgument,
568  "Utility var have no state ( which implicates a "
569  "single label for data output reasons ).")
570  }
571 
572  NodeId proposedId = addNode_(var, DesiredId);
573 
574  auto varut = new Potential< GUM_SCALAR >(aContent);
575 
576  (*varut) << variable(proposedId);
577 
578  _utilityMap_.insert(proposedId, varut);
579 
580  return proposedId;
581  }
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.
NodeId addNode_(const DiscreteVariable &variableType, NodeId DesiredId)
Add a node.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it&#39;s node id.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ ancestors() [1/2]

INLINE NodeSet gum::DAGmodel::ancestors ( const NodeId  id) const
inherited

returns the set of nodes with directed path ingoing to a given node

Note that the set of nodes returned may be empty if no path within the ArcGraphPart is ingoing to the given node.

Parameters
idthe node which is the head of a directed path with the returned nodes
namethe name of the node which is the head of a directed path with the returned nodes

Definition at line 96 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

96 { return dag().ancestors(id); }
NodeSet ancestors(NodeId id) const
returns the set of nodes with directed path ingoing to a given node
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ ancestors() [2/2]

INLINE NodeSet gum::DAGmodel::ancestors ( const std::string &  name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 98 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

98  {
99  return ancestors(idFromName(name));
100  }
NodeSet ancestors(const NodeId id) const
returns the set of nodes with directed path ingoing to a given node
Definition: DAGmodel_inl.h:96
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ arcs()

INLINE const ArcSet & gum::DAGmodel::arcs ( ) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 43 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

43 { return dag_.arcs(); }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
+ Here is the call graph for this function:

◆ chanceNodeSize()

template<typename GUM_SCALAR >
INLINE Size gum::InfluenceDiagram< GUM_SCALAR >::chanceNodeSize ( ) const

Returns the number of chance nodes.

Definition at line 430 of file influenceDiagram_tpl.h.

430  {
431  return _potentialMap_.size();
432  }
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.

◆ changeVariableName() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::InfluenceDiagram< GUM_SCALAR >::changeVariableName ( NodeId  id,
const std::string &  new_name 
)

we allow the user to change the name of a variable

Exceptions
DuplicateLabelif this name already exists
NotFoundRaised if no nodes matches id.

Definition at line 646 of file influenceDiagram_tpl.h.

647  {
648  _variableMap_.changeName(id, new_name);
649  }
VariableNodeMap _variableMap_
Mapping between id and variable.
void changeName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable

◆ changeVariableName() [2/2]

template<typename GUM_SCALAR>
void gum::InfluenceDiagram< GUM_SCALAR >::changeVariableName ( const std::string &  name,
const std::string &  new_name 
)
inline

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 319 of file influenceDiagram.h.

319  {
320  changeVariableName(idFromName(name), new_name);
321  }
NodeId idFromName(const std::string &name) const final
Getter by name.
void changeVariableName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable

◆ children() [1/4]

INLINE const NodeSet & gum::DAGmodel::children ( const NodeId  id) const
inherited

returns the set of nodes with arc outgoing from a given node

Note that the set of nodes returned may be empty if no node is outgoing from the given node.

Parameters
idthe node which is the tail of an arc with the returned nodes
namethe name of the node which is the tail of an arc with the returned nodes

Definition at line 65 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

65 { return dag_.children(id); }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ children() [2/4]

INLINE const NodeSet & gum::DAGmodel::children ( const std::string &  name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 66 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

66  {
67  return dag_.children(idFromName(name));
68  }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ children() [3/4]

INLINE NodeSet gum::DAGmodel::children ( const NodeSet ids) const
inherited

returns the children of a set of nodes

Definition at line 70 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

70 { return dag_.children(ids); }
std::vector< NodeId > ids(const std::vector< std::string > &names) const
transform a vector of names into a vector of nodeId
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ children() [4/4]

INLINE NodeSet gum::DAGmodel::children ( const std::vector< std::string > &  names) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 72 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

72  {
73  return children(nodeset(names));
74  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:65
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
+ Here is the call graph for this function:

◆ clear()

template<typename GUM_SCALAR>
void gum::InfluenceDiagram< GUM_SCALAR >::clear ( )

◆ completeInstantiation()

INLINE Instantiation gum::GraphicalModel::completeInstantiation ( ) const
inherited

Get an instantiation over all the variables of the model.

Definition at line 84 of file graphicalModel_inl.h.

84  {
85  Instantiation I;
86 
87  for (const auto node: nodes())
88  I << variable(node);
89 
90  return I;
91  }
virtual const NodeGraphPart & nodes() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.

◆ copyStructureAndTables_()

template<typename GUM_SCALAR >
void gum::InfluenceDiagram< GUM_SCALAR >::copyStructureAndTables_ ( const InfluenceDiagram< GUM_SCALAR > &  IDsource)
protected

Copying tables from another influence diagram.

Definition at line 258 of file influenceDiagram_tpl.h.

259  {
260  for (auto node: IDsource.nodes()) {
261  if (IDsource.isChanceNode(node))
262  addChanceNode(IDsource.variable(node), node);
263  else if (IDsource.isUtilityNode(node))
264  addUtilityNode(IDsource.variable(node), node);
265  else // decision node
266  addDecisionNode(IDsource.variable(node), node);
267  }
268  // we add arc in the same order of the potentials
269  for (auto node: IDsource.nodes()) {
270  const auto& s = IDsource.variable(node).name();
271  if (IDsource.isChanceNode(node)) {
272  for (Idx par = 1; par <= IDsource.parents(node).size(); par++)
273  addArc(IDsource.cpt(node).variable(par).name(), s);
274  } else if (IDsource.isUtilityNode(node)) {
275  for (Idx par = 1; par <= IDsource.parents(node).size(); par++)
276  addArc(IDsource.utility(node).variable(par).name(), s);
277  } else { // decision node
278  // here the order does not depends on a Potential
279  for (NodeId par: IDsource.parents(node))
280  addArc(par, node);
281  }
282  }
283 
284  // Copying potentials
285  for (auto node: IDsource.nodes()) {
286  const auto& s = IDsource.variable(node).name();
287  if (IDsource.isChanceNode(node)) {
288  cpt(node).fillWith(IDsource.cpt(s));
289  } else if (IDsource.isUtilityNode(node)) {
290  utility(node).fillWith(IDsource.utility(s));
291  }
292  }
293  }
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.
NodeId addChanceNode(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it&#39;s associate node and it&#39;s CPT.
virtual const Potential< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
NodeId addUtilityNode(const DiscreteVariable &variable, NodeId id=0)
Add a utility variable, it&#39;s associate node and it&#39;s UT.
NodeId addDecisionNode(const DiscreteVariable &variable, NodeId id=0)
Add a decision variable.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a potential variable.

◆ cpt() [1/2]

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::InfluenceDiagram< GUM_SCALAR >::cpt ( NodeId  varId) const
virtual

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 377 of file influenceDiagram_tpl.h.

377  {
378  return *(_potentialMap_[varId]);
379  }
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.

◆ cpt() [2/2]

template<typename GUM_SCALAR>
virtual const Potential< GUM_SCALAR >& gum::InfluenceDiagram< GUM_SCALAR >::cpt ( std::string  name) const
inlinefinalvirtual

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 130 of file influenceDiagram.h.

130  {
131  return cpt(idFromName(name));
132  };
NodeId idFromName(const std::string &name) const final
Getter by name.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a potential variable.

◆ dag()

INLINE const DAG & gum::DAGmodel::dag ( ) const
inherited

Returns a constant reference to the dag of this Bayes Net.

Definition at line 35 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

35 { return dag_; }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ decisionNodeSize()

template<typename GUM_SCALAR >
INLINE Size gum::InfluenceDiagram< GUM_SCALAR >::decisionNodeSize ( ) const

Returns the number of decision nodes.

Definition at line 438 of file influenceDiagram_tpl.h.

438  {
439  return (size() - _utilityMap_.size() - _potentialMap_.size());
440  }
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:38
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.

◆ decisionOrder()

template<typename GUM_SCALAR >
std::vector< NodeId > gum::InfluenceDiagram< GUM_SCALAR >::decisionOrder ( ) const

Returns the sequence of decision nodes in the directed path.

Exceptions
NotFoundif such a path does not exist

Definition at line 856 of file influenceDiagram_tpl.h.

856  {
857  if (!decisionOrderExists()) { GUM_ERROR(NotFound, "No decision path exists") }
858 
859  std::vector< NodeId > decisionSequence;
860 
861  for (const auto elt: topologicalOrder(false))
862  if (isDecisionNode(elt)) decisionSequence.push_back(elt);
863 
864  return decisionSequence;
865  }
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
bool decisionOrderExists() const
True if a directed path exist with all decision nodes.
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_inl.h:86
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ decisionOrderExists()

template<typename GUM_SCALAR >
bool gum::InfluenceDiagram< GUM_SCALAR >::decisionOrderExists ( ) const

True if a directed path exist with all decision nodes.

Definition at line 729 of file influenceDiagram_tpl.h.

729  {
730  const Sequence< NodeId >& order = topologicalOrder(true);
731 
732  // Finding first decision node
733  Sequence< NodeId >::const_iterator orderIter = order.begin();
734 
735  while ((orderIter != order.end()) && (!isDecisionNode(*orderIter)))
736  ++orderIter;
737 
738  if (orderIter == order.end()) return true;
739 
740  NodeId parentDecision = (*orderIter);
741  ++orderIter;
742 
743  // Checking path between decisions nodes
744  while (orderIter != order.end()) {
745  if (isDecisionNode(*orderIter)) {
746  if (!existsPathBetween(parentDecision, *orderIter)) return false;
747 
748  parentDecision = *orderIter;
749  }
750 
751  ++orderIter;
752  }
753 
754  return true;
755  }
SequenceIterator< NodeId > const_iterator
Definition: sequence.h:1030
iterator begin() const
Returns an unsafe begin iterator.
Definition: sequence_tpl.h:631
bool existsPathBetween(NodeId src, NodeId dest) const
Returns true if a path exists between two nodes.
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision 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_inl.h:86
const iterator & end() const noexcept
Returns the unsafe end iterator.
Definition: sequence_tpl.h:638
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ descendants() [1/2]

INLINE NodeSet gum::DAGmodel::descendants ( const NodeId  id) const
inherited

returns the set of nodes with directed path outgoing from a given node

Note that the set of nodes returned may be empty if no path within the ArcGraphPart is outgoing from the given node.

Parameters
idthe node which is the tail of a directed path with the returned nodes
namethe name of the node which is the tail of a directed path with the returned nodes

Definition at line 90 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

90 { return dag().descendants(id); }
NodeSet descendants(NodeId id) const
returns the set of nodes with directed path outgoing from a given node
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ descendants() [2/2]

INLINE NodeSet gum::DAGmodel::descendants ( const std::string &  name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 92 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

92  {
93  return descendants(idFromName(name));
94  }
NodeSet descendants(const NodeId id) const
returns the set of nodes with directed path outgoing from a given node
Definition: DAGmodel_inl.h:90
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ empty()

INLINE bool gum::GraphicalModel::empty ( ) const
virtualinherited

Return true if this graphical model is empty.

Definition at line 94 of file graphicalModel_inl.h.

94 { return size() == 0; }
virtual Size size() const =0
Returns the number of variables in this Directed Graphical Model.

◆ erase() [1/3]

template<typename GUM_SCALAR >
void gum::InfluenceDiagram< GUM_SCALAR >::erase ( NodeId  id)

Erase a Variable from the network and remove the variable from all his children.

If no variable matches the id, then nothing is done.

Parameters
idThe id of the variable to erase.

Definition at line 611 of file influenceDiagram_tpl.h.

611  {
612  if (_variableMap_.exists(varId)) {
613  // Reduce the variable child's CPT or Utility Table if necessary
614  for (const auto chi: dag_.children(varId))
615  if (isChanceNode(chi))
616  _potentialMap_[chi]->erase(variable(varId));
617  else if (isUtilityNode(chi))
618  _utilityMap_[chi]->erase(variable(varId));
619 
620  if (isChanceNode(varId)) {
621  delete _potentialMap_[varId];
622  _potentialMap_.erase(varId);
623  } else if (isUtilityNode(varId)) {
624  delete _utilityMap_[varId];
625  _utilityMap_.erase(varId);
626  }
627 
628  _variableMap_.erase(varId);
629  dag_.eraseNode(varId);
630  }
631  }
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 children.
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
bool exists(NodeId id) const
Return true if id matches a node.
void erase(NodeId id)
Removes a var and it&#39;s id of this mapping. The pointer is deleted.
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
VariableNodeMap _variableMap_
Mapping between id and variable.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it&#39;s node id.
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
Definition: diGraph_inl.h:67

◆ erase() [2/3]

template<typename GUM_SCALAR>
void gum::InfluenceDiagram< GUM_SCALAR >::erase ( const std::string &  name)
inline

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 303 of file influenceDiagram.h.

303 { erase(idFromName(name)); };
NodeId idFromName(const std::string &name) const final
Getter by name.
void erase(NodeId id)
Erase a Variable from the network and remove the variable from all his children.

◆ erase() [3/3]

template<typename GUM_SCALAR >
INLINE void gum::InfluenceDiagram< GUM_SCALAR >::erase ( const DiscreteVariable var)

Erase a Variable from the network and remove the variable from all his children.

If no variable matches, then nothing is done.

Parameters
varThe reference on the variable to remove.

Definition at line 639 of file influenceDiagram_tpl.h.

639  {
640  erase(_variableMap_.get(var));
641  }
void erase(NodeId id)
Erase a Variable from the network and remove the variable from all his children.
VariableNodeMap _variableMap_
Mapping between id and variable.
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.

◆ eraseArc() [1/3]

template<typename GUM_SCALAR >
INLINE void gum::InfluenceDiagram< GUM_SCALAR >::eraseArc ( const Arc arc)

Removes an arc in the ID, and update diagram's potential nodes cpt if necessary.

If (tail, head) doesn't exist, the nothing happens.

Parameters
arcThe arc removed.

Definition at line 678 of file influenceDiagram_tpl.h.

678  {
679  if (dag_.existsArc(arc)) {
680  NodeId head = arc.head(), tail = arc.tail();
681  dag_.eraseArc(arc);
682 
683  if (isChanceNode(head))
684  // Removes parent in the child's CPT
685  (*(_potentialMap_[head])) >> variable(tail);
686  else if (isUtilityNode(head))
687  // Removes parent in the child's UT
688  (*(_utilityMap_[head])) >> variable(tail);
689  }
690  }
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it&#39;s node id.
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ eraseArc() [2/3]

template<typename GUM_SCALAR >
INLINE void gum::InfluenceDiagram< GUM_SCALAR >::eraseArc ( NodeId  tail,
NodeId  head 
)

Removes an arc in the ID, and update diagram's potential nodes cpt if necessary.

If (tail, head) doesn't exist, the nothing happens.

Parameters
headand
tailas NodeId

Definition at line 698 of file influenceDiagram_tpl.h.

698  {
699  eraseArc(Arc(tail, head));
700  }
void eraseArc(const Arc &arc)
Removes an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.

◆ eraseArc() [3/3]

template<typename GUM_SCALAR>
void gum::InfluenceDiagram< GUM_SCALAR >::eraseArc ( const std::string &  tail,
const std::string &  head 
)
inline

Add an arc in the ID, and update diagram's potential nodes cpt if necessary.

Parameters
headand
tailas NodeId
Exceptions
InvalidEdgeIf arc.tail and/or arc.head are not in the ID.
InvalidEdgeif tail is a utility node

Definition at line 361 of file influenceDiagram.h.

361  {
362  eraseArc(idFromName(tail), idFromName(head));
363  }
NodeId idFromName(const std::string &name) const final
Getter by name.
void eraseArc(const Arc &arc)
Removes an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.

◆ exists() [1/2]

INLINE bool gum::DAGmodel::exists ( NodeId  node) const
finalvirtualinherited

Return true if this node exists in this graphical model.

Implements gum::GraphicalModel.

Definition at line 82 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

82 { return dag_.exists(node); }
bool exists(const NodeId id) const
alias for existsNode
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ exists() [2/2]

bool gum::GraphicalModel::exists ( const std::string &  name) const
inlineinherited

Return true if this graphical model is empty.

Definition at line 112 of file graphicalModel.h.

112 { return exists(idFromName(name)); };
virtual bool exists(NodeId node) const =0
Return true if this node exists in this graphical model.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.

◆ existsArc() [1/2]

INLINE bool gum::DAGmodel::existsArc ( const NodeId  tail,
const NodeId  head 
) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 45 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

45  {
46  return dag_.existsArc(tail, head);
47  }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
+ Here is the call graph for this function:

◆ existsArc() [2/2]

INLINE bool gum::DAGmodel::existsArc ( const std::string &  nametail,
const std::string &  namehead 
) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 49 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

49  {
50  return existsArc(idFromName(nametail), idFromName(namehead));
51  }
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:45
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ existsPathBetween() [1/2]

template<typename GUM_SCALAR >
bool gum::InfluenceDiagram< GUM_SCALAR >::existsPathBetween ( NodeId  src,
NodeId  dest 
) const

Returns true if a path exists between two nodes.

Definition at line 761 of file influenceDiagram_tpl.h.

761  {
762  List< NodeId > nodeFIFO;
763  // mark[node] contains 0 if not visited
764  // mark[node] = predecessor if visited
765  NodeProperty< int > mark = dag_.nodesProperty((int)-1);
766  NodeId current;
767 
768  mark[src] = (int)src;
769  nodeFIFO.pushBack(src);
770 
771  while (!nodeFIFO.empty()) {
772  current = nodeFIFO.front();
773  nodeFIFO.popFront();
774 
775  for (const auto new_one: dag_.children(current)) {
776  if (mark[new_one] != -1) continue; // if this node is already marked, continue
777 
778  mark[new_one] = (int)current;
779 
780  if (new_one == dest) break; // if we reach *orderIter, stop.
781 
782  nodeFIFO.pushBack(new_one);
783  }
784  }
785 
786  if (mark[dest] == -1) return false;
787 
788  return true;
789  }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
NodeProperty< VAL > nodesProperty(VAL(*f)(const NodeId &), Size size=0) const
a method to create a HashTable with key:NodeId and value:VAL
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ existsPathBetween() [2/2]

template<typename GUM_SCALAR>
bool gum::InfluenceDiagram< GUM_SCALAR >::existsPathBetween ( const std::string &  src,
const std::string &  dest 
) const
inline

True if a directed path exist with all decision nodes.

Definition at line 392 of file influenceDiagram.h.

392  {
393  return existsPathBetween(idFromName(src), idFromName(dest));
394  }
NodeId idFromName(const std::string &name) const final
Getter by name.
bool existsPathBetween(NodeId src, NodeId dest) const
Returns true if a path exists between two nodes.

◆ family() [1/2]

INLINE NodeSet gum::DAGmodel::family ( const NodeId  id) const
inherited

returns the parents of a node and the node

Note that the set of nodes returned may be empty if no arc within the ArcGraphPart is ingoing into the given node.

Parameters
idthe node which is the head of an arc with the returned nodes
namethe name of the node the node which is the head of an arc with the returned nodes

Definition at line 59 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

59 { return dag_.family(id); }
NodeSet family(NodeId id) const
returns the set of nodes which consists in the node and its parents
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ family() [2/2]

INLINE NodeSet gum::DAGmodel::family ( const std::string &  name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 61 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

61  {
62  return dag_.family(idFromName(name));
63  }
NodeSet family(NodeId id) const
returns the set of nodes which consists in the node and its parents
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ fastPrototype()

template<typename GUM_SCALAR >
InfluenceDiagram< GUM_SCALAR > gum::InfluenceDiagram< GUM_SCALAR >::fastPrototype ( const std::string &  dotlike,
Size  domainSize = 2 
)
static

Create an Influence Diagram with a dot-like syntax which specifies:

  • the structure "a->*b->$c;b->d<-*e;".
  • the prefix of a variable can be :
    • nothing : chance node (a,d)
    • * : decision node (*b)
    • $ : utility node ($c) *
  • the type of the chance or decision variables with different syntax:

Note that if the dot-like string contains such a specification more than once for a variable, the first specification will be used.

Parameters
dotlikethe string containing the specification
domainSizethe default domain size for chance and decision variables
Returns
the resulting influence diagram

Definition at line 149 of file influenceDiagram_tpl.h.

149  {
151 
152  for (const auto& chaine: split(dotlike, ";")) {
153  NodeId lastId = 0;
154  bool notfirst = false;
155  for (const auto& souschaine: split(chaine, "->")) {
156  bool forward = true;
157  for (const auto& node: split(souschaine, "<-")) {
158  auto idVar = build_node_for_ID(infdiag, node, domainSize);
159  if (notfirst) {
160  if (forward) {
161  infdiag.addArc(lastId, idVar);
162  forward = false;
163  } else {
164  infdiag.addArc(idVar, lastId);
165  }
166  } else {
167  notfirst = true;
168  forward = false;
169  }
170  lastId = idVar;
171  }
172  }
173  }
174 
175  for (const auto n: infdiag.nodes()) {
176  if (infdiag.isChanceNode(n))
177  infdiag.cpt(n).randomCPT();
178  else if (infdiag.isUtilityNode(n)) {
179  infdiag.utility(n).random().scale(50).translate(-10);
180  }
181  }
182 
183  infdiag.setProperty("name", "fastPrototype");
184  return infdiag;
185  }
NodeId build_node_for_ID(gum::InfluenceDiagram< GUM_SCALAR > &infdiag, std::string node, gum::Size default_domain_size)
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
virtual const Potential< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a potential variable.
Class representing an Influence Diagram.

◆ getChildrenDecision_()

template<typename GUM_SCALAR >
Sequence< NodeId > gum::InfluenceDiagram< GUM_SCALAR >::getChildrenDecision_ ( NodeId  parentDecision) const
protected

Returns the list of children decision for a given nodeId.

Definition at line 818 of file influenceDiagram_tpl.h.

818  {
819  Sequence< NodeId > childrenSeq;
820 
821  List< NodeId > nodeFIFO;
822  NodeId current;
823 
824  // mark[node] contains false if not visited
825  // mark[node] contains true if visited
826  NodeProperty< bool > mark = dag_.nodesProperty(false);
827 
828  mark[parentDecision] = true;
829 
830  nodeFIFO.pushBack(parentDecision);
831 
832  while (!nodeFIFO.empty()) {
833  current = nodeFIFO.front();
834  nodeFIFO.popFront();
835 
836  for (const auto new_one: dag_.children(current)) {
837  if (mark[new_one]) continue; // if this node is already marked, continue
838 
839  mark[new_one] = true;
840 
841  if (!isDecisionNode(new_one))
842  nodeFIFO.pushBack(new_one);
843  else
844  childrenSeq.insert(new_one);
845  }
846  }
847 
848  return childrenSeq;
849  }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of 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 isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:393

◆ getDecisionGraph()

template<typename GUM_SCALAR >
gum::DAG * gum::InfluenceDiagram< GUM_SCALAR >::getDecisionGraph ( ) const

Returns the temporal Graph.

Definition at line 795 of file influenceDiagram_tpl.h.

795  {
796  auto temporalGraph = new gum::DAG();
797 
798  for (const auto node: dag_.nodes()) {
799  if (isDecisionNode(node)) {
800  if (!temporalGraph->existsNode(node)) temporalGraph->addNodeWithId(node);
801 
802  for (const auto chi: getChildrenDecision_(node)) {
803  if (!temporalGraph->existsNode(chi)) temporalGraph->addNodeWithId(chi);
804 
805  temporalGraph->addArc(node, chi);
806  }
807  }
808  }
809 
810  return temporalGraph;
811  }
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Sequence< NodeId > getChildrenDecision_(NodeId parentDecision) const
Returns the list of children decision for a given nodeId.
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
Base class for dag.
Definition: DAG.h:101

◆ getPartialTemporalOrder()

template<typename GUM_SCALAR >
const List< NodeSet > & gum::InfluenceDiagram< GUM_SCALAR >::getPartialTemporalOrder ( bool  clear = true) const

Returns partial temporal ordering.

Exceptions
NotFoundif such a sequence does not exist

Definition at line 872 of file influenceDiagram_tpl.h.

872  {
873  if (clear) {
874  _temporalOrder_.clear();
875 
876  std::vector< NodeId > order = decisionOrder();
877  NodeSet nodeList = dag_.asNodeSet();
878 
879  for (auto i: order) {
880  NodeSet partialOrderedSet;
881 
882  for (const auto par: dag_.parents(i)) {
883  if (nodeList.contains(par) && isChanceNode(par)) {
884  partialOrderedSet.insert(par);
885  nodeList.erase(par);
886  }
887  }
888 
889  if (!partialOrderedSet.empty()) _temporalOrder_.pushFront(partialOrderedSet);
890 
891  NodeSet decisionSet;
892 
893  decisionSet.insert(i);
894 
895  _temporalOrder_.pushFront(decisionSet);
896  }
897 
898  NodeSet lastSet; //= new gum::NodeSet();
899 
900  for (const auto node: nodeList)
901  if (isChanceNode(node)) lastSet.insert(node);
902 
903  if (!lastSet.empty()) _temporalOrder_.pushFront(lastSet);
904  }
905 
906  return _temporalOrder_;
907  }
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
std::vector< NodeId > decisionOrder() const
Returns the sequence of decision nodes in the directed path.
List< NodeSet > _temporalOrder_
The temporal order.
NodeSet asNodeSet() const
returns a copy of the set of nodes represented by the NodeGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606

◆ hasSameStructure()

bool gum::DAGmodel::hasSameStructure ( const DAGmodel other)
inherited
Returns
true if all the named node are the same and all the named arcs are the same

Definition at line 69 of file DAGmodel.cpp.

References gum::Set< Key, Alloc >::emplace().

69  {
70  if (this == &other) return true;
71 
72  if (size() != other.size()) return false;
73 
74  if (sizeArcs() != other.sizeArcs()) return false;
75 
76  for (const auto& nid: nodes()) {
77  try {
78  other.idFromName(variable(nid).name());
79  } catch (NotFound) { return false; }
80  }
81 
82  for (const auto& arc: arcs()) {
83  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
84  other.idFromName(variable(arc.head()).name()))))
85  return false;
86  }
87 
88  return true;
89  }
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:43
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:41
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:38
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
+ Here is the call graph for this function:

◆ idFromName()

template<typename GUM_SCALAR >
INLINE NodeId gum::InfluenceDiagram< GUM_SCALAR >::idFromName ( const std::string &  name) const
finalvirtual

Getter by name.

Exceptions
NotFoundif no such name exists in the graph.

Implements gum::GraphicalModel.

Definition at line 469 of file influenceDiagram_tpl.h.

469  {
470  return _variableMap_.idFromName(name);
471  }
VariableNodeMap _variableMap_
Mapping between id and variable.
NodeId idFromName(const std::string &name) const

◆ ids()

INLINE std::vector< NodeId > gum::GraphicalModel::ids ( const std::vector< std::string > &  names) const
inherited

transform a vector of names into a vector of nodeId

Returns
the vector of names

Definition at line 117 of file graphicalModel_inl.h.

117  {
118  std::vector< NodeId > res;
119  const VariableNodeMap& v = variableNodeMap();
120  std::transform(names.cbegin(),
121  names.cend(),
122  std::back_inserter(res),
123  [v](const std::string& n) { return v.idFromName(n); });
124  return res;
125  }
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.

◆ isChanceNode() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::InfluenceDiagram< GUM_SCALAR >::isChanceNode ( NodeId  varId) const

Returns true if node is a chance one.

Definition at line 414 of file influenceDiagram_tpl.h.

414  {
415  return _potentialMap_.exists(varId);
416  }
NodeProperty< Potential< GUM_SCALAR > *> _potentialMap_
Mapping between potential variable&#39;s id and their CPT.

◆ isChanceNode() [2/2]

template<typename GUM_SCALAR>
bool gum::InfluenceDiagram< GUM_SCALAR >::isChanceNode ( const std::string &  name) const
inline

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 164 of file influenceDiagram.h.

164 { return isChanceNode(idFromName(name)); };
NodeId idFromName(const std::string &name) const final
Getter by name.
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.

◆ isDecisionNode() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::InfluenceDiagram< GUM_SCALAR >::isDecisionNode ( NodeId  varId) const

Returns true if node is a decision one.

Definition at line 402 of file influenceDiagram_tpl.h.

402  {
403  bool ret = true;
404 
405  if (isUtilityNode(varId) || isChanceNode(varId)) ret = false;
406 
407  return ret;
408  }
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.

◆ isDecisionNode() [2/2]

template<typename GUM_SCALAR>
bool gum::InfluenceDiagram< GUM_SCALAR >::isDecisionNode ( const std::string &  name) const
inline

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 158 of file influenceDiagram.h.

158 { return isDecisionNode(idFromName(name)); };
NodeId idFromName(const std::string &name) const final
Getter by name.
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.

◆ isIndependent() [1/4]

INLINE bool gum::DAGmodel::isIndependent ( NodeId  X,
NodeId  Y,
const NodeSet Z 
) const
finalvirtualinherited

check if node X and node Y are independent given nodes Z

Implements gum::GraphicalModel.

Definition at line 113 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

113  {
114  return dag().dSeparation(X, Y, Z);
115  }
bool dSeparation(NodeId X, NodeId Y, const NodeSet &Z) const
check if node X and node Y are independent given nodes Z (in the sense of d-separation) ...
Definition: DAG.cpp:105
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ isIndependent() [2/4]

INLINE bool gum::DAGmodel::isIndependent ( const NodeSet X,
const NodeSet Y,
const NodeSet Z 
) const
finalvirtualinherited

check if nodes X and nodes Y are independent given nodes Z

Implements gum::GraphicalModel.

Definition at line 117 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

117  {
118  return dag().dSeparation(X, Y, Z);
119  }
bool dSeparation(NodeId X, NodeId Y, const NodeSet &Z) const
check if node X and node Y are independent given nodes Z (in the sense of d-separation) ...
Definition: DAG.cpp:105
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ isIndependent() [3/4]

bool gum::DAGmodel::isIndependent ( const std::string &  Xname,
const std::string &  Yname,
const std::vector< std::string > &  Znames 
) const
inlineinherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 185 of file DAGmodel.h.

187  {
188  return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
189  };
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if node X and node Y are independent given nodes Z
Definition: DAGmodel_inl.h:113

◆ isIndependent() [4/4]

bool gum::DAGmodel::isIndependent ( const std::vector< std::string > &  Xnames,
const std::vector< std::string > &  Ynames,
const std::vector< std::string > &  Znames 
) const
inlineinherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 191 of file DAGmodel.h.

193  {
194  return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
195  };
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if node X and node Y are independent given nodes Z
Definition: DAGmodel_inl.h:113

◆ isUtilityNode() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::InfluenceDiagram< GUM_SCALAR >::isUtilityNode ( NodeId  varId) const

Returns true if node is a utility one.

Definition at line 394 of file influenceDiagram_tpl.h.

394  {
395  return _utilityMap_.exists(varId);
396  }
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.

◆ isUtilityNode() [2/2]

template<typename GUM_SCALAR>
bool gum::InfluenceDiagram< GUM_SCALAR >::isUtilityNode ( const std::string &  name) const
inline

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 153 of file influenceDiagram.h.

153 { return isUtilityNode(idFromName(name)); };
NodeId idFromName(const std::string &name) const final
Getter by name.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.

◆ log10DomainSize()

INLINE double gum::GraphicalModel::log10DomainSize ( ) const
inherited

Definition at line 73 of file graphicalModel_inl.h.

73  {
74  double dSize = 0.0;
75 
76  for (const auto node: nodes()) {
77  dSize += std::log10(variable(node).domainSize());
78  }
79 
80  return dSize;
81  }
virtual const NodeGraphPart & nodes() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.

◆ moralGraph()

const UndiGraph & gum::DAGmodel::moralGraph ( bool  clear = true) const
inherited

The node's id are coherent with the variables and nodes of the topology.

Parameters
clearIf false returns the previously created moral graph.

Definition at line 55 of file DAGmodel.cpp.

References gum::Set< Key, Alloc >::emplace().

55  {
56  if (clear || (_mutableMoralGraph_ == nullptr)) { // we have to call dag().moralGraph()
57  if (_mutableMoralGraph_ == nullptr) {
58  _mutableMoralGraph_ = new UndiGraph();
59  } else {
60  // clear is True , __mutableMoralGraph exists
62  }
64  }
65 
66  return *_mutableMoralGraph_;
67  }
void clear() override
removes all the nodes and edges from the graph
Definition: undiGraph_inl.h:42
UndiGraph moralGraph() const
build a UndiGraph by moralizing the dag
Definition: DAG.cpp:55
UndiGraph * _mutableMoralGraph_
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:230
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ moralGraph_()

template<typename GUM_SCALAR >
void gum::InfluenceDiagram< GUM_SCALAR >::moralGraph_ ( UndiGraph graph) const
protectedvirtual

Returns the moral graph of this InfluenceDiagram.

Definition at line 710 of file influenceDiagram_tpl.h.

710  {
711  for (const auto node: dag_.nodes())
712  if (!isUtilityNode(node)) graph.addNodeWithId(node);
713 
714  for (const auto node: dag_.nodes()) {
715  if (!isDecisionNode(node))
716  for (const auto par: dag_.parents(node)) {
717  if (isChanceNode(node)) graph.addEdge(node, par);
718 
719  for (const auto par2: dag_.parents(node))
720  if (par != par2) graph.addEdge(par, par2);
721  }
722  }
723  }
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
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 NodeGraphPart & nodes() const
return *this as a NodeGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222

◆ moralizedAncestralGraph() [1/2]

INLINE UndiGraph gum::DAGmodel::moralizedAncestralGraph ( const NodeSet nodes) const
inherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 109 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

109  {
111  }
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAG.cpp:79
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ moralizedAncestralGraph() [2/2]

INLINE UndiGraph gum::DAGmodel::moralizedAncestralGraph ( const std::vector< std::string > &  nodenames) const
inherited

build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes

Parameters
nodesthe set of nodeId
nodenamesthe vector of names of nodes
Returns
the moralized ancestral graph

Definition at line 104 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

104  {
105  return moralizedAncestralGraph(nodeset(nodenames));
106  }
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAGmodel_inl.h:109
+ Here is the call graph for this function:

◆ names() [1/2]

INLINE std::vector< std::string > gum::GraphicalModel::names ( const std::vector< NodeId > &  ids) const
inherited

transform a vector of NodeId in a vector of names

Returns
the vector of names

Definition at line 97 of file graphicalModel_inl.h.

97  {
98  std::vector< std::string > res;
99  const VariableNodeMap& v = variableNodeMap();
100  std::transform(ids.cbegin(), ids.cend(), std::back_inserter(res), [v](NodeId n) {
101  return v[n].name();
102  });
103  return res;
104  }
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ names() [2/2]

INLINE std::vector< std::string > gum::GraphicalModel::names ( const NodeSet ids) const
inherited

transform a NodeSet in a vector of names

Returns
the vector of names

Definition at line 107 of file graphicalModel_inl.h.

107  {
108  const VariableNodeMap& v = variableNodeMap();
109  std::vector< std::string > res;
110  for (auto n: ids) {
111  res.push_back(v.name(n));
112  }
113  return res;
114  }
std::vector< NodeId > ids(const std::vector< std::string > &names) const
transform a vector of names into a vector of nodeId
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.

◆ nodeId()

template<typename GUM_SCALAR >
INLINE NodeId gum::InfluenceDiagram< GUM_SCALAR >::nodeId ( const DiscreteVariable var) const
finalvirtual

Return id node from discrete var pointer.

Exceptions
NotFoundIf no variable matches var.

Implements gum::GraphicalModel.

Definition at line 463 of file influenceDiagram_tpl.h.

463  {
464  return _variableMap_.get(var);
465  }
VariableNodeMap _variableMap_
Mapping between id and variable.
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.

◆ nodes()

INLINE const NodeGraphPart & gum::DAGmodel::nodes ( ) const
finalvirtualinherited

Returns a constant reference to the dag of this Bayes Net.

Implements gum::GraphicalModel.

Definition at line 84 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

84 { return (NodeGraphPart&)dag_; }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ nodeset()

NodeSet gum::GraphicalModel::nodeset ( const std::vector< std::string > &  names) const
inherited

transform a vector of names into a NodeSet

Returns
NodeSet

Definition at line 58 of file graphicalModel.cpp.

References gum::Set< Key, Alloc >::emplace().

58  {
59  NodeSet res;
60  for (const auto& name: names) {
61  res.insert(idFromName(name));
62  }
63  return res;
64  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
+ Here is the call graph for this function:

◆ operator=()

template<typename GUM_SCALAR >
InfluenceDiagram< GUM_SCALAR > & gum::InfluenceDiagram< GUM_SCALAR >::operator= ( const InfluenceDiagram< GUM_SCALAR > &  source)

Copy Operator.

Definition at line 221 of file influenceDiagram_tpl.h.

221  {
222  if (this != &source) {
223  clear();
224  // Copying tables and structure
225  copyStructureAndTables_(source);
226  }
227 
228  return *this;
229  }
void copyStructureAndTables_(const InfluenceDiagram< GUM_SCALAR > &IDsource)
Copying tables from another influence diagram.

◆ parents() [1/4]

INLINE const NodeSet & gum::DAGmodel::parents ( const NodeId  id) const
inherited

returns the set of nodes with arc ingoing to a given node

Note that the set of nodes returned may be empty if no arc within the ArcGraphPart is ingoing into the given node.

Parameters
idthe node which is the head of an arc with the returned nodes
namethe name of the node the node which is the head of an arc with the returned nodes

Definition at line 53 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

53 { return dag_.parents(id); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ parents() [2/4]

INLINE const NodeSet & gum::DAGmodel::parents ( const std::string &  name) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 55 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

55  {
56  return parents(idFromName(name));
57  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ Here is the call graph for this function:

◆ parents() [3/4]

INLINE NodeSet gum::DAGmodel::parents ( const NodeSet ids) const
inherited

returns the parents of a set of nodes

Definition at line 76 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

76 { return dag_.children(ids); }
std::vector< NodeId > ids(const std::vector< std::string > &names) const
transform a vector of names into a vector of nodeId
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ parents() [4/4]

INLINE NodeSet gum::DAGmodel::parents ( const std::vector< std::string > &  names) const
inherited

return true if the arc tail->head exists in the DAGmodel

Parameters
tailthe nodeId (or the name) of the tail in tail->head
headthe nodeId (or the name) of the head in tail->head
Returns
true if the arc exists

Definition at line 78 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

78  {
79  return parents(nodeset(names));
80  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
+ Here is the call graph for this function:

◆ property()

INLINE const std::string & gum::GraphicalModel::property ( const std::string &  name) const
inherited

Return the value of the property name of this GraphicalModel.

Exceptions
NotFoundRaised if no name property is found.

Definition at line 38 of file graphicalModel_inl.h.

38  {
39  try {
40  return _properties_()[name];
41  } catch (NotFound&) {
42  std::string msg = "The following property does not exists: ";
43  GUM_ERROR(NotFound, msg + name)
44  }
45  }
HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ propertyWithDefault()

INLINE const std::string & gum::GraphicalModel::propertyWithDefault ( const std::string &  name,
const std::string &  byDefault 
) const
inherited

Return the value of the property name of this GraphicalModel.

return byDefault if the property name is not found

Definition at line 57 of file graphicalModel_inl.h.

58  {
59  try {
60  return _properties_()[name];
61  } catch (NotFound&) { return byDefault; }
62  }
HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...

◆ removeTables_()

template<typename GUM_SCALAR >
void gum::InfluenceDiagram< GUM_SCALAR >::removeTables_ ( )
protected

Removing ancient table.

Definition at line 245 of file influenceDiagram_tpl.h.

245  {
246  for (const auto node: dag_.nodes()) {
247  if (isChanceNode(node))
248  delete &cpt(node);
249  else if (isUtilityNode(node))
250  delete &utility(node);
251  }
252  }
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
virtual const Potential< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a potential variable.

◆ setProperty()

INLINE void gum::GraphicalModel::setProperty ( const std::string &  name,
const std::string &  value 
)
inherited

Add or change a property of this GraphicalModel.

Definition at line 65 of file graphicalModel_inl.h.

65  {
66  try {
67  _properties_()[name] = value;
68  } catch (NotFound&) { _properties_().insert(name, value); }
69  }
HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ size()

INLINE Size gum::DAGmodel::size ( ) const
finalvirtualinherited

Returns the number of variables in this Directed Graphical Model.

Implements gum::GraphicalModel.

Definition at line 38 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

38 { return dag().size(); }
Size size() const
alias for sizeNodes
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ sizeArcs()

INLINE Size gum::DAGmodel::sizeArcs ( ) const
inherited

Returns the number of arcs in this Directed Graphical Model.

Definition at line 41 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

41 { return dag_.sizeArcs(); }
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
+ Here is the call graph for this function:

◆ toDot()

template<typename GUM_SCALAR >
std::string gum::InfluenceDiagram< GUM_SCALAR >::toDot ( ) const
Returns
Returns a dot representation of this Influence Diagram.

Definition at line 296 of file influenceDiagram_tpl.h.

296  {
297  std::stringstream output;
298  std::stringstream decisionNode;
299  std::stringstream utilityNode;
300  std::stringstream chanceNode;
301  std::stringstream arcstream;
302  output << "digraph \"";
303 
304  try {
305  output << this->property("name") << "\" {" << std::endl;
306  } catch (NotFound&) { output << "no_name\" {" << std::endl; }
307 
308  output << " node [bgcolor=\"#AAAAAA\", style=filled, height=0];" << std::endl;
309 
310  decisionNode << "node [shape = box];" << std::endl;
311 
312  utilityNode << "node [shape = hexagon, margin=0];" << std::endl;
313  chanceNode << "node [shape = ellipse];" << std::endl;
314  std::string tab = " ";
315 
316  for (const auto node: dag_.nodes()) {
317  if (isChanceNode(node))
318  chanceNode << tab << "\"" << node << "-" << variable(node).name() << "\""
319  << ";";
320  else if (isUtilityNode(node))
321  utilityNode << tab << "\"" << node << "-" << variable(node).name() << "\""
322  << ";";
323  else
324  decisionNode << tab << "\"" << node << "-" << variable(node).name() << "\""
325  << ";";
326 
327  if (dag_.children(node).size() > 0)
328  for (const auto chi: dag_.children(node)) {
329  arcstream << "\"" << node << "-" << variable(node).name() << "\""
330  << " -> "
331  << "\"" << chi << "-" << variable(chi).name() << "\"";
332  if (isDecisionNode(chi)) { arcstream << " [style=\"tapered, bold\"]"; }
333  arcstream << ";" << std::endl;
334  }
335  }
336 
337  output << decisionNode.str() << std::endl
338  << utilityNode.str() << std::endl
339  << chanceNode.str() << std::endl
340  << std::endl
341  << arcstream.str() << std::endl
342  << "}" << std::endl;
343 
344  return output.str();
345  }
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
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 NodeGraphPart & nodes() const
return *this as a NodeGraphPart
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:222
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:694
const std::string & name() const
returns the name of the variable
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it&#39;s node id.
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.

◆ topologicalOrder()

INLINE const Sequence< NodeId > & gum::DAGmodel::topologicalOrder ( bool  clear = true) const
inherited

The topological order stays the same as long as no variable or arcs are added or erased src the topology.

Parameters
clearIf false returns the previously created topology.

Definition at line 86 of file DAGmodel_inl.h.

References gum::Set< Key, Alloc >::emplace().

86  {
87  return dag().topologicalOrder(clear);
88  }
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: diGraph.cpp:88
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35
+ Here is the call graph for this function:

◆ toString()

template<typename GUM_SCALAR >
std::string gum::InfluenceDiagram< GUM_SCALAR >::toString ( ) const
Returns
Returns a string representation of this Influence Diagram.

Definition at line 348 of file influenceDiagram_tpl.h.

348  {
349  std::stringstream output;
350 
351  output << "Influence Diagram{" << std::endl;
352  output << " chance: " << chanceNodeSize() << "," << std::endl;
353  output << " utility: " << utilityNodeSize() << "," << std::endl;
354  output << " decision: " << decisionNodeSize() << "," << std::endl;
355  output << " arcs: " << dag().sizeArcs() << "," << std::endl;
356 
357  double dSize = log10DomainSize();
358 
359  if (dSize > 6)
360  output << " domainSize: 10^" << dSize;
361  else
362  output << " domainSize: " << std::round(std::pow(10.0, dSize));
363 
364  output << std::endl << "}";
365 
366  return output.str();
367  }
Size decisionNodeSize() const
Returns the number of decision nodes.
Size chanceNodeSize() const
Returns the number of chance nodes.
Size utilityNodeSize() const
Returns the number of utility nodes.
double log10DomainSize() const
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:35

◆ utility() [1/2]

template<typename GUM_SCALAR >
INLINE const Potential< GUM_SCALAR > & gum::InfluenceDiagram< GUM_SCALAR >::utility ( NodeId  varId) const
virtual

Returns the utility table of a utility node.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 386 of file influenceDiagram_tpl.h.

386  {
387  return *(_utilityMap_[varId]);
388  }
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.

◆ utility() [2/2]

template<typename GUM_SCALAR>
virtual const Potential< GUM_SCALAR >& gum::InfluenceDiagram< GUM_SCALAR >::utility ( std::string  name) const
inlinefinalvirtual

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 139 of file influenceDiagram.h.

139  {
140  return utility(idFromName(name));
141  };
NodeId idFromName(const std::string &name) const final
Getter by name.
virtual const Potential< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.

◆ utilityNodeSize()

template<typename GUM_SCALAR >
INLINE Size gum::InfluenceDiagram< GUM_SCALAR >::utilityNodeSize ( ) const

Returns the number of utility nodes.

Definition at line 422 of file influenceDiagram_tpl.h.

422  {
423  return _utilityMap_.size();
424  }
NodeProperty< Potential< GUM_SCALAR > *> _utilityMap_
Mapping between utility variable&#39;s id and their utility table.

◆ variable()

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::InfluenceDiagram< GUM_SCALAR >::variable ( NodeId  id) const
finalvirtual

Returns a constant reference over a variable given it's node id.

Exceptions
NotFoundIf no variable's id matches varId.

Implements gum::GraphicalModel.

Definition at line 455 of file influenceDiagram_tpl.h.

455  {
456  return _variableMap_[id];
457  }
VariableNodeMap _variableMap_
Mapping between id and variable.

◆ variableFromName()

template<typename GUM_SCALAR >
INLINE const DiscreteVariable & gum::InfluenceDiagram< GUM_SCALAR >::variableFromName ( const std::string &  name) const
finalvirtual

Getter by name.

Exceptions
NotFoundif no such name exists in the graph.

Implements gum::GraphicalModel.

Definition at line 476 of file influenceDiagram_tpl.h.

476  {
477  return _variableMap_.variableFromName(name);
478  }
VariableNodeMap _variableMap_
Mapping between id and variable.
const DiscreteVariable & variableFromName(const std::string &name) const

◆ variableNodeMap()

template<typename GUM_SCALAR >
INLINE const VariableNodeMap & gum::InfluenceDiagram< GUM_SCALAR >::variableNodeMap ( ) const
finalvirtual

Returns a constant reference to the VariableNodeMap of this Influence Diagram.

Implements gum::GraphicalModel.

Definition at line 447 of file influenceDiagram_tpl.h.

447  {
448  return _variableMap_;
449  }
VariableNodeMap _variableMap_
Mapping between id and variable.

Member Data Documentation

◆ _potentialMap_

template<typename GUM_SCALAR>
NodeProperty< Potential< GUM_SCALAR >* > gum::InfluenceDiagram< GUM_SCALAR >::_potentialMap_
private

Mapping between potential variable's id and their CPT.

Definition at line 433 of file influenceDiagram.h.

◆ _temporalOrder_

template<typename GUM_SCALAR>
List< NodeSet > gum::InfluenceDiagram< GUM_SCALAR >::_temporalOrder_
mutableprivate

The temporal order.

Definition at line 438 of file influenceDiagram.h.

◆ _utilityMap_

template<typename GUM_SCALAR>
NodeProperty< Potential< GUM_SCALAR >* > gum::InfluenceDiagram< GUM_SCALAR >::_utilityMap_
private

Mapping between utility variable's id and their utility table.

Definition at line 435 of file influenceDiagram.h.

◆ _variableMap_

template<typename GUM_SCALAR>
VariableNodeMap gum::InfluenceDiagram< GUM_SCALAR >::_variableMap_
private

Mapping between id and variable.

Definition at line 430 of file influenceDiagram.h.

◆ dag_

DAG gum::DAGmodel::dag_
protectedinherited

The DAG of this Directed Graphical Model.

Definition at line 222 of file DAGmodel.h.


The documentation for this class was generated from the following files: