aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
staticTriangulation.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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, const NodeProperty< Size >* domsizes);
91 
92  /// returns the fill-ins added by the triangulation algorithm
93  const EdgeSet& fillIns();
94 
95  /// returns an elimination ordering compatible with the triangulated graph
96  const std::vector< NodeId >& eliminationOrder();
97 
98  /** @brief returns the index of a given node in the elimination order
99  * (0 = first node eliminated) */
100  Idx eliminationOrder(const NodeId);
101 
102  /** @brief returns a table indicating, for each node, at which step it was
103  * deleted by the triangulation process */
105 
106  /// returns the triangulated graph
107  const UndiGraph& triangulatedGraph();
108 
109  /// returns the elimination tree of a compatible ordering
110  const CliqueGraph& eliminationTree();
111 
112  /// returns a compatible junction tree
113  const CliqueGraph& junctionTree();
114 
115  /** @brief returns the Id of the clique of the junction tree created by the
116  * elimination of a given node during the triangulation process */
118 
119  /** @brief returns the Ids of the cliques of the junction tree created by
120  * the elimination of the nodes */
122 
123  /// returns a junction tree of maximal prime subgraphs
124  /** @warning Actually, the cliques of the junction tree are guarranteed to
125  * be maximal prime subgraph of the original graph that was triangulated
126  * only if the triangulation performed is minimal (in the sense that
127  * removing any edge in the triangulated graph results in a nontriangulated
128  * graph). This can be ensured by requiring minimality of the
129  * triangulation. */
131 
132  /** @brief returns the Id of the maximal prime subgraph created by the
133  * elimination of a given node during the triangulation process */
135 
136  /// reinitialize the graph to be triangulated to an empty graph
137  void clear();
138 
139  /// sets/unset the minimality requirement
140  void setMinimalRequirement(bool);
141 
142  /// indicates wether minimality is required
143  virtual bool isMinimalityRequired() const final;
144 
145  /** @brief sets/unsets the record of the fill-ins in the standard
146  * triangulation procedure */
147  void setFillIns(bool);
148 
149  /// returns the graph to be triangulated
150  /** @warning if we have not set yet a graph, then originalGraph () will
151  * return a nullptr. */
152  const UndiGraph* originalGraph() const;
153 
154  /// returns the elimination sequence strategy used by the triangulation
156 
157  /// returns the junction tree strategy used by the triangulation
159 
160  /// @}
161 
162 
163  protected:
164  // ############################################################################
165  /// @name Constructors / Destructors
166  // ############################################################################
167  /// @{
168 
169  /// default constructor: without any graph
170  /** @param elimSeq the elimination sequence used to triangulate the graph
171  * @param JTStrategy the junction tree strategy used to create junction
172  * trees from elimination trees
173  * @param minimality a Boolean indicating whether we should enforce that
174  * the triangulation is minimal w.r.t. inclusion */
175  StaticTriangulation(const EliminationSequenceStrategy& elimSeq,
176  const JunctionTreeStrategy& JTStrategy,
177  bool minimality = false);
178 
179  /// constructor with a given graph
180  /** @param graph the graph to be triangulated, i.e., the nodes of which will
181  * be eliminated
182  * @param dom_sizes the domain sizes of the nodes to be eliminated
183  * @param elimSeq the elimination sequence used to triangulate the graph
184  * @param JTStrategy the junction tree strategy used to create junction
185  * trees
186  * @warning Note that we allow dom_sizes to be defined over nodes/variables
187  * that do not belong to graph. These sizes will simply be ignored. However,
188  * it is compulsory that all the nodes of graph belong to dom_sizes
189  * @param minimality a Boolean indicating whether we should enforce that
190  * the triangulation is minimal w.r.t. inclusion
191  * @warning note that, by aGrUM's rule, the graph is not copied but only
192  * referenced by the triangulation algorithm. */
193  StaticTriangulation(const UndiGraph* graph,
194  const NodeProperty< Size >* dom_sizes,
195  const EliminationSequenceStrategy& elimSeq,
196  const JunctionTreeStrategy& JTStrategy,
197  bool minimality = false);
198 
199  /// forbid copy constructor except in newfactory
201 
202  /// forbid move constructor except in children's constructors
204 
205  /// @}
206 
207 
208  // ############################################################################
209  /// @name Accessors / Modifiers
210  // ############################################################################
211  /// @{
212 
213  /// the function called to initialize the triangulation process
214  /** This function is called when the triangulation process starts and is
215  * used to initialize the elimination sequence strategy. Actually, the
216  * graph that is modified by the triangulation algorithm is a copy of
217  * the original graph, and this copy needs be known by the elimination
218  * sequence strategy. initTriangulation_ is used to transmit this
219  * knowledge to the elimination sequence (through method setGraph of the
220  * elimination sequence class).
221  * @param graph the very graph that is triangulated (this is a copy of
222  * _original_graph_) */
223  virtual void initTriangulation_(UndiGraph& graph);
224 
225  /// @}
226 
227 
228  /// the elimination sequence strategy used by the triangulation
230 
231  /// the junction tree strategy used by the triangulation
233 
234 
235  private:
236  /// a pointer to the (external) original graph (which will be triangulated)
237  const UndiGraph* _original_graph_{nullptr};
238 
239  /// the triangulated graph
241 
242  /// the fill-ins added during the whole triangulation process
244 
245  /// the order in which nodes are eliminated by the algorithm
247 
248  /// the elimination order (access by NodeId)
250 
251  /// the cliques formed by the elimination of the nodes
253 
254  /// the elimination tree computed by the algorithm
256 
257  /// the junction tree computed by the algorithm
258  /** note that the junction tree is owned by the junctionTreeStrategy and,
259  * therefore, its deletion from memory is not handled by the static
260  * triangulation class. */
261  const CliqueGraph* _junction_tree_{nullptr};
262 
263  /// the maximal prime subgraph junction tree computed from the junction tree
265 
266  /** @brief indicates which clique of the max prime junction tree was created
267  * by the elmination of a given node (the key of the table) */
269 
270  /// a boolean indicating whether we have parformed a triangulation
271  bool _has_triangulation_{false};
272 
273  /// a boolean indicating whether we have constructed the triangulated graph
275 
276  /// a boolean indicating whether the elimination tree has been computed
278 
279  /// a boolean indicating whether the junction tree has been constructed
280  bool _has_junction_tree_{false};
281 
282  /** @brief indicates whether a maximal prime subgraph junction tree has
283  * been constructed */
285 
286  /// indicates whether we actually computed fill-ins
287  bool _has_fill_ins_{false};
288 
289  /// indicates whether the triangulation must be minimal
291 
292  /** @brief a vector containing the set of fill-ins added after each node
293  * elimination (used by recursive thinning) */
295 
296  /** @brief a boolean indicating if we want fill-ins list with the standard
297  * triangulation method */
298  bool _we_want_fill_ins_{false};
299 
300  // ===========================================================================
301 
302  /// the function that performs the triangulation
303  void _triangulate_();
304 
305  /// returns an elimination tree from a triangulated graph
307 
308  /// computes the junction tree of the maximal prime subgraphs
310 
311  /// removes redondant fill-ins and compute proper elimination cliques and
312  /// order
314 
315  /// used for computing the junction tree of the maximal prime subgraphs
316  void _computeMaxPrimeMergings_(const NodeId node,
317  const NodeId from,
318  std::vector< Arc >& merged_cliques,
319  NodeSet& mark) const;
320 
321  /// forbid copy operator
323  };
324 
325 } /* namespace gum */
326 
327 #ifndef GUM_NO_INLINE
328 # include <agrum/tools/graphs/algorithms/triangulations/staticTriangulation_inl.h>
329 #endif // GUM_NO_INLINE
330 
331 #endif /* GUM_STATIC_TRIANGULATION_H */
const CliqueGraph & junctionTree()
returns a compatible junction tree
bool _has_triangulated_graph_
a boolean indicating whether we have constructed the triangulated graph
const UndiGraph & triangulatedGraph()
returns the triangulated graph
bool _has_max_prime_junction_tree_
indicates whether a maximal prime subgraph junction tree has been constructed
const UndiGraph * originalGraph() const
returns the graph to be triangulated
CliqueGraph _elim_tree_
the elimination tree computed by the algorithm
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
StaticTriangulation(StaticTriangulation &&)
forbid move constructor except in children&#39;s constructors
void _computeMaxPrimeJunctionTree_()
computes the junction tree of the maximal prime subgraphs
EliminationSequenceStrategy * elimination_sequence_strategy_
the elimination sequence strategy used by the triangulation
CliqueGraph _max_prime_junction_tree_
the maximal prime subgraph junction tree computed from the junction tree
bool _minimality_required_
indicates whether the triangulation must be minimal
virtual ~StaticTriangulation()
destructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void _triangulate_()
the function that performs the triangulation
std::vector< EdgeSet > _added_fill_ins_
a vector containing the set of fill-ins added after each node elimination (used by recursive thinning...
const EdgeSet & fillIns()
returns the fill-ins added by the triangulation algorithm
StaticTriangulation(const StaticTriangulation &)
forbid copy constructor except in newfactory
bool _we_want_fill_ins_
a boolean indicating if we want fill-ins list with the standard triangulation method ...
StaticTriangulation(const UndiGraph *graph, const NodeProperty< Size > *dom_sizes, const EliminationSequenceStrategy &elimSeq, const JunctionTreeStrategy &JTStrategy, bool minimality=false)
constructor with a given graph
NodeId createdMaxPrimeSubgraph(const NodeId id)
returns the Id of the maximal prime subgraph created by the elimination of a given node during the tr...
NodeProperty< NodeId > _reverse_elim_order_
the elimination order (access by NodeId)
EdgeSet _fill_ins_
the fill-ins added during the whole triangulation process
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
UndiGraph _triangulated_graph_
the triangulated graph
bool _has_junction_tree_
a boolean indicating whether the junction tree has been constructed
const CliqueGraph & eliminationTree()
returns the elimination tree of a compatible ordering
bool _has_triangulation_
a boolean indicating whether we have parformed a triangulation
bool _has_fill_ins_
indicates whether we actually computed fill-ins
NodeProperty< NodeSet > _elim_cliques_
the cliques formed by the elimination of the nodes
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 ...
const std::vector< NodeId > & eliminationOrder()
returns an elimination ordering compatible with the triangulated graph
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
virtual void initTriangulation_(UndiGraph &graph)
the function called to initialize the triangulation process
const CliqueGraph * _junction_tree_
the junction tree computed by the algorithm
std::vector< NodeId > _elim_order_
the order in which nodes are eliminated by the algorithm
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 ...
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
const UndiGraph * _original_graph_
a pointer to the (external) original graph (which will be triangulated)
StaticTriangulation & operator=(const StaticTriangulation &)
forbid copy operator
const CliqueGraph & maxPrimeSubgraphTree()
returns a junction tree of maximal prime subgraphs
bool _has_elimination_tree_
a boolean indicating whether the elimination tree has been computed
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...
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 (...
void _computeEliminationTree_()
returns an elimination tree from a triangulated graph
StaticTriangulation(const EliminationSequenceStrategy &elimSeq, const JunctionTreeStrategy &JTStrategy, bool minimality=false)
default constructor: without any graph
void _computeRecursiveThinning_()
removes redondant fill-ins and compute proper elimination cliques and order