aGrUM 2.3.2
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-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
42#ifndef __INFERENCE_ENGINE__H__
43#define __INFERENCE_ENGINE__H__
44
50#include <map>
51
56
57namespace gum {
58 namespace credal {
59
71 template < typename GUM_SCALAR >
76
78
81
82 protected:
85
90
95
98
105
112
115
120
135
149
154
158 VarMod2BNsMap< GUM_SCALAR > dbnOpt_;
159
170 std::vector< std::pair< NodeId, Idx > > threadRanges_;
171
178
179 // the minimal number of operations that a thread should execute
181
184
187 void repetitiveInit_();
188
195 void initExpectations_();
196
202 void initMarginals_();
203
209
213 void initMarginalSets_();
214
216
219
228 virtual const GUM_SCALAR computeEpsilon_();
229
238 inline void updateExpectations_(const NodeId& id, const std::vector< GUM_SCALAR >& vertex);
239
251 inline void updateCredalSets_(const NodeId& id,
252 const std::vector< GUM_SCALAR >& vertex,
253 const bool& elimRedund = false);
254
256
259
264
265 public:
268
269 // InferenceEngine ();
279 virtual ~InferenceEngine();
280
282
285
286 virtual void makeInference() = 0;
288
291
295 VarMod2BNsMap< GUM_SCALAR >* getVarMod2BNsMap();
296
301 const CredalNet< GUM_SCALAR >& credalNet() const;
302
308
314
320 void setRepetitiveInd(const bool repetitive);
321
325 void storeVertices(const bool value);
331 void storeBNOpt(const bool value);
332
337 bool repetitiveInd() const;
344
347 bool storeVertices() const;
353 bool storeBNOpt() const;
355
357
362 virtual void addEvidence(NodeId id, const Idx val) final;
363
365
370 virtual void addEvidence(const std::string& nodeName, const Idx val) final;
371
373
378 virtual void addEvidence(NodeId id, const std::string& label) final;
379
381
386 virtual void addEvidence(const std::string& nodeName, const std::string& label) final;
387
389
396 virtual void addEvidence(NodeId id, const std::vector< GUM_SCALAR >& vals) final;
397
399
406 virtual void addEvidence(const std::string& nodeName,
407 const std::vector< GUM_SCALAR >& vals) final;
408
410
418 virtual void addEvidence(const Tensor< GUM_SCALAR >& pot) final;
419
420
422 virtual void eraseAllEvidence();
423
426
430 void insertModalsFile(const std::string& path);
431
436 void insertModals(const std::map< std::string, std::vector< GUM_SCALAR > >& modals);
437
442 virtual void insertEvidenceFile(const std::string& path);
443
448 void insertEvidence(const std::map< std::string, std::vector< GUM_SCALAR > >& eviMap);
449
454 void insertEvidence(const NodeProperty< std::vector< GUM_SCALAR > >& evidence);
455
460 void insertQueryFile(const std::string& path);
461
466 void insertQuery(const NodeProperty< std::vector< bool > >& query);
467
469
472
478 Tensor< GUM_SCALAR > marginalMin(const NodeId id) const;
479
485 Tensor< GUM_SCALAR > marginalMax(const NodeId id) const;
486
492 Tensor< GUM_SCALAR > marginalMin(const std::string& varName) const;
493
499 Tensor< GUM_SCALAR > marginalMax(const std::string& varName) const;
500
506 const GUM_SCALAR& expectationMin(const NodeId id) const;
507
513 const GUM_SCALAR& expectationMax(const NodeId id) const;
514
520 const GUM_SCALAR& expectationMin(const std::string& varName) const;
521
527 const GUM_SCALAR& expectationMax(const std::string& varName) const;
528
540 const std::vector< GUM_SCALAR >& dynamicExpMin(const std::string& varName) const;
541
553 const std::vector< GUM_SCALAR >& dynamicExpMax(const std::string& varName) const;
554
560 const std::vector< std::vector< GUM_SCALAR > >& vertices(const NodeId id) const;
561
566 void saveMarginals(const std::string& path) const;
567
572 void saveExpectations(const std::string& path) const;
573
578 void saveVertices(const std::string& path) const;
579
585 void dynamicExpectations(); // if someone forgets the protected call at
586 // the end
587 // of its own algorithm, the user can call it
588
592 std::string toString() const;
593
598 const std::string getApproximationSchemeMsg() { return this->messageApproximationScheme(); }
599
601 };
602
603
604#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
605 extern template class InferenceEngine< double >;
606#endif
607 } // namespace credal
608} // namespace gum
609
611
612#endif
This file contains general scheme for iteratively convergent algorithms.
ApproximationScheme(bool verbosity=false)
The class for generic Hash Tables.
Definition hashTable.h:637
std::string messageApproximationScheme() const
Returns the approximation scheme message.
ThreadNumberManager(Size nb_threads=0)
default constructor
Class template representing a Credal Network.
Definition credalNet.h:97
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.
void saveVertices(const std::string &path) const
Saves vertices to file.
margi oldMarginalMax_
Old upper marginals used to compute epsilon.
const std::vector< GUM_SCALAR > & dynamicExpMin(const std::string &varName) const
Get the lower dynamic expectation of a given variable prefix (without the time step included,...
const std::string getApproximationSchemeMsg()
Get approximation scheme state.
margi evidence_
Holds observed variables states.
virtual void insertEvidenceFile(const std::string &path)
Insert evidence from file.
virtual void makeInference()=0
To be redefined by each credal net algorithm.
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.
margi marginalMax_
Upper marginals.
NodeProperty< GUM_SCALAR > expe
void initExpectations_()
Initialize lower and upper expectations before inference, with the lower expectation being initialize...
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.
void displatchMarginalsToThreads_()
computes Vector threadRanges_, that assigns some part of marginalMin_ and marginalMax_ to the threads
NodeProperty< std::vector< NodeId > > cluster
typename gum::HashTable< std::string, std::vector< GUM_SCALAR > > dynExpe
void saveExpectations(const std::string &path) const
Saves expectations to file.
virtual const GUM_SCALAR computeEpsilon_()
Compute approximation scheme epsilon using the old marginals and the new ones.
void saveMarginals(const std::string &path) const
Saves marginals to file.
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(const std::string &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
void insertModalsFile(const std::string &path)
Insert variables modalities from file to compute expectations.
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.
void insertQueryFile(const std::string &path)
Insert query variables states 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.
virtual ~InferenceEngine()
Destructor.
const CredalNet< GUM_SCALAR > & credalNet() const
Get this creadal 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 usefull 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.