aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
linearApproximationPolicy_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 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(
38  GUM_SCALAR low,
39  GUM_SCALAR high,
40  GUM_SCALAR eps) :
41  ApproximationPolicy< GUM_SCALAR >(),
42  lowLimit_(low), highLimit_(high), epsilon_(eps) {
43  if (eps <= 0) { GUM_ERROR(OutOfBounds, "Epsilon must be >0"); }
44 
45  computeNbInterval_();
46  }
47 
48  // Copy constructor.
49  template < typename GUM_SCALAR >
54 
55 
56  // @brief Convert value to his approximation.
57  template < typename GUM_SCALAR >
59  const GUM_SCALAR& value) const {
60  return decode__(GUM_SCALAR(encode(value)));
61  }
62 
63  // @brief Combine using addition with the given gum::ApproximationPolicy.
64  template < typename GUM_SCALAR >
66  const ApproximationPolicy< GUM_SCALAR >* ap) {
67  try {
69  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
70 
73 
75 
77 
79 
81 
83 
85 
87 
89 
91  } catch (const std::bad_cast&) {}
92  }
93 
94  template < typename GUM_SCALAR >
96  const ApproximationPolicy< GUM_SCALAR >* ap) {
97  try {
99  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
100 
103 
105 
107 
109 
110  newVal = highLimit_ - lap->lowLimit();
111 
113 
115 
117 
119 
121  } catch (const std::bad_cast&) {}
122  }
123 
124  template < typename GUM_SCALAR >
126  const ApproximationPolicy< GUM_SCALAR >* ap) {
127  try {
129  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
130 
133 
135 
137 
139 
140  newVal = highLimit_ * lap->lowLimit();
141 
143 
145 
147 
149 
151  } catch (const std::bad_cast&) {}
152  }
153 
154  template < typename GUM_SCALAR >
156  const ApproximationPolicy< GUM_SCALAR >* ap) {
157  try {
159  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
160 
163 
165 
167 
169 
170  newVal = highLimit_ / lap->lowLimit();
171 
173 
175 
177 
179 
181  } catch (const std::bad_cast&) {}
182  }
183 
184  template < typename GUM_SCALAR >
186  const ApproximationPolicy< GUM_SCALAR >* ap) {
187  try {
189  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
190 
192  = lowLimit_ > lap->lowLimit() ? lowLimit_ : lap->lowLimit();
194  = lowLimit_ > lap->lowLimit() ? lowLimit_ : lap->lowLimit();
195 
198 
200 
202 
204 
206 
208 
210 
212 
214  } catch (const std::bad_cast&) {}
215  }
216 
217  template < typename GUM_SCALAR >
219  const ApproximationPolicy< GUM_SCALAR >* ap) {
220  try {
222  = dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
223 
225  = lowLimit_ < lap->lowLimit() ? lowLimit_ : lap->lowLimit();
227  = lowLimit_ < lap->lowLimit() ? lowLimit_ : lap->lowLimit();
228 
231 
233 
235 
237 
239 
241 
243 
245 
247  } catch (const std::bad_cast&) {}
248  }
249 
250  // Convert value to his approximation. This method is slower than @ref
251  // fromExact since it verifies the bounds
252  template < typename GUM_SCALAR >
254  const GUM_SCALAR& value) {
255  if (value > this->highLimit_) {
256  GUM_ERROR(OutOfUpperBound, "Value asked is higher than high limit");
257  }
258 
259  if (value < this->lowLimit_) {
260  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit");
261  }
262 
263  return fromExact(value);
264  }
265 
266  // Convert value to approximation representation
267  template < typename GUM_SCALAR >
269  const GUM_SCALAR& value) const {
270 // we keep the bounds checked in debug mode
271 #ifdef GUM_DEBUG_MODE
272  if (value > this->highLimit_) {
273  GUM_TRACE(value << " not in (" << this->lowLimit_ << "-" << this->highLimit_
274  << ")");
275  GUM_ERROR(OutOfUpperBound, "Value asked is higher than High limit");
276  }
277 
278  if (value < this->lowLimit_) {
279  GUM_TRACE(value << " not in (" << this->lowLimit_ << "-" << this->highLimit_
280  << ")");
281  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit");
282  }
283 
284 #endif // GUM_DEBUG_MODE
285  return encode__(value);
286  }
287 
288  // Convert approximation representation to value
289  template < typename GUM_SCALAR >
292  if (representation > nbInterval_) {
294  "Interval Number asked is higher than total number of interval");
295  }
296 
298  }
299 
300  // Sets approximation factor
301  template < typename GUM_SCALAR >
302  INLINE void
304  epsilon_ = e;
306  }
307 
308  // set bounds in a whole
309  template < typename GUM_SCALAR >
311  const GUM_SCALAR& newLowLimit,
312  const GUM_SCALAR& newHighLimit) {
313  if (newLowLimit > newHighLimit) {
314  GUM_ERROR(OutOfBounds, "Asked low value is higher than asked high value");
315  }
316 
320  }
321 
322  // Sets lowest possible value
323  template < typename GUM_SCALAR >
325  const GUM_SCALAR& newLowLimit) {
326  if (newLowLimit > this->highLimit_) {
327  GUM_ERROR(OutOfUpperBound, "Value asked is higher than High limit");
328  }
329 
331 
333  }
334 
335  // Gets lowest possible value
336  template < typename GUM_SCALAR >
337  INLINE const GUM_SCALAR&
339  return lowLimit_;
340  }
341 
342  // Sets Highest possible value
343  template < typename GUM_SCALAR >
345  const GUM_SCALAR& newHighLimit) {
346  if (newHighLimit < this->lowLimit_) {
347  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit");
348  }
349 
351 
353  }
354 
355  // Gets Highest possible value
356  template < typename GUM_SCALAR >
357  INLINE const GUM_SCALAR&
359  return highLimit_;
360  }
361 
362  // Concretely computes the approximate representation
363  template < typename GUM_SCALAR >
365  const GUM_SCALAR& value) const {
366  if (value <= this->lowLimit_) return 0;
367 
368  if (value >= this->highLimit_) return nbInterval_;
369 
370  return 1 + Idx(((value - this->lowLimit_) / this->epsilon_));
371  }
372 
373  // Concretely computes the approximate value from representation
374  template < typename GUM_SCALAR >
376  const GUM_SCALAR& representation) const {
377  if (representation == 0) return this->lowLimit_;
378 
379  if (representation == nbInterval_) return this->highLimit_;
380 
381  return (GUM_SCALAR)(((representation * this->epsilon_) - (this->epsilon_ / 2))
382  + this->lowLimit_);
383  }
384 
385  // get the number of interval
386  template < typename GUM_SCALAR >
388  nbInterval_ = 1 + Idx((this->highLimit_ - this->lowLimit_) / this->epsilon_);
389  }
390 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669