aGrUM  0.16.0
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 53 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 __nbTotalVar, and __varSeq.

41  :
42  __varSeq(varSeq) {
43  GUM_CONSTRUCTOR(MultiDimFunctionGraphGenerator);
44 
45  __nbTotalVar = __varSeq.size();
46  }
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 49 of file multiDimFunctionGraphGenerator.cpp.

49  {
50  GUM_DESTRUCTOR(MultiDimFunctionGraphGenerator);
51  }
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 147 of file multiDimFunctionGraphGenerator.cpp.

References __nbTotalVar.

Referenced by generate().

148  {
149  return !(currentNodeId == 1
150  || ((double)std::rand() / (double)RAND_MAX
151  < 0.9
152  + 0.01
153  * (float(__nbTotalVar - node2MinVar[currentNodeId])
154  / float(__nbTotalVar))
155  && node2MinVar[currentNodeId] < __nbTotalVar - 1));
156  }
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 158 of file multiDimFunctionGraphGenerator.cpp.

References gum::getRandomGenerator().

Referenced by generate().

158  {
159  Idx randOut = 0;
160 
161  if (span != 0) {
162  auto generator = gum::getRandomGenerator();
163  std::weibull_distribution< double > distribution(double(span), 1.0);
164  randOut = (Idx)(distribution(generator) * span / 2);
165  if (randOut > span) randOut = span;
166  }
167 
168  return offset + randOut;
169  }
std::default_random_engine getRandomGenerator(unsigned int seed)
define a random_engine with correct seed
Size Idx
Type for indexes.
Definition: types.h:53
+ 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 53 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().

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

◆ __nbTotalVar

Idx gum::MultiDimFunctionGraphGenerator::__nbTotalVar
private

The total number of variables.

Definition at line 94 of file multiDimFunctionGraphGenerator.h.

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

◆ __varSeq

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

The variables.

Definition at line 91 of file multiDimFunctionGraphGenerator.h.

Referenced by generate(), and MultiDimFunctionGraphGenerator().


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