aGrUM  0.16.0
DAGmodel.cpp
Go to the documentation of this file.
1 
24 
25 #ifdef GUM_NO_INLINE
27 #endif /* GUM_NO_INLINE */
28 
29 namespace gum {
30  DAGmodel::DAGmodel() : __mutableMoralGraph(nullptr), __propertiesMap(nullptr) {
31  GUM_CONSTRUCTOR(DAGmodel);
32  }
33 
35  _dag(from._dag), __mutableMoralGraph(nullptr), __propertiesMap(nullptr) {
36  GUM_CONS_CPY(DAGmodel);
37 
38  if (from.__propertiesMap) {
41  }
42  }
43 
45  GUM_DESTRUCTOR(DAGmodel);
46  // Removing previous properties
47 
48  if (__propertiesMap) { delete __propertiesMap; }
49 
51  }
52 
53  void DAGmodel::__moralGraph() const {
54  // @todo : this is a copy of DAG::moralGraph ... we should do better
56  // transform the arcs into edges
57 
58  for (const auto arc : arcs())
59  __mutableMoralGraph->addEdge(arc.first(), arc.second());
60 
61  //}
62 
63  // marry the parents
64  for (const auto node : nodes()) {
65  const auto& par = parents(node);
66 
67  for (auto it1 = par.begin(); it1 != par.end(); ++it1) {
68  auto it2 = it1;
69 
70  for (++it2; it2 != par.end(); ++it2) {
71  // will automatically check if this edge already exists
72  __mutableMoralGraph->addEdge(*it1, *it2);
73  }
74  }
75  }
76  }
77 
79  if (this != &source) {
80  if (__propertiesMap) {
81  delete __propertiesMap;
82  __propertiesMap = nullptr;
83  }
84 
85  if (__mutableMoralGraph) {
86  delete __mutableMoralGraph;
87  __mutableMoralGraph = nullptr;
88  }
89 
90  if (source.__propertiesMap != 0) {
93  }
94 
95  _dag = source._dag;
96  }
97 
98  return *this;
99  }
100 
101  const UndiGraph& DAGmodel::moralGraph(bool clear) const {
102  if (clear
103  || (__mutableMoralGraph == nullptr)) { // we have to call _moralGraph
104  if (__mutableMoralGraph == nullptr) {
106  } else {
107  // clear is True ,__mutableMoralGraph exists
109  }
110 
111  __moralGraph();
112  }
113 
114  return *__mutableMoralGraph;
115  }
116 
118  return this->dag().topologicalOrder(clear);
119  }
120 
122  if (this == &other) return true;
123 
124  if (size() != other.size()) return false;
125 
126  if (sizeArcs() != other.sizeArcs()) return false;
127 
128  for (const auto& nid : nodes()) {
129  try {
130  other.idFromName(variable(nid).name());
131  } catch (NotFound) { return false; }
132  }
133 
134  for (const auto& arc : arcs()) {
135  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
136  other.idFromName(variable(arc.head()).name()))))
137  return false;
138  }
139 
140  return true;
141  }
142 } // namespace gum
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
Definition: DAGmodel.cpp:78
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:104
virtual void clear()
removes all the nodes and edges from the graph
Definition: undiGraph_inl.h:43
Virtual base class for PGMs using a DAG.
Definition: DAGmodel.h:48
UndiGraph * __mutableMoralGraph
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:211
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:106
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:102
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:35
virtual ~DAGmodel()
Destructor.
Definition: DAGmodel.cpp:44
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
HashTable< std::string, std::string > * __propertiesMap
The properties of this Directed Graphical Model. Initialized using a lazy instantiation.
Definition: DAGmodel.h:215
bool hasSameStructure(const DAGmodel &other)
Definition: DAGmodel.cpp:121
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:96
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
const Sequence< NodeId > & topologicalOrder(bool clear=true) const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
Definition: DAGmodel.cpp:117
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void __moralGraph() const
Returns the moral graph of this DAGModel.
Definition: DAGmodel.cpp:53
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
DAGmodel()
Default constructor.
Definition: DAGmodel.cpp:30
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const Sequence< NodeId > & topologicalOrder(bool clear=true) const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
Definition: diGraph.cpp:91
const UndiGraph & moralGraph(bool clear=true) const
The node&#39;s id are coherent with the variables and nodes of the topology.
Definition: DAGmodel.cpp:101
Base class for undirected graphs.
Definition: undiGraph.h:109
void populateNodes(const NodeGraphPart &s)
populateNodes clears *this and fills it with the same nodes as "s"
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:63