32 template <
typename GUM_SCALAR >
43 template <
typename GUM_SCALAR >
50 template <
typename GUM_SCALAR >
58 if (pair.second !=
nullptr) {
delete (pair.second); }
66 template <
typename GUM_SCALAR >
71 template <
typename GUM_SCALAR >
78 template <
typename GUM_SCALAR >
85 template <
typename GUM_SCALAR >
92 template <
typename GUM_SCALAR >
99 template <
typename GUM_SCALAR >
106 template <
typename GUM_SCALAR >
116 template <
typename GUM_SCALAR >
121 "No Bayes net has been assigned to " 122 "the inference algorithm.");
128 template <
typename GUM_SCALAR >
139 template <
typename GUM_SCALAR >
149 template <
typename GUM_SCALAR >
157 template <
typename GUM_SCALAR >
160 if (
__bn !=
nullptr) {
161 for (
const auto node :
__bn->dag()) {
169 template <
typename GUM_SCALAR >
181 template <
typename GUM_SCALAR >
184 const Idx val)
const {
188 "No Bayes net has been assigned to the " 189 "inference algorithm");
191 if (!
__bn->dag().exists(
id)) {
195 if (
__bn->variable(
id).domainSize() <= val) {
197 "node " <<
__bn->variable(
id) <<
" has fewer possible values than " 204 pot <<
__bn->variable(
id);
205 pot.endMultipleChanges(0.0);
216 template <
typename GUM_SCALAR >
220 bool notZero =
false;
243 template <
typename GUM_SCALAR >
250 template <
typename GUM_SCALAR >
258 template <
typename GUM_SCALAR >
261 const std::string& label) {
266 template <
typename GUM_SCALAR >
269 const std::string& label) {
270 NodeId id = this->
BN().idFromName(nodeName);
275 template <
typename GUM_SCALAR >
277 NodeId id,
const std::vector< GUM_SCALAR >& vals) {
281 "No Bayes net has been assigned to the " 282 "inference algorithm");
284 if (!
__bn->dag().exists(
id)) {
288 if (
__bn->variable(
id).domainSize() != vals.size()) {
290 "node " <<
__bn->variable(
id)
291 <<
" and its evidence vector have different sizes.");
301 template <
typename GUM_SCALAR >
303 const std::string& nodeName,
const std::vector< GUM_SCALAR >& vals) {
308 template <
typename GUM_SCALAR >
312 if (pot.nbrDim() != 1) {
317 "No Bayes net has been assigned to the " 318 "inference algorithm");
325 <<
" already has an evidence. Please use chgEvidence().");
338 if (is_hard_evidence) {
350 template <
typename GUM_SCALAR >
359 template <
typename GUM_SCALAR >
362 for (
const auto pot : potlist)
368 template <
typename GUM_SCALAR >
371 for (
const auto pot : potset)
377 template <
typename GUM_SCALAR >
384 template <
typename GUM_SCALAR >
391 template <
typename GUM_SCALAR >
398 template <
typename GUM_SCALAR >
405 template <
typename GUM_SCALAR >
407 const std::string& nodeName)
const {
413 template <
typename GUM_SCALAR >
415 const std::string& nodeName)
const {
421 template <
typename GUM_SCALAR >
423 const std::string& nodeName)
const {
428 template <
typename GUM_SCALAR >
435 template <
typename GUM_SCALAR >
443 template <
typename GUM_SCALAR >
446 const std::string& label) {
451 template <
typename GUM_SCALAR >
454 const std::string& label) {
455 NodeId id = this->
BN().idFromName(nodeName);
460 template <
typename GUM_SCALAR >
462 NodeId id,
const std::vector< GUM_SCALAR >& vals) {
466 "No Bayes net has been assigned to the " 467 "inference algorithm");
469 if (!
__bn->dag().exists(
id)) {
473 if (
__bn->variable(
id).domainSize() != vals.size()) {
475 "node " <<
__bn->variable(
id)
476 <<
" and its evidence have different sizes.");
487 template <
typename GUM_SCALAR >
489 const std::string& nodeName,
const std::vector< GUM_SCALAR >& vals) {
495 template <
typename GUM_SCALAR >
504 "No Bayes net has been assigned to the " 505 "inference algorithm");
511 id <<
" has no evidence. Please use addEvidence().");
524 localPot->
set(I, pot[I]);
529 bool hasChangedSoftHard =
false;
531 if (is_hard_evidence) {
533 hasChangedSoftHard =
true;
545 hasChangedSoftHard =
true;
549 if (hasChangedSoftHard) {
562 template <
typename GUM_SCALAR >
583 template <
typename GUM_SCALAR >
591 template <
typename GUM_SCALAR >
597 if (pair.second !=
nullptr) {
delete (pair.second); }
605 if (has_hard_evidence) {
616 template <
typename GUM_SCALAR >
623 template <
typename GUM_SCALAR >
630 template <
typename GUM_SCALAR >
637 template <
typename GUM_SCALAR >
645 template <
typename GUM_SCALAR >
653 template <
typename GUM_SCALAR >
661 template <
typename GUM_SCALAR >
673 template <
typename GUM_SCALAR >
681 template <
typename GUM_SCALAR >
688 template <
typename GUM_SCALAR >
694 "No Bayes net has been assigned to the " 695 "inference algorithm");
707 template <
typename GUM_SCALAR >
void __setBayesNetDuringConstruction(const IBayesNet< GUM_SCALAR > *bn)
assigns a BN during the inference engine construction
virtual bool hasSoftEvidence(NodeId id) const final
indicates whether node id has received a soft evidence
virtual Size nbrEvidence() const final
returns the number of evidence entered into the Bayesian network
virtual Size nbrSoftEvidence() const final
returns the number of soft evidence entered into the Bayesian network
aGrUM's Potential is a multi-dimensional array with tensor operators.
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
NodeProperty< Size > __domain_sizes
the domain sizes of the random variables
virtual void _onStateChanged()=0
fired when the stage is changed
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual void addSetOfEvidence(const Set< const Potential< GUM_SCALAR > * > &potset) final
adds a new set of evidence
const NodeProperty< const Potential< GUM_SCALAR > *> & evidence() const
returns the set of evidence
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn)=0
fired after a new Bayes net has been assigned to the engine
virtual void addListOfEvidence(const List< const Potential< GUM_SCALAR > * > &potlist) final
adds a new list of evidence
StateOfInference __state
the current state of the inference (outdated/ready/done)
virtual void _onEvidenceAdded(const NodeId id, bool isHardEvidence)=0
fired after a new evidence is inserted
virtual ~BayesNetInference()
destructor
BayesNetInference()
default constructor with a null BN (useful for virtual inheritance)
virtual void _onAllEvidenceErased(bool contains_hard_evidence)=0
fired before all the evidence are erased
virtual void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard)=0
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
virtual bool isInferenceOutdatedBNPotentials() const noexcept final
returns whether the inference object is in a OutdatedBNPotential state
virtual void _makeInference()=0
called when the inference has to be performed effectively
virtual bool isInferenceOutdatedBNStructure() const noexcept final
returns whether the inference object is in a OutdatedBNStructure state
virtual void prepareInference() final
prepare the internal inference structures for the next inference
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
void erase(const Key &k)
Erases an element from the set.
Generic doubly linked lists.
Class representing the minimal interface for Bayesian Network.
gum is the global namespace for all aGrUM entities
bool __isHardEvidence(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
The class for generic Hash Tables.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Representation of a setA Set is a structure that contains arbitrary elements.
virtual void makeInference() final
perform the heavy computations needed to compute the targets' posteriors
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
<agrum/BN/inference/BayesNetInference.h>
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
const NodeSet & softEvidenceNodes() const
returns the set of nodes with soft evidence
virtual const NodeProperty< Size > & domainSizes() const final
get the domain sizes of the random variables of the BN
void inc()
Operator increment.
virtual void _updateOutdatedBNPotentials()=0
prepares inference when the latter is in OutdatedBNPotentials state
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
virtual bool isDone() const noexcept final
returns whether the inference object is in a done state
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
This file contains abstract class definitions for Bayesian networks inference classes.
virtual void _updateOutdatedBNStructure()=0
prepares inference when the latter is in OutdatedBNStructure state
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
Potential< GUM_SCALAR > __createHardEvidence(NodeId id, Idx val) const
create the internal structure for a hard evidence
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
virtual void setBN(const IBayesNet< GUM_SCALAR > *bn)
assigns a new BN to the inference engine
void _setOutdatedBNStructureState()
put the inference into an outdated BN structure state
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
Class for assigning/browsing values to tuples of discrete variables.
virtual void clear()
clears all the data structures allocated for the last inference
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
void __computeDomainSizes()
computes the domain sizes of the random variables
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
void setFirst()
Assign the first values to the tuple of the Instantiation.
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
Size Idx
Type for indexes.
virtual StateOfInference state() const noexcept final
returns the state of the inference engine
void clear()
Removes all the elements, if any, from the set.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
virtual void _onEvidenceErased(const NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
Size size() const noexcept
Returns the number of elements in the set.
virtual Size nbrHardEvidence() const final
returns the number of hard evidence entered into the Bayesian network
StateOfInference
current state of the inference
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
Size NodeId
Type for node ids.
void insert(const Key &k)
Inserts a new element into the set.
#define GUM_ERROR(type, msg)
void _setOutdatedBNPotentialsState()
puts the inference into an OutdatedBNPotentials state if it is not already in an OutdatedBNStructure ...
virtual void eraseEvidence(NodeId id) final
removed the evidence, if any, corresponding to node id
bool end() const
Returns true if the Instantiation reached the end.