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];
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);
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);
180 _functionGraph_->_model_.eraseNode(eraseId);
182 if (_functionGraph_->_root_ == eraseId) _functionGraph_->_root_ = replacingId;
186 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
204 && modality >
_functionGraph_->_internalNodeMap_[node]->nodeVar()->domainSize() - 1)
206 "Modality " << modality <<
"is higher than domain size "
208 <<
"minus 1 of variable "
219 <<
" is after variable "
221 <<
"in Function Graph order.")
224 const
NodeId oldSonNode = parentNode->son(modality);
225 if (oldSonNode == sonNode) return;
229 _functionGraph_->_internalNodeMap_[oldSonNode]->removeParent(node, modality);
231 parentNode->
setSon(modality, sonNode);
233 _functionGraph_->_internalNodeMap_[sonNode]->addParent(node, modality);
237 template < typename GUM_ELEMENT, template < class > class TerminalNodePolicy >
246 const Link< NodeId >* curElem = _functionGraph_->_var2NodeIdMap_[*varIter]->list();
248 for (; curElem != nullptr; nbElem++, curElem = curElem->nextLink())
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);
261 sifIter != siftingSeq.
endSafe();
264 Idx currentPos = _functionGraph_->variablesSequence().pos(*sifIter);
265 Idx bestSize = _functionGraph_->realSize();
266 Idx bestPos = currentPos;
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();
280 while (currentPos < _functionGraph_->variablesSequence().size() - 1) {
281 moveTo(*sifIter, currentPos + 1);
284 bestPos = currentPos;
289 moveTo(*sifIter, bestPos);
294 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
302 currentPos != desiredPos;
312 currentPos != desiredPos;
323 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
334 NodeId* currentNewXNodeSons =
nullptr;
337 NodeId* currentNewYNodeSons =
nullptr;
338 NodeId currentNewYNodeId = 0;
341 while (oldxNodes->
list()) {
350 for (indy = 0; indy < y->
domainSize(); ++indy) {
356 for (indx = 0; indx < x->
domainSize(); ++indx) {
357 currentNewXNodeSons[indx] = currentOldXNode->
son(indx);
360 currentNewXNodeSons[indx]
365 currentNewYNodeSons[indy] = nodeRedundancyCheck_(x, currentNewXNodeSons);
369 currentNewYNodeId = currentNewYNodeSons[0];
370 if (_isRedundant_(y, currentNewYNodeSons)) {
371 migrateNode_(oldxNodes->
list()->
element(), currentNewYNodeId);
374 currentNewYNodeId = _checkIsomorphism_(y, currentNewYNodeSons);
375 if (currentNewYNodeId != 0) {
376 migrateNode_(oldxNodes->
list()->
element(), currentNewYNodeId);
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(
388 currentOldXNode->
setNode(y, currentNewYNodeSons);
390 for (Idx i = 0; i < currentOldXNode->
nodeVar()->domainSize(); ++i) {
391 if (_functionGraph_->_internalNodeMap_.exists(currentNewYNodeSons[i])) {
392 _functionGraph_->_internalNodeMap_[currentNewYNodeSons[i]]->addParent(
398 _functionGraph_->_var2NodeIdMap_[y]->addLink(oldxNodes->
list()->
element());
406 while (oldyNodes->
list()) {
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);
418 _functionGraph_->_var2NodeIdMap_[y]->addLink(curId);
425 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
428 const NodeId& destination) {
433 while (picle !=
nullptr) {
453 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
457 NodeId newNode = sonsIds[0];
474 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
483 while (currentElem !=
nullptr) {
488 while (i < var->domainSize() && sons[i] == nody->
son(i))
492 currentElem = currentElem->
nextLink();
498 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
503 if (sons[m] != sons[0])
return false;
508 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
516 for (SequenceIterator< const DiscreteVariable* > varIter
522 while (currentNodeId !=
nullptr) {
523 nextNodeId = currentNodeId->
nextLink();
529 for (currentInd = 1; currentInd < (*varIter)->domainSize(); currentInd++) {
530 if (currentNode->
son(currentInd) != currentNode->
son(0)) {
536 if (theSame ==
true) {
539 currentNodeId = nextNodeId;
549 while (anotherNodeId->
nextLink() !=
nullptr) {
550 nextNodeId = anotherNodeId->
nextLink();
554 for (modality = 0; modality < (*varIter)->domainSize(); ++modality) {
555 if (anotherNode->
son(modality) != currentNode->
son(modality))
break;
556 if (modality == (*varIter)->domainSize() - 1) {
563 anotherNodeId = nextNodeId;
566 currentNodeId = currentNodeId->
nextLink();
571 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
574 for (SequenceIterator< const DiscreteVariable* > varIter = oldSequence.
begin();
575 varIter != oldSequence.
end();
584 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
592 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
598 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
605 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
612 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
619 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
625 template <
typename GUM_ELEMENT,
template <
class >
class TerminalNodePolicy >
632 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.
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.
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)
Represent a node's parent.