56 template < GUM_Numeric GUM_SCALAR >
60 = *(this->
tree_->data(p).iso_map.begin().val());
63 for (
const auto inst: seq) {
64 for (
const auto input: inst->type().slotChains())
65 for (
const auto inst2: inst->getInstances(input->id()))
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())));
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());
83 template < GUM_Numeric GUM_SCALAR >
86 Set< Tensor< GUM_SCALAR >* >& pool,
88 for (
const auto inst: match) {
89 for (
const auto& elt: *inst) {
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())));
100 for (
const auto inst: match)
101 for (
const auto& elt: *inst) {
106 for (
const auto chld: inst->type().containerDag().children(elt.second->id())) {
111 for (
const auto par: inst->type().containerDag().parents(elt.second->id())) {
112 switch (inst->type().get(par).elt_type()) {
120 for (
const auto inst2: inst->getInstances(par))
121 if (match.exists(inst2))
126 inst->type().get(par)))));
137 if (inst->hasRefAttr(elt.second->id())) {
138 const std::vector< std::pair< PRMInstance< GUM_SCALAR >*, std::string > >& ref_attr
139 = inst->getRefAttr(elt.second->id());
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();
145 for (
const auto child: pair->first->type().containerDag().children(
id))
160 template < GUM_Numeric GUM_SCALAR >
163 Set< Tensor< GUM_SCALAR >* >& pool) {
172 Size max(0), max_count(1);
174 Tensor< GUM_SCALAR >* pot = 0;
176 for (
size_t idx = 0; idx < data.
inners.
size(); ++idx) {
178 pot->add(*(data.
vars.second(elim_order[idx])));
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();
194 if (pot->domainSize() > max) {
195 max = pot->domainSize();
197 }
else if (pot->domainSize() == max) {
201 for (
const auto p: toRemove)
204 pot->erase(*(data.
vars.second(elim_order[idx])));
207 for (
const auto pot: trash)
210 return std::make_pair(max, max_count);
214 template < GUM_Numeric GUM_SCALAR >
219 template < GUM_Numeric GUM_SCALAR >
225 template < GUM_Numeric GUM_SCALAR >
230 template < GUM_Numeric GUM_SCALAR >
235 template < GUM_Numeric GUM_SCALAR >
243 template < GUM_Numeric GUM_SCALAR >
249 template < GUM_Numeric GUM_SCALAR >
255 template < GUM_Numeric GUM_SCALAR >
260 template < GUM_Numeric GUM_SCALAR >
267 template < GUM_Numeric GUM_SCALAR >
272 template < GUM_Numeric GUM_SCALAR >
279 template < GUM_Numeric GUM_SCALAR >
282 return this->
tree_->frequency(*i) > this->
tree_->frequency(*j);
285 template < GUM_Numeric GUM_SCALAR >
287 return (this->
tree_->internalGraph().size(i) > this->tree_->internalGraph().size(j));
293 template < GUM_Numeric GUM_SCALAR >
299 template < GUM_Numeric GUM_SCALAR >
305 template < GUM_Numeric GUM_SCALAR >
310 template < GUM_Numeric GUM_SCALAR >
317 template < GUM_Numeric GUM_SCALAR >
322 template < GUM_Numeric GUM_SCALAR >
330 template < GUM_Numeric GUM_SCALAR >
336 template < GUM_Numeric GUM_SCALAR >
342 template < GUM_Numeric GUM_SCALAR >
344 auto pm =
_map_.tryGet(p);
347 pm =
_map_.tryGet(p);
352 template < GUM_Numeric GUM_SCALAR >
354 auto pm =
_map_.tryGet(p);
357 pm =
_map_.tryGet(p);
362 template < GUM_Numeric GUM_SCALAR >
368 template < GUM_Numeric GUM_SCALAR >
374 template < GUM_Numeric GUM_SCALAR >
380 template < GUM_Numeric GUM_SCALAR >
387 _map_.insert(p, std::make_pair(inner, outer));
392 template < GUM_Numeric GUM_SCALAR >
397 template < GUM_Numeric GUM_SCALAR >
403 template < GUM_Numeric GUM_SCALAR >
408 template < GUM_Numeric GUM_SCALAR >
414 template < GUM_Numeric GUM_SCALAR >
416 auto pm =
_map_.tryGet(&p);
419 pm =
_map_.tryGet(&p);
424 template < GUM_Numeric GUM_SCALAR >
428 for (
const auto n: r->
nodes())
431 return tree_width >=
cost(*r);
434 template < GUM_Numeric GUM_SCALAR >
438 return cost(*parent) >=
cost(*child);
441 template < GUM_Numeric GUM_SCALAR >
446 template < GUM_Numeric GUM_SCALAR >
void insert(const T1 &first, const T2 &second)
const T1 & first(const T2 &second) const
Exception : a similar element already exists.
Generic doubly linked lists.
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Multidimensional matrix stored as a sparse array in memory.
virtual NodeId addNode()
insert a new node and return its id
class for graph triangulations for which we enforce a given partial ordering on the nodes elimination...
void insert(const Key &k)
Insert an element at the end of the sequence.
bool exists(const Key &k) const
Check the existence of k in the sequence.
The generic class for storing (ordered) sequences of objects.
void insert(const Key &k)
Inserts a new element into the set.
void erase(const Key &k)
Erases an element from the set.
Size size() const noexcept
Returns the number of elements in the set.
const std::vector< NodeId > & eliminationOrder() override
returns an elimination ordering compatible with the triangulated graph
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
PRMAttribute is a member of a Class in a PRM.
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
const std::string & name() const
Returns the name of this object.
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
This class is used to define an edge growth of a pattern in this DFSTree.
This is class is an implementation of a simple serach strategy for the gspan algorithm: it accept a g...
bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth) override
FrequenceSearch(Size freq)
Default constructor.
bool accept_root(const Pattern *r) override
bool operator()(LabelData *i, LabelData *j) override
FrequenceSearch & operator=(const FrequenceSearch &from)
Copy operator.
~FrequenceSearch() override
Destructor.
This contains all the information we want for a node in a DFSTree.
const NodeGraphPart & nodes() const
LabelData & label(NodeId node)
Returns the LabelData assigned to node.
This is an abstract class used to tune search strategies in the gspan algorithm.
double computeCost_(const Pattern &p)
virtual ~SearchStrategy()
Destructor.
SearchStrategy< GUM_SCALAR > & operator=(const SearchStrategy< GUM_SCALAR > &from)
Copy operator.
DFSTree< GUM_SCALAR > * tree_
void setTree(DFSTree< GUM_SCALAR > *tree)
SearchStrategy()
Default constructor.
This is class is an implementation of a strict strategy for the GSpan algorithm.
StrictSearch(Size freq=2)
Default constructor.
bool accept_root(const Pattern *r) override
bool operator()(LabelData *i, LabelData *j) override
HashTable< const Pattern *, std::pair< double, double > > _map_
double _inner_cost_(const Pattern *p)
StrictSearch & operator=(const StrictSearch &from)
Copy operator.
~StrictSearch() override
Destructor.
void _buildPatternGraph_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
void _compute_costs_(const Pattern *p)
std::pair< Size, Size > _elimination_cost_(typename StrictSearch< GUM_SCALAR >::PData &data, Set< Tensor< GUM_SCALAR > * > &pool)
bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth) override
double _outer_cost_(const Pattern *p)
std::string _str_(const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const
A growth is accepted if and only if the new growth has a tree width less large or equal than its fath...
TreeWidthSearch()
Default constructor.
~TreeWidthSearch() override
Destructor.
HashTable< const Pattern *, double > _map_
TreeWidthSearch & operator=(const TreeWidthSearch &from)
Copy operator.
bool accept_root(const Pattern *r) override
bool accept_growth(const Pattern *parent, const Pattern *child, const EdgeGrowth< GUM_SCALAR > &growth) override
double cost(const Pattern &p)
bool operator()(LabelData *i, LabelData *j) override
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size NodeId
Type for node ids.
namespace for all probabilistic relational models entities
gum is the global namespace for all aGrUM entities
Headers of the SearchStrategy class and child.
Inner class to handle data about labels in this interface graph.
Size tree_width
The size in terms of tree width of the given label.
Private structure to represent data about a pattern.
Bijection< NodeId, std::string > node2attr
A bijection to easily keep track between graph and attributes, its of the form instance_name DOT attr...
NodeProperty< Size > mod
The pattern's variables modalities.
UndiGraph graph
A yet to be triangulated undigraph.
NodeSet outputs
Returns the set of outputs nodes given all the matches of pattern.
NodeSet inners
Returns the set of inner nodes.
Bijection< NodeId, const DiscreteVariable * > vars
Bijection between graph's nodes and their corresponding DiscreteVariable, for inference purpose.