aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber > Class Template Reference

<agrum/BN/generator/MCayesNetGenerator.h> More...

#include <MCBayesNetGenerator.h>

Inheritance diagram for gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >:
Collaboration diagram for gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >:

Public Member Functions

Constructors / Destructor
 MCBayesNetGenerator (Size nbrNodes, Size maxArcs, Idx maxModality=2, Size iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
 Constructor.
 MCBayesNetGenerator (BayesNet< GUM_SCALAR > bayesNet, Size iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
 Constructor.
 ~MCBayesNetGenerator () override
 Destructor.
BN generation methods
void generateBN (BayesNet< GUM_SCALAR > &bayesNet) override
 Generates a random Bayesian network.
Getters
Size iteration () const
 Return a constant reference to the number of iteration imposed on the Markov Chain BayesNetGenerator.
Idx p () const
 Return a constant reference to the probabilité p imposed on the Markov Chain BayesNetGenerator.
Idx q () const
 Return a constant reference to the probabilité imposed on the Markov Chain BayesNetGenerator.
Setters
void setIteration (Size iteration)
 Modifies the value of the number of iterations impose on the BayesNetGenerator.
void setP (Idx p)
 Modifies the value of the probability p imposed on the BayesNetGenerator.
void setQ (Idx q)
 Modifies the value of the probability q imposed on the BayesNetGenerator.
BN generation methods
void fromDAG (BayesNet< GUM_SCALAR > &bayesNet)
 Virtual function that Generates a Bayesian networks.
void fillCPT (BayesNet< GUM_SCALAR > &bn) const
 function that insert random values in the CPT of each nodes according to the CPTGenerator.
Getters
Size nbrNodes () const
 Return a constant reference to the number of nodes imposed on the IBayesNetGenerator.
Size maxArcs () const
 Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator.
Size maxModality () const
 Return a constant reference to the maximum modality imposed on the IBayesNetGenerator.
Setters
void setNbrNodes (Size nbrNodes)
 Modifies the value of the number of nodes imposed on the BayesGenerator.
void setMaxArcs (Size maxArcs)
 Modifies the value of the number of nodes imposed on the BayesGenerator.
void setMaxModality (Size maxModality)
 Modifies the value of the number of nodes imposed on the BayesGenerator.
CPT generation methods
void generateCPT (const Idx &varId, const Tensor< GUM_SCALAR > &cpt) const override
 Generates a CPT using floats.
CPT disturbing methods
void disturbReducCPT (NodeId node, BayesNet< GUM_SCALAR > &bayesNet, Tensor< GUM_SCALAR > &src, Tensor< GUM_SCALAR > &marg) override
 Disturb a CPT using GUM_SCALAR when removing parent varIdi.
void disturbAugmCPT (NodeId node, BayesNet< GUM_SCALAR > &bayesNet, Tensor< GUM_SCALAR > &src, GUM_SCALAR variation) override
 Disturb a CPT using GUM_SCALAR when inserting a new parent new_parent.

Protected Member Functions

bool _isPolytree_ ()
 The function that verify if graph is a polytree.
bool _connect_ (NodeId i, NodeId j)
 The function that verify if node i and j are connected.
bool _directedPath_ (NodeId tail, NodeId head)
 The function that verify if there is a oriented path from node i to node j.
void _insertArc_ (NodeId i, NodeId j)
 The function that will insert an arc between node i to node j, but only if there isn't any cycle created.
void _eraseArc_ (NodeId i, NodeId j, bool mustbeconnex=true)
 The function that will remove the arc between node i and node j.
void _PMMx_poly_ ()
 In the case that the graph is a polytree, the function will, according to the probability p and q, choose which change of state must occur (AorR or AR or jump) then will assert that the imposed constraints are respected and if not, will return to the previous topology.
void _PMMx_multi_ ()
 In the case that the graph is a multiconnected graph, the function will, according to the probability p and q, choose which change of state must occur (AorR or jump) then will assert that the imposed constraint are respected and if not, will return to the previous topology.
void _jump_poly_ ()
 In the case that the graph is a polytree, the function will add a random arc by the use of the function insertArc if the arc does not exist already.
void _jump_multi_ ()
 In the case that the graph is a multiconnect graph, the function will choose randomly two nodes and will remove the arc between them by the use of the function insertArc if the arc exists.
void _Add_or_Remove_ ()
 The function will add or remove a random arc in the graph using the functions insertArc and removeArc.
void _Add_and_Remove_ ()
 The function will remove and add a random arc changing the topology of the graph but asserting its connectivity.
virtual bool _checkConditions_ ()
 The boolean function that will assert the respect of the constraint.
bool _is_connected_ (const NodeId i, const NodeId j, NodeSet &excluded)
 The internal function used by the previous is_connected.
bool _directedPath_ (NodeId tail, NodeId head, NodeSet &excluded)
 The internal function used by the previous directedPath.
void _chooseNodes_ (NodeId &i, NodeId &j)
 The function that randomly choose two nodes of the graph.
void _chooseCloseNodes_ (NodeId &i, NodeId &j)
 The function that randomly choose two neighbours nodes of the graph.
void _transformPoly_ (Idx nbiter)
 The function that randomly change the simple tree into a polytree.
void _createTree_ (Size BNSize)
 The function that randomly generate a simple tree.
NodeId _createPartTree_ (Size BNSize, Idx &n)
 The internal function used by createTree that randomly generate a simple tree.

Protected Attributes

Size iteration_
Idx p_
Idx q_
Size nbrNodes_
Size maxArcs_
Size maxModality_
DAG dag_

Detailed Description

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator = SimpleCPTGenerator, template< typename > class ICPTDisturber = SimpleCPTDisturber>
class gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >

<agrum/BN/generator/MCayesNetGenerator.h>

Class for generating Bayesian networks with Markov chains.

This class randomly generates a Bayesian network given 6 parameters: the number of nodes, the maximum number of arcs the and of iterations the maximum modality.

Warning
Be Careful when entering the parameters, high Values may cause the density of the Bayesian network to be too high resulting in the failure of most of the inference Methods.

This Generation method require the use of two probability parameters(p, q) defining the choice of processes that will provoke the change of state. You can see in the graph below how the probabilities are used.

Definition at line 169 of file MCBayesNetGenerator.h.

Constructor & Destructor Documentation

◆ MCBayesNetGenerator() [1/2]

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator ( Size nbrNodes,
Size maxArcs,
Idx maxModality = 2,
Size iteration = NB_INIT_ITERATIONS,
Idx p = 30,
Idx q = 40 )

Constructor.

Use by default the SimpleCPTGenerator for generating the BNs CPT and the SimpleCPTDisturber to tweak the CPT when the dimension of the table changes.

Parameters
nbrNodesThe number of nodes in the generated BN.
maxArcsThe maximum number of Arcs.
maxModalityEach DRV has from 2 to maxModality modalities
iterationThe number of iterations wanted to repeat the algorithm
pprobability for the change of the state (see use of p and q )
qprobability for the change of the state (see use of p and q )

Definition at line 71 of file MCBayesNetGenerator_tpl.h.

78 if (p + q > 100)
80 "the sum of the probabilities p and q must be at most equal to 100");
81
83 p_ = p;
84 q_ = q;
85
87 }
<agrum/BN/generator/MCayesNetGenerator.h>
Idx p() const
Return a constant reference to the probabilité p imposed on the Markov Chain BayesNetGenerator.
MCBayesNetGenerator(Size nbrNodes, Size maxArcs, Idx maxModality=2, Size iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
Constructor.
Idx q() const
Return a constant reference to the probabilité imposed on the Markov Chain BayesNetGenerator.
Size iteration() const
Return a constant reference to the number of iteration imposed on the Markov Chain BayesNetGenerator.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References MCBayesNetGenerator(), GUM_ERROR, IBNG, iteration(), iteration_, gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxModality(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes(), p(), p_, q(), and q_.

Referenced by MCBayesNetGenerator(), MCBayesNetGenerator(), and ~MCBayesNetGenerator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MCBayesNetGenerator() [2/2]

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator ( BayesNet< GUM_SCALAR > bayesNet,
Size iteration = NB_INIT_ITERATIONS,
Idx p = 30,
Idx q = 40 )
explicit

Constructor.

Use by default the SimpleCPTGenerator for generating the BNs CPT and the SimpleCPTDisturber to tweak the CPT when the dimension of the table changes.

Parameters
bayesNetthe IBayesNet used as reference to fill the parameters nbrNodes, maxArcs and maxModality
iterationThe number of iterations wanted to repeat the algorithm
pprobability for the change of the state (see probability_p_q )
qprobability for the change of the state (see probability_p_q )

Definition at line 92 of file MCBayesNetGenerator_tpl.h.

96 :
98 (Size)(bayesNet.sizeArcs() * 1.1),
101 p_ = p;
102 q_ = q;
103 }

References MCBayesNetGenerator(), gum::getMaxModality(), iteration(), iteration_, p(), p_, q(), and q_.

Here is the call graph for this function:

◆ ~MCBayesNetGenerator()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::~MCBayesNetGenerator ( )
override

Destructor.

Definition at line 109 of file MCBayesNetGenerator_tpl.h.

References MCBayesNetGenerator().

Here is the call graph for this function:

Member Function Documentation

◆ _Add_and_Remove_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_Add_and_Remove_ ( )
protected

The function will remove and add a random arc changing the topology of the graph but asserting its connectivity.

Definition at line 291 of file MCBayesNetGenerator_tpl.h.

291 {
292 NodeId i, j, head, tail;
293 _chooseNodes_(i, j);
294
295 if (this->dag_.existsArc(i, j) || this->dag_.existsArc(j, i)) {
296 return;
297 } else {
298 Idx per = randomValue(100);
299
300 if (per < 50) {
301 head = i;
302 tail = j;
303 } else {
304 head = j;
305 tail = i;
306 }
307
308 for (auto node: this->dag_.parents(j)) {
310 excluded.insert(j);
311
312 if (_is_connected_(node, i, excluded)) {
313 this->dag_.eraseArc(Arc(node, j));
314 this->dag_.addArc(head, tail);
315 return;
316 }
317 }
318
319 for (auto node: this->dag_.children(j)) {
321 excluded.insert(j);
322
323 if (_is_connected_(node, i, excluded)) {
324 this->dag_.eraseArc(Arc{j, node});
325 this->dag_.addArc(head, tail);
326 return;
327 }
328 }
329 }
330 }
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
bool _is_connected_(const NodeId i, const NodeId j, NodeSet &excluded)
The internal function used by the previous is_connected.
void _chooseNodes_(NodeId &i, NodeId &j)
The function that randomly choose two nodes of the graph.
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.

◆ _Add_or_Remove_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_Add_or_Remove_ ( )
protected

The function will add or remove a random arc in the graph using the functions insertArc and removeArc.

Definition at line 278 of file MCBayesNetGenerator_tpl.h.

278 {
279 NodeId i, j;
280 _chooseNodes_(i, j);
281 if (this->dag_.existsArc(i, j)) {
282 _eraseArc_(i, j);
283
284 return;
285 } else _insertArc_(i, j);
286 }
void _insertArc_(NodeId i, NodeId j)
The function that will insert an arc between node i to node j, but only if there isn't any cycle crea...
void _eraseArc_(NodeId i, NodeId j, bool mustbeconnex=true)
The function that will remove the arc between node i and node j.

References _chooseNodes_(), _eraseArc_(), and gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_.

Here is the call graph for this function:

◆ _checkConditions_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
bool gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_checkConditions_ ( )
protectedvirtual

The boolean function that will assert the respect of the constraint.

Reimplemented in gum::MaxInducedWidthMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >, and gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >.

Definition at line 183 of file MCBayesNetGenerator_tpl.h.

183 {
184 return this->maxArcs_ >= this->dag_.sizeArcs();
185 }

References gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, and gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs_.

Referenced by gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_checkConditions_(), and _transformPoly_().

Here is the caller graph for this function:

◆ _chooseCloseNodes_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_chooseCloseNodes_ ( NodeId & i,
NodeId & j )
protected

The function that randomly choose two neighbours nodes of the graph.

Definition at line 412 of file MCBayesNetGenerator_tpl.h.

414 {
415 NodeId temp = randomValue(this->dag_.size());
416 Size co = 0;
417
418 if (this->dag_.parents(temp).size()) {
419 j = temp;
420 auto it = this->dag_.parents(j).begin();
421 co = randomValue(this->dag_.parents(j).size());
422
423 while (co--) {
424 ++it;
425 }
426
427 i = *it;
428 } else if (this->dag_.children(temp).size()) {
429 i = temp;
430 auto it = this->dag_.children(i).begin();
431 co = randomValue(this->dag_.children(i).size());
432
433 while (co--) {
434 ++it;
435 }
436
437 j = *it;
438 } else {
439 GUM_ERROR(FatalError, "Sorry Misconstructed BN because of isolated node.")
440 }
441 }

References gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, and gum::randomValue().

Referenced by _transformPoly_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _chooseNodes_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_chooseNodes_ ( NodeId & i,
NodeId & j )
protected

The function that randomly choose two nodes of the graph.

Definition at line 397 of file MCBayesNetGenerator_tpl.h.

398 {
399 if (this->dag_.size() < 3) {
400 GUM_ERROR(ArgumentError, "This dag has only " << this->dag_.size() << " nodes.")
401 }
402 i = randomValue(this->dag_.size());
403 j = randomValue(this->dag_.size());
404
405 while (i == j)
406 j = randomValue(this->dag_.size());
407 }

References gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, and GUM_ERROR.

Referenced by _Add_or_Remove_(), _jump_multi_(), and _jump_poly_().

Here is the caller graph for this function:

◆ _connect_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
bool gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_connect_ ( NodeId i,
NodeId j )
protected

The function that verify if node i and j are connected.

Definition at line 513 of file MCBayesNetGenerator_tpl.h.

514 {
515 if (this->dag_.existsArc(i, j) || this->dag_.existsArc(j, i)) return true;
516 else {
518 excluded.insert(i);
519
520 for (auto par: this->dag_.parents(i)) {
521 if (!excluded.exists(par) && _is_connected_(par, j, excluded)) return true;
522 }
523
524 for (auto chi: this->dag_.children(i)) {
525 if (!excluded.exists(chi) && _is_connected_(chi, j, excluded)) return true;
526 }
527
528 return false;
529 }
530 }

References _is_connected_(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, gum::Set< Key >::exists(), and gum::Set< Key >::insert().

Here is the call graph for this function:

◆ _createPartTree_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
NodeId gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_createPartTree_ ( Size BNSize,
Idx & n )
protected

The internal function used by createTree that randomly generate a simple tree.

n : id number for node label

Definition at line 464 of file MCBayesNetGenerator_tpl.h.

465 {
466 /*
467 Size nb_mod = 2 + randomValue(this->maxModality_ - 1);
468 std::stringstream strBuff;
469 strBuff << "n_" << n++;
470 NodeId root = this->bayesNet_.add(LabelizedVariable(strBuff.str(), "", nb_mod));
471 */
472 NodeId root = this->dag_.addNode();
473 Size maxNodes = BNSize - 1;
474 Size SubG = 0;
475
476 while (maxNodes) {
480 this->dag_.addArc(root, rootS);
481 }
482
483 return root;
484 }
NodeId _createPartTree_(Size BNSize, Idx &n)
The internal function used by createTree that randomly generate a simple tree.

References _createPartTree_(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, and gum::randomValue().

Referenced by _createPartTree_(), and _createTree_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _createTree_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_createTree_ ( Size BNSize)
protected

The function that randomly generate a simple tree.

Definition at line 446 of file MCBayesNetGenerator_tpl.h.

446 {
447 Idx n = 0;
448 NodeId root = this->dag_.addNode();
449 Size maxNodes = BNSize - 1;
450 Size SubG = 0;
451
452 while (maxNodes) {
456 this->dag_.addArc(root, rootS);
457 }
458 }

References _createPartTree_(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, and gum::randomValue().

Referenced by generateBN().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _directedPath_() [1/2]

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
bool gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_directedPath_ ( NodeId tail,
NodeId head )
protected

The function that verify if there is a oriented path from node i to node j.

Definition at line 559 of file MCBayesNetGenerator_tpl.h.

560 {
561 if (this->dag_.existsArc(tail, head)) return true;
562 else {
564 excluded.insert(tail);
565
566 for (auto node: this->dag_.children(tail)) {
567 if (_directedPath_(node, head, excluded)) return true;
568 }
569
570 return false;
571 }
572 }
bool _directedPath_(NodeId tail, NodeId head)
The function that verify if there is a oriented path from node i to node j.

References _directedPath_(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, and gum::Set< Key >::insert().

Referenced by _directedPath_(), and _directedPath_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _directedPath_() [2/2]

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
bool gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_directedPath_ ( NodeId tail,
NodeId head,
NodeSet & excluded )
protected

The internal function used by the previous directedPath.

It asserts the existence of an oriented path between node i and node j avoiding passing through nodes listed in excluded.

Definition at line 577 of file MCBayesNetGenerator_tpl.h.

580 {
581 if (this->dag_.existsArc(tail, head)) return true;
582 else {
583 excluded.insert(tail);
584
585 for (auto node: this->dag_.children(tail)) {
586 if (!excluded.exists(node) && _directedPath_(node, head, excluded)) return true;
587 }
588
589 return false;
590 }
591 }

References _directedPath_(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, gum::Set< Key >::exists(), and gum::Set< Key >::insert().

Here is the call graph for this function:

◆ _eraseArc_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_eraseArc_ ( NodeId i,
NodeId j,
bool mustbeconnex = true )
protected

The function that will remove the arc between node i and node j.

If the boolean parameter mustbeconnex is true, the function will assert that the graph remain connected and will restore the arc otherwise.

Definition at line 371 of file MCBayesNetGenerator_tpl.h.

374 {
375 /*if (disturbing_) {
376 const BayesNet< GUM_SCALAR > bayesNet(this->bayesNet_);
377 Tensor< GUM_SCALAR > potj;
378 potj.copy(this->bayesNet_.cpt(j));
379 this->bayesNet_.eraseArc(i, j);
380
381 if (_connect_(i, j) || !mustbeconnex) {
382 auto marg = *hashMarginal_[i];
383
384 this->disturbReducCPT(j, this->bayesNet_, potj, marg);
385 } else this->bayesNet_.addArc(i, j);
386 } else */
387 {
388 this->dag_.eraseArc(Arc(i, j));
389
390 if (!_connect_(i, j) && mustbeconnex) { this->dag_.addArc(i, j); }
391 }
392 }
bool _connect_(NodeId i, NodeId j)
The function that verify if node i and j are connected.

Referenced by _Add_or_Remove_(), _jump_multi_(), and _transformPoly_().

Here is the caller graph for this function:

◆ _insertArc_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_insertArc_ ( NodeId i,
NodeId j )
protected

The function that will insert an arc between node i to node j, but only if there isn't any cycle created.

Definition at line 355 of file MCBayesNetGenerator_tpl.h.

356 {
357 if (_directedPath_(j, i)) return;
358
359 /*if (disturbing_) {
360 auto potj = this->bayesNet_.cpt(j);
361 this->bayesNet_.addArc(i, j);
362
363 this->disturbAugmCPT(j, this->bayesNet_, potj, (GUM_SCALAR)0.5);
364 } else */
365 this->dag_.addArc(i, j);
366 }

◆ _is_connected_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
bool gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_is_connected_ ( const NodeId i,
const NodeId j,
NodeSet & excluded )
protected

The internal function used by the previous is_connected.

It asserts the existence of an unoriented path between node i and node j avoiding passing through nodes listed in excluded.

Definition at line 535 of file MCBayesNetGenerator_tpl.h.

538 {
539 if (this->dag_.existsArc(i, j) || this->dag_.existsArc(j, i)) return true;
540 else {
541 excluded.insert(i);
542
543 for (auto par: this->dag_.parents(i)) {
544 if (!excluded.exists(par) && _is_connected_(par, j, excluded)) return true;
545 }
546
547 for (auto chi: this->dag_.children(i)) {
548 if (!excluded.exists(chi) && _is_connected_(chi, j, excluded)) return true;
549 }
550
551 return false;
552 }
553 }

References _is_connected_(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_, gum::Set< Key >::exists(), and gum::Set< Key >::insert().

Referenced by _connect_(), and _is_connected_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _isPolytree_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
bool gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_isPolytree_ ( )
protected

The function that verify if graph is a polytree.

Definition at line 506 of file MCBayesNetGenerator_tpl.h.

506 {
507 return this->dag_.size() - 1 == this->dag_.sizeArcs();
508 }

References gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_.

◆ _jump_multi_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_jump_multi_ ( )
protected

In the case that the graph is a multiconnect graph, the function will choose randomly two nodes and will remove the arc between them by the use of the function insertArc if the arc exists.

Definition at line 345 of file MCBayesNetGenerator_tpl.h.

345 {
346 NodeId i, j;
347 _chooseNodes_(i, j);
348
349 if (this->dag_.existsArc(i, j)) { _eraseArc_(i, j); }
350 }

References _chooseNodes_(), _eraseArc_(), and gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_.

Here is the call graph for this function:

◆ _jump_poly_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_jump_poly_ ( )
protected

In the case that the graph is a polytree, the function will add a random arc by the use of the function insertArc if the arc does not exist already.

Definition at line 335 of file MCBayesNetGenerator_tpl.h.

335 {
336 NodeId i, j;
337 _chooseNodes_(i, j);
338
339 if (!this->dag_.existsArc(i, j)) _insertArc_(i, j);
340 }

References _chooseNodes_().

Here is the call graph for this function:

◆ _PMMx_multi_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_PMMx_multi_ ( )
protected

In the case that the graph is a multiconnected graph, the function will, according to the probability p and q, choose which change of state must occur (AorR or jump) then will assert that the imposed constraint are respected and if not, will return to the previous topology.

Definition at line 236 of file MCBayesNetGenerator_tpl.h.

236 {
237 while (true) {
238 if (!iteration_--) return;
239 DAG tmp_dag = this->dag_;
240
241 Idx per = randomValue(100);
242
243 if (per < p_ + q_) {
245 if (_checkConditions_()) {
246 if (_isPolytree_()) {
247 if (per < p_) {
248 tmp_dag = this->dag_;
249 _PMMx_poly_();
250 break;
251 } else {
252 this->dag_ = tmp_dag;
253 }
254 } else {
255 tmp_dag = this->dag_;
256 }
257 } else {
258 this->dag_ = tmp_dag;
259 }
260 } else {
261 _jump_multi_();
262 if (_checkConditions_()) {
263 tmp_dag = this->dag_;
264 if (_isPolytree_()) {
265 _PMMx_poly_();
266 break;
267 }
268 } else {
269 this->dag_ = tmp_dag;
270 }
271 }
272 }
273 }
void _jump_multi_()
In the case that the graph is a multiconnect graph, the function will choose randomly two nodes and w...
void _PMMx_poly_()
In the case that the graph is a polytree, the function will, according to the probability p and q,...
virtual bool _checkConditions_()
The boolean function that will assert the respect of the constraint.
void _Add_or_Remove_()
The function will add or remove a random arc in the graph using the functions insertArc and removeArc...
bool _isPolytree_()
The function that verify if graph is a polytree.

◆ _PMMx_poly_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_PMMx_poly_ ( )
protected

In the case that the graph is a polytree, the function will, according to the probability p and q, choose which change of state must occur (AorR or AR or jump) then will assert that the imposed constraints are respected and if not, will return to the previous topology.

Definition at line 193 of file MCBayesNetGenerator_tpl.h.

193 {
194 while (true) {
195 if (!iteration_--) return;
196 DAG tmp_dag = this->dag_;
197 Idx per = randomValue(100);
198
199 if (per < p_) {
201
202 if (_checkConditions_()) {
203 tmp_dag = this->dag_;
204 _PMMx_multi_();
205 break;
206 } else {
207 this->dag_ = tmp_dag;
208 }
209 } else {
210 if (per < p_ + q_) {
212
213 if (!_checkConditions_()) {
214 this->dag_ = tmp_dag;
215 } else {
216 tmp_dag = this->dag_;
217 }
218 } else {
219 _jump_poly_();
220
221 if (_checkConditions_()) {
222 tmp_dag = this->dag_;
223 _PMMx_multi_();
224 break;
225 } else {
226 this->dag_ = tmp_dag;
227 }
228 }
229 }
230 }
231 }
void _PMMx_multi_()
In the case that the graph is a multiconnected graph, the function will, according to the probability...
void _Add_and_Remove_()
The function will remove and add a random arc changing the topology of the graph but asserting its co...
void _jump_poly_()
In the case that the graph is a polytree, the function will add a random arc by the use of the functi...

Referenced by generateBN().

Here is the caller graph for this function:

◆ _transformPoly_()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_transformPoly_ ( Idx nbiter)
protected

The function that randomly change the simple tree into a polytree.

Definition at line 491 of file MCBayesNetGenerator_tpl.h.

491 {
492 while (nbiter--) {
493 NodeId i, j;
495 auto dag_tmp = this->dag_;
496 _eraseArc_(i, j, false);
497 this->dag_.addArc(j, i);
498
499 if (!_checkConditions_()) this->dag_ = dag_tmp;
500 }
501 }
void _chooseCloseNodes_(NodeId &i, NodeId &j)
The function that randomly choose two neighbours nodes of the graph.

References _checkConditions_(), _chooseCloseNodes_(), _eraseArc_(), and gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_.

Referenced by generateBN().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disturbAugmCPT()

template<GUM_Numeric GUM_SCALAR>
void gum::SimpleCPTDisturber< GUM_SCALAR >::disturbAugmCPT ( NodeId node,
BayesNet< GUM_SCALAR > & bayesNet,
Tensor< GUM_SCALAR > & src,
GUM_SCALAR variation )
overrideinherited

Disturb a CPT using GUM_SCALAR when inserting a new parent new_parent.

Parameters
nodeA reference on the CPT owner.
bayesNetthe Bayesian network.
srccopy of the CPT before augmentation.
variationdegree of variation from the initial probability.

Definition at line 95 of file simpleCPTDisturber_tpl.h.

98 {
100
101 for (i.setFirst(); !i.end(); ++i)
102 bayesNet.cpt(node).set(i, src.get(i) + GUM_SCALAR(randomProba()) * variation);
103
104 bayesNet.cpt(node).normalizeAsCPT();
105 }
<agrum/BN/generator/simpleCPTDisturber.h>
double randomProba()
Returns a random double between 0 and 1 included (i.e.

◆ disturbReducCPT()

template<GUM_Numeric GUM_SCALAR>
void gum::SimpleCPTDisturber< GUM_SCALAR >::disturbReducCPT ( NodeId node,
BayesNet< GUM_SCALAR > & bayesNet,
Tensor< GUM_SCALAR > & src,
Tensor< GUM_SCALAR > & marg )
overrideinherited

Disturb a CPT using GUM_SCALAR when removing parent varIdi.

Parameters
nodeThe variable on the CPT owner.
bayesNetthe Bayesian network.
srccopy of the CPT before reduction.
margof the inference before reduction on the node varIdi.

Definition at line 71 of file simpleCPTDisturber_tpl.h.

74 {
78
79 for (i_dest.setFirst(); !i_dest.end(); ++i_dest) {
81
82 i_src.setVals(i_dest);
83 for (i_marg.setFirst(); !i_marg.end(); ++i_marg) {
84 i_src.setVals(i_marg);
85 potval += src.get(i_src) * marg.get(i_marg);
86 }
87
88 bayesNet.cpt(node).set(i_dest, potval);
89 }
90
91 bayesNet.cpt(node).normalizeAsCPT();
92 }

References gum::Instantiation::end(), gum::Instantiation::setFirst(), and gum::Instantiation::setVals().

Here is the call graph for this function:

◆ fillCPT()

void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fillCPT ( BayesNet< GUM_SCALAR > & bn) const
inherited

function that insert random values in the CPT of each nodes according to the CPTGenerator.

Returns
null but modify inputed empty Bayesian network

Definition at line 130 of file IBayesNetGenerator_tpl.h.

82 {
83 for (auto node: bn.nodes())
84 this->generateCPT(bn.cpt(node).pos(bn.variable(node)), bn.cpt(node));
85 }
Class for generating Bayesian networks.
void generateCPT(const Idx &varId, const Tensor< GUM_SCALAR > &cpt) const override
Generates a CPT using floats.

References dag_, maxModality_, and gum::randomValue().

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::generateBN(), and gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromDAG()

void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fromDAG ( BayesNet< GUM_SCALAR > & bayesNet)
inherited

Virtual function that Generates a Bayesian networks.

Parameters
bayesNetBayesian network to be completed after initialisation
Returns
null but modify inputed empty Bayesian network

Definition at line 122 of file IBayesNetGenerator_tpl.h.

127 {
128 bn.clear();
129
130 const auto width = (this->dag_.size() >= 100) ? 3 : 2;
131 int n = 0;
132 const auto& topo = this->dag_.topologicalOrder();
133 for (const auto node: topo) {
134 bn.add(RangeVariable(std::format("X{:0{}}", n++, width),
135 "",
136 0,
137 long(1 + randomValue(this->maxModality_ - 1))),
138 node);
139 }
140 bn.beginTopologyTransformation();
141 for (auto arc: this->dag_.arcs()) {
142 bn.addArc(arc.tail(), arc.head());
143 }
144 bn.endTopologyTransformation();
145 }

References maxArcs(), and maxArcs_.

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::generateBN(), and gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateBN()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::generateBN ( BayesNet< GUM_SCALAR > & bayesNet)
overridevirtual

Generates a random Bayesian network.

Parameters
bayesNetempty IBayesNet to generate.
Returns
null but modify inputed Bayesian network

Implements gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >.

Definition at line 116 of file MCBayesNetGenerator_tpl.h.

117 {
119 Timer timer;
120 _createTree_(this->nbrNodes_);
121 _transformPoly_(this->nbrNodes_ / 2);
122 _PMMx_poly_();
123 this->fromDAG(bayesNet);
124
125 this->fromDAG(bayesNet);
126 this->fillCPT(bayesNet);
128 }
void _createTree_(Size BNSize)
The function that randomly generate a simple tree.
void _transformPoly_(Idx nbiter)
The function that randomly change the simple tree into a polytree.

References _createTree_(), _PMMx_poly_(), _transformPoly_(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fillCPT(), gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::fromDAG(), iteration(), iteration_, and gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes_.

Here is the call graph for this function:

◆ generateCPT()

template<GUM_Numeric GUM_SCALAR>
void gum::SimpleCPTGenerator< GUM_SCALAR >::generateCPT ( const Idx & varId,
const Tensor< GUM_SCALAR > & cpt ) const
overrideinherited

Generates a CPT using floats.

Parameters
varIdThe variable id of the CPT owner.
cptA reference on the CPT to fill.

Definition at line 73 of file simpleCPTGenerator_tpl.h.

74 {
75 cpt.random().normalizeAsCPT(varId);
76 }
<agrum/BN/generator/simpleCPTGenerator.h>

◆ iteration()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
Size gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::iteration ( ) const

Return a constant reference to the number of iteration imposed on the Markov Chain BayesNetGenerator.

Definition at line 596 of file MCBayesNetGenerator_tpl.h.

596 {
597 return iteration_;
598 }

References iteration_.

Referenced by MCBayesNetGenerator(), MCBayesNetGenerator(), generateBN(), and setIteration().

Here is the caller graph for this function:

◆ maxArcs()

Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxArcs ( ) const
inherited

Return a constant reference to the maximum number of arcs imposed on the IBayesNetGenerator.

Definition at line 151 of file IBayesNetGenerator_tpl.h.

98 {
99 return maxArcs_;
100 }

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator(), gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator(), and fromDAG().

Here is the caller graph for this function:

◆ maxModality()

Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxModality ( ) const
inherited

Return a constant reference to the maximum modality imposed on the IBayesNetGenerator.

Definition at line 157 of file IBayesNetGenerator_tpl.h.

88 {
89 return maxModality_;
90 }

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator(), gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator(), and ~IBayesNetGenerator().

Here is the caller graph for this function:

◆ nbrNodes()

Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::nbrNodes ( ) const
inherited

Return a constant reference to the number of nodes imposed on the IBayesNetGenerator.

Definition at line 145 of file IBayesNetGenerator_tpl.h.

93 {
94 return nbrNodes_;
95 }

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MCBayesNetGenerator(), and gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::SimpleBayesNetGenerator().

Here is the caller graph for this function:

◆ p()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
Idx gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::p ( ) const

Return a constant reference to the probabilité p imposed on the Markov Chain BayesNetGenerator.

Definition at line 603 of file MCBayesNetGenerator_tpl.h.

603 {
604 return p_;
605 }

References p_.

Referenced by MCBayesNetGenerator(), MCBayesNetGenerator(), and setP().

Here is the caller graph for this function:

◆ q()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
Idx gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::q ( ) const

Return a constant reference to the probabilité imposed on the Markov Chain BayesNetGenerator.

Definition at line 610 of file MCBayesNetGenerator_tpl.h.

610 {
611 return q_;
612 }

References q_.

Referenced by MCBayesNetGenerator(), MCBayesNetGenerator(), and setQ().

Here is the caller graph for this function:

◆ setIteration()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::setIteration ( Size iteration)

Modifies the value of the number of iterations impose on the BayesNetGenerator.

Definition at line 617 of file MCBayesNetGenerator_tpl.h.

618 {
620 }

References iteration(), and iteration_.

Here is the call graph for this function:

◆ setMaxArcs()

void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::setMaxArcs ( Size maxArcs)
inherited

Modifies the value of the number of nodes imposed on the BayesGenerator.

Definition at line 172 of file IBayesNetGenerator_tpl.h.

119 {
121 GUM_ERROR(OperationNotAllowed, " maxArcs value not possible ")
122
124 }

◆ setMaxModality()

void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::setMaxModality ( Size maxModality)
inherited

Modifies the value of the number of nodes imposed on the BayesGenerator.

Definition at line 177 of file IBayesNetGenerator_tpl.h.

103 {
104 if (maxModality < 2)
105 GUM_ERROR(OperationNotAllowed, " maxModality must be at least equal to two ")
106
108 }

◆ setNbrNodes()

void gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::setNbrNodes ( Size nbrNodes)
inherited

Modifies the value of the number of nodes imposed on the BayesGenerator.

Definition at line 167 of file IBayesNetGenerator_tpl.h.

111 {
112 if ((maxArcs_ < nbrNodes - 1) || (maxArcs_ > (nbrNodes * (nbrNodes - 1)) / 2))
113 GUM_ERROR(OperationNotAllowed, " nbrNodes value not possible ")
114
116 }

◆ setP()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::setP ( Idx p)

Modifies the value of the probability p imposed on the BayesNetGenerator.

Definition at line 625 of file MCBayesNetGenerator_tpl.h.

625 {
626 p_ = p;
627
628 if (p + q_ > 100)
630 "the sum of the probabilities p and q must be at most equal to 100");
631 }

References GUM_ERROR, p(), p_, and q_.

Here is the call graph for this function:

◆ setQ()

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::setQ ( Idx q)

Modifies the value of the probability q imposed on the BayesNetGenerator.

Definition at line 636 of file MCBayesNetGenerator_tpl.h.

636 {
637 q_ = q;
638
639 if (p_ + q > 100)
641 "the sum of the probabilities p and q must be at most equal to 100");
642 }

References GUM_ERROR, p_, q(), and q_.

Here is the call graph for this function:

Member Data Documentation

◆ dag_

◆ iteration_

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator = SimpleCPTGenerator, template< typename > class ICPTDisturber = SimpleCPTDisturber>
Size gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::iteration_
protected

◆ maxArcs_

◆ maxModality_

Size gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::maxModality_
protectedinherited

Definition at line 186 of file IBayesNetGenerator.h.

Referenced by ~IBayesNetGenerator(), and fillCPT().

◆ nbrNodes_

◆ p_

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator = SimpleCPTGenerator, template< typename > class ICPTDisturber = SimpleCPTDisturber>
Idx gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::p_
protected

Definition at line 299 of file MCBayesNetGenerator.h.

Referenced by MCBayesNetGenerator(), MCBayesNetGenerator(), p(), setP(), and setQ().

◆ q_

template<GUM_Numeric GUM_SCALAR, template< typename > class ICPTGenerator = SimpleCPTGenerator, template< typename > class ICPTDisturber = SimpleCPTDisturber>
Idx gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::q_
protected

Definition at line 299 of file MCBayesNetGenerator.h.

Referenced by MCBayesNetGenerator(), MCBayesNetGenerator(), q(), setP(), and setQ().


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