60 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
68 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
74 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
81 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
94 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
105 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
113 for (
Idx i = 0; i < newNodeStruct->
nbSons(); i++)
121 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
123 const GUM_ELEMENT& value) {
133 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
137 bool updateParents) {
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);
151 nodeIter = nodeIter->nextLink();
154 _functionGraph_->eraseTerminalNode(eraseId);
157 InternalNode* eraseNode = _functionGraph_->_internalNodeMap_[eraseId];
160 Link< Parent >* picle = eraseNode->parents();
161 while (picle !=
nullptr) {
162 setSon(picle->element().parentId, picle->element().modality, replacingId);
163 picle = picle->nextLink();
167 _functionGraph_->_var2NodeIdMap_[_functionGraph_->_internalNodeMap_[eraseId]->nodeVar()]
168 ->searchAndRemoveLink(eraseId);
170 delete _functionGraph_->_internalNodeMap_[eraseId];
171 _functionGraph_->_internalNodeMap_.erase(eraseId);
180 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
198 && modality >
_functionGraph_->_internalNodeMap_[node]->nodeVar()->domainSize() - 1)
200 "Modality " << modality <<
"is higher than domain size "
202 <<
"minus 1 of variable "
213 <<
" is after variable "
215 <<
"in Function Graph order.")
219 _functionGraph_->_internalNodeMap_[sonNode]->addParent(node, modality);
223 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
232 const Link< NodeId >* curElem = _functionGraph_->_var2NodeIdMap_[*varIter]->list();
234 for (; curElem != nullptr; nbElem++, curElem = curElem->nextLink())
236 varLvlSize.insert(*varIter, nbElem);
237 siftingSeq.insert(*varIter);
238 Idx pos = siftingSeq.pos(*varIter);
239 while (pos > 0 && varLvlSize[siftingSeq.atPos(pos - 1)] > nbElem) {
240 siftingSeq.swap(pos - 1, pos);
247 sifIter != siftingSeq.
endSafe();
250 Idx currentPos = _functionGraph_->variablesSequence().pos(*sifIter);
251 Idx bestSize = _functionGraph_->realSize();
252 Idx bestPos = currentPos;
256 while (currentPos > 0) {
257 moveTo(*sifIter, currentPos - 1);
258 currentPos = _functionGraph_->variablesSequence().pos(*sifIter);
259 if (_functionGraph_->realSize() < bestSize) {
260 bestPos = currentPos;
261 bestSize = _functionGraph_->realSize();
266 while (currentPos < _functionGraph_->variablesSequence().size() - 1) {
267 moveTo(*sifIter, currentPos + 1);
270 bestPos = currentPos;
275 moveTo(*sifIter, bestPos);
280 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
288 currentPos != desiredPos;
297 for (
Idx currentPos = _functionGraph_->variablesSequence().pos(movedVar);
298 currentPos != desiredPos;
309 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
320 NodeId* currentNewXNodeSons =
nullptr;
323 NodeId* currentNewYNodeSons =
nullptr;
324 NodeId currentNewYNodeId = 0;
327 while (oldxNodes->
list()) {
336 for (indy = 0; indy < y->
domainSize(); ++indy) {
342 for (indx = 0; indx < x->
domainSize(); ++indx) {
343 currentNewXNodeSons[indx] = currentOldXNode->
son(indx);
346 currentNewXNodeSons[indx]
351 currentNewYNodeSons[indy] = nodeRedundancyCheck_(x, currentNewXNodeSons);
355 currentNewYNodeId = currentNewYNodeSons[0];
356 if (_isRedundant_(y, currentNewYNodeSons)) {
357 migrateNode_(oldxNodes->
list()->
element(), currentNewYNodeId);
360 currentNewYNodeId = _checkIsomorphism_(y, currentNewYNodeSons);
361 if (currentNewYNodeId != 0) {
362 migrateNode_(oldxNodes->
list()->
element(), currentNewYNodeId);
366 for (Idx i = 0; i < currentOldXNode->
nodeVar()->domainSize(); ++i) {
367 if (_functionGraph_->_internalNodeMap_.exists(currentOldXNode->
son(i))) {
368 _functionGraph_->_internalNodeMap_[currentOldXNode->
son(i)]->removeParent(
374 currentOldXNode->
setNode(y, currentNewYNodeSons);
376 for (Idx i = 0; i < currentOldXNode->
nodeVar()->domainSize(); ++i) {
377 if (_functionGraph_->_internalNodeMap_.exists(currentNewYNodeSons[i])) {
378 _functionGraph_->_internalNodeMap_[currentNewYNodeSons[i]]->addParent(
384 _functionGraph_->_var2NodeIdMap_[y]->addLink(oldxNodes->
list()->
element());
392 while (oldyNodes->
list()) {
394 if (_functionGraph_->_internalNodeMap_[curId]->parents() ==
nullptr) {
395 for (Idx i = 0; i < _functionGraph_->_internalNodeMap_[curId]->nodeVar()->domainSize(); ++i)
396 if (_functionGraph_->_internalNodeMap_.exists(
397 _functionGraph_->_internalNodeMap_[curId]->son(i)))
398 _functionGraph_->_internalNodeMap_[_functionGraph_->_internalNodeMap_[curId]->son(i)]
399 ->removeParent(curId, i);
400 delete _functionGraph_->_internalNodeMap_[curId];
401 _functionGraph_->_internalNodeMap_.erase(curId);
402 _functionGraph_->_model_.eraseNode(curId);
404 _functionGraph_->_var2NodeIdMap_[y]->addLink(curId);
411 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
414 const NodeId& destination) {
418 while (picle !=
nullptr) {
437 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
441 NodeId newNode = sonsIds[0];
458 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
467 while (currentElem !=
nullptr) {
472 while (i < var->domainSize() && sons[i] == nody->
son(i))
476 currentElem = currentElem->
nextLink();
482 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
487 if (sons[m] != sons[0])
return false;
492 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
500 for (SequenceIterator< const DiscreteVariable* > varIter
506 while (currentNodeId !=
nullptr) {
507 nextNodeId = currentNodeId->
nextLink();
513 for (currentInd = 1; currentInd < (*varIter)->domainSize(); currentInd++) {
514 if (currentNode->
son(currentInd) != currentNode->
son(0)) {
520 if (theSame ==
true) {
523 currentNodeId = nextNodeId;
533 while (anotherNodeId->
nextLink() !=
nullptr) {
534 nextNodeId = anotherNodeId->
nextLink();
538 for (modality = 0; modality < (*varIter)->domainSize(); ++modality) {
539 if (anotherNode->
son(modality) != currentNode->
son(modality))
break;
540 if (modality == (*varIter)->domainSize() - 1) {
547 anotherNodeId = nextNodeId;
550 currentNodeId = currentNodeId->
nextLink();
555 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
558 for (SequenceIterator< const DiscreteVariable* > varIter = oldSequence.
begin();
559 varIter != oldSequence.
end();
568 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
576 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
582 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
589 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
596 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
603 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
609 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
616 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
Base class for discrete random variable.
virtual Size domainSize() const =0
The class for generic Hash Tables.
Structure used to represent a node internal structure.
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.
Link of a chain list allocated using the SmallObjectAllocator.
const T & element() const
Returns the element stored in this link.
const Link< T > * nextLink() const
Returns next link.
const Link< T > * list() const
Returns the first link in the chained list.
void searchAndRemoveLink(const T &elem)
Removes a element from the list.
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 moveTo(const DiscreteVariable *x, Idx desiredPos)
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...
virtual ~MultiDimFunctionGraphManager()
Class destructor.
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() override
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.
~MultiDimFunctionGraphTreeManager() override
Class destructor.
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.
The generic class for storing (ordered) sequences of objects.
#define GUM_ERROR(type, msg)
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Headers of the Link and LinkedList classes.
Headers of MultiDimFunctionGraphManager.
gum is the global namespace for all aGrUM entities
Header file of gum::Sequence, a class for storing (ordered) sequences of objects.
#define SOA_DEALLOCATE(x, y)