aGrUM  0.14.2
searchStrategy.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_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  ***************************************************************************/
27 #ifndef GUM_SEARCHSTRATEGY_H
28 #define GUM_SEARCHSTRATEGY_H
29 
30 #include <list>
31 #include <ostream>
32 #include <utility>
33 #include <vector>
34 
35 #include <agrum/core/math/math.h>
36 #include <agrum/core/bijection.h>
37 #include <agrum/core/sequence.h>
38 #include <agrum/core/set.h>
39 
40 #include <agrum/graphs/diGraph.h>
41 
43 
47 
48 namespace gum {
49  namespace prm {
50 
51  template < typename GUM_SCALAR >
52  class GSpan;
53 
54  namespace gspan {
55  template < typename GUM_SCALAR >
56  class DFSTree;
57 
58  // clang_format off
68  // clang_format on
69  template < typename GUM_SCALAR >
70  class SearchStrategy {
71  public:
72  // =========================================================================
74  // ==========================================================================
76 
78  SearchStrategy< GUM_SCALAR >();
79 
81  SearchStrategy< GUM_SCALAR >(const SearchStrategy< GUM_SCALAR >& from);
82 
84  virtual ~SearchStrategy< GUM_SCALAR >();
85 
87  SearchStrategy< GUM_SCALAR >&
88  operator=(const SearchStrategy< GUM_SCALAR >& from);
89 
91  // =========================================================================
93  // ==========================================================================
95 
96  void setTree(DFSTree< GUM_SCALAR >* tree);
97 
98  virtual bool accept_root(const Pattern* r) = 0;
99 
100  virtual bool accept_growth(const Pattern* parent,
101  const Pattern* child,
102  const EdgeGrowth< GUM_SCALAR >& growth) = 0;
103 
104  virtual bool operator()(LabelData* i, LabelData* j) = 0;
105  virtual bool operator()(Pattern* i, Pattern* j) = 0;
107 
108  protected:
110  double _computeCost(const Pattern& p);
111  };
112 
122  template < typename GUM_SCALAR >
123  class FrequenceSearch : public SearchStrategy< GUM_SCALAR > {
124  public:
125  // =========================================================================
127  // ==========================================================================
129 
131  explicit FrequenceSearch(Size freq);
132 
134  FrequenceSearch(const FrequenceSearch& from);
135 
137  virtual ~FrequenceSearch();
138 
141 
143  // =========================================================================
145  // ==========================================================================
147 
148  virtual bool accept_root(const Pattern* r);
149 
150  virtual bool accept_growth(const Pattern* parent,
151  const Pattern* child,
152  const EdgeGrowth< GUM_SCALAR >& growth);
153 
154  virtual bool operator()(LabelData* i, LabelData* j);
155  virtual bool operator()(Pattern* i, Pattern* j);
157 
158  private:
160  };
161 
172  template < typename GUM_SCALAR >
173  class StrictSearch : public SearchStrategy< GUM_SCALAR > {
174  public:
175  // =========================================================================
177  // ==========================================================================
179 
181  explicit StrictSearch(Size freq = 2);
182 
184  StrictSearch(const StrictSearch& from);
185 
187  virtual ~StrictSearch();
188 
190  StrictSearch& operator=(const StrictSearch& from);
191 
193  // =========================================================================
195  // ==========================================================================
197 
198  virtual bool accept_root(const Pattern* r);
199 
200  virtual bool accept_growth(const Pattern* parent,
201  const Pattern* child,
202  const EdgeGrowth< GUM_SCALAR >& growth);
203 
204  virtual bool operator()(LabelData* i, LabelData* j);
205  virtual bool operator()(Pattern* i, Pattern* j);
207 
208  private:
210  double __inner_cost(const Pattern* p);
211  double __outer_cost(const Pattern* p);
212  void __compute_costs(const Pattern* p);
215  struct PData {
233  };
234  std::string __dot;
235  std::string __str(const PRMInstance< GUM_SCALAR >* i,
236  const PRMAttribute< GUM_SCALAR >* a) const;
237  std::string __str(const PRMInstance< GUM_SCALAR >* i,
238  const PRMAttribute< GUM_SCALAR >& a) const;
239  std::string __str(const PRMInstance< GUM_SCALAR >* i,
240  const PRMSlotChain< GUM_SCALAR >& a) const;
241  void __buildPatternGraph(
242  typename StrictSearch< GUM_SCALAR >::PData& data,
243  Set< Potential< GUM_SCALAR >* >& pool,
244  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
245  std::pair< Size, Size >
246  __elimination_cost(typename StrictSearch< GUM_SCALAR >::PData& data,
247  Set< Potential< GUM_SCALAR >* >& pool);
248  };
249 
259  template < typename GUM_SCALAR >
260  class TreeWidthSearch : public SearchStrategy< GUM_SCALAR > {
261  public:
262  // =========================================================================
264  // ==========================================================================
266 
268  TreeWidthSearch();
269 
271  TreeWidthSearch(const TreeWidthSearch& from);
272 
274  virtual ~TreeWidthSearch();
275 
278 
280  // =========================================================================
282  // ==========================================================================
284 
285  double cost(const Pattern& p);
286 
287  virtual bool accept_root(const Pattern* r);
288 
289  virtual bool accept_growth(const Pattern* parent,
290  const Pattern* child,
291  const EdgeGrowth< GUM_SCALAR >& growth);
292 
293  virtual bool operator()(LabelData* i, LabelData* j);
294  virtual bool operator()(Pattern* i, Pattern* j);
296 
297  private:
299  };
300 
301 
302 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
303 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
304 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
305 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
306  extern template class SearchStrategy< double >;
307 # endif
308 # endif
309 # endif
310 #endif
311 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
312 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
313 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
314 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
315  extern template class FrequenceSearch< double >;
316 # endif
317 # endif
318 # endif
319 #endif
320 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
321 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
322 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
323 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
324  extern template class StrictSearch< double >;
325 # endif
326 # endif
327 # endif
328 #endif
329 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
330 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
331 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
332 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
333  extern template class TreeWidthSearch< double >;
334 # endif
335 # endif
336 # endif
337 #endif
338 
339 
340  } /* namespace gspan */
341  } /* namespace prm */
342 } /* namespace gum */
343 
345 
346 #endif /* GUM_DFS_TREE_H */
Useful macros for maths.
void setTree(DFSTree< GUM_SCALAR > *tree)
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
This class is used to define an edge growth of a pattern in this DFSTree.
Definition: edgeGrowth.h:60
Base classes for oriented graphs.
NodeSet inners
Returns the set of inner nodes.
Header file of gum::Sequence, a class for storing (ordered) sequences of objects. ...
Sets of elements (i.e.
Inner class to handle data about labels in this interface graph.
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:60
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
UndiGraph graph
A yet to be triangulated undigraph.
Bijection< NodeId, const DiscreteVariable *> vars
Bijection between graph&#39;s nodes and their corresponding DiscreteVariable, for inference purpose...
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:676
DFSTree< GUM_SCALAR > * _tree
virtual bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth)=0
A DFSTree is used by gspan to sort lexicographically patterns discovered in an interface graph...
Definition: DFSTree.h:68
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
NodeSet outputs
Returns the set of outputs nodes given all the matches of pattern.
virtual bool accept_root(const Pattern *r)=0
Bijection< NodeId, std::string > node2attr
A bijection to easily keep track between graph and attributes, its of the form instance_name DOT attr...
Headers of the DFSTree class.
NodeProperty< Size > mod
The pattern&#39;s variables modalities.
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
Definition: PRMObject.h:218
Private structure to represent data about a pattern.
Inline implementation of the SearchStrategy class.
HashTable< const Pattern *, std::pair< double, double > > __map
This is class is an implementation of a strict strategy for the GSpan algorithm.
class for graph triangulations for which we enforce a given partial ordering on the nodes elimination...
This is class is an implementation of a simple serach strategy for the gspan algorithm: it accept a g...
double _computeCost(const Pattern &p)
Base class for undirected graphs.
Definition: undiGraph.h:106
Headers of InterfaceGraph.
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:58
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Headers of the Pattern class.
virtual bool operator()(LabelData *i, LabelData *j)=0
This contains all the information we want for a node in a DFSTree.
Definition: pattern.h:70
This is an abstract class used to tune search strategies in the gspan algorithm.
Definition: DFSTree.h:58
Set of pairs of elements with fast search for both elements.
SearchStrategy< GUM_SCALAR > & operator=(const SearchStrategy< GUM_SCALAR > &from)
Copy operator.