aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
utils_prm_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
44#include <agrum/PRM/utils_prm.h>
45
46namespace gum {
47 namespace prm {
48
49 template < GUM_Numeric GUM_SCALAR >
50 Tensor< GUM_SCALAR >*
52 const Tensor< GUM_SCALAR >& source) {
53 const MultiDimImplementation< GUM_SCALAR >* impl = source.content();
54 Tensor< GUM_SCALAR >* p = 0;
55
56 try {
57 if (dynamic_cast< const MultiDimReadOnly< GUM_SCALAR >* >(impl)) {
58 if (dynamic_cast< const MultiDimNoisyORCompound< GUM_SCALAR >* >(impl)) {
59 p = new Tensor< GUM_SCALAR >(new MultiDimNoisyORCompound< GUM_SCALAR >(
60 bij,
61 static_cast< const MultiDimNoisyORCompound< GUM_SCALAR >& >(*impl)));
62 } else if (dynamic_cast< const MultiDimNoisyORNet< GUM_SCALAR >* >(impl)) {
63 p = new Tensor< GUM_SCALAR >(new MultiDimNoisyORNet< GUM_SCALAR >(
64 bij,
65 static_cast< const MultiDimNoisyORNet< GUM_SCALAR >& >(*impl)));
66 } else if (dynamic_cast< const aggregator::MultiDimAggregator< GUM_SCALAR >* >(impl)) {
67 p = new Tensor< GUM_SCALAR >(
68 static_cast< MultiDimImplementation< GUM_SCALAR >* >(impl->newFactory()));
69
70 for (auto var: impl->variablesSequence())
71 p->add(*(bij.second(var)));
72 } else if (dynamic_cast< const MultiDimBucket< GUM_SCALAR >* >(impl)) {
73 // This is necessary just to prevent non initialized arrays
74 const_cast< MultiDimBucket< GUM_SCALAR >* >(
75 static_cast< const MultiDimBucket< GUM_SCALAR >* >(impl))
76 ->compute();
77
78 try {
79 p = new Tensor< GUM_SCALAR >(new MultiDimBijArray< GUM_SCALAR >(
80 bij,
81 static_cast< const MultiDimBucket< GUM_SCALAR >* >(impl)->bucket()));
82 } catch (OperationNotAllowed const&) {
83 // This is an empty bucket, it happens if all variables were
84 // eliminated
85 return new Tensor< GUM_SCALAR >();
86 }
87 } else {
88 GUM_ERROR(FatalError, "encountered an unexpected MultiDim implementation")
89 }
90 } else {
91 if (dynamic_cast< const MultiDimArray< GUM_SCALAR >* >(impl)) {
92 p = new Tensor< GUM_SCALAR >(new MultiDimBijArray< GUM_SCALAR >(
93 bij,
94 static_cast< const MultiDimArray< GUM_SCALAR >& >(*impl)));
95 } else if (dynamic_cast< const MultiDimBijArray< GUM_SCALAR >* >(impl)) {
96 p = new Tensor< GUM_SCALAR >(new MultiDimBijArray< GUM_SCALAR >(
97 bij,
98 static_cast< const MultiDimBijArray< GUM_SCALAR >& >(*impl)));
99 } else if (dynamic_cast< const MultiDimSparse< GUM_SCALAR >* >(impl)) {
100 GUM_ERROR(FatalError, "There is no MultiDimSparse in PRMs, normally...")
101 } else {
102 // Just need to make the copy using the bijection but we only use
103 // multidim array
104 GUM_ERROR(FatalError, "encountered an unexpected MultiDim implementation")
105 }
106 }
107
108 return p;
109 } catch (Exception const&) {
110 if (p) delete p;
111
112 throw;
113 }
114 }
115
116 // the function used to combine two tables
117 template < GUM_Numeric GUM_SCALAR >
118 Tensor< GUM_SCALAR > multTensor(const Tensor< GUM_SCALAR >& t1,
119 const Tensor< GUM_SCALAR >& t2) {
120 return t1 * t2;
121 }
122
123 template < GUM_Numeric GUM_SCALAR >
125 Set< Tensor< GUM_SCALAR >* >& pool,
126 Set< Tensor< GUM_SCALAR >* >& trash) {
127 Tensor< GUM_SCALAR >* pot = nullptr;
128 Tensor< GUM_SCALAR >* tmp = nullptr;
129
130 gum::VariableSet var_set;
131 var_set.insert(var);
133
134 for (const auto p: pool)
135 if (p->contains(*var)) pots.insert(p);
136
137 if (pots.size() == 0) {
138 return;
139 } else if (pots.size() == 1) {
140 tmp = const_cast< Tensor< GUM_SCALAR >* >(*pots.begin());
141 pot = new Tensor< GUM_SCALAR >(tmp->sumOut(var_set));
142 } else {
144 tmp = Comb.execute(pots);
145 pot = new Tensor< GUM_SCALAR >(tmp->sumOut(var_set));
146 delete tmp;
147 }
148
149 for (const auto p: pots) {
150 pool.erase(const_cast< Tensor< GUM_SCALAR >* >(p));
151
152 if (trash.exists(const_cast< Tensor< GUM_SCALAR >* >(p))) {
153 trash.erase(const_cast< Tensor< GUM_SCALAR >* >(p));
154 delete const_cast< Tensor< GUM_SCALAR >* >(p);
155 }
156 }
157
158 pool.insert(pot);
159 trash.insert(pot);
160 }
161
162 template < GUM_Numeric GUM_SCALAR >
163 void eliminateNodes(const std::vector< const DiscreteVariable* >& elim_order,
164 Set< Tensor< GUM_SCALAR >* >& pool,
165 Set< Tensor< GUM_SCALAR >* >& trash) {
166 for (auto var: elim_order) {
167 eliminateNode(var, pool, trash);
168 }
169 }
170
171 } /* namespace prm */
172} // namespace gum
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1640
Base class for discrete random variable.
Base class for all aGrUM's exceptions.
Definition exceptions.h:122
Exception : fatal (unknown ?) error.
Multidimensional matrix stored as an array in memory.
Decorator of a MultiDimArray, using a bijection over the variables.
A multidim implementation for buckets.
A class to combine efficiently several MultiDim tables.
TABLE * execute(const Set< const TABLE * > &set) const final
Creates and returns the result of the combination of the tables within set.
MultiDimContainer< GUM_ELEMENT > * newFactory() const override=0
Creates an empty clone of this MultiDimContainer.
const Sequence< const DiscreteVariable * > & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
Noisy OR representation.
Multidimensional matrix stored as a sparse array in memory.
Exception : operation not allowed.
Representation of a set.
Definition set.h:129
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:409
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:607
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
namespace for all probabilistic relational models entities
Definition agrum.h:68
Tensor< GUM_SCALAR > multTensor(const Tensor< GUM_SCALAR > &t1, const Tensor< GUM_SCALAR > &t2)
void eliminateNode(const DiscreteVariable *var, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Tensor< GUM_SCALAR > * copyTensor(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Tensor< GUM_SCALAR > &source)
Returns a copy of a Tensor after applying a bijection over the variables in source.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet