aGrUM  0.16.0
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
 
double log10DomainSize () const
 
bool hasSameStructure (const DAGmodel &other)
 
Constructors and Destructor
 BayesNet ()
 Default constructor. More...
 
 BayesNet (std::string name)
 Default constructor. More...
 
 ~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 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...
 
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...
 
Getter and setters
const std::string & property (const std::string &name) const
 Return the value of the property name of this DAGModel. More...
 
const std::string & propertyWithDefault (const std::string &name, const std::string &byDefault) const
 Return the value of the property name of this DAGModel. More...
 
void setProperty (const std::string &name, const std::string &value)
 Add or change a property of this DAGModel. More...
 
Variable manipulation methods.
const DAGdag () const
 Returns a constant reference to the dag of this Bayes Net. More...
 
Size size () const
 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...
 
bool empty () const
 Retursn true if this Directed Graphical Model is empty. More...
 
const NodeGraphPartnodes () const
 Returns a constant reference to the dag of this Bayes Net. More...
 
virtual Instantiation completeInstantiation () const final
 Get an instantiation over all the variables of the model. More...
 
Arc manipulation methods.
const ArcSetarcs () const
 returns the set of nodes with arc ingoing to a given node 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
 returns the set of nodes with arc ingoing to a given node 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
 returns the set of nodes with arc ingoing to a given node More...
 
Graphical methods
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...
 

Static Public Member Functions

static BayesNet< GUM_SCALAR > fastPrototype (const std::string &dotlike, Size domainSize=2)
 Create a bn with a dotlike syntax : 'a->b->c;b->d;'. 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_2) = \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, if it's domain size changes, then the data in it's CPT is 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 160 of file BayesNet_tpl.h.

160  : IBayesNet< GUM_SCALAR >() {
161  GUM_CONSTRUCTOR(BayesNet);
162  }
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:160

◆ 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 165 of file BayesNet_tpl.h.

165  :
166  IBayesNet< GUM_SCALAR >(name) {
167  GUM_CONSTRUCTOR(BayesNet);
168  }
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:160

◆ ~BayesNet()

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

Destructor.

Definition at line 193 of file BayesNet_tpl.h.

193  {
194  GUM_DESTRUCTOR(BayesNet);
195  for (const auto p : __probaMap) {
196  delete p.second;
197  }
198  }
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:160
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654

◆ BayesNet() [3/3]

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

Copy constructor.

Definition at line 171 of file BayesNet_tpl.h.

171  :
172  IBayesNet< GUM_SCALAR >(source), __varMap(source.__varMap) {
173  GUM_CONS_CPY(BayesNet);
174 
175  __copyPotentials(source);
176  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
BayesNet()
Default constructor.
Definition: BayesNet_tpl.h:160
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:638

Member Function Documentation

◆ __clearPotentials()

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

clear all potentials

Definition at line 627 of file BayesNet_tpl.h.

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

627  {
628  // Removing previous potentials
629  for (const auto& elt : __probaMap) {
630  delete elt.second;
631  }
632 
633  __probaMap.clear();
634  }
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
+ Here is the caller 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 638 of file BayesNet_tpl.h.

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

639  {
640  // Copying potentials
641 
642  for (const auto src : source.__probaMap) {
643  // First we build the node's CPT
644  Potential< GUM_SCALAR >* copy_array = new Potential< GUM_SCALAR >();
645  copy_array->beginMultipleChanges();
646  for (gum::Idx i = 0; i < src.second->nbrDim(); i++) {
647  (*copy_array) << variableFromName(src.second->variable(i).name());
648  }
649  copy_array->endMultipleChanges();
650  copy_array->copyFrom(*(src.second));
651 
652  // We add the CPT to the CPT's hashmap
653  __probaMap.insert(src.first, copy_array);
654  }
655  }
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
Definition: BayesNet_tpl.h:309
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
Size Idx
Type for indexes.
Definition: types.h:53
+ 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 692 of file BayesNet_tpl.h.

693  {
694  delete __probaMap[id];
695  __probaMap[id] = newPot;
696  }
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654

◆ 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 232 of file BayesNet_tpl.h.

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

232  {
233  auto ptr = new MultiDimArray< GUM_SCALAR >();
234  NodeId res = 0;
235 
236  try {
237  res = add(var, ptr);
238 
239  } catch (Exception&) {
240  delete ptr;
241  throw;
242  }
243 
244  return res;
245  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
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 248 of file BayesNet_tpl.h.

249  {
250  if (nbrmod < 2) {
251  GUM_ERROR(OperationNotAllowed,
252  "Variable " << name << "needs more than " << nbrmod
253  << " modalities");
254  }
255 
256  LabelizedVariable v(name, name, nbrmod);
257  return add(v);
258  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
#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 261 of file BayesNet_tpl.h.

262  {
263  NodeId proposedId = dag().nextNodeId();
264  NodeId res = 0;
265 
266  res = add(var, aContent, proposedId);
267 
268  return res;
269  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
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:63
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 272 of file BayesNet_tpl.h.

273  {
274  auto ptr = new MultiDimArray< GUM_SCALAR >();
275  NodeId res = 0;
276 
277  try {
278  res = add(var, ptr, id);
279 
280  } catch (Exception&) {
281  delete ptr;
282  throw;
283  }
284 
285  return res;
286  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
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 290 of file BayesNet_tpl.h.

292  {
293  __varMap.insert(id, var);
294  this->_dag.addNodeWithId(id);
295 
296  auto cpt = new Potential< GUM_SCALAR >(aContent);
297  (*cpt) << variable(id);
298  __probaMap.insert(id, cpt);
299  return id;
300  }
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:202
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:315

◆ addAMPLITUDE()

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

Others aggregators.

Definition at line 461 of file BayesNet_tpl.h.

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

461  {
462  return add(var, new aggregator::Amplitude< GUM_SCALAR >());
463  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 466 of file BayesNet_tpl.h.

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

466  {
467  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an AND has to be boolean");
468 
469  return add(var, new aggregator::And< GUM_SCALAR >());
470  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
#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 348 of file BayesNet_tpl.h.

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

348  {
349  if (this->_dag.existsArc(tail, head)) {
350  GUM_ERROR(DuplicateElement,
351  "The arc (" << tail << "," << head << ") already exists.")
352  }
353 
354  this->_dag.addArc(tail, head);
355  // Add parent in the child's CPT
356  (*(__probaMap[head])) << variable(tail);
357  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:43
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
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 360 of file BayesNet_tpl.h.

361  {
362  try {
363  addArc(this->idFromName(tail), this->idFromName(head));
364  } catch (DuplicateElement) {
365  GUM_ERROR(DuplicateElement,
366  "The arc " << tail << "->" << head << " already exists.")
367  }
368  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:348
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:303
#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 473 of file BayesNet_tpl.h.

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

474  {
475  return add(var, new aggregator::Count< GUM_SCALAR >(value));
476  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 479 of file BayesNet_tpl.h.

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

480  {
481  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean");
482 
483  return add(var, new aggregator::Exists< GUM_SCALAR >(value));
484  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
#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 487 of file BayesNet_tpl.h.

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

488  {
489  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an EXISTS has to be boolean");
490 
491  return add(var, new aggregator::Forall< GUM_SCALAR >(value));
492  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
#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 565 of file BayesNet_tpl.h.

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

567  {
568  return add(var, new MultiDimLogit< GUM_SCALAR >(external_weight), id);
569  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 545 of file BayesNet_tpl.h.

546  {
547  return add(var, new MultiDimLogit< GUM_SCALAR >(external_weight));
548  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232

◆ addMAX()

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

Others aggregators.

Definition at line 495 of file BayesNet_tpl.h.

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

495  {
496  return add(var, new aggregator::Max< GUM_SCALAR >());
497  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 500 of file BayesNet_tpl.h.

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

500  {
501  return add(var, new aggregator::Median< GUM_SCALAR >());
502  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 505 of file BayesNet_tpl.h.

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

505  {
506  return add(var, new aggregator::Min< GUM_SCALAR >());
507  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 558 of file BayesNet_tpl.h.

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

560  {
561  return add(var, new MultiDimNoisyAND< GUM_SCALAR >(external_weight), id);
562  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 539 of file BayesNet_tpl.h.

540  {
541  return add(var, new MultiDimNoisyAND< GUM_SCALAR >(external_weight));
542  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232

◆ 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 521 of file BayesNet_tpl.h.

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

522  {
523  return addNoisyORCompound(var, external_weight);
524  }
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:527
+ 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 551 of file BayesNet_tpl.h.

553  {
554  return addNoisyORCompound(var, external_weight, id);
555  }
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:527

◆ 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 527 of file BayesNet_tpl.h.

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

528  {
529  return add(var, new MultiDimNoisyORCompound< GUM_SCALAR >(external_weight));
530  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 572 of file BayesNet_tpl.h.

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

◆ 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 533 of file BayesNet_tpl.h.

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

534  {
535  return add(var, new MultiDimNoisyORNet< GUM_SCALAR >(external_weight));
536  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
+ 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 579 of file BayesNet_tpl.h.

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

◆ 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 510 of file BayesNet_tpl.h.

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

510  {
511  if (var.domainSize() > 2) GUM_ERROR(SizeError, "an OR has to be boolean");
512 
513  return add(var, new aggregator::Or< GUM_SCALAR >());
514  }
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
Definition: BayesNet_tpl.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 586 of file BayesNet_tpl.h.

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

588  {
589  auto* CImodel =
590  dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(cpt(head).content());
591 
592  if (CImodel != 0) {
593  // or is OK
594  addArc(tail, head);
595 
596  CImodel->causalWeight(variable(tail), causalWeight);
597  } else {
598  GUM_ERROR(InvalidArc,
599  "Head variable (" << variable(head).name()
600  << ") is not a CIModel variable !");
601  }
602  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:348
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:315
#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 620 of file BayesNet.h.

622  {
623  addWeightedArc(idFromName(tail), idFromName(head), causalWeight);
624  };
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:586
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:303

◆ arcs()

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

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

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

Parameters
idthe node toward which the arcs returned are pointing

Definition at line 104 of file DAGmodel_inl.h.

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

Referenced by gum::EssentialGraph::__buildEssentialGraph(), gum::DAGmodel::__moralGraph(), gum::MarkovBlanket::hasSameStructure(), and gum::DAGmodel::hasSameStructure().

104 { return _dag.arcs(); }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
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 613 of file BayesNet_tpl.h.

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

613  {
614  for (const auto node : nodes())
615  __probaMap[node]->beginMultipleChanges();
616  }
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
+ 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 671 of file BayesNet_tpl.h.

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

672  {
673  if (cpt(id).nbrDim() != newPot->nbrDim()) {
674  GUM_ERROR(OperationNotAllowed,
675  "cannot exchange potentials with different "
676  "dimensions for variable with id "
677  << id);
678  }
679 
680  for (Idx i = 0; i < cpt(id).nbrDim(); i++) {
681  if (&cpt(id).variable(i) != &(newPot->variable(i))) {
682  GUM_ERROR(OperationNotAllowed,
683  "cannot exchange potentials because, for variable with id "
684  << id << ", dimension " << i << " differs. ");
685  }
686  }
687 
688  _unsafeChangePotential(id, newPot);
689  }
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:692
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:315
#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 699 of file BayesNet_tpl.h.

700  {
701  changePotential(idFromName(name), newPot);
702  }
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:671
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:303

◆ 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 214 of file BayesNet_tpl.h.

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

215  {
216  if (variable(id).varType() != VarType::Labelized) {
217  GUM_ERROR(NotFound, "Variable " << id << " is not a LabelizedVariable.");
218  }
219  LabelizedVariable* var = dynamic_cast< LabelizedVariable* >(
220  const_cast< DiscreteVariable* >(&variable(id)));
221 
222  var->changeLabel(var->posLabel(old_label), new_label);
223  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
#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 336 of file BayesNet.h.

338  {
339  changeVariableLabel(idFromName(name), old_label, new_label);
340  }
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: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:303

◆ 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 208 of file BayesNet_tpl.h.

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

209  {
210  __varMap.changeName(id, new_name);
211  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
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 315 of file BayesNet.h.

315  {
316  changeVariableName(idFromName(name), new_name);
317  }
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable&#39;s name in the gum::BayesNet.
Definition: BayesNet_tpl.h:208
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:303

◆ 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 arcs returned may be empty if no arc within the ArcGraphPart is outgoing from the given node.

Parameters
idthe node which is the tail of the arcs returned

Definition at line 111 of file DAGmodel_inl.h.

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

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

111  {
112  return _dag.children(id);
113  }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
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]

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

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

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

Parameters
idthe node toward which the arcs returned are pointing

Definition at line 165 of file DAGmodel.h.

References gum::DAGmodel::hasSameStructure(), gum::DAGmodel::idFromName(), gum::DAGmodel::log10DomainSize(), gum::DAGmodel::moralGraph(), gum::DAGmodel::operator=(), gum::DAGmodel::parents(), and gum::DAGmodel::topologicalOrder().

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

◆ completeInstantiation()

INLINE Instantiation gum::DAGmodel::completeInstantiation ( ) const
finalvirtualinherited

Get an instantiation over all the variables of the model.

Definition at line 86 of file DAGmodel_inl.h.

References gum::DAGmodel::dag(), and gum::DAGmodel::variable().

86  {
87  Instantiation I;
88 
89  for (const auto node : dag())
90  I << variable(node);
91 
92  return I;
93  }
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:63
+ Here is the call 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 315 of file BayesNet_tpl.h.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::__bnCopy(), gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::BayesNet< double >::cpt(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::SimpleCPTDisturber< GUM_SCALAR >::disturbAugmCPT(), gum::SimpleCPTDisturber< GUM_SCALAR >::disturbReducCPT(), and gum::credal::CredalNet< GUM_SCALAR >::toString().

315  {
316  return *(__probaMap[varId]);
317  }
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
+ 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 155 of file BayesNet.h.

155  {
156  return cpt(idFromName(name));
157  };
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:315
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:303

◆ dag()

◆ 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:106
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:115
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::DAGmodel::empty ( ) const
inherited

Retursn true if this Directed Graphical Model is empty.

Definition at line 99 of file DAGmodel_inl.h.

References gum::DAGmodel::size().

99 { return size() == 0; }
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:96
+ 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 620 of file BayesNet_tpl.h.

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

620  {
621  for (const auto node : nodes())
622  __probaMap[node]->endMultipleChanges();
623  }
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
+ 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 330 of file BayesNet_tpl.h.

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

330  {
331  if (__varMap.exists(varId)) {
332  // Reduce the variable child's CPT
333  const NodeSet& children = this->children(varId);
334 
335  for (const auto c : children) {
336  __probaMap[c]->erase(variable(varId));
337  }
338 
339  delete __probaMap[varId];
340 
341  __probaMap.erase(varId);
342  __varMap.erase(varId);
343  this->_dag.eraseNode(varId);
344  }
345  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:111
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
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.
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
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 267 of file BayesNet.h.

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

267 { 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:303
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:330
+ 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 325 of file BayesNet_tpl.h.

325  {
326  erase(__varMap.get(var));
327  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
Definition: BayesNet_tpl.h:330
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 371 of file BayesNet_tpl.h.

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

371  {
372  if (__varMap.exists(arc.tail()) && __varMap.exists(arc.head())) {
373  NodeId head = arc.head(), tail = arc.tail();
374  this->_dag.eraseArc(arc);
375  // Remove parent froms child's CPT
376  (*(__probaMap[head])) >> variable(tail);
377  }
378  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
bool exists(NodeId id) const
Return true if id matches a node.
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable&#39;s id and their CPT.
Definition: BayesNet.h:654
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 381 of file BayesNet_tpl.h.

381  {
382  eraseArc(Arc(tail, head));
383  }
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:371

◆ 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 416 of file BayesNet.h.

416  {
417  eraseArc(idFromName(tail), idFromName(head));
418  }
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:303
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head&#39;s CTP.
Definition: BayesNet_tpl.h:371

◆ fastPrototype()

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

Create a bn with a dotlike syntax : 'a->b->c;b->d;'.

The domain size maybe specified using 'a[10]' or using 'a{yes|maybe|no}'. Note that if the dotlike string contains such a specification for an already defined 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 127 of file BayesNet_tpl.h.

128  {
130 
131 
132  for (const auto& chaine : split(dotlike, ";")) {
133  NodeId lastId = 0;
134  bool notfirst = false;
135  for (const auto& souschaine : split(chaine, "->")) {
136  bool forward = true;
137  for (const auto& node : split(souschaine, "<-")) {
138  auto idVar = build_node(bn, node, domainSize);
139  if (notfirst) {
140  if (forward) {
141  bn.addArc(lastId, idVar);
142  forward = false;
143  } else {
144  bn.addArc(idVar, lastId);
145  }
146  } else {
147  notfirst = true;
148  forward = false;
149  }
150  lastId = idVar;
151  }
152  }
153  }
154  bn.generateCPTs();
155  bn.setProperty("name", dotlike);
156  return bn;
157  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:348
Class representing a Bayesian Network.
Definition: BayesNet.h:78
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, std::string node, gum::Size domainSize)
Definition: BayesNet_tpl.h:61
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this DAGModel.
Definition: DAGmodel_inl.h:56
void generateCPTs() const
randomly generates CPTs for a given structure
Definition: BayesNet_tpl.h:658
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 664 of file BayesNet_tpl.h.

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

664  {
665  SimpleCPTGenerator< GUM_SCALAR > generator;
666 
667  generator.generateCPT(cpt(node).pos(variable(node)), cpt(node));
668  }
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:315
+ 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 632 of file BayesNet.h.

632  {
633  generateCPT(idFromName(name));
634  };
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:664
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:303

◆ generateCPTs()

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

randomly generates CPTs for a given structure

Definition at line 658 of file BayesNet_tpl.h.

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

658  {
659  for (const auto node : nodes())
660  generateCPT(node);
661  }
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
Definition: BayesNet_tpl.h:664
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
+ 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 121 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().

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

121  {
122  if (this == &other) return true;
123 
124  if (size() != other.size()) return false;
125 
126  if (sizeArcs() != other.sizeArcs()) return false;
127 
128  for (const auto& nid : nodes()) {
129  try {
130  other.idFromName(variable(nid).name());
131  } catch (NotFound) { return false; }
132  }
133 
134  for (const auto& arc : arcs()) {
135  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
136  other.idFromName(variable(arc.head()).name()))))
137  return false;
138  }
139 
140  return true;
141  }
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:104
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:102
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:96
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
+ Here is the call graph for this function:
+ Here is the caller 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 303 of file BayesNet_tpl.h.

Referenced by gum::BayesNet< double >::addWeightedArc(), gum::build_node(), gum::BayesNet< double >::changeVariableLabel(), gum::BayesNet< double >::changeVariableName(), gum::BayesNet< double >::cpt(), 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().

303  {
304  return __varMap.idFromName(name);
305  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
NodeId idFromName(const std::string &name) const
+ Here is the caller 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 220 of file IBayesNet_tpl.h.

220  {
221  auto value = (GUM_SCALAR)1.0;
222 
223  GUM_SCALAR tmp;
224 
225  for (auto node : nodes()) {
226  if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) { return (GUM_SCALAR)0; }
227 
228  value *= tmp;
229  }
230 
231  return value;
232  }
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:115

◆ log10DomainSize()

INLINE double gum::DAGmodel::log10DomainSize ( ) const
inherited
Returns
Returns the log10 domain size of the joint probabilty for the Directed Graphical Model

Definition at line 75 of file DAGmodel_inl.h.

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

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

75  {
76  double dSize = 0.0;
77 
78  for (const auto node : nodes()) {
79  dSize += std::log10(variable(node).domainSize());
80  }
81 
82  return dSize;
83  }
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe 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 239 of file IBayesNet_tpl.h.

239  {
240  auto value = (GUM_SCALAR)0.0;
241 
242  GUM_SCALAR tmp;
243 
244  for (auto node : nodes()) {
245  if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
246  return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
247  }
248 
249  value += log2(cpt(node)[i]);
250  }
251 
252  return value;
253  }
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:115

◆ 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

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:115

◆ 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:115

◆ 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:115
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 355 of file IBayesNet_tpl.h.

356  {
357  if (soids.contains(target)) return NodeSet({target});
358 
359  NodeSet res;
360  NodeSet alreadyVisitedUp;
361  NodeSet alreadyVisitedDn;
362  alreadyVisitedDn << target;
363  alreadyVisitedUp << target;
364 
365  for (auto fath : _dag.parents(target))
367  fath, soids, res, alreadyVisitedUp, alreadyVisitedDn);
368  for (auto chil : _dag.children(target))
370  chil, soids, res, alreadyVisitedUp, alreadyVisitedDn);
371  return res;
372  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
void __minimalCondSetVisitUp(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisitedUp, NodeSet &alreadyVisitedDn) const
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
void __minimalCondSetVisitDn(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 375 of file IBayesNet_tpl.h.

376  {
377  NodeSet res;
378  for (auto node : targets) {
379  res += minimalCondSet(node, soids);
380  }
381  return res;
382  }
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

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:115
+ 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:115

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

References gum::DAGmodel::__moralGraph(), gum::DAGmodel::__mutableMoralGraph, and gum::UndiGraph::clear().

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(), gum::prm::SVE< GUM_SCALAR >::__initLiftedNodes(), and gum::DAGmodel::children().

101  {
102  if (clear
103  || (__mutableMoralGraph == nullptr)) { // we have to call _moralGraph
104  if (__mutableMoralGraph == nullptr) {
105  __mutableMoralGraph = new UndiGraph();
106  } else {
107  // clear is True ,__mutableMoralGraph exists
109  }
110 
111  __moralGraph();
112  }
113 
114  return *__mutableMoralGraph;
115  }
virtual void clear()
removes all the nodes and edges from the graph
Definition: undiGraph_inl.h:43
UndiGraph * __mutableMoralGraph
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:211
void __moralGraph() const
Returns the moral graph of this DAGModel.
Definition: DAGmodel.cpp:53
+ Here is the call graph for this function:
+ Here is the caller 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 227 of file BayesNet_tpl.h.

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

227  {
228  return __varMap.get(var);
229  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
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
inherited

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

Definition at line 115 of file DAGmodel_inl.h.

References gum::DAGmodel::_dag.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::__bnCopy(), gum::EssentialGraph::__buildEssentialGraph(), gum::MarkovBlanket::__buildMarkovBlanket(), gum::DAGmodel::__moralGraph(), gum::credal::CredalNet< GUM_SCALAR >::__sort_varType(), gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__verticesSampling(), gum::ImportanceSampling< GUM_SCALAR >::_unsharpenBN(), gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::disturbBN(), gum::Estimator< GUM_SCALAR >::Estimator(), gum::getMaxModality(), gum::DAGmodel::hasSameStructure(), gum::DAGmodel::log10DomainSize(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), gum::prm::ClassBayesNet< GUM_SCALAR >::modalities(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), gum::prm::ClassBayesNet< GUM_SCALAR >::toDot(), gum::credal::CredalNet< GUM_SCALAR >::toString(), and gum::BayesNetFragment< GUM_SCALAR >::~BayesNetFragment().

115  {
116  return (NodeGraphPart&)_dag;
117  }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
+ 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 303 of file IBayesNet_tpl.h.

303  {
304  return !this->operator==(from);
305  }
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 180 of file BayesNet_tpl.h.

180  {
181  if (this != &source) {
183  __varMap = source.__varMap;
184 
186  __copyPotentials(source);
187  }
188 
189  return *this;
190  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
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:638
void __clearPotentials()
clear all potentials
Definition: BayesNet_tpl.h:627

◆ 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 256 of file IBayesNet_tpl.h.

256  {
257  if (size() != from.size()) { return false; }
258 
259  if (sizeArcs() != from.sizeArcs()) { return false; }
260 
261  // alignment of variables between the 2 BNs
262  Bijection< const DiscreteVariable*, const DiscreteVariable* > alignment;
263 
264  for (auto node : nodes()) {
265  try {
266  alignment.insert(&variable(node),
267  &from.variableFromName(variable(node).name()));
268  } catch (NotFound&) {
269  // a name is not found in from
270  return false;
271  }
272  }
273 
274  for (auto node : nodes()) {
275  NodeId fromnode = from.idFromName(variable(node).name());
276 
277  if (cpt(node).nbrDim() != from.cpt(fromnode).nbrDim()) { return false; }
278 
279  if (cpt(node).domainSize() != from.cpt(fromnode).domainSize()) {
280  return false;
281  }
282 
283  Instantiation i(cpt(node));
284  Instantiation j(from.cpt(fromnode));
285 
286  for (i.setFirst(); !i.end(); i.inc()) {
287  for (Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
288  const DiscreteVariable* p = &(i.variable(indice));
289  j.chgVal(*(alignment.second(p)), i.val(*p));
290  }
291 
292  if (std::pow(cpt(node).get(i) - from.cpt(fromnode).get(j), (GUM_SCALAR)2)
293  > (GUM_SCALAR)1e-6) {
294  return false;
295  }
296  }
297  }
298 
299  return true;
300  }
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:102
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:96
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:115
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 arcs returned may be empty if no arc within the ArcGraphPart is ingoing into the given node.

Parameters
idthe node toward which the arcs returned are pointing

Definition at line 106 of file DAGmodel_inl.h.

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

Referenced by gum::MarkovBlanket::__buildMarkovBlanket(), gum::DAGmodel::__moralGraph(), gum::BayesNetFragment< GUM_SCALAR >::_installCPT(), gum::BayesNetFragment< GUM_SCALAR >::checkConsistency(), gum::DAGmodel::children(), gum::BayesNetFragment< GUM_SCALAR >::installCPT(), gum::DAGmodel::parents(), gum::prm::InstanceBayesNet< GUM_SCALAR >::toDot(), and gum::prm::ClassBayesNet< GUM_SCALAR >::toDot().

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

◆ parents() [2/2]

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

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

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

Parameters
idthe node toward which the arcs returned are pointing

Definition at line 156 of file DAGmodel.h.

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

156  {
157  return parents(idFromName(name));
158  };
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:106
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::DAGmodel::property ( const std::string &  name) const
inherited

Return the value of the property name of this DAGModel.

Exceptions
NotFoundRaised if no name property is found.

Definition at line 37 of file DAGmodel_inl.h.

References gum::DAGmodel::__properties(), and GUM_ERROR.

Referenced by gum::InfluenceDiagram< GUM_SCALAR >::toDot().

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  }
HashTable< std::string, std::string > & __properties() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
Definition: DAGmodel_inl.h:66
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ propertyWithDefault()

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

Return the value of the property name of this DAGModel.

return byDefault if the property name is not found

Definition at line 48 of file DAGmodel_inl.h.

References gum::DAGmodel::__properties().

49  {
50  try {
51  return __properties()[name];
52  } catch (NotFound&) { return byDefault; }
53  }
HashTable< std::string, std::string > & __properties() const
Return the properties of this Directed Graphical Model and initialize the hash table is necessary...
Definition: DAGmodel_inl.h:66
+ Here is the call 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 452 of file BayesNet_tpl.h.

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

452  {
453  reverseArc(Arc(tail, head));
454  }
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:452
+ 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 449 of file BayesNet.h.

449  {
450  reverseArc(idFromName(tail), idFromName(head));
451  }
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:303
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
Definition: BayesNet_tpl.h:452

◆ 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 386 of file BayesNet_tpl.h.

386  {
387  // check that the arc exsists
388  if (!__varMap.exists(arc.tail()) || !__varMap.exists(arc.head())
389  || !dag().existsArc(arc)) {
390  GUM_ERROR(InvalidArc, "a nonexisting arc cannot be reversed");
391  }
392 
393  NodeId tail = arc.tail(), head = arc.head();
394 
395  // check that the reversal does not induce a cycle
396  try {
397  DAG d = dag();
398  d.eraseArc(arc);
399  d.addArc(head, tail);
400  } catch (Exception&) {
401  GUM_ERROR(InvalidArc, "this arc reversal would induce a directed cycle");
402  }
403 
404  // with the same notations as Shachter (1986), "evaluating influence
405  // diagrams",
406  // p.878, we shall first compute the product of probabilities:
407  // pi_j^old (x_j | x_c^old(j) ) * pi_i^old (x_i | x_c^old(i) )
408  Potential< GUM_SCALAR > prod{cpt(tail) * cpt(head)};
409 
410  // modify the topology of the graph: add to tail all the parents of head
411  // and add to head all the parents of tail
413  NodeSet new_parents;
414  for (const auto node : this->parents(tail))
415  new_parents.insert(node);
416  for (const auto node : this->parents(head))
417  new_parents.insert(node);
418  // remove arc (head, tail)
419  eraseArc(arc);
420 
421  // add the necessary arcs to the tail
422  for (const auto p : new_parents) {
423  if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
424  }
425 
426  addArc(head, tail);
427  // add the necessary arcs to the head
428  new_parents.erase(tail);
429 
430  for (const auto p : new_parents) {
431  if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
432  }
433 
435 
436  // update the conditional distributions of head and tail
437  Set< const DiscreteVariable* > del_vars;
438  del_vars << &(variable(tail));
439  Potential< GUM_SCALAR > new_cpt_head =
440  prod.margSumOut(del_vars).putFirst(&variable(head));
441 
442  auto& cpt_head = const_cast< Potential< GUM_SCALAR >& >(cpt(head));
443  cpt_head = std::move(new_cpt_head);
444 
445  Potential< GUM_SCALAR > new_cpt_tail{
446  (prod / cpt_head).putFirst(&variable(tail))};
447  auto& cpt_tail = const_cast< Potential< GUM_SCALAR >& >(cpt(tail));
448  cpt_tail = std::move(new_cpt_tail);
449  }
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head&#39;s CPT.
Definition: BayesNet_tpl.h:348
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:106
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
bool exists(NodeId id) const
Return true if id matches a node.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Definition: BayesNet_tpl.h:613
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
Definition: BayesNet_tpl.h:315
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
Definition: BayesNet_tpl.h:620
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:63
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:371

◆ setProperty()

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

Add or change a property of this DAGModel.

Definition at line 56 of file DAGmodel_inl.h.

References gum::DAGmodel::__properties(), and gum::HashTable< Key, Val, Alloc >::insert().

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

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

◆ size()

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

Returns the number of variables in this Directed Graphical Model.

Definition at line 96 of file DAGmodel_inl.h.

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

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

96 { 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:63
+ 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 102 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==().

102 { return _dag.sizeArcs(); }
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
+ 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 175 of file IBayesNet_tpl.h.

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

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

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

Referenced by gum::EssentialGraph::__buildEssentialGraph(), gum::InfluenceDiagramGenerator< GUM_SCALAR >::__checkTemporalOrder(), gum::DAGmodel::children(), gum::InfluenceDiagram< GUM_SCALAR >::decisionOrderExists(), and gum::InfluenceDiagram< GUM_SCALAR >::getDecisionOrder().

117  {
118  return this->dag().topologicalOrder(clear);
119  }
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:63
+ 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  double compressionRatio = log10(1.0 * param) - dSize;
153 
154  std::stringstream s;
155  s << "BN{nodes: " << size() << ", arcs: " << dag().sizeArcs() << ", ";
156 
157  if (dSize > 6)
158  s << "domainSize: 10^" << dSize;
159  else
160  s << "domainSize: " << std::round(std::pow(10.0, dSize));
161 
162  s << ", parameters: " << param << ", compression ratio: ";
163 
164  if (compressionRatio > -3)
165  s << trunc(100.0 - std::pow(10.0, compressionRatio + 2.0));
166  else
167  s << "100-10^" << compressionRatio + 2.0;
168 
169  s << "% }";
170 
171  return s.str();
172  }
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:96
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:115
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
double log10DomainSize() const
Definition: DAGmodel_inl.h:75
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:63
+ Here is the caller graph for this function:

◆ 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 202 of file BayesNet_tpl.h.

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

202  {
203  return __varMap.get(id);
204  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
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 297 of file BayesNet.h.

297  {
298  return variable(idFromName(name));
299  };
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Definition: BayesNet_tpl.h:202
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:303

◆ 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 309 of file BayesNet_tpl.h.

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

309  {
310  return __varMap.variableFromName(name);
311  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
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 320 of file BayesNet_tpl.h.

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

320  {
321  return __varMap;
322  }
VariableNodeMap __varMap
the map between variable and id
Definition: BayesNet.h:651
+ 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

◆ __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 654 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 651 of file BayesNet.h.

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

◆ _dag


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