35 template <
typename GUM_SCALAR >
46 template <
typename GUM_SCALAR >
53 template <
typename GUM_SCALAR >
61 if (pair.second !=
nullptr) {
delete (pair.second); }
69 template <
typename GUM_SCALAR >
74 template <
typename GUM_SCALAR >
81 template <
typename GUM_SCALAR >
88 template <
typename GUM_SCALAR >
95 template <
typename GUM_SCALAR >
102 template <
typename GUM_SCALAR >
109 template <
typename GUM_SCALAR >
119 template <
typename GUM_SCALAR >
124 "No Bayes net has been assigned to " 125 "the inference algorithm.");
131 template <
typename GUM_SCALAR >
142 template <
typename GUM_SCALAR >
152 template <
typename GUM_SCALAR >
160 template <
typename GUM_SCALAR >
163 if (
__bn !=
nullptr) {
164 for (
const auto node :
__bn->dag()) {
172 template <
typename GUM_SCALAR >
184 template <
typename GUM_SCALAR >
187 const Idx val)
const {
191 "No Bayes net has been assigned to the " 192 "inference algorithm");
194 if (!
__bn->dag().exists(
id)) {
198 if (
__bn->variable(
id).domainSize() <= val) {
200 "node " <<
__bn->variable(
id) <<
" has fewer possible values than " 207 pot <<
__bn->variable(
id);
208 pot.endMultipleChanges(0.0);
219 template <
typename GUM_SCALAR >
223 bool notZero =
false;
246 template <
typename GUM_SCALAR >
253 template <
typename GUM_SCALAR >
261 template <
typename GUM_SCALAR >
264 const std::string& label) {
269 template <
typename GUM_SCALAR >
272 const std::string& label) {
273 NodeId id = this->
BN().idFromName(nodeName);
278 template <
typename GUM_SCALAR >
280 NodeId id,
const std::vector< GUM_SCALAR >& vals) {
284 "No Bayes net has been assigned to the " 285 "inference algorithm");
287 if (!
__bn->dag().exists(
id)) {
291 if (
__bn->variable(
id).domainSize() != vals.size()) {
293 "node " <<
__bn->variable(
id)
294 <<
" and its evidence vector have different sizes.");
304 template <
typename GUM_SCALAR >
306 const std::string& nodeName,
const std::vector< GUM_SCALAR >& vals) {
311 template <
typename GUM_SCALAR >
315 if (pot.nbrDim() != 1) {
320 "No Bayes net has been assigned to the " 321 "inference algorithm");
328 <<
" already has an evidence. Please use chgEvidence().");
341 if (is_hard_evidence) {
353 template <
typename GUM_SCALAR >
362 template <
typename GUM_SCALAR >
365 for (
const auto pot : potlist)
371 template <
typename GUM_SCALAR >
374 for (
const auto pot : potset)
380 template <
typename GUM_SCALAR >
387 template <
typename GUM_SCALAR >
394 template <
typename GUM_SCALAR >
401 template <
typename GUM_SCALAR >
408 template <
typename GUM_SCALAR >
410 const std::string& nodeName)
const {
416 template <
typename GUM_SCALAR >
418 const std::string& nodeName)
const {
424 template <
typename GUM_SCALAR >
426 const std::string& nodeName)
const {
431 template <
typename GUM_SCALAR >
438 template <
typename GUM_SCALAR >
446 template <
typename GUM_SCALAR >
449 const std::string& label) {
454 template <
typename GUM_SCALAR >
457 const std::string& label) {
458 NodeId id = this->
BN().idFromName(nodeName);
463 template <
typename GUM_SCALAR >
465 NodeId id,
const std::vector< GUM_SCALAR >& vals) {
469 "No Bayes net has been assigned to the " 470 "inference algorithm");
472 if (!
__bn->dag().exists(
id)) {
476 if (
__bn->variable(
id).domainSize() != vals.size()) {
478 "node " <<
__bn->variable(
id)
479 <<
" and its evidence have different sizes.");
490 template <
typename GUM_SCALAR >
492 const std::string& nodeName,
const std::vector< GUM_SCALAR >& vals) {
498 template <
typename GUM_SCALAR >
507 "No Bayes net has been assigned to the " 508 "inference algorithm");
514 id <<
" has no evidence. Please use addEvidence().");
527 localPot->
set(I, pot[I]);
532 bool hasChangedSoftHard =
false;
534 if (is_hard_evidence) {
536 hasChangedSoftHard =
true;
548 hasChangedSoftHard =
true;
552 if (hasChangedSoftHard) {
565 template <
typename GUM_SCALAR >
586 template <
typename GUM_SCALAR >
594 template <
typename GUM_SCALAR >
600 if (pair.second !=
nullptr) {
delete (pair.second); }
608 if (has_hard_evidence) {
619 template <
typename GUM_SCALAR >
626 template <
typename GUM_SCALAR >
633 template <
typename GUM_SCALAR >
640 template <
typename GUM_SCALAR >
648 template <
typename GUM_SCALAR >
656 template <
typename GUM_SCALAR >
664 template <
typename GUM_SCALAR >
676 template <
typename GUM_SCALAR >
684 template <
typename GUM_SCALAR >
691 template <
typename GUM_SCALAR >
697 "No Bayes net has been assigned to the " 698 "inference algorithm");
710 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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.