aGrUM 2.3.2
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 ()
 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 the specification wanted for the Bayesian markov.

Detailed Description

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

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

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<typename GUM_SCALAR, template< class > class ICPTGenerator, template< class > class ICPTDisturber>
INLINE 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 84 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<typename GUM_SCALAR, template< class > class ICPTGenerator, template< class > class ICPTDisturber>
INLINE gum::MaxParentsMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::~MaxParentsMCBayesNetGenerator ( )

Destructor.

Definition at line 99 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.

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

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

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 }

References dag_.

Referenced by MCBayesNetGenerator(), and generateBN().

Here is the caller graph for this function:

◆ _checkConditions_()

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

function to holding the the specification wanted for the Bayesian markov.

Returns
boolean state that verify the conditions

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

Definition at line 107 of file maxParentsMCBayesNetGenerator_tpl.h.

108 {
109 for (auto node: this->dag_.nodes())
110 if (this->dag_.parents(node).size() > maxParents_) return false;
111
113 }
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.

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

◆ _chooseNodes_()

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

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 }

Referenced by _PMMx_multi_().

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.

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

Referenced by _is_connected_().

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.

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 }

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.

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 }

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

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 }

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

Referenced by _jump_multi_().

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

◆ _eraseArc_()

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

Referenced by _PMMx_multi_().

Here is the caller graph for this function:

◆ _insertArc_()

INLINE 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 dag_.

Referenced by _PMMx_poly_().

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.

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

Referenced by _directedPath_(), and _isPolytree_().

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

◆ _isPolytree_()

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

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

References _is_connected_().

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.

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

References _directedPath_().

Here is the call graph for this function:

◆ _jump_poly_()

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

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

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

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 }

References _chooseNodes_(), _eraseArc_(), and dag_.

Referenced by MCBayesNetGenerator().

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

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 }

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.

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 }

References dag_.

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

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_(), _PMMx_poly_(), dag_, iteration_, p_, and q_.

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

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

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

◆ maxParents()

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

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

INLINE Idx gum::MCBayesNetGenerator< GUM_SCALAR, SimpleCPTGenerator, SimpleCPTDisturber >::p ( ) const
inherited

◆ q()

◆ setIteration()

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

619 {
621 }

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

◆ setMaxParents()

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

129 {
130 if (maxParents == 0)
132 "maxParents must be at least equal to 1 to have a connexe graph")
133
135 }

References GUM_ERROR, maxParents(), and maxParents_.

Here is the call graph for this function:

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

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

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 }

◆ setQ()

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

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

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<typename 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 generateBN().


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