aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
multiDimNoisyORCompound_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
50
#include <
agrum/base/multidim/ICIModels/multiDimNoisyORCompound.h
>
51
52
namespace
gum
{
53
55
56
template
< GUM_Numeric GUM_SCALAR >
57
MultiDimNoisyORCompound< GUM_SCALAR >::MultiDimNoisyORCompound
(GUM_SCALAR external_weight,
58
GUM_SCALAR default_weight) :
59
MultiDimICIModel
< GUM_SCALAR >(external_weight, default_weight) {
60
GUM_CONSTRUCTOR(
MultiDimNoisyORCompound
);
61
}
62
64
65
template
< GUM_Numeric GUM_SCALAR >
66
MultiDimNoisyORCompound< GUM_SCALAR >::MultiDimNoisyORCompound
(
67
const
MultiDimNoisyORCompound< GUM_SCALAR >
& from) :
MultiDimICIModel
< GUM_SCALAR >(from) {
68
GUM_CONS_CPY(
MultiDimNoisyORCompound
);
69
}
70
72
73
template
< GUM_Numeric GUM_SCALAR >
74
MultiDimNoisyORCompound< GUM_SCALAR >::MultiDimNoisyORCompound
(
75
const
Bijection< const DiscreteVariable*, const DiscreteVariable* >
& bij,
76
const
MultiDimNoisyORCompound< GUM_SCALAR >
& from) :
77
MultiDimICIModel
< GUM_SCALAR >(bij, from) {
78
GUM_CONSTRUCTOR(
MultiDimNoisyORCompound
);
79
}
80
82
83
template
< GUM_Numeric GUM_SCALAR >
84
MultiDimNoisyORCompound< GUM_SCALAR >::~MultiDimNoisyORCompound
() {
85
GUM_DESTRUCTOR(
MultiDimNoisyORCompound
);
86
}
87
88
template
< GUM_Numeric GUM_SCALAR >
89
GUM_SCALAR
MultiDimNoisyORCompound< GUM_SCALAR >::get
(
const
Instantiation
& i)
const
{
90
if
(this->
nbrDim
() < 1) {
GUM_ERROR
(
OperationNotAllowed
,
"Not enough variable for a NoisyOr "
) }
91
92
const
DiscreteVariable
& C = this->
variable
((
Idx
)0);
93
94
if
(i.
val
(C) > 1)
return
(GUM_SCALAR)0.0;
95
96
GUM_SCALAR ratio = (GUM_SCALAR)1.0 - this->
externalWeight
();
97
98
GUM_SCALAR fact = (GUM_SCALAR)ratio;
99
100
if
(fact != (GUM_SCALAR)0) {
101
for
(
Idx
j = 1; j < this->
nbrDim
(); j++) {
102
const
DiscreteVariable
& v = this->
variable
(j);
103
104
if
(i.
val
(v) == 1) {
105
GUM_SCALAR pr = (1 - this->
causalWeight
(v)) / ratio;
106
107
if
(pr == (GUM_SCALAR)0.0) {
108
fact = (GUM_SCALAR)0.0;
109
break
;
110
}
else
{
111
fact *= pr;
112
}
113
}
114
}
115
}
116
117
return
(i.
val
(C) != 1) ? fact : (GUM_SCALAR)1.0 - fact;
118
}
119
120
template
< GUM_Numeric GUM_SCALAR >
121
std::string
MultiDimNoisyORCompound< GUM_SCALAR >::toString
()
const
{
122
std::stringstream s;
123
s << MultiDimImplementation< GUM_SCALAR >::variable(0) <<
"=noisyORCompound(["
124
<< this->
externalWeight
() <<
"]"
;
125
126
for
(
Idx
i = 1; i < MultiDimImplementation< GUM_SCALAR >::nbrDim(); i++)
127
s <<
", "
<<
MultiDimImplementation< GUM_SCALAR >::variable
(i) <<
"["
128
<< this->
causalWeight
(
MultiDimImplementation< GUM_SCALAR >::variable
(i)) <<
"]"
;
129
130
s <<
")"
;
131
return
s.str();
132
}
133
134
// For friendly displaying the content of the variable.
135
136
template
< GUM_Numeric GUM_SCALAR >
137
std::ostream&
operator<<
(std::ostream& s,
const
MultiDimNoisyORCompound< GUM_SCALAR >
& ag) {
138
return
s << ag.
toString
();
139
}
140
141
template
< GUM_Numeric GUM_SCALAR >
142
MultiDimContainer< GUM_SCALAR >
*
MultiDimNoisyORCompound< GUM_SCALAR >::newFactory
()
const
{
143
return
new
MultiDimNoisyORCompound< GUM_SCALAR >
(this->
_external_weight_
,
144
this->
_default_weight_
);
145
}
146
147
// returns the name of the implementation
148
template
< GUM_Numeric GUM_SCALAR >
149
const
std::string&
MultiDimNoisyORCompound< GUM_SCALAR >::name
()
const
{
150
static
const
std::string str =
"MultiDimNoisyORCompound"
;
151
return
str;
152
}
153
154
// ==================================================
155
}
/* namespace gum */
gum::Bijection
Set of pairs of elements with fast search for both elements.
Definition
bijection.h:1640
gum::DiscreteVariable
Base class for discrete random variable.
Definition
discreteVariable.h:80
gum::Instantiation
Class for assigning/browsing values to tuples of discrete variables.
Definition
instantiation.h:102
gum::Instantiation::val
Idx val(Idx i) const
Returns the current value of the variable at position i.
Definition
instantiation_inl.h:166
gum::MultiDimContainer< GUM_SCALAR >
gum::MultiDimICIModel< GUM_SCALAR >::_external_weight_
GUM_SCALAR _external_weight_
Definition
multiDimICIModel.h:179
gum::MultiDimICIModel< GUM_SCALAR >::MultiDimICIModel
MultiDimICIModel(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
gum::MultiDimICIModel< GUM_SCALAR >::externalWeight
GUM_SCALAR externalWeight() const
Definition
multiDimICIModel_tpl.h:124
gum::MultiDimICIModel< GUM_SCALAR >::_default_weight_
GUM_SCALAR _default_weight_
Definition
multiDimICIModel.h:187
gum::MultiDimICIModel< GUM_SCALAR >::causalWeight
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Definition
multiDimICIModel_tpl.h:107
gum::MultiDimICIModel< GUM_SCALAR >::variable
const DiscreteVariable & variable(Idx i) const override
Definition
multiDimImplementation_tpl.h:192
gum::MultiDimICIModel< GUM_SCALAR >::nbrDim
Idx nbrDim() const override
Definition
multiDimImplementation_tpl.h:182
gum::MultiDimNoisyORCompound
Noisy OR representation.
Definition
multiDimNoisyORCompound.h:80
gum::MultiDimNoisyORCompound::MultiDimNoisyORCompound
MultiDimNoisyORCompound(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
Definition
multiDimNoisyORCompound_tpl.h:57
gum::MultiDimNoisyORCompound::name
const std::string & name() const override
returns the real name of the multiDimArray
Definition
multiDimNoisyORCompound_tpl.h:149
gum::MultiDimNoisyORCompound::newFactory
MultiDimContainer< GUM_SCALAR > * newFactory() const override
This method creates a clone of this object, withouth its content (including variable),...
Definition
multiDimNoisyORCompound_tpl.h:142
gum::MultiDimNoisyORCompound::~MultiDimNoisyORCompound
~MultiDimNoisyORCompound() override
Destructor.
Definition
multiDimNoisyORCompound_tpl.h:84
gum::MultiDimNoisyORCompound::get
GUM_SCALAR get(const Instantiation &i) const override
returns the real name of the multiDimArray
Definition
multiDimNoisyORCompound_tpl.h:89
gum::MultiDimNoisyORCompound::toString
std::string toString() const override
returns the real name of the multiDimArray
Definition
multiDimNoisyORCompound_tpl.h:121
OperationNotAllowed
Exception : operation not allowed.
GUM_ERROR
#define GUM_ERROR(type, msg)
Definition
exceptions.h:76
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
multiDimNoisyORCompound.h
class for multiDimNoisyORCompound
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
gum::operator<<
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0