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

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

#include <maxParentsMCBayesNetGenerator.h>

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

Public Member Functions

Constructors / Destructor
 MaxParentsMCBayesNetGenerator (Size nbrNodes, Size maxArcs, Size maxModality=2, Size maxParents=1, Idx iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
 Constructor.
 MaxParentsMCBayesNetGenerator (BayesNet< GUM_SCALAR > bayesNet, Size maxParents=2, Idx iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
 Constructor.
 ~MaxParentsMCBayesNetGenerator () override
 Destructor.
Getters
Size maxParents () const
 Return a constant reference to the number of maximum parents imposed on the Markov Chain BayesNetGenerator.
Setters
void setMaxParents (Size maxParents)
 Modifies the value of the number of maximum parents imposed on the BayesNetGenerator.
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.
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.
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 maxParents_
Size iteration_
Idx p_
Idx q_
Size nbrNodes_
Size maxArcs_
Size maxModality_
DAG dag_

Private Member Functions

bool _checkConditions_ () final
 function to holding the specification wanted for the Bayesian network.

Detailed Description

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

<agrum/BN/generator/SimpleMCayesNetGenerator.h>

Class for generating Bayesian networks with Markov chains.

This class is inherited from MCBayesNetGenerator and is an example of Markov Chain Bayesian network Generator that can be implemented. Here a constraint is added which is the maximum number of parents that a node can have. To create our own MCBayesNetGenerator with a specific constraint you must:
1) Create the class XXXXXXMCBayesNetGenerator<GUM_SCALAR,ICPTGenerator,ICPTDisturber>.
2) Add a variable specific to the constraint (here maxParents_).
3) Verify in the function checkCondition_() the wanted constraint and then launch MCBayesNetGenerator<GUM_SCALAR,ICPTGenerator,ICPTDisturber>:: checkConditions() to verify the default constraint.
4) Define the getters and setters of the constraint variable.
5) And verify in the constructor that the constraint is not in contradiction with the generator here maxParent must be >= 1.

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.

Definition at line 97 of file maxParentsMCBayesNetGenerator.h.

Constructor & Destructor Documentation

◆ MaxParentsMCBayesNetGenerator() [1/2]

template<GUM_Numeric GUM_SCALAR, template< class > class ICPTGenerator, template< class > class ICPTDisturber>
gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MaxParentsMCBayesNetGenerator ( Size nbrNodes,
Size maxArcs,
Size maxModality = 2,
Size maxParents = 1,
Idx 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
maxParentsThe number of maximum parents for each node imposed on the generator
iterationThe number of iterations wanted to repeat the algorithm
pprobability for the change of the state (see useof p and q )
qprobability for the change of the state (see useof p and q )

Definition at line 60 of file maxParentsMCBayesNetGenerator_tpl.h.

67 :
69 maxArcs,
72 p,
73 q) {
74 if (maxParents == 0)
76 "maxParents must be at least equal to 1 to have a connexe graph")
77
80 }
MCBayesNetGenerator(Size nbrNodes, Size maxArcs, Idx maxModality=2, Size iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
<agrum/BN/generator/SimpleMCayesNetGenerator.h>
MaxParentsMCBayesNetGenerator(Size nbrNodes, Size maxArcs, Size maxModality=2, Size maxParents=1, Idx iteration=NB_INIT_ITERATIONS, Idx p=30, Idx q=40)
Constructor.
Size maxParents() const
Return a constant reference to the number of maximum parents imposed on the Markov Chain BayesNetGene...
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References MaxParentsMCBayesNetGenerator(), gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::MCBayesNetGenerator(), GUM_ERROR, gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::iteration(), gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::maxArcs(), gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::maxModality(), maxParents(), maxParents_, gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::nbrNodes(), gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::p(), and gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::q().

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

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

◆ MaxParentsMCBayesNetGenerator() [2/2]

template<GUM_Numeric GUM_SCALAR, template< class > class ICPTGenerator, template< class > class ICPTDisturber>
gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::MaxParentsMCBayesNetGenerator ( BayesNet< GUM_SCALAR > bayesNet,
Size maxParents = 2,
Idx 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
maxParentsThe number of maximum parents for each node imposed on the generator
iterationThe number of iterations wanted to repeat the algorithm
pprobability for the change of the state (see useof p and q )
qprobability for the change of the state (see useof p and q )

Definition at line 85 of file maxParentsMCBayesNetGenerator_tpl.h.

References MaxParentsMCBayesNetGenerator(), gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::MCBayesNetGenerator(), gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::iteration(), maxParents(), maxParents_, gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::p(), and gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::q().

Here is the call graph for this function:

◆ ~MaxParentsMCBayesNetGenerator()

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

Destructor.

Definition at line 100 of file maxParentsMCBayesNetGenerator_tpl.h.

References MaxParentsMCBayesNetGenerator().

Here is the call graph for this function:

Member Function Documentation

◆ _Add_and_Remove_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_Add_and_Remove_ ( )
protectedinherited

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

Definition at line 369 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
<agrum/BN/generator/MCayesNetGenerator.h>
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.

Referenced by MCBayesNetGenerator().

Here is the caller graph for this function:

◆ _Add_or_Remove_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_Add_or_Remove_ ( )
protectedinherited

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

Definition at line 363 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 }

References dag_.

Referenced by MCBayesNetGenerator(), and generateBN().

Here is the caller graph for this function:

◆ _checkConditions_()

template<GUM_Numeric GUM_SCALAR, template< class > class ICPTGenerator, template< class > class ICPTDisturber>
bool gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_checkConditions_ ( )
finalprivatevirtual

function to holding the specification wanted for the Bayesian network.

Returns
boolean state that verify the conditions

Reimplemented from gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >.

Definition at line 108 of file maxParentsMCBayesNetGenerator_tpl.h.

109 {
110 for (auto node: this->dag_.nodes())
111 if (this->dag_.parents(node).size() > maxParents_) return false;
112
114 }
virtual bool _checkConditions_()
The boolean function that will assert the respect of the constraint.

References gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_checkConditions_(), gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::dag_, and maxParents_.

Here is the call graph for this function:

◆ _chooseCloseNodes_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_chooseCloseNodes_ ( NodeId & i,
NodeId & j )
protectedinherited

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

Definition at line 401 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 }

◆ _chooseNodes_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_chooseNodes_ ( NodeId & i,
NodeId & j )
protectedinherited

The function that randomly choose two nodes of the graph.

Definition at line 396 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 }

Referenced by setQ().

Here is the caller graph for this function:

◆ _connect_()

bool gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_connect_ ( NodeId i,
NodeId j )
protectedinherited

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

Definition at line 312 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_(), and dag_.

Referenced by _is_connected_().

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

◆ _createPartTree_()

NodeId gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_createPartTree_ ( Size BNSize,
Idx & n )
protectedinherited

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

n : id number for node label

Definition at line 420 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 }

References dag_.

◆ _createTree_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_createTree_ ( Size BNSize)
protectedinherited

The function that randomly generate a simple tree.

Definition at line 412 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 }

◆ _directedPath_()

bool gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_directedPath_ ( NodeId tail,
NodeId head )
protectedinherited

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

Definition at line 317 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 }

Referenced by _jump_multi_().

Here is the caller graph for this function:

◆ _eraseArc_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_eraseArc_ ( NodeId i,
NodeId j,
bool mustbeconnex = true )
protectedinherited

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 330 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 }

◆ _insertArc_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_insertArc_ ( NodeId i,
NodeId j )
protectedinherited

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 323 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 }

References _is_connected_(), and dag_.

Referenced by _PMMx_poly_().

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

◆ _is_connected_()

bool gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_is_connected_ ( const NodeId i,
const NodeId j,
NodeSet & excluded )
protectedinherited

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 383 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 _connect_(), and dag_.

Referenced by _connect_(), and _insertArc_().

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

◆ _isPolytree_()

bool gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_isPolytree_ ( )
protectedinherited

The function that verify if graph is a polytree.

Definition at line 308 of file MCBayesNetGenerator_tpl.h.

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

References dag_, and gum::Set< Key >::insert().

Referenced by generateBN().

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

◆ _jump_multi_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_jump_multi_ ( )
protectedinherited

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 357 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 _directedPath_().

Referenced by generateBN().

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

◆ _jump_poly_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_jump_poly_ ( )
protectedinherited

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 350 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 }

Referenced by MCBayesNetGenerator().

Here is the caller graph for this function:

◆ _PMMx_multi_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_PMMx_multi_ ( )
protectedinherited

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 345 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 }

Referenced by MCBayesNetGenerator().

Here is the caller graph for this function:

◆ _PMMx_poly_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_PMMx_poly_ ( )
protectedinherited

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 338 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 }

References _insertArc_(), and dag_.

Referenced by generateBN().

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

◆ _transformPoly_()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::_transformPoly_ ( Idx nbiter)
protectedinherited

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

Definition at line 407 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 }

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

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::generateBN ( BayesNet< GUM_SCALAR > & bayesNet)
overridevirtualinherited

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

References _Add_or_Remove_(), _checkConditions_(), _isPolytree_(), _jump_multi_(), _PMMx_poly_(), dag_, iteration_, p_, q_, and gum::randomValue().

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

Size gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::iteration ( ) const
inherited

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

◆ maxParents()

template<GUM_Numeric GUM_SCALAR, template< class > class ICPTGenerator, template< class > class ICPTDisturber>
Size gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::maxParents ( ) const

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

Definition at line 119 of file maxParentsMCBayesNetGenerator_tpl.h.

120 {
121 return maxParents_;
122 }

References maxParents_.

Referenced by MaxParentsMCBayesNetGenerator(), MaxParentsMCBayesNetGenerator(), and setMaxParents().

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

◆ q()

◆ setIteration()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::setIteration ( Size iteration)
inherited

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

Definition at line 285 of file MCBayesNetGenerator_tpl.h.

618 {
620 }

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

◆ setMaxParents()

template<GUM_Numeric GUM_SCALAR, template< class > class ICPTGenerator, template< class > class ICPTDisturber>
void gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::setMaxParents ( Size maxParents)

Modifies the value of the number of maximum parents imposed on the BayesNetGenerator.

Definition at line 127 of file maxParentsMCBayesNetGenerator_tpl.h.

128 {
129 if (maxParents == 0)
131 "maxParents must be at least equal to 1 to have a connexe graph")
132
134 }

References GUM_ERROR, maxParents(), and maxParents_.

Here is the call graph for this function:

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

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::setP ( Idx p)
inherited

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

Definition at line 289 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 }

◆ setQ()

void gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::setQ ( Idx q)
inherited

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

Definition at line 293 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 _chooseNodes_(), and dag_.

Here is the call graph for this function:

Member Data Documentation

◆ dag_

◆ iteration_

Size gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::iteration_
protectedinherited

Definition at line 298 of file MCBayesNetGenerator.h.

Referenced by MCBayesNetGenerator(), and generateBN().

◆ maxArcs_

◆ maxModality_

Definition at line 186 of file IBayesNetGenerator.h.

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

◆ maxParents_

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

◆ nbrNodes_

◆ p_

Idx gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::p_
protectedinherited

Definition at line 299 of file MCBayesNetGenerator.h.

Referenced by MCBayesNetGenerator(), and generateBN().

◆ q_

Idx gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::q_
protectedinherited

Definition at line 299 of file MCBayesNetGenerator.h.

Referenced by MCBayesNetGenerator(), and generateBN().


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