aGrUM 2.3.2
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<typename 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<typename 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 70 of file MCBayesNetGenerator_tpl.h.

77 if (p + q > 100)
79 "the sum of the probabilities p and q must be at most equal to 100");
80
82 p_ = p;
83 q_ = q;
84
86 }
<agrum/BN/generator/MCayesNetGenerator.h>
Idx q() const
Return a constant reference to the probabilité 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.
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.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

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<typename 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 91 of file MCBayesNetGenerator_tpl.h.

95 :
97 (Size)(bayesNet.sizeArcs() * 1.1),
100 p_ = p;
101 q_ = q;
102 }

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

Here is the call graph for this function:

◆ ~MCBayesNetGenerator()

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

Destructor.

Definition at line 108 of file MCBayesNetGenerator_tpl.h.

References MCBayesNetGenerator().

Here is the call graph for this function:

Member Function Documentation

◆ _Add_and_Remove_()

template<typename 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 290 of file MCBayesNetGenerator_tpl.h.

290 {
291 NodeId i, j, head, tail;
292 _chooseNodes_(i, j);
293
294 if (this->dag_.existsArc(i, j) || this->dag_.existsArc(j, i)) {
295 return;
296 } else {
297 Idx per = randomValue(100);
298
299 if (per < 50) {
300 head = i;
301 tail = j;
302 } else {
303 head = j;
304 tail = i;
305 }
306
307 for (auto node: this->dag_.parents(j)) {
309 excluded.insert(j);
310
311 if (_is_connected_(node, i, excluded)) {
312 this->dag_.eraseArc(Arc(node, j));
313 this->dag_.addArc(head, tail);
314 return;
315 }
316 }
317
318 for (auto node: this->dag_.children(j)) {
320 excluded.insert(j);
321
322 if (_is_connected_(node, i, excluded)) {
323 this->dag_.eraseArc(Arc{j, node});
324 this->dag_.addArc(head, tail);
325 return;
326 }
327 }
328 }
329 }
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.

References _chooseNodes_().

Here is the call graph for this function:

◆ _Add_or_Remove_()

template<typename 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 277 of file MCBayesNetGenerator_tpl.h.

277 {
278 NodeId i, j;
279 _chooseNodes_(i, j);
280 if (this->dag_.existsArc(i, j)) {
281 _eraseArc_(i, j);
282
283 return;
284 } else _insertArc_(i, j);
285 }
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_(), _insertArc_(), and gum::IBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator >::dag_.

Here is the call graph for this function:

◆ _checkConditions_()

template<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 182 of file MCBayesNetGenerator_tpl.h.

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

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<typename 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 413 of file MCBayesNetGenerator_tpl.h.

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

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<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 398 of file MCBayesNetGenerator_tpl.h.

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

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

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

Here is the caller graph for this function:

◆ _connect_()

template<typename 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 514 of file MCBayesNetGenerator_tpl.h.

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

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<typename 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 465 of file MCBayesNetGenerator_tpl.h.

466 {
467 /*
468 Size nb_mod = 2 + randomValue(this->maxModality_ - 1);
469 std::stringstream strBuff;
470 strBuff << "n_" << n++;
471 NodeId root = this->bayesNet_.add(LabelizedVariable(strBuff.str(), "", nb_mod));
472 */
473 NodeId root = this->dag_.addNode();
474 Size maxNodes = BNSize - 1;
475 Size SubG = 0;
476
477 while (maxNodes) {
481 this->dag_.addArc(root, rootS);
482 }
483
484 return root;
485 }
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<typename 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 447 of file MCBayesNetGenerator_tpl.h.

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

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<typename 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 560 of file MCBayesNetGenerator_tpl.h.

561 {
562 if (this->dag_.existsArc(tail, head)) return true;
563 else {
565 excluded.insert(tail);
566
567 for (auto node: this->dag_.children(tail)) {
568 if (_directedPath_(node, head, excluded)) return true;
569 }
570
571 return false;
572 }
573 }
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<typename 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 578 of file MCBayesNetGenerator_tpl.h.

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

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<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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_(), and _transformPoly_().

Here is the caller graph for this function:

◆ _insertArc_()

template<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 }

Referenced by _Add_or_Remove_().

Here is the caller graph for this function:

◆ _is_connected_()

template<typename 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 536 of file MCBayesNetGenerator_tpl.h.

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

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<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE bool gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_isPolytree_ ( )
protected

The function that verify if graph is a polytree.

Definition at line 507 of file MCBayesNetGenerator_tpl.h.

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

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

◆ _jump_multi_()

template<typename 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 344 of file MCBayesNetGenerator_tpl.h.

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

◆ _jump_poly_()

template<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 334 of file MCBayesNetGenerator_tpl.h.

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

References _chooseNodes_().

Here is the call graph for this function:

◆ _PMMx_multi_()

template<typename 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 235 of file MCBayesNetGenerator_tpl.h.

235 {
236 while (true) {
237 if (!iteration_--) return;
238 DAG tmp_dag = this->dag_;
239
240 Idx per = randomValue(100);
241
242 if (per < p_ + q_) {
244 if (_checkConditions_()) {
245 if (_isPolytree_()) {
246 if (per < p_) {
247 tmp_dag = this->dag_;
248 _PMMx_poly_();
249 break;
250 } else {
251 this->dag_ = tmp_dag;
252 }
253 } else {
254 tmp_dag = this->dag_;
255 }
256 } else {
257 this->dag_ = tmp_dag;
258 }
259 } else {
260 _jump_multi_();
261 if (_checkConditions_()) {
262 tmp_dag = this->dag_;
263 if (_isPolytree_()) {
264 _PMMx_poly_();
265 break;
266 }
267 } else {
268 this->dag_ = tmp_dag;
269 }
270 }
271 }
272 }
bool _isPolytree_()
The function that verify if graph is a polytree.
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...

◆ _PMMx_poly_()

template<typename 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 192 of file MCBayesNetGenerator_tpl.h.

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

Referenced by generateBN().

Here is the caller graph for this function:

◆ _transformPoly_()

template<typename 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 492 of file MCBayesNetGenerator_tpl.h.

492 {
493 while (nbiter--) {
494 NodeId i, j;
496 auto dag_tmp = this->dag_;
497 _eraseArc_(i, j, false);
498 this->dag_.addArc(j, i);
499
500 if (!_checkConditions_()) this->dag_ = dag_tmp;
501 }
502 }
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<typename 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 94 of file simpleCPTDisturber_tpl.h.

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

◆ disturbReducCPT()

template<typename 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 70 of file simpleCPTDisturber_tpl.h.

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

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()

INLINE 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) {
135 strBuff << "X" << std::setfill('0') << std::setw(width) << n++;
136 bn.add(RangeVariable(strBuff.str(), "", 0, long(1 + randomValue(this->maxModality_ - 1))),
137 node);
138 }
139 bn.beginTopologyTransformation();
140 for (auto arc: this->dag_.arcs()) {
141 bn.addArc(arc.tail(), arc.head());
142 }
143 bn.endTopologyTransformation();
144 }

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<typename 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 115 of file MCBayesNetGenerator_tpl.h.

116 {
118 Timer timer;
119 _createTree_(this->nbrNodes_);
120 _transformPoly_(this->nbrNodes_ / 2);
121 _PMMx_poly_();
122 this->fromDAG(bayesNet);
123
124 this->fromDAG(bayesNet);
125 this->fillCPT(bayesNet);
127 }
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<typename 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 72 of file simpleCPTGenerator_tpl.h.

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

◆ iteration()

template<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 597 of file MCBayesNetGenerator_tpl.h.

597 {
598 return iteration_;
599 }

References iteration_.

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

Here is the caller graph for this function:

◆ maxArcs()

INLINE 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()

INLINE 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()

INLINE 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<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 604 of file MCBayesNetGenerator_tpl.h.

604 {
605 return p_;
606 }

References p_.

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

Here is the caller graph for this function:

◆ q()

template<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 611 of file MCBayesNetGenerator_tpl.h.

611 {
612 return q_;
613 }

References q_.

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

Here is the caller graph for this function:

◆ setIteration()

template<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE 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 618 of file MCBayesNetGenerator_tpl.h.

619 {
621 }

References iteration(), and iteration_.

Here is the call graph for this function:

◆ setMaxArcs()

INLINE 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()

INLINE 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()

INLINE 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<typename GUM_SCALAR, template< typename > class ICPTGenerator, template< typename > class ICPTDisturber>
INLINE void gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::setP ( Idx p)

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

Definition at line 626 of file MCBayesNetGenerator_tpl.h.

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

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

Here is the call graph for this function:

◆ setQ()

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

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

Definition at line 637 of file MCBayesNetGenerator_tpl.h.

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

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

Here is the call graph for this function:

Member Data Documentation

◆ dag_

◆ iteration_

template<typename 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<typename 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<typename 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: