aGrUM  0.16.0
linearApproximationPolicy_tpl.h
Go to the documentation of this file.
1 
31 // Help IDE parsers
33 
34 namespace gum {
35 
36  // Class constructor
37  template < typename GUM_SCALAR >
39  GUM_SCALAR low, GUM_SCALAR high, 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 
45  }
46 
47  // Copy constructor.
48  template < typename GUM_SCALAR >
51  ApproximationPolicy< GUM_SCALAR >(md),
53 
54 
55  // @brief Convert value to his approximation.
56  template < typename GUM_SCALAR >
58  const GUM_SCALAR& value) const {
59  return __decode(GUM_SCALAR(encode(value)));
60  }
61 
62  // @brief Combine using addition with the given gum::ApproximationPolicy.
63  template < typename GUM_SCALAR >
66  try {
68  dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
69 
70  GUM_SCALAR newHighLimit = _lowLimit + lap->lowLimit();
71  GUM_SCALAR newLowLimit = _lowLimit + lap->lowLimit();
72 
73  GUM_SCALAR newVal = _lowLimit + lap->highLimit();
74 
75  if (newHighLimit < newVal) newHighLimit = newVal;
76 
77  if (newLowLimit > newVal) newLowLimit = newVal;
78 
79  newVal = _highLimit + lap->lowLimit();
80 
81  if (newHighLimit < newVal) newHighLimit = newVal;
82 
83  if (newLowLimit > newVal) newLowLimit = newVal;
84 
85  newVal = _highLimit + lap->highLimit();
86 
87  if (newHighLimit < newVal) newHighLimit = newVal;
88 
89  if (newLowLimit > newVal) newLowLimit = newVal;
90  } catch (const std::bad_cast&) {}
91  }
92 
93  template < typename GUM_SCALAR >
96  try {
98  dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
99 
100  GUM_SCALAR newHighLimit = _lowLimit - lap->lowLimit();
101  GUM_SCALAR newLowLimit = _lowLimit - lap->lowLimit();
102 
103  GUM_SCALAR newVal = _lowLimit - lap->highLimit();
104 
105  if (newHighLimit < newVal) newHighLimit = newVal;
106 
107  if (newLowLimit > newVal) newLowLimit = newVal;
108 
109  newVal = _highLimit - lap->lowLimit();
110 
111  if (newHighLimit < newVal) newHighLimit = newVal;
112 
113  if (newLowLimit > newVal) newLowLimit = newVal;
114 
115  newVal = _highLimit - lap->highLimit();
116 
117  if (newHighLimit < newVal) newHighLimit = newVal;
118 
119  if (newLowLimit > newVal) newLowLimit = newVal;
120  } catch (const std::bad_cast&) {}
121  }
122 
123  template < typename GUM_SCALAR >
126  try {
128  dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
129 
130  GUM_SCALAR newHighLimit = _lowLimit * lap->lowLimit();
131  GUM_SCALAR newLowLimit = _lowLimit * lap->lowLimit();
132 
133  GUM_SCALAR newVal = _lowLimit * lap->highLimit();
134 
135  if (newHighLimit < newVal) newHighLimit = newVal;
136 
137  if (newLowLimit > newVal) newLowLimit = newVal;
138 
139  newVal = _highLimit * lap->lowLimit();
140 
141  if (newHighLimit < newVal) newHighLimit = newVal;
142 
143  if (newLowLimit > newVal) newLowLimit = newVal;
144 
145  newVal = _highLimit * lap->highLimit();
146 
147  if (newHighLimit < newVal) newHighLimit = newVal;
148 
149  if (newLowLimit > newVal) newLowLimit = newVal;
150  } catch (const std::bad_cast&) {}
151  }
152 
153  template < typename GUM_SCALAR >
156  try {
158  dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
159 
160  GUM_SCALAR newHighLimit = _lowLimit / lap->lowLimit();
161  GUM_SCALAR newLowLimit = _lowLimit / lap->lowLimit();
162 
163  GUM_SCALAR newVal = _lowLimit / lap->highLimit();
164 
165  if (newHighLimit < newVal) newHighLimit = newVal;
166 
167  if (newLowLimit > newVal) newLowLimit = newVal;
168 
169  newVal = _highLimit / lap->lowLimit();
170 
171  if (newHighLimit < newVal) newHighLimit = newVal;
172 
173  if (newLowLimit > newVal) newLowLimit = newVal;
174 
175  newVal = _highLimit / lap->highLimit();
176 
177  if (newHighLimit < newVal) newHighLimit = newVal;
178 
179  if (newLowLimit > newVal) newLowLimit = newVal;
180  } catch (const std::bad_cast&) {}
181  }
182 
183  template < typename GUM_SCALAR >
186  try {
188  dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
189 
190  GUM_SCALAR newHighLimit =
191  _lowLimit > lap->lowLimit() ? _lowLimit : lap->lowLimit();
192  GUM_SCALAR newLowLimit =
193  _lowLimit > lap->lowLimit() ? _lowLimit : lap->lowLimit();
194 
195  GUM_SCALAR newVal =
196  _lowLimit > lap->highLimit() ? _lowLimit : lap->highLimit();
197 
198  if (newHighLimit < newVal) newHighLimit = newVal;
199 
200  if (newLowLimit > newVal) newLowLimit = newVal;
201 
202  newVal = _highLimit > lap->lowLimit() ? _highLimit : lap->lowLimit();
203 
204  if (newHighLimit < newVal) newHighLimit = newVal;
205 
206  if (newLowLimit > newVal) newLowLimit = newVal;
207 
208  newVal = _highLimit > lap->highLimit() ? _highLimit : lap->highLimit();
209 
210  if (newHighLimit < newVal) newHighLimit = newVal;
211 
212  if (newLowLimit > newVal) newLowLimit = newVal;
213  } catch (const std::bad_cast&) {}
214  }
215 
216  template < typename GUM_SCALAR >
219  try {
221  dynamic_cast< const LinearApproximationPolicy< GUM_SCALAR >* >(ap);
222 
223  GUM_SCALAR newHighLimit =
224  _lowLimit < lap->lowLimit() ? _lowLimit : lap->lowLimit();
225  GUM_SCALAR newLowLimit =
226  _lowLimit < lap->lowLimit() ? _lowLimit : lap->lowLimit();
227 
228  GUM_SCALAR newVal =
229  _lowLimit < lap->highLimit() ? _lowLimit : lap->highLimit();
230 
231  if (newHighLimit < newVal) newHighLimit = newVal;
232 
233  if (newLowLimit > newVal) newLowLimit = newVal;
234 
235  newVal = _highLimit < lap->lowLimit() ? _highLimit : lap->lowLimit();
236 
237  if (newHighLimit < newVal) newHighLimit = newVal;
238 
239  if (newLowLimit > newVal) newLowLimit = newVal;
240 
241  newVal = _highLimit < lap->highLimit() ? _highLimit : lap->highLimit();
242 
243  if (newHighLimit < newVal) newHighLimit = newVal;
244 
245  if (newLowLimit > newVal) newLowLimit = newVal;
246  } catch (const std::bad_cast&) {}
247  }
248 
249  // Convert value to his approximation. This method is slower than @ref
250  // fromExact since it verifies the bounds
251  template < typename GUM_SCALAR >
253  const GUM_SCALAR& value) {
254  if (value > this->_highLimit) {
255  GUM_ERROR(OutOfUpperBound, "Value asked is higher than high limit");
256  }
257 
258  if (value < this->_lowLimit) {
259  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit");
260  }
261 
262  return fromExact(value);
263  }
264 
265  // Convert value to approximation representation
266  template < typename GUM_SCALAR >
268  const GUM_SCALAR& value) const {
269 // we keep the bounds checked in debug mode
270 #ifdef GUM_DEBUG_MODE
271  if (value > this->_highLimit) {
272  GUM_TRACE(value << " not in (" << this->_lowLimit << "-" << this->_highLimit
273  << ")");
274  GUM_ERROR(OutOfUpperBound, "Value asked is higher than High limit");
275  }
276 
277  if (value < this->_lowLimit) {
278  GUM_TRACE(value << " not in (" << this->_lowLimit << "-" << this->_highLimit
279  << ")");
280  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit");
281  }
282 
283 #endif // GUM_DEBUG_MODE
284  return __encode(value);
285  }
286 
287  // Convert approximation representation to value
288  template < typename GUM_SCALAR >
289  INLINE GUM_SCALAR
291  if (representation > _nbInterval) {
293  "Interval Number asked is higher than total number of interval");
294  }
295 
296  return __decode(GUM_SCALAR(representation));
297  }
298 
299  // Sets approximation factor
300  template < typename GUM_SCALAR >
301  INLINE void
303  _epsilon = e;
305  }
306 
307  // set bounds in a whole
308  template < typename GUM_SCALAR >
310  const GUM_SCALAR& newLowLimit, const GUM_SCALAR& newHighLimit) {
311  if (newLowLimit > newHighLimit) {
312  GUM_ERROR(OutOfBounds, "Asked low value is higher than asked high value");
313  }
314 
315  _lowLimit = newLowLimit;
316  _highLimit = newHighLimit;
318  }
319 
320  // Sets lowest possible value
321  template < typename GUM_SCALAR >
323  const GUM_SCALAR& newLowLimit) {
324  if (newLowLimit > this->_highLimit) {
325  GUM_ERROR(OutOfUpperBound, "Value asked is higher than High limit");
326  }
327 
328  _lowLimit = newLowLimit;
329 
331  }
332 
333  // Gets lowest possible value
334  template < typename GUM_SCALAR >
335  INLINE const GUM_SCALAR&
337  return _lowLimit;
338  }
339 
340  // Sets Highest possible value
341  template < typename GUM_SCALAR >
343  const GUM_SCALAR& newHighLimit) {
344  if (newHighLimit < this->_lowLimit) {
345  GUM_ERROR(OutOfLowerBound, "Value asked is lower than low limit");
346  }
347 
348  _highLimit = newHighLimit;
349 
351  }
352 
353  // Gets Highest possible value
354  template < typename GUM_SCALAR >
355  INLINE const GUM_SCALAR&
357  return _highLimit;
358  }
359 
360  // Concretely computes the approximate representation
361  template < typename GUM_SCALAR >
363  const GUM_SCALAR& value) const {
364  if (value <= this->_lowLimit) return 0;
365 
366  if (value >= this->_highLimit) return _nbInterval;
367 
368  return 1 + Idx(((value - this->_lowLimit) / this->_epsilon));
369  }
370 
371  // Concretely computes the approximate value from representation
372  template < typename GUM_SCALAR >
374  const GUM_SCALAR& representation) const {
375  if (representation == 0) return this->_lowLimit;
376 
377  if (representation == _nbInterval) return this->_highLimit;
378 
379  return (GUM_SCALAR)(((representation * this->_epsilon) - (this->_epsilon / 2))
380  + this->_lowLimit);
381  }
382 
383  // get the number of interval
384  template < typename GUM_SCALAR >
386  _nbInterval = 1 + Idx((this->_highLimit - this->_lowLimit) / this->_epsilon);
387  }
388 } // namespace gum
void combineSub(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using substraction with the given gum::ApproximationPolicy.
virtual void setEpsilon(const GUM_SCALAR &e)
Sets approximation factor.
Idx __encode(const GUM_SCALAR &value) const
Concretely computes the approximate representation.
GUM_SCALAR _lowLimit
Lowest value possible.
void combineDiv(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using division with the given gum::ApproximationPolicy.
GUM_SCALAR _epsilon
Approximation factor.
void combineMin(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using min with the given gum::ApproximationPolicy.
virtual void setLimits(const GUM_SCALAR &newLowLimit, const GUM_SCALAR &newHighLimit)
Set bounds in a whole.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const GUM_SCALAR & highLimit() const
Gets Highest possible value.
GUM_SCALAR __decode(const GUM_SCALAR &representation) const
Concretely computes the approximate value from representation.
Idx encode(const GUM_SCALAR &value) const
Encode a given value into its approximation representation.
GUM_SCALAR _highLimit
Highest value possible.
Idx _nbInterval
The number of interval.
virtual void setHighLimit(const GUM_SCALAR &newHighLimit)
Sets Highest possible value.
const GUM_SCALAR & lowLimit() const
Gets lowest possible value.
LinearApproximationPolicy(GUM_SCALAR low=(GUM_SCALAR) 0.0, GUM_SCALAR high=(GUM_SCALAR) 1.0, GUM_SCALAR eps=(GUM_SCALAR) 0.1)
Default constructor.
void combineMult(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using multiplication with the given gum::ApproximationPolicy.
Size Idx
Type for indexes.
Definition: types.h:53
Class implementing linear approximation policy (meaning possible value are split out in interval)...
GUM_SCALAR decode(Idx representation) const
Convert approximation representation to value.
GUM_SCALAR safeFromExact(const GUM_SCALAR &value)
Convert value to his approximation.
void combineMax(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using max with the given gum::ApproximationPolicy.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
Mother class for all approximation policy classes.
virtual void setLowLimit(const GUM_SCALAR &newLowLimit)
Sets lowest possible value.
void combineAdd(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using addition with the given gum::ApproximationPolicy.
GUM_SCALAR fromExact(const GUM_SCALAR &value) const
Convert value to his approximation.
void _computeNbInterval()
Get the number of interval.