aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
groundedInference_tpl.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#pragma once
42
43
52
53namespace gum {
54 namespace prm {
55
56 template < GUM_Numeric GUM_SCALAR >
58 GUM_DESTRUCTOR(GroundedInference);
59
60 if (_inf_ != nullptr) delete _inf_;
61
62 if (!_obs_.empty())
63 for (const auto pot: _obs_)
64 // We used const ptrs only because of
65 // MarginalTargetedInference::addEvidence()
66 // requires it
67 delete const_cast< Tensor< GUM_SCALAR >* >(pot);
68 }
69
70 template < GUM_Numeric GUM_SCALAR >
72 const typename PRMInference< GUM_SCALAR >::Chain& chain) {
73 Tensor< GUM_SCALAR >* bn_obs = new Tensor< GUM_SCALAR >();
74 // Retrieving the BN's variable
75 auto var_name = std::format("{}.{}", chain.first->name(), chain.second->safeName());
76 bn_obs->add(_inf_->BN().variableFromName(var_name));
77 // Retrievin the PRM<GUM_SCALAR>'s evidence and copying it in bn_obs
78 const Tensor< GUM_SCALAR >* prm_obs = this->evidence(chain.first)[chain.second->id()];
79 Instantiation i(*bn_obs), j(*prm_obs);
80
81 for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc()) {
82 bn_obs->set(i, prm_obs->get(j));
83 }
84
85 _obs_.insert(bn_obs);
86 }
87
88 template < GUM_Numeric GUM_SCALAR >
90 const typename PRMInference< GUM_SCALAR >::Chain& chain) {
91 std::stringstream var_name;
92 var_name << chain.first->name() << "." << chain.second->safeName();
93 const DiscreteVariable& var = _inf_->BN().variableFromName(var_name.str());
94
95 for (auto iter = _obs_.beginSafe(); iter != _obs_.endSafe();
96 ++iter) { // safe iterator needed here
97 if ((**iter).contains(var)) {
98 _inf_->eraseEvidence(var_name.str());
99 const Tensor< GUM_SCALAR >* e = *iter;
100 _obs_.erase(iter);
101 delete e;
102 break;
103 }
104 }
105 }
106
107 template < GUM_Numeric GUM_SCALAR >
109 const PRMSystem< GUM_SCALAR >& system) :
110 PRMInference< GUM_SCALAR >(prm, system), _inf_(0) {
111 GUM_CONSTRUCTOR(GroundedInference);
112 }
113
114 template < GUM_Numeric GUM_SCALAR >
116 const GroundedInference< GUM_SCALAR >& source) :
117 PRMInference< GUM_SCALAR >(source), _inf_(0) {
118 GUM_CONS_CPY(GroundedInference);
119 GUM_ERROR(FatalError, "illegal to copy constructor")
120 }
121
122 template < GUM_Numeric GUM_SCALAR >
125 GUM_ERROR(FatalError, "illegal call to copy operator")
126 }
127
128 template < GUM_Numeric GUM_SCALAR >
130 if (_inf_ != 0) {
131 return *_inf_;
132 } else {
133 GUM_ERROR(NotFound, "the inference engine is not yet defined")
134 }
135 }
136
137 template < GUM_Numeric GUM_SCALAR >
140 if (_inf_ != 0) { delete _inf_; }
141
142 _inf_ = bn_inf;
143 }
144
145 template < GUM_Numeric GUM_SCALAR >
147 const typename PRMInference< GUM_SCALAR >::Chain& chain,
148 Tensor< GUM_SCALAR >& m) {
149 if (_inf_ == 0) { GUM_ERROR(OperationNotAllowed, "no inference engine defined") }
150
151 std::stringstream sBuff;
152
153 if (!_obs_.empty()) {
154 for (auto e: _obs_) {
155 try {
156 _inf_->addEvidence(*e);
157 } catch (InvalidArgument const&) { _inf_->chgEvidence(*e); }
158 }
159 }
160
161 sBuff << chain.first->name() << "." << chain.second->safeName();
162 m = _inf_->posterior(_inf_->BN().idFromName(sBuff.str()));
163 }
164
165 template < GUM_Numeric GUM_SCALAR >
167 const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
168 Tensor< GUM_SCALAR >& j) {
169 GUM_ERROR(FatalError, "not yet implemented")
170 }
171
172 template < GUM_Numeric GUM_SCALAR >
174 return "grounded inference";
175 }
176
177 } /* namespace prm */
178} /* namespace gum */
Base class for discrete random variable.
Exception : fatal (unknown ?) error.
Class for assigning/browsing values to tuples of discrete variables.
void inc()
Operator increment.
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.
<agrum/BN/inference/marginalTargetedInference.h>
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
<agrum/PRM/groundedInference.h>
void setBNInference(MarginalTargetedInference< GUM_SCALAR > *bn_inf)
Defines the bayesnet inference engine used by this class.
void joint_(const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Tensor< GUM_SCALAR > &j) override
Generic method to compute the marginal of given element.
void evidenceAdded_(const typename PRMInference< GUM_SCALAR >::Chain &chain) override
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.
~GroundedInference() override
Destructor.
std::string name() const override
Returns the bayesnet inference engine used by this class.
void evidenceRemoved_(const typename PRMInference< GUM_SCALAR >::Chain &chain) override
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
void posterior_(const typename PRMInference< GUM_SCALAR >::Chain &chain, Tensor< GUM_SCALAR > &m) override
Generic method to compute the marginal of given element.
GroundedInference & operator=(const GroundedInference &source)
Copy operator.
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
List< const Tensor< GUM_SCALAR > * > _obs_
MarginalTargetedInference< GUM_SCALAR > & getBNInference()
Returns the bayesnet inference engine used by this class.
GroundedInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > * > Chain
Code alias.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition PRMSystem.h:72
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Headers of GroundedInference.
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46