aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
graphicalModelInference.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49#ifndef AGRUM_GRAPHICALMODELINFERENCE_H
50#define AGRUM_GRAPHICALMODELINFERENCE_H
51
52
53#include <agrum/agrum.h>
54
57
58namespace gum {
59
103
104 template < typename GUM_SCALAR >
106 public:
128
129
130 // ############################################################################
132 // ############################################################################
134
136
139
141
149
151 virtual ~GraphicalModelInference();
152
154
155
156 // ############################################################################
158 // ############################################################################
160
162
164 virtual const GraphicalModel& model() const final;
165
167 virtual const NodeProperty< Size >& domainSizes() const final;
168
170 virtual bool isInferenceReady() const noexcept final;
172 virtual bool isInferenceOutdatedStructure() const noexcept final;
174 virtual bool isInferenceOutdatedTensors() const noexcept final;
176
181 virtual bool isInferenceDone() const noexcept final;
182
183
185 virtual void prepareInference() final;
186
188
193 virtual void makeInference() final;
194
196 virtual void clear();
197
199 virtual StateOfInference state() const noexcept final;
201
202
203 // ############################################################################
205 // ############################################################################
207
209
214 virtual void addEvidence(NodeId id, const Idx val) final;
215
217
222 virtual void addEvidence(const std::string& nodeName, const Idx val) final;
223
225
230 virtual void addEvidence(NodeId id, const std::string& label) final;
231
233
238 virtual void addEvidence(const std::string& nodeName, const std::string& label) final;
239
241
248 virtual void addEvidence(NodeId id, const std::vector< GUM_SCALAR >& vals) final;
249
251
258 virtual void addEvidence(const std::string& nodeName,
259 const std::vector< GUM_SCALAR >& vals) final;
260
262
270 virtual void addEvidence(const Tensor< GUM_SCALAR >& pot) final;
271
273
281 virtual void addEvidence(Tensor< GUM_SCALAR >&& pot) final;
282
284
292 virtual void addSetOfEvidence(const Set< const Tensor< GUM_SCALAR >* >& potset) final;
293
295
303 virtual void addListOfEvidence(const List< const Tensor< GUM_SCALAR >* >& potlist) final;
304
306
311 virtual void chgEvidence(NodeId id, const Idx val) final;
312
314
319 virtual void chgEvidence(const std::string& nodeName, const Idx val) final;
320
322
327 virtual void chgEvidence(NodeId id, const std::string& label) final;
328
330
335 virtual void chgEvidence(const std::string& nodeName, const std::string& label) final;
336
338
345 virtual void chgEvidence(NodeId id, const std::vector< GUM_SCALAR >& vals) final;
346
348
355 virtual void chgEvidence(const std::string& nodeName,
356 const std::vector< GUM_SCALAR >& vals) final;
357
359
367 virtual void chgEvidence(const Tensor< GUM_SCALAR >& pot) final;
368
370 virtual void eraseAllEvidence() final;
371
373 virtual void eraseEvidence(NodeId id) final;
374
376 virtual void eraseEvidence(const std::string& nodeName) final;
377
379 virtual bool hasEvidence() const final;
380
382 virtual bool hasEvidence(NodeId id) const final;
383
385 virtual bool hasHardEvidence(NodeId id) const final;
386
388 virtual bool hasSoftEvidence(NodeId id) const final;
390
391 virtual bool hasEvidence(const std::string& nodeName) const final;
392
394 virtual bool hasHardEvidence(const std::string& nodeName) const final;
395
397 virtual bool hasSoftEvidence(const std::string& nodeName) const final;
398
400 virtual Size nbrEvidence() const final;
401
403 virtual Size nbrHardEvidence() const final;
404
406 virtual Size nbrSoftEvidence() const final;
407
409 const NodeProperty< const Tensor< GUM_SCALAR >* >& evidence() const;
410
412 const NodeSet& softEvidenceNodes() const;
413
415 const NodeSet& hardEvidenceNodes() const;
416
418 const NodeProperty< Idx >& hardEvidence() const;
419
421
422
423 protected:
425 virtual void onStateChanged_() = 0;
426
428 virtual void onEvidenceAdded_(const NodeId id, bool isHardEvidence) = 0;
429
431 virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence) = 0;
432
434 virtual void onAllEvidenceErased_(bool contains_hard_evidence) = 0;
435
444 virtual void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard) = 0;
445
447 virtual void onModelChanged_(const GraphicalModel* model) = 0;
448
450
453 virtual void updateOutdatedStructure_() = 0;
454
456
459 virtual void updateOutdatedTensors_() = 0;
460
462
463 virtual void makeInference_() = 0;
464
466
486
497
498
499 private:
502
504 const GraphicalModel* _model_{nullptr};
505
508
511
514
517
520
521
523 Tensor< GUM_SCALAR > _createHardEvidence_(NodeId id, Idx val) const;
524
526 bool _isHardEvidence_(const Tensor< GUM_SCALAR >& pot, Idx& val) const;
527
530
531 protected:
535 virtual void setState_(const StateOfInference state) final;
536
537 void setModel_(const GraphicalModel* model);
538
541
542 bool hasNoModel_() const { return _model_ == nullptr; };
543 };
544} // namespace gum
545
547
548
549#endif // AGRUM_GRAPHICALMODELINFERENCE_H
GraphicalModelInference()
default constructor with a null model (useful for virtual inheritance)
const NodeSet & softEvidenceNodes() const
returns the set of nodes with soft evidence
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual void prepareInference() final
prepare the internal inference structures for the next inference
void _computeDomainSizes_()
computes the domain sizes of the random variables
virtual bool isInferenceOutdatedStructure() const noexcept final
returns whether the inference object is in a OutdatedStructure state
virtual void setState_(const StateOfInference state) final
set the state of the inference engine and call the notification onStateChanged_ when necessary (i....
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
virtual void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard)=0
fired after an evidence is changed, in particular when its status (soft/hard) changes
GraphicalModelInference(const GraphicalModel *model)
default constructor
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual void addListOfEvidence(const List< const Tensor< GUM_SCALAR > * > &potlist) final
adds a new list of evidence
NodeProperty< const Tensor< GUM_SCALAR > * > _evidence_
the set of evidence entered into the network
virtual const NodeProperty< Size > & domainSizes() const final
get the domain sizes of the random variables of the model
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
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual Size nbrSoftEvidence() const final
returns the number of soft evidence entered into the Bayesian network
virtual StateOfInference state() const noexcept final
returns the state of the inference engine
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
StateOfInference _state_
the current state of the inference (outdated/ready/done)
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
NodeProperty< Size > _domain_sizes_
the domain sizes of the random variables
const NodeProperty< const Tensor< GUM_SCALAR > * > & evidence() const
returns the set of evidence
void setModelDuringConstruction_(const GraphicalModel *model)
assigns a model during the inference engine construction
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
virtual void makeInference() final
perform the heavy computations needed to compute the targets' posteriors
virtual Size nbrEvidence() const final
returns the number of evidence entered into the Bayesian network
virtual bool hasEvidence() const final
indicates whether some node(s) have received 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
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
virtual void makeInference_()=0
called when the inference has to be performed effectively
virtual void clear()
clears all the data structures allocated for the last inference
void setOutdatedTensorsState_()
puts the inference into an OutdatedTensors state if it is not already in an OutdatedStructure state
virtual void addSetOfEvidence(const Set< const Tensor< GUM_SCALAR > * > &potset) final
adds a new set of evidence
virtual Size nbrHardEvidence() const final
returns the number of hard evidence entered into the Bayesian network
virtual void updateOutdatedStructure_()=0
prepares inference when the latter is in OutdatedStructure state
virtual void eraseEvidence(NodeId id) final
removed the evidence, if any, corresponding to node id
virtual bool hasSoftEvidence(NodeId id) const final
indicates whether node id has received a soft evidence
StateOfInference
current state of the inference
Tensor< GUM_SCALAR > _createHardEvidence_(NodeId id, Idx val) const
create the internal structure for a hard evidence
virtual bool isInferenceDone() const noexcept final
returns whether the inference object is in a InferenceDone state
virtual const GraphicalModel & model() const final
Returns a constant reference over the IBayesNet referenced by this class.
NodeSet _hard_evidence_nodes_
the set of nodes that received hard evidence
virtual bool isInferenceOutdatedTensors() const noexcept final
returns whether the inference object is in a OutdatedTensor state
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
void setOutdatedStructureState_()
put the inference into an outdated model structure state
virtual void onEvidenceErased_(const NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
Virtual base class for probabilistic graphical models.
Generic doubly linked lists.
Definition list.h:379
Representation of a set.
Definition set.h:131
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Implementation of the non pure virtual methods of class GraphicalModelInference.
Class representing probabilistic DAG model.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
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
Definition agrum.h:46
STL namespace.
Header of the Tensor class.