aGrUM  0.20.2
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  }
const Sequence< const DiscreteVariable *> varSeq__
The variables.
MultiDimFunctionGraphGenerator(Idx maxVar, Idx minVar, const Sequence< const DiscreteVariable * > &varSeq)
Default constructor.
Idx nbTotalVar__
The total number of variables.
+ 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 150 of file multiDimFunctionGraphGenerator.cpp.

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

152  {
153  return !(currentNodeId == 1
154  || ((double)std::rand() / (double)RAND_MAX
155  < 0.9
156  + 0.01
157  * (float(nbTotalVar__ - node2MinVar[currentNodeId])
158  / float(nbTotalVar__))
159  && node2MinVar[currentNodeId] < nbTotalVar__ - 1));
160  }
Idx nbTotalVar__
The total number of variables.
+ 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
60  = varSeq__.beginSafe();
61  varIter != varSeq__.endSafe();
62  ++varIter) {
63  generatedFunctionGraph->add(**varIter);
64  }
65 
66  HashTable< NodeId, Idx > node2MinVar;
67  Idx nbIter = 0;
68 
69  std::vector< NodeId > filo;
70 
71  generatedFunctionGraph->manager()->setRootNode(
72  generatedFunctionGraph->manager()->addInternalNode(varSeq__.atPos(0)));
73  node2MinVar.insert(generatedFunctionGraph->root(), 0);
74  filo.push_back(generatedFunctionGraph->root());
75 
76  while (!filo.empty()) { //&& nbIter < 20 ){
77 
78  NodeId currentNodeId = filo.back();
79  filo.pop_back();
80  Idx cvp = node2MinVar[currentNodeId];
81  const InternalNode* currentNode
82  = generatedFunctionGraph->node(currentNodeId);
83 
84  LinkedList< NodeId > potentialSons;
85  Idx nbPotentialSons = 0;
86  for (Idx varPos = 0;
87  varPos < generatedFunctionGraph->variablesSequence().size();
88  varPos++) {
89  const DiscreteVariable* var
90  = generatedFunctionGraph->variablesSequence().atPos(varPos);
91 
92  Idx vsp = varSeq__.pos(var);
93  if (vsp > cvp) {
94  const Link< NodeId >* nicleIter
95  = generatedFunctionGraph->varNodeListe(var)->list();
96  while (nicleIter) {
97  nbPotentialSons++;
98  potentialSons.addLink(nicleIter->element());
99  nicleIter = nicleIter->nextLink();
100  }
101  }
102  }
103 
104  for (Idx modality = 0; modality < currentNode->nodeVar()->domainSize();
105  modality++) {
106  if (!potentialSons.list()
107  || (double)std::rand() / (double)RAND_MAX
108  > (1.0 / (1.0 + 3.0 / nbPotentialSons))) {
109  if (createLeaf__(currentNodeId, node2MinVar)) {
110  generatedFunctionGraph->manager()->setSon(
111  currentNodeId,
112  modality,
113  generatedFunctionGraph->manager()->addTerminalNode(
114  (double)std::rand() / (double)RAND_MAX * 100));
115  } else {
116  Idx sonVarPos
117  = generateVarPos__(node2MinVar[currentNodeId] + 1,
118  nbTotalVar__ - node2MinVar[currentNodeId] - 2);
119  generatedFunctionGraph->manager()->setSon(
120  currentNodeId,
121  modality,
122  generatedFunctionGraph->manager()->addInternalNode(
123  varSeq__.atPos(sonVarPos)));
124  filo.push_back(currentNode->son(modality));
125  node2MinVar.insert(currentNode->son(modality), sonVarPos);
126  }
127  } else {
128  Idx sonPos
129  = (Idx)(((double)std::rand() / (double)RAND_MAX) * nbPotentialSons);
130  sonPos = sonPos == nbPotentialSons ? nbPotentialSons - 1 : sonPos;
131  Link< NodeId >* nicleIter = potentialSons.list();
132  while (sonPos) {
133  nicleIter = nicleIter->nextLink();
134  sonPos--;
135  }
136  generatedFunctionGraph->manager()->setSon(currentNodeId,
137  modality,
138  nicleIter->element());
139  }
140  }
141  ++nbIter;
142  }
143 
144  generatedFunctionGraph->manager()->reduce();
145  generatedFunctionGraph->manager()->clean();
146 
147  return generatedFunctionGraph;
148  }
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 Sequence< const DiscreteVariable *> varSeq__
The variables.
Idx generateVarPos__(Idx offset, Idx span)
Generate a variable position.
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.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
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.
bool createLeaf__(NodeId currentNodeId, HashTable< NodeId, Idx > &node2MinVar)
Creates a leaf.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
Idx nbTotalVar__
The total number of variables.
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:

◆ generateVarPos__()

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

Generate a variable position.

Definition at line 162 of file multiDimFunctionGraphGenerator.cpp.

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

162  {
163  Idx randOut = 0;
164 
165  if (span != 0) {
166  auto generator = gum::getRandomGenerator();
167  std::weibull_distribution< double > distribution(double(span), 1.0);
168  randOut = (Idx)(distribution(generator) * span / 2);
169  if (randOut > span) randOut = span;
170  }
171 
172  return offset + randOut;
173  }
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:

Member Data Documentation

◆ genSeed__

Idx gum::MultiDimFunctionGraphGenerator::genSeed__
staticprivate

The seed for random numbers.

Definition at line 100 of file multiDimFunctionGraphGenerator.h.

◆ nbTotalVar__

Idx gum::MultiDimFunctionGraphGenerator::nbTotalVar__
private

The total number of variables.

Definition at line 97 of file multiDimFunctionGraphGenerator.h.

◆ varSeq__

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

The variables.

Definition at line 94 of file multiDimFunctionGraphGenerator.h.


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