aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
decisionTensor_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
44
45namespace gum {
46
47 template < GUM_Numeric GUM_SCALAR >
49 GUM_CONSTRUCTOR(DecisionTensor);
50 probPot.fillWith(GUM_SCALAR(1));
51 utilPot.fillWith(GUM_SCALAR(0));
52 }
53
54 template < GUM_Numeric GUM_SCALAR >
58
59 template < GUM_Numeric GUM_SCALAR >
60 DecisionTensor< GUM_SCALAR >::DecisionTensor(const Tensor< GUM_SCALAR >& prob,
61 const Tensor< GUM_SCALAR >& util) :
62 probPot(prob), utilPot(util) {
63 GUM_CONSTRUCTOR(DecisionTensor);
64 }
65
66 template < GUM_Numeric GUM_SCALAR >
71
72 template < GUM_Numeric GUM_SCALAR >
74 probPot(std::forward< Tensor< GUM_SCALAR > >(dp.probPot)),
75 utilPot(std::forward< Tensor< GUM_SCALAR > >(dp.utilPot)) {
76 GUM_CONS_MOV(DecisionTensor);
77 }
78
79 template < GUM_Numeric GUM_SCALAR >
82 p.fillWith(GUM_SCALAR(1));
83 probPot = p;
84 p.fillWith(GUM_SCALAR(0));
85 utilPot = p;
86 }
87
88 template < GUM_Numeric GUM_SCALAR >
91 GUM_OP_CPY(DecisionTensor);
92 if (&src == this) return *this;
93 probPot = src.probPot;
94 utilPot = src.utilPot;
95 return *this;
96 }
97
98 template < GUM_Numeric GUM_SCALAR >
101 GUM_OP_MOV(DecisionTensor);
102 if (&src == this) return *this;
103 probPot = std::forward< Tensor< GUM_SCALAR > >(src.probPot);
104 utilPot = std::forward< Tensor< GUM_SCALAR > >(src.utilPot);
105 return *this;
106 }
107
108 template < GUM_Numeric GUM_SCALAR >
110 // @see Evaluating Influence Diagrams using LIMIDS (2000) - section 3.3
111 return ((p.probPot == this->probPot)
112 && (p.probPot * p.utilPot == this->probPot * this->utilPot));
113 }
114
115 template < GUM_Numeric GUM_SCALAR >
116 const DiscreteVariable* DecisionTensor< GUM_SCALAR >::variable(std::string_view name) const {
117 for (const auto& v: probPot.variablesSequence()) {
118 if (v->name() == name) return v;
119 }
120 for (const auto& v: utilPot.variablesSequence()) {
121 if (v->name() == name) return v;
122 }
123
124 GUM_ERROR(NotFound, "'" << name << "' can not be found in DecisionTensor.")
125 }
126
127 template < GUM_Numeric GUM_SCALAR >
131
132 template < GUM_Numeric GUM_SCALAR >
136
137 template < GUM_Numeric GUM_SCALAR >
142
143 template < GUM_Numeric GUM_SCALAR >
149
150 template < GUM_Numeric GUM_SCALAR >
155
156 template < GUM_Numeric GUM_SCALAR >
158 DecisionTensor< GUM_SCALAR >::operator^(const std::vector< std::string >& ontonames) const {
159 return DecisionTensor< GUM_SCALAR >::marginalization(*this, ontonames);
160 }
161
162 template < GUM_Numeric GUM_SCALAR >
163 Tensor< GUM_SCALAR >
164 DecisionTensor< GUM_SCALAR >::divideEvenZero(const Tensor< GUM_SCALAR >& p1,
165 const Tensor< GUM_SCALAR >& p2) {
166 Tensor< GUM_SCALAR > res(p1);
167 Instantiation I(res);
168 for (I.setFirst(); !I.end(); I.inc()) {
169 if (p2[I] != 0) res.set(I, res[I] / p2[I]);
170 }
171 return res;
172 }
173
174 template < GUM_Numeric GUM_SCALAR >
180
181 template < GUM_Numeric GUM_SCALAR >
184 const gum::VariableSet& onto) {
185 const auto pr = dp.probPot.sumIn(onto);
186 return DecisionTensor(pr, divideEvenZero((dp.probPot * dp.utilPot).sumIn(onto), pr));
187 }
188
189 template < GUM_Numeric GUM_SCALAR >
192 const std::vector< std::string >& ontonames) {
193 gum::VariableSet onto;
194 for (const auto& varname: ontonames) {
195 onto.insert(dp.variable(varname));
196 }
197 return marginalization(dp, onto);
198 }
199
200 template < GUM_Numeric GUM_SCALAR >
201 std::pair< GUM_SCALAR, GUM_SCALAR > DecisionTensor< GUM_SCALAR >::meanVar() {
202 const auto tmp = probPot * utilPot;
203 const GUM_SCALAR s = probPot.sum();
204 const double m = tmp.sum() / s;
205 const double m2 = (tmp * utilPot).sum() / s;
206 double var = m2 - m * m;
207 if (var < 0.0) var = 0.0; // var is a small number<0 due to computation errors
208 return std::pair< GUM_SCALAR, GUM_SCALAR >(m, var);
209 }
210
211 template < GUM_Numeric GUM_SCALAR >
213 return "prob : " + probPot.toString() + " util:" + utilPot.toString();
214 }
215
216 template < GUM_Numeric GUM_SCALAR >
217 std::ostream& operator<<(std::ostream& out, const DecisionTensor< GUM_SCALAR >& array) {
218 out << array.toString();
219 return out;
220 }
221
222} // namespace gum
<agrum/ID/inference/decisionTensor.h>
bool operator==(const DecisionTensor< GUM_SCALAR > &p) const
static DecisionTensor< GUM_SCALAR > marginalization(const DecisionTensor< GUM_SCALAR > &dp, const gum::VariableSet &onto)
void insertProba(const gum::Tensor< GUM_SCALAR > &proba)
std::pair< GUM_SCALAR, GUM_SCALAR > meanVar()
static Tensor< GUM_SCALAR > divideEvenZero(const Tensor< GUM_SCALAR > &p1, const Tensor< GUM_SCALAR > &p2)
virtual std::string toString() const
DecisionTensor< GUM_SCALAR > & operator=(const DecisionTensor< GUM_SCALAR > &src)
void insertUtility(const gum::Tensor< GUM_SCALAR > &util)
DecisionTensor< GUM_SCALAR > operator^(const gum::VariableSet &onto) const
const DiscreteVariable * variable(std::string_view name) const
DecisionTensor< GUM_SCALAR > operator*(const DecisionTensor< GUM_SCALAR > &dp1) const
Tensor< GUM_SCALAR > utilPot
DecisionTensor< GUM_SCALAR > operator*=(const DecisionTensor< GUM_SCALAR > &dp1)
Tensor< GUM_SCALAR > probPot
static DecisionTensor< GUM_SCALAR > combination(const DecisionTensor< GUM_SCALAR > &dp1, const DecisionTensor< GUM_SCALAR > &dp2)
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
bool end() const
Returns true if the Instantiation reached the end.
void inc()
Operator increment.
void setFirst()
Assign the first values to the tuple of the Instantiation.
Exception : the element we looked for cannot be found.
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
const Tensor< GUM_SCALAR > & fillWith(const Tensor< GUM_SCALAR > &src) const
copy a Tensor data using name of variables and labels (not necessarily the same variables in the same...
Definition tensor_tpl.h:304
This file contains abstract class definitions influence diagrams inference classes.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
STL namespace.