aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
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) {
31  GUM_CONSTRUCTOR(DAGmodel);
32  }
33 
35  _dag(from._dag), __mutableMoralGraph(nullptr) {
36  GUM_CONS_CPY(DAGmodel);
37  }
38 
40  GUM_DESTRUCTOR(DAGmodel);
42  }
43 
44  void DAGmodel::__moralGraph() const {
45  // @todo : this is a copy of DAG::moralGraph ... we should do better
47  // transform the arcs into edges
48 
49  for (const auto arc: arcs())
50  __mutableMoralGraph->addEdge(arc.first(), arc.second());
51 
52  //}
53 
54  // marry the parents
55  for (const auto node: nodes()) {
56  const auto& par = parents(node);
57 
58  for (auto it1 = par.begin(); it1 != par.end(); ++it1) {
59  auto it2 = it1;
60 
61  for (++it2; it2 != par.end(); ++it2) {
62  // will automatically check if this edge already exists
63  __mutableMoralGraph->addEdge(*it1, *it2);
64  }
65  }
66  }
67  }
68 
70  if (this != &source) {
72 
73  if (__mutableMoralGraph) {
74  delete __mutableMoralGraph;
75  __mutableMoralGraph = nullptr;
76  }
77  _dag = source._dag;
78  }
79 
80  return *this;
81  }
82 
83  const UndiGraph& DAGmodel::moralGraph(bool clear) const {
84  if (clear
85  || (__mutableMoralGraph == nullptr)) { // we have to call _moralGraph
86  if (__mutableMoralGraph == nullptr) {
88  } else {
89  // clear is True ,__mutableMoralGraph exists
91  }
92 
93  __moralGraph();
94  }
95 
96  return *__mutableMoralGraph;
97  }
98 
100  return this->dag().topologicalOrder(clear);
101  }
102 
104  if (this == &other) return true;
105 
106  if (size() != other.size()) return false;
107 
108  if (sizeArcs() != other.sizeArcs()) return false;
109 
110  for (const auto& nid: nodes()) {
111  try {
112  other.idFromName(variable(nid).name());
113  } catch (NotFound) { return false; }
114  }
115 
116  for (const auto& arc: arcs()) {
117  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
118  other.idFromName(variable(arc.head()).name()))))
119  return false;
120  }
121 
122  return true;
123  }
124 } // namespace gum
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
Definition: DAGmodel.cpp:69
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:44
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:47
UndiGraph * __mutableMoralGraph
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:170
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:46
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:42
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:35
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:39
virtual ~DAGmodel()
Destructor.
Definition: DAGmodel.cpp:39
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool hasSameStructure(const DAGmodel &other)
Definition: DAGmodel.cpp:103
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:609
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:162
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:99
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
void __moralGraph() const
Returns the moral graph of this DAGModel.
Definition: DAGmodel.cpp:44
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:60
GraphicalModel & operator=(const GraphicalModel &source)
Private copy operator.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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:83
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:36