aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
PRMInference.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
48
49#ifndef GUM_PRM_INFERENCE_H
50#define GUM_PRM_INFERENCE_H
51
52#include <string>
53
54#include <agrum/PRM/PRM.h>
55
56namespace gum {
57 namespace prm {
67 template < typename GUM_SCALAR >
69 public:
71 using Chain
72 = std::pair< const PRMInstance< GUM_SCALAR >*, const PRMAttribute< GUM_SCALAR >* >;
73
76
78 using EMapIterator = typename NodeProperty< const Tensor< GUM_SCALAR >* >::iterator_safe;
79
81 using EMapConstIterator =
82 typename NodeProperty< const Tensor< GUM_SCALAR >* >::const_iterator_safe;
83
84 // ========================================================================
86 // ========================================================================
88
91
93 PRMInference(const PRMInference& source);
94
96 virtual ~PRMInference();
97
99 PRMInference& operator=(const PRMInference& source);
100
102 // ========================================================================
104 // ========================================================================
106
108 virtual std::string name() const = 0;
109
111 // ========================================================================
112 // ========================================================================
114 // ========================================================================
116
128 void posterior(const Chain& chain, Tensor< GUM_SCALAR >& m);
129
141 void joint(const std::vector< Chain >& chains, Tensor< GUM_SCALAR >& j);
142
144 // ========================================================================
146 // ========================================================================
148
152
156
159 const EMap& evidence(const PRMInstance< GUM_SCALAR >& i) const;
160
163 const EMap& evidence(const PRMInstance< GUM_SCALAR >* i) const;
164
166 bool hasEvidence(const PRMInstance< GUM_SCALAR >& i) const;
167
169 bool hasEvidence(const PRMInstance< GUM_SCALAR >* i) const;
170
172 bool hasEvidence(const Chain& chain) const;
173
175 bool hasEvidence() const;
176
183 void addEvidence(const Chain& chain, const Tensor< GUM_SCALAR >& p);
184
190 void removeEvidence(const Chain& chain);
191
193 void clearEvidence();
194
196
197 protected:
198 // ========================================================================
200 // ========================================================================
202
205 virtual void evidenceAdded_(const Chain& chain) = 0;
206
209 virtual void evidenceRemoved_(const Chain& chain) = 0;
210
215 virtual void posterior_(const Chain& chain, Tensor< GUM_SCALAR >& m) = 0;
216
222 virtual void joint_(const std::vector< Chain >& queries, Tensor< GUM_SCALAR >& j) = 0;
223
225 PRM< GUM_SCALAR > const* prm_;
226
229
231
232 private:
233 // ========================================================================
235 // ========================================================================
237
239 using EvidenceIterator =
240 typename HashTable< const PRMInstance< GUM_SCALAR >*, EMap* >::iterator_safe;
243 typename HashTable< const PRMInstance< GUM_SCALAR >*, EMap* >::const_iterator_safe;
244
247
250
252 };
253
254
255#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
256 extern template class PRMInference< double >;
257#endif
258
259
260 } /* namespace prm */
261} /* namespace gum */
262
264
265#endif /* GUM_PRM_INFERENCE_H */
Inline implementation of PRMInference.
Headers of PRM.
The class for generic Hash Tables.
Definition hashTable.h:637
PRMAttribute is a member of a Class in a PRM.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > * > Chain
Code alias.
void addEvidence(const Chain &chain, const Tensor< GUM_SCALAR > &p)
Add an evidence to the given instance's elt.
virtual ~PRMInference()
Destructor.
virtual std::string name() const =0
Returns the name of the current inference algorithm.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
virtual void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)=0
Generic method to compute the posterior of given element.
void posterior(const Chain &chain, Tensor< GUM_SCALAR > &m)
Compute the posterior of the formal attribute pointed by chain and stores it in m.
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.
typename NodeProperty< const Tensor< GUM_SCALAR > * >::iterator_safe EMapIterator
Code alias.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
EMap & _EMap_(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences, if necessary creates an EMap for i.
void joint(const std::vector< Chain > &chains, Tensor< GUM_SCALAR > &j)
Compute the joint probability of the formals attributes pointed by chains and stores it in m.
void removeEvidence(const Chain &chain)
Remove evidence on the given instance's elt.
PRMSystem< GUM_SCALAR > const * sys_
The Model on which inference is done.
PRMInference & operator=(const PRMInference &source)
Copy operator.
NodeProperty< const Tensor< GUM_SCALAR > * > EMap
Code alias.
typename NodeProperty< const Tensor< GUM_SCALAR > * >::const_iterator_safe EMapConstIterator
Code alias.
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.
virtual void evidenceAdded_(const Chain &chain)=0
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.
virtual void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m)=0
Generic method to compute the posterior of given element.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
virtual void evidenceRemoved_(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
typename HashTable< const PRMInstance< GUM_SCALAR > *, EMap * >::const_iterator_safe EvidenceConstIterator
Code alias.
void clearEvidence()
Remove all evidences.
typename HashTable< const PRMInstance< GUM_SCALAR > *, EMap * >::iterator_safe EvidenceIterator
Code alias.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
Definition PRMInstance.h:79
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition PRMSystem.h:70
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46