56 template < GUM_Numeric GUM_SCALAR >
59 for (
const auto& elt2: *elt.second)
68 template < GUM_Numeric GUM_SCALAR >
76 for (
const auto& elt2: *elt.second) {
77 Tensor< GUM_SCALAR >* e =
new Tensor< GUM_SCALAR >();
78 e->add(*(elt2.second->variablesSequence().front()));
82 e->set(i, elt2.second->get(i));
89 template < GUM_Numeric GUM_SCALAR >
99 for (
const auto& elt2: *elt.second) {
100 Tensor< GUM_SCALAR >* e =
new Tensor< GUM_SCALAR >();
101 e->add(*(elt2.second->variablesSequence().front()));
105 e->set(i, elt2.second->get(i));
115 template < GUM_Numeric GUM_SCALAR >
127 template < GUM_Numeric GUM_SCALAR >
129 const Tensor< GUM_SCALAR >& p) {
130 if (chain.first->exists(chain.second->id())) {
131 if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
134 Tensor< GUM_SCALAR >* e =
new Tensor< GUM_SCALAR >();
135 e->add(chain.second->type().variable());
143 if (emap.
exists(chain.second->id())) {
144 delete emap[chain.second->id()];
145 emap[chain.second->id()] = e;
147 emap.
insert(chain.second->id(), e);
153 "the given PRMAttribute does not belong to this "
154 "Instance<GUM_SCALAR>.");
158 template < GUM_Numeric GUM_SCALAR >
165 template < GUM_Numeric GUM_SCALAR >
171 template < GUM_Numeric GUM_SCALAR >
178 template < GUM_Numeric GUM_SCALAR >
192 template < GUM_Numeric GUM_SCALAR >
199 template < GUM_Numeric GUM_SCALAR >
204 template < GUM_Numeric GUM_SCALAR >
209 template < GUM_Numeric GUM_SCALAR >
211 return (
hasEvidence(chain.first)) ?
evidence(chain.first).exists(chain.second->id()) :
false;
214 template < GUM_Numeric GUM_SCALAR >
219 template < GUM_Numeric GUM_SCALAR >
222 if (
_EMap_(chain.first).exists(chain.second->id())) {
224 delete _EMap_(chain.first)[chain.second->id()];
225 _EMap_(chain.first).erase(chain.second->id());
230 template < GUM_Numeric GUM_SCALAR >
233 Tensor< GUM_SCALAR >& m) {
237 m.add(chain.second->type().variable());
238 const Tensor< GUM_SCALAR >& e = *(
evidence(chain.first)[chain.second->id()]);
244 if (chain.second != &(chain.first->get(chain.second->safeName()))) {
246 = std::make_pair(chain.first, &(chain.first->get(chain.second->safeName())));
247 m.add(good_chain.second->type().variable());
250 m.add(chain.second->type().variable());
251 posterior_(chain, m);
256 template < GUM_Numeric GUM_SCALAR >
259 Tensor< GUM_SCALAR >& j) {
262 for (
auto chain = chains.begin(); chain != chains.end(); ++chain) {
263 j.add(chain->second->type().variable());
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > * > Chain
Code alias.
void addEvidence(const Chain &chain, const Tensor< GUM_SCALAR > &p)
Add an evidence to the given instance's elt.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
virtual void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)=0
Generic method to compute the posterior of given element.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.
EMap & _EMap_(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences, if necessary creates an EMap for i.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
void posterior(const Chain &chain, Tensor< GUM_SCALAR > &m)
Compute the posterior of the formal attribute pointed by chain and stores it in m.
PRMSystem< GUM_SCALAR > const * sys_
The Model on which inference is done.
PRMInference & operator=(const PRMInference &source)
Copy operator.
NodeProperty< const Tensor< GUM_SCALAR > * > EMap
Code alias.
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.
virtual void evidenceAdded_(const Chain &chain)=0
virtual ~PRMInference()
Destructor.
virtual void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m)=0
void joint(const std::vector< Chain > &chains, Tensor< GUM_SCALAR > &j)
Compute the joint probability of the formals attributes pointed by chains and stores it in m.
virtual void evidenceRemoved_(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
void removeEvidence(const Chain &chain)
Remove evidence on the given instance's elt.
void clearEvidence()
Remove all evidences.
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...
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Complete concept for GUM_SCALAR template parameter.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
namespace for all probabilistic relational models entities
gum is the global namespace for all aGrUM entities