aGrUM 3.1.1
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
121 }
122
123 template < typename GUM_SCALAR >
126 }
128 template < typename GUM_SCALAR >
132
133 template < typename GUM_SCALAR >
135 std::stringstream s;
136 s << this->variable(0) << "=CIModel([" << externalWeight() << "]";
137
138 for (Idx i = 1; i < this->nbrDim(); i++)
139 s << ", " << this->variable(i) << "[" << causalWeight(this->variable(i)) << "]";
140
141 s << ")";
142 return s.str();
143 }
144
145 template < typename GUM_SCALAR >
147 if (auto p = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(&src)) {
148 if (src.domainSize() != this->domainSize()) {
149 GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit")
150 }
151 _external_weight_ = p->_external_weight_;
152 _default_weight_ = p->_default_weight_;
153 for (Idx i = 1; i < this->nbrDim(); i++) {
154 _causal_weights_.set(const_cast< const DiscreteVariable* >(&this->variable(i)),
155 p->causalWeight(this->variable(i)));
156 }
157 } else {
159 }
160 }
161
162 // returns the name of the implementation
163 template < typename GUM_SCALAR >
164 const std::string& MultiDimICIModel< GUM_SCALAR >::name() const {
165 static const std::string str = "MultiDimICIModel";
166 return str;
167 }
168
169 template < typename GUM_SCALAR >
171 const DiscreteVariable* y) {
174 _causal_weights_.erase(x);
175 }
176
177 template < typename GUM_SCALAR >
182
183 template < typename GUM_SCALAR >
185
186 template < typename GUM_SCALAR >
188
189 template < typename GUM_SCALAR >
191
192 template < typename GUM_SCALAR >
194
195 template < typename GUM_SCALAR >
197
198 template < typename GUM_SCALAR >
200 return i->toString();
201 }
202
203 template < typename GUM_SCALAR >
205 return this->nbrDim();
206 }
207
208} /* 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.
virtual void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src) const
Basic copy of a MultiDimContainer.
abstract class for Conditional Indepency Models
void setDecNotification(const gum::Instantiation &) override
Copy of a multiDimICIModel.
void setLastNotification(const gum::Instantiation &) override
Copy of a multiDimICIModel.
void changeNotification(const gum::Instantiation &, const gum::DiscreteVariable *const, gum::Idx, gum::Idx) override
Copy of a multiDimICIModel.
void setChangeNotification(const gum::Instantiation &) override
Copy of a multiDimICIModel.
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
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
This is called by MultiDimContainer::replace() to proceed with the replacing between x and y.
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
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
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.
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
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