54 template <
typename GUM_SCALAR >
64 template <
typename GUM_SCALAR >
77 for (
const auto attr: attr_set) {
81 if ((!ignore.
exists(iter->first)) && (
_bb_.exists(iter->first)))
92 std::vector< const DiscreteVariable* > elim_order;
96 for (
const auto attr: attr_set)
97 pool.
insert(&(
const_cast< Tensor< GUM_SCALAR >&
>(query->
get(attr).cpf())));
102 const auto& var = bn.
variable(var_id);
103 elim_order.push_back(&var);
112 while (!elim_list.
empty()) {
116 }
else if (
_bb_.exists(elim_list.
front())) {
124 for (
const auto chain: sc_set)
126 if ((!ignore.
exists(parent)) && (
_bb_.exists(*parent)))
130 template <
typename GUM_SCALAR >
145 for (
const auto attr: attr_set) {
148 if ((!ignore.exists(iter->first)) && (
_bb_.exists(iter->first)))
161 for (
const auto agg: i->
type().aggregates())
166 std::vector< const DiscreteVariable* > elim_order;
169 const auto& var = bn.
variable(node);
170 elim_order.push_back(&var);
180 while (!my_list.
empty()) {
182 if ((!ignore.exists(my_list.
front())) && (
_bb_.exists(my_list.
front())))
184 }
else if (
_bb_.exists(my_list.
front())) {
185 elim_list.insert(my_list.
front());
192 for (
const auto chain: sc_set)
194 if ((!ignore.exists(parent)) &&
_bb_.exists(parent) && (parent != from))
198 template <
typename GUM_SCALAR >
211 for (
const auto attr: attr_set) {
214 if ((!ignore.exists(iter->first)) && (
_bb_.exists(iter->first)))
227 for (
const auto agg: i->
type().aggregates())
232 std::vector< const DiscreteVariable* > elim_order;
235 const auto& var = bn.
variable(node);
236 elim_order.push_back(&var);
247 while (!elim_list.empty()) {
249 if ((!ignore.exists(elim_list.front())) && (
_bb_.exists(elim_list.front())))
251 }
else if (
_bb_.exists(elim_list.front())) {
252 ignore.insert(elim_list.front());
255 elim_list.popFront();
259 for (
const auto chain: sc_set)
261 if ((!ignore.exists(parent)) && (
_bb_.exists(parent)))
265 template <
typename GUM_SCALAR >
270 for (
const auto& elt: this->
evidence(i))
271 if (
_bb_.requisiteNodes(i).exists(elt.first))
272 pool.
insert(
const_cast< Tensor< GUM_SCALAR >*
>(elt.second));
275 for (
const auto& a: *i)
276 if (
_bb_.requisiteNodes(i).exists(a.first))
277 pool.
insert(&(
const_cast< Tensor< GUM_SCALAR >&
>(a.second->cpf())));
284 for (
auto var = full_elim_order.begin(); var != full_elim_order.end(); ++var)
288 template <
typename GUM_SCALAR >
301 for (
const auto lifted_pot: *lifted_pool) {
308 template <
typename GUM_SCALAR >
315 for (
const auto node:
_bb_.requisiteNodes(i))
317 lifted_pool->
insert(
const_cast< Tensor< GUM_SCALAR >*
>(&(c.
get(node).cpf())));
319 NodeSet inners, outers, ignore;
321 for (
const auto& elt: *i) {
322 if (
_bb_.requisiteNodes(*i).exists(elt.first)) {
325 else if (!outers.
exists(elt.first)) inners.
insert(elt.first);
334 && i->
type().isInnerNode(i->
type().get(par))
335 &&
_bb_.requisiteNodes(i).exists(par)) {
355 GUM_ASSERT(inners.
size() || outers.
size());
359 for (
size_t idx = 0; idx < inners.
size(); ++idx)
370 template <
typename GUM_SCALAR >
374 std::list< NodeId > l;
376 for (
const auto node: cdg.
dag().
nodes()) {
383 visited_node.
insert(l.front());
385 if (!class_elim_order.
exists(cdg.
get(l.front()).first)) {
386 class_elim_order.
insert(cdg.
get(l.front()).first);
389 for (
const auto child: cdg.
dag().
children(l.front())) {
390 if (!visited_node.
contains(child)) { l.push_back(child); }
397 for (
auto c: class_elim_order) {
398 std::string
name = c->name();
399 auto pos =
name.find_first_of(
"<");
400 if (pos != std::string::npos) {
name =
name.substr(0, pos); }
407 template <
typename GUM_SCALAR >
412 _bb_.compute(i, elt->
id());
415 std::vector< const Tensor< GUM_SCALAR >* > result;
416 for (
auto pot: pool) {
417 if (pot->contains(*(m.variablesSequence().atPos(0)))) result.push_back(pot);
420 while (result.size() > 1) {
421 const auto& p1 = *(result.back());
423 const auto& p2 = *(result.back());
425 auto mult =
new Tensor< GUM_SCALAR >(p1 * p2);
426 result.push_back(mult);
430 m = *(result.back());
433 GUM_ASSERT(m.nbrDim() == (
Size)1);
436 for (
const auto pot: trash)
445 delete elt.second.first;
446 delete elt.second.second;
457 template <
typename GUM_SCALAR >
462 template <
typename GUM_SCALAR >
467 for (
const auto node:
_bb_.requisiteNodes(i)) {
468 switch (i->
type().get(node).elt_type()) {
482 "There should not be elements other"
483 " than PRMAttribute<GUM_SCALAR> and SlotChain.");
492 template <
typename GUM_SCALAR >
496 GUM_CONSTRUCTOR(
SVED);
499 template <
typename GUM_SCALAR >
502 for (
const auto& elt: this->
evidence(i))
503 pool.
insert(
const_cast< Tensor< GUM_SCALAR >*
>(elt.second));
506 template <
typename GUM_SCALAR >
507 INLINE std::vector< NodeId >&
512 template <
typename GUM_SCALAR >
514 auto pos = s.find_first_of(
"<");
515 if (pos != std::string::npos) {
return s.substr(0, pos); }
519 template <
typename GUM_SCALAR >
524 auto first_name =
_trim_(first->
type().name());
525 auto second_name =
_trim_(second->
type().name());
529 template <
typename GUM_SCALAR >
530 INLINE Tensor< GUM_SCALAR >*
533 return &(
const_cast< Tensor< GUM_SCALAR >&
>(i->
get(agg->
safeName()).cpf()));
536 template <
typename GUM_SCALAR >
542 template <
typename GUM_SCALAR >
548 template <
typename GUM_SCALAR >
558 template <
typename GUM_SCALAR >
568 template <
typename GUM_SCALAR >
576 while (!elim_list.empty()) {
578 if ((!ignore.exists(elim_list.front())) && (
_bb_.exists(elim_list.front()))) {
581 }
else if (
_bb_.exists(elim_list.front())) {
582 reduced_list.insert(elim_list.front());
585 elim_list.popFront();
589 template <
typename GUM_SCALAR >
Headers of SVED (Structured Value Elimination with d-seperation).
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.
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).
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
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 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 Bijection< const DiscreteVariable *, const DiscreteVariable * > & bijection() const
Returns a mapping between DiscreteVariable used in this and the ones used in this PRMInstance<GUM_SCA...
std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > & getRefAttr(NodeId id)
Returns a vector of pairs of refering attributes of id.
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>.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
std::string _trim_(const std::string &s)
Returns true if second can be eliminated before first.
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > _elim_orders_
virtual void evidenceAdded_(const Chain &chain)
See PRMInference::evidenceAdded_().
Set< Tensor< GUM_SCALAR > * > BucketSet
Code alias.
virtual void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)
See PRMInference::joint_().
void _eliminateNodesUpward_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore)
Returns true if second can be eliminated before first.
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
virtual void evidenceRemoved_(const Chain &chain)
See PRMInference::evidenceRemoved_().
virtual void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m)
See PRMInference::posterior_().
void _initLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
void _reduceElimList_(const PRMInstance< GUM_SCALAR > *i, List< const PRMInstance< GUM_SCALAR > * > &elim_list, List< const PRMInstance< GUM_SCALAR > * > &reduced_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
HashTable< const Set< NodeId > *, BucketSet * > _lifted_pools_
The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique for each family of instances wi...
Set< NodeId > & _getAttrSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Sequence< std::string > * _class_elim_order_
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Tensor< GUM_SCALAR > * _getAggTensor_(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
void _initElimOrder_()
Returns true if second can be eliminated before first.
virtual std::string name() const
Returns the name of the current inference algorithm.
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > * > > _req_set_
First pair -> requisite Attributes Second pair -> requisite SlotChains.
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Set< NodeId > & _getSCSet_(const PRMInstance< GUM_SCALAR > *i)
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.
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
void _eliminateNodes_(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
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)
Returns true if second can be eliminated before first.
StructuredBayesBall< GUM_SCALAR > _bb_
void _initReqSets_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
namespace for all probabilistic relational models entities
void eliminateNode(const DiscreteVariable *var, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
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.
gum is the global namespace for all aGrUM entities