56 template < GUM_Numeric GUM_SCALAR >
61 delete elt.second.first;
64 template < GUM_Numeric GUM_SCALAR >
67 delete elt.second.first;
73 template < GUM_Numeric GUM_SCALAR >
76 if (!i->
exists(n))
return false;
79 if (
_inf_->hasEvidence(chain)) {
80 const Tensor< GUM_SCALAR >* e =
_inf_->evidence(i)[n];
85 if ((e->get(inst) == (GUM_SCALAR)1.0)) ++count;
86 else if (e->get(inst) != (GUM_SCALAR)0.0)
return false;
95 template < GUM_Numeric GUM_SCALAR >
105 for (
const auto& elt: marks)
109 template < GUM_Numeric GUM_SCALAR >
113 auto p_marks = marks.
tryGet(i);
116 p_marks = marks.
tryGet(i);
119 if (!(*p_marks)->exists(n)) { (*p_marks)->insert(n, std::pair< bool, bool >(
false,
false)); }
122 switch (i->
type().get(n).elt_type()) {
134 for (
const auto chi: i->
type().containerDag().children(n))
147 for (
const auto par: i->
type().containerDag().parents(n))
155 for (
const auto chi: i->
type().containerDag().children(n))
162 for (
auto iter = refs.begin(); iter != refs.end(); ++iter)
163 _fromParent_(iter->first, iter->first->type().get(iter->second).id(), marks);
180 template < GUM_Numeric GUM_SCALAR >
184 auto p_marks = marks.
tryGet(i);
187 p_marks = marks.
tryGet(i);
190 if (!(*p_marks)->exists(n)) { (*p_marks)->insert(n, std::pair< bool, bool >(
false,
false)); }
196 for (
const auto par: i->
type().containerDag().parents(n))
198 }
else if (!
_getMark_(marks, i, n).second) {
202 for (
const auto chi: i->
type().containerDag().children(n))
208 _fromParent_(iter->first, iter->first->type().get(iter->second).id(), marks);
213 template < GUM_Numeric GUM_SCALAR >
218 for (
const auto& elt: marks) {
221 for (
const auto& elt2: *elt.second)
222 if (elt2.second.first) req_set->
insert(elt2.first);
224 req_map.
insert(elt.first, req_set);
230 for (
const auto& elt: req_map)
231 if (elt.second->size() == 0) to_remove.
insert(elt.first);
233 for (
const auto remo: to_remove) {
234 delete req_map[remo];
239 for (
const auto& elt: req_map) {
246 req_map[elt.first] = 0;
254 template < GUM_Numeric GUM_SCALAR >
258 std::string result(i->
type().name());
260 for (
const auto node: i->
type().containerDag().nodes())
261 if (req_nodes.
exists(node)) result += std::format(
"-{}", node);
266 template < GUM_Numeric GUM_SCALAR >
272 template < GUM_Numeric GUM_SCALAR >
274 const StructuredBayesBall< GUM_SCALAR >& source) : _inf_(0) {
279 template < GUM_Numeric GUM_SCALAR >
281 const StructuredBayesBall< GUM_SCALAR >& source) {
285 template < GUM_Numeric GUM_SCALAR >
291 template < GUM_Numeric GUM_SCALAR >
297 template < GUM_Numeric GUM_SCALAR >
303 template < GUM_Numeric GUM_SCALAR >
309 template < GUM_Numeric GUM_SCALAR >
314 template < GUM_Numeric GUM_SCALAR >
319 template < GUM_Numeric GUM_SCALAR >
324 template < GUM_Numeric GUM_SCALAR >
329 template < GUM_Numeric GUM_SCALAR >
334 template < GUM_Numeric GUM_SCALAR >
339 template < GUM_Numeric GUM_SCALAR >
345 template < GUM_Numeric GUM_SCALAR >
346 std::pair< bool, bool >&
350 return (*(marks[i]))[n];
Exception : fatal (unknown ?) error.
The class for generic Hash Tables.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
void erase(const Key &key)
Removes a given element from the hash table.
optional_ref< Val > tryGet(const Key &key)
Returns a pointer to the value associated with a given key, or nullptr if the key does not exist.
Class for assigning/browsing values to tuples of discrete variables.
bool end() const
Returns true if the Instantiation reached the end.
void inc()
Operator increment.
void setFirst()
Assign the first values to the tuple of the Instantiation.
bool exists(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.
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > * > Chain
Code alias.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
bool exists(NodeId id) const
Returns true if id matches an PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
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>.
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 PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
<agrum/PRM/structuredBayesBall.h>
std::pair< bool, bool > & _getMark_(InstanceMap &marks, const PRMInstance< GUM_SCALAR > *i, NodeId n)
Code alias.
HashTable< const PRMInstance< GUM_SCALAR > *, std::pair< std::string, Set< NodeId > * > > _keyMap_
Associate an PRMInstance<GUM_SCALAR> with a unique key w.r.t. d-separation and the set of requisite n...
const PRMSlotChain< GUM_SCALAR > & _getSC_(const PRMInstance< GUM_SCALAR > *i, NodeId n)
Code alias.
Size occurrence(std::string_view key) const
Returns the number of occurrence of the given key, which is the number of PRMInstance<GUM_SCALAR> sha...
const PRMInference< GUM_SCALAR > * _inf_
The PRM at which model belongs.
void compute(const PRMInstance< GUM_SCALAR > *i, NodeId n)
Compute the set or requisite nodes for each required instance given the current set of observations....
bool exists(const PRMInstance< GUM_SCALAR > *i) const
Returns true if i has requisite nodes.
void _clean_()
Cleans this before a new computation.
HashTable< NodeId, std::pair< bool, bool > > MarkMap
Code alias.
const Set< NodeId > & requisiteNodes(const PRMInstance< GUM_SCALAR > *i) const
Returns the set of requisite nodes w.r.t. d-separation for i.
HashTable< std::string, std::pair< Set< NodeId > *, Size > > _reqMap_
Associate a Key with the set of requisite nodes associated with it. The Size value is the number of i...
void _fillMaps_(InstanceMap &marks)
Fill keyMap and reqMap.
void _fromParent_(const PRMInstance< GUM_SCALAR > *i, NodeId n, InstanceMap &marks)
When the ball is receive on i->get(n) from a parent.
void _fromChild_(const PRMInstance< GUM_SCALAR > *i, NodeId n, InstanceMap &marks)
When the ball is received on i->get(n) from a child.
StructuredBayesBall & operator=(const StructuredBayesBall &source)
Copy operator.
void _compute_(const PRMInstance< GUM_SCALAR > *i, NodeId n)
The real compute method.
float liftRatio() const
Returns the ratio between the total number of instances and the number of instances with the same con...
std::string _buildHashKey_(const PRMInstance< GUM_SCALAR > *i, Set< NodeId > &req_nodes)
Builds the HashKey for the given instance and requisite nodes set.
const std::string & key(const PRMInstance< GUM_SCALAR > *i) const
Returns a unique key w.r.t. d-separation for i.
HashTable< const PRMInstance< GUM_SCALAR > *, MarkMap * > InstanceMap
~StructuredBayesBall()
Destructor.
bool _isHardEvidence_(const PRMInstance< GUM_SCALAR > *i, NodeId n)
Returns true if there is a hard evidence on i->get(n).
StructuredBayesBall(const PRMInference< GUM_SCALAR > &inference)
Default Constructor.
#define GUM_ERROR(type, msg)
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 StructuredBayesBall.