aGrUM 2.3.2
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-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#pragma once
41
42
51
52namespace gum {
53 namespace prm {
54
55 template < typename GUM_SCALAR >
57 GUM_DESTRUCTOR(GroundedInference);
58
59 if (_inf_ != nullptr) delete _inf_;
60
61 if (!_obs_.empty())
62 for (const auto pot: _obs_)
63 // We used const ptrs only because of
64 // MarginalTargetedInference::addEvidence()
65 // requires it
66 delete const_cast< Tensor< GUM_SCALAR >* >(pot);
67 }
68
69 template < typename GUM_SCALAR >
71 const typename PRMInference< GUM_SCALAR >::Chain& chain) {
72 Tensor< GUM_SCALAR >* bn_obs = new Tensor< GUM_SCALAR >();
73 // Retrieving the BN's variable
74 std::stringstream var_name;
75 var_name << chain.first->name() << "." << chain.second->safeName();
76 bn_obs->add(_inf_->BN().variableFromName(var_name.str()));
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 < typename 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 < typename GUM_SCALAR >
108 INLINE
110 const PRMSystem< GUM_SCALAR >& system) :
111 PRMInference< GUM_SCALAR >(prm, system), _inf_(0) {
112 GUM_CONSTRUCTOR(GroundedInference);
113 }
114
115 template < typename GUM_SCALAR >
117 const GroundedInference< GUM_SCALAR >& source) :
118 PRMInference< GUM_SCALAR >(source), _inf_(0) {
119 GUM_CONS_CPY(GroundedInference);
120 GUM_ERROR(FatalError, "illegal to copy constructor")
121 }
122
123 template < typename GUM_SCALAR >
126 GUM_ERROR(FatalError, "illegal call to copy operator")
127 }
128
129 template < typename GUM_SCALAR >
132 if (_inf_ != 0) {
133 return *_inf_;
134 } else {
135 GUM_ERROR(NotFound, "the inference engine is not yet defined")
136 }
137 }
138
139 template < typename GUM_SCALAR >
142 if (_inf_ != 0) { delete _inf_; }
143
144 _inf_ = bn_inf;
145 }
146
147 template < typename GUM_SCALAR >
149 const typename PRMInference< GUM_SCALAR >::Chain& chain,
150 Tensor< GUM_SCALAR >& m) {
151 if (_inf_ == 0) { GUM_ERROR(OperationNotAllowed, "no inference engine defined") }
152
153 std::stringstream sBuff;
154
155 if (!_obs_.empty()) {
156 for (auto e: _obs_) {
157 try {
158 _inf_->addEvidence(*e);
159 } catch (InvalidArgument const&) { _inf_->chgEvidence(*e); }
160 }
161 }
162
163 sBuff << chain.first->name() << "." << chain.second->safeName();
164 m = _inf_->posterior(_inf_->BN().idFromName(sBuff.str()));
165 }
166
167 template < typename GUM_SCALAR >
169 const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
170 Tensor< GUM_SCALAR >& j) {
171 GUM_ERROR(FatalError, "not yet implemented")
172 }
173
174 template < typename GUM_SCALAR >
175 INLINE std::string GroundedInference< GUM_SCALAR >::name() const {
176 return "grounded inference";
177 }
178
179 } /* namespace prm */
180} /* 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>
virtual std::string name() const
Returns the bayesnet inference engine used by this class.
virtual void posterior_(const typename PRMInference< GUM_SCALAR >::Chain &chain, Tensor< GUM_SCALAR > &m)
Generic method to compute the marginal of given element.
virtual void evidenceRemoved_(const typename PRMInference< GUM_SCALAR >::Chain &chain)
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
List< const Tensor< GUM_SCALAR > * > _obs_
void setBNInference(MarginalTargetedInference< GUM_SCALAR > *bn_inf)
Defines the bayesnet inference engine used by this class.
virtual void evidenceAdded_(const typename PRMInference< GUM_SCALAR >::Chain &chain)
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.
virtual ~GroundedInference()
Destructor.
GroundedInference & operator=(const GroundedInference &source)
Copy operator.
virtual void joint_(const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Tensor< GUM_SCALAR > &j)
Generic method to compute the marginal of given element.
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.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
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
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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