aGrUM  0.21.0
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...
 
const DiscreteVariablevariable (const std::string &name) const
 Returns the CPT of a potential variable. 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 208 of file influenceDiagram_tpl.h.

208  : DAGmodel() {
209  GUM_CONSTRUCTOR(InfluenceDiagram);
210  }
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 216 of file influenceDiagram_tpl.h.

216  {
217  GUM_DESTRUCTOR(InfluenceDiagram);
218  removeTables_();
219  }
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 225 of file influenceDiagram_tpl.h.

225  {
226  GUM_CONS_CPY(InfluenceDiagram);
227  copyStructureAndTables_(source);
228  }
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 499 of file influenceDiagram_tpl.h.

499  {
500  return addChanceNode(var, varId);
501  }
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 672 of file influenceDiagram_tpl.h.

672  {
673  if (isUtilityNode(tail)) { GUM_ERROR(InvalidArc, "Tail cannot be a utility node") }
674 
675  dag_.addArc(tail, head);
676 
677  if (isChanceNode(head))
678  // Add parent in the child's CPT
679  (*(_potentialMap_[head])) << variable(tail);
680  else if (isUtilityNode(head)) {
681  // Add parent in the child's UT
682  (*(_utilityMap_[head])) << variable(tail);
683  }
684  }
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 342 of file influenceDiagram.h.

342  {
343  addArc(idFromName(tail), idFromName(head));
344  }
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 538 of file influenceDiagram_tpl.h.

538  {
539  auto newMultiDim = new MultiDimArray< GUM_SCALAR >();
540  NodeId res;
541 
542  try {
543  res = addChanceNode(var, newMultiDim, varId);
544  } catch (Exception&) {
545  delete newMultiDim;
546  throw;
547  }
548 
549  return res;
550  }
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 558 of file influenceDiagram_tpl.h.

560  {
561  NodeId proposedId = addNode_(var, DesiredId);
562 
563  auto varcpt = new Potential< GUM_SCALAR >(aContent);
564  (*varcpt) << variable(proposedId);
565  _potentialMap_.insert(proposedId, varcpt);
566 
567  return proposedId;
568  }
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 528 of file influenceDiagram_tpl.h.

529  {
530  return addNode_(var, varId);
531  }
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 601 of file influenceDiagram_tpl.h.

602  {
603  // None thread safe code!
604  NodeId proposedId;
605 
606  if (DesiredId == 0)
607  proposedId = dag_.nextNodeId();
608  else
609  proposedId = DesiredId;
610 
611  _variableMap_.insert(proposedId, variableType);
612 
613  dag_.addNodeWithId(proposedId);
614 
615  // end critical section
616  return proposedId;
617  }
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 509 of file influenceDiagram_tpl.h.

509  {
510  auto newMultiDim = new MultiDimArray< GUM_SCALAR >();
511  NodeId res;
512 
513  try {
514  res = addUtilityNode(var, newMultiDim, varId);
515  } catch (Exception&) {
516  if (newMultiDim != nullptr) delete newMultiDim;
517  throw;
518  }
519 
520  return res;
521  }
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 577 of file influenceDiagram_tpl.h.

579  {
580  if (var.domainSize() != 1) {
581  GUM_ERROR(InvalidArgument,
582  "Utility var have no state ( which implicates a "
583  "single label for data output reasons ).")
584  }
585 
586  NodeId proposedId = addNode_(var, DesiredId);
587 
588  auto varut = new Potential< GUM_SCALAR >(aContent);
589 
590  (*varut) << variable(proposedId);
591 
592  _utilityMap_.insert(proposedId, varut);
593 
594  return proposedId;
595  }
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 444 of file influenceDiagram_tpl.h.

444  {
445  return _potentialMap_.size();
446  }
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 660 of file influenceDiagram_tpl.h.

661  {
662  _variableMap_.changeName(id, new_name);
663  }
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 322 of file influenceDiagram.h.

322  {
323  changeVariableName(idFromName(name), new_name);
324  }
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 272 of file influenceDiagram_tpl.h.

273  {
274  for (auto node: IDsource.nodes()) {
275  if (IDsource.isChanceNode(node))
276  addChanceNode(IDsource.variable(node), node);
277  else if (IDsource.isUtilityNode(node))
278  addUtilityNode(IDsource.variable(node), node);
279  else // decision node
280  addDecisionNode(IDsource.variable(node), node);
281  }
282  // we add arc in the same order of the potentials
283  for (auto node: IDsource.nodes()) {
284  const auto& s = IDsource.variable(node).name();
285  if (IDsource.isChanceNode(node)) {
286  for (Idx par = 1; par <= IDsource.parents(node).size(); par++)
287  addArc(IDsource.cpt(node).variable(par).name(), s);
288  } else if (IDsource.isUtilityNode(node)) {
289  for (Idx par = 1; par <= IDsource.parents(node).size(); par++)
290  addArc(IDsource.utility(node).variable(par).name(), s);
291  } else { // decision node
292  // here the order does not depends on a Potential
293  for (NodeId par: IDsource.parents(node))
294  addArc(par, node);
295  }
296  }
297 
298  // Copying potentials
299  for (auto node: IDsource.nodes()) {
300  const auto& s = IDsource.variable(node).name();
301  if (IDsource.isChanceNode(node)) {
302  cpt(node).fillWith(IDsource.cpt(s));
303  } else if (IDsource.isUtilityNode(node)) {
304  utility(node).fillWith(IDsource.utility(s));
305  }
306  }
307  }
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 391 of file influenceDiagram_tpl.h.

391  {
392  return *(_potentialMap_[varId]);
393  }
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 452 of file influenceDiagram_tpl.h.

452  {
453  return (size() - _utilityMap_.size() - _potentialMap_.size());
454  }
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 870 of file influenceDiagram_tpl.h.

870  {
871  if (!decisionOrderExists()) { GUM_ERROR(NotFound, "No decision path exists") }
872 
873  std::vector< NodeId > decisionSequence;
874 
875  for (const auto elt: topologicalOrder(false))
876  if (isDecisionNode(elt)) decisionSequence.push_back(elt);
877 
878  return decisionSequence;
879  }
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 743 of file influenceDiagram_tpl.h.

743  {
744  const Sequence< NodeId >& order = topologicalOrder(true);
745 
746  // Finding first decision node
747  Sequence< NodeId >::const_iterator orderIter = order.begin();
748 
749  while ((orderIter != order.end()) && (!isDecisionNode(*orderIter)))
750  ++orderIter;
751 
752  if (orderIter == order.end()) return true;
753 
754  NodeId parentDecision = (*orderIter);
755  ++orderIter;
756 
757  // Checking path between decisions nodes
758  while (orderIter != order.end()) {
759  if (isDecisionNode(*orderIter)) {
760  if (!existsPathBetween(parentDecision, *orderIter)) return false;
761 
762  parentDecision = *orderIter;
763  }
764 
765  ++orderIter;
766  }
767 
768  return true;
769  }
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 625 of file influenceDiagram_tpl.h.

625  {
626  if (_variableMap_.exists(varId)) {
627  // Reduce the variable child's CPT or Utility Table if necessary
628  for (const auto chi: dag_.children(varId))
629  if (isChanceNode(chi))
630  _potentialMap_[chi]->erase(variable(varId));
631  else if (isUtilityNode(chi))
632  _utilityMap_[chi]->erase(variable(varId));
633 
634  if (isChanceNode(varId)) {
635  delete _potentialMap_[varId];
636  _potentialMap_.erase(varId);
637  } else if (isUtilityNode(varId)) {
638  delete _utilityMap_[varId];
639  _utilityMap_.erase(varId);
640  }
641 
642  _variableMap_.erase(varId);
643  dag_.eraseNode(varId);
644  }
645  }
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 306 of file influenceDiagram.h.

306 { 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 653 of file influenceDiagram_tpl.h.

653  {
654  erase(_variableMap_.get(var));
655  }
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 692 of file influenceDiagram_tpl.h.

692  {
693  if (dag_.existsArc(arc)) {
694  NodeId head = arc.head(), tail = arc.tail();
695  dag_.eraseArc(arc);
696 
697  if (isChanceNode(head))
698  // Removes parent in the child's CPT
699  (*(_potentialMap_[head])) >> variable(tail);
700  else if (isUtilityNode(head))
701  // Removes parent in the child's UT
702  (*(_utilityMap_[head])) >> variable(tail);
703  }
704  }
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 712 of file influenceDiagram_tpl.h.

712  {
713  eraseArc(Arc(tail, head));
714  }
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 364 of file influenceDiagram.h.

364  {
365  eraseArc(idFromName(tail), idFromName(head));
366  }
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 775 of file influenceDiagram_tpl.h.

775  {
776  List< NodeId > nodeFIFO;
777  // mark[node] contains 0 if not visited
778  // mark[node] = predecessor if visited
779  NodeProperty< int > mark = dag_.nodesProperty((int)-1);
780  NodeId current;
781 
782  mark[src] = (int)src;
783  nodeFIFO.pushBack(src);
784 
785  while (!nodeFIFO.empty()) {
786  current = nodeFIFO.front();
787  nodeFIFO.popFront();
788 
789  for (const auto new_one: dag_.children(current)) {
790  if (mark[new_one] != -1) continue; // if this node is already marked, continue
791 
792  mark[new_one] = (int)current;
793 
794  if (new_one == dest) break; // if we reach *orderIter, stop.
795 
796  nodeFIFO.pushBack(new_one);
797  }
798  }
799 
800  if (mark[dest] == -1) return false;
801 
802  return true;
803  }
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 395 of file influenceDiagram.h.

395  {
396  return existsPathBetween(idFromName(src), idFromName(dest));
397  }
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 163 of file influenceDiagram_tpl.h.

163  {
165 
166  for (const auto& chaine: split(dotlike, ";")) {
167  NodeId lastId = 0;
168  bool notfirst = false;
169  for (const auto& souschaine: split(chaine, "->")) {
170  bool forward = true;
171  for (const auto& node: split(souschaine, "<-")) {
172  auto idVar = build_node_for_ID(infdiag, node, domainSize);
173  if (notfirst) {
174  if (forward) {
175  infdiag.addArc(lastId, idVar);
176  forward = false;
177  } else {
178  infdiag.addArc(idVar, lastId);
179  }
180  } else {
181  notfirst = true;
182  forward = false;
183  }
184  lastId = idVar;
185  }
186  }
187  }
188 
189  for (const auto n: infdiag.nodes()) {
190  if (infdiag.isChanceNode(n))
191  infdiag.cpt(n).randomCPT();
192  else if (infdiag.isUtilityNode(n)) {
193  infdiag.utility(n).random().scale(50).translate(-10);
194  }
195  }
196 
197  infdiag.setProperty("name", "fastPrototype");
198  return infdiag;
199  }
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 832 of file influenceDiagram_tpl.h.

832  {
833  Sequence< NodeId > childrenSeq;
834 
835  List< NodeId > nodeFIFO;
836  NodeId current;
837 
838  // mark[node] contains false if not visited
839  // mark[node] contains true if visited
840  NodeProperty< bool > mark = dag_.nodesProperty(false);
841 
842  mark[parentDecision] = true;
843 
844  nodeFIFO.pushBack(parentDecision);
845 
846  while (!nodeFIFO.empty()) {
847  current = nodeFIFO.front();
848  nodeFIFO.popFront();
849 
850  for (const auto new_one: dag_.children(current)) {
851  if (mark[new_one]) continue; // if this node is already marked, continue
852 
853  mark[new_one] = true;
854 
855  if (!isDecisionNode(new_one))
856  nodeFIFO.pushBack(new_one);
857  else
858  childrenSeq.insert(new_one);
859  }
860  }
861 
862  return childrenSeq;
863  }
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 809 of file influenceDiagram_tpl.h.

809  {
810  auto temporalGraph = new gum::DAG();
811 
812  for (const auto node: dag_.nodes()) {
813  if (isDecisionNode(node)) {
814  if (!temporalGraph->existsNode(node)) temporalGraph->addNodeWithId(node);
815 
816  for (const auto chi: getChildrenDecision_(node)) {
817  if (!temporalGraph->existsNode(chi)) temporalGraph->addNodeWithId(chi);
818 
819  temporalGraph->addArc(node, chi);
820  }
821  }
822  }
823 
824  return temporalGraph;
825  }
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 886 of file influenceDiagram_tpl.h.

886  {
887  if (clear) {
888  _temporalOrder_.clear();
889 
890  std::vector< NodeId > order = decisionOrder();
891  NodeSet nodeList = dag_.asNodeSet();
892 
893  for (auto i: order) {
894  NodeSet partialOrderedSet;
895 
896  for (const auto par: dag_.parents(i)) {
897  if (nodeList.contains(par) && isChanceNode(par)) {
898  partialOrderedSet.insert(par);
899  nodeList.erase(par);
900  }
901  }
902 
903  if (!partialOrderedSet.empty()) _temporalOrder_.pushFront(partialOrderedSet);
904 
905  NodeSet decisionSet;
906 
907  decisionSet.insert(i);
908 
909  _temporalOrder_.pushFront(decisionSet);
910  }
911 
912  NodeSet lastSet; //= new gum::NodeSet();
913 
914  for (const auto node: nodeList)
915  if (isChanceNode(node)) lastSet.insert(node);
916 
917  if (!lastSet.empty()) _temporalOrder_.pushFront(lastSet);
918  }
919 
920  return _temporalOrder_;
921  }
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 483 of file influenceDiagram_tpl.h.

483  {
484  return _variableMap_.idFromName(name);
485  }
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 428 of file influenceDiagram_tpl.h.

428  {
429  return _potentialMap_.exists(varId);
430  }
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 416 of file influenceDiagram_tpl.h.

416  {
417  bool ret = true;
418 
419  if (isUtilityNode(varId) || isChanceNode(varId)) ret = false;
420 
421  return ret;
422  }
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 408 of file influenceDiagram_tpl.h.

408  {
409  return _utilityMap_.exists(varId);
410  }
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 724 of file influenceDiagram_tpl.h.

724  {
725  for (const auto node: dag_.nodes())
726  if (!isUtilityNode(node)) graph.addNodeWithId(node);
727 
728  for (const auto node: dag_.nodes()) {
729  if (!isDecisionNode(node))
730  for (const auto par: dag_.parents(node)) {
731  if (isChanceNode(node)) graph.addEdge(node, par);
732 
733  for (const auto par2: dag_.parents(node))
734  if (par != par2) graph.addEdge(par, par2);
735  }
736  }
737  }
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 477 of file influenceDiagram_tpl.h.

477  {
478  return _variableMap_.get(var);
479  }
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 235 of file influenceDiagram_tpl.h.

235  {
236  if (this != &source) {
237  clear();
238  // Copying tables and structure
239  copyStructureAndTables_(source);
240  }
241 
242  return *this;
243  }
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 259 of file influenceDiagram_tpl.h.

259  {
260  for (const auto node: dag_.nodes()) {
261  if (isChanceNode(node))
262  delete &cpt(node);
263  else if (isUtilityNode(node))
264  delete &utility(node);
265  }
266  }
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 310 of file influenceDiagram_tpl.h.

310  {
311  std::stringstream output;
312  std::stringstream decisionNode;
313  std::stringstream utilityNode;
314  std::stringstream chanceNode;
315  std::stringstream arcstream;
316  output << "digraph \"";
317 
318  try {
319  output << this->property("name") << "\" {" << std::endl;
320  } catch (NotFound&) { output << "no_name\" {" << std::endl; }
321 
322  output << " node [bgcolor=\"#AAAAAA\", style=filled, height=0];" << std::endl;
323 
324  decisionNode << "node [shape = box];" << std::endl;
325 
326  utilityNode << "node [shape = hexagon, margin=0];" << std::endl;
327  chanceNode << "node [shape = ellipse];" << std::endl;
328  std::string tab = " ";
329 
330  for (const auto node: dag_.nodes()) {
331  if (isChanceNode(node))
332  chanceNode << tab << "\"" << node << "-" << variable(node).name() << "\""
333  << ";";
334  else if (isUtilityNode(node))
335  utilityNode << tab << "\"" << node << "-" << variable(node).name() << "\""
336  << ";";
337  else
338  decisionNode << tab << "\"" << node << "-" << variable(node).name() << "\""
339  << ";";
340 
341  if (dag_.children(node).size() > 0)
342  for (const auto chi: dag_.children(node)) {
343  arcstream << "\"" << node << "-" << variable(node).name() << "\""
344  << " -> "
345  << "\"" << chi << "-" << variable(chi).name() << "\"";
346  if (isDecisionNode(chi)) { arcstream << " [style=\"tapered, bold\"]"; }
347  arcstream << ";" << std::endl;
348  }
349  }
350 
351  output << decisionNode.str() << std::endl
352  << utilityNode.str() << std::endl
353  << chanceNode.str() << std::endl
354  << std::endl
355  << arcstream.str() << std::endl
356  << "}" << std::endl;
357 
358  return output.str();
359  }
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 362 of file influenceDiagram_tpl.h.

362  {
363  std::stringstream output;
364 
365  output << "Influence Diagram{" << std::endl;
366  output << " chance: " << chanceNodeSize() << "," << std::endl;
367  output << " utility: " << utilityNodeSize() << "," << std::endl;
368  output << " decision: " << decisionNodeSize() << "," << std::endl;
369  output << " arcs: " << dag().sizeArcs() << "," << std::endl;
370 
371  double dSize = log10DomainSize();
372 
373  if (dSize > 6)
374  output << " domainSize: 10^" << dSize;
375  else
376  output << " domainSize: " << std::round(std::pow(10.0, dSize));
377 
378  output << std::endl << "}";
379 
380  return output.str();
381  }
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 400 of file influenceDiagram_tpl.h.

400  {
401  return *(_utilityMap_[varId]);
402  }
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 436 of file influenceDiagram_tpl.h.

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

◆ variable() [1/2]

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 469 of file influenceDiagram_tpl.h.

469  {
470  return _variableMap_[id];
471  }
VariableNodeMap _variableMap_
Mapping between id and variable.

◆ variable() [2/2]

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

Returns the CPT of a potential variable.

Exceptions
NotFoundIf no variable's id matches varId.

Definition at line 185 of file influenceDiagram.h.

185  {
186  return variable(idFromName(name));
187  };
NodeId idFromName(const std::string &name) const final
Getter by name.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variable given it&#39;s node id.

◆ 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 490 of file influenceDiagram_tpl.h.

490  {
491  return _variableMap_.variableFromName(name);
492  }
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 461 of file influenceDiagram_tpl.h.

461  {
462  return _variableMap_;
463  }
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 436 of file influenceDiagram.h.

◆ _temporalOrder_

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

The temporal order.

Definition at line 441 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 438 of file influenceDiagram.h.

◆ _variableMap_

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

Mapping between id and variable.

Definition at line 433 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: