55 template < GUM_Numeric GUM_SCALAR >
65 template < GUM_Numeric GUM_SCALAR >
78 for (
const auto attr: attr_set) {
82 if ((!ignore.
exists(iter->first)) && (
_bb_.exists(iter->first)))
91 std::vector< const DiscreteVariable* > elim_order;
95 for (
const auto attr: attr_set)
96 pool.
insert(&(
const_cast< Tensor< GUM_SCALAR >&
>(query->
get(attr).cpf())));
101 const auto& var = bn.
variable(var_id);
102 elim_order.push_back(&var);
111 while (!elim_list.
empty()) {
115 }
else if (
_bb_.exists(elim_list.
front())) {
123 for (
const auto chain: sc_set)
125 if ((!ignore.
exists(parent)) && (
_bb_.exists(*parent)))
129 template < GUM_Numeric GUM_SCALAR >
144 for (
const auto attr: attr_set) {
147 if ((!ignore.exists(iter->first)) && (
_bb_.exists(iter->first)))
158 for (
const auto agg: i->
type().aggregates())
163 std::vector< const DiscreteVariable* > elim_order;
165 for (
auto node: *(*p_eo)) {
166 const auto& var = bn.
variable(node);
167 elim_order.push_back(&var);
175 while (!my_list.
empty()) {
177 if ((!ignore.exists(my_list.
front())) && (
_bb_.exists(my_list.
front())))
179 }
else if (
_bb_.exists(my_list.
front())) {
180 elim_list.insert(my_list.
front());
187 for (
const auto chain: sc_set)
189 if ((!ignore.exists(parent)) &&
_bb_.exists(parent) && (parent != from))
193 template < GUM_Numeric GUM_SCALAR >
206 for (
const auto attr: attr_set) {
209 if ((!ignore.exists(iter->first)) && (
_bb_.exists(iter->first)))
220 for (
const auto agg: i->
type().aggregates())
225 std::vector< const DiscreteVariable* > elim_order;
227 for (
auto node: *(*p_eo)) {
228 const auto& var = bn.
variable(node);
229 elim_order.push_back(&var);
238 while (!elim_list.empty()) {
240 if ((!ignore.exists(elim_list.front())) && (
_bb_.exists(elim_list.front())))
242 }
else if (
_bb_.exists(elim_list.front())) {
243 ignore.insert(elim_list.front());
246 elim_list.popFront();
250 for (
const auto chain: sc_set)
252 if ((!ignore.exists(parent)) && (
_bb_.exists(parent)))
256 template < GUM_Numeric GUM_SCALAR >
261 for (
const auto& elt: this->
evidence(i))
262 if (
_bb_.requisiteNodes(i).exists(elt.first))
263 pool.
insert(
const_cast< Tensor< GUM_SCALAR >*
>(elt.second));
266 for (
const auto& a: *i)
267 if (
_bb_.requisiteNodes(i).exists(a.first))
268 pool.
insert(&(
const_cast< Tensor< GUM_SCALAR >&
>(a.second->cpf())));
275 for (
auto var = full_elim_order.begin(); var != full_elim_order.end(); ++var)
279 template < GUM_Numeric GUM_SCALAR >
292 for (
const auto lifted_pot: *lifted_pool) {
299 template < GUM_Numeric GUM_SCALAR >
306 for (
const auto node:
_bb_.requisiteNodes(i))
308 lifted_pool->
insert(
const_cast< Tensor< GUM_SCALAR >*
>(&(c.
get(node).cpf())));
310 NodeSet inners, outers, ignore;
312 for (
const auto& elt: *i) {
313 if (
_bb_.requisiteNodes(*i).exists(elt.first)) {
316 else if (!outers.
exists(elt.first)) inners.
insert(elt.first);
325 && i->
type().isInnerNode(i->
type().get(par))
326 &&
_bb_.requisiteNodes(i).exists(par)) {
346 GUM_ASSERT(inners.
size() || outers.
size());
350 for (
size_t idx = 0; idx < inners.
size(); ++idx)
361 template < GUM_Numeric GUM_SCALAR >
365 std::list< NodeId > l;
374 visited_node.
insert(l.front());
376 if (!class_elim_order.
exists(cdg.
get(l.front()).first)) {
377 class_elim_order.
insert(cdg.
get(l.front()).first);
381 if (!visited_node.
contains(child)) { l.push_back(child); }
388 for (
auto c: class_elim_order) {
389 std::string
name = c->name();
390 if (
auto pos =
name.find_first_of(
"<"); pos != std::string::npos) {
399 template < GUM_Numeric GUM_SCALAR >
404 _bb_.compute(i, elt->
id());
407 std::vector< const Tensor< GUM_SCALAR >* > result;
408 for (
auto pot: pool) {
409 if (pot->contains(*(m.variablesSequence().atPos(0)))) result.push_back(pot);
412 while (result.size() > 1) {
413 const auto& p1 = *(result.back());
415 const auto& p2 = *(result.back());
417 auto mult =
new Tensor< GUM_SCALAR >(p1 * p2);
418 result.push_back(mult);
422 m = *(result.back());
425 GUM_ASSERT(m.nbrDim() == (
Size)1);
428 for (
const auto pot: trash)
437 delete elt.second.first;
438 delete elt.second.second;
449 template < GUM_Numeric GUM_SCALAR >
454 template < GUM_Numeric GUM_SCALAR >
459 for (
const auto node:
_bb_.requisiteNodes(i)) {
460 switch (i->
type().get(node).elt_type()) {
474 "There should not be elements other"
475 " than PRMAttribute<GUM_SCALAR> and SlotChain.");
484 template < GUM_Numeric GUM_SCALAR >
487 GUM_CONSTRUCTOR(
SVED);
490 template < GUM_Numeric GUM_SCALAR >
492 for (
const auto& elt: this->
evidence(i))
493 pool.
insert(
const_cast< Tensor< GUM_SCALAR >*
>(elt.second));
496 template < GUM_Numeric GUM_SCALAR >
501 template < GUM_Numeric GUM_SCALAR >
503 if (
auto pos = s.find_first_of(
"<"); pos != std::string::npos) {
504 return std::string{s.substr(0, pos)};
506 return std::string{s};
509 template < GUM_Numeric GUM_SCALAR >
514 auto first_name =
_trim_(first->
type().name());
515 auto second_name =
_trim_(second->
type().name());
519 template < GUM_Numeric GUM_SCALAR >
520 Tensor< GUM_SCALAR >*
523 return &(
const_cast< Tensor< GUM_SCALAR >&
>(i->
get(agg->
safeName()).cpf()));
526 template < GUM_Numeric GUM_SCALAR >
531 template < GUM_Numeric GUM_SCALAR >
536 template < GUM_Numeric GUM_SCALAR >
546 template < GUM_Numeric GUM_SCALAR >
556 template < GUM_Numeric GUM_SCALAR >
564 while (!elim_list.empty()) {
566 if ((!ignore.exists(elim_list.front())) && (
_bb_.exists(elim_list.front()))) {
569 }
else if (
_bb_.exists(elim_list.front())) {
570 reduced_list.insert(elim_list.front());
573 elim_list.popFront();
577 template < GUM_Numeric GUM_SCALAR >
Headers of SVED (Structured Value Elimination with d-separation).
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 nodes which consists in the node and its parents returns the set of children of a ...
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.
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
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.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
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.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
bool empty() const noexcept
Indicates whether the set is the empty set.
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
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 & internalDag() 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.
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
const DiscreteVariable & variable(NodeId id) const override
See gum::IBaseBayesNet::variable().
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.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
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.
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) override
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
bool isOutputNode(const PRMClassElement< GUM_SCALAR > &elt) const override
Returns true if elt is an output node.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
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.
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
bool hasRefAttr(NodeId id) const
Returns true if id has at least one referring PRMAttribute<GUM_SCALAR>.
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...
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > & getRefAttr(NodeId id)
Returns a vector of pairs of refering attributes of id.
const Set< PRMInstance< GUM_SCALAR > * > & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > _elim_orders_
Set< Tensor< GUM_SCALAR > * > BucketSet
Code alias.
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.
void evidenceRemoved_(const Chain &chain) override
See PRMInference::evidenceRemoved_().
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.
void _initLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, 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 > & _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.
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.
Set< NodeId > & _getAttrSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
void _initElimOrder_()
Returns true if second can be eliminated before first.
std::string name() const override
Returns the name of the current inference algorithm.
~SVED() override
Destructor.
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > * > > _req_set_
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Tensor< GUM_SCALAR > * _getAggTensor_(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j) override
See PRMInference::joint_().
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.
void evidenceAdded_(const Chain &chain) override
See PRMInference::evidenceAdded_().
typename PRMInference< GUM_SCALAR >::Chain Chain
Code alias.
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
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.
void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m) override
See PRMInference::posterior_().
std::string _trim_(std::string_view s)
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