aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimICIModel_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 A Interface to all Causal Independence models
25  *
26  * Causal Independence (CI) is a method of defining a discrete distribution
27  * that can dramatically reduce the number of prior probabilities necessary to
28  * define a distribution.
29  *
30  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
31  */
32 #include <agrum/tools/core/bijection.h>
33 #include <agrum/tools/multidim/ICIModels/multiDimICIModel.h>
34 #include <agrum/tools/multidim/implementations/multiDimReadOnly.h>
35 
36 namespace gum {
37 
38  // Default constructor
39  template < typename GUM_SCALAR >
40  INLINE
46  }
47 
48  // Default constructor
49  template < typename GUM_SCALAR >
51  const MultiDimICIModel< GUM_SCALAR >& from) :
57  }
58 
59  // Copy constructor using a bijection to replace variables from source.
60  template < typename GUM_SCALAR >
62  const Bijection< const DiscreteVariable*, const DiscreteVariable* >& bij,
63  const MultiDimICIModel< GUM_SCALAR >& from) :
68 
72  ++iter) {
73  try {
74  causalWeight(*(bij.first(iter.key())), iter.val());
75  } catch (NotFound&) { causalWeight(*(iter.key()), iter.val()); }
76  }
77  }
78 
79  // destructor
80  template < typename GUM_SCALAR >
83  }
84 
85  template < typename GUM_SCALAR >
87  const DiscreteVariable& v) const {
89  }
90 
91  template < typename GUM_SCALAR >
92  INLINE void
94  GUM_SCALAR w) const {
95  if (!this->contains(v)) {
96  GUM_ERROR(InvalidArgument, v.name() << " is not a cause for this CI Model");
97  }
98 
99  if (w == (GUM_SCALAR)0) {
100  GUM_ERROR(gum::OutOfBounds, "causal weight in CI Model>0");
101  }
102 
103  causal_weights__.set(&v, w);
104  }
105 
106  template < typename GUM_SCALAR >
108  return external_weight__;
109  }
110 
111  template < typename GUM_SCALAR >
114  }
115 
116  template < typename GUM_SCALAR >
118  std::stringstream s;
119  s << this->variable(0) << "=CIModel([" << externalWeight() << "],";
120 
121  for (Idx i = 1; i < this->nbrDim(); i++) {
122  s << this->variable(i) << "[" << causalWeight(this->variable(i)) << "]";
123  }
124 
125  s << ")";
126 
127  std::string res;
128  s >> res;
129  return res;
130  }
131  template < typename GUM_SCALAR >
133  const MultiDimContainer< GUM_SCALAR >& src) const {
134  auto p = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(&src);
135  if (p == nullptr)
137  else {
138  if (src.domainSize() != this->domainSize()) {
139  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit");
140  }
143  for (Idx i = 1; i < this->nbrDim(); i++) {
145  const_cast< const DiscreteVariable* >(&this->variable(i)),
146  p->causalWeight(this->variable(i)));
147  }
148  }
149  }
150 
151  // returns the name of the implementation
152  template < typename GUM_SCALAR >
154  static const std::string str = "MultiDimICIModel";
155  return str;
156  }
157 
158  template < typename GUM_SCALAR >
160  const DiscreteVariable* y) {
164  }
165 
166 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669