aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::MultiDimFunctionGraphGenerator Class Reference

Class implementing a function graph generator with template type double. More...

#include <multiDimFunctionGraphGenerator.h>

+ Collaboration diagram for gum::MultiDimFunctionGraphGenerator:

Public Member Functions

MultiDimFunctionGraph< double > * generate ()
 Generates a MultiDimFunctionGraph. More...
 
Constructors / Destructors
 MultiDimFunctionGraphGenerator (Idx maxVar, Idx minVar, const Sequence< const DiscreteVariable * > &varSeq)
 Default constructor. More...
 
 ~MultiDimFunctionGraphGenerator ()
 Class destructor. More...
 

Detailed Description

Class implementing a function graph generator with template type double.

Warning
Doxygen does not like spanning command on multiple line, so we could not configure it with the correct include directive. Use the following code snippet to include this file.
#include
<agrum/tools/multidim/implementations/multiDimFunctionGraphGenerator.h>

Definition at line 54 of file multiDimFunctionGraphGenerator.h.

Constructor & Destructor Documentation

◆ MultiDimFunctionGraphGenerator()

gum::MultiDimFunctionGraphGenerator::MultiDimFunctionGraphGenerator ( Idx  maxVar,
Idx  minVar,
const Sequence< const DiscreteVariable * > &  varSeq 
)

Default constructor.

Definition at line 40 of file multiDimFunctionGraphGenerator.cpp.

References gum::Set< Key, Alloc >::emplace().

43  :
44  _varSeq_(varSeq) {
45  GUM_CONSTRUCTOR(MultiDimFunctionGraphGenerator);
46 
47  _nbTotalVar_ = _varSeq_.size();
48  }
Idx _nbTotalVar_
The total number of variables.
const Sequence< const DiscreteVariable *> _varSeq_
The variables.
MultiDimFunctionGraphGenerator(Idx maxVar, Idx minVar, const Sequence< const DiscreteVariable * > &varSeq)
Default constructor.
+ Here is the call graph for this function:

◆ ~MultiDimFunctionGraphGenerator()

gum::MultiDimFunctionGraphGenerator::~MultiDimFunctionGraphGenerator ( )

Class destructor.

Definition at line 51 of file multiDimFunctionGraphGenerator.cpp.

References gum::Set< Key, Alloc >::emplace().

51  {
52  GUM_DESTRUCTOR(MultiDimFunctionGraphGenerator);
53  }
MultiDimFunctionGraphGenerator(Idx maxVar, Idx minVar, const Sequence< const DiscreteVariable * > &varSeq)
Default constructor.
+ Here is the call graph for this function:

Member Function Documentation

◆ _createLeaf_()

bool gum::MultiDimFunctionGraphGenerator::_createLeaf_ ( NodeId  currentNodeId,
HashTable< NodeId, Idx > &  node2MinVar 
)
private

Creates a leaf.

Definition at line 137 of file multiDimFunctionGraphGenerator.cpp.

References gum::Set< Key, Alloc >::emplace().

138  {
139  return !(
140  currentNodeId == 1
141  || ((double)std::rand() / (double)RAND_MAX
142  < 0.9
143  + 0.01 * (float(_nbTotalVar_ - node2MinVar[currentNodeId]) / float(_nbTotalVar_))
144  && node2MinVar[currentNodeId] < _nbTotalVar_ - 1));
145  }
Idx _nbTotalVar_
The total number of variables.
+ Here is the call graph for this function:

◆ _generateVarPos_()

Idx gum::MultiDimFunctionGraphGenerator::_generateVarPos_ ( Idx  offset,
Idx  span 
)
private

Generate a variable position.

Definition at line 147 of file multiDimFunctionGraphGenerator.cpp.

References gum::Set< Key, Alloc >::emplace().

147  {
148  Idx randOut = 0;
149 
150  if (span != 0) {
151  auto generator = gum::getRandomGenerator();
152  std::weibull_distribution< double > distribution(double(span), 1.0);
153  randOut = (Idx)(distribution(generator) * span / 2);
154  if (randOut > span) randOut = span;
155  }
156 
157  return offset + randOut;
158  }
std::default_random_engine getRandomGenerator(unsigned int seed)
define a random_engine with correct seed
Size Idx
Type for indexes.
Definition: types.h:52
+ Here is the call graph for this function:

◆ generate()

MultiDimFunctionGraph< double > * gum::MultiDimFunctionGraphGenerator::generate ( )

Generates a MultiDimFunctionGraph.

Definition at line 55 of file multiDimFunctionGraphGenerator.cpp.

References gum::Set< Key, Alloc >::emplace().

55  {
56  MultiDimFunctionGraph< double >* generatedFunctionGraph
58 
59  for (SequenceIteratorSafe< const DiscreteVariable* > varIter = _varSeq_.beginSafe();
60  varIter != _varSeq_.endSafe();
61  ++varIter) {
62  generatedFunctionGraph->add(**varIter);
63  }
64 
65  HashTable< NodeId, Idx > node2MinVar;
66  Idx nbIter = 0;
67 
68  std::vector< NodeId > filo;
69 
70  generatedFunctionGraph->manager()->setRootNode(
71  generatedFunctionGraph->manager()->addInternalNode(_varSeq_.atPos(0)));
72  node2MinVar.insert(generatedFunctionGraph->root(), 0);
73  filo.push_back(generatedFunctionGraph->root());
74 
75  while (!filo.empty()) { //&& nbIter < 20 ){
76 
77  NodeId currentNodeId = filo.back();
78  filo.pop_back();
79  Idx cvp = node2MinVar[currentNodeId];
80  const InternalNode* currentNode = generatedFunctionGraph->node(currentNodeId);
81 
82  LinkedList< NodeId > potentialSons;
83  Idx nbPotentialSons = 0;
84  for (Idx varPos = 0; varPos < generatedFunctionGraph->variablesSequence().size(); varPos++) {
85  const DiscreteVariable* var = generatedFunctionGraph->variablesSequence().atPos(varPos);
86 
87  Idx vsp = _varSeq_.pos(var);
88  if (vsp > cvp) {
89  const Link< NodeId >* nicleIter = generatedFunctionGraph->varNodeListe(var)->list();
90  while (nicleIter) {
91  nbPotentialSons++;
92  potentialSons.addLink(nicleIter->element());
93  nicleIter = nicleIter->nextLink();
94  }
95  }
96  }
97 
98  for (Idx modality = 0; modality < currentNode->nodeVar()->domainSize(); modality++) {
99  if (!potentialSons.list()
100  || (double)std::rand() / (double)RAND_MAX > (1.0 / (1.0 + 3.0 / nbPotentialSons))) {
101  if (_createLeaf_(currentNodeId, node2MinVar)) {
102  generatedFunctionGraph->manager()->setSon(
103  currentNodeId,
104  modality,
105  generatedFunctionGraph->manager()->addTerminalNode((double)std::rand()
106  / (double)RAND_MAX * 100));
107  } else {
108  Idx sonVarPos = _generateVarPos_(node2MinVar[currentNodeId] + 1,
109  _nbTotalVar_ - node2MinVar[currentNodeId] - 2);
110  generatedFunctionGraph->manager()->setSon(
111  currentNodeId,
112  modality,
113  generatedFunctionGraph->manager()->addInternalNode(_varSeq_.atPos(sonVarPos)));
114  filo.push_back(currentNode->son(modality));
115  node2MinVar.insert(currentNode->son(modality), sonVarPos);
116  }
117  } else {
118  Idx sonPos = (Idx)(((double)std::rand() / (double)RAND_MAX) * nbPotentialSons);
119  sonPos = sonPos == nbPotentialSons ? nbPotentialSons - 1 : sonPos;
120  Link< NodeId >* nicleIter = potentialSons.list();
121  while (sonPos) {
122  nicleIter = nicleIter->nextLink();
123  sonPos--;
124  }
125  generatedFunctionGraph->manager()->setSon(currentNodeId, modality, nicleIter->element());
126  }
127  }
128  ++nbIter;
129  }
130 
131  generatedFunctionGraph->manager()->reduce();
132  generatedFunctionGraph->manager()->clean();
133 
134  return generatedFunctionGraph;
135  }
Idx _nbTotalVar_
The total number of variables.
void setSon(const NodeId &node, const Idx &modality, const NodeId &sonNode)
Sets nodes son for given modality to designated son node.
void clean()
Removes var without nodes in the diagram.
void setRootNode(const NodeId &root)
Sets root node of decision diagram.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
NodeId addInternalNode(const DiscreteVariable *var)
Inserts a new non terminal node in graph.
bool _createLeaf_(NodeId currentNodeId, HashTable< NodeId, Idx > &node2MinVar)
Creates a leaf.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
const Sequence< const DiscreteVariable *> _varSeq_
The variables.
Idx _generateVarPos_(Idx offset, Idx span)
Generate a variable position.
const NodeId & root() const
Returns the id of the root node from the diagram.
const LinkedList< NodeId > * varNodeListe(const DiscreteVariable *var) const
Returns the list of node associated to given variable.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
NodeId addTerminalNode(const GUM_SCALAR &value)
Adds a value to the MultiDimFunctionGraph.
Size Idx
Type for indexes.
Definition: types.h:52
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
virtual void reduce()=0
Ensures that every isomorphic subgraphs are merged together.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
+ Here is the call graph for this function:

Member Data Documentation

◆ _genSeed_

Idx gum::MultiDimFunctionGraphGenerator::_genSeed_
staticprivate

The seed for random numbers.

Definition at line 99 of file multiDimFunctionGraphGenerator.h.

◆ _nbTotalVar_

Idx gum::MultiDimFunctionGraphGenerator::_nbTotalVar_
private

The total number of variables.

Definition at line 96 of file multiDimFunctionGraphGenerator.h.

◆ _varSeq_

const Sequence< const DiscreteVariable* > gum::MultiDimFunctionGraphGenerator::_varSeq_
private

The variables.

Definition at line 93 of file multiDimFunctionGraphGenerator.h.


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