aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
multiDimFunctionGraphManager_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
56
57namespace gum {
58
59 // Default constructor
60 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
66
67 // Destructor
68 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
72
74 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
79
80 // Inserts a new non terminal node in graph.
81 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
83 const DiscreteVariable* var,
84 NodeId nid) {
85 InternalNode* newNodeStruct = new InternalNode(var);
86
87 _functionGraph_->_internalNodeMap_.insert(nid, newNodeStruct);
88
89 _functionGraph_->_var2NodeIdMap_[var]->addLink(nid);
90
91 return nid;
92 }
93
94 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
96 const DiscreteVariable* var) {
97 InternalNode* newNodeStruct = new InternalNode(var);
98 NodeId nid = _functionGraph_->_model_.addNode();
99 _functionGraph_->_internalNodeMap_.insert(nid, newNodeStruct);
100 _functionGraph_->_var2NodeIdMap_[var]->addLink(nid);
101
102 return nid;
103 }
104
105 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
107 const DiscreteVariable* var,
108 NodeId* sons) {
109 InternalNode* newNodeStruct = new InternalNode(var, sons);
110 NodeId nid = _functionGraph_->_model_.addNode();
111 _functionGraph_->_internalNodeMap_.insert(nid, newNodeStruct);
112 _functionGraph_->_var2NodeIdMap_[var]->addLink(nid);
113 for (Idx i = 0; i < newNodeStruct->nbSons(); i++)
114 if (!_functionGraph_->isTerminalNode(sons[i]))
115 _functionGraph_->_internalNodeMap_[sons[i]]->addParent(nid, i);
116
117 return nid;
118 }
119
120 // Adds a value to the MultiDimFunctionGraph.
121 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
123 const GUM_ELEMENT& value) {
124 if (_functionGraph_->existsTerminalNodeWithValue(value))
125 return _functionGraph_->terminalNodeId(value);
126
127 NodeId node = _functionGraph_->_model_.addNode();
128 _functionGraph_->addTerminalNode(node, value);
129 return node;
130 }
131
132 // Erases a node from the diagram.
133 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
135 NodeId eraseId,
136 NodeId replacingId,
137 bool updateParents) {
138 if (!_functionGraph_->_model_.exists(eraseId))
139 GUM_ERROR(NotFound, "Node : " << eraseId << " doesn't exists in the graph")
140
141 if (_functionGraph_->isTerminalNode(eraseId)) {
142 for (auto iterVar = _functionGraph_->variablesSequence().begin();
143 iterVar != _functionGraph_->variablesSequence().end();
144 ++iterVar) {
145 Link< NodeId >* nodeIter = _functionGraph_->_var2NodeIdMap_[*iterVar]->list();
146 while (nodeIter != nullptr) {
147 for (Idx modality = 0; modality < (*iterVar)->domainSize(); ++modality)
148 if (_functionGraph_->node(nodeIter->element())->son(modality) == eraseId)
149 setSon(nodeIter->element(), modality, replacingId);
150
151 nodeIter = nodeIter->nextLink();
152 }
153 }
154 _functionGraph_->eraseTerminalNode(eraseId);
155
156 } else {
157 InternalNode* eraseNode = _functionGraph_->_internalNodeMap_[eraseId];
158
159 if (updateParents) {
160 // setSon removes the current link from the parents of eraseNode
161 Link< Parent >* picle = eraseNode->parents();
162 while (picle != nullptr) {
163 const Parent parent = picle->element();
164 picle = picle->nextLink();
165 setSon(parent.parentId, parent.modality, replacingId);
166 }
167 }
168
169 // the sons of eraseNode must not consider it as a parent anymore
170 for (Idx i = 0; i < eraseNode->nbSons(); ++i)
171 if (_functionGraph_->isInternalNode(eraseNode->son(i)))
172 _functionGraph_->_internalNodeMap_[eraseNode->son(i)]->removeParent(eraseId, i);
174 _functionGraph_->_var2NodeIdMap_[eraseNode->nodeVar()]->searchAndRemoveLink(eraseId);
175
176 delete _functionGraph_->_internalNodeMap_[eraseId];
177 _functionGraph_->_internalNodeMap_.erase(eraseId);
178 }
179
180 _functionGraph_->_model_.eraseNode(eraseId);
181
182 if (_functionGraph_->_root_ == eraseId) _functionGraph_->_root_ = replacingId;
183 }
184
186 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
188 const NodeId& node,
189 const Idx& modality,
190 const NodeId& sonNode) {
191 // Ensuring that both nodes exists in the graph
192 if (!_functionGraph_->_model_.exists(node))
193 GUM_ERROR(NotFound, "Node : " << node << " doesn't exists in the graph")
194 if (!_functionGraph_->_model_.exists(sonNode))
195 GUM_ERROR(NotFound, "Node : " << sonNode << " doesn't exists in the graph")
196
197 // Check if starting node is not terminal
198 if (_functionGraph_->isTerminalNode(node))
199 GUM_ERROR(InvalidNode, "You cannot insert an arc from terminal node : " << node)
201 // Check if associated modality is lower than node bound variable domain
202 // size
203 if (_functionGraph_->isInternalNode(node)
204 && modality > _functionGraph_->_internalNodeMap_[node]->nodeVar()->domainSize() - 1)
206 "Modality " << modality << "is higher than domain size "
207 << _functionGraph_->_internalNodeMap_[node]->nodeVar()->domainSize()
208 << "minus 1 of variable "
209 << _functionGraph_->_internalNodeMap_[node]->nodeVar()->name())
210
211 // Check if variable order is respected
212 if (_functionGraph_->isInternalNode(sonNode)
213 && _functionGraph_->variablesSequence().pos(
214 _functionGraph_->_internalNodeMap_[node]->nodeVar())
215 >= _functionGraph_->variablesSequence().pos(
216 _functionGraph_->_internalNodeMap_[sonNode]->nodeVar()))
218 "Variable " << _functionGraph_->_internalNodeMap_[node]->nodeVar()
219 << " is after variable "
220 << _functionGraph_->_internalNodeMap_[sonNode]->nodeVar()
221 << "in Function Graph order.")
222
223 InternalNode* parentNode = _functionGraph_->_internalNodeMap_[node];
224 const NodeId oldSonNode = parentNode->son(modality);
225 if (oldSonNode == sonNode) return;
227 // the old son is no longer a child of node for this modality
228 if (_functionGraph_->isInternalNode(oldSonNode))
229 _functionGraph_->_internalNodeMap_[oldSonNode]->removeParent(node, modality);
230
231 parentNode->setSon(modality, sonNode);
232 if (sonNode && !_functionGraph_->isTerminalNode(sonNode))
233 _functionGraph_->_internalNodeMap_[sonNode]->addParent(node, modality);
234 }
235
236 // Changes var position in variable sequence
237 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
238 void MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy >::minimizeSize() {
239 // Ordering variables by number of nodes asssociated to them
243 = _functionGraph_->variablesSequence().beginSafe();
244 varIter != _functionGraph_->variablesSequence().endSafe();
245 ++varIter) {
246 const Link< NodeId >* curElem = _functionGraph_->_var2NodeIdMap_[*varIter]->list();
247 Idx nbElem = 0;
248 for (; curElem != nullptr; nbElem++, curElem = curElem->nextLink())
249 ;
250 varLvlSize.insert(*varIter, nbElem);
251 siftingSeq.insert(*varIter);
252 Idx pos = siftingSeq.pos(*varIter);
253 while (pos > 0 && varLvlSize[siftingSeq.atPos(pos - 1)] > nbElem) {
254 siftingSeq.swap(pos - 1, pos);
255 pos--;
256 }
257 }
258
259 // Sifting var par var
261 sifIter != siftingSeq.endSafe();
262 ++sifIter) {
263 // Initialization
264 Idx currentPos = _functionGraph_->variablesSequence().pos(*sifIter);
265 Idx bestSize = _functionGraph_->realSize();
266 Idx bestPos = currentPos;
267
268
269 // Sifting towards upper places
270 while (currentPos > 0) {
271 moveTo(*sifIter, currentPos - 1);
272 currentPos = _functionGraph_->variablesSequence().pos(*sifIter);
273 if (_functionGraph_->realSize() < bestSize) {
274 bestPos = currentPos;
275 bestSize = _functionGraph_->realSize();
276 }
277 }
278
279 // Sifting towards lower places
280 while (currentPos < _functionGraph_->variablesSequence().size() - 1) {
281 moveTo(*sifIter, currentPos + 1);
282 currentPos = _functionGraph_->variablesSequence().pos(*sifIter);
283 if (_functionGraph_->realSize() < bestSize) {
284 bestPos = currentPos;
285 bestSize = _functionGraph_->realSize();
286 }
287 }
288
289 moveTo(*sifIter, bestPos);
290 }
291 }
292
293 // Changes var position in variable sequence
294 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
296 const DiscreteVariable* movedVar,
297 Idx desiredPos) {
298 // First we determine the position of both variable
299 // We also determine which one precede the other
300 if (_functionGraph_->variablesSequence().pos(movedVar) > desiredPos)
301 for (Idx currentPos = _functionGraph_->variablesSequence().pos(movedVar);
302 currentPos != desiredPos;
303 currentPos--) {
304 const DiscreteVariable* preVar = _functionGraph_->variablesSequence().atPos(currentPos - 1);
305 if (_functionGraph_->_var2NodeIdMap_[preVar]->list()
306 && _functionGraph_->_var2NodeIdMap_[movedVar]->list())
307 _adjacentSwap_(preVar, movedVar);
308 _functionGraph_->invert_(currentPos - 1, currentPos);
309 }
310 else
311 for (Idx currentPos = _functionGraph_->variablesSequence().pos(movedVar);
312 currentPos != desiredPos;
313 currentPos++) {
314 const DiscreteVariable* suiVar = _functionGraph_->variablesSequence().atPos(currentPos + 1);
315 if (_functionGraph_->_var2NodeIdMap_[suiVar]->list()
316 && _functionGraph_->_var2NodeIdMap_[movedVar]->list())
317 _adjacentSwap_(movedVar, suiVar);
318 _functionGraph_->invert_(currentPos, currentPos + 1);
319 }
320 }
321
322 // Swap two adjacent variable.
323 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
325 const DiscreteVariable* x,
326 const DiscreteVariable* y) {
327 LinkedList< NodeId >* oldxNodes = _functionGraph_->_var2NodeIdMap_[x];
328 _functionGraph_->_var2NodeIdMap_[x] = new LinkedList< NodeId >();
329 LinkedList< NodeId >* oldyNodes = _functionGraph_->_var2NodeIdMap_[y];
330 _functionGraph_->_var2NodeIdMap_[y] = new LinkedList< NodeId >();
331
332
333 InternalNode* currentOldXNode = nullptr;
334 NodeId* currentNewXNodeSons = nullptr;
335 Idx indx = 0;
336
337 NodeId* currentNewYNodeSons = nullptr;
338 NodeId currentNewYNodeId = 0;
339 Idx indy = 0;
340
341 while (oldxNodes->list()) {
342 // Recuperating a node associated to variables x
343 currentOldXNode = _functionGraph_->_internalNodeMap_[oldxNodes->list()->element()];
344
345 // Creating a new node associated to variable y
346 currentNewYNodeSons = InternalNode::allocateNodeSons(y);
347
348 // Now the graph needs to be remap by inserting nodes bound to x
349 // for each values assumed by y
350 for (indy = 0; indy < y->domainSize(); ++indy) {
351 // Creating a new node bound to x that will be the son of the node
352 // tied to y for the current value assumed by y
353 currentNewXNodeSons = InternalNode::allocateNodeSons(x);
354
355 // Iterating on the different values taht x can assumed to do the remap
356 for (indx = 0; indx < x->domainSize(); ++indx) {
357 currentNewXNodeSons[indx] = currentOldXNode->son(indx);
358 if (!_functionGraph_->isTerminalNode(currentOldXNode->son(indx))
359 && _functionGraph_->node(currentOldXNode->son(indx))->nodeVar() == y)
360 currentNewXNodeSons[indx]
361 = _functionGraph_->node(currentOldXNode->son(indx))->son(indy);
362 }
363
364 // Inserting the new node bound to x
365 currentNewYNodeSons[indy] = nodeRedundancyCheck_(x, currentNewXNodeSons);
366 }
367
368 // Replacing old node x by new node y
369 currentNewYNodeId = currentNewYNodeSons[0];
370 if (_isRedundant_(y, currentNewYNodeSons)) {
371 migrateNode_(oldxNodes->list()->element(), currentNewYNodeId);
372 SOA_DEALLOCATE(currentNewYNodeSons, y->domainSize() * sizeof(NodeId));
373 } else {
374 currentNewYNodeId = _checkIsomorphism_(y, currentNewYNodeSons);
375 if (currentNewYNodeId != 0) {
376 migrateNode_(oldxNodes->list()->element(), currentNewYNodeId);
377 SOA_DEALLOCATE(currentNewYNodeSons, y->domainSize() * sizeof(NodeId));
378 } else {
379 // Updating the sons (they must not consider old x as their parent)
380 for (Idx i = 0; i < currentOldXNode->nodeVar()->domainSize(); ++i) {
381 if (_functionGraph_->_internalNodeMap_.exists(currentOldXNode->son(i))) {
382 _functionGraph_->_internalNodeMap_[currentOldXNode->son(i)]->removeParent(
383 oldxNodes->list()->element(),
384 i);
385 }
386 }
387 // Reaffecting old node x internal attributes to correct new one
388 currentOldXNode->setNode(y, currentNewYNodeSons);
389 // Updating new sons (they must consider the node as a parent)
390 for (Idx i = 0; i < currentOldXNode->nodeVar()->domainSize(); ++i) {
391 if (_functionGraph_->_internalNodeMap_.exists(currentNewYNodeSons[i])) {
392 _functionGraph_->_internalNodeMap_[currentNewYNodeSons[i]]->addParent(
393 oldxNodes->list()->element(),
394 i);
395 }
396 }
397
398 _functionGraph_->_var2NodeIdMap_[y]->addLink(oldxNodes->list()->element());
399 }
400 }
401
402 oldxNodes->searchAndRemoveLink(oldxNodes->list()->element());
403 }
404 delete oldxNodes;
405
406 while (oldyNodes->list()) {
407 NodeId curId = oldyNodes->list()->element();
408 if (_functionGraph_->_internalNodeMap_[curId]->parents() == nullptr) {
409 for (Idx i = 0; i < _functionGraph_->_internalNodeMap_[curId]->nodeVar()->domainSize(); ++i)
410 if (_functionGraph_->_internalNodeMap_.exists(
411 _functionGraph_->_internalNodeMap_[curId]->son(i)))
412 _functionGraph_->_internalNodeMap_[_functionGraph_->_internalNodeMap_[curId]->son(i)]
413 ->removeParent(curId, i);
414 delete _functionGraph_->_internalNodeMap_[curId];
415 _functionGraph_->_internalNodeMap_.erase(curId);
416 _functionGraph_->_model_.eraseNode(curId);
417 } else {
418 _functionGraph_->_var2NodeIdMap_[y]->addLink(curId);
419 }
420 oldyNodes->searchAndRemoveLink(curId);
421 }
422 delete oldyNodes;
423 }
424
425 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
427 const NodeId& origin,
428 const NodeId& destination) {
429 InternalNode* org = _functionGraph_->_internalNodeMap_[origin];
430 // Upating parents after the change
431 // setSon removes the current link from the parents of org
432 Link< Parent >* picle = org->parents();
433 while (picle != nullptr) {
434 const Parent parent = picle->element();
435 picle = picle->nextLink();
436 setSon(parent.parentId, parent.modality, destination);
437 }
438
439 // Updating sons after the change
440 for (Idx i = 0; i < org->nbSons(); ++i)
441 if (_functionGraph_->_internalNodeMap_.exists(org->son(i)))
442 _functionGraph_->_internalNodeMap_[org->son(i)]->removeParent(origin, i);
443
444 delete org;
445 _functionGraph_->_internalNodeMap_.erase(origin);
446 _functionGraph_->_model_.eraseNode(origin);
447
448 if (_functionGraph_->root() == origin) this->setRootNode(destination);
449 }
450
451 // Checks if a similar node does not already exists in the graph or
452 // if it has the same child for every variable value.
453 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
455 const DiscreteVariable* var,
456 NodeId* sonsIds) {
457 NodeId newNode = sonsIds[0];
458
459 if (_isRedundant_(var, sonsIds)) {
460 SOA_DEALLOCATE(sonsIds, sizeof(NodeId) * var->domainSize());
461 } else {
462 newNode = _checkIsomorphism_(var, sonsIds);
463 if (newNode == 0) {
464 newNode = addInternalNode_(var, sonsIds);
465 } else {
466 SOA_DEALLOCATE(sonsIds, sizeof(NodeId) * var->domainSize());
467 }
468 }
469
470 return newNode;
471 }
472
473 // Checks if a similar node does not already exists in the graph.
474 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
476 const DiscreteVariable* var,
477 NodeId* sons) {
478 const InternalNode* nody = nullptr;
479 Idx i = 0;
480
481 // Check abscence of identical node
482 Link< NodeId >* currentElem = _functionGraph_->_var2NodeIdMap_[var]->list();
483 while (currentElem != nullptr) {
484 nody = _functionGraph_->_internalNodeMap_[currentElem->element()];
485
486 // Check on the other sons
487 i = 0;
488 while (i < var->domainSize() && sons[i] == nody->son(i))
489 ++i;
490 if (i == var->domainSize()) return currentElem->element();
491
492 currentElem = currentElem->nextLink();
493 }
494 return 0;
495 }
496
497 // Checks if node has the same child for every variable value
498 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
500 const DiscreteVariable* var,
501 NodeId* sons) {
502 for (Idx m = 1; m < var->domainSize(); m++)
503 if (sons[m] != sons[0]) return false;
504 return true;
505 }
506
507 // Ensures that every isomorphic subgraphs are merged together.
508 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
510 Link< NodeId >* currentNodeId = nullptr;
511 Link< NodeId >* nextNodeId = nullptr;
512 InternalNode* currentNode = nullptr;
513 bool theSame = true;
514 Idx currentInd;
515
516 for (SequenceIterator< const DiscreteVariable* > varIter
517 = _functionGraph_->variablesSequence().rbegin();
518 varIter != _functionGraph_->variablesSequence().rend();
519 --varIter) {
520 currentNodeId = _functionGraph_->_var2NodeIdMap_[*varIter]->list();
521
522 while (currentNodeId != nullptr) {
523 nextNodeId = currentNodeId->nextLink();
524 currentNode = _functionGraph_->_internalNodeMap_[currentNodeId->element()];
525
526 // First isomorphism to handle is the one where all node children are
527 // the same
528 theSame = true;
529 for (currentInd = 1; currentInd < (*varIter)->domainSize(); currentInd++) {
530 if (currentNode->son(currentInd) != currentNode->son(0)) {
531 theSame = false;
532 break;
533 }
534 }
535
536 if (theSame == true) {
537 migrateNode_(currentNodeId->element(), currentNode->son(0));
538 _functionGraph_->_var2NodeIdMap_[*varIter]->searchAndRemoveLink(currentNodeId->element());
539 currentNodeId = nextNodeId;
540 continue;
541 }
542
543 // Second isomorphism to handle is the one where two nodes have same
544 // variable and same children
545 if (nextNodeId) {
546 Link< NodeId >* anotherNodeId = currentNodeId->nextLink();
547 InternalNode* anotherNode = nullptr;
548 Idx modality = 0;
549 while (anotherNodeId->nextLink() != nullptr) {
550 nextNodeId = anotherNodeId->nextLink();
551 anotherNode = _functionGraph_->_internalNodeMap_[anotherNodeId->element()];
552
553 // Check on the other sons
554 for (modality = 0; modality < (*varIter)->domainSize(); ++modality) {
555 if (anotherNode->son(modality) != currentNode->son(modality)) break;
556 if (modality == (*varIter)->domainSize() - 1) {
557 migrateNode_(anotherNodeId->element(), currentNodeId->element());
558 _functionGraph_->_var2NodeIdMap_[*varIter]->searchAndRemoveLink(
559 anotherNodeId->element());
560 }
561 }
562
563 anotherNodeId = nextNodeId;
564 }
565 }
566 currentNodeId = currentNodeId->nextLink();
567 }
568 }
569 }
570
571 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
573 Sequence< const DiscreteVariable* > oldSequence(_functionGraph_->variablesSequence());
574 for (SequenceIterator< const DiscreteVariable* > varIter = oldSequence.begin();
575 varIter != oldSequence.end();
576 ++varIter)
577 if (!_functionGraph_->varNodeListe(*varIter)->list()) _functionGraph_->erase(**varIter);
578 }
579
580 // ==========================================================================
581 // MultiDimFunctionGraphTreeManager
582 // ==========================================================================
583
584 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
591
592 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
595 GUM_DESTRUCTOR(MultiDimFunctionGraphTreeManager);
596 }
597
598 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
604
605 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
607
608 // ===========================================================================
609 // MultiDimFunctionGraphROManager
610 // ===========================================================================
611
612 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
618
619 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
621 TerminalNodePolicy >::~MultiDimFunctionGraphROManager() {
622 GUM_DESTRUCTOR(MultiDimFunctionGraphROManager);
623 }
624
625 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
631
632 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
636
637} // namespace gum
Base class for discrete random variable.
virtual Size domainSize() const =0
The class for generic Hash Tables.
Definition hashTable.h:640
Structure used to represent a node internal structure.
InternalNode()
Default Constructor Creates an empty node with no variable attached.
void setNode(const DiscreteVariable *v, NodeId *sons)
Allows you to respecify the node, changing its attached variable as well as its son map.
const DiscreteVariable * nodeVar() const
Returns the node variable.
Idx nbSons() const
Returns the number of sons.
static NodeId * allocateNodeSons(const DiscreteVariable *v)
Allocates a table of nodeid of the size given in parameter.
NodeId son(Idx modality) const
Returns the son at a given index.
Link< Parent > * parents()
Returns the list of parents.
Exception: at least one argument passed to a function is not what was expected.
Exception : node does not exist.
const Link< T > * list() const
Returns the first link in the chained list.
Definition link_tpl.h:136
void searchAndRemoveLink(const T &elem)
Removes a element from the list.
Definition link_tpl.h:163
void eraseNode(NodeId id, NodeId replacingId=0, bool updateParents=true)
Erases a node from the diagram.
NodeId addInternalNode(const DiscreteVariable *var)
Inserts a new non terminal node in graph.
void clean()
Removes var without nodes in the diagram.
NodeId nodeRedundancyCheck_(const DiscreteVariable *var, NodeId *sonsMap)
Check for redundancy.
bool _isRedundant_(const DiscreteVariable *var, NodeId *sons)
Checks if node has the same child for every variable value.
void _adjacentSwap_(const DiscreteVariable *x, const DiscreteVariable *y)
void reduce_()
Ensures that every isomorphic subgraphs are merged together.
void setRootNode(const NodeId &root)
Sets root node of decision diagram.
NodeId addTerminalNode(const GUM_ELEMENT &value)
Adds a value to the MultiDimFunctionGraph.
void minimizeSize()
Performs a sifting in search of a(local) minimal size.
NodeId addInternalNode_(const DiscreteVariable *var, NodeId *sons)
Adds an internal node.
MultiDimFunctionGraphManager(MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > *master)
Default constructor.
void migrateNode_(const NodeId &x, const NodeId &y)
Remaps all arcs going to ou going from the first given node to the second node, then delete first nod...
NodeId _checkIsomorphism_(const DiscreteVariable *var, NodeId *sons)
Checks if a similar node does not already exists in the graph.
MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * _functionGraph_
The multidimdecisiongraph supposed to be edited.
void setSon(const NodeId &node, const Idx &modality, const NodeId &sonNode)
Sets nodes son for given modality to designated son node.
MultiDimFunctionGraphROManager(MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > *master)
void reduce() override
Ensures that every isomorphic subgraphs are merged together.
NodeId addInternalNode(const DiscreteVariable *var, NodeId *sons) override
Inserts a new non terminal node in graph.
NodeId addInternalNode(const DiscreteVariable *var, NodeId *sons) override
Inserts a new non terminal node in graph.
void reduce() override
Ensures that every isomorphic subgraphs are merged together.
MultiDimFunctionGraphTreeManager(MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > *master)
Class constructor.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
iterator begin() const
Returns an unsafe begin iterator.
const iterator & end() const noexcept
Returns the unsafe end iterator.
iterator_safe beginSafe() const
Returns a safe begin iterator.
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
Safe iterators for Sequence.
Definition sequence.h:1148
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
Headers of MultiDimFunctionGraphManager.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Header file of gum::Sequence, a class for storing (ordered) sequences of objects.
#define SOA_DEALLOCATE(x, y)
Represent a node's parent.
NodeId parentId