aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
inferenceEngine.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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
42#ifndef GUM_INFERENCE_ENGINE_H
43#define GUM_INFERENCE_ENGINE_H
44
50#include <map>
51
56
57#include <string_view>
58
59namespace gum {
60 namespace credal {
61
73 template < GUM_Numeric GUM_SCALAR >
78
80
83
84 protected:
87
92
97
100
107
114
117
122
137
151
156
160 VarMod2BNsMap< GUM_SCALAR > dbnOpt_;
161
172 std::vector< std::pair< NodeId, Idx > > threadRanges_;
173
180
181 // the minimal number of operations that a thread should execute
183
186
189 void repetitiveInit_();
190
197 void initExpectations_();
198
204 void initMarginals_();
205
211
215 void initMarginalSets_();
216
218
221
230 virtual const GUM_SCALAR computeEpsilon_();
231
240 inline void updateExpectations_(const NodeId& id, const std::vector< GUM_SCALAR >& vertex);
241
253 inline void updateCredalSets_(const NodeId& id,
254 const std::vector< GUM_SCALAR >& vertex,
255 const bool& elimRedund = false);
256
258
261
266
267 public:
270
271 // InferenceEngine ();
281 ~InferenceEngine() override;
282
284
287
288 virtual void makeInference() = 0;
290
293
297 VarMod2BNsMap< GUM_SCALAR >* getVarMod2BNsMap();
298
303 const CredalNet< GUM_SCALAR >& credalNet() const;
304
310
316
322 void setRepetitiveInd(const bool repetitive);
323
327 void storeVertices(const bool value);
333 void storeBNOpt(const bool value);
334
339 bool repetitiveInd() const;
346
349 bool storeVertices() const;
355 bool storeBNOpt() const;
357
359
364 virtual void addEvidence(NodeId id, const Idx val) final;
365
367
372 virtual void addEvidence(std::string_view nodeName, const Idx val) final;
373
375
380 virtual void addEvidence(NodeId id, std::string_view label) final;
381
383
388 virtual void addEvidence(std::string_view nodeName, std::string_view label) final;
389
391
398 virtual void addEvidence(NodeId id, const std::vector< GUM_SCALAR >& vals) final;
399
401
408 virtual void addEvidence(std::string_view nodeName,
409 const std::vector< GUM_SCALAR >& vals) final;
410
412
420 virtual void addEvidence(const Tensor< GUM_SCALAR >& pot) final;
421
422
424 virtual void eraseAllEvidence();
425
428
432 void insertModalsFile(std::string_view path);
433
438 void insertModals(const std::map< std::string, std::vector< GUM_SCALAR > >& modals);
439
444 virtual void insertEvidenceFile(std::string_view path);
445
450 void insertEvidence(const std::map< std::string, std::vector< GUM_SCALAR > >& eviMap);
451
456 void insertEvidence(const NodeProperty< std::vector< GUM_SCALAR > >& evidence);
457
462 void insertQueryFile(std::string_view path);
463
468 void insertQuery(const NodeProperty< std::vector< bool > >& query);
469
471
474
480 Tensor< GUM_SCALAR > marginalMin(const NodeId id) const;
481
487 Tensor< GUM_SCALAR > marginalMax(const NodeId id) const;
488
494 Tensor< GUM_SCALAR > marginalMin(std::string_view varName) const;
495
501 Tensor< GUM_SCALAR > marginalMax(std::string_view varName) const;
502
508 const GUM_SCALAR& expectationMin(const NodeId id) const;
509
515 const GUM_SCALAR& expectationMax(const NodeId id) const;
516
522 const GUM_SCALAR& expectationMin(std::string_view varName) const;
523
529 const GUM_SCALAR& expectationMax(std::string_view varName) const;
530
542 const std::vector< GUM_SCALAR >& dynamicExpMin(std::string_view varName) const;
543
555 const std::vector< GUM_SCALAR >& dynamicExpMax(std::string_view varName) const;
556
562 const std::vector< std::vector< GUM_SCALAR > >& vertices(const NodeId id) const;
563
568 void saveMarginals(std::string_view path) const;
569
574 void saveExpectations(std::string_view path) const;
575
580 void saveVertices(std::string_view path) const;
581
587 void dynamicExpectations(); // if someone forgets the protected call at
588 // the end
589 // of its own algorithm, the user can call it
590
594 std::string toString() const;
595
600 const std::string getApproximationSchemeMsg();
601
603 };
604
605
606#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
607 extern template class InferenceEngine< double >;
608#endif
609 } // namespace credal
610} // namespace gum
611
613
614#endif
This file contains general scheme for iteratively convergent algorithms.
ApproximationScheme(bool verbosity=false)
The class for generic Hash Tables.
Definition hashTable.h:640
ThreadNumberManager(Size nb_threads=0)
default constructor
Class template representing a Credal Network.
Definition credalNet.h:97
Abstract class template representing a CredalNet inference engine.
void updateExpectations_(const NodeId &id, const std::vector< GUM_SCALAR > &vertex)
Given a node id and one of it's possible vertex obtained during inference, update this node lower and...
void repetitiveInit_()
Initialize t0_ and t1_ clusters.
void dynamicExpectations()
Compute dynamic expectations.
margi oldMarginalMax_
Old upper marginals used to compute epsilon.
const std::string getApproximationSchemeMsg()
Get approximation scheme state.
margi evidence_
Holds observed variables states.
virtual void makeInference()=0
To be redefined by each credal net algorithm.
const std::vector< GUM_SCALAR > & dynamicExpMin(std::string_view varName) const
Get the lower dynamic expectation of a given variable prefix (without the time step included,...
void dispatchMarginalsToThreads_()
computes Vector threadRanges_, that assigns some part of marginalMin_ and marginalMax_ to the threads
cluster t1_
Clusters of nodes used with dynamic networks.
dynExpe dynamicExpMin_
Lower dynamic expectations.
bool storeBNOpt_
Iterations limit stopping rule used by some algorithms such as CNMonteCarloSampling.
~InferenceEngine() override
Destructor.
void saveExpectations(std::string_view path) const
Saves expectations to file.
margi marginalMax_
Upper marginals.
NodeProperty< GUM_SCALAR > expe
void insertModalsFile(std::string_view path)
Insert variables modalities from file to compute expectations.
void initExpectations_()
Initialize lower and upper expectations before inference, with the lower expectation being initialize...
void saveVertices(std::string_view path) const
Saves vertices to file.
void insertQueryFile(std::string_view path)
Insert query variables states from file.
void updateCredalSets_(const NodeId &id, const std::vector< GUM_SCALAR > &vertex, const bool &elimRedund=false)
Given a node id and one of it's possible vertex, update it's credal set.
bool repetitiveInd_
True if using repetitive independence ( dynamic network only ), False otherwise.
const NodeProperty< std::vector< NodeId > > & getT1Cluster() const
Get the t1_ cluster.
NodeProperty< std::vector< NodeId > > cluster
typename gum::HashTable< std::string, std::vector< GUM_SCALAR > > dynExpe
virtual const GUM_SCALAR computeEpsilon_()
Compute approximation scheme epsilon using the old marginals and the new ones.
const std::vector< std::vector< GUM_SCALAR > > & vertices(const NodeId id) const
Get the vertice of a given node id.
InferenceEngine(const CredalNet< GUM_SCALAR > &credalNet)
Construtor.
void initMarginalSets_()
Initialize credal set vertices with empty sets.
margi oldMarginalMin_
Old lower marginals used to compute epsilon.
bool storeVertices_
True if credal sets vertices are stored, False otherwise.
dynExpe dynamicExpMax_
Upper dynamic expectations.
const std::vector< GUM_SCALAR > & dynamicExpMax(std::string_view varName) const
Get the upper dynamic expectation of a given variable prefix (without the time step included,...
NodeProperty< std::vector< std::vector< GUM_SCALAR > > > credalSet
std::string toString() const
Print all nodes marginals to standart output.
void insertQuery(const NodeProperty< std::vector< bool > > &query)
Insert query variables and states from Property.
bool repetitiveInd() const
Get the current independence status.
NodeProperty< std::vector< bool > > query
void dynamicExpectations_()
Rearrange lower and upper expectations to suit dynamic networks.
bool storeVertices() const
Get the number of iterations without changes used to stop some algorithms.
const CredalNet< GUM_SCALAR > * credalNet_
A pointer to the Credal Net used.
void setRepetitiveInd(const bool repetitive)
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual void eraseAllEvidence()
removes all the evidence entered into the network
NodeProperty< std::vector< GUM_SCALAR > > margi
expe expectationMax_
Upper expectations, if some variables modalities were inserted.
void insertEvidence(const std::map< std::string, std::vector< GUM_SCALAR > > &eviMap)
Insert evidence from map.
void insertModals(const std::map< std::string, std::vector< GUM_SCALAR > > &modals)
Insert variables modalities from map to compute expectations.
query query_
Holds the query nodes states.
virtual void insertEvidenceFile(std::string_view path)
Insert evidence from file.
credalSet marginalSets_
Credal sets vertices, if enabled.
Tensor< GUM_SCALAR > marginalMin(const NodeId id) const
Get the lower marginals of a given node id.
void saveMarginals(std::string_view path) const
Saves marginals to file.
const CredalNet< GUM_SCALAR > & credalNet() const
Get this credal network.
VarMod2BNsMap< GUM_SCALAR > * getVarMod2BNsMap()
Get optimum IBayesNet.
margi marginalMin_
Lower marginals.
cluster t0_
Clusters of nodes used with dynamic networks.
const GUM_SCALAR & expectationMin(const NodeId id) const
Get the lower expectation of a given node id.
dynExpe modal_
Variables modalities used to compute expectations.
expe expectationMin_
Lower expectations, if some variables modalities were inserted.
Tensor< GUM_SCALAR > marginalMax(const NodeId id) const
Get the upper marginals of a given node id.
const GUM_SCALAR & expectationMax(const NodeId id) const
Get the upper expectation of a given node id.
int timeSteps_
The number of time steps of this network (only useful for dynamic networks).
std::vector< std::pair< NodeId, Idx > > threadRanges_
the ranges of elements of marginalMin_ and marginalMax_ processed by each thread
const NodeProperty< std::vector< NodeId > > & getT0Cluster() const
Get the t0_ cluster.
void initMarginals_()
Initialize lower and upper old marginals and marginals before inference, with the lower marginal bein...
VarMod2BNsMap< GUM_SCALAR > dbnOpt_
Object used to efficiently store optimal bayes net during inference, for some algorithms.
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.
Abstract class representing CredalNet inference engines.
namespace for all credal networks entities
Definition agrum.h:61
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
A wrapper that enables to store data in a way that prevents false cacheline sharing.
A class to manage the number of threads to use in an algorithm.
Class used to store optimum IBayesNet during some inference algorithms.