aGrUM  0.14.2
DAGmodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}@lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
22 
23 #ifdef GUM_NO_INLINE
25 #endif /* GUM_NO_INLINE */
26 
27 namespace gum {
28  DAGmodel::DAGmodel() : __mutableMoralGraph(nullptr), __propertiesMap(nullptr) {
29  GUM_CONSTRUCTOR(DAGmodel);
30  }
31 
33  _dag(from._dag), __mutableMoralGraph(nullptr), __propertiesMap(nullptr) {
34  GUM_CONS_CPY(DAGmodel);
35 
36  if (from.__propertiesMap) {
39  }
40  }
41 
43  GUM_DESTRUCTOR(DAGmodel);
44  // Removing previous properties
45 
46  if (__propertiesMap) { delete __propertiesMap; }
47 
49  }
50 
51  void DAGmodel::__moralGraph() const {
52  // @todo : this is a copy of DAG::moralGraph ... we should do better
54  // transform the arcs into edges
55 
56  for (const auto arc : arcs())
57  __mutableMoralGraph->addEdge(arc.first(), arc.second());
58 
59  //}
60 
61  // marry the parents
62  for (const auto node : nodes()) {
63  const auto& par = parents(node);
64 
65  for (auto it1 = par.begin(); it1 != par.end(); ++it1) {
66  auto it2 = it1;
67 
68  for (++it2; it2 != par.end(); ++it2) {
69  // will automatically check if this edge already exists
70  __mutableMoralGraph->addEdge(*it1, *it2);
71  }
72  }
73  }
74  }
75 
77  if (this != &source) {
78  if (__propertiesMap) {
79  delete __propertiesMap;
80  __propertiesMap = nullptr;
81  }
82 
83  if (__mutableMoralGraph) {
84  delete __mutableMoralGraph;
85  __mutableMoralGraph = nullptr;
86  }
87 
88  if (source.__propertiesMap != 0) {
91  }
92 
93  _dag = source._dag;
94  }
95 
96  return *this;
97  }
98 
99  const UndiGraph& DAGmodel::moralGraph(bool clear) const {
100  if (clear
101  || (__mutableMoralGraph == nullptr)) { // we have to call _moralGraph
102  if (__mutableMoralGraph == nullptr) {
104  } else {
105  // clear is True ,__mutableMoralGraph exists
107  }
108 
109  __moralGraph();
110  }
111 
112  return *__mutableMoralGraph;
113  }
114 
116  return this->dag().topologicalOrder(clear);
117  }
118 
120  if (this == &other) return true;
121 
122  if (size() != other.size()) return false;
123 
124  if (sizeArcs() != other.sizeArcs()) return false;
125 
126  for (const auto& nid : nodes()) {
127  try {
128  other.idFromName(variable(nid).name());
129  } catch (NotFound) { return false; }
130  }
131 
132  for (const auto& arc : arcs()) {
133  if (!other.arcs().exists(Arc(other.idFromName(variable(arc.tail()).name()),
134  other.idFromName(variable(arc.head()).name()))))
135  return false;
136  }
137 
138  return true;
139  }
140 } // namespace gum
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
Definition: DAGmodel.cpp:76
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:101
virtual void clear()
removes all the nodes and edges from the graph
Definition: undiGraph_inl.h:40
Virtual base class for PGMs using a DAG.
Definition: DAGmodel.h:45
UndiGraph * __mutableMoralGraph
The moral graph of this Directed Graphical Model.
Definition: DAGmodel.h:208
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:103
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:99
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:32
virtual ~DAGmodel()
Destructor.
Definition: DAGmodel.cpp:42
gum is the global namespace for all aGrUM entities
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:212
bool hasSameStructure(const DAGmodel &other)
Definition: DAGmodel.cpp:119
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:93
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:604
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:200
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:115
Class representing probabilistic DAG model.
void __moralGraph() const
Returns the moral graph of this DAGModel.
Definition: DAGmodel.cpp:51
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
DAGmodel()
Default constructor.
Definition: DAGmodel.cpp:28
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:112
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
Interface-like class encapsulating basic functionalities for a IBayesNet.
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:88
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:99
Base class for undirected graphs.
Definition: undiGraph.h:106
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:60