aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
staticTriangulation.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief base class for all non-incremental triangulations.
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef GUM_STATIC_TRIANGULATION_H
28 #define GUM_STATIC_TRIANGULATION_H
29 
30 #include <vector>
31 
32 #include <agrum/agrum.h>
33 
34 #include <agrum/tools/graphs/algorithms/triangulations/eliminationStrategies/eliminationSequenceStrategy.h>
35 #include <agrum/tools/graphs/algorithms/triangulations/junctionTreeStrategies/junctionTreeStrategy.h>
36 #include <agrum/tools/graphs/algorithms/triangulations/triangulation.h>
37 #include <agrum/tools/graphs/cliqueGraph.h>
38 
39 
40 namespace gum {
41 
42 
43  /** @class StaticTriangulation
44  * @brief base class for all non-incremental triangulation methods
45  *
46  * \ingroup graph_group
47  *
48  */
50  public:
51  // ############################################################################
52  /// @name Constructors / Destructors
53  // ############################################################################
54  /// @{
55 
56  /** @brief returns a fresh triangulation of the same type as the current
57  * object but over an empty graph
58  *
59  * note that we return a pointer as it enables subclasses to return
60  * pointers to their types, not Triangulation pointers. See item 25 of the
61  * more effective C++. */
62  virtual StaticTriangulation* newFactory() const = 0;
63 
64  /// virtual copy constructor
65  /** note that we return a pointer as it enables subclasses to return
66  * pointers to their types, not Triangulation pointers. See item 25 of the
67  * more effective C++. */
68  virtual StaticTriangulation* copyFactory() const = 0;
69 
70  /// destructor
71  virtual ~StaticTriangulation();
72 
73  /// @}
74 
75 
76  // ############################################################################
77  /// @name Accessors / Modifiers
78  // ############################################################################
79  /// @{
80 
81  /// initialize the triangulation data structures for a new graph
82  /** @param graph the graph to be triangulated, i.e., the nodes of which
83  * will be eliminated
84  * @param domsizes the domain sizes of the nodes to be eliminated
85  * @warning Note that we allow domsizes to be defined over nodes/variables
86  * that do not belong to graph. These sizes will simply be ignored. However,
87  * it is compulsory that all the nodes of graph belong to dom_sizes
88  * @warning the graph is not copied but only referenced by the elimination
89  * sequence algorithm. */
90  virtual void setGraph(const UndiGraph* graph,
91  const NodeProperty< Size >* domsizes);
92 
93  /// returns the fill-ins added by the triangulation algorithm
94  const EdgeSet& fillIns();
95 
96  /// returns an elimination ordering compatible with the triangulated graph
97  const std::vector< NodeId >& eliminationOrder();
98 
99  /** @brief returns the index of a given node in the elimination order
100  * (0 = first node eliminated) */
101  Idx eliminationOrder(const NodeId);
102 
103  /** @brief returns a table indicating, for each node, at which step it was
104  * deleted by the triangulation process */
106 
107  /// returns the triangulated graph
108  const UndiGraph& triangulatedGraph();
109 
110  /// returns the elimination tree of a compatible ordering
111  const CliqueGraph& eliminationTree();
112 
113  /// returns a compatible junction tree
114  const CliqueGraph& junctionTree();
115 
116  /** @brief returns the Id of the clique of the junction tree created by the
117  * elimination of a given node during the triangulation process */
119 
120  /** @brief returns the Ids of the cliques of the junction tree created by
121  * the elimination of the nodes */
123 
124  /// returns a junction tree of maximal prime subgraphs
125  /** @warning Actually, the cliques of the junction tree are guarranteed to
126  * be maximal prime subgraph of the original graph that was triangulated
127  * only if the triangulation performed is minimal (in the sense that
128  * removing any edge in the triangulated graph results in a nontriangulated
129  * graph). This can be ensured by requiring minimality of the
130  * triangulation. */
132 
133  /** @brief returns the Id of the maximal prime subgraph created by the
134  * elimination of a given node during the triangulation process */
136 
137  /// reinitialize the graph to be triangulated to an empty graph
138  void clear();
139 
140  /// sets/unset the minimality requirement
141  void setMinimalRequirement(bool);
142 
143  /// indicates wether minimality is required
144  virtual bool isMinimalityRequired() const final;
145 
146  /** @brief sets/unsets the record of the fill-ins in the standard
147  * triangulation procedure */
148  void setFillIns(bool);
149 
150  /// returns the graph to be triangulated
151  /** @warning if we have not set yet a graph, then originalGraph () will
152  * return a nullptr. */
153  const UndiGraph* originalGraph() const;
154 
155  /// returns the elimination sequence strategy used by the triangulation
157 
158  /// returns the junction tree strategy used by the triangulation
160 
161  /// @}
162 
163 
164  protected:
165  // ############################################################################
166  /// @name Constructors / Destructors
167  // ############################################################################
168  /// @{
169 
170  /// default constructor: without any graph
171  /** @param elimSeq the elimination sequence used to triangulate the graph
172  * @param JTStrategy the junction tree strategy used to create junction
173  * trees from elimination trees
174  * @param minimality a Boolean indicating whether we should enforce that
175  * the triangulation is minimal w.r.t. inclusion */
176  StaticTriangulation(const EliminationSequenceStrategy& elimSeq,
177  const JunctionTreeStrategy& JTStrategy,
178  bool minimality = false);
179 
180  /// constructor with a given graph
181  /** @param graph the graph to be triangulated, i.e., the nodes of which will
182  * be eliminated
183  * @param dom_sizes the domain sizes of the nodes to be eliminated
184  * @param elimSeq the elimination sequence used to triangulate the graph
185  * @param JTStrategy the junction tree strategy used to create junction
186  * trees
187  * @warning Note that we allow dom_sizes to be defined over nodes/variables
188  * that do not belong to graph. These sizes will simply be ignored. However,
189  * it is compulsory that all the nodes of graph belong to dom_sizes
190  * @param minimality a Boolean indicating whether we should enforce that
191  * the triangulation is minimal w.r.t. inclusion
192  * @warning note that, by aGrUM's rule, the graph is not copied but only
193  * referenced by the triangulation algorithm. */
194  StaticTriangulation(const UndiGraph* graph,
195  const NodeProperty< Size >* dom_sizes,
196  const EliminationSequenceStrategy& elimSeq,
197  const JunctionTreeStrategy& JTStrategy,
198  bool minimality = false);
199 
200  /// forbid copy constructor except in newfactory
202 
203  /// forbid move constructor except in children's constructors
205 
206  /// @}
207 
208 
209  // ############################################################################
210  /// @name Accessors / Modifiers
211  // ############################################################################
212  /// @{
213 
214  /// the function called to initialize the triangulation process
215  /** This function is called when the triangulation process starts and is
216  * used to initialize the elimination sequence strategy. Actually, the
217  * graph that is modified by the triangulation algorithm is a copy of
218  * the original graph, and this copy needs be known by the elimination
219  * sequence strategy. initTriangulation_ is used to transmit this
220  * knowledge to the elimination sequence (through method setGraph of the
221  * elimination sequence class).
222  * @param graph the very graph that is triangulated (this is a copy of
223  * original_graph__) */
224  virtual void initTriangulation_(UndiGraph& graph);
225 
226  /// @}
227 
228 
229  /// the elimination sequence strategy used by the triangulation
231 
232  /// the junction tree strategy used by the triangulation
234 
235 
236  private:
237  /// a pointer to the (external) original graph (which will be triangulated)
238  const UndiGraph* original_graph__{nullptr};
239 
240  /// the triangulated graph
242 
243  /// the fill-ins added during the whole triangulation process
245 
246  /// the order in which nodes are eliminated by the algorithm
248 
249  /// the elimination order (access by NodeId)
251 
252  /// the cliques formed by the elimination of the nodes
254 
255  /// the elimination tree computed by the algorithm
257 
258  /// the junction tree computed by the algorithm
259  /** note that the junction tree is owned by the junctionTreeStrategy and,
260  * therefore, its deletion from memory is not handled by the static
261  * triangulation class. */
262  const CliqueGraph* junction_tree__{nullptr};
263 
264  /// the maximal prime subgraph junction tree computed from the junction tree
266 
267  /** @brief indicates which clique of the max prime junction tree was created
268  * by the elmination of a given node (the key of the table) */
270 
271  /// a boolean indicating whether we have parformed a triangulation
272  bool has_triangulation__{false};
273 
274  /// a boolean indicating whether we have constructed the triangulated graph
276 
277  /// a boolean indicating whether the elimination tree has been computed
279 
280  /// a boolean indicating whether the junction tree has been constructed
281  bool has_junction_tree__{false};
282 
283  /** @brief indicates whether a maximal prime subgraph junction tree has
284  * been constructed */
286 
287  /// indicates whether we actually computed fill-ins
288  bool has_fill_ins__{false};
289 
290  /// indicates whether the triangulation must be minimal
292 
293  /** @brief a vector containing the set of fill-ins added after each node
294  * elimination (used by recursive thinning) */
296 
297  /** @brief a boolean indicating if we want fill-ins list with the standard
298  * triangulation method */
299  bool we_want_fill_ins__{false};
300 
301  // ===========================================================================
302 
303  /// the function that performs the triangulation
304  void triangulate__();
305 
306  /// returns an elimination tree from a triangulated graph
308 
309  /// computes the junction tree of the maximal prime subgraphs
311 
312  /// removes redondant fill-ins and compute proper elimination cliques and
313  /// order
315 
316  /// used for computing the junction tree of the maximal prime subgraphs
317  void computeMaxPrimeMergings__(const NodeId node,
318  const NodeId from,
319  std::vector< Arc >& merged_cliques,
320  NodeSet& mark) const;
321 
322  /// forbid copy operator
324  };
325 
326 } /* namespace gum */
327 
328 #ifndef GUM_NO_INLINE
329 # include <agrum/tools/graphs/algorithms/triangulations/staticTriangulation_inl.h>
330 #endif // GUM_NO_INLINE
331 
332 #endif /* GUM_STATIC_TRIANGULATION_H */
bool has_max_prime_junction_tree__
indicates whether a maximal prime subgraph junction tree has been constructed
const CliqueGraph & junctionTree()
returns a compatible junction tree
const UndiGraph & triangulatedGraph()
returns the triangulated graph
bool has_elimination_tree__
a boolean indicating whether the elimination tree has been computed
void computeMaxPrimeMergings__(const NodeId node, const NodeId from, std::vector< Arc > &merged_cliques, NodeSet &mark) const
used for computing the junction tree of the maximal prime subgraphs
EdgeSet fill_ins__
the fill-ins added during the whole triangulation process
const UndiGraph * originalGraph() const
returns the graph to be triangulated
void clear()
reinitialize the graph to be triangulated to an empty graph
void setMinimalRequirement(bool)
sets/unset the minimality requirement
virtual bool isMinimalityRequired() const final
indicates wether minimality is required
const CliqueGraph * junction_tree__
the junction tree computed by the algorithm
StaticTriangulation(StaticTriangulation &&)
forbid move constructor except in children&#39;s constructors
EliminationSequenceStrategy * elimination_sequence_strategy_
the elimination sequence strategy used by the triangulation
std::vector< NodeId > elim_order__
the order in which nodes are eliminated by the algorithm
bool minimality_required__
indicates whether the triangulation must be minimal
bool has_junction_tree__
a boolean indicating whether the junction tree has been constructed
virtual ~StaticTriangulation()
destructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void triangulate__()
the function that performs the triangulation
const EdgeSet & fillIns()
returns the fill-ins added by the triangulation algorithm
StaticTriangulation(const StaticTriangulation &)
forbid copy constructor except in newfactory
CliqueGraph max_prime_junction_tree__
the maximal prime subgraph junction tree computed from the junction tree
bool has_triangulation__
a boolean indicating whether we have parformed a triangulation
bool has_triangulated_graph__
a boolean indicating whether we have constructed the triangulated graph
CliqueGraph elim_tree__
the elimination tree computed by the algorithm
const UndiGraph * original_graph__
a pointer to the (external) original graph (which will be triangulated)
bool has_fill_ins__
indicates whether we actually computed fill-ins
StaticTriangulation(const UndiGraph *graph, const NodeProperty< Size > *dom_sizes, const EliminationSequenceStrategy &elimSeq, const JunctionTreeStrategy &JTStrategy, bool minimality=false)
constructor with a given graph
NodeProperty< NodeId > node_2_max_prime_clique__
indicates which clique of the max prime junction tree was created by the elmination of a given node (...
NodeId createdMaxPrimeSubgraph(const NodeId id)
returns the Id of the maximal prime subgraph created by the elimination of a given node during the tr...
void computeMaxPrimeJunctionTree__()
computes the junction tree of the maximal prime subgraphs
Idx eliminationOrder(const NodeId)
returns the index of a given node in the elimination order (0 = first node eliminated) ...
JunctionTreeStrategy & junctionTreeStrategy() const
returns the junction tree strategy used by the triangulation
const CliqueGraph & eliminationTree()
returns the elimination tree of a compatible ordering
bool we_want_fill_ins__
a boolean indicating if we want fill-ins list with the standard triangulation method ...
NodeProperty< NodeId > reverse_elim_order__
the elimination order (access by NodeId)
void setFillIns(bool)
sets/unsets the record of the fill-ins in the standard triangulation procedure
const NodeProperty< NodeId > & reverseEliminationOrder()
returns a table indicating, for each node, at which step it was deleted by the triangulation process ...
NodeProperty< NodeSet > elim_cliques__
the cliques formed by the elimination of the nodes
const std::vector< NodeId > & eliminationOrder()
returns an elimination ordering compatible with the triangulated graph
virtual void initTriangulation_(UndiGraph &graph)
the function called to initialize the triangulation process
virtual StaticTriangulation * copyFactory() const =0
virtual copy constructor
virtual StaticTriangulation * newFactory() const =0
returns a fresh triangulation of the same type as the current object but over an empty graph ...
EliminationSequenceStrategy & eliminationSequenceStrategy() const
returns the elimination sequence strategy used by the triangulation
const NodeProperty< NodeId > & createdJunctionTreeCliques()
returns the Ids of the cliques of the junction tree created by the elimination of the nodes ...
std::vector< EdgeSet > added_fill_ins__
a vector containing the set of fill-ins added after each node elimination (used by recursive thinning...
void computeRecursiveThinning__()
removes redondant fill-ins and compute proper elimination cliques and order
base class for all non-incremental triangulation methods
JunctionTreeStrategy * junction_tree_strategy_
the junction tree strategy used by the triangulation
virtual void setGraph(const UndiGraph *graph, const NodeProperty< Size > *domsizes)
initialize the triangulation data structures for a new graph
void computeEliminationTree__()
returns an elimination tree from a triangulated graph
UndiGraph triangulated_graph__
the triangulated graph
StaticTriangulation & operator=(const StaticTriangulation &)
forbid copy operator
const CliqueGraph & maxPrimeSubgraphTree()
returns a junction tree of maximal prime subgraphs
NodeId createdJunctionTreeClique(const NodeId id)
returns the Id of the clique of the junction tree created by the elimination of a given node during t...
StaticTriangulation(const EliminationSequenceStrategy &elimSeq, const JunctionTreeStrategy &JTStrategy, bool minimality=false)
default constructor: without any graph