57 template < GUM_Numeric GUM_SCALAR >
75 template < GUM_Numeric GUM_SCALAR >
81 template < GUM_Numeric GUM_SCALAR >
92 template < GUM_Numeric GUM_SCALAR >
97 "No Bayes net has been assigned to the "
98 "inference algorithm");
99 if (!this->
BN().internalDag().exists(node)) {
107 template < GUM_Numeric GUM_SCALAR >
109 return isTarget(this->
BN().idFromName(nodeName));
113 template < GUM_Numeric GUM_SCALAR >
124 template < GUM_Numeric GUM_SCALAR >
129 "No Bayes net has been assigned to the "
130 "inference algorithm");
132 if (!this->
BN().internalDag().exists(target)) {
146 template < GUM_Numeric GUM_SCALAR >
151 "No Bayes net has been assigned to the "
152 "inference algorithm");
156 for (
const auto target: this->
BN().internalDag()) {
166 template < GUM_Numeric GUM_SCALAR >
171 "No Bayes net has been assigned to the "
172 "inference algorithm");
178 template < GUM_Numeric GUM_SCALAR >
183 "No Bayes net has been assigned to the "
184 "inference algorithm");
186 if (!this->
BN().internalDag().exists(target)) {
201 template < GUM_Numeric GUM_SCALAR >
206 "No Bayes net has been assigned to the "
207 "inference algorithm");
213 template < GUM_Numeric GUM_SCALAR >
219 template < GUM_Numeric GUM_SCALAR >
225 template < GUM_Numeric GUM_SCALAR >
231 template < GUM_Numeric GUM_SCALAR >
245 template < GUM_Numeric GUM_SCALAR >
260 template < GUM_Numeric GUM_SCALAR >
261 const Tensor< GUM_SCALAR >&
269 template < GUM_Numeric GUM_SCALAR >
277 template < GUM_Numeric GUM_SCALAR >
279 return H(this->
BN().idFromName(nodeName));
282 template < GUM_Numeric GUM_SCALAR >
285 const auto& vtarget = this->
BN().variable(target);
289 "Target <" << vtarget.name() <<
"> (" << target <<
") can not be in evs (" << evs
292 auto condset = this->
BN().minimalCondSet(target, evs);
294 Tensor< GUM_SCALAR > res;
297 res.add(this->
BN().variable(target));
299 for (
const auto& n: condset) {
300 res.add(this->
BN().variable(n));
307 for (
const auto& n: condset)
311 const auto& pot = this->
posterior(target);
313 res.set(inst, pot[inst]);
321 template < GUM_Numeric GUM_SCALAR >
323 std::string_view target,
324 const std::vector< std::string >& evs) {
325 const auto& bn = this->
BN();
329 template < GUM_Numeric GUM_SCALAR >
334 template < GUM_Numeric GUM_SCALAR >
void _setBayesNetDuringConstruction_(const IBayesNet< GUM_SCALAR > *bn)
assigns a BN during the inference engine construction
BayesNetInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
DAG dag() const
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
const NodeProperty< const Tensor< GUM_SCALAR > * > & evidence() const
returns the set of evidence
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i....
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
virtual void makeInference() final
perform the heavy computations needed to compute the targets' posteriors
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
Virtual base class for probabilistic graphical models.
Class representing the minimal interface for Bayesian network with no numerical data.
Class for assigning/browsing values to tuples of discrete variables.
bool end() const
Returns true if the Instantiation reached the end.
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
Exception: at least one argument passed to a function is not what was expected.
NodeSet _targets_
the set of marginal targets
~MarginalTargetedInference() override
destructor
void onModelChanged_(const GraphicalModel *bn) override
fired after a new Bayes net has been assigned to the engine
virtual const Tensor< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
virtual void eraseTarget(NodeId target) final
removes an existing (marginal) target
virtual void onAllMarginalTargetsErased_()=0
fired before a all marginal targets are removed
void _setAllMarginalTargets_()
sets all the nodes of the Bayes net as targets
bool isTargetedMode_() const
virtual const Tensor< GUM_SCALAR > & posterior_(NodeId id)=0
asks derived classes for the posterior of a given variable
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
virtual void onMarginalTargetErased_(const NodeId id)=0
fired before a marginal target is removed
virtual GUM_SCALAR H(NodeId X) final
Entropy Compute Shanon's entropy of a node given the observation.
virtual void onAllMarginalTargetsAdded_()=0
fired after all the nodes of the BN are added as marginal targets
virtual bool isInTargetMode() const noexcept final
indicates whether the inference is in a target mode
bool _targeted_mode_
whether the actual targets are default
virtual Size nbrTargets() const noexcept final
returns the number of marginal targets
MarginalTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
virtual const NodeSet & targets() const noexcept final
returns the list of marginal targets
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
virtual void addAllTargets() final
adds all nodes as targets
virtual void eraseAllTargets()
Clear all previously defined targets.
virtual void onMarginalTargetAdded_(const NodeId id)=0
fired after a new marginal target is inserted
Tensor< GUM_SCALAR > evidenceImpact(NodeId target, const NodeSet &evs)
Create a gum::Tensor for P(target|evs) (for all instantiation of target and evs).
NodeSet asNodeSet() const
returns a copy of the set of nodes represented by the NodeGraphPart
Exception : a pointer or a reference on a nullptr (0) object.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Exception : a looked-for element could not be found.
#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 ...
bool contains(std::string_view s, std::string_view needle)
true if needle in s
This file contains the abstract inference class definition for computing (incrementally) marginal pos...
gum is the global namespace for all aGrUM entities