aGrUM  0.20.2
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 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 299 of file searchStrategy_tpl.h.

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

299  :
300  SearchStrategy< GUM_SCALAR >(), freq__(freq), dot__(".") {
301  GUM_CONSTRUCTOR(StrictSearch);
302  }
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 305 of file searchStrategy_tpl.h.

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

306  :
307  SearchStrategy< GUM_SCALAR >(from),
308  freq__(from.freq__) {
309  GUM_CONS_CPY(StrictSearch);
310  }
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 313 of file searchStrategy_tpl.h.

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

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

Member Function Documentation

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

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

333  {
334  return inner_cost__(child)
335  + this->tree_->frequency(*child) * outer_cost__(child)
336  < this->tree_->frequency(*child) * outer_cost__(parent);
337  }
double outer_cost__(const Pattern *p)
double inner_cost__(const Pattern *p)
DFSTree< GUM_SCALAR > * tree_
+ 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 325 of file searchStrategy_tpl.h.

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

325  {
326  return (this->tree_->frequency(*r) >= freq__);
327  }
DFSTree< GUM_SCALAR > * tree_
+ Here is the call graph for this function:

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

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

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

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

395  {
396  typename StrictSearch< GUM_SCALAR >::PData data;
397  Set< Potential< GUM_SCALAR >* > pool;
398  buildPatternGraph__(data,
399  pool,
400  *(this->tree_->data(*p).iso_map.begin().val()));
401  double inner = std::log(elimination_cost__(data, pool).first);
402  double outer = this->computeCost_(*p);
403  map__.insert(p, std::make_pair(inner, outer));
404  }
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)
HashTable< const Pattern *, std::pair< double, double > > map__
DFSTree< GUM_SCALAR > * tree_
double computeCost_(const Pattern &p)
+ 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(
46  &(inst2->get(input->lastElt().safeName()))))) {
47  cost += std::log(input->type().variable().domainSize());
48  input_set.insert(&(inst2->get(input->lastElt().safeName())));
49  }
50 
51  for (auto vec = inst->beginInvRef(); vec != inst->endInvRef(); ++vec)
52  for (const auto inverse: *vec.val())
53  if (!seq.exists(inverse.first)) {
54  cost += std::log(
55  inst->get(vec.key()).type().variable().domainSize());
56  break;
57  }
58  }
59 
60  return cost;
61  }
DFSTree< GUM_SCALAR > * tree_
+ 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 155 of file searchStrategy_tpl.h.

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

157  {
158  List< NodeSet > partial_order;
159 
160  if (data.inners.size()) partial_order.insert(data.inners);
161 
162  if (data.outputs.size()) partial_order.insert(data.outputs);
163 
164  PartialOrderedTriangulation t(&(data.graph), &(data.mod), &partial_order);
165  const std::vector< NodeId >& elim_order = t.eliminationOrder();
166  Size max(0), max_count(1);
167  Set< Potential< GUM_SCALAR >* > trash;
168  Potential< GUM_SCALAR >* pot = 0;
169 
170  for (size_t idx = 0; idx < data.inners.size(); ++idx) {
171  pot = new Potential< GUM_SCALAR >(new MultiDimSparse< GUM_SCALAR >(0));
172  pot->add(*(data.vars.second(elim_order[idx])));
173  trash.insert(pot);
174  Set< Potential< GUM_SCALAR >* > toRemove;
175 
176  for (const auto p: pool)
177  if (p->contains(*(data.vars.second(elim_order[idx])))) {
178  for (auto var = p->variablesSequence().begin();
179  var != p->variablesSequence().end();
180  ++var) {
181  try {
182  pot->add(**var);
183  } catch (DuplicateElement&) {}
184  }
185 
186  toRemove.insert(p);
187  }
188 
189  if (pot->domainSize() > max) {
190  max = pot->domainSize();
191  max_count = 1;
192  } else if (pot->domainSize() == max) {
193  ++max_count;
194  }
195 
196  for (const auto p: toRemove)
197  pool.erase(p);
198 
199  pot->erase(*(data.vars.second(elim_order[idx])));
200  }
201 
202  for (const auto pot: trash)
203  delete pot;
204 
205  return std::make_pair(max, max_count);
206  }
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 354 of file searchStrategy_tpl.h.

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

354  {
355  try {
356  return map__[p].first;
357  } catch (NotFound&) {
358  compute_costs__(p);
359  return map__[p].first;
360  }
361  }
void compute_costs__(const Pattern *p)
HashTable< const Pattern *, std::pair< double, double > > map__
+ 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 347 of file searchStrategy_tpl.h.

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

348  {
349  return i->tree_width * this->tree_->graph().size(i)
350  < j->tree_width * this->tree_->graph().size(j);
351  }
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 340 of file searchStrategy_tpl.h.

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

341  {
342  return inner_cost__(i) + this->tree_->frequency(*i) * outer_cost__(i)
343  < inner_cost__(j) + this->tree_->frequency(*j) * outer_cost__(j);
344  }
double outer_cost__(const Pattern *p)
double inner_cost__(const Pattern *p)
DFSTree< GUM_SCALAR > * tree_
+ 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 318 of file searchStrategy_tpl.h.

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

319  {
320  freq__ = from.freq__;
321  return *this;
322  }
+ 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 364 of file searchStrategy_tpl.h.

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

364  {
365  try {
366  return map__[p].second;
367  } catch (NotFound&) {
368  compute_costs__(p);
369  return map__[p].second;
370  }
371  }
void compute_costs__(const Pattern *p)
HashTable< const Pattern *, std::pair< double, double > > map__
+ 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 235 of file searchStrategy_tpl.h.

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

235  {
236  this->tree_ = tree;
237  }
DFSTree< GUM_SCALAR > * tree_
+ 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 374 of file searchStrategy_tpl.h.

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

376  {
377  return i->name() + dot__ + a->safeName();
378  }
+ 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 381 of file searchStrategy_tpl.h.

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

383  {
384  return i->name() + dot__ + a.safeName();
385  }
+ 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 388 of file searchStrategy_tpl.h.

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

390  {
391  return i->name() + dot__ + a.lastElt().safeName();
392  }
+ 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 237 of file searchStrategy.h.

◆ freq__

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

Definition at line 212 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 216 of file searchStrategy.h.

◆ tree_

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

Definition at line 112 of file searchStrategy.h.


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