aGrUM  0.14.2
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.

Definition at line 50 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 37 of file multiDimFunctionGraphGenerator.cpp.

References __nbTotalVar, and __varSeq.

38  :
39  __varSeq(varSeq) {
40  GUM_CONSTRUCTOR(MultiDimFunctionGraphGenerator);
41 
42  __nbTotalVar = __varSeq.size();
43  }
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.

◆ ~MultiDimFunctionGraphGenerator()

gum::MultiDimFunctionGraphGenerator::~MultiDimFunctionGraphGenerator ( )

Class destructor.

Definition at line 46 of file multiDimFunctionGraphGenerator.cpp.

46  {
47  GUM_DESTRUCTOR(MultiDimFunctionGraphGenerator);
48  }
MultiDimFunctionGraphGenerator(Idx maxVar, Idx minVar, const Sequence< const DiscreteVariable * > &varSeq)
Default constructor.

Member Function Documentation

◆ __createLeaf()

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

Creates a leaf.

Definition at line 144 of file multiDimFunctionGraphGenerator.cpp.

References __nbTotalVar.

Referenced by generate().

145  {
146  return !(currentNodeId == 1
147  || ((double)std::rand() / (double)RAND_MAX
148  < 0.9
149  + 0.01
150  * (float(__nbTotalVar - node2MinVar[currentNodeId])
151  / float(__nbTotalVar))
152  && node2MinVar[currentNodeId] < __nbTotalVar - 1));
153  }
Idx __nbTotalVar
The total number of variables.
+ Here is the caller graph for this function:

◆ __generateVarPos()

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

Generate a variable position.

Definition at line 155 of file multiDimFunctionGraphGenerator.cpp.

References gum::getRandomGenerator().

Referenced by generate().

155  {
156  Idx randOut = 0;
157 
158  if (span != 0) {
159  auto generator = gum::getRandomGenerator();
160  std::weibull_distribution< double > distribution(double(span), 1.0);
161  randOut = (Idx)(distribution(generator) * span / 2);
162  if (randOut > span) randOut = span;
163  }
164 
165  return offset + randOut;
166  }
std::default_random_engine getRandomGenerator(unsigned int seed)
define a random_engine with correct seed
Size Idx
Type for indexes.
Definition: types.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generate()

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

Generates a MultiDimFunctionGraph.

Definition at line 50 of file multiDimFunctionGraphGenerator.cpp.

References __createLeaf(), __generateVarPos(), __nbTotalVar, __varSeq, gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::add(), gum::MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >::addInternalNode(), gum::LinkedList< T >::addLink(), gum::MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >::addTerminalNode(), gum::MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >::clean(), gum::DiscreteVariable::domainSize(), gum::Link< T >::element(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::getReducedAndOrderedInstance(), gum::HashTable< Key, Val, Alloc >::insert(), gum::LinkedList< T >::list(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::manager(), gum::Link< T >::nextLink(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::node(), gum::InternalNode::nodeVar(), gum::MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >::reduce(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::root(), gum::MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >::setRootNode(), gum::MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >::setSon(), gum::InternalNode::son(), gum::MultiDimImplementation< GUM_SCALAR >::variablesSequence(), and gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::varNodeListe().

50  {
51  MultiDimFunctionGraph< double >* generatedFunctionGraph =
53 
54  for (SequenceIteratorSafe< const DiscreteVariable* > varIter =
55  __varSeq.beginSafe();
56  varIter != __varSeq.endSafe();
57  ++varIter) {
58  generatedFunctionGraph->add(**varIter);
59  }
60 
61  HashTable< NodeId, Idx > node2MinVar;
62  Idx nbIter = 0;
63 
64  std::vector< NodeId > filo;
65 
66  generatedFunctionGraph->manager()->setRootNode(
67  generatedFunctionGraph->manager()->addInternalNode(__varSeq.atPos(0)));
68  node2MinVar.insert(generatedFunctionGraph->root(), 0);
69  filo.push_back(generatedFunctionGraph->root());
70 
71  while (!filo.empty()) { //&& nbIter < 20 ){
72 
73  NodeId currentNodeId = filo.back();
74  filo.pop_back();
75  Idx cvp = node2MinVar[currentNodeId];
76  const InternalNode* currentNode =
77  generatedFunctionGraph->node(currentNodeId);
78 
79  LinkedList< NodeId > potentialSons;
80  Idx nbPotentialSons = 0;
81  for (Idx varPos = 0;
82  varPos < generatedFunctionGraph->variablesSequence().size();
83  varPos++) {
84  const DiscreteVariable* var =
85  generatedFunctionGraph->variablesSequence().atPos(varPos);
86 
87  Idx vsp = __varSeq.pos(var);
88  if (vsp > cvp) {
89  const Link< NodeId >* nicleIter =
90  generatedFunctionGraph->varNodeListe(var)->list();
91  while (nicleIter) {
92  nbPotentialSons++;
93  potentialSons.addLink(nicleIter->element());
94  nicleIter = nicleIter->nextLink();
95  }
96  }
97  }
98 
99  for (Idx modality = 0; modality < currentNode->nodeVar()->domainSize();
100  modality++) {
101  if (!potentialSons.list()
102  || (double)std::rand() / (double)RAND_MAX
103  > (1.0 / (1.0 + 3.0 / nbPotentialSons))) {
104  if (__createLeaf(currentNodeId, node2MinVar)) {
105  generatedFunctionGraph->manager()->setSon(
106  currentNodeId,
107  modality,
108  generatedFunctionGraph->manager()->addTerminalNode(
109  (double)std::rand() / (double)RAND_MAX * 100));
110  } else {
111  Idx sonVarPos =
112  __generateVarPos(node2MinVar[currentNodeId] + 1,
113  __nbTotalVar - node2MinVar[currentNodeId] - 2);
114  generatedFunctionGraph->manager()->setSon(
115  currentNodeId,
116  modality,
117  generatedFunctionGraph->manager()->addInternalNode(
118  __varSeq.atPos(sonVarPos)));
119  filo.push_back(currentNode->son(modality));
120  node2MinVar.insert(currentNode->son(modality), sonVarPos);
121  }
122  } else {
123  Idx sonPos =
124  (Idx)(((double)std::rand() / (double)RAND_MAX) * nbPotentialSons);
125  sonPos = sonPos == nbPotentialSons ? nbPotentialSons - 1 : sonPos;
126  Link< NodeId >* nicleIter = potentialSons.list();
127  while (sonPos) {
128  nicleIter = nicleIter->nextLink();
129  sonPos--;
130  }
131  generatedFunctionGraph->manager()->setSon(
132  currentNodeId, modality, nicleIter->element());
133  }
134  }
135  ++nbIter;
136  }
137 
138  generatedFunctionGraph->manager()->reduce();
139  generatedFunctionGraph->manager()->clean();
140 
141  return generatedFunctionGraph;
142  }
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.
Idx __nbTotalVar
The total number of variables.
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.
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:50
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.
Idx __generateVarPos(Idx offset, Idx span)
Generate a variable position.
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 94 of file multiDimFunctionGraphGenerator.h.

◆ __nbTotalVar

Idx gum::MultiDimFunctionGraphGenerator::__nbTotalVar
private

The total number of variables.

Definition at line 91 of file multiDimFunctionGraphGenerator.h.

Referenced by __createLeaf(), generate(), and MultiDimFunctionGraphGenerator().

◆ __varSeq

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

The variables.

Definition at line 88 of file multiDimFunctionGraphGenerator.h.

Referenced by generate(), and MultiDimFunctionGraphGenerator().


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