aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
searchStrategy.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_SEARCHSTRATEGY_H
50#define GUM_SEARCHSTRATEGY_H
51
52#include <list>
53#include <ostream>
54#include <utility>
55#include <vector>
56
58
59namespace gum {
60 namespace prm {
61
62 template < typename GUM_SCALAR >
63 class GSpan;
64
65 namespace gspan {
66 template < typename GUM_SCALAR >
67 class DFSTree;
68
69 // clang_format off
79 // clang_format on
80 template < typename GUM_SCALAR >
82 public:
83 // =========================================================================
85 // ==========================================================================
87
90
93
95 virtual ~SearchStrategy();
96
99
101 // =========================================================================
103 // ==========================================================================
105
106 void setTree(DFSTree< GUM_SCALAR >* tree);
107
108 virtual bool accept_root(const Pattern* r) = 0;
109
110 virtual bool accept_growth(const Pattern* parent,
111 const Pattern* child,
112 const EdgeGrowth< GUM_SCALAR >& growth)
113 = 0;
114
115 virtual bool operator()(LabelData* i, LabelData* j) = 0;
116 virtual bool operator()(Pattern* i, Pattern* j) = 0;
118
119 protected:
120 DFSTree< GUM_SCALAR >* tree_;
121 double computeCost_(const Pattern& p);
122 };
123
133 template < typename GUM_SCALAR >
134 class FrequenceSearch: public SearchStrategy< GUM_SCALAR > {
135 public:
136 // =========================================================================
138 // ==========================================================================
140
142 explicit FrequenceSearch(Size freq);
143
145 FrequenceSearch(const FrequenceSearch& from);
146
148 virtual ~FrequenceSearch();
149
152
154 // =========================================================================
156 // ==========================================================================
158
159 virtual bool accept_root(const Pattern* r);
160
161 virtual bool accept_growth(const Pattern* parent,
162 const Pattern* child,
163 const EdgeGrowth< GUM_SCALAR >& growth);
164
165 virtual bool operator()(LabelData* i, LabelData* j);
166 virtual bool operator()(Pattern* i, Pattern* j);
168
169 private:
171 };
172
183 template < typename GUM_SCALAR >
184 class StrictSearch: public SearchStrategy< GUM_SCALAR > {
185 public:
186 // =========================================================================
188 // ==========================================================================
190
192 explicit StrictSearch(Size freq = 2);
193
195 StrictSearch(const StrictSearch& from);
196
198 virtual ~StrictSearch();
199
201 StrictSearch& operator=(const StrictSearch& from);
202
204 // =========================================================================
206 // ==========================================================================
208
209 virtual bool accept_root(const Pattern* r);
210
211 virtual bool accept_growth(const Pattern* parent,
212 const Pattern* child,
213 const EdgeGrowth< GUM_SCALAR >& growth);
214
215 virtual bool operator()(LabelData* i, LabelData* j);
216 virtual bool operator()(Pattern* i, Pattern* j);
218
219 private:
221 double _inner_cost_(const Pattern* p);
222 double _outer_cost_(const Pattern* p);
223 void _compute_costs_(const Pattern* p);
225
246
247 std::string _dot_;
248 std::string _str_(const PRMInstance< GUM_SCALAR >* i,
249 const PRMAttribute< GUM_SCALAR >* a) const;
250 std::string _str_(const PRMInstance< GUM_SCALAR >* i,
251 const PRMAttribute< GUM_SCALAR >& a) const;
252 std::string _str_(const PRMInstance< GUM_SCALAR >* i,
253 const PRMSlotChain< GUM_SCALAR >& a) const;
255 Set< Tensor< GUM_SCALAR >* >& pool,
256 const Sequence< PRMInstance< GUM_SCALAR >* >& match);
257 std::pair< Size, Size > _elimination_cost_(typename StrictSearch< GUM_SCALAR >::PData& data,
258 Set< Tensor< GUM_SCALAR >* >& pool);
259 };
260
270 template < typename GUM_SCALAR >
271 class TreeWidthSearch: public SearchStrategy< GUM_SCALAR > {
272 public:
273 // =========================================================================
275 // ==========================================================================
277
280
282 TreeWidthSearch(const TreeWidthSearch& from);
283
285 virtual ~TreeWidthSearch();
286
289
291 // =========================================================================
293 // ==========================================================================
295
296 double cost(const Pattern& p);
297
298 virtual bool accept_root(const Pattern* r);
299
300 virtual bool accept_growth(const Pattern* parent,
301 const Pattern* child,
302 const EdgeGrowth< GUM_SCALAR >& growth);
303
304 virtual bool operator()(LabelData* i, LabelData* j);
305 virtual bool operator()(Pattern* i, Pattern* j);
307
308 private:
310 };
311
312
313#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
314# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
315# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
316# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
317 extern template class SearchStrategy< double >;
318# endif
319# endif
320# endif
321#endif
322#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
323# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
324# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
325# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
326 extern template class FrequenceSearch< double >;
327# endif
328# endif
329# endif
330#endif
331#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
332# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
333# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
334# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
335 extern template class StrictSearch< double >;
336# endif
337# endif
338# endif
339#endif
340#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
341# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
342# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
343# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
344 extern template class TreeWidthSearch< double >;
345# endif
346# endif
347# endif
348#endif
349
350
351 } /* namespace gspan */
352 } /* namespace prm */
353} /* namespace gum */
354
356
357#endif /* GUM_DFS_TREE_H */
The class for generic Hash Tables.
Definition hashTable.h:637
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Representation of a set.
Definition set.h:131
Base class for undirected graphs.
Definition undiGraph.h:128
This class discovers pattern in a PRM<GUM_SCALAR>'s PRMSystem<GUM_SCALAR> to speed up structured infe...
Definition gspan.h:86
PRMAttribute is a member of a Class in a PRM.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
Definition PRMInstance.h:79
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
A DFSTree is used by gspan to sort lexicographically patterns discovered in an interface graph.
Definition DFSTree.h:77
This class is used to define an edge growth of a pattern in this DFSTree.
Definition edgeGrowth.h:73
This is class is an implementation of a simple serach strategy for the gspan algorithm: it accept a g...
FrequenceSearch(Size freq)
Default constructor.
virtual bool operator()(LabelData *i, LabelData *j)
virtual bool accept_root(const Pattern *r)
virtual bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth)
FrequenceSearch & operator=(const FrequenceSearch &from)
Copy operator.
This contains all the information we want for a node in a DFSTree.
Definition pattern.h:90
This is an abstract class used to tune search strategies in the gspan algorithm.
double computeCost_(const Pattern &p)
virtual bool operator()(LabelData *i, LabelData *j)=0
virtual bool operator()(Pattern *i, Pattern *j)=0
SearchStrategy< GUM_SCALAR > & operator=(const SearchStrategy< GUM_SCALAR > &from)
Copy operator.
DFSTree< GUM_SCALAR > * tree_
void setTree(DFSTree< GUM_SCALAR > *tree)
virtual bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth)=0
virtual bool accept_root(const Pattern *r)=0
This is class is an implementation of a strict strategy for the GSpan algorithm.
double _outer_cost_(const Pattern *p)
virtual bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth)
StrictSearch(Size freq=2)
Default constructor.
virtual bool accept_root(const Pattern *r)
StrictSearch & operator=(const StrictSearch &from)
Copy operator.
double _inner_cost_(const Pattern *p)
HashTable< const Pattern *, std::pair< double, double > > _map_
virtual bool operator()(LabelData *i, LabelData *j)
void _compute_costs_(const Pattern *p)
void _buildPatternGraph_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
std::pair< Size, Size > _elimination_cost_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool)
std::string _str_(const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const
A growth is accepted if and only if the new growth has a tree width less large or equal than its fath...
virtual bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth)
HashTable< const Pattern *, double > _map_
TreeWidthSearch & operator=(const TreeWidthSearch &from)
Copy operator.
virtual bool operator()(LabelData *i, LabelData *j)
virtual bool accept_root(const Pattern *r)
Headers of the DFSTree class.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Inline implementation of the SearchStrategy class.
Inner class to handle data about labels in this interface graph.
Private structure to represent data about a pattern.
Bijection< NodeId, std::string > node2attr
A bijection to easily keep track between graph and attributes, its of the form instance_name DOT attr...
NodeProperty< Size > mod
The pattern's variables modalities.
UndiGraph graph
A yet to be triangulated undigraph.
NodeSet outputs
Returns the set of outputs nodes given all the matches of pattern.
NodeSet inners
Returns the set of inner nodes.
Bijection< NodeId, const DiscreteVariable * > vars
Bijection between graph's nodes and their corresponding DiscreteVariable, for inference purpose.