55 template <
typename GUM_SCALAR >
60 s << &(a.
type().variable()) <<
" - ";
61 s << i.
name() <<
"." << a.
safeName() <<
": input=" << i.
type().isInputNode(class_a);
62 s <<
" output=" << i.
type().isOutputNode(class_a)
63 <<
" inner=" << i.
type().isInnerNode(class_a);
67 template <
typename GUM_SCALAR >
70 s << i.
name() << std::endl;
71 s <<
"Attributes: " << std::endl;
75 if (i.
type().slotChains().size()) {
76 s << std::endl <<
"SlotChains: " << std::endl;
77 for (
auto sc: i.
type().slotChains()) {
78 s << sc->name() <<
" ";
84 template <
typename GUM_SCALAR >
86 std::stringstream str;
93 template <
typename LIST >
98 s << i->name() <<
" ";
104 template <
typename GUM_SCALAR >
108 for (
auto var: pot.variablesSequence()) {
115 template <
typename SET >
126 template <
typename GUM_SCALAR >
145 template <
typename GUM_SCALAR >
157 for (
auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
158 if (!ignore.
exists(child->first)) {
166 }
else if (!eliminated.
exists(child->first)) {
168 delayedVars.
insert(iter.key());
177 std::vector< const DiscreteVariable* > elim_order;
181 for (
auto attr = query->
begin(); attr != query->
end(); ++attr) {
182 pool.
insert(&(
const_cast< Tensor< GUM_SCALAR >&
>((*(attr.val())).cpf())));
189 const auto& var = bn.
variable(var_id);
190 elim_order.push_back(&var);
203 while (!elim_list.
empty()) {
222 for (
const auto chain: query->
type().slotChains())
223 for (
const auto parent: query->
getInstances(chain->id()))
224 if (!ignore.
exists(parent))
228 template <
typename GUM_SCALAR >
237 for (
const auto pot: pool)
238 if (pot->contains(*var)) {
243 for (
const auto pot: toRemove)
247 if (other != var) bucket->
add(*other);
249 Tensor< GUM_SCALAR >* bucket_pot =
new Tensor< GUM_SCALAR >(bucket);
251 pool.insert(bucket_pot);
255 template <
typename GUM_SCALAR >
270 for (
auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
271 if (!ignore.exists(child->first)) {
273 }
else if (!eliminated.exists(child->first)) {
275 delayedVars.
insert(iter.key());
282 eliminated.insert(i);
285 for (
const auto node: my_list) {
287 if (!ignore.exists(node)) {
290 }
else if (node != from) {
291 elim_list.insert(node);
296 for (
const auto chain: i->
type().slotChains()) {
298 if (inst != from) { elim_list.insert(inst); }
303 template <
typename GUM_SCALAR >
313 for (
const auto agg: i->
type().aggregates())
319 std::vector< const DiscreteVariable* > elim;
322 const auto& var = bn.
variable(node);
323 if (delayedVars !=
nullptr) {
324 if (!delayedVars->
exists(node)) {
325 const auto& var = bn.
variable(node);
326 elim.push_back(&var);
329 elim.push_back(&var);
343 template <
typename GUM_SCALAR >
355 for (
auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
356 if (!ignore.exists(child->first)) {
364 eliminated.insert(i);
368 while (!elim_list.empty()) {
370 if (!ignore.exists(elim_list.front())) {
380 tmp_list.
insert(elim_list.front());
383 elim_list.popFront();
387 for (
const auto chain: i->
type().slotChains()) {
389 if (!ignore.exists(parent)) {
396 template <
typename GUM_SCALAR >
404 for (
const auto& elt: this->
evidence(i)) {
406 = i->
type().isInputNode(i->
get(elt.first)) || i->
type().isInnerNode(i->
get(elt.first));
408 if (inner) {
break; }
418 for (
const auto& elt: *i) {
419 tmp_pool.
insert(&(
const_cast< Tensor< GUM_SCALAR >&
>(elt.second->cpf())));
427 std::vector< const DiscreteVariable* > inner_elim_order;
428 std::vector< const DiscreteVariable* > output_elim_order;
430 for (
size_t idx = 0; idx < full_elim_order.size(); ++idx) {
431 auto var_id = full_elim_order[idx];
432 const auto& var = bn.
variable(var_id);
434 if (!i->type().isOutputNode(i->get(full_elim_order[idx]))) {
435 inner_elim_order.push_back(&var);
436 }
else if (delayedVars !=
nullptr) {
437 if (!delayedVars->
exists(full_elim_order[idx])) { output_elim_order.push_back(&var); }
439 output_elim_order.push_back(&var);
446 for (
const auto pot: tmp_pool)
449 if (!output_elim_order.empty())
eliminateNodes(output_elim_order, pool, trash);
456 for (
const auto agg: i->
type().aggregates())
460 std::vector< const DiscreteVariable* > elim;
465 const auto& var = bn.
variable(*iter);
466 if (delayedVars !=
nullptr) {
467 if (!delayedVars->
exists(*iter)) { elim.push_back(&var); }
469 elim.push_back(&var);
480 template <
typename GUM_SCALAR >
493 for (
const auto lifted_pot: *lifted_pool) {
500 template <
typename GUM_SCALAR >
511 lifted_pool->
insert(
const_cast< Tensor< GUM_SCALAR >*
>(&(c.
get(node).cpf())));
537 for (
size_t idx = 0; idx < inners.
size(); ++idx)
550 template <
typename GUM_SCALAR >
554 std::list< NodeId > l;
556 for (
const auto node: cdg.
dag().
nodes()) {
563 visited_node.
insert(l.front());
565 if (!class_elim_order.
exists(cdg.
get(l.front()).first)) {
566 class_elim_order.
insert(cdg.
get(l.front()).first);
569 for (
const auto child: cdg.
dag().
children(l.front())) {
570 if (!visited_node.
contains(child)) { l.push_back(child); }
577 for (
auto c: class_elim_order) {
578 std::string
name = c->name();
579 auto pos =
name.find_first_of(
"<");
580 if (pos != std::string::npos) {
name =
name.substr(0, pos); }
587 template <
typename GUM_SCALAR >
595 std::vector< Tensor< GUM_SCALAR >* > result;
597 for (
const auto pot: pool) {
598 if (pot->contains(elt->
type().
variable())) { result.push_back(pot); }
601 while (result.size() > 1) {
602 auto& p1 = *(result.back());
604 auto& p2 = *(result.back());
606 auto mult =
new Tensor< GUM_SCALAR >(p1 * p2);
608 result.push_back(mult);
611 m = *(result.back());
614 for (
const auto pot: trash) {
619 template <
typename GUM_SCALAR >
624 template <
typename GUM_SCALAR >
628 GUM_CONSTRUCTOR(
SVE);
631 template <
typename GUM_SCALAR >
634 for (
const auto& elt: this->
evidence(i))
635 pool.
insert(
const_cast< Tensor< GUM_SCALAR >*
>(elt.second));
638 template <
typename GUM_SCALAR >
639 INLINE std::vector< NodeId >&
644 template <
typename GUM_SCALAR >
646 auto pos = s.find_first_of(
"<");
647 if (pos != std::string::npos) {
return s.substr(0, pos); }
651 template <
typename GUM_SCALAR >
656 auto first_name =
_trim_(first->
type().name());
657 auto second_name =
_trim_(second->
type().name());
661 template <
typename GUM_SCALAR >
662 INLINE Tensor< GUM_SCALAR >*
665 return &(
const_cast< Tensor< GUM_SCALAR >&
>(i->
get(agg->
id()).cpf()));
668 template <
typename GUM_SCALAR >
673 template <
typename GUM_SCALAR >
678 template <
typename GUM_SCALAR >
691 static std::string dot =
".";
700 template <
typename GUM_SCALAR >
Headers of SVE (Structured Variable Elimination).
Headers of ClassDependencyGraph<GUM_SCALAR>.
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
UndiGraph moralGraph() const
The node's id are coherent with the variables and nodes of the topology.
The default triangulation algorithm used by aGrUM.
Exception : a similar element already exists.
Exception : fatal (unknown ?) error.
Generic doubly linked lists.
Val & front() const
Returns a reference to first element of a list, if any.
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Val & push_back(Args &&... args)
An alias for pushBack used for STL compliance.
void popFront()
Removes the first element of a List, if any.
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
A multidim implementation for buckets.
const gum::VariableSet & allVariables() const
Returns the sequence of all the variables contained in the bucket.
void add(const MultiDimContainer< GUM_SCALAR > &impl)
Add a MultiDimContainer in the bucket.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Exception : the element we looked for cannot be found.
class for graph triangulations for which we enforce a given partial ordering on the nodes elimination...
bool exists(const Key &k) const
Check the existence of k in the sequence.
void insert(const Key &k)
Insert an element at the end of the sequence.
The generic class for storing (ordered) sequences of objects.
Size size() const noexcept
Returns the number of elements in the set.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
void insert(const Key &k)
Inserts a new element into the set.
bool empty() const noexcept
Indicates whether the set is the empty set.
void erase(const Key &k)
Erases an element from the set.
const std::vector< NodeId > & eliminationOrder()
returns an elimination ordering compatible with the triangulated graph
This class decorates a gum::prm::Class<GUM_SCALAR> has an IBaseBayesNet.
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
This class represent the dependencies of all classes in a PRM<GUM_SCALAR>.
const EltPair & get(NodeId id) const
Returns a constant reference over the element assiociated with the node id in the ClassDependencyGrap...
const DAG & dag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
This class decorates an PRMInstance<GUM_SCALAR> as an IBaseBayesNet.
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
PRMAttribute is a member of a Class in a PRM.
virtual PRMType & type()=0
See gum::PRMClassElement::type().
virtual bool isInnerNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if the node is an inner node.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
NodeId id() const
Returns the NodeId of this element in it's class DAG.
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
virtual bool isOutputNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if elt is an output node.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.
bool hasEvidence(const PRMInstance< GUM_SCALAR > &i) const
Returns true if i has evidence.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
const iterator & end()
Returns a reference over the iterator at the end of the list of gum::prm::PRMAttribute<GUM_SCALAR> in...
const Bijection< const DiscreteVariable *, const DiscreteVariable * > & bijection() const
Returns a mapping between DiscreteVariable used in this and the ones used in this PRMInstance<GUM_SCA...
InvRefIterator beginInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
const InvRefIterator & endInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
const Set< PRMInstance< GUM_SCALAR > * > & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
iterator begin()
Returns an iterator at the begining of the list of gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInst...
const std::string & name() const
Returns the name of this object.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Sequence< std::string > * _class_elim_order_
Set< Tensor< GUM_SCALAR > * > BucketSet
Code alias.
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
virtual void evidenceAdded_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceAdded_().
void _initLiftedNodes_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
void _variableElimination_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > _elim_orders_
void _eliminateNodes_(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
void _addDelayedVariable_(const PRMInstance< GUM_SCALAR > *i, const PRMInstance< GUM_SCALAR > *j, NodeId id)
When there is a loop in the references some variable elimination must be delayed, this methods add su...
std::string _trim_(const std::string &s)
Returns true if second can be eliminated before first.
void _eliminateNodesUpward_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
virtual void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)
See PRMInference<GUM_SCALAR>::joint_().
void _initElimOrder_()
Returns true if second can be eliminated before first.
HashTable< std::string, Size > _delayedVariablesCounters_
Some variable must be delayed for more than one PRMInstance<GUM_SCALAR>, when the delayed variable co...
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
typename PRMInference< GUM_SCALAR >::Chain Chain
Code alias.
void _eliminateNodesDownward_(const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
bool _checkElimOrder_(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet * > _lifted_pools_
Tensor< GUM_SCALAR > * _getAggTensor_(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
virtual void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m)
See PRMInference<GUM_SCALAR>::posterior_().
HashTable< const PRMInstance< GUM_SCALAR > *, gum::VariableSet * > _delayedVariables_
void _eliminateDelayedVariables_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
virtual void evidenceRemoved_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceRemoved_().
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
virtual std::string name() const
Returns the name of the current inference algorithm.
#define GUM_ERROR(type, msg)
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
namespace for all probabilistic relational models entities
std::string __print_attribute__(const PRMInstance< GUM_SCALAR > &i, const PRMAttribute< GUM_SCALAR > &a)
std::string __print_pot__(const Tensor< GUM_SCALAR > &pot)
void eliminateNode(const DiscreteVariable *var, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
std::string __print_instance__(const PRMInstance< GUM_SCALAR > &i)
std::string __print_set__(SET set)
std::string __print_list__(LIST l)
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Tensor< GUM_SCALAR > * copyTensor(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Tensor< GUM_SCALAR > &source)
Returns a copy of a Tensor after applying a bijection over the variables in source.
std::string __print_system__(const PRMSystem< GUM_SCALAR > &s)
gum is the global namespace for all aGrUM entities
Set< const DiscreteVariable * > VariableSet