56 template < GUM_Numeric GUM_SCALAR >
65 template < GUM_Numeric GUM_SCALAR >
71 template < GUM_Numeric GUM_SCALAR >
79 if (pair.second !=
nullptr) {
delete (pair.second); }
86 template < GUM_Numeric GUM_SCALAR >
92 template < GUM_Numeric GUM_SCALAR >
98 template < GUM_Numeric GUM_SCALAR >
104 template < GUM_Numeric GUM_SCALAR >
110 template < GUM_Numeric GUM_SCALAR >
117 template < GUM_Numeric GUM_SCALAR >
126 template < GUM_Numeric GUM_SCALAR >
130 "No Bayes net has been assigned to "
131 "the inference algorithm.");
136 template < GUM_Numeric GUM_SCALAR >
146 template < GUM_Numeric GUM_SCALAR >
155 template < GUM_Numeric GUM_SCALAR >
162 template < GUM_Numeric GUM_SCALAR >
166 for (
auto node:
_model_->nodes()) {
173 template < GUM_Numeric GUM_SCALAR >
183 template < GUM_Numeric GUM_SCALAR >
189 "No Bayes net has been assigned to the "
190 "inference algorithm");
194 if (
_model_->variable(
id).domainSize() <= val) {
196 "node " <<
_model_->variable(
id) <<
" has fewer possible values than " << val);
203 template < GUM_Numeric GUM_SCALAR >
207 bool notZero =
false;
211 if (pot[I] != GUM_SCALAR(0.0)) {
229 template < GUM_Numeric GUM_SCALAR >
235 template < GUM_Numeric GUM_SCALAR >
242 template < GUM_Numeric GUM_SCALAR >
248 template < GUM_Numeric GUM_SCALAR >
250 std::string_view label) {
251 const NodeId id = this->
model().idFromName(nodeName);
256 template < GUM_Numeric GUM_SCALAR >
258 const std::vector< GUM_SCALAR >& vals) {
262 "No Bayes net has been assigned to the "
263 "inference algorithm");
267 if (
_model_->variable(
id).domainSize() != vals.size()) {
269 "node " <<
_model_->variable(
id)
270 <<
" and its evidence vector have different sizes.");
273 Tensor< GUM_SCALAR > pot;
274 pot.add(
_model_->variable(
id));
280 template < GUM_Numeric GUM_SCALAR >
282 const std::vector< GUM_SCALAR >& vals) {
287 template < GUM_Numeric GUM_SCALAR >
293 "No Bayes net has been assigned to the "
294 "inference algorithm");
300 " node " <<
id <<
" already has an evidence. Please use chgEvidence().");
310 _evidence_.insert(
id,
new Tensor< GUM_SCALAR >(std::forward< Tensor< GUM_SCALAR > >(pot)));
311 if (is_hard_evidence) {
322 template < GUM_Numeric GUM_SCALAR >
324 Tensor< GUM_SCALAR > new_pot(pot);
329 template < GUM_Numeric GUM_SCALAR >
331 const List<
const Tensor< GUM_SCALAR >* >& potlist) {
332 for (
const auto pot: potlist)
337 template < GUM_Numeric GUM_SCALAR >
339 const Set<
const Tensor< GUM_SCALAR >* >& potset) {
340 for (
const auto pot: potset)
345 template < GUM_Numeric GUM_SCALAR >
351 template < GUM_Numeric GUM_SCALAR >
357 template < GUM_Numeric GUM_SCALAR >
363 template < GUM_Numeric GUM_SCALAR >
369 template < GUM_Numeric GUM_SCALAR >
375 template < GUM_Numeric GUM_SCALAR >
381 template < GUM_Numeric GUM_SCALAR >
387 template < GUM_Numeric GUM_SCALAR >
393 template < GUM_Numeric GUM_SCALAR >
400 template < GUM_Numeric GUM_SCALAR >
406 template < GUM_Numeric GUM_SCALAR >
408 std::string_view label) {
414 template < GUM_Numeric GUM_SCALAR >
416 const std::vector< GUM_SCALAR >& vals) {
420 "No Bayes net has been assigned to the "
421 "inference algorithm");
425 if (
_model_->variable(
id).domainSize() != vals.size()) {
427 "node " <<
_model_->variable(
id) <<
" and its evidence have different sizes.");
431 Tensor< GUM_SCALAR > pot;
432 pot.add(
_model_->variable(
id));
438 template < GUM_Numeric GUM_SCALAR >
440 const std::vector< GUM_SCALAR >& vals) {
445 template < GUM_Numeric GUM_SCALAR >
448 if (pot.nbrDim() != 1) {
453 "No Bayes net has been assigned to the "
454 "inference algorithm");
469 const Tensor< GUM_SCALAR >* localPot =
_evidence_[id];
472 localPot->set(I, pot[I]);
477 bool hasChangedSoftHard =
false;
479 if (is_hard_evidence) {
481 hasChangedSoftHard =
true;
493 hasChangedSoftHard =
true;
497 if (hasChangedSoftHard) {
507 template < GUM_Numeric GUM_SCALAR >
527 template < GUM_Numeric GUM_SCALAR >
533 template < GUM_Numeric GUM_SCALAR >
539 if (pair.second !=
nullptr) {
delete (pair.second); }
547 if (has_hard_evidence) {
555 template < GUM_Numeric GUM_SCALAR >
561 template < GUM_Numeric GUM_SCALAR >
567 template < GUM_Numeric GUM_SCALAR >
573 template < GUM_Numeric GUM_SCALAR >
579 template < GUM_Numeric GUM_SCALAR >
586 template < GUM_Numeric GUM_SCALAR >
592 template < GUM_Numeric GUM_SCALAR >
602 template < GUM_Numeric GUM_SCALAR >
609 template < GUM_Numeric GUM_SCALAR >
615 template < GUM_Numeric GUM_SCALAR >
621 "No model been assigned to the "
622 "inference algorithm");
631 template < GUM_Numeric GUM_SCALAR >
642 template < GUM_Numeric GUM_SCALAR >
Exception : fatal (unknown ?) error.
GraphicalModelInference()
default constructor with a null model (useful for virtual inheritance)
virtual void prepareInference() final
prepare the internal inference structures for the next inference
void _computeDomainSizes_()
computes the domain sizes of the random variables
const NodeSet & softEvidenceNodes() const
returns the set of nodes with soft evidence
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
virtual void onAllEvidenceErased_(bool contains_hard_evidence)=0
fired before all the evidence are erased
virtual void onStateChanged_()=0
fired when the stage is changed
const NodeProperty< const Tensor< GUM_SCALAR > * > & evidence() const
returns the set of evidence
virtual StateOfInference state() const noexcept final
returns the state of the inference engine
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 isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
GraphicalModelInference(const GraphicalModel *model)
default constructor
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i....
NodeProperty< const Tensor< GUM_SCALAR > * > _evidence_
the set of evidence entered into the network
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual void onModelChanged_(const GraphicalModel *model)=0
fired after a new Bayes net has been assigned to the engine
void setModel_(const GraphicalModel *model)
bool _isHardEvidence_(const Tensor< GUM_SCALAR > &pot, Idx &val) const
checks whether a tensor corresponds to a hard evidence or not
NodeSet _soft_evidence_nodes_
the set of nodes that received soft evidence
StateOfInference _state_
the current state of the inference (outdated/ready/done)
NodeProperty< Size > _domain_sizes_
the domain sizes of the random variables
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
virtual const NodeProperty< Size > & domainSizes() const final
get the domain sizes of the random variables of the model
virtual ~GraphicalModelInference()
destructor
virtual void eraseEvidence(NodeId id) final
removed the evidence, if any, corresponding to node id
void setModelDuringConstruction_(const GraphicalModel *model)
assigns a model during the inference engine construction
void setOutdatedTensorsState_()
puts the inference into an OutdatedTensors state if it is not already in an OutdatedStructure state
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
virtual void updateOutdatedTensors_()=0
prepares inference when the latter is in OutdatedTensors state
virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence)=0
fired after a new evidence is inserted
virtual bool isInferenceOutdatedTensors() const noexcept final
returns whether the inference object is in a OutdatedTensor state
virtual bool hasSoftEvidence(NodeId id) const final
indicates whether node id has received a soft evidence
virtual void makeInference_()=0
called when the inference has to be performed effectively
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
virtual void addListOfEvidence(const List< const Tensor< GUM_SCALAR > * > &potlist) final
adds a new list of evidence
virtual void updateOutdatedStructure_()=0
prepares inference when the latter is in OutdatedStructure state
virtual Size nbrHardEvidence() const final
returns the number of hard evidence entered into the Bayesian network
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
StateOfInference
current state of the inference
Tensor< GUM_SCALAR > _createHardEvidence_(NodeId id, Idx val) const
create the internal structure for a hard evidence
void setOutdatedStructureState_()
put the inference into an outdated model structure state
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
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
const GraphicalModel * _model_
the Bayes net on which we perform inferences
NodeProperty< Idx > _hard_evidence_
assign to each node with a hard evidence the index of its observed value
virtual void addSetOfEvidence(const Set< const Tensor< GUM_SCALAR > * > &potset) final
adds a new set of evidence
virtual void clear()
clears all the data structures allocated for the last inference
virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
virtual bool isInferenceOutdatedStructure() const noexcept final
returns whether the inference object is in a OutdatedStructure state
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
Virtual base class for probabilistic graphical models.
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.
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.
Generic doubly linked lists.
Exception : a pointer or a reference on a nullptr (0) object.
static Tensor< GUM_SCALAR > deterministicTensor(const DiscreteVariable &var, Idx value)
Exception : a looked-for element could not be found.
#define GUM_ERROR(type, msg)
This file contains abstract class definitions for graphical models inference classes.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities