35 template <
typename GUM_SCALAR,
39 class TerminalNodePolicy >
45 __DG2(DG2), __function(),
46 __DG1InstantiationNeeded(DG1->realSize(), true, false),
47 __DG2InstantiationNeeded(DG2->realSize(), true, false) {
51 TerminalNodePolicy >::getReducedAndOrderedInstance();
60 template <
typename GUM_SCALAR,
64 class TerminalNodePolicy >
86 template <
typename GUM_SCALAR,
90 class TerminalNodePolicy >
98 Idx* varInst =
nullptr;
110 __rd->manager()->setRootNode(root);
119 template <
typename GUM_SCALAR,
120 template <
typename >
122 template <
typename >
123 class TerminalNodePolicy >
127 __DG1->variablesSequence().beginSafe();
129 __DG2->variablesSequence().beginSafe();
131 while (fite !=
__DG1->variablesSequence().endSafe()
132 && site !=
__DG2->variablesSequence().endSafe()) {
135 if (
__rd->variablesSequence().exists(*fite)) {
142 if (
__rd->variablesSequence().exists(*site)) {
149 if (!
__DG2->variablesSequence().exists(*fite)) {
157 if (!
__DG1->variablesSequence().exists(*site)) {
165 if (*fite == *site) {
194 if (fite ==
__DG1->variablesSequence().endSafe()) {
195 for (; site !=
__DG2->variablesSequence().endSafe(); ++site)
196 if (!
__rd->variablesSequence().exists(*site))
__rd->add(**site);
198 for (; fite !=
__DG1->variablesSequence().endSafe(); ++fite)
199 if (!
__rd->variablesSequence().exists(*fite))
__rd->add(**fite);
216 template <
typename GUM_SCALAR,
217 template <
typename >
219 template <
typename >
220 class TerminalNodePolicy >
227 Idx posi = d->variablesSequence().pos(from);
230 while (d->variablesSequence().atPos(posi) != to) {
231 dist *= (*(d->variablesSequence().atPos(posi))).domainSize();
241 template <
typename GUM_SCALAR,
242 template <
typename >
244 template <
typename >
245 class TerminalNodePolicy >
254 for (
auto varIter = dg->variablesSequence().rbeginSafe();
255 varIter != dg->variablesSequence().rendSafe();
257 Idx varPos =
__rd->variablesSequence().pos(*varIter);
258 const Link< NodeId >* nodeIter = dg->varNodeListe(*varIter)->list();
259 while (nodeIter !=
nullptr) {
260 short int* instantiationNeeded =
264 short int* varDescendant =
266 nodesVarDescendant.
insert(nodeIter->
element(), varDescendant);
268 instantiationNeeded[j] = (
short int)0;
269 varDescendant[j] = (
short int)0;
272 varDescendant[varPos] = (
short int)1;
273 for (
Idx modality = 0; modality < dg->node(nodeIter->
element())->nbSons();
275 if (!dg->isTerminalNode(dg->node(nodeIter->
element())->son(modality))) {
276 short int* sonVarDescendant =
277 nodesVarDescendant[dg->node(nodeIter->
element())->son(modality)];
278 for (
Idx varIdx = 0; varIdx <
__nbVar; varIdx++) {
279 varDescendant[varIdx] += sonVarDescendant[varIdx];
280 if (varDescendant[varIdx] && varIdx < varPos)
281 instantiationNeeded[varIdx] = (
short int)1;
289 for (
auto varIter = dg->variablesSequence().beginSafe();
290 varIter != dg->variablesSequence().endSafe();
292 const Link< NodeId >* nodeIter = dg->varNodeListe(*varIter)->list();
293 while (nodeIter !=
nullptr) {
294 for (
Idx modality = 0; modality < dg->node(nodeIter->
element())->nbSons();
297 if (!dg->isTerminalNode(sonId)) {
298 for (
Idx varIdx = 0; varIdx <
__nbVar; ++varIdx) {
299 if (dgInstNeed[nodeIter->
element()][varIdx]
300 && nodesVarDescendant[sonId][varIdx]) {
301 dgInstNeed[sonId][varIdx] = (
short int)1;
311 it != nodesVarDescendant.
end();
315 nodesVarDescendant.
clear();
340 template <
typename GUM_SCALAR,
341 template <
typename >
343 template <
typename >
344 class TerminalNodePolicy >
358 return __rd->manager()->addTerminalNode(
369 short int* dg1NeededVar =
373 Idx dg1CurrentVarPos =
376 :
__rd->variablesSequence().pos(
378 short int* dg2NeededVar =
382 Idx dg2CurrentVarPos =
385 :
__rd->variablesSequence().pos(
388 short int* instNeeded =
391 instNeeded[i] = dg1NeededVar[i] + dg2NeededVar[i];
393 double curSitKey = currentSituation.
key(instNeeded);
403 origDG2 = currentSituation.
DG2Node();
408 Idx leadVarPos =
__rd->variablesSequence().size();
410 SetNodeFunction leadFunction =
nullptr;
412 bool sameVar =
false;
414 if (!
__DG1->isTerminalNode(currentSituation.
DG1Node())) {
415 if (currentSituation.
varModality(dg1CurrentVarPos) != 0) {
418 ->son(currentSituation.
varModality(dg1CurrentVarPos) - 1));
420 newNode =
__compute(currentSituation, lastInstVarPos);
431 leadNodeId = currentSituation.
DG1Node();
432 leadVarPos = dg1CurrentVarPos;
436 if (!
__DG2->isTerminalNode(currentSituation.
DG2Node())) {
437 if (currentSituation.
varModality(dg2CurrentVarPos) != 0) {
440 ->son(currentSituation.
varModality(dg2CurrentVarPos) - 1));
442 newNode =
__compute(currentSituation, lastInstVarPos);
452 if (leadVarPos == dg2CurrentVarPos) { sameVar =
true; }
454 if (leadVarPos > dg2CurrentVarPos) {
456 leadNodeId = currentSituation.
DG2Node();
457 leadVarPos = dg2CurrentVarPos;
466 for (
Idx varPos = lastInstVarPos + 1; varPos < leadVarPos; ++varPos) {
467 if (instNeeded[varPos]) {
472 for (
Idx modality = 0; modality < curVar->
domainSize(); modality++) {
475 sonsIds[modality] =
__compute(currentSituation, varPos);
478 newNode =
__rd->manager()->addInternalNode(curVar, sonsIds);
502 Idx varPos =
__rd->variablesSequence().pos(curVar);
507 for (
Idx modality = 0; modality < curVar->
domainSize(); modality++) {
512 sonsIds[modality] =
__compute(currentSituation, varPos);
515 newNode =
__rd->manager()->addInternalNode(curVar, sonsIds);
528 const InternalNode* leaddgNode = leaddg->node(leadNodeId);
534 for (
Idx modality = 0; modality < curVar->
domainSize(); modality++) {
536 (currentSituation.*leadFunction)(leaddgNode->
son(modality));
538 sonsIds[modality] =
__compute(currentSituation, leadVarPos);
541 newNode =
__rd->manager()->addInternalNode(curVar, sonsIds);
554 template <
typename GUM_SCALAR,
555 template <
typename >
557 template <
typename >
558 class TerminalNodePolicy >
565 template <
typename GUM_SCALAR,
566 template <
typename >
568 template <
typename >
569 class TerminalNodePolicy >
576 template <
typename GUM_SCALAR,
577 template <
typename >
579 template <
typename >
580 class TerminalNodePolicy >
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const NodeId & DG2Node() const
Get DG2 diagram current explored Node.
Safe iterators for Sequence.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
const DiscreteVariable * nodeVar() const
Returns the node variable.
void __findRetrogradeVariables(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *dg, HashTable< NodeId, short int * > &dgInstNeed)
Establish for each node in both function graph if it has retrograde variables beneath it...
Unsafe Iterators for hashtablesHashTableIterator provides a fast but unsafe way to parse HashTables...
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * compute()
Computes and builds the Function Graph that is the result of the operation.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __rd
The resulting function graph.
#define SOA_DEALLOCATE(x, y)
NodeId son(Idx modality) const
Returns the son at a given index.
short int * __default
Just a comptuationnal trick.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< NodeId, short int *> __DG1InstantiationNeeded
Table uses to know if a given node of first function graph has retrograde vrariables.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const double & key(short int *instNeeded)
Returns o4DGContext key.
Idx __nbVar
The total number of variable implied in the operation.
void chgVarModality(Idx, Idx)
Changes given variable modality.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const T & element() const
Returns the element stored in this link.
virtual Size domainSize() const =0
Idx __distance(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *, const DiscreteVariable *, const DiscreteVariable *)
Heuristic methods to decide which of two retrograde variables should come first.
const NodeId & DG1Node() const
Get DG1 diagram current explored Node.
HashTable< double, NodeId > __explorationTable
The hashtable used to know if two pair of nodes have already been visited.
void setDG1Node(const NodeId &)
Set DG1 diagram current explored Node.
HashTable< NodeId, short int *> __DG2InstantiationNeeded
Table uses to know if a given node of second function graph has retrograde vrariables.
Structure used to represent a node internal structure.
Link of a chain list allocated using the SmallObjectAllocator.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Class implementingting a function graph.
Class used to perform Function Graph Operations.
const FUNCTOR< GUM_SCALAR > __function
The function to be performed on the leaves.
void setDG2Node(const NodeId &)
Set DG2 diagram current explored Node.
const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __DG1
One of the two function graphs used for the operation.
void clear()
Removes all the elements in the hash table.
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
MultiDimFunctionGraphOperator(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *DG1, const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *DG2)
Default constructor.
Size Idx
Type for indexes.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Class used to manipulate context during Function Graph Operations.
~MultiDimFunctionGraphOperator()
Default destructor.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const Link< T > * nextLink() const
Returns next link.
void __establishVarOrder()
Computes an order for the final Decision graph that will minimize the number of re exploration...
Size NodeId
Type for node ids.
Idx varModality(Idx)
Changes given variable modality.
NodeId __compute(O4DGContext ¤tSituation, Idx lastInstVarPos)
The main recursion function.
const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __DG2
The other one.