aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
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 175 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 280 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

280  :
281  SearchStrategy< GUM_SCALAR >(), _freq_(freq), _dot_(".") {
282  GUM_CONSTRUCTOR(StrictSearch);
283  }
StrictSearch(Size freq=2)
Default constructor.
+ Here is the call graph for this function:

◆ 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 286 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

286  :
287  SearchStrategy< GUM_SCALAR >(from), _freq_(from._freq_) {
288  GUM_CONS_CPY(StrictSearch);
289  }
StrictSearch(Size freq=2)
Default constructor.
+ Here is the call graph for this function:

◆ ~StrictSearch()

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

Destructor.

Definition at line 292 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

292  {
293  GUM_DESTRUCTOR(StrictSearch);
294  }
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 62 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

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

References gum::prm::gspan::operator<<().

371  {
372  typename StrictSearch< GUM_SCALAR >::PData data;
373  Set< Potential< GUM_SCALAR >* > pool;
374  _buildPatternGraph_(data, pool, *(this->tree_->data(*p).iso_map.begin().val()));
375  double inner = std::log(_elimination_cost_(data, pool).first);
376  double outer = this->computeCost_(*p);
377  _map_.insert(p, std::make_pair(inner, outer));
378  }
HashTable< const Pattern *, std::pair< double, double > > _map_
DFSTree< GUM_SCALAR > * tree_
std::pair< Size, Size > _elimination_cost_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Potential< GUM_SCALAR > * > &pool)
void _buildPatternGraph_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Potential< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
double computeCost_(const Pattern &p)
+ Here is the call 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 141 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

143  {
144  List< NodeSet > partial_order;
145 
146  if (data.inners.size()) partial_order.insert(data.inners);
147 
148  if (data.outputs.size()) partial_order.insert(data.outputs);
149 
150  PartialOrderedTriangulation t(&(data.graph), &(data.mod), &partial_order);
151  const std::vector< NodeId >& elim_order = t.eliminationOrder();
152  Size max(0), max_count(1);
153  Set< Potential< GUM_SCALAR >* > trash;
154  Potential< GUM_SCALAR >* pot = 0;
155 
156  for (size_t idx = 0; idx < data.inners.size(); ++idx) {
157  pot = new Potential< GUM_SCALAR >(new MultiDimSparse< GUM_SCALAR >(0));
158  pot->add(*(data.vars.second(elim_order[idx])));
159  trash.insert(pot);
160  Set< Potential< GUM_SCALAR >* > toRemove;
161 
162  for (const auto p: pool)
163  if (p->contains(*(data.vars.second(elim_order[idx])))) {
164  for (auto var = p->variablesSequence().begin(); var != p->variablesSequence().end();
165  ++var) {
166  try {
167  pot->add(**var);
168  } catch (DuplicateElement&) {}
169  }
170 
171  toRemove.insert(p);
172  }
173 
174  if (pot->domainSize() > max) {
175  max = pot->domainSize();
176  max_count = 1;
177  } else if (pot->domainSize() == max) {
178  ++max_count;
179  }
180 
181  for (const auto p: toRemove)
182  pool.erase(p);
183 
184  pot->erase(*(data.vars.second(elim_order[idx])));
185  }
186 
187  for (const auto pot: trash)
188  delete pot;
189 
190  return std::make_pair(max, max_count);
191  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call 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 330 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

330  {
331  try {
332  return _map_[p].first;
333  } catch (NotFound&) {
334  _compute_costs_(p);
335  return _map_[p].first;
336  }
337  }
HashTable< const Pattern *, std::pair< double, double > > _map_
void _compute_costs_(const Pattern *p)
+ Here is the call 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 340 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

340  {
341  try {
342  return _map_[p].second;
343  } catch (NotFound&) {
344  _compute_costs_(p);
345  return _map_[p].second;
346  }
347  }
HashTable< const Pattern *, std::pair< double, double > > _map_
void _compute_costs_(const Pattern *p)
+ Here is the call 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 351 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

352  {
353  return i->name() + _dot_ + a->safeName();
354  }
+ 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 358 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

359  {
360  return i->name() + _dot_ + a.safeName();
361  }
+ 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 365 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

366  {
367  return i->name() + _dot_ + a.lastElt().safeName();
368  }
+ Here is the call 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 310 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

312  {
313  return _inner_cost_(child) + this->tree_->frequency(*child) * _outer_cost_(child)
314  < this->tree_->frequency(*child) * _outer_cost_(parent);
315  }
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

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

Definition at line 304 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

304  {
305  return (this->tree_->frequency(*r) >= _freq_);
306  }
DFSTree< GUM_SCALAR > * tree_
+ 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 35 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

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

◆ 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 324 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

324  {
325  return i->tree_width * this->tree_->graph().size(i)
326  < j->tree_width * this->tree_->graph().size(j);
327  }
DFSTree< GUM_SCALAR > * tree_
+ Here is the call graph for this function:

◆ 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 318 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

318  {
319  return _inner_cost_(i) + this->tree_->frequency(*i) * _outer_cost_(i)
320  < _inner_cost_(j) + this->tree_->frequency(*j) * _outer_cost_(j);
321  }
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 298 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

298  {
299  _freq_ = from._freq_;
300  return *this;
301  }
+ Here is the call 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 219 of file searchStrategy_tpl.h.

References gum::prm::gspan::operator<<().

219  {
220  this->tree_ = tree;
221  }
DFSTree< GUM_SCALAR > * tree_
+ Here is the call graph for this function:

Member Data Documentation

◆ _dot_

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

Definition at line 236 of file searchStrategy.h.

◆ _freq_

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

Definition at line 211 of file searchStrategy.h.

◆ _map_

template<typename GUM_SCALAR >
HashTable< const Pattern*, std::pair< double, double > > gum::prm::gspan::StrictSearch< GUM_SCALAR >::_map_
private

Definition at line 215 of file searchStrategy.h.

◆ tree_

template<typename GUM_SCALAR >
DFSTree< GUM_SCALAR >* gum::prm::gspan::SearchStrategy< GUM_SCALAR >::tree_
protectedinherited

Definition at line 111 of file searchStrategy.h.


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