aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
discretizedVariable.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 #ifndef GUM_DISCRETIZED_VARIABLE_H
23 #define GUM_DISCRETIZED_VARIABLE_H
24 
25 #include <iostream>
26 #include <limits>
27 #include <sstream>
28 #include <vector>
29 
30 #include <agrum/agrum.h>
31 
32 #include <agrum/tools/variables/IDiscretizedVariable.h>
33 
34 namespace gum {
35  /**
36  * @class DiscretizedVariable
37  * @brief Class for discretized random variable.
38  * @ingroup multidim_group
39  *
40  * a DiscretizedVariable contains a vector of T_TICKS, assuming that the
41  * modalities of the variable arc [T1,T2[,[T2,T3[,...,[Tn-1,Tn]. T can be
42  * plus_infinity or minus_infinity.
43  *
44  * operator [] has a T_TICKS argument.
45  * @warning DiscretizedVariable is a template but it is assumed that there is
46  * a complete order on T_TICKS.
47  * @warning Note that, if T_TICKS allows it, you may use
48  * std::numeric_limits<T_TICKS>::infinity() and
49  * std::numeric_limits<T_TICKS>::infinity() as tick value.
50  * @warning If a variable contains less than 3 ticks, it is considered as
51  * empty(). A variable with 3 ticks is binary and contains 2 ranges :
52  * [T1,T2[ (modality 0) and [T2,T3] (modality 1).
53  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
54  */
55  template < typename T_TICKS >
56  class DiscretizedVariable: public IDiscretizedVariable {
57  private:
58  std::vector< T_TICKS > _ticks_; // Array from 0 to domainSize-2
59  Size _ticks_size_;
60 
61  protected:
62  /**
63  * make a copy
64  * @param aDRV the copied object
65  */
66  void copy_(const DiscretizedVariable< T_TICKS >& aDRV);
67 
68  /**
69  * perform a dichotomy on ticks
70  * @param target T_TICKS value
71  * @param min first index
72  * @param max last index
73  * @return either the index of target, either the index of the predecessor
74  of
75  target in ticks
76  */
77  Idx dichotomy_(const T_TICKS& target, Idx min, Idx max) const;
78 
79  /**
80  * seach the class of target (internally use dichotomy_)
81  * @param target
82  * @return the class of target
83  */
84  Idx pos_(const T_TICKS& target) const;
85 
86  public:
87  /** @name constructors & destructors
88  * @{
89  **/
90 
91  /**
92  * Constructor
93  * @param aName the name
94  * @param aDesc the description
95  */
96  DiscretizedVariable(const std::string& aName, const std::string& aDesc);
97 
98  /**
99  * Constructor
100  * @param aName the name
101  * @param aDesc the description
102  */
103  DiscretizedVariable(const std::string& aName,
104  const std::string& aDesc,
105  const std::vector< T_TICKS >& ticks);
106 
107  /**
108  * Copy constructor
109  * @param aDRV
110  */
111  DiscretizedVariable(const DiscretizedVariable< T_TICKS >& aDRV);
112 
113  /**
114  * Destructor.
115  */
116  virtual ~DiscretizedVariable();
117 
118  /// @}
119 
120  /// a virtual clone
121  virtual DiscretizedVariable< T_TICKS >* clone() const;
122 
123  /// returns the type of variable
124 
125  virtual VarType varType() const;
126 
127  /**
128  * operator =
129  * @param aDRV a labelized discrete random variable
130  * @return a reference to *this
131  **/
132  DiscretizedVariable< T_TICKS >& operator=(const DiscretizedVariable< T_TICKS >& aDRV);
133 
134  /**
135  *
136  * @param aTick
137  * @return true if the tick already exists
138  */
139  bool isTick(const T_TICKS& aTick) const;
140 
141  /**
142  * add a tick.
143  * @param aTick
144  * @throw DefaultInLabel
145  */
146  DiscretizedVariable& addTick(const T_TICKS& aTick);
147 
148  /**
149  * erase all the Ticks
150  */
151  void eraseTicks();
152 
153  /**
154  * @param i
155  * @return the ith label
156  * @throw OutOfBound
157  */
158  virtual std::string label(Idx i) const;
159 
160  /// get a numerical representation of he indice-the value.
161  virtual double numerical(Idx indice) const;
162 
163  /// from the label to its index in var.
164  /// @throws NotFound
165  virtual Idx index(const std::string& label) const;
166 
167  /**
168  *
169  * @return the size of the random discrete variable domain
170  */
171  virtual Size domainSize() const;
172  virtual const std::string domain() const;
173 
174  /// from the index to the tick.
175  /// @throws NotFound
176  const T_TICKS& tick(Idx i) const;
177 
178  /// Return the list of ticks
179  const std::vector< T_TICKS >& ticks() const;
180 
181  /// return the list of ticks as a vector of doubles
182  virtual std::vector< double > ticksAsDoubles() const;
183  };
184 
185 } /* namespace gum */
186 
187 /// always include the implementation of the templates
188 #include <agrum/tools/variables/discretizedVariable_tpl.h>
189 
190 #endif /* GUM_DISCRETIZED_VARIABLE_H */