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

Class representing a Bayesian Network. More...

#include <agrum/BN/BayesNet.h>

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

Public Member Functions

NodeId addNoisyAND (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a noisyAND implementation. More...
 
NodeId addNoisyAND (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, its associate node and a noisyAND implementation. More...
 
NodeId addLogit (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a Logit implementation. More...
 
NodeId addLogit (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, its associate node and a Logit implementation. More...
 
NodeId addOR (const DiscreteVariable &var)
 Add a variable, it's associate node and an OR implementation. More...
 
NodeId addAND (const DiscreteVariable &var)
 Add a variable, it's associate node and an AND implementation. More...
 
void addWeightedArc (NodeId tail, NodeId head, GUM_SCALAR causalWeight)
 Add an arc in the BN, and update arc.head's CPT. More...
 
void addWeightedArc (const std::string &tail, const std::string &head, GUM_SCALAR causalWeight)
 Add an arc in the BN, and update arc.head's CPT. More...
 
void generateCPTs () const
 randomly generates CPTs for a given structure More...
 
void generateCPT (NodeId node) const
 randomly generate CPT for a given node in a given structure More...
 
void generateCPT (const std::string &name) const
 
void changePotential (NodeId id, Potential< GUM_SCALAR > *newPot)
 change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId. More...
 
void changePotential (const std::string &name, Potential< GUM_SCALAR > *newPot)
 
bool operator== (const IBayesNet< GUM_SCALAR > &from) const
 This operator compares 2 BNs ! More...
 
bool operator!= (const IBayesNet< GUM_SCALAR > &from) const
 
Size dim () const
 Returns the dimension (the number of free parameters) in this bayes net. More...
 
Size maxVarDomainSize () const
 
GUM_SCALAR minParam () const
 
GUM_SCALAR maxParam () const
 
GUM_SCALAR minNonZeroParam () const
 
GUM_SCALAR maxNonOneParam () const
 
virtual std::string toDot () const
 
std::string toString () const
 
NodeSet minimalCondSet (NodeId target, const NodeSet &soids) const
 
NodeSet minimalCondSet (const NodeSet &targets, const NodeSet &soids) const
 
bool hasSameStructure (const DAGmodel &other)
 
double log10DomainSize () const
 
Constructors and Destructor
 BayesNet ()
 Default constructor. More...
 
 BayesNet (std::string name)
 Default constructor. More...
 
virtual ~BayesNet () final
 Destructor. More...
 
 BayesNet (const BayesNet< GUM_SCALAR > &source)
 Copy constructor. More...
 
Operators
BayesNet< GUM_SCALAR > & operator= (const BayesNet< GUM_SCALAR > &source)
 Copy operator. More...
 
Variable manipulation methods
const Potential< GUM_SCALAR > & cpt (NodeId varId) const final
 Returns the CPT of a variable. More...
 
const Potential< GUM_SCALAR > & cpt (const std::string &name) const
 Returns the CPT of a variable. More...
 
const VariableNodeMapvariableNodeMap () const final
 Returns a map between variables and nodes of this gum::BayesNet. More...
 
NodeId add (const DiscreteVariable &var)
 Add a variable to the gum::BayesNet. More...
 
NodeId add (const std::string &name, unsigned int nbrmod)
 Shortcut for add(gum::LabelizedVariable(name,name,nbrmod)) More...
 
NodeId add (const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent)
 Add a variable to the gum::BayesNet. More...
 
NodeId add (const DiscreteVariable &var, NodeId id)
 Add a variable to the gum::BayesNet. More...
 
NodeId add (const DiscreteVariable &var, MultiDimImplementation< GUM_SCALAR > *aContent, NodeId id)
 Add a variable to the gum::BayesNet. More...
 
void clear ()
 clear the whole Bayes net * More...
 
void erase (NodeId varId)
 Remove a variable from the gum::BayesNet. More...
 
void erase (const std::string &name)
 Removes a variable from the gum::BayesNet. More...
 
void erase (const DiscreteVariable &var)
 Remove a variable from the gum::BayesNet. More...
 
const DiscreteVariablevariable (NodeId id) const final
 Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet. More...
 
const DiscreteVariablevariable (const std::string &name) const
 Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet. More...
 
void changeVariableName (NodeId id, const std::string &new_name)
 Changes a variable's name in the gum::BayesNet. More...
 
void changeVariableName (const std::string &name, const std::string &new_name)
 Changes a variable's name. More...
 
void changeVariableLabel (NodeId id, const std::string &old_label, const std::string &new_label)
 Changes a variable's label in the gum::BayesNet. More...
 
void changeVariableLabel (const std::string &name, const std::string &old_label, const std::string &new_label)
 Changes a variable's name. More...
 
NodeId nodeId (const DiscreteVariable &var) const final
 Returns a variable's id in the gum::BayesNet. More...
 
NodeId idFromName (const std::string &name) const final
 Returns a variable's id given its name in the gum::BayesNet. More...
 
const DiscreteVariablevariableFromName (const std::string &name) const final
 Returns a variable given its name in the gum::BayesNet. More...
 
Arc manipulation methods.
void addArc (NodeId tail, NodeId head)
 Add an arc in the BN, and update arc.head's CPT. More...
 
void addArc (const std::string &tail, const std::string &head)
 Add an arc in the BN, and update arc.head's CPT. More...
 
void eraseArc (const Arc &arc)
 Removes an arc in the BN, and update head's CTP. More...
 
void eraseArc (NodeId tail, NodeId head)
 Removes an arc in the BN, and update head's CTP. More...
 
void eraseArc (const std::string &tail, const std::string &head)
 Removes an arc in the BN, and update head's CTP. More...
 
void beginTopologyTransformation ()
 When inserting/removing arcs, node CPTs change their dimension with a cost in time. More...
 
void endTopologyTransformation ()
 terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions. More...
 
void reverseArc (NodeId tail, NodeId head)
 Reverses an arc while preserving the same joint distribution. More...
 
void reverseArc (const std::string &tail, const std::string &head)
 Reverses an arc while preserving the same joint distribution. More...
 
void reverseArc (const Arc &arc)
 Reverses an arc while preserving the same joint distribution. More...
 
Accessors for nodes with CI or logical implementation
NodeId addNoisyOR (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, it's associate node and a gum::noisyOR implementation. More...
 
NodeId addNoisyORNet (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, it's associate node and a gum::noisyOR implementation. More...
 
NodeId addNoisyORCompound (const DiscreteVariable &var, GUM_SCALAR external_weight)
 Add a variable, it's associate node and a gum::noisyOR implementation. More...
 
NodeId addNoisyOR (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a noisyOR implementation. More...
 
NodeId addNoisyORNet (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a noisyOR implementation. More...
 
NodeId addNoisyORCompound (const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
 Add a variable, its associate node and a noisyOR implementation. More...
 
NodeId addAMPLITUDE (const DiscreteVariable &var)
 Others aggregators. More...
 
NodeId addCOUNT (const DiscreteVariable &var, Idx value=1)
 Others aggregators. More...
 
NodeId addEXISTS (const DiscreteVariable &var, Idx value=1)
 Others aggregators. More...
 
NodeId addFORALL (const DiscreteVariable &var, Idx value=1)
 Others aggregators. More...
 
NodeId addMAX (const DiscreteVariable &var)
 Others aggregators. More...
 
NodeId addMEDIAN (const DiscreteVariable &var)
 Others aggregators. More...
 
NodeId addMIN (const DiscreteVariable &var)
 Others aggregators. More...
 
NodeId addSUM (const DiscreteVariable &var)
 Others aggregators. More...
 
Joint Probability manipulation methods
GUM_SCALAR jointProbability (const Instantiation &i) const
 Compute a parameter of the joint probability for the BN (given an instantiation of the vars) More...
 
GUM_SCALAR log2JointProbability (const Instantiation &i) const
 Compute a parameter of the log joint probability for the BN (given an instantiation of the vars) 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
 Returns a constant reference to the dag of this Bayes Net. 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...
 
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 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
 check if X and Y are independent given Z More...
 
bool isIndependent (const std::string &Xname, const std::string &Yname, const std::vector< std::string > &Zanmes) 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...
 
Variable manipulation methods.
virtual bool empty () const
 Retursn true if this Directed Graphical Model is empty. More...
 
Instantiation completeInstantiation () const
 Get an instantiation over all the variables of the model. More...
 

Static Public Member Functions

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

Protected Attributes

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

Friends

class BayesNetFactory< GUM_SCALAR >
 

Detailed Description

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

Class representing a Bayesian Network.

Bayesian Networks are a probabilistic graphical model in which nodes are random variables and the probability distribution is defined by the product:

\(P(X_1, \ldots, X_n) = \prod_{i=1}^{n} P(X_i | \pi(X_i))\),

where \(\pi(X_i)\) is the parent of \(X_i\).

The probability distribution can be represented as a directed acyclic graph (DAG) where:

  • Nodes are discrete random variables.
  • An arc A -> B represent a dependency between variables A and B, i.e. B conditional probability distribution is defined as \(P(B| \pi(B)\).

After a variable is added to the BN, it's domain cannot change. But it arcs are added, the data in its CPT are lost.

You should look a the gum::BayesNetFactory class which can help build Bayesian Networks.

You can print a BayesNet using gum::operator<<(std::ostream&, const BayesNet<GUM_SCALAR>&).

Definition at line 78 of file BayesNet.h.

Constructor & Destructor Documentation

◆ BayesNet() [1/3]

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

Default constructor.

Definition at line 172 of file BayesNet_tpl.h.

172  : IBayesNet< GUM_SCALAR >() {
173  GUM_CONSTRUCTOR(BayesNet);
174  }
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:172

◆ BayesNet() [2/3]

template<typename GUM_SCALAR >
INLINE gum::BayesNet< GUM_SCALAR >::BayesNet ( std::string  name)
explicit

Default constructor.

Parameters
nameThe BayesNet's name.

Definition at line 177 of file BayesNet_tpl.h.

177  :
178  IBayesNet< GUM_SCALAR >(name) {
179  GUM_CONSTRUCTOR(BayesNet);
180  }
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:172

◆ ~BayesNet()

template<typename GUM_SCALAR >
gum::BayesNet< GUM_SCALAR >::~BayesNet ( )
finalvirtual

Destructor.

Definition at line 205 of file BayesNet_tpl.h.

205  {
206  GUM_DESTRUCTOR(BayesNet);
207  for (const auto p: probaMap__) {
208  delete p.second;
209  }
210  }
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:172
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671

◆ BayesNet() [3/3]

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

Copy constructor.

Definition at line 183 of file BayesNet_tpl.h.

183  :
184  IBayesNet< GUM_SCALAR >(source), varMap__(source.varMap__) {
185  GUM_CONS_CPY(BayesNet);
186 
187  copyPotentials__(source);
188  }
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:172
void copyPotentials__(const BayesNet< GUM_SCALAR > &source)
copy of potentials from a BN to another, using names of vars as ref.
Definition: BayesNet_tpl.h:664
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668

Member Function Documentation

◆ add() [1/5]

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable var)

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Potential.

The variable is added by copy to the gum::BayesNet. The variable's gum::Potential implementation will be a gum::MultiDimArray.

Parameters
varThe variable added by copy.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 244 of file BayesNet_tpl.h.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), gum::learning::genericBNLearner::Database::BNVars__(), gum::build_node(), gum::BayesNet< double >::cpt(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

244  {
245  auto ptr = new MultiDimArray< GUM_SCALAR >();
246  NodeId res = 0;
247 
248  try {
249  res = add(var, ptr);
250 
251  } catch (Exception&) {
252  delete ptr;
253  throw;
254  }
255 
256  return res;
257  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
Size NodeId
Type for node ids.
Definition: graphElements.h:98
+ Here is the caller graph for this function:

◆ add() [2/5]

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const std::string &  name,
unsigned int  nbrmod 
)

Shortcut for add(gum::LabelizedVariable(name,name,nbrmod))

Add a gum::LabelizedVariable to the gum::BayesNet

This method is just a shortcut for a often used pattern

Exceptions
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.
NotAllowedif nbrmod<2

Definition at line 260 of file BayesNet_tpl.h.

261  {
262  if (nbrmod < 2) {
263  GUM_ERROR(OperationNotAllowed,
264  "Variable " << name << "needs more than " << nbrmod
265  << " modalities");
266  }
267 
268  RangeVariable v(name, name, 0, nbrmod - 1);
269  return add(v);
270  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ add() [3/5]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable var,
MultiDimImplementation< GUM_SCALAR > *  aContent 
)

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Potential.

The variable is added by copy to the gum::BayesNet.

Parameters
varThe variable added by copy.
aContentThe gum::MultiDimImplementation to use for this variable's gum::Potential implementation.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 273 of file BayesNet_tpl.h.

274  {
275  NodeId proposedId = dag().nextNodeId();
276  NodeId res = 0;
277 
278  res = add(var, aContent, proposedId);
279 
280  return res;
281  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
NodeId nextNodeId() const
returns a new node id, not yet used by any node
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
Size NodeId
Type for node ids.
Definition: graphElements.h:98

◆ add() [4/5]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable var,
NodeId  id 
)

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Potential.

The variable is added by copy to the gum::BayesNet. The variable's gum::Potential implementation will be a gum::MultiDimArray.

Parameters
varThe variable added by copy.
idThe variable's forced gum::NodeId in the gum::BayesNet.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateElementRaised id is already used.
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 284 of file BayesNet_tpl.h.

285  {
286  auto ptr = new MultiDimArray< GUM_SCALAR >();
287  NodeId res = 0;
288 
289  try {
290  res = add(var, ptr, id);
291 
292  } catch (Exception&) {
293  delete ptr;
294  throw;
295  }
296 
297  return res;
298  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
Size NodeId
Type for node ids.
Definition: graphElements.h:98

◆ add() [5/5]

template<typename GUM_SCALAR>
NodeId gum::BayesNet< GUM_SCALAR >::add ( const DiscreteVariable var,
MultiDimImplementation< GUM_SCALAR > *  aContent,
NodeId  id 
)

Add a variable to the gum::BayesNet.

Add a gum::DiscreteVariable, it's associated gum::NodeId and it's gum::Potential.

Parameters
varThe variable added by copy.
aContentThe gum::MultiDimImplementation to use for this variable's gum::Potential implementation.
idThe variable's forced gum::NodeId in the gum::BayesNet.
Returns
Returns the variable's id in the gum::BayesNet.
Exceptions
DuplicateElementRaised id is already used.
DuplicateLabelRaised if variable.name() is already used in this gum::BayesNet.

Definition at line 302 of file BayesNet_tpl.h.

304  {
305  varMap__.insert(id, var);
306  this->dag_.addNodeWithId(id);
307 
308  auto cpt = new Potential< GUM_SCALAR >(aContent);
309  (*cpt) << variable(id);
310  probaMap__.insert(id, cpt);
311  return id;
312  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:327
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:213

◆ addAMPLITUDE()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addAMPLITUDE ( const DiscreteVariable var)

Others aggregators.

Definition at line 483 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

483  {
484  return add(var, new aggregator::Amplitude< GUM_SCALAR >());
485  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addAND()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addAND ( const DiscreteVariable var)

Add a variable, it's associate node and an AND implementation.

The id of the new variable is automatically generated.

Warning
AND is implemented as a gum::aggregator::And which means that if parents are not boolean, all value>1 is True
Parameters
varThe variable added by copy.
Returns
the id of the added variable.
Exceptions
SizeErrorif variable.domainSize()>2

Definition at line 488 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

488  {
489  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an AND has to be boolean");
490 
491  return add(var, new aggregator::And< GUM_SCALAR >());
492  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ addArc() [1/2]

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

Add an arc in the BN, and update arc.head's CPT.

Parameters
headand
tailas NodeId
Exceptions
InvalidEdgeIf arc.tail and/or arc.head are not in the BN.
DuplicateElementif the arc already exists

Definition at line 370 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_(), gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), gum::BayesNet< double >::changeVariableLabel(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), gum::BayesNet< double >::fastPrototype(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

370  {
371  if (this->dag_.existsArc(tail, head)) {
372  GUM_ERROR(DuplicateElement,
373  "The arc (" << tail << "," << head << ") already exists.")
374  }
375 
376  this->dag_.addArc(tail, head);
377  // Add parent in the child's CPT
378  (*(probaMap__[head])) << variable(tail);
379  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:43
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:213
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ addArc() [2/2]

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

Add an arc in the BN, and update arc.head's CPT.

Exceptions
gum::DuplicateElementif the arc already exists

Definition at line 382 of file BayesNet_tpl.h.

383  {
384  try {
385  addArc(this->idFromName(tail), this->idFromName(head));
386  } catch (DuplicateElement) {
387  GUM_ERROR(DuplicateElement,
388  "The arc " << tail << "->" << head << " already exists.")
389  }
390  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:370
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ addCOUNT()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addCOUNT ( const DiscreteVariable var,
Idx  value = 1 
)

Others aggregators.

Definition at line 495 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

496  {
497  return add(var, new aggregator::Count< GUM_SCALAR >(value));
498  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addEXISTS()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addEXISTS ( const DiscreteVariable var,
Idx  value = 1 
)

Others aggregators.

Definition at line 501 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

502  {
503  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean");
504 
505  return add(var, new aggregator::Exists< GUM_SCALAR >(value));
506  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ addFORALL()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addFORALL ( const DiscreteVariable var,
Idx  value = 1 
)

Others aggregators.

Definition at line 509 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

510  {
511  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean");
512 
513  return add(var, new aggregator::Forall< GUM_SCALAR >(value));
514  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ addLogit() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addLogit ( const DiscreteVariable var,
GUM_SCALAR  external_weight,
NodeId  id 
)

Add a variable, its associate node and a Logit implementation.

Parameters
varThe variable added by copy
external_weightsee gum::MultiDimLogit
idproposed gum::nodeId for the variable
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.

Definition at line 591 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::reverseArc().

593  {
594  return add(var, new MultiDimLogit< GUM_SCALAR >(external_weight), id);
595  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addLogit() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addLogit ( const DiscreteVariable var,
GUM_SCALAR  external_weight 
)

Add a variable, its associate node and a Logit implementation.

The id of the new variable is automatically generated.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimLogit
Returns
the id of the added variable.

Definition at line 571 of file BayesNet_tpl.h.

572  {
573  return add(var, new MultiDimLogit< GUM_SCALAR >(external_weight));
574  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244

◆ addMAX()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addMAX ( const DiscreteVariable var)

Others aggregators.

Definition at line 517 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

517  {
518  return add(var, new aggregator::Max< GUM_SCALAR >());
519  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addMEDIAN()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addMEDIAN ( const DiscreteVariable var)

Others aggregators.

Definition at line 522 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

522  {
523  return add(var, new aggregator::Median< GUM_SCALAR >());
524  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addMIN()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addMIN ( const DiscreteVariable var)

Others aggregators.

Definition at line 527 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

527  {
528  return add(var, new aggregator::Min< GUM_SCALAR >());
529  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addNoisyAND() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyAND ( const DiscreteVariable var,
GUM_SCALAR  external_weight,
NodeId  id 
)

Add a variable, its associate node and a noisyAND implementation.

Parameters
varThe variable added by copy
external_weightsee gum::MultiDimNoisyAND
idproposed gum::nodeId for the variable
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.

Definition at line 584 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::reverseArc().

586  {
587  return add(var, new MultiDimNoisyAND< GUM_SCALAR >(external_weight), id);
588  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addNoisyAND() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyAND ( const DiscreteVariable var,
GUM_SCALAR  external_weight 
)

Add a variable, its associate node and a noisyAND implementation.

The id of the new variable is automatically generated.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyAND
Returns
the id of the added variable.

Definition at line 565 of file BayesNet_tpl.h.

566  {
567  return add(var, new MultiDimNoisyAND< GUM_SCALAR >(external_weight));
568  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244

◆ addNoisyOR() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyOR ( const DiscreteVariable var,
GUM_SCALAR  external_weight 
)

Add a variable, it's associate node and a gum::noisyOR implementation.

The id of the new variable is automatically generated. Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the gum::BayesNet::addNoisyOR as an alias for gum::BayesNet::addNoisyORCompound

Parameters
varThe variable added by copy.
external_weightsee ref gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
Returns
the id of the added variable.

Definition at line 547 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::reverseArc().

548  {
549  return addNoisyORCompound(var, external_weight);
550  }
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it&#39;s associate node and a gum::noisyOR implementation.
Definition: BayesNet_tpl.h:553
+ Here is the caller graph for this function:

◆ addNoisyOR() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyOR ( const DiscreteVariable var,
GUM_SCALAR  external_weight,
NodeId  id 
)

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyORNet, gum::MultiDimNoisyORCompound
idThe chosen id
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id is already used

Definition at line 577 of file BayesNet_tpl.h.

579  {
580  return addNoisyORCompound(var, external_weight, id);
581  }
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it&#39;s associate node and a gum::noisyOR implementation.
Definition: BayesNet_tpl.h:553

◆ addNoisyORCompound() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORCompound ( const DiscreteVariable var,
GUM_SCALAR  external_weight 
)

Add a variable, it's associate node and a gum::noisyOR implementation.

The id of the new variable is automatically generated. Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the gum::BayesNet::addNoisyOR as an alias for gum::BayesNet::addNoisyORCompound

Parameters
varThe variable added by copy.
external_weightsee ref gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
Returns
the id of the added variable.

Definition at line 553 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::reverseArc().

554  {
555  return add(var, new MultiDimNoisyORCompound< GUM_SCALAR >(external_weight));
556  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addNoisyORCompound() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORCompound ( const DiscreteVariable var,
GUM_SCALAR  external_weight,
NodeId  id 
)

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyORNet, gum::MultiDimNoisyORCompound
idThe chosen id
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id is already used

Definition at line 598 of file BayesNet_tpl.h.

599  {
600  return add(
601  var, new MultiDimNoisyORCompound< GUM_SCALAR >(external_weight), id);
602  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244

◆ addNoisyORNet() [1/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORNet ( const DiscreteVariable var,
GUM_SCALAR  external_weight 
)

Add a variable, it's associate node and a gum::noisyOR implementation.

The id of the new variable is automatically generated. Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the gum::BayesNet::addNoisyOR as an alias for gum::BayesNet::addNoisyORCompound

Parameters
varThe variable added by copy.
external_weightsee ref gum::MultiDimNoisyORNet,gum::MultiDimNoisyORCompound
Returns
the id of the added variable.

Definition at line 559 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::reverseArc().

560  {
561  return add(var, new MultiDimNoisyORNet< GUM_SCALAR >(external_weight));
562  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addNoisyORNet() [2/2]

template<typename GUM_SCALAR>
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addNoisyORNet ( const DiscreteVariable var,
GUM_SCALAR  external_weight,
NodeId  id 
)

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the 'classical' noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

Parameters
varThe variable added by copy.
external_weightsee gum::MultiDimNoisyORNet, gum::MultiDimNoisyORCompound
idThe chosen id
Warning
give an id should be reserved for rare and specific situations !!!
Returns
the id of the added variable.
Exceptions
DuplicateElementif id is already used

Definition at line 605 of file BayesNet_tpl.h.

607  {
608  return add(var, new MultiDimNoisyORNet< GUM_SCALAR >(external_weight), id);
609  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244

◆ addOR()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addOR ( const DiscreteVariable var)

Add a variable, it's associate node and an OR implementation.

The id of the new variable is automatically generated.

Warning
OR is implemented as a gum::aggregator::Or which means that if parents are not boolean, all value>1 is True
Parameters
varThe variable added by copy.
Returns
the id of the added variable.
Exceptions
SizeErrorif variable.domainSize()>2

Definition at line 532 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

532  {
533  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an OR has to be boolean");
534 
535  return add(var, new aggregator::Or< GUM_SCALAR >());
536  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ addSUM()

template<typename GUM_SCALAR >
INLINE NodeId gum::BayesNet< GUM_SCALAR >::addSUM ( const DiscreteVariable var)

Others aggregators.

Definition at line 539 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_(), and gum::BayesNet< double >::reverseArc().

539  {
540  return add(var, new aggregator::Sum< GUM_SCALAR >());
541  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:244
+ Here is the caller graph for this function:

◆ addWeightedArc() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::addWeightedArc ( NodeId  tail,
NodeId  head,
GUM_SCALAR  causalWeight 
)

Add an arc in the BN, and update arc.head's CPT.

Parameters
headand
tailas NodeId
causalWeightsee gum::MultiDimICIModel
Exceptions
InvalidArcIf arc.tail and/or arc.head are not in the BN.
InvalidArcIf variable in arc.head is not a NoisyOR variable.

Definition at line 612 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::addWeightedArc(), and gum::BayesNet< double >::reverseArc().

614  {
615  auto* CImodel =
616  dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(cpt(head).content());
617 
618  if (CImodel != 0) {
619  // or is OK
620  addArc(tail, head);
621 
622  CImodel->causalWeight(variable(tail), causalWeight);
623  } else {
624  GUM_ERROR(InvalidArc,
625  "Head variable (" << variable(head).name()
626  << ") is not a CIModel variable !");
627  }
628  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:370
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:327
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ addWeightedArc() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::addWeightedArc ( const std::string &  tail,
const std::string &  head,
GUM_SCALAR  causalWeight 
)
inline

Add an arc in the BN, and update arc.head's CPT.

Parameters
headand
tailas std::string
causalWeightsee gum::MultiDimICIModel
NotFoundif no node with sun names is found
Exceptions
InvalidArcIf arc.tail and/or arc.head are not in the BN.
InvalidArcIf variable in arc.head is not a NoisyOR variable.

Definition at line 637 of file BayesNet.h.

639  {
640  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
641  };
void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:612
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315

◆ ancestors() [1/2]

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 145 of file DAGmodel.cpp.

References gum::Set< Key, Alloc >::begin(), gum::Set< Key, Alloc >::contains(), gum::Set< Key, Alloc >::empty(), gum::Set< Key, Alloc >::erase(), gum::Set< Key, Alloc >::insert(), and gum::DAGmodel::parents().

Referenced by gum::DAGmodel::ancestors().

145  {
146  NodeSet res;
147  NodeSet tmp;
148  for (auto next: parents(id))
149  tmp.insert(next);
150 
151  while (!tmp.empty()) {
152  auto current = *(tmp.begin());
153  tmp.erase(current);
154  res.insert(current);
155  for (auto next: parents(current)) {
156  if (!tmp.contains(next) && !res.contains(next)) { tmp.insert(next); }
157  }
158  }
159  return res;
160  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:55
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ancestors() [2/2]

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 162 of file DAGmodel.cpp.

References gum::DAGmodel::ancestors(), and gum::DAGmodel::idFromName().

162  {
163  return ancestors(idFromName(name));
164  }
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
NodeSet ancestors(const NodeId id) const
returns the set of nodes with directed path ingoing to a given node
Definition: DAGmodel.cpp:145
+ 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 44 of file DAGmodel_inl.h.

References gum::ArcGraphPart::arcs(), and gum::DAGmodel::dag_.

Referenced by gum::EssentialGraph::buildEssentialGraph__(), gum::MarkovBlanket::hasSameStructure(), gum::DAGmodel::hasSameStructure(), gum::DAGmodel::moralGraph__(), and gum::BayesNetFragment< GUM_SCALAR >::toBN().

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

◆ beginTopologyTransformation()

template<typename GUM_SCALAR >
void gum::BayesNet< GUM_SCALAR >::beginTopologyTransformation ( )

When inserting/removing arcs, node CPTs change their dimension with a cost in time.

begin Multiple Change for all CPTs

These functions delay the CPTs change to be done just once at the end of a sequence of topology modification. begins a sequence of insertions/deletions of arcs without changing the dimensions of the CPTs.

Definition at line 639 of file BayesNet_tpl.h.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), and gum::BayesNet< double >::eraseArc().

639  {
640  for (const auto node: nodes())
641  probaMap__[node]->beginMultipleChanges();
642  }
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
+ Here is the caller graph for this function:

◆ changePotential() [1/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::changePotential ( NodeId  id,
Potential< GUM_SCALAR > *  newPot 
)

change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

Exceptions
NotAllowedif newPot has not the same signature as probaMap__[NodeId]

Definition at line 697 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::generateCPT().

698  {
699  if (cpt(id).nbrDim() != newPot->nbrDim()) {
700  GUM_ERROR(OperationNotAllowed,
701  "cannot exchange potentials with different "
702  "dimensions for variable with id "
703  << id);
704  }
705 
706  for (Idx i = 0; i < cpt(id).nbrDim(); i++) {
707  if (&cpt(id).variable(i) != &(newPot->variable(i))) {
708  GUM_ERROR(OperationNotAllowed,
709  "cannot exchange potentials because, for variable with id "
710  << id << ", dimension " << i << " differs. ");
711  }
712  }
713 
714  unsafeChangePotential_(id, newPot);
715  }
void unsafeChangePotential_(NodeId id, Potential< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
Definition: BayesNet_tpl.h:718
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:327
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ changePotential() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::changePotential ( const std::string &  name,
Potential< GUM_SCALAR > *  newPot 
)

Definition at line 725 of file BayesNet_tpl.h.

726  {
727  changePotential(idFromName(name), newPot);
728  }
void changePotential(NodeId id, Potential< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
Definition: BayesNet_tpl.h:697
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315

◆ changeVariableLabel() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNet< GUM_SCALAR >::changeVariableLabel ( NodeId  id,
const std::string &  old_label,
const std::string &  new_label 
)

Changes a variable's label in the gum::BayesNet.

This will change the gum::LabelizedVariable names in the gum::BayesNet.

Exceptions
DuplicateLabelRaised if new_label is already used in this gum::LabelizedVariable.
NotFoundRaised if no variable matches id or if the variable is not a LabelizedVariable

Definition at line 226 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::changeVariableLabel(), and gum::BayesNet< double >::changeVariableName().

227  {
228  if (variable(id).varType() != VarType::Labelized) {
229  GUM_ERROR(NotFound, "Variable " << id << " is not a LabelizedVariable.");
230  }
231  LabelizedVariable* var = dynamic_cast< LabelizedVariable* >(
232  const_cast< DiscreteVariable* >(&variable(id)));
233 
234  var->changeLabel(var->posLabel(old_label), new_label);
235  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ changeVariableLabel() [2/2]

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::changeVariableLabel ( const std::string &  name,
const std::string &  old_label,
const std::string &  new_label 
)
inline

Changes a variable's name.

Definition at line 352 of file BayesNet.h.

354  {
355  changeVariableLabel(idFromName(name), old_label, new_label);
356  }
void changeVariableLabel(NodeId id, const std::string &old_label, const std::string &new_label)
Changes a variable&#39;s label in the gum::BayesNet.
Definition: BayesNet_tpl.h:226
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315

◆ changeVariableName() [1/2]

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

Changes a variable's name in the gum::BayesNet.

This will change the gum::DiscreteVariable names in the gum::BayesNet.

Exceptions
DuplicateLabelRaised if newName is already used in this gum::BayesNet.
NotFoundRaised if no variable matches id.

Definition at line 220 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::changeVariableName(), and gum::BayesNet< double >::variable().

221  {
222  varMap__.changeName(id, new_name);
223  }
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
void changeName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable
+ Here is the caller graph for this function:

◆ changeVariableName() [2/2]

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

Changes a variable's name.

Definition at line 331 of file BayesNet.h.

331  {
332  changeVariableName(idFromName(name), new_name);
333  }
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:220
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315

◆ children() [1/2]

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 63 of file DAGmodel_inl.h.

References gum::ArcGraphPart::children(), and gum::DAGmodel::dag_.

Referenced by gum::MarkovBlanket::buildMarkovBlanket__(), gum::DAGmodel::descendants(), gum::MarkovBlanket::MarkovBlanket(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::prm::ClassBayesNet< GUM_SCALAR >::toDot().

63  {
64  return dag_.children(id);
65  }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:213
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ children() [2/2]

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::ArcGraphPart::children(), gum::DAGmodel::dag_, and gum::DAGmodel::idFromName().

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

◆ clear()

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

clear the whole Bayes net *

Definition at line 360 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::clearPotentials__(), and gum::BayesNet< double >::cpt().

360  {
361  if (!this->empty()) {
362  auto l = this->nodes();
363  for (const auto no: l) {
364  this->erase(no);
365  }
366  }
367  }
virtual bool empty() const
Retursn true if this Directed Graphical Model is empty.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:342
+ Here is the caller graph for this function:

◆ clearPotentials__()

template<typename GUM_SCALAR >
void gum::BayesNet< GUM_SCALAR >::clearPotentials__ ( )
private

clear all potentials

Definition at line 653 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::generateCPT().

653  {
654  // Removing previous potentials
655  for (const auto& elt: probaMap__) {
656  delete elt.second;
657  }
658 
659  probaMap__.clear();
660  }
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
+ Here is the caller graph for this function:

◆ completeInstantiation()

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

Get an instantiation over all the variables of the model.

Definition at line 85 of file graphicalModel_inl.h.

References gum::GraphicalModel::nodes(), and gum::GraphicalModel::variable().

85  {
86  Instantiation I;
87 
88  for (const auto node: nodes())
89  I << variable(node);
90 
91  return I;
92  }
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.
+ Here is the call graph for this function:

◆ copyPotentials__()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::copyPotentials__ ( const BayesNet< GUM_SCALAR > &  source)
private

copy of potentials from a BN to another, using names of vars as ref.

Definition at line 664 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::generateCPT().

665  {
666  // Copying potentials
667 
668  for (const auto src: source.probaMap__) {
669  // First we build the node's CPT
670  Potential< GUM_SCALAR >* copy_array = new Potential< GUM_SCALAR >();
671  copy_array->beginMultipleChanges();
672  for (gum::Idx i = 0; i < src.second->nbrDim(); i++) {
673  (*copy_array) << variableFromName(src.second->variable(i).name());
674  }
675  copy_array->endMultipleChanges();
676  copy_array->copyFrom(*(src.second));
677 
678  // We add the CPT to the CPT's hashmap
679  probaMap__.insert(src.first, copy_array);
680  }
681  }
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:321
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
Size Idx
Type for indexes.
Definition: types.h:53
+ Here is the caller graph for this function:

◆ cpt() [1/2]

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

Returns the CPT of a variable.

Parameters
varIdA variable's id in the gum::BayesNet.
Returns
The variable's CPT.
Exceptions
NotFoundIf no variable's id matches varId.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 327 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_(), gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), gum::build_node(), gum::BayesNet< double >::cpt(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), gum::SimpleCPTDisturber< GUM_SCALAR >::disturbAugmCPT(), gum::SimpleCPTDisturber< GUM_SCALAR >::disturbReducCPT(), gum::AggregatorDecomposition< GUM_SCALAR >::getDecomposedAggregator(), gum::BayesNetFragment< GUM_SCALAR >::toBN(), and gum::credal::CredalNet< GUM_SCALAR >::toString().

327  {
328  return *(probaMap__[varId]);
329  }
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
+ Here is the caller graph for this function:

◆ cpt() [2/2]

template<typename GUM_SCALAR>
const Potential< GUM_SCALAR >& gum::BayesNet< GUM_SCALAR >::cpt ( const std::string &  name) const
inline

Returns the CPT of a variable.

Definition at line 166 of file BayesNet.h.

166  {
167  return cpt(idFromName(name));
168  };
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:327
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315

◆ dag()

◆ descendants() [1/2]

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 124 of file DAGmodel.cpp.

References gum::Set< Key, Alloc >::begin(), gum::DAGmodel::children(), gum::Set< Key, Alloc >::contains(), gum::Set< Key, Alloc >::empty(), gum::Set< Key, Alloc >::erase(), and gum::Set< Key, Alloc >::insert().

Referenced by gum::DAGmodel::descendants().

124  {
125  NodeSet res;
126  NodeSet tmp;
127  for (auto next: children(id))
128  tmp.insert(next);
129 
130  while (!tmp.empty()) {
131  auto current = *(tmp.begin());
132  tmp.erase(current);
133  res.insert(current);
134  for (auto next: children(current)) {
135  if (!tmp.contains(next) && !res.contains(next)) { tmp.insert(next); }
136  }
137  }
138  return res;
139  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:63
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ descendants() [2/2]

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 141 of file DAGmodel.cpp.

References gum::DAGmodel::descendants(), and gum::DAGmodel::idFromName().

141  {
142  return descendants(idFromName(name));
143  }
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
NodeSet descendants(const NodeId id) const
returns the set of nodes with directed path outgoing from a given node
Definition: DAGmodel.cpp:124
+ Here is the call graph for this function:

◆ dim()

template<typename GUM_SCALAR >
Size gum::IBayesNet< GUM_SCALAR >::dim ( ) const
inherited

Returns the dimension (the number of free parameters) in this bayes net.

\( dim(G)=\sum_{i \in nodes} ((r_i-1)\cdot q_i) \) where \( r_i \) is the number of instantiations of node \( i \) and \( q_i \) is the number of instantiations of its parents.

Definition at line 79 of file IBayesNet_tpl.h.

79  {
80  Size dim = 0;
81 
82  for (auto node: nodes()) {
83  Size q = 1;
84 
85  for (auto parent: parents(node))
86  q *= variable(parent).domainSize();
87 
88  dim += (variable(node).domainSize() - 1) * q;
89  }
90 
91  return dim;
92  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:55
virtual Size domainSize() const =0
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
Definition: IBayesNet_tpl.h:79
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ empty()

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

Retursn true if this Directed Graphical Model is empty.

Definition at line 95 of file graphicalModel_inl.h.

References gum::GraphicalModel::size().

95 { return size() == 0; }
virtual Size size() const =0
Returns the number of variables in this Directed Graphical Model.
+ Here is the call graph for this function:

◆ endTopologyTransformation()

template<typename GUM_SCALAR >
void gum::BayesNet< GUM_SCALAR >::endTopologyTransformation ( )

terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.

end Multiple Change for all CPTs

Definition at line 646 of file BayesNet_tpl.h.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), and gum::BayesNet< double >::eraseArc().

646  {
647  for (const auto node: nodes())
648  probaMap__[node]->endMultipleChanges();
649  }
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
+ Here is the caller graph for this function:

◆ erase() [1/3]

template<typename GUM_SCALAR >
void gum::BayesNet< GUM_SCALAR >::erase ( NodeId  varId)

Remove a variable from the gum::BayesNet.

Removes the corresponding variable from the gum::BayesNet and from all of it's children gum::Potential.

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

Parameters
varIdThe variable's id to remove.

Definition at line 342 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::cpt(), and gum::BayesNet< double >::erase().

342  {
343  if (varMap__.exists(varId)) {
344  // Reduce the variable child's CPT
345  const NodeSet& children = this->children(varId);
346 
347  for (const auto c: children) {
348  probaMap__[c]->erase(variable(varId));
349  }
350 
351  delete probaMap__[varId];
352 
353  probaMap__.erase(varId);
354  varMap__.erase(varId);
355  this->dag_.eraseNode(varId);
356  }
357  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:63
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
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 > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:213
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
Definition: diGraph_inl.h:69
+ Here is the caller graph for this function:

◆ erase() [2/3]

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

Removes a variable from the gum::BayesNet.

Definition at line 283 of file BayesNet.h.

Referenced by gum::BayesNet< double >::erase().

283 { erase(idFromName(name)); };
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:342
+ Here is the caller graph for this function:

◆ erase() [3/3]

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

Remove a variable from the gum::BayesNet.

Removes the corresponding variable from the gum::BayesNet and from all of it's children gum::Potential.

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

Parameters
varA reference on the variable to remove.

Definition at line 337 of file BayesNet_tpl.h.

337  {
338  erase(varMap__.get(var));
339  }
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:342
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::BayesNet< GUM_SCALAR >::eraseArc ( const Arc arc)

Removes an arc in the BN, and update head's CTP.

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

Parameters
arcThe arc removed.

Definition at line 393 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::changeVariableLabel(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), and gum::BayesNet< double >::eraseArc().

393  {
394  if (varMap__.exists(arc.tail()) && varMap__.exists(arc.head())) {
395  NodeId head = arc.head(), tail = arc.tail();
396  this->dag_.eraseArc(arc);
397  // Remove parent froms child's CPT
398  (*(probaMap__[head])) >> variable(tail);
399  }
400  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
bool exists(NodeId id) const
Return true if id matches a node.
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:213
Size NodeId
Type for node ids.
Definition: graphElements.h:98
+ Here is the caller graph for this function:

◆ eraseArc() [2/3]

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

Removes an arc in the BN, and update head's CTP.

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

Parameters
headand
tailas NodeId

Definition at line 403 of file BayesNet_tpl.h.

403  {
404  eraseArc(Arc(tail, head));
405  }
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:393

◆ eraseArc() [3/3]

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

Removes an arc in the BN, and update head's CTP.

Definition at line 432 of file BayesNet.h.

432  {
433  eraseArc(idFromName(tail), idFromName(head));
434  }
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:393

◆ 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 46 of file DAGmodel_inl.h.

References gum::DAGmodel::dag_, and gum::ArcGraphPart::existsArc().

Referenced by gum::DAGmodel::existsArc(), gum::BayesNetFragment< GUM_SCALAR >::toDot(), and gum::BayesNetFragment< GUM_SCALAR >::whenArcDeleted().

46  {
47  return dag_.existsArc(tail, head);
48  }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:213
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
+ Here is the call graph for this function:
+ Here is the caller 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 50 of file DAGmodel_inl.h.

References gum::DAGmodel::existsArc(), and gum::DAGmodel::idFromName().

51  {
52  return existsArc(idFromName(nametail), idFromName(namehead));
53  }
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:46
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 >
BayesNet< GUM_SCALAR > gum::BayesNet< GUM_SCALAR >::fastPrototype ( const std::string &  dotlike,
Size  domainSize = 2 
)
static

Create a Bayesian network with a dot-like syntax which specifies:

  • the structure "a->b->c;b->d<-e;".
  • the type of the 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 variables
Returns
the resulting bayesian network

Definition at line 139 of file BayesNet_tpl.h.

140  {
142 
143 
144  for (const auto& chaine: split(dotlike, ";")) {
145  NodeId lastId = 0;
146  bool notfirst = false;
147  for (const auto& souschaine: split(chaine, "->")) {
148  bool forward = true;
149  for (const auto& node: split(souschaine, "<-")) {
150  auto idVar = build_node(bn, node, domainSize);
151  if (notfirst) {
152  if (forward) {
153  bn.addArc(lastId, idVar);
154  forward = false;
155  } else {
156  bn.addArc(idVar, lastId);
157  }
158  } else {
159  notfirst = true;
160  forward = false;
161  }
162  lastId = idVar;
163  }
164  }
165  }
166  bn.generateCPTs();
167  bn.setProperty("name", "fastPrototype");
168  return bn;
169  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:370
Class representing a Bayesian Network.
Definition: BayesNet.h:78
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, std::string node, gum::Size default_domain_size)
Definition: BayesNet_tpl.h:61
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.
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:684
Size NodeId
Type for node ids.
Definition: graphElements.h:98

◆ generateCPT() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNet< GUM_SCALAR >::generateCPT ( NodeId  node) const

randomly generate CPT for a given node in a given structure

Definition at line 690 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::addWeightedArc(), and gum::BayesNet< double >::generateCPT().

690  {
691  SimpleCPTGenerator< GUM_SCALAR > generator;
692 
693  generator.generateCPT(cpt(node).pos(variable(node)), cpt(node));
694  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:327
+ Here is the caller graph for this function:

◆ generateCPT() [2/2]

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

Definition at line 649 of file BayesNet.h.

649  {
650  generateCPT(idFromName(name));
651  };
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:690
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315

◆ generateCPTs()

template<typename GUM_SCALAR >
INLINE void gum::BayesNet< GUM_SCALAR >::generateCPTs ( ) const

randomly generates CPTs for a given structure

Definition at line 684 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::addWeightedArc(), and gum::BayesNet< double >::fastPrototype().

684  {
685  for (const auto node: nodes())
686  generateCPT(node);
687  }
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:690
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
+ Here is the caller graph for this function:

◆ 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 102 of file DAGmodel.cpp.

References gum::DAGmodel::arcs(), gum::Set< Key, Alloc >::exists(), gum::DAGmodel::idFromName(), gum::DAGmodel::nodes(), gum::DAGmodel::size(), gum::DAGmodel::sizeArcs(), and gum::DAGmodel::variable().

102  {
103  if (this == &other) return true;
104 
105  if (size() != other.size()) return false;
106 
107  if (sizeArcs() != other.sizeArcs()) return false;
108 
109  for (const auto& nid: nodes()) {
110  try {
111  other.idFromName(variable(nid).name());
112  } catch (NotFound) { return false; }
113  }
114 
115  for (const auto& arc: arcs()) {
116  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
117  other.idFromName(variable(arc.head()).name()))))
118  return false;
119  }
120 
121  return true;
122  }
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:44
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:42
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:39
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
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::BayesNet< GUM_SCALAR >::idFromName ( const std::string &  name) const
finalvirtual

Returns a variable's id given its name in the gum::BayesNet.

Parameters
nameThe variable's name from which the gum::NodeId is returned.
Returns
Returns the variable gum::NodeId in the gum::BayesNet.
Exceptions
NotFoundRaised if name does not match a variable in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 315 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_(), gum::BayesNet< double >::addWeightedArc(), gum::build_node(), gum::BayesNet< double >::changeVariableLabel(), gum::BayesNet< double >::changeVariableName(), gum::BayesNet< double >::cpt(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), gum::BayesNet< double >::erase(), gum::BayesNet< double >::eraseArc(), gum::BayesNet< double >::generateCPT(), gum::BayesNet< double >::idFromName(), gum::BayesNet< double >::reverseArc(), and gum::BayesNet< double >::variable().

315  {
316  return varMap__.idFromName(name);
317  }
NodeId idFromName(const std::string &name) const
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
+ Here is the caller graph for this function:

◆ isIndependent() [1/2]

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

check if X and Y are independent given Z

Definition at line 200 of file DAGmodel.cpp.

References gum::DAGmodel::moralizedAncestralGraph().

Referenced by gum::DAGmodel::isIndependent().

200  {
201  NodeSet cumul{Z};
202  cumul << X << Y;
203  auto g = moralizedAncestralGraph(cumul);
204  for (auto node: Z)
205  g.eraseNode(node);
206  return !g.hasUndirectedPath(X, Y);
207  }
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAGmodel.cpp:166
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isIndependent() [2/2]

bool gum::DAGmodel::isIndependent ( const std::string &  Xname,
const std::string &  Yname,
const std::vector< std::string > &  Zanmes 
) 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 209 of file DAGmodel.cpp.

References gum::DAGmodel::idFromName(), gum::Set< Key, Alloc >::insert(), and gum::DAGmodel::isIndependent().

211  {
212  NodeSet Z;
213  for (const auto& name: Znames)
214  Z.insert(idFromName(name));
215  return isIndependent(idFromName(Xname), idFromName(Yname), Z);
216  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const
check if X and Y are independent given Z
Definition: DAGmodel.cpp:200
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ Here is the call graph for this function:

◆ jointProbability()

template<typename GUM_SCALAR >
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::jointProbability ( const Instantiation i) const
inherited

Compute a parameter of the joint probability for the BN (given an instantiation of the vars)

Warning
a variable not present in the instantiation is assumed to be instantiated to 0.

Definition at line 211 of file IBayesNet_tpl.h.

211  {
212  auto value = (GUM_SCALAR)1.0;
213 
214  GUM_SCALAR tmp;
215 
216  for (auto node: nodes()) {
217  if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) { return (GUM_SCALAR)0; }
218 
219  value *= tmp;
220  }
221 
222  return value;
223  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70

◆ log10DomainSize()

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

Definition at line 74 of file graphicalModel_inl.h.

References gum::GraphicalModel::nodes(), and gum::GraphicalModel::variable().

Referenced by gum::InfluenceDiagram< GUM_SCALAR >::toString(), and gum::IMarkovNet< GUM_SCALAR >::toString().

74  {
75  double dSize = 0.0;
76 
77  for (const auto node: nodes()) {
78  dSize += std::log10(variable(node).domainSize());
79  }
80 
81  return dSize;
82  }
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.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log2JointProbability()

template<typename GUM_SCALAR >
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::log2JointProbability ( const Instantiation i) const
inherited

Compute a parameter of the log joint probability for the BN (given an instantiation of the vars)

Compute a parameter of the joint probability for the BN (given an instantiation of the vars)

Warning
a variable not present in the instantiation is assumed to be instantiated to 0.

Definition at line 230 of file IBayesNet_tpl.h.

230  {
231  auto value = (GUM_SCALAR)0.0;
232 
233  GUM_SCALAR tmp;
234 
235  for (auto node: nodes()) {
236  if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
237  return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
238  }
239 
240  value += log2(cpt(node)[i]);
241  }
242 
243  return value;
244  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70

◆ maxNonOneParam()

template<typename GUM_SCALAR >
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::maxNonOneParam ( ) const
inherited
Returns
the biggest value (not equal to 1) in the CPTs of *this
Warning
can return one if no other value in the CPTs than one....

Definition at line 135 of file IBayesNet_tpl.h.

135  {
136  GUM_SCALAR res = 0.0;
137  for (auto node: nodes()) {
138  auto v = cpt(node).maxNonOne();
139  if (v > res) { res = v; }
140  }
141  return res;
142  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70

◆ maxParam()

template<typename GUM_SCALAR >
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::maxParam ( ) const
inherited
Returns
the biggest value in the CPTs of *this

Definition at line 115 of file IBayesNet_tpl.h.

115  {
116  GUM_SCALAR res = 1.0;
117  for (auto node: nodes()) {
118  auto v = cpt(node).max();
119  if (v > res) { res = v; }
120  }
121  return res;
122  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70

◆ maxVarDomainSize()

template<typename GUM_SCALAR >
Size gum::IBayesNet< GUM_SCALAR >::maxVarDomainSize ( ) const
inherited
Returns
the biggest domainSize among the variables of *this

Definition at line 95 of file IBayesNet_tpl.h.

Referenced by gum::ImportanceSampling< GUM_SCALAR >::onContextualize_().

95  {
96  Size res = 0;
97  for (auto node: nodes()) {
98  auto v = variable(node).domainSize();
99  if (v > res) { res = v; }
100  }
101  return res;
102  }
virtual Size domainSize() const =0
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ Here is the caller graph for this function:

◆ minimalCondSet() [1/2]

template<typename GUM_SCALAR >
NodeSet gum::IBayesNet< GUM_SCALAR >::minimalCondSet ( NodeId  target,
const NodeSet soids 
) const
inherited

Definition at line 346 of file IBayesNet_tpl.h.

347  {
348  if (soids.contains(target)) return NodeSet({target});
349 
350  NodeSet res;
351  NodeSet alreadyVisitedUp;
352  NodeSet alreadyVisitedDn;
353  alreadyVisitedDn << target;
354  alreadyVisitedUp << target;
355 
356  for (auto fath: dag_.parents(target))
358  fath, soids, res, alreadyVisitedUp, alreadyVisitedDn);
359  for (auto chil: dag_.children(target))
361  chil, soids, res, alreadyVisitedUp, alreadyVisitedDn);
362  return res;
363  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
void minimalCondSetVisitDn__(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisitedUp, NodeSet &alreadyVisitedDn) const
const NodeSet & parents(const 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:213
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
void minimalCondSetVisitUp__(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisitedUp, NodeSet &alreadyVisitedDn) const

◆ minimalCondSet() [2/2]

template<typename GUM_SCALAR >
NodeSet gum::IBayesNet< GUM_SCALAR >::minimalCondSet ( const NodeSet targets,
const NodeSet soids 
) const
inherited

Definition at line 366 of file IBayesNet_tpl.h.

367  {
368  NodeSet res;
369  for (auto node: targets) {
370  res += minimalCondSet(node, soids);
371  }
372  return res;
373  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const

◆ minNonZeroParam()

template<typename GUM_SCALAR >
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::minNonZeroParam ( ) const
inherited
Returns
the smallest value (not equal to 0) in the CPTs of *this
Warning
can return 0 if no other value in the CPTs than 0...

Definition at line 125 of file IBayesNet_tpl.h.

Referenced by gum::ImportanceSampling< GUM_SCALAR >::onContextualize_().

125  {
126  GUM_SCALAR res = 1.0;
127  for (auto node: nodes()) {
128  auto v = cpt(node).minNonZero();
129  if (v < res) { res = v; }
130  }
131  return res;
132  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
+ Here is the caller graph for this function:

◆ minParam()

template<typename GUM_SCALAR >
GUM_SCALAR gum::IBayesNet< GUM_SCALAR >::minParam ( ) const
inherited
Returns
the smallest value in the CPTs of *this

Definition at line 105 of file IBayesNet_tpl.h.

105  {
106  GUM_SCALAR res = 1.0;
107  for (auto node: nodes()) {
108  auto v = cpt(node).min();
109  if (v < res) { res = v; }
110  }
111  return res;
112  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70

◆ 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 82 of file DAGmodel.cpp.

References gum::UndiGraph::clear(), gum::DAGmodel::moralGraph__(), and gum::DAGmodel::mutableMoralGraph__.

Referenced by gum::prm::SVED< GUM_SCALAR >::eliminateNodes__(), gum::prm::SVE< GUM_SCALAR >::eliminateNodes__(), gum::prm::SVED< GUM_SCALAR >::eliminateNodesWithEvidence__(), gum::prm::SVE< GUM_SCALAR >::eliminateNodesWithEvidence__(), gum::prm::SVED< GUM_SCALAR >::initLiftedNodes__(), and gum::prm::SVE< GUM_SCALAR >::initLiftedNodes__().

82  {
83  if (clear
84  || (mutableMoralGraph__ == nullptr)) { // we have to call moralGraph_
85  if (mutableMoralGraph__ == nullptr) {
86  mutableMoralGraph__ = new UndiGraph();
87  } else {
88  // clear is True ,__mutableMoralGraph exists
90  }
91 
92  moralGraph__();
93  }
94 
95  return *mutableMoralGraph__;
96  }
virtual void clear()
removes all the nodes and edges from the graph
Definition: undiGraph_inl.h:47
UndiGraph * mutableMoralGraph__
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:221
void moralGraph__() const
Returns the moral graph of this DAGModel.
Definition: DAGmodel.cpp:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ moralizedAncestralGraph() [1/2]

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 166 of file DAGmodel.cpp.

References gum::NodeGraphPart::addNodeWithId(), gum::Set< Key, Alloc >::erase(), gum::NodeGraphPart::exists(), and gum::DAGmodel::parents().

Referenced by gum::DAGmodel::isIndependent(), and gum::DAGmodel::moralizedAncestralGraph().

166  {
167  UndiGraph res;
168  NodeSet tmp{nodes};
169 
170  // findings all nodes
171  while (!tmp.empty()) {
172  auto current = *(tmp.begin());
173  tmp.erase(current);
174 
175  res.addNodeWithId(current);
176  for (auto next: parents(current))
177  if (!tmp.contains(next) && !res.exists(next)) tmp.insert(next);
178  }
179 
180  // finding all edges and moralizing
181  for (auto current: res)
182  for (auto father: parents(current)) {
183  res.addEdge(current,
184  father); // addEdge does not complain if edge already exists
185  for (auto other_father: parents(current))
186  if (other_father != father) res.addEdge(father, other_father);
187  }
188 
189  return res;
190  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:55
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ moralizedAncestralGraph() [2/2]

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 192 of file DAGmodel.cpp.

References gum::DAGmodel::idFromName(), gum::Set< Key, Alloc >::insert(), gum::DAGmodel::moralizedAncestralGraph(), and gum::DAGmodel::nodes().

193  {
194  NodeSet nodes;
195  for (const auto& name: nodenames)
196  nodes.insert(idFromName(name));
197  return moralizedAncestralGraph(nodes);
198  }
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition: DAGmodel.cpp:166
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
+ Here is the call graph for this function:

◆ nodeId()

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

Returns a variable's id in the gum::BayesNet.

Parameters
varThe variable from which the gum::NodeId is returned.
Returns
Returns the gum::DiscreteVariable gum::NodeId in the gum::BayesNet.
Exceptions
NotFoundIf var is not in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 239 of file BayesNet_tpl.h.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), and gum::BayesNet< double >::changeVariableLabel().

239  {
240  return varMap__.get(var);
241  }
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.
+ Here is the caller graph for this function:

◆ nodes()

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

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

Implements gum::GraphicalModel.

Definition at line 70 of file DAGmodel_inl.h.

References gum::DAGmodel::dag_.

Referenced by gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory(), gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), gum::build_node(), gum::EssentialGraph::buildEssentialGraph__(), gum::MarkovBlanket::buildMarkovBlanket__(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::disturbBN(), gum::Estimator< GUM_SCALAR >::Estimator(), gum::AggregatorDecomposition< GUM_SCALAR >::getDecomposedAggregator(), gum::getMaxModality(), gum::DAGmodel::hasSameStructure(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), gum::prm::ClassBayesNet< GUM_SCALAR >::modalities(), gum::DAGmodel::moralGraph__(), gum::DAGmodel::moralizedAncestralGraph(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::credal::CredalNet< GUM_SCALAR >::sort_varType__(), gum::BayesNetFragment< GUM_SCALAR >::toBN(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), gum::prm::ClassBayesNet< GUM_SCALAR >::toDot(), gum::credal::CredalNet< GUM_SCALAR >::toString(), gum::ImportanceSampling< GUM_SCALAR >::unsharpenBN_(), gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::verticesSampling__(), and gum::BayesNetFragment< GUM_SCALAR >::~BayesNetFragment().

70  {
71  return (NodeGraphPart&)dag_;
72  }
DAG dag_
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:213
+ Here is the caller graph for this function:

◆ operator!=()

template<typename GUM_SCALAR>
bool gum::IBayesNet< GUM_SCALAR >::operator!= ( const IBayesNet< GUM_SCALAR > &  from) const
inherited
Returns
Returns false if the src and this are equal.

Definition at line 294 of file IBayesNet_tpl.h.

294  {
295  return !this->operator==(from);
296  }
bool operator==(const IBayesNet< GUM_SCALAR > &from) const
This operator compares 2 BNs !

◆ operator=()

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

Copy operator.

Parameters
sourceThe copied BayesNet.
Returns
The copy of source.

Definition at line 192 of file BayesNet_tpl.h.

192  {
193  if (this != &source) {
195  varMap__ = source.varMap__;
196 
198  copyPotentials__(source);
199  }
200 
201  return *this;
202  }
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
Definition: IBayesNet_tpl.h:67
void copyPotentials__(const BayesNet< GUM_SCALAR > &source)
copy of potentials from a BN to another, using names of vars as ref.
Definition: BayesNet_tpl.h:664
void clearPotentials__()
clear all potentials
Definition: BayesNet_tpl.h:653
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668

◆ operator==()

template<typename GUM_SCALAR>
bool gum::IBayesNet< GUM_SCALAR >::operator== ( const IBayesNet< GUM_SCALAR > &  from) const
inherited

This operator compares 2 BNs !

Warning
To identify nodes between BNs, it is assumed that they share the same name.
Returns
true if the src and this are equal.

Definition at line 247 of file IBayesNet_tpl.h.

247  {
248  if (size() != from.size()) { return false; }
249 
250  if (sizeArcs() != from.sizeArcs()) { return false; }
251 
252  // alignment of variables between the 2 BNs
253  Bijection< const DiscreteVariable*, const DiscreteVariable* > alignment;
254 
255  for (auto node: nodes()) {
256  try {
257  alignment.insert(&variable(node),
258  &from.variableFromName(variable(node).name()));
259  } catch (NotFound&) {
260  // a name is not found in from
261  return false;
262  }
263  }
264 
265  for (auto node: nodes()) {
266  NodeId fromnode = from.idFromName(variable(node).name());
267 
268  if (cpt(node).nbrDim() != from.cpt(fromnode).nbrDim()) { return false; }
269 
270  if (cpt(node).domainSize() != from.cpt(fromnode).domainSize()) {
271  return false;
272  }
273 
274  Instantiation i(cpt(node));
275  Instantiation j(from.cpt(fromnode));
276 
277  for (i.setFirst(); !i.end(); i.inc()) {
278  for (Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
279  const DiscreteVariable* p = &(i.variable(indice));
280  j.chgVal(*(alignment.second(p)), i.val(*p));
281  }
282 
283  if (std::pow(cpt(node).get(i) - from.cpt(fromnode).get(j), (GUM_SCALAR)2)
284  > (GUM_SCALAR)1e-6) {
285  return false;
286  }
287  }
288  }
289 
290  return true;
291  }
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:42
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:39
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
Size NodeId
Type for node ids.
Definition: graphElements.h:98

◆ parents() [1/2]

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 55 of file DAGmodel_inl.h.

References gum::DAGmodel::dag_, and gum::ArcGraphPart::parents().

Referenced by gum::DAGmodel::ancestors(), gum::MarkovBlanket::buildMarkovBlanket__(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), gum::BayesNetFragment< GUM_SCALAR >::installCPT_(), gum::DAGmodel::moralGraph__(), gum::DAGmodel::moralizedAncestralGraph(), gum::DAGmodel::parents(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::prm::ClassBayesNet< GUM_SCALAR >::toDot().

55  {
56  return dag_.parents(id);
57  }
const NodeSet & parents(const 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:213
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parents() [2/2]

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 59 of file DAGmodel_inl.h.

References gum::DAGmodel::idFromName(), and gum::DAGmodel::parents().

59  {
60  return parents(idFromName(name));
61  }
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:55
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
+ 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 37 of file graphicalModel_inl.h.

References GUM_ERROR, and gum::GraphicalModel::properties__().

Referenced by gum::InfluenceDiagram< GUM_SCALAR >::toDot(), gum::IMarkovNet< GUM_SCALAR >::toDot(), and gum::IMarkovNet< GUM_SCALAR >::toDotAsFactorGraph().

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

◆ 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.

References gum::GraphicalModel::properties__().

Referenced by gum::build_node().

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...
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reverseArc() [1/3]

template<typename GUM_SCALAR >
INLINE void gum::BayesNet< GUM_SCALAR >::reverseArc ( NodeId  tail,
NodeId  head 
)

Reverses an arc while preserving the same joint distribution.

This method uses Shachter's 1986 algorithm for reversing an arc in the Bayes net while preserving the same joint distribution. By performing this reversal, we also add new arcs (required to not alter the joint distribution)

Exceptions
InvalidArcexception if the arc does not exist or if its reversal would induce a directed cycle.

Definition at line 474 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::eraseArc(), and gum::BayesNet< double >::reverseArc().

474  {
475  reverseArc(Arc(tail, head));
476  }
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:474
+ Here is the caller graph for this function:

◆ reverseArc() [2/3]

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

Reverses an arc while preserving the same joint distribution.

This method uses Shachter's 1986 algorithm for reversing an arc in the Bayes net while preserving the same joint distribution. By performing this reversal, we also add new arcs (required to not alter the joint distribution)

Exceptions
InvalidArcexception if the arc does not exist or if its reversal would induce a directed cycle.

Definition at line 465 of file BayesNet.h.

465  {
466  reverseArc(idFromName(tail), idFromName(head));
467  }
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:474

◆ reverseArc() [3/3]

template<typename GUM_SCALAR >
void gum::BayesNet< GUM_SCALAR >::reverseArc ( const Arc arc)

Reverses an arc while preserving the same joint distribution.

This method uses Shachter's 1986 algorithm for reversing an arc in the Bayes net while preserving the same joint distribution. By performing this reversal, we also add new arcs (required to not alter the joint distribution)

Exceptions
InvalidArcexception if the arc does not exist or if its reversal would induce a directed cycle.

Definition at line 408 of file BayesNet_tpl.h.

408  {
409  // check that the arc exsists
410  if (!varMap__.exists(arc.tail()) || !varMap__.exists(arc.head())
411  || !dag().existsArc(arc)) {
412  GUM_ERROR(InvalidArc, "a nonexisting arc cannot be reversed");
413  }
414 
415  NodeId tail = arc.tail(), head = arc.head();
416 
417  // check that the reversal does not induce a cycle
418  try {
419  DAG d = dag();
420  d.eraseArc(arc);
421  d.addArc(head, tail);
422  } catch (Exception&) {
423  GUM_ERROR(InvalidArc, "this arc reversal would induce a directed cycle");
424  }
425 
426  // with the same notations as Shachter (1986), "evaluating influence
427  // diagrams",
428  // p.878, we shall first compute the product of probabilities:
429  // pi_j^old (x_j | x_c^old(j) ) * pi_i^old (x_i | x_c^old(i) )
430  Potential< GUM_SCALAR > prod{cpt(tail) * cpt(head)};
431 
432  // modify the topology of the graph: add to tail all the parents of head
433  // and add to head all the parents of tail
435  NodeSet new_parents;
436  for (const auto node: this->parents(tail))
437  new_parents.insert(node);
438  for (const auto node: this->parents(head))
439  new_parents.insert(node);
440  // remove arc (head, tail)
441  eraseArc(arc);
442 
443  // add the necessary arcs to the tail
444  for (const auto p: new_parents) {
445  if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
446  }
447 
448  addArc(head, tail);
449  // add the necessary arcs to the head
450  new_parents.erase(tail);
451 
452  for (const auto p: new_parents) {
453  if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
454  }
455 
457 
458  // update the conditional distributions of head and tail
459  Set< const DiscreteVariable* > del_vars;
460  del_vars << &(variable(tail));
461  Potential< GUM_SCALAR > new_cpt_head =
462  prod.margSumOut(del_vars).putFirst(&variable(head));
463 
464  auto& cpt_head = const_cast< Potential< GUM_SCALAR >& >(cpt(head));
465  cpt_head = std::move(new_cpt_head);
466 
467  Potential< GUM_SCALAR > new_cpt_tail{
468  (prod / cpt_head).putFirst(&variable(tail))};
469  auto& cpt_tail = const_cast< Potential< GUM_SCALAR >& >(cpt(tail));
470  cpt_tail = std::move(new_cpt_tail);
471  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:370
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:55
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
bool exists(NodeId id) const
Return true if id matches a node.
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
Definition: DAGmodel_inl.h:46
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:639
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:327
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:646
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:393

◆ 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.

References gum::HashTable< Key, Val, Alloc >::insert(), and gum::GraphicalModel::properties__().

Referenced by gum::BayesNet< double >::fastPrototype(), and gum::IMarkovNet< GUM_SCALAR >::IMarkovNet().

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

◆ size()

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

Returns the number of variables in this Directed Graphical Model.

Implements gum::GraphicalModel.

Definition at line 39 of file DAGmodel_inl.h.

References gum::DAGmodel::dag(), and gum::NodeGraphPart::size().

Referenced by gum::InfluenceDiagram< GUM_SCALAR >::decisionNodeSize(), gum::MarkovBlanket::hasSameStructure(), gum::DAGmodel::hasSameStructure(), gum::credal::CredalNet< GUM_SCALAR >::initCNNets__(), gum::IBayesNet< double >::operator==(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::prm::ClassBayesNet< GUM_SCALAR >::toDot().

39 { 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:36
+ Here is the call graph for this function:
+ Here is the caller 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 42 of file DAGmodel_inl.h.

References gum::DAGmodel::dag_, and gum::ArcGraphPart::sizeArcs().

Referenced by gum::MarkovBlanket::hasSameStructure(), gum::DAGmodel::hasSameStructure(), and gum::IBayesNet< double >::operator==().

42 { 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:213
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toDot()

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

Reimplemented in gum::BayesNetFragment< GUM_SCALAR >, gum::prm::ClassBayesNet< GUM_SCALAR >, and gum::prm::InstanceBayesNet< GUM_SCALAR >.

Definition at line 166 of file IBayesNet_tpl.h.

166  {
167  std::stringstream output;
168  output << "digraph \"";
169 
170  std::string bn_name;
171 
172  try {
173  bn_name = this->property("name");
174  } catch (NotFound&) { bn_name = "no_name"; }
175 
176  output << bn_name << "\" {" << std::endl;
177  output << " graph [bgcolor=transparent,label=\"" << bn_name << "\"];"
178  << std::endl;
179  output << " node [style=filled fillcolor=\"#ffffaa\"];" << std::endl
180  << std::endl;
181 
182  for (auto node: nodes())
183  output << "\"" << variable(node).name() << "\" [comment=\"" << node << ":"
184  << variable(node).toStringWithDescription() << "\"];" << std::endl;
185 
186  output << std::endl;
187 
188  std::string tab = " ";
189 
190  for (auto node: nodes()) {
191  if (children(node).size() > 0) {
192  for (auto child: children(node)) {
193  output << tab << "\"" << variable(node).name() << "\" -> "
194  << "\"" << variable(child).name() << "\";" << std::endl;
195  }
196  } else if (parents(node).size() == 0) {
197  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
198  }
199  }
200 
201  output << "}" << std::endl;
202 
203  return output.str();
204  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:63
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:55
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:39
const std::string toStringWithDescription() const
string version of *this using description attribute instead of name.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
const std::string & name() const
returns the name of the variable
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.

◆ topologicalOrder()

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 98 of file DAGmodel.cpp.

References gum::DAGmodel::dag(), and gum::DiGraph::topologicalOrder().

Referenced by gum::EssentialGraph::buildEssentialGraph__(), gum::InfluenceDiagramGenerator< GUM_SCALAR >::checkTemporalOrder__(), gum::InfluenceDiagram< GUM_SCALAR >::decisionOrderExists(), and gum::InfluenceDiagram< GUM_SCALAR >::getDecisionOrder().

98  {
99  return this->dag().topologicalOrder(clear);
100  }
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:91
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toString()

template<typename GUM_SCALAR >
INLINE std::string gum::IBayesNet< GUM_SCALAR >::toString ( ) const
inherited
Returns
Returns a string representation of this IBayesNet.

Definition at line 145 of file IBayesNet_tpl.h.

Referenced by gum::operator<<().

145  {
146  Size param = 0;
147  double dSize = log10DomainSize();
148 
149  for (auto node: nodes())
150  param += cpt(node).content()->realSize();
151 
152  std::stringstream s;
153  s << "BN{nodes: " << size() << ", arcs: " << dag().sizeArcs() << ", ";
154 
155  if (dSize > 6)
156  s << "domainSize: 10^" << dSize;
157  else
158  s << "domainSize: " << std::round(std::pow(10.0, dSize));
159 
160  s << ", dim: " << param << "}";
161 
162  return s.str();
163  }
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:39
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
double log10DomainSize() const
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:70
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:36
+ Here is the caller graph for this function:

◆ unsafeChangePotential_()

template<typename GUM_SCALAR>
void gum::BayesNet< GUM_SCALAR >::unsafeChangePotential_ ( NodeId  id,
Potential< GUM_SCALAR > *  newPot 
)
private

change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

Warning
no verification of dimensions are performer
See also
changePotential

Definition at line 718 of file BayesNet_tpl.h.

719  {
720  delete probaMap__[id];
721  probaMap__[id] = newPot;
722  }
NodeProperty< Potential< GUM_SCALAR > *> probaMap__
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:671

◆ variable() [1/2]

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

Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.

Parameters
idThe variable's id to return.
Returns
Returns a constant reference of the gum::DiscreteVariable corresponding to id in the gum::BayesNet.
Exceptions
NotFoundRaised if id does not match a a variable in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 214 of file BayesNet_tpl.h.

Referenced by gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_(), gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory(), gum::credal::CredalNet< GUM_SCALAR >::bnCopy__(), gum::build_node(), gum::learning::genericBNLearner::Database::Database(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), gum::BayesNet< double >::erase(), gum::getMaxModality(), gum::credal::CredalNet< GUM_SCALAR >::toString(), and gum::BayesNet< double >::variable().

214  {
215  return varMap__.get(id);
216  }
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.
+ Here is the caller graph for this function:

◆ variable() [2/2]

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

Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.

Definition at line 313 of file BayesNet.h.

313  {
314  return variable(idFromName(name));
315  };
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:214
NodeId idFromName(const std::string &name) const final
Returns a variable&#39;s id given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:315

◆ variableFromName()

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

Returns a variable given its name in the gum::BayesNet.

Parameters
nameThe variable's name in the gum::BayesNet.
Returns
Returns the gum::DiscreteVariable named name in the gum::BayesNet.
Exceptions
NotFoundRaised if name does not match a variable in the gum::BayesNet.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 321 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::changeVariableLabel(), and gum::BayesNet< double >::variableFromName().

321  {
322  return varMap__.variableFromName(name);
323  }
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
const DiscreteVariable & variableFromName(const std::string &name) const
+ Here is the caller graph for this function:

◆ variableNodeMap()

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

Returns a map between variables and nodes of this gum::BayesNet.

Returns
Returns a constant reference to the gum::VariableNodeMap.

Implements gum::IBayesNet< GUM_SCALAR >.

Definition at line 332 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::cpt(), and gum::learning::DAG2BNLearner< ALLOC >::createBN().

332  {
333  return varMap__;
334  }
VariableNodeMap varMap__
the map between variable and id
Definition: BayesNet.h:668
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ BayesNetFactory< GUM_SCALAR >

template<typename GUM_SCALAR>
friend class BayesNetFactory< GUM_SCALAR >
friend

Definition at line 79 of file BayesNet.h.

Member Data Documentation

◆ dag_

◆ probaMap__

template<typename GUM_SCALAR>
NodeProperty< Potential< GUM_SCALAR >* > gum::BayesNet< GUM_SCALAR >::probaMap__
private

Mapping between the variable's id and their CPT.

Definition at line 671 of file BayesNet.h.

Referenced by gum::BayesNet< double >::copyPotentials__().

◆ varMap__

template<typename GUM_SCALAR>
VariableNodeMap gum::BayesNet< GUM_SCALAR >::varMap__
private

the map between variable and id

Definition at line 668 of file BayesNet.h.

Referenced by gum::BayesNet< double >::operator=().


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