aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
linearApproximationPolicy.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 Classes used to practice approximation on value.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Jean-Christophe Magnan
27  *
28  */
29 #ifndef GUM_LINEAR_APPROXIMATION_POLICY_H
30 #define GUM_LINEAR_APPROXIMATION_POLICY_H
31 
32 #include <agrum/agrum.h>
33 #include <agrum/tools/core/approximations/approximationPolicy.h>
34 #include <typeinfo>
35 
36 namespace gum {
37 
38  /**
39  * @class LinearApproximationPolicy
40  * @brief Class implementing linear approximation policy (meaning possible
41  * value are split out in interval).
42  * @ingroup approximationpolicy_group
43  *
44  * @warning Doxygen does not like spanning command on multiple line, so we
45  * could not configure it with the correct include directive. Use the
46  * following code snippet to include this file.
47  * @code
48  * #include <agrum/tools/core/approximations/linearApproximationPolicy.h>
49  * @endcode
50  *
51  * @tparam GUM_SCALAR The type used for computations.
52  */
53  template < typename GUM_SCALAR >
55  public:
56  // ===========================================================================
57  /// @name Constructors / Destructors
58  // ===========================================================================
59  /// @{
60 
61  /**
62  * @brief Default constructor.
63  *
64  * @param low The lower limit.
65  * @param high The higher limit.
66  * @param eps The epsilon.
67  * @throw OutOfBound if out of bounds (low<high, eps>0)
68  */
69  LinearApproximationPolicy(GUM_SCALAR low = (GUM_SCALAR)0.0,
70  GUM_SCALAR high = (GUM_SCALAR)1.0,
71  GUM_SCALAR eps = (GUM_SCALAR)0.1);
72 
73  /**
74  * @brief Copy constructor.
75  * @param md The gum::LinearApproximationPolicy to copy.
76  */
78 
79  /// @}
80 
81  // ===========================================================================
82  /// @name Accessors/Modifiers
83  // ===========================================================================
84  /// @{
85 
86  /**
87  * @brief Convert value to his approximation.
88  * @param value The converted value.
89  * @return The value approximation representation.
90  */
91  GUM_SCALAR fromExact(const GUM_SCALAR& value) const;
92 
93  /**
94  * @brief Combine using addition with the given gum::ApproximationPolicy.
95  * @param ap The policy to combine with.
96  */
97  void combineAdd(const ApproximationPolicy< GUM_SCALAR >* ap);
98 
99  /**
100  * @brief Combine using substraction with the given
101  * gum::ApproximationPolicy.
102  * @param ap The policy to combine with.
103  */
104  void combineSub(const ApproximationPolicy< GUM_SCALAR >* ap);
105 
106  /**
107  * @brief Combine using multiplication with the given
108  * gum::ApproximationPolicy.
109  * @param ap The policy to combine with.
110  */
111  void combineMult(const ApproximationPolicy< GUM_SCALAR >* ap);
112 
113  /**
114  * @brief Combine using division with the given gum::ApproximationPolicy.
115  * @param ap The policy to combine with.
116  */
117  void combineDiv(const ApproximationPolicy< GUM_SCALAR >* ap);
118 
119  /**
120  * @brief Combine using max with the given gum::ApproximationPolicy.
121  * @param ap The policy to combine with.
122  */
123  void combineMax(const ApproximationPolicy< GUM_SCALAR >* ap);
124 
125  /**
126  * @brief Combine using min with the given gum::ApproximationPolicy.
127  * @param ap The policy to combine with.
128  */
129  void combineMin(const ApproximationPolicy< GUM_SCALAR >* ap);
130 
131  /**
132  * @brief Convert value to his approximation.
133  *
134  * This method is slower than @ref fromExact since it verifies the
135  * bounds.
136  *
137  * @throw OutOfLowerBound Raised if value is out of bounds.
138  * @throw OutOfUpperBound Raised if value is out of bounds.
139  */
140  GUM_SCALAR safeFromExact(const GUM_SCALAR& value);
141 
142  /**
143  * @brief Encode a given value into its approximation representation.
144  * @param value The to encode.
145  * @return Returns the encoded value.
146  * @throw OutOfLowerBound Raised if value is out of bounds.
147  * @throw OutOfUpperBound Raised if value is out of bounds.
148  */
149  Idx encode(const GUM_SCALAR& value) const;
150 
151  /**
152  * @brief Convert approximation representation to value.
153  * @param representation The approximation representation to decode.
154  * @return Returns the value decoded from its approximation
155  * reprensentation.
156  */
157  GUM_SCALAR decode(Idx representation) const;
158 
159  /**
160  * @brief Sets approximation factor.
161  * @param e The new epsilon value.
162  */
163  virtual void setEpsilon(const GUM_SCALAR& e);
164 
165  /**
166  * @brief Set bounds in a whole.
167  * @param newLowLimit New lower bound.
168  * @param newHighLimit New higher bound.
169  * @throw OutOfBounds Raised if new bounds are not legit.
170  */
171  virtual void setLimits(const GUM_SCALAR& newLowLimit, const GUM_SCALAR& newHighLimit);
172 
173  /**
174  * @brief Sets lowest possible value.
175  * @param newLowLimit New lower bound.
176  * @throw OutOfUpperBound Raised if out of bound.
177  */
178  virtual void setLowLimit(const GUM_SCALAR& newLowLimit);
179 
180  /**
181  * @brief Gets lowest possible value.
182  * @return Returns the lowest possible value.
183  */
184  const GUM_SCALAR& lowLimit() const;
185 
186  /**
187  * @brief Sets Highest possible value.
188  * @param newHighLimit New higher bound.
189  * @throw OutOfLowerBound Raised if out of bound.
190  */
191  virtual void setHighLimit(const GUM_SCALAR& newHighLimit);
192 
193  /**
194  * @brief Gets Highest possible value.
195  * @return Returns the highest possible value.
196  */
197  const GUM_SCALAR& highLimit() const;
198  /// @}
199 
200  protected:
201  /// Lowest value possible.
202  GUM_SCALAR lowLimit_;
203 
204  /// Highest value possible.
205  GUM_SCALAR highLimit_;
206 
207  /// Approximation factor.
208  GUM_SCALAR epsilon_;
209 
210  /**
211  * @brief Concretely computes the approximate representation.
212  *
213  * @warning We accept value smaller or higher than limits : please @see
214  * gum::ApproximationPolicy::safeFromExact(const GUM_SCALAR&).
215  *
216  * @param value The value to encode.
217  * @return The encoded value.
218  */
219  Idx _encode_(const GUM_SCALAR& value) const;
220 
221  /**
222  * @brief Concretely computes the approximate value from representation.
223  * @param representation The approximate value to decode.
224  * @return The decoded value.
225  */
226  GUM_SCALAR _decode_(const GUM_SCALAR& representation) const;
227 
228  /**
229  * @brief Get the number of interval.
230  */
231  void computeNbInterval_();
232 
233  /// The number of interval.
235  };
236 } // namespace gum
237 
238 
239 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
240 extern template class gum::LinearApproximationPolicy< double >;
241 #endif
242 
243 
244 // Always inline template classes implementation.
245 #include <agrum/tools/core/approximations/linearApproximationPolicy_tpl.h>
246 
247 #endif /* GUM_LINEAR_APPROXIMATION_POLICY_H */
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.
void combineDiv(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using division with the given gum::ApproximationPolicy.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
LinearApproximationPolicy(const LinearApproximationPolicy< GUM_SCALAR > *md)
Copy constructor.
void combineMin(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using min with the given gum::ApproximationPolicy.
GUM_SCALAR _decode_(const GUM_SCALAR &representation) const
Concretely computes the approximate value from representation.
virtual void setLimits(const GUM_SCALAR &newLowLimit, const GUM_SCALAR &newHighLimit)
Set bounds in a whole.
Idx _encode_(const GUM_SCALAR &value) const
Concretely computes the approximate representation.
void computeNbInterval_()
Get the number of interval.
const GUM_SCALAR & highLimit() const
Gets Highest possible value.
GUM_SCALAR highLimit_
Highest value possible.
GUM_SCALAR lowLimit_
Lowest value possible.
Idx encode(const GUM_SCALAR &value) const
Encode a given value into its approximation representation.
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.
Idx nbInterval_
The number of interval.
void combineMult(const ApproximationPolicy< GUM_SCALAR > *ap)
Combine using multiplication with the given gum::ApproximationPolicy.
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.
GUM_SCALAR epsilon_
Approximation factor.
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.