aGrUM 2.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-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
56
57namespace gum {
58
59 // Default constructor
60 template < typename GUM_SCALAR >
61 INLINE MultiDimICIModel< GUM_SCALAR >::MultiDimICIModel(GUM_SCALAR external_weight,
62 GUM_SCALAR default_weight) :
63 MultiDimReadOnly< GUM_SCALAR >(), _external_weight_(external_weight),
64 _default_weight_(default_weight) {
65 GUM_CONSTRUCTOR(MultiDimICIModel);
66 }
67
68 // Default constructor
69 template < typename GUM_SCALAR >
70 INLINE
78
79 // Copy constructor using a bijection to replace variables from source.
80 template < typename GUM_SCALAR >
83 const MultiDimICIModel< GUM_SCALAR >& from) : MultiDimReadOnly< GUM_SCALAR >() {
84 GUM_CONSTRUCTOR(MultiDimICIModel);
87
89 = from._causal_weights_.beginSafe();
90 iter != from._causal_weights_.endSafe();
91 ++iter) {
92 try {
93 causalWeight(*(bij.first(iter.key())), iter.val());
94 } catch (NotFound const&) { causalWeight(*(iter.key()), iter.val()); }
95 }
96 }
97
98 // destructor
99 template < typename GUM_SCALAR >
103
104 template < typename GUM_SCALAR >
106 return (_causal_weights_.exists(&v)) ? _causal_weights_[&v] : _default_weight_;
107 }
108
109 template < typename GUM_SCALAR >
111 GUM_SCALAR w) const {
112 if (!this->contains(v)) {
113 GUM_ERROR(InvalidArgument, v.name() << " is not a cause for this CI Model")
114 }
115
116 if (w == (GUM_SCALAR)0) { GUM_ERROR(gum::OutOfBounds, "causal weight in CI Model>0") }
117
118 _causal_weights_.set(&v, w);
119 }
120
121 template < typename GUM_SCALAR >
123 return _external_weight_;
124 }
125
126 template < typename GUM_SCALAR >
127 INLINE void MultiDimICIModel< GUM_SCALAR >::externalWeight(GUM_SCALAR w) const {
129 }
130
131 template < typename GUM_SCALAR >
133 std::stringstream s;
134 s << this->variable(0) << "=CIModel([" << externalWeight() << "]";
135
136 for (Idx i = 1; i < this->nbrDim(); i++)
137 s << ", " << this->variable(i) << "[" << causalWeight(this->variable(i)) << "]";
138
139 s << ")";
140 return s.str();
141 }
142
143 template < typename GUM_SCALAR >
145 auto p = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(&src);
146 if (p == nullptr) MultiDimReadOnly< GUM_SCALAR >::copyFrom(src);
147 else {
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 }
158 }
159
160 // returns the name of the implementation
161 template < typename GUM_SCALAR >
162 INLINE const std::string& MultiDimICIModel< GUM_SCALAR >::name() const {
163 static const std::string str = "MultiDimICIModel";
164 return str;
165 }
166
167 template < typename GUM_SCALAR >
174
175} /* namespace gum */
Set of pairs of elements with fast search for both elements.
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
Base class for discrete random variable.
Safe Const Iterators for hashtables.
Definition hashTable.h:1602
Exception: at least one argument passed to a function is not what was expected.
Abstract base class for all multi dimensionnal containers.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
GUM_SCALAR externalWeight() const
Copy of a multiDimICIModel.
virtual const std::string & name() const override
returns the real name of the multiDimArray
GUM_SCALAR _default_weight_
in Henrion (89) in a hashtable with a default_value.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const override
Copy of a multiDimICIModel.
MultiDimICIModel(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
virtual ~MultiDimICIModel()
Destructor.
std::string toString() const override
Copy of a multiDimICIModel.
HashTable< const DiscreteVariable *, GUM_SCALAR > _causal_weights_
in Henrion (89) in a hashtable with a default_value.
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
GUM_SCALAR _external_weight_
in Henrion (89).
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
virtual bool contains(const DiscreteVariable &v) const override
Returns true if var is in *this.
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
virtual Size domainSize() const =0
Returns the product of the variables domain size.
MultiDimReadOnly()
Default constructor.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
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