aGrUM  0.16.0
multiDimFunctionGraphGenerator.cpp
Go to the documentation of this file.
1 
32 #include <cstdlib>
33 #include <random>
34 
37 
38 namespace gum {
39  // Constructor
41  Idx maxVar, Idx minVar, const Sequence< const DiscreteVariable* >& varSeq) :
42  __varSeq(varSeq) {
43  GUM_CONSTRUCTOR(MultiDimFunctionGraphGenerator);
44 
45  __nbTotalVar = __varSeq.size();
46  }
47 
48  // Destructor
50  GUM_DESTRUCTOR(MultiDimFunctionGraphGenerator);
51  }
52 
54  MultiDimFunctionGraph< double >* generatedFunctionGraph =
56 
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  }
146 
148  NodeId currentNodeId, HashTable< NodeId, Idx >& node2MinVar) {
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  }
157 
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  }
170 
171 } // namespace gum
Safe iterators for Sequence.
Definition: sequence.h:1206
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.
Class implementing a function graph generator with template type double.
const DiscreteVariable * nodeVar() const
Returns the node variable.
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
NodeId son(Idx modality) const
Returns the son at a given index.
NodeId addInternalNode(const DiscreteVariable *var)
Inserts a new non terminal node in graph.
MultiDimFunctionGraph< double > * generate()
Generates a MultiDimFunctionGraph.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Idx __nbTotalVar
The total number of variables.
virtual Size domainSize() const =0
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.
Structure used to represent a node internal structure.
Definition: internalNode.h:102
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
MultiDimFunctionGraphGenerator(Idx maxVar, Idx minVar, const Sequence< const DiscreteVariable * > &varSeq)
Default constructor.
Chain list allocated using the SmallObjectAllocator.
Definition: link.h:134
std::default_random_engine getRandomGenerator(unsigned int seed)
define a random_engine with correct seed
const Link< T > * list() const
Returns the first link in the chained list.
Definition: link_tpl.h:115
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
void addLink(const T &elem)
Adds a link.
Definition: link_tpl.h:136
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.