aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimNoisyAND_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief class for NoisyAND-net implementation as multiDim
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 
29 #include <agrum/tools/core/exceptions.h>
30 #include <agrum/tools/multidim/ICIModels/multiDimNoisyAND.h>
31 
32 namespace gum {
33 
34  // Default constructor
35  template < typename GUM_SCALAR >
39  if (external_weight == 0) {
40  GUM_ERROR(InvalidArgument, "external weight can not be null for a NoisyAND")
41  }
42 
44  }
45 
46  // Default constructor
47  template < typename GUM_SCALAR >
48  INLINE
52  }
53 
54  // Copy constructor using a bijection to replace variables from source.
55  template < typename GUM_SCALAR >
57  const Bijection< const DiscreteVariable*, const DiscreteVariable* >& bij,
58  const MultiDimNoisyAND< GUM_SCALAR >& from) :
61  }
62 
63  // destructor
64  template < typename GUM_SCALAR >
67  }
68 
69  template < typename GUM_SCALAR >
71  if (this->nbrDim() < 1) {
72  GUM_ERROR(OperationNotAllowed, "Not enough variable for a NoisyAND ")
73  }
74 
75  const DiscreteVariable& C = this->variable((Idx)0);
76 
77  if (i.val(C) > 1) return (GUM_SCALAR)0.0;
78 
79  GUM_SCALAR fact = this->externalWeight();
80 
81  for (Idx j = 1; j < this->nbrDim(); j++) {
82  const DiscreteVariable& v = this->variable(j);
83 
84  if (i.val(v) == 0) {
85  fact *= ((GUM_SCALAR)1.0 - this->causalWeight(v));
86  } else {
87  fact *= this->causalWeight(v);
88  }
89  }
90 
91  return (i.val(C) == 1) ? fact : (GUM_SCALAR)1.0 - fact;
92  }
93 
94  template < typename GUM_SCALAR >
97  s << MultiDimImplementation< GUM_SCALAR >::variable(0) << "=noisyAND(["
98  << this->externalWeight() << "],";
99 
100  for (Idx i = 1; i < MultiDimImplementation< GUM_SCALAR >::nbrDim(); i++) {
103  }
104 
105  s << ")";
106 
107  std::string res;
108  s >> res;
109  return res;
110  }
111 
112  // For friendly displaying the content of the variable.
113 
114  template < typename GUM_SCALAR >
116  return s << ag.toString();
117  }
118 
119  template < typename GUM_SCALAR >
122  }
123 
124  // returns the name of the implementation
125  template < typename GUM_SCALAR >
127  static const std::string str = "MultiDimNoisyAND";
128  return str;
129  }
130 
131 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643