56 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
70 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
82 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
91 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
99 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
108 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
113 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
116 const GUM_SCALAR&
value)
const {
118 "Function Graph can't be edited so "
119 "easily.\nMultiDimFunctionGraphManager "
120 "provides the framework to edit a "
124 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
128 "Function Graph can't be edited so "
129 "easily.\nMultiDimFunctionGraphManager "
130 "provides the framework to edit a "
134 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
136 const std::vector< GUM_SCALAR >& v)
const {
138 "Function Graph can't be edited so "
139 "easily.\nMultiDimFunctionGraphManager "
140 "provides the framework to editaa "
144 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
146 std::initializer_list< GUM_SCALAR > l)
const {
148 "Function Graph can't be edited so "
149 "easily.\nMultiDimFunctionGraphManager "
150 "provides the framework to edit a "
154 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
162 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
176 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
181 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
188 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
192 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
196 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
200 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
204 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
208 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
211 std::stringstream sBuff;
212 sBuff << (*i) <<
" = " << this->
get(*i);
216 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
221 "You cannot copy another type of multiDim "
222 "into a MultiDimFunctionGraph.");
225 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
229 "You cannot copy another type of multiDim "
230 "into a MultiDimFunctionGraph.");
233 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
236 if (this->
_isReduced_ != src.isReducedAndOrdered())
238 "Cannot copy a Reduced and Ordered "
239 "function graph into Tree function graph "
249 this->
add(**varIter);
251 std::vector< NodeId > lifo;
254 if (src.isTerminalNode(src.root()))
258 this->
manager()->addInternalNode(src.node(src.root())->nodeVar()));
259 src2dest.
insert(src.root(), this->root());
260 lifo.push_back(src.root());
264 while (!lifo.empty()) {
265 NodeId currentSrcNodeId = lifo.back();
268 const InternalNode* currentSrcNode = src.node(currentSrcNodeId);
270 for (
Idx index = 0; index < currentSrcNode->
nbSons(); ++index) {
272 NodeId srcSonNodeId = currentSrcNode->
son(index), destSonNodeId = 0;
273 if (src.isTerminalNode(srcSonNodeId)) {
274 destSonNodeId = this->
manager()->addTerminalNode(src.nodeValue(srcSonNodeId));
277 lifo.push_back(srcSonNodeId);
279 src2dest.
insert(srcSonNodeId, destSonNodeId);
283 src2dest.
second(currentSrcNode->
son(index)));
291 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
295 if (this->
_isReduced_ != src.isReducedAndOrdered())
297 "Cannot copy a Reduced and Ordered "
298 "function graph into Tree function graph "
308 if ((*varIter)->domainSize() != reassign.
second(*varIter)->domainSize())
310 "Var " << (*varIter)->name() <<
" and var " << reassign.
second(*varIter)->name()
311 <<
" have different domain sizes (" << (*varIter)->domainSize()
312 <<
"!=" << reassign.
second(*varIter)->domainSize() <<
")")
313 this->add(*(reassign.
second(*varIter)));
316 std::vector< NodeId > lifo;
319 if (src.isTerminalNode(src.root())) {
320 this->manager()->setRootNode(this->manager()->addTerminalNode(src.nodeValue(src.root())));
322 this->manager()->setRootNode(
323 this->manager()->addInternalNode(reassign.second(src.node(src.root())->nodeVar())));
324 src2dest.insert(src.root(), this->root());
325 lifo.push_back(src.root());
329 while (!lifo.empty()) {
330 NodeId currentSrcNodeId = lifo.back();
333 const InternalNode* currentSrcNode = src.node(currentSrcNodeId);
335 for (Idx index = 0; index < currentSrcNode->nbSons(); ++index) {
336 if (!src2dest.existsFirst(currentSrcNode->son(index))) {
337 NodeId srcSonNodeId = currentSrcNode->son(index), destSonNodeId = 0;
338 if (src.isTerminalNode(srcSonNodeId)) {
339 destSonNodeId = this->manager()->addTerminalNode(src.nodeValue(srcSonNodeId));
341 destSonNodeId = this->manager()->addInternalNode(
342 reassign.second(src.node(srcSonNodeId)->nodeVar()));
343 lifo.push_back(srcSonNodeId);
345 src2dest.insert(srcSonNodeId, destSonNodeId);
347 this->manager()->setSon(src2dest.second(currentSrcNodeId),
349 src2dest.second(currentSrcNode->son(index)));
357 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
361 if (this->
_isReduced_ != src.isReducedAndOrdered())
363 "Cannot copy a Reduced and Ordered "
364 "function graph into Tree function graph "
384 this->
manager()->addInternalNode(src.node(src.root())->nodeVar()));
385 src2dest.insert(src.root(), this->root());
386 lifo.push_back(src.root());
390 while (!lifo.empty()) {
391 NodeId currentSrcNodeId = lifo.back();
394 const InternalNode* currentSrcNode = src.node(currentSrcNodeId);
396 for (
Idx index = 0; index < currentSrcNode->
nbSons(); ++index) {
397 if (!src2dest.exists(currentSrcNode->
son(index))) {
398 NodeId srcSonNodeId = currentSrcNode->
son(index), destSonNodeId = 0;
399 if (src.isTerminalNode(srcSonNodeId)) {
400 destSonNodeId = this->manager()->addTerminalNode(gamma * src.nodeValue(srcSonNodeId));
402 destSonNodeId = this->manager()->addInternalNode(src.node(srcSonNodeId)->nodeVar());
403 lifo.push_back(srcSonNodeId);
405 src2dest.
insert(srcSonNodeId, destSonNodeId);
407 this->manager()->setSon(src2dest[currentSrcNodeId],
409 src2dest[currentSrcNode->
son(index)]);
417 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
429 delete nodeIter.val();
438 delete varIter.val();
446 this->
erase(**varIter);
450 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
453 std::stringstream output;
454 std::stringstream terminalStream;
455 std::stringstream nonTerminalStream;
456 std::stringstream arcstream;
458 output << std::endl <<
"digraph \" " <<
_tableName_ <<
"\" {" << std::endl;
460 terminalStream <<
"node [shape = box];" << std::endl;
461 nonTerminalStream <<
"node [shape = ellipse];" << std::endl;
462 std::string tab =
" ";
466 if (*nodeIter != 0) {
468 terminalStream << tab << *nodeIter <<
";" << tab << *nodeIter <<
" [label=\"" << *nodeIter
474 nonTerminalStream << tab << *nodeIter <<
";" << tab << *nodeIter <<
" [label=\""
475 << *nodeIter <<
" - " << currentNode->
nodeVar()->
name() <<
"\"]"
480 for (
Idx sonIter = 0; sonIter < currentNode->
nbSons(); ++sonIter) {
481 if (!sonMap.
exists(currentNode->
son(sonIter)))
483 sonMap[currentNode->
son(sonIter)]->addLink(sonIter);
486 for (
auto sonIter = sonMap.
beginSafe(); sonIter != sonMap.
endSafe(); ++sonIter) {
487 arcstream << tab << *nodeIter <<
" -> " << sonIter.key() <<
" [label=\" ";
493 arcstream <<
"\",color=\"#0000ff\"]"
495 delete sonIter.val();
500 while (parentIter !=
nullptr) {
501 arcstream << tab << *nodeIter <<
" -> " << parentIter->
element().parentId
502 <<
" [label=\"" << parentIter->
element().modality <<
"\",color=\"#ff0000\"]"
504 parentIter = parentIter->
nextLink();
510 output << terminalStream.str() << std::endl
511 << nonTerminalStream.str() << std::endl
512 << arcstream.str() << std::endl
519 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
526 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
538 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
544 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
551 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
558 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
559 INLINE
const GUM_SCALAR&
567 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
576 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
582 "Var " << var->
name() <<
" has not been inserted in the function graph")
587 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
588 INLINE
const std::string&
594 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
596 const std::string&
name) {
601 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
607 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
614 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
621 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
628 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
635 template <
typename GUM_SCALAR,
template <
class >
class TerminalNodePolicy >
642 currentNodeId = currentNode->
son(inst.
val(*(currentNode->
nodeVar())));
Unsafe Iterators for hashtables.
bool existsFirst(const T1 &first) const
Returns true if first is the first element in a pair in the gum::Bijection.
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
Set of pairs of elements with fast search for both elements.
Base class for discrete random variable.
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
const GUM_SCALAR & terminalNodeValue(const NodeId &n) const
Returns the value of the terminal node that has the given id.
void addTerminalNode(const NodeId &n, const GUM_SCALAR &v)
Insert a new terminal node with given value.
const GUM_SCALAR & value() const
Returns the value of the current terminal nodes pointed by the constant safe iterator.
void clearAllTerminalNodes()
Erase all terminal nodes.
bool existsTerminalNodeWithId(const NodeId &n) const
Returns true if a terminal node matching this id exists.
The class for generic Hash Tables.
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
Class for assigning/browsing values to tuples of discrete variables.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Structure used to represent a node internal structure.
const DiscreteVariable * nodeVar() const
Returns the node variable.
Idx nbSons() const
Returns the number of sons.
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.
Link of a chain list allocated using the SmallObjectAllocator.
const Link< T > * nextLink() const
Returns next link.
const T & element() const
Returns the element stored in this link.
Abstract base class for all multi dimensionnal containers.
virtual std::string toString() const
Returns a representation of this MultiDimContainer.
Class implementingting a function graph manager.
NodeId addInternalNode(const DiscreteVariable *var)
Inserts a new non terminal node in graph.
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
bool _isReduced_
Wheter the MultiDimFunctionGraphManager is reduced or not.
MultiDimFunctionGraph(bool isReduced=true)
Default constructor.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval)
Listen to changes in a given Instantiation.
std::string toDot(bool withBackArcs=false) const
Returns a const reference to the manager of this diagram.
virtual void setDecNotification(const Instantiation &i)
Listen to increment in each recorded Instantiation.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
~MultiDimFunctionGraph()
Class destructor.
const NodeId & root() const
virtual void setChangeNotification(const Instantiation &i)
Listen to an assignment of a value in a Instantiation.
bool isTerminalNode(const NodeId &node) const
void setTableName(const std::string &name)
Sets the name of the table represented by this structure.
bool isInternalNode(const NodeId &node) const
Indicates if given node is terminal or not.
virtual Size realSize() const
Returns the real number of parameters used for this table.
virtual void setIncNotification(const Instantiation &i)
Listen to increment in a given Instantiation.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getTreeInstance()
Returns an arborescent instance.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * _manager_
A reference to the manager that edits this function graph.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
virtual void copy(const MultiDimContainer< GUM_SCALAR > &src)
HashTable< NodeId, InternalNode * > _internalNodeMap_
Associates each non-terminal node to a variable.
HashTable< const DiscreteVariable *, LinkedList< NodeId > * > _var2NodeIdMap_
Mapping between var and node.
const double & nodeValue(NodeId n) const
virtual void setLastNotification(const Instantiation &i)
Listen to setLast in a given Instantiation.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > & operator=(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &from)
Copy Operator.
const LinkedList< NodeId > * varNodeListe(const DiscreteVariable *var) const
Returns the list of node associated to given variable.
bool isReducedAndOrdered() const
Returns true if this MultiDimFunctionGraph is reduced and Ordered.
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
std::string _tableName_
The name of the data structure.
GUM_SCALAR & get_(const Instantiation &inst) const
std::string _name_
The name of the data structure.
void copyAndMultiplyByScalar(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &src, GUM_SCALAR gamma)
Copies src diagrams and multiply every value by the given scalar.
const NodeGraphPart & model() const
Returns a const reference to the manager of this diagram.
void copyAndReassign(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &src, const Bijection< const DiscreteVariable *, const DiscreteVariable * > &reassign)
Copies src diagrams structure into this diagrams.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
const std::string & tableName() const
Returns the name of the table represented by this structure.
virtual void populate(const std::vector< GUM_SCALAR > &v) const
NodeId _root_
The root node of the function graph.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)
virtual void fill(const GUM_SCALAR &d) const
virtual const std::string & name() const
Returns the real name of the multiDim implementation.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src, Instantiation *p_i=(Instantiation *) 0) const
void clear()
Clears the function graph.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
Creates an empty clone of this MultiDimContainer.
NodeGraphPart _model_
Indicates available nodeIds.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
virtual void setFirstNotification(const Instantiation &i)
Listen to setFirst in a given Instantiation.
MultiDimImplementation()
Default constructor.
virtual void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
virtual void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
Class for node sets in graph.
NodeGraphPartIterator NodeIterator
Exception : operation not allowed.
Safe iterators for Sequence.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Headers of MultiDimFunctionGraph.
gum is the global namespace for all aGrUM entities