aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
multiDimICIModel_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
57
58namespace gum {
59
60 // Default constructor
61 template < typename GUM_SCALAR >
62 MultiDimICIModel< GUM_SCALAR >::MultiDimICIModel(GUM_SCALAR external_weight,
63 GUM_SCALAR default_weight) :
64 MultiDimReadOnly< GUM_SCALAR >(), _external_weight_(external_weight),
65 _default_weight_(default_weight) {
66 GUM_CONSTRUCTOR(MultiDimICIModel);
67 }
68
69 // Default constructor
70 template < typename GUM_SCALAR >
71 MultiDimICIModel< GUM_SCALAR >::MultiDimICIModel(const MultiDimICIModel< GUM_SCALAR >& from) :
72 MultiDimReadOnly< GUM_SCALAR >(from) {
73 GUM_CONS_CPY(MultiDimICIModel);
74 _default_weight_ = from._default_weight_;
75 _external_weight_ = from._external_weight_;
76 _causal_weights_ = from._causal_weights_;
77 }
78
79 // Copy constructor using a bijection to replace variables from source.
80 template < typename GUM_SCALAR >
81 MultiDimICIModel< GUM_SCALAR >::MultiDimICIModel(
82 const Bijection< const DiscreteVariable*, const DiscreteVariable* >& bij,
83 const MultiDimICIModel< GUM_SCALAR >& from) : MultiDimReadOnly< GUM_SCALAR >() {
84 GUM_CONSTRUCTOR(MultiDimICIModel);
85 _default_weight_ = from._default_weight_;
86 _external_weight_ = from._external_weight_;
87
88 for (HashTableConstIteratorSafe< const DiscreteVariable*, GUM_SCALAR > iter
89 = from._causal_weights_.beginSafe();
90 iter != from._causal_weights_.endSafe();
91 ++iter) {
92 if (auto pFirst = bij.tryFirst(iter.key())) {
93 causalWeight(**pFirst, iter.val());
94 } else {
95 causalWeight(*(iter.key()), iter.val());
96 }
97 }
98 }
99
100 // destructor
101 template < typename GUM_SCALAR >
105
106 template < typename GUM_SCALAR >
108 auto p = _causal_weights_.tryGet(&v);
109 return p ? *p : _default_weight_;
110 }
111
112 template < typename GUM_SCALAR >
113 void MultiDimICIModel< GUM_SCALAR >::causalWeight(const DiscreteVariable& v, GUM_SCALAR w) const {
114 if (!this->contains(v)) {
115 GUM_ERROR(InvalidArgument, v.name() << " is not a cause for this CI Model")
116 }
117
118 if (w == (GUM_SCALAR)0) { GUM_ERROR(gum::OutOfBounds, "causal weight in CI Model>0") }
119 checkCausalWeight_(_external_weight_, w);
121 _causal_weights_.set(&v, w);
122 }
123
124 template < typename GUM_SCALAR >
128
129 template < typename GUM_SCALAR >
130 void MultiDimICIModel< GUM_SCALAR >::externalWeight(GUM_SCALAR w) const {
131 // the default weight is checked only if a cause actually uses it (see also add())
132 for (Idx i = 1; i < this->nbrDim(); i++)
134 // without any cause, the external weight is checked alone (a causal weight equal to the leak
135 // is compatible with it)
136 if (this->nbrDim() <= 1) checkCausalWeight_(w, w);
138 }
139
140 template < typename GUM_SCALAR >
142 // every variable but the first one is a cause: without an explicit weight, it uses the default
143 // weight
144 if (this->nbrDim() > 0 && !_causal_weights_.exists(&v))
147 }
148
149 template < typename GUM_SCALAR >
150 void MultiDimICIModel< GUM_SCALAR >::checkCausalWeight_(GUM_SCALAR, GUM_SCALAR) const {}
151
152 template < typename GUM_SCALAR >
154 GUM_SCALAR causal_weight) {
155 if (!(external_weight >= (GUM_SCALAR)0 && external_weight <= (GUM_SCALAR)1)) {
156 GUM_ERROR(OutOfBounds, "The external weight (" << external_weight << ") must be in [0,1]")
157 }
158 if (!(causal_weight >= (GUM_SCALAR)0 && causal_weight <= (GUM_SCALAR)1)) {
159 GUM_ERROR(OutOfBounds, "A causal weight (" << causal_weight << ") must be in [0,1]")
161 }
163 template < typename GUM_SCALAR >
165 std::stringstream s;
166 s << this->variable(0) << "=CIModel([" << externalWeight() << "]";
167
168 for (Idx i = 1; i < this->nbrDim(); i++)
169 s << ", " << this->variable(i) << "[" << causalWeight(this->variable(i)) << "]";
170
171 s << ")";
172 return s.str();
173 }
174
175 template < typename GUM_SCALAR >
177 if (auto p = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(&src)) {
178 if (src.domainSize() != this->domainSize()) {
179 GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit")
180 }
181 for (Idx i = 1; i < this->nbrDim(); i++)
182 checkCausalWeight_(p->_external_weight_, p->causalWeight(this->variable(i)));
183 _external_weight_ = p->_external_weight_;
184 _default_weight_ = p->_default_weight_;
185 for (Idx i = 1; i < this->nbrDim(); i++) {
186 _causal_weights_.set(const_cast< const DiscreteVariable* >(&this->variable(i)),
187 p->causalWeight(this->variable(i)));
188 }
189 } else {
190 MultiDimReadOnly< GUM_SCALAR >::copyFrom(src);
191 }
192 }
194 // returns the name of the implementation
195 template < typename GUM_SCALAR >
196 const std::string& MultiDimICIModel< GUM_SCALAR >::name() const {
197 static const std::string str = "MultiDimICIModel";
198 return str;
199 }
200
201 template < typename GUM_SCALAR >
203 const DiscreteVariable* y) {
206 _causal_weights_.erase(x);
207 }
208
209 template < typename GUM_SCALAR >
214
215 template < typename GUM_SCALAR >
217
218 template < typename GUM_SCALAR >
220
221 template < typename GUM_SCALAR >
223
224 template < typename GUM_SCALAR >
226
227 template < typename GUM_SCALAR >
229
230 template < typename GUM_SCALAR >
232 return i->toString();
233 }
234
235 template < typename GUM_SCALAR >
237 return this->nbrDim();
238 }
239
240} /* namespace gum */
Set of pairs of elements with fast search for both elements.
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
std::string toString() const
Give a string version of instantiation.
Exception: at least one argument passed to a function is not what was expected.
abstract class for Conditional Indepency Models
void setDecNotification(const gum::Instantiation &) override
Copy of a multiDimICIModel.
void add(const DiscreteVariable &v) override
Adds a new variable: the first one is the effect, the next ones are causes.
void changeNotification(const gum::Instantiation &, const gum::DiscreteVariable *const, gum::Idx, gum::Idx) override
Copy of a multiDimICIModel.
static void checkWeightsAreProbabilities_(GUM_ELEMENT external_weight, GUM_ELEMENT causal_weight)
Checks that both weights are probabilities (in [0,1]).
void setChangeNotification(const gum::Instantiation &) override
Copy of a multiDimICIModel.
GUM_ELEMENT _external_weight_
in Henrion (89).
MultiDimICIModel(GUM_ELEMENT external_weight, GUM_ELEMENT default_weight=(GUM_ELEMENT) 1.0)
Default constructor.
GUM_ELEMENT externalWeight() const
Copy of a multiDimICIModel.
Size realSize() const override
virtual void checkCausalWeight_(GUM_SCALAR external_weight, GUM_SCALAR causal_weight) const
HashTable< const DiscreteVariable *, GUM_ELEMENT > _causal_weights_
in Henrion (89) in a hashtable with a default_value.
std::string toString() const override
Copy of a multiDimICIModel.
void setFirstNotification(const gum::Instantiation &) override
Copy of a multiDimICIModel.
GUM_ELEMENT causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
~MultiDimICIModel() override
Destructor.
void setIncNotification(const gum::Instantiation &) override
Copy of a multiDimICIModel.
void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src) const override
Copy of a multiDimICIModel.
const std::string & name() const override
returns the real name of the multiDimArray
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
const DiscreteVariable & variable(Idx i) const override
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
virtual Size domainSize() const =0
Returns the product of the variables domain size.
Abstract base class for all multi dimensionnal read only structure.
Exception : operation not allowed.
Exception : out of bound.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
GUM_SHARED_PUBLIC bool contains(std::string_view s, std::string_view needle)
true if needle in s
Abstract base class for all multi dimensionnal Causal Independency models.
Header of the MultiDimReadOnly class.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46