aGrUM  0.16.0
gum::prm::gspan::StrictSearch< GUM_SCALAR > Class Template Reference

This is class is an implementation of a strict strategy for the GSpan algorithm. More...

#include <agrum/PRM/gspan/DFSTree.h>

+ Inheritance diagram for gum::prm::gspan::StrictSearch< GUM_SCALAR >:
+ Collaboration diagram for gum::prm::gspan::StrictSearch< GUM_SCALAR >:

Public Member Functions

Constructor and destructor.
 StrictSearch (Size freq=2)
 Default constructor. More...
 
 StrictSearch (const StrictSearch &from)
 Copy constructor. More...
 
virtual ~StrictSearch ()
 Destructor. More...
 
StrictSearchoperator= (const StrictSearch &from)
 Copy operator. More...
 
Search methods.
virtual bool accept_root (const Pattern *r)
 
virtual bool accept_growth (const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth)
 
virtual bool operator() (LabelData *i, LabelData *j)
 
virtual bool operator() (Pattern *i, Pattern *j)
 
Search methods.
void setTree (DFSTree< GUM_SCALAR > *tree)
 

Protected Attributes

DFSTree< GUM_SCALAR > * _tree
 

Protected Member Functions

double _computeCost (const Pattern &p)
 

Classes

struct  PData
 Private structure to represent data about a pattern. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::prm::gspan::StrictSearch< GUM_SCALAR >

This is class is an implementation of a strict strategy for the GSpan algorithm.

This will force early cuts in the DFSTree and should help not spending much time searching for new patterns.

A new growth is accepted if it is at least better than its predecessor.

Definition at line 176 of file searchStrategy.h.

Constructor & Destructor Documentation

◆ StrictSearch() [1/2]

template<typename GUM_SCALAR >
INLINE gum::prm::gspan::StrictSearch< GUM_SCALAR >::StrictSearch ( Size  freq = 2)
explicit

Default constructor.

Definition at line 296 of file searchStrategy_tpl.h.

296  :
297  SearchStrategy< GUM_SCALAR >(), __freq(freq), __dot(".") {
298  GUM_CONSTRUCTOR(StrictSearch);
299  }
StrictSearch(Size freq=2)
Default constructor.

◆ StrictSearch() [2/2]

template<typename GUM_SCALAR >
INLINE gum::prm::gspan::StrictSearch< GUM_SCALAR >::StrictSearch ( const StrictSearch< GUM_SCALAR > &  from)

Copy constructor.

Definition at line 302 of file searchStrategy_tpl.h.

303  :
304  SearchStrategy< GUM_SCALAR >(from),
305  __freq(from.__freq) {
306  GUM_CONS_CPY(StrictSearch);
307  }
StrictSearch(Size freq=2)
Default constructor.

◆ ~StrictSearch()

template<typename GUM_SCALAR >
INLINE gum::prm::gspan::StrictSearch< GUM_SCALAR >::~StrictSearch ( )
virtual

Destructor.

Definition at line 310 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator=().

310  {
311  GUM_DESTRUCTOR(StrictSearch);
312  }
StrictSearch(Size freq=2)
Default constructor.
+ Here is the call graph for this function:

Member Function Documentation

◆ __buildPatternGraph()

template<typename GUM_SCALAR >
void gum::prm::gspan::StrictSearch< GUM_SCALAR >::__buildPatternGraph ( typename StrictSearch< GUM_SCALAR >::PData data,
Set< Potential< GUM_SCALAR > * > &  pool,
const Sequence< PRMInstance< GUM_SCALAR > * > &  match 
)
private

Definition at line 65 of file searchStrategy_tpl.h.

References gum::UndiGraph::addEdge(), gum::NodeGraphPart::addNode(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::first(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::graph, gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::inners, gum::Set< Key, Alloc >::insert(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::insert(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::mod, gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::node2attr, gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::outputs, and gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::vars.

Referenced by gum::prm::gspan::StrictSearch< GUM_SCALAR >::__compute_costs().

68  {
69  for (const auto inst : match) {
70  for (const auto& elt : *inst) {
71  // Adding the node
72  NodeId id = data.graph.addNode();
73  data.node2attr.insert(id, __str(inst, elt.second));
74  data.mod.insert(id, elt.second->type()->domainSize());
75  data.vars.insert(id, &elt.second->type().variable());
76  pool.insert(
77  const_cast< Potential< GUM_SCALAR >* >(&(elt.second->cpf())));
78  }
79  }
80 
81  // Second we add edges and nodes to inners or outputs
82  for (const auto inst : match)
83  for (const auto& elt : *inst) {
84  NodeId node = data.node2attr.first(__str(inst, elt.second));
85  bool found =
86  false; // If this is set at true, then node is an outer node
87 
88  // Children existing in the instance type's DAG
89  for (const auto chld :
90  inst->type().containerDag().children(elt.second->id())) {
91  data.graph.addEdge(
92  node, data.node2attr.first(__str(inst, inst->get(chld))));
93  }
94 
95  // Parents existing in the instance type's DAG
96  for (const auto par :
97  inst->type().containerDag().parents(elt.second->id())) {
98  switch (inst->type().get(par).elt_type()) {
101  data.graph.addEdge(
102  node, data.node2attr.first(__str(inst, inst->get(par))));
103  break;
104  }
105 
107  for (const auto inst2 : inst->getInstances(par))
108  if (match.exists(inst2))
109  data.graph.addEdge(
110  node,
111  data.node2attr.first(
112  __str(inst2,
113  static_cast< const PRMSlotChain< GUM_SCALAR >& >(
114  inst->type().get(par)))));
115 
116  break;
117  }
118 
119  default: { /* Do nothing */
120  }
121  }
122  }
123 
124  // Referring PRMAttribute<GUM_SCALAR>
125  if (inst->hasRefAttr(elt.second->id())) {
126  const std::vector<
127  std::pair< PRMInstance< GUM_SCALAR >*, std::string > >& ref_attr =
128  inst->getRefAttr(elt.second->id());
129 
130  for (auto pair = ref_attr.begin(); pair != ref_attr.end(); ++pair) {
131  if (match.exists(pair->first)) {
132  NodeId id = pair->first->type().get(pair->second).id();
133 
134  for (const auto child :
135  pair->first->type().containerDag().children(id))
136  data.graph.addEdge(node,
137  data.node2attr.first(__str(
138  pair->first, pair->first->get(child))));
139  } else {
140  found = true;
141  }
142  }
143  }
144 
145  if (found)
146  data.outputs.insert(node);
147  else
148  data.inners.insert(node);
149  }
150  }
std::string __str(const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const
Size NodeId
Type for node ids.
Definition: graphElements.h:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __compute_costs()

template<typename GUM_SCALAR >
INLINE void gum::prm::gspan::StrictSearch< GUM_SCALAR >::__compute_costs ( const Pattern p)
private

Definition at line 392 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__buildPatternGraph(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::__elimination_cost(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::__map, gum::prm::gspan::SearchStrategy< GUM_SCALAR >::_computeCost(), and gum::prm::gspan::SearchStrategy< GUM_SCALAR >::_tree.

Referenced by gum::prm::gspan::StrictSearch< GUM_SCALAR >::__inner_cost(), and gum::prm::gspan::StrictSearch< GUM_SCALAR >::__outer_cost().

392  {
393  typename StrictSearch< GUM_SCALAR >::PData data;
394  Set< Potential< GUM_SCALAR >* > pool;
396  data, pool, *(this->_tree->data(*p).iso_map.begin().val()));
397  double inner = std::log(__elimination_cost(data, pool).first);
398  double outer = this->_computeCost(*p);
399  __map.insert(p, std::make_pair(inner, outer));
400  }
void __buildPatternGraph(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Potential< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
std::pair< Size, Size > __elimination_cost(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Potential< GUM_SCALAR > * > &pool)
DFSTree< GUM_SCALAR > * _tree
HashTable< const Pattern *, std::pair< double, double > > __map
double _computeCost(const Pattern &p)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __elimination_cost()

template<typename GUM_SCALAR >
std::pair< Size, Size > gum::prm::gspan::StrictSearch< GUM_SCALAR >::__elimination_cost ( typename StrictSearch< GUM_SCALAR >::PData data,
Set< Potential< GUM_SCALAR > * > &  pool 
)
private

Definition at line 153 of file searchStrategy_tpl.h.

References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::MultiDimDecorator< GUM_SCALAR >::domainSize(), gum::StaticTriangulation::eliminationOrder(), gum::MultiDimDecorator< GUM_SCALAR >::erase(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::graph, gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::inners, gum::Set< Key, Alloc >::insert(), gum::List< Val, Alloc >::insert(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::mod, gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::outputs, gum::Set< Key, Alloc >::size(), and gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::vars.

Referenced by gum::prm::gspan::StrictSearch< GUM_SCALAR >::__compute_costs().

155  {
156  List< NodeSet > partial_order;
157 
158  if (data.inners.size()) partial_order.insert(data.inners);
159 
160  if (data.outputs.size()) partial_order.insert(data.outputs);
161 
162  PartialOrderedTriangulation t(&(data.graph), &(data.mod), &partial_order);
163  const std::vector< NodeId >& elim_order = t.eliminationOrder();
164  Size max(0), max_count(1);
165  Set< Potential< GUM_SCALAR >* > trash;
166  Potential< GUM_SCALAR >* pot = 0;
167 
168  for (size_t idx = 0; idx < data.inners.size(); ++idx) {
169  pot = new Potential< GUM_SCALAR >(new MultiDimSparse< GUM_SCALAR >(0));
170  pot->add(*(data.vars.second(elim_order[idx])));
171  trash.insert(pot);
172  Set< Potential< GUM_SCALAR >* > toRemove;
173 
174  for (const auto p : pool)
175  if (p->contains(*(data.vars.second(elim_order[idx])))) {
176  for (auto var = p->variablesSequence().begin();
177  var != p->variablesSequence().end();
178  ++var) {
179  try {
180  pot->add(**var);
181  } catch (DuplicateElement&) {}
182  }
183 
184  toRemove.insert(p);
185  }
186 
187  if (pot->domainSize() > max) {
188  max = pot->domainSize();
189  max_count = 1;
190  } else if (pot->domainSize() == max) {
191  ++max_count;
192  }
193 
194  for (const auto p : toRemove)
195  pool.erase(p);
196 
197  pot->erase(*(data.vars.second(elim_order[idx])));
198  }
199 
200  for (const auto pot : trash)
201  delete pot;
202 
203  return std::make_pair(max, max_count);
204  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __inner_cost()

template<typename GUM_SCALAR >
INLINE double gum::prm::gspan::StrictSearch< GUM_SCALAR >::__inner_cost ( const Pattern p)
private

Definition at line 351 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__compute_costs(), and gum::prm::gspan::StrictSearch< GUM_SCALAR >::__map.

Referenced by gum::prm::gspan::StrictSearch< GUM_SCALAR >::accept_growth(), and gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator()().

351  {
352  try {
353  return __map[p].first;
354  } catch (NotFound&) {
355  __compute_costs(p);
356  return __map[p].first;
357  }
358  }
HashTable< const Pattern *, std::pair< double, double > > __map
void __compute_costs(const Pattern *p)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __outer_cost()

template<typename GUM_SCALAR >
INLINE double gum::prm::gspan::StrictSearch< GUM_SCALAR >::__outer_cost ( const Pattern p)
private

Definition at line 361 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__compute_costs(), and gum::prm::gspan::StrictSearch< GUM_SCALAR >::__map.

Referenced by gum::prm::gspan::StrictSearch< GUM_SCALAR >::accept_growth(), and gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator()().

361  {
362  try {
363  return __map[p].second;
364  } catch (NotFound&) {
365  __compute_costs(p);
366  return __map[p].second;
367  }
368  }
HashTable< const Pattern *, std::pair< double, double > > __map
void __compute_costs(const Pattern *p)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __str() [1/3]

template<typename GUM_SCALAR >
INLINE std::string gum::prm::gspan::StrictSearch< GUM_SCALAR >::__str ( const PRMInstance< GUM_SCALAR > *  i,
const PRMAttribute< GUM_SCALAR > *  a 
) const
private

Definition at line 371 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__dot, gum::prm::PRMObject::name(), and gum::prm::PRMClassElement< GUM_SCALAR >::safeName().

373  {
374  return i->name() + __dot + a->safeName();
375  }
+ Here is the call graph for this function:

◆ __str() [2/3]

template<typename GUM_SCALAR >
INLINE std::string gum::prm::gspan::StrictSearch< GUM_SCALAR >::__str ( const PRMInstance< GUM_SCALAR > *  i,
const PRMAttribute< GUM_SCALAR > &  a 
) const
private

Definition at line 378 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__dot, gum::prm::PRMObject::name(), and gum::prm::PRMClassElement< GUM_SCALAR >::safeName().

380  {
381  return i->name() + __dot + a.safeName();
382  }
+ Here is the call graph for this function:

◆ __str() [3/3]

template<typename GUM_SCALAR >
INLINE std::string gum::prm::gspan::StrictSearch< GUM_SCALAR >::__str ( const PRMInstance< GUM_SCALAR > *  i,
const PRMSlotChain< GUM_SCALAR > &  a 
) const
private

Definition at line 385 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__dot, gum::prm::PRMSlotChain< GUM_SCALAR >::lastElt(), and gum::prm::PRMObject::name().

387  {
388  return i->name() + __dot + a.lastElt().safeName();
389  }
+ Here is the call graph for this function:

◆ _computeCost()

template<typename GUM_SCALAR >
double gum::prm::gspan::SearchStrategy< GUM_SCALAR >::_computeCost ( const Pattern p)
protectedinherited

Definition at line 36 of file searchStrategy_tpl.h.

References gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::exists(), and gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::insert().

Referenced by gum::prm::gspan::StrictSearch< GUM_SCALAR >::__compute_costs(), and gum::prm::gspan::TreeWidthSearch< GUM_SCALAR >::cost().

36  {
37  double cost = 0;
38  const Sequence< PRMInstance< GUM_SCALAR >* >& seq =
39  *(this->_tree->data(p).iso_map.begin().val());
40  Sequence< PRMClassElement< GUM_SCALAR >* > input_set;
41 
42  for (const auto inst : seq) {
43  for (const auto input : inst->type().slotChains())
44  for (const auto inst2 : inst->getInstances(input->id()))
45  if ((!seq.exists(inst2))
46  && (!input_set.exists(
47  &(inst2->get(input->lastElt().safeName()))))) {
48  cost += std::log(input->type().variable().domainSize());
49  input_set.insert(&(inst2->get(input->lastElt().safeName())));
50  }
51 
52  for (auto vec = inst->beginInvRef(); vec != inst->endInvRef(); ++vec)
53  for (const auto inverse : *vec.val())
54  if (!seq.exists(inverse.first)) {
55  cost +=
56  std::log(inst->get(vec.key()).type().variable().domainSize());
57  break;
58  }
59  }
60 
61  return cost;
62  }
DFSTree< GUM_SCALAR > * _tree
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ accept_growth()

template<typename GUM_SCALAR >
INLINE bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::accept_growth ( const Pattern parent,
const Pattern child,
const EdgeGrowth< GUM_SCALAR > &  growth 
)
virtual

Implements gum::prm::gspan::SearchStrategy< GUM_SCALAR >.

Definition at line 327 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__inner_cost(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::__outer_cost(), and gum::prm::gspan::SearchStrategy< GUM_SCALAR >::_tree.

330  {
331  return __inner_cost(child)
332  + this->_tree->frequency(*child) * __outer_cost(child)
333  < this->_tree->frequency(*child) * __outer_cost(parent);
334  }
double __outer_cost(const Pattern *p)
DFSTree< GUM_SCALAR > * _tree
double __inner_cost(const Pattern *p)
+ Here is the call graph for this function:

◆ accept_root()

template<typename GUM_SCALAR >
INLINE bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::accept_root ( const Pattern r)
virtual

◆ operator()() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator() ( LabelData i,
LabelData j 
)
virtual

Implements gum::prm::gspan::SearchStrategy< GUM_SCALAR >.

Definition at line 344 of file searchStrategy_tpl.h.

References gum::prm::gspan::SearchStrategy< GUM_SCALAR >::_tree, and gum::prm::gspan::LabelData::tree_width.

345  {
346  return i->tree_width * this->_tree->graph().size(i)
347  < j->tree_width * this->_tree->graph().size(j);
348  }
DFSTree< GUM_SCALAR > * _tree

◆ operator()() [2/2]

template<typename GUM_SCALAR >
INLINE bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator() ( gspan::Pattern i,
gspan::Pattern j 
)
virtual

Implements gum::prm::gspan::SearchStrategy< GUM_SCALAR >.

Definition at line 337 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__inner_cost(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::__outer_cost(), and gum::prm::gspan::SearchStrategy< GUM_SCALAR >::_tree.

338  {
339  return __inner_cost(i) + this->_tree->frequency(*i) * __outer_cost(i)
340  < __inner_cost(j) + this->_tree->frequency(*j) * __outer_cost(j);
341  }
double __outer_cost(const Pattern *p)
DFSTree< GUM_SCALAR > * _tree
double __inner_cost(const Pattern *p)
+ Here is the call graph for this function:

◆ operator=()

template<typename GUM_SCALAR >
INLINE StrictSearch< GUM_SCALAR > & gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator= ( const StrictSearch< GUM_SCALAR > &  from)

Copy operator.

Definition at line 316 of file searchStrategy_tpl.h.

References gum::prm::gspan::StrictSearch< GUM_SCALAR >::__freq.

Referenced by gum::prm::gspan::StrictSearch< GUM_SCALAR >::~StrictSearch().

316  {
317  __freq = from.__freq;
318  return *this;
319  }
+ Here is the caller graph for this function:

◆ setTree()

template<typename GUM_SCALAR >
INLINE void gum::prm::gspan::SearchStrategy< GUM_SCALAR >::setTree ( DFSTree< GUM_SCALAR > *  tree)
inherited

Definition at line 233 of file searchStrategy_tpl.h.

References gum::prm::gspan::SearchStrategy< GUM_SCALAR >::_tree.

233  {
234  this->_tree = tree;
235  }
DFSTree< GUM_SCALAR > * _tree

Member Data Documentation

◆ __dot

template<typename GUM_SCALAR>
std::string gum::prm::gspan::StrictSearch< GUM_SCALAR >::__dot
private

◆ __freq

template<typename GUM_SCALAR>
Size gum::prm::gspan::StrictSearch< GUM_SCALAR >::__freq
private

◆ __map

◆ _tree


The documentation for this class was generated from the following files: