aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
linearApproximationPolicy_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 Inlined implementation of gum::LienarApproxiationPolicy.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Jean-Christophe Magnan
27  *
28  */
29 
30 // Help IDE parsers
31 #include <agrum/tools/core/approximations/linearApproximationPolicy.h>
32 
33 namespace gum {
34 
35  // Class constructor
36  template < typename GUM_SCALAR >
37  LinearApproximationPolicy< GUM_SCALAR >::LinearApproximationPolicy(GUM_SCALAR low,
38  GUM_SCALAR high,
39  GUM_SCALAR eps) :
40  ApproximationPolicy< GUM_SCALAR >(),
41  lowLimit_(low), highLimit_(high), epsilon_(eps) {
42  if (eps <= 0) { GUM_ERROR(OutOfBounds, "Epsilon must be >0") }
43 
44  computeNbInterval_();
45  }
46 
47  // Copy constructor.
48  template < typename GUM_SCALAR >
53 
54 
55  // @brief Convert value to his approximation.
56  template < typename GUM_SCALAR >
59  return _decode_(GUM_SCALAR(encode(value)));
60  }
61 
62  // @brief Combine using addition with the given gum::ApproximationPolicy.
63  template < typename GUM_SCALAR >
65  const ApproximationPolicy< GUM_SCALAR >* ap) {
66  try {
68  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
69 
72 
74 
76 
78 
80 
82 
84 
86 
88 
90  } catch (const std::bad_cast&) {}
91  }
92 
93  template < typename GUM_SCALAR >
95  const ApproximationPolicy< GUM_SCALAR >* ap) {
96  try {
98  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
99 
102 
104 
106 
108 
109  newVal = highLimit_ - lap->lowLimit();
110 
112 
114 
116 
118 
120  } catch (const std::bad_cast&) {}
121  }
122 
123  template < typename GUM_SCALAR >
125  const ApproximationPolicy< GUM_SCALAR >* ap) {
126  try {
128  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
129 
132 
134 
136 
138 
139  newVal = highLimit_ * lap->lowLimit();
140 
142 
144 
146 
148 
150  } catch (const std::bad_cast&) {}
151  }
152 
153  template < typename GUM_SCALAR >
155  const ApproximationPolicy< GUM_SCALAR >* ap) {
156  try {
158  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
159 
162 
164 
166 
168 
169  newVal = highLimit_ / lap->lowLimit();
170 
172 
174 
176 
178 
180  } catch (const std::bad_cast&) {}
181  }
182 
183  template < typename GUM_SCALAR >
185  const ApproximationPolicy< GUM_SCALAR >* ap) {
186  try {
188  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
189 
192 
194 
196 
198 
200 
202 
204 
206 
208 
210  } catch (const std::bad_cast&) {}
211  }
212 
213  template < typename GUM_SCALAR >
215  const ApproximationPolicy< GUM_SCALAR >* ap) {
216  try {
218  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
219 
222 
224 
226 
228 
230 
232 
234 
236 
238 
240  } catch (const std::bad_cast&) {}
241  }
242 
243  // Convert value to his approximation. This method is slower than @ref
244  // fromExact since it verifies the bounds
245  template < typename GUM_SCALAR >
248  if (value > this->highLimit_) {
249  GUM_ERROR(OutOfUpperBound, "Value asked is higher than high limit")
250  }
251 
252  if (value < this->lowLimit_) {
253  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit")
254  }
255 
256  return fromExact(value);
257  }
258 
259  // Convert value to approximation representation
260  template < typename GUM_SCALAR >
262 // we keep the bounds checked in debug mode
263 #ifdef GUM_DEBUG_MODE
264  if (value > this->highLimit_) {
266  "Value asked is higher than High limit : not in (" << this->lowLimit_ << "-"
267  << this->highLimit_ << ")")
268  }
269 
270  if (value < this->lowLimit_) {
272  "Value asked is lower than low limit : not in (" << this->lowLimit_ << "-"
273  << this->highLimit_ << ")")
274  }
275 
276 #endif // GUM_DEBUG_MODE
277  return _encode_(value);
278  }
279 
280  // Convert approximation representation to value
281  template < typename GUM_SCALAR >
283  if (representation > nbInterval_) {
284  GUM_ERROR(OutOfUpperBound, "Interval Number asked is higher than total number of interval")
285  }
286 
288  }
289 
290  // Sets approximation factor
291  template < typename GUM_SCALAR >
293  epsilon_ = e;
295  }
296 
297  // set bounds in a whole
298  template < typename GUM_SCALAR >
300  const GUM_SCALAR& newHighLimit) {
301  if (newLowLimit > newHighLimit) {
302  GUM_ERROR(OutOfBounds, "Asked low value is higher than asked high value")
303  }
304 
308  }
309 
310  // Sets lowest possible value
311  template < typename GUM_SCALAR >
313  if (newLowLimit > this->highLimit_) {
314  GUM_ERROR(OutOfUpperBound, "Value asked is higher than High limit")
315  }
316 
318 
320  }
321 
322  // Gets lowest possible value
323  template < typename GUM_SCALAR >
325  return lowLimit_;
326  }
327 
328  // Sets Highest possible value
329  template < typename GUM_SCALAR >
330  INLINE void
332  if (newHighLimit < this->lowLimit_) {
333  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit")
334  }
335 
337 
339  }
340 
341  // Gets Highest possible value
342  template < typename GUM_SCALAR >
344  return highLimit_;
345  }
346 
347  // Concretely computes the approximate representation
348  template < typename GUM_SCALAR >
350  if (value <= this->lowLimit_) return 0;
351 
352  if (value >= this->highLimit_) return nbInterval_;
353 
354  return 1 + Idx(((value - this->lowLimit_) / this->epsilon_));
355  }
356 
357  // Concretely computes the approximate value from representation
358  template < typename GUM_SCALAR >
361  if (representation == 0) return this->lowLimit_;
362 
363  if (representation == nbInterval_) return this->highLimit_;
364 
365  return (GUM_SCALAR)(((representation * this->epsilon_) - (this->epsilon_ / 2))
366  + this->lowLimit_);
367  }
368 
369  // get the number of interval
370  template < typename GUM_SCALAR >
372  nbInterval_ = 1 + Idx((this->highLimit_ - this->lowLimit_) / this->epsilon_);
373  }
374 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643