aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimICIModel_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 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 >
45  }
46 
47  // Default constructor
48  template < typename GUM_SCALAR >
49  INLINE
56  }
57 
58  // Copy constructor using a bijection to replace variables from source.
59  template < typename GUM_SCALAR >
61  const Bijection< const DiscreteVariable*, const DiscreteVariable* >& bij,
62  const MultiDimICIModel< GUM_SCALAR >& from) :
67 
71  ++iter) {
72  try {
73  causalWeight(*(bij.first(iter.key())), iter.val());
74  } catch (NotFound&) { causalWeight(*(iter.key()), iter.val()); }
75  }
76  }
77 
78  // destructor
79  template < typename GUM_SCALAR >
82  }
83 
84  template < typename GUM_SCALAR >
87  }
88 
89  template < typename GUM_SCALAR >
91  GUM_SCALAR w) const {
92  if (!this->contains(v)) {
93  GUM_ERROR(InvalidArgument, v.name() << " is not a cause for this CI Model")
94  }
95 
96  if (w == (GUM_SCALAR)0) { GUM_ERROR(gum::OutOfBounds, "causal weight in CI Model>0") }
97 
99  }
100 
101  template < typename GUM_SCALAR >
103  return _external_weight_;
104  }
105 
106  template < typename GUM_SCALAR >
109  }
110 
111  template < typename GUM_SCALAR >
113  std::stringstream s;
114  s << this->variable(0) << "=CIModel([" << externalWeight() << "],";
115 
116  for (Idx i = 1; i < this->nbrDim(); i++) {
117  s << this->variable(i) << "[" << causalWeight(this->variable(i)) << "]";
118  }
119 
120  s << ")";
121 
122  std::string res;
123  s >> res;
124  return res;
125  }
126  template < typename GUM_SCALAR >
128  auto p = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(&src);
129  if (p == nullptr)
131  else {
132  if (src.domainSize() != this->domainSize()) {
133  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit")
134  }
137  for (Idx i = 1; i < this->nbrDim(); i++) {
138  _causal_weights_.set(const_cast< const DiscreteVariable* >(&this->variable(i)),
139  p->causalWeight(this->variable(i)));
140  }
141  }
142  }
143 
144  // returns the name of the implementation
145  template < typename GUM_SCALAR >
147  static const std::string str = "MultiDimICIModel";
148  return str;
149  }
150 
151  template < typename GUM_SCALAR >
153  const DiscreteVariable* y) {
157  }
158 
159 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643