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