aGrUM 3.1.1
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-2026 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-2026 *
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 < GUM_Numeric GUM_SCALAR >
63 class GSpan;
64
65 namespace gspan {
66 template < GUM_Numeric GUM_SCALAR >
67 class DFSTree;
68
69 // clang_format off
79 // clang_format on
80 template < GUM_Numeric 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) = 0;
113
114 virtual bool operator()(LabelData* i, LabelData* j) = 0;
115 virtual bool operator()(Pattern* i, Pattern* j) = 0;
117
118 protected:
119 DFSTree< GUM_SCALAR >* tree_;
120 double computeCost_(const Pattern& p);
121 };
122
132 template < GUM_Numeric GUM_SCALAR >
133 class FrequenceSearch: public SearchStrategy< GUM_SCALAR > {
134 public:
135 // =========================================================================
137 // ==========================================================================
139
141 explicit FrequenceSearch(Size freq);
142
144 FrequenceSearch(const FrequenceSearch& from);
145
147 ~FrequenceSearch() override;
148
151
153 // =========================================================================
155 // ==========================================================================
157
158 bool accept_root(const Pattern* r) override;
159
160 bool accept_growth(const Pattern* parent,
161 const Pattern* child,
162 const EdgeGrowth< GUM_SCALAR >& growth) override;
163
164 bool operator()(LabelData* i, LabelData* j) override;
165 bool operator()(Pattern* i, Pattern* j) override;
167
168 private:
170 };
171
182 template < GUM_Numeric GUM_SCALAR >
183 class StrictSearch: public SearchStrategy< GUM_SCALAR > {
184 public:
185 // =========================================================================
187 // ==========================================================================
189
191 explicit StrictSearch(Size freq = 2);
192
194 StrictSearch(const StrictSearch& from);
195
197 ~StrictSearch() override;
198
200 StrictSearch& operator=(const StrictSearch& from);
201
203 // =========================================================================
205 // ==========================================================================
207
208 bool accept_root(const Pattern* r) override;
209
210 bool accept_growth(const Pattern* parent,
211 const Pattern* child,
212 const EdgeGrowth< GUM_SCALAR >& growth) override;
213
214 bool operator()(LabelData* i, LabelData* j) override;
215 bool operator()(Pattern* i, Pattern* j) override;
217
218 private:
220 double _inner_cost_(const Pattern* p);
221 double _outer_cost_(const Pattern* p);
222 void _compute_costs_(const Pattern* p);
224
245
246 std::string _dot_;
247 std::string _str_(const PRMInstance< GUM_SCALAR >* i,
248 const PRMAttribute< GUM_SCALAR >* a) const;
249 std::string _str_(const PRMInstance< GUM_SCALAR >* i,
250 const PRMAttribute< GUM_SCALAR >& a) const;
251 std::string _str_(const PRMInstance< GUM_SCALAR >* i,
252 const PRMSlotChain< GUM_SCALAR >& a) const;
254 Set< Tensor< GUM_SCALAR >* >& pool,
255 const Sequence< PRMInstance< GUM_SCALAR >* >& match);
256 std::pair< Size, Size > _elimination_cost_(typename StrictSearch< GUM_SCALAR >::PData& data,
257 Set< Tensor< GUM_SCALAR >* >& pool);
258 };
259
269 template < GUM_Numeric GUM_SCALAR >
270 class TreeWidthSearch: public SearchStrategy< GUM_SCALAR > {
271 public:
272 // =========================================================================
274 // ==========================================================================
276
279
281 TreeWidthSearch(const TreeWidthSearch& from);
282
284 ~TreeWidthSearch() override;
285
288
290 // =========================================================================
292 // ==========================================================================
294
295 double cost(const Pattern& p);
296
297 bool accept_root(const Pattern* r) override;
298
299 bool accept_growth(const Pattern* parent,
300 const Pattern* child,
301 const EdgeGrowth< GUM_SCALAR >& growth) override;
302
303 bool operator()(LabelData* i, LabelData* j) override;
304 bool operator()(Pattern* i, Pattern* j) override;
306
307 private:
309 };
310
311
312#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
313# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
314# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
315# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
316 extern template class SearchStrategy< double >;
317# endif
318# endif
319# endif
320#endif
321#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
322# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
323# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
324# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
325 extern template class FrequenceSearch< double >;
326# endif
327# endif
328# endif
329#endif
330#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
331# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
332# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
333# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
334 extern template class StrictSearch< double >;
335# endif
336# endif
337# endif
338#endif
339#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
340# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
341# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
342# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
343 extern template class TreeWidthSearch< double >;
344# endif
345# endif
346# endif
347#endif
348
349
350 } /* namespace gspan */
351 } /* namespace prm */
352} /* namespace gum */
353
355
356#endif /* GUM_DFS_TREE_H */
The class for generic Hash Tables.
Definition hashTable.h:640
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Representation of a set.
Definition set.h:129
Base class for undirected graphs.
Definition undiGraph.h:130
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...
bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth) override
FrequenceSearch(Size freq)
Default constructor.
bool accept_root(const Pattern *r) override
bool operator()(LabelData *i, LabelData *j) override
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
SearchStrategy< GUM_SCALAR > & operator=(const SearchStrategy< GUM_SCALAR > &from)
Copy operator.
virtual bool operator()(Pattern *i, Pattern *j)=0
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.
StrictSearch(Size freq=2)
Default constructor.
bool accept_root(const Pattern *r) override
bool operator()(LabelData *i, LabelData *j) override
HashTable< const Pattern *, std::pair< double, double > > _map_
double _inner_cost_(const Pattern *p)
StrictSearch & operator=(const StrictSearch &from)
Copy operator.
~StrictSearch() override
Destructor.
void _buildPatternGraph_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
void _compute_costs_(const Pattern *p)
std::pair< Size, Size > _elimination_cost_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool)
bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth) override
double _outer_cost_(const Pattern *p)
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...
HashTable< const Pattern *, double > _map_
TreeWidthSearch & operator=(const TreeWidthSearch &from)
Copy operator.
bool accept_root(const Pattern *r) override
bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth) override
bool operator()(LabelData *i, LabelData *j) override
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.