aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
linearApproximationPolicy.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 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 virtual ApproximationPolicy< GUM_SCALAR > {
56  public:
57  // ===========================================================================
58  /// @name Constructors / Destructors
59  // ===========================================================================
60  /// @{
61 
62  /**
63  * @brief Default constructor.
64  *
65  * @param low The lower limit.
66  * @param high The higher limit.
67  * @param eps The epsilon.
68  * @throw OutOfBound if out of bounds (low<high, eps>0)
69  */
70  LinearApproximationPolicy(GUM_SCALAR low = (GUM_SCALAR)0.0,
71  GUM_SCALAR high = (GUM_SCALAR)1.0,
72  GUM_SCALAR eps = (GUM_SCALAR)0.1);
73 
74  /**
75  * @brief Copy constructor.
76  * @param md The gum::LinearApproximationPolicy to copy.
77  */
79 
80  /// @}
81 
82  // ===========================================================================
83  /// @name Accessors/Modifiers
84  // ===========================================================================
85  /// @{
86 
87  /**
88  * @brief Convert value to his approximation.
89  * @param value The converted value.
90  * @return The value approximation representation.
91  */
92  GUM_SCALAR fromExact(const GUM_SCALAR& value) const;
93 
94  /**
95  * @brief Combine using addition with the given gum::ApproximationPolicy.
96  * @param ap The policy to combine with.
97  */
98  void combineAdd(const ApproximationPolicy< GUM_SCALAR >* ap);
99 
100  /**
101  * @brief Combine using substraction with the given
102  * gum::ApproximationPolicy.
103  * @param ap The policy to combine with.
104  */
105  void combineSub(const ApproximationPolicy< GUM_SCALAR >* ap);
106 
107  /**
108  * @brief Combine using multiplication with the given
109  * gum::ApproximationPolicy.
110  * @param ap The policy to combine with.
111  */
112  void combineMult(const ApproximationPolicy< GUM_SCALAR >* ap);
113 
114  /**
115  * @brief Combine using division with the given gum::ApproximationPolicy.
116  * @param ap The policy to combine with.
117  */
118  void combineDiv(const ApproximationPolicy< GUM_SCALAR >* ap);
119 
120  /**
121  * @brief Combine using max with the given gum::ApproximationPolicy.
122  * @param ap The policy to combine with.
123  */
124  void combineMax(const ApproximationPolicy< GUM_SCALAR >* ap);
125 
126  /**
127  * @brief Combine using min with the given gum::ApproximationPolicy.
128  * @param ap The policy to combine with.
129  */
130  void combineMin(const ApproximationPolicy< GUM_SCALAR >* ap);
131 
132  /**
133  * @brief Convert value to his approximation.
134  *
135  * This method is slower than @ref fromExact since it verifies the
136  * bounds.
137  *
138  * @throw OutOfLowerBound Raised if value is out of bounds.
139  * @throw OutOfUpperBound Raised if value is out of bounds.
140  */
141  GUM_SCALAR safeFromExact(const GUM_SCALAR& value);
142 
143  /**
144  * @brief Encode a given value into its approximation representation.
145  * @param value The to encode.
146  * @return Returns the encoded value.
147  * @throw OutOfLowerBound Raised if value is out of bounds.
148  * @throw OutOfUpperBound Raised if value is out of bounds.
149  */
150  Idx encode(const GUM_SCALAR& value) const;
151 
152  /**
153  * @brief Convert approximation representation to value.
154  * @param representation The approximation representation to decode.
155  * @return Returns the value decoded from its approximation
156  * reprensentation.
157  */
158  GUM_SCALAR decode(Idx representation) const;
159 
160  /**
161  * @brief Sets approximation factor.
162  * @param e The new epsilon value.
163  */
164  virtual void setEpsilon(const GUM_SCALAR& e);
165 
166  /**
167  * @brief Set bounds in a whole.
168  * @param newLowLimit New lower bound.
169  * @param newHighLimit New higher bound.
170  * @throw OutOfBounds Raised if new bounds are not legit.
171  */
172  virtual void setLimits(const GUM_SCALAR& newLowLimit,
173  const GUM_SCALAR& newHighLimit);
174 
175  /**
176  * @brief Sets lowest possible value.
177  * @param newLowLimit New lower bound.
178  * @throw OutOfUpperBound Raised if out of bound.
179  */
180  virtual void setLowLimit(const GUM_SCALAR& newLowLimit);
181 
182  /**
183  * @brief Gets lowest possible value.
184  * @return Returns the lowest possible value.
185  */
186  const GUM_SCALAR& lowLimit() const;
187 
188  /**
189  * @brief Sets Highest possible value.
190  * @param newHighLimit New higher bound.
191  * @throw OutOfLowerBound Raised if out of bound.
192  */
193  virtual void setHighLimit(const GUM_SCALAR& newHighLimit);
194 
195  /**
196  * @brief Gets Highest possible value.
197  * @return Returns the highest possible value.
198  */
199  const GUM_SCALAR& highLimit() const;
200  /// @}
201 
202  protected:
203  /// Lowest value possible.
204  GUM_SCALAR lowLimit_;
205 
206  /// Highest value possible.
207  GUM_SCALAR highLimit_;
208 
209  /// Approximation factor.
210  GUM_SCALAR epsilon_;
211 
212  /**
213  * @brief Concretely computes the approximate representation.
214  *
215  * @warning We accept value smaller or higher than limits : please @see
216  * gum::ApproximationPolicy::safeFromExact(const GUM_SCALAR&).
217  *
218  * @param value The value to encode.
219  * @return The encoded value.
220  */
221  Idx encode__(const GUM_SCALAR& value) const;
222 
223  /**
224  * @brief Concretely computes the approximate value from representation.
225  * @param representation The approximate value to decode.
226  * @return The decoded value.
227  */
228  GUM_SCALAR decode__(const GUM_SCALAR& representation) const;
229 
230  /**
231  * @brief Get the number of interval.
232  */
233  void computeNbInterval_();
234 
235  /// The number of interval.
237  };
238 } // namespace gum
239 
240 
241 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
242 extern template class gum::LinearApproximationPolicy< double >;
243 #endif
244 
245 
246 // Always inline template classes implementation.
247 #include <agrum/tools/core/approximations/linearApproximationPolicy_tpl.h>
248 
249 #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:669
LinearApproximationPolicy(const LinearApproximationPolicy< GUM_SCALAR > *md)
Copy constructor.
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.
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.
GUM_SCALAR decode__(const GUM_SCALAR &representation) const
Concretely computes the approximate value from 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.
Idx encode__(const GUM_SCALAR &value) const
Concretely computes the approximate representation.
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.