aGrUM 3.2.0
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 >:
[legend]
Collaboration diagram for gum::prm::gspan::StrictSearch< GUM_SCALAR >:
[legend]

Classes

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

Public Member Functions

Constructor and destructor.
 StrictSearch (Size freq=2)
 Default constructor.
 StrictSearch (const StrictSearch &from)
 Copy constructor.
 ~StrictSearch () override
 Destructor.
StrictSearch & operator= (const StrictSearch &from)
 Copy operator.
Search methods.
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
bool operator() (Pattern *i, Pattern *j) override
Search methods.
void setTree (DFSTree< GUM_SCALAR > *tree)

Protected Member Functions

double computeCost_ (const Pattern &p)

Protected Attributes

DFSTree< GUM_SCALAR > * tree_

Private Member Functions

double _inner_cost_ (const Pattern *p)
double _outer_cost_ (const Pattern *p)
void _compute_costs_ (const Pattern *p)
std::string _str_ (const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const
std::string _str_ (const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > &a) const
std::string _str_ (const PRMInstance< GUM_SCALAR > *i, const PRMSlotChain< GUM_SCALAR > &a) const
void _buildPatternGraph_ (typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
std::pair< Size, Size > _elimination_cost_ (typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool)

Private Attributes

Size _freq_
HashTable< const Pattern *, std::pair< double, double > > _map_
std::string _dot_

Detailed Description

template<GUM_Numeric 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 183 of file searchStrategy.h.

Constructor & Destructor Documentation

◆ StrictSearch() [1/2]

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

Default constructor.

Definition at line 294 of file searchStrategy_tpl.h.

294 :
297 }
This is class is an implementation of a strict strategy for the GSpan algorithm.
StrictSearch(Size freq=2)
Default constructor.

References gum::prm::gspan::SearchStrategy< GUM_SCALAR >::SearchStrategy(), StrictSearch(), _dot_, and _freq_.

Referenced by StrictSearch(), StrictSearch(), ~StrictSearch(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StrictSearch() [2/2]

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

Copy constructor.

Definition at line 300 of file searchStrategy_tpl.h.

References gum::prm::gspan::SearchStrategy< GUM_SCALAR >::SearchStrategy(), StrictSearch(), and _freq_.

Here is the call graph for this function:

◆ ~StrictSearch()

template<GUM_Numeric GUM_SCALAR>
gum::prm::gspan::StrictSearch< GUM_SCALAR >::~StrictSearch ( )
override

Destructor.

Definition at line 306 of file searchStrategy_tpl.h.

306 {
308 }

References StrictSearch().

Here is the call graph for this function:

Member Function Documentation

◆ _buildPatternGraph_()

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

Definition at line 84 of file searchStrategy_tpl.h.

87 {
88 for (const auto inst: match) {
89 for (const auto& elt: *inst) {
90 // Adding the node
91 NodeId id = data.graph.addNode();
92 data.node2attr.insert(id, _str_(inst, elt.second));
93 data.mod.insert(id, elt.second->type()->domainSize());
94 data.vars.insert(id, &elt.second->type().variable());
95 pool.insert(const_cast< Tensor< GUM_SCALAR >* >(&(elt.second->cpf())));
96 }
97 }
98
99 // Second we add edges and nodes to inners or outputs
100 for (const auto inst: match)
101 for (const auto& elt: *inst) {
102 NodeId node = data.node2attr.first(_str_(inst, elt.second));
103 bool found = false; // If this is set at true, then node is an outer node
104
105 // Children existing in the instance type's DAG
106 for (const auto chld: inst->type().containerDag().children(elt.second->id())) {
107 data.graph.addEdge(node, data.node2attr.first(_str_(inst, inst->get(chld))));
108 }
109
110 // Parents existing in the instance type's DAG
111 for (const auto par: inst->type().containerDag().parents(elt.second->id())) {
112 switch (inst->type().get(par).elt_type()) {
115 data.graph.addEdge(node, data.node2attr.first(_str_(inst, inst->get(par))));
116 break;
117 }
118
120 for (const auto inst2: inst->getInstances(par))
121 if (match.exists(inst2))
122 data.graph.addEdge(node,
123 data.node2attr.first(
124 _str_(inst2,
125 static_cast< const PRMSlotChain< GUM_SCALAR >& >(
126 inst->type().get(par)))));
127
128 break;
129 }
130
131 default : { /* Do nothing */
132 }
133 }
134 }
135
136 // Referring PRMAttribute<GUM_SCALAR>
137 if (inst->hasRefAttr(elt.second->id())) {
139 = inst->getRefAttr(elt.second->id());
140
141 for (auto pair = ref_attr.begin(); pair != ref_attr.end(); ++pair) {
142 if (match.exists(pair->first)) {
143 NodeId id = pair->first->type().get(pair->second).id();
144
145 for (const auto child: pair->first->type().containerDag().children(id))
146 data.graph.addEdge(
147 node,
148 data.node2attr.first(_str_(pair->first, pair->first->get(child))));
149 } else {
150 found = true;
151 }
152 }
153 }
154
155 if (found) data.outputs.insert(node);
156 else data.inners.insert(node);
157 }
158 }
std::string _str_(const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const

References _str_(), gum::UndiGraph::addEdge(), gum::NodeGraphPart::addNode(), gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::first(), gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::graph, gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::inners, gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::insert(), gum::Set< Key >::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, gum::prm::PRMClassElement< GUM_SCALAR >::prm_aggregate, gum::prm::PRMClassElement< GUM_SCALAR >::prm_attribute, gum::prm::PRMClassElement< GUM_SCALAR >::prm_slotchain, and gum::prm::gspan::StrictSearch< GUM_SCALAR >::PData::vars.

Referenced by _compute_costs_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _compute_costs_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::gspan::StrictSearch< GUM_SCALAR >::_compute_costs_ ( const Pattern * p)
private

Definition at line 381 of file searchStrategy_tpl.h.

381 {
384 _buildPatternGraph_(data, pool, *(this->tree_->data(*p).iso_map.begin().val()));
386 double outer = this->computeCost_(*p);
387 _map_.insert(p, std::make_pair(inner, outer));
388 }
double computeCost_(const Pattern &p)
DFSTree< GUM_SCALAR > * tree_
HashTable< const Pattern *, std::pair< double, double > > _map_
void _buildPatternGraph_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
std::pair< Size, Size > _elimination_cost_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool)

References _buildPatternGraph_(), _elimination_cost_(), _map_, gum::prm::gspan::SearchStrategy< GUM_SCALAR >::computeCost_(), and gum::prm::gspan::SearchStrategy< GUM_SCALAR >::tree_.

Referenced by _inner_cost_(), and _outer_cost_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _elimination_cost_()

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

Definition at line 161 of file searchStrategy_tpl.h.

163 {
165
166 if (data.inners.size()) partial_order.insert(data.inners);
167
168 if (data.outputs.size()) partial_order.insert(data.outputs);
169
171 const std::vector< NodeId >& elim_order = t.eliminationOrder();
172 Size max(0), max_count(1);
175
176 for (size_t idx = 0; idx < data.inners.size(); ++idx) {
178 pot->add(*(data.vars.second(elim_order[idx])));
179 trash.insert(pot);
181
182 for (const auto p: pool)
183 if (p->contains(*(data.vars.second(elim_order[idx])))) {
184 for (auto var = p->variablesSequence().begin(); var != p->variablesSequence().end();
185 ++var) {
186 try {
187 pot->add(**var);
188 } catch (DuplicateElement const&) {}
189 }
190
191 toRemove.insert(p);
192 }
193
194 if (pot->domainSize() > max) {
195 max = pot->domainSize();
196 max_count = 1;
197 } else if (pot->domainSize() == max) {
198 ++max_count;
199 }
200
201 for (const auto p: toRemove)
202 pool.erase(p);
203
204 pot->erase(*(data.vars.second(elim_order[idx])));
205 }
206
207 for (const auto pot: trash)
208 delete pot;
209
211 }

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

Referenced by _compute_costs_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _inner_cost_()

template<GUM_Numeric GUM_SCALAR>
double gum::prm::gspan::StrictSearch< GUM_SCALAR >::_inner_cost_ ( const Pattern * p)
private

Definition at line 343 of file searchStrategy_tpl.h.

343 {
344 auto pm = _map_.tryGet(p);
345 if (!pm) {
347 pm = _map_.tryGet(p);
348 }
349 return pm->first;
350 }
void _compute_costs_(const Pattern *p)

References _compute_costs_(), and _map_.

Referenced by accept_growth(), and operator()().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _outer_cost_()

template<GUM_Numeric GUM_SCALAR>
double gum::prm::gspan::StrictSearch< GUM_SCALAR >::_outer_cost_ ( const Pattern * p)
private

Definition at line 353 of file searchStrategy_tpl.h.

353 {
354 auto pm = _map_.tryGet(p);
355 if (!pm) {
357 pm = _map_.tryGet(p);
358 }
359 return pm->second;
360 }

References _compute_costs_(), and _map_.

Referenced by accept_growth(), and operator()().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _str_() [1/3]

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

Definition at line 369 of file searchStrategy_tpl.h.

370 {
371 return i->name() + _dot_ + a.safeName();
372 }

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

Here is the call graph for this function:

◆ _str_() [2/3]

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

Definition at line 363 of file searchStrategy_tpl.h.

364 {
365 return i->name() + _dot_ + a->safeName();
366 }

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

Referenced by _buildPatternGraph_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _str_() [3/3]

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

Definition at line 375 of file searchStrategy_tpl.h.

376 {
377 return i->name() + _dot_ + a.lastElt().safeName();
378 }

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

Here is the call graph for this function:

◆ accept_growth()

template<GUM_Numeric GUM_SCALAR>
bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::accept_growth ( const Pattern * parent,
const Pattern * child,
const EdgeGrowth< GUM_SCALAR > & growth )
overridevirtual

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

Definition at line 323 of file searchStrategy_tpl.h.

325 {
326 return _inner_cost_(child) + this->tree_->frequency(*child) * _outer_cost_(child)
327 < this->tree_->frequency(*child) * _outer_cost_(parent);
328 }
double _inner_cost_(const Pattern *p)
double _outer_cost_(const Pattern *p)

References _inner_cost_(), _outer_cost_(), and gum::prm::gspan::SearchStrategy< GUM_SCALAR >::tree_.

Here is the call graph for this function:

◆ accept_root()

template<GUM_Numeric GUM_SCALAR>
bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::accept_root ( const Pattern * r)
overridevirtual

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

Definition at line 318 of file searchStrategy_tpl.h.

318 {
319 return (this->tree_->frequency(*r) >= _freq_);
320 }

References _freq_, and gum::prm::gspan::SearchStrategy< GUM_SCALAR >::tree_.

◆ computeCost_()

template<GUM_Numeric GUM_SCALAR>
double gum::prm::gspan::SearchStrategy< GUM_SCALAR >::computeCost_ ( const Pattern & p)
protectedinherited

Definition at line 57 of file searchStrategy_tpl.h.

57 {
58 double cost = 0;
60 = *(this->tree_->data(p).iso_map.begin().val());
62
63 for (const auto inst: seq) {
64 for (const auto input: inst->type().slotChains())
65 for (const auto inst2: inst->getInstances(input->id()))
66 if ((!seq.exists(inst2))
67 && (!input_set.exists(&(inst2->get(input->lastElt().safeName()))))) {
68 cost += std::log(input->type().variable().domainSize());
69 input_set.insert(&(inst2->get(input->lastElt().safeName())));
70 }
71
72 for (auto vec = inst->beginInvRef(); vec != inst->endInvRef(); ++vec)
73 for (const auto& inverse: *vec.val())
74 if (!seq.exists(inverse.first)) {
75 cost += std::log(inst->get(vec.key()).type().variable().domainSize());
76 break;
77 }
78 }
79
80 return cost;
81 }
This is an abstract class used to tune search strategies in the gspan algorithm.

References gum::SequenceImplementation< Key, Gen >::exists(), gum::SequenceImplementation< Key, Gen >::insert(), and tree_.

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator()() [1/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator() ( LabelData * i,
LabelData * j )
overridevirtual

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

Definition at line 337 of file searchStrategy_tpl.h.

337 {
338 return i->tree_width * this->tree_->internalGraph().size(i)
339 < j->tree_width * this->tree_->internalGraph().size(j);
340 }

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

◆ operator()() [2/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::prm::gspan::StrictSearch< GUM_SCALAR >::operator() ( gspan::Pattern * i,
gspan::Pattern * j )
overridevirtual

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

Definition at line 331 of file searchStrategy_tpl.h.

331 {
332 return _inner_cost_(i) + this->tree_->frequency(*i) * _outer_cost_(i)
333 < _inner_cost_(j) + this->tree_->frequency(*j) * _outer_cost_(j);
334 }

References _inner_cost_(), _outer_cost_(), and gum::prm::gspan::SearchStrategy< GUM_SCALAR >::tree_.

Here is the call graph for this function:

◆ operator=()

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

Copy operator.

Definition at line 312 of file searchStrategy_tpl.h.

312 {
314 return *this;
315 }

References StrictSearch(), and _freq_.

Here is the call graph for this function:

◆ setTree()

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

Definition at line 236 of file searchStrategy_tpl.h.

236 {
237 this->tree_ = tree;
238 }

References tree_.

Referenced by operator=().

Here is the caller graph for this function:

Member Data Documentation

◆ _dot_

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

Definition at line 246 of file searchStrategy.h.

Referenced by StrictSearch(), _str_(), _str_(), and _str_().

◆ _freq_

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

Definition at line 219 of file searchStrategy.h.

Referenced by StrictSearch(), StrictSearch(), accept_root(), and operator=().

◆ _map_

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

Definition at line 223 of file searchStrategy.h.

Referenced by _compute_costs_(), _inner_cost_(), and _outer_cost_().

◆ tree_


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