aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
labelizedVariable.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
47#ifndef GUM_LABELIZED_DISCRETE_VARIABLE_H
48#define GUM_LABELIZED_DISCRETE_VARIABLE_H
49
50#include <iostream>
51#include <sstream>
52#include <string>
53
54#include <agrum/agrum.h>
55
58
59namespace gum {
60 /* =========================================================================*/
61 /* =========================================================================*/
62 /* === GUM_LABELIZED_VARIABLE === */
63 /* =========================================================================*/
64 /* =========================================================================*/
69 /* =========================================================================*/
70
71 class LabelizedVariable final: public DiscreteVariable {
72 public:
73 // ############################################################################
75 // ############################################################################
77
83
84 LabelizedVariable(const std::string& aName,
85 const std::string& aDesc = "",
86 const Size nbrLabel = 2);
87
93
94 LabelizedVariable(const std::string& aName,
95 const std::string& aDesc,
96 const std::vector< std::string >& labels);
97
99
102
104
106
108
110 LabelizedVariable* clone() const final;
111
113
114 // ############################################################################
116 // ############################################################################
118
120
121 LabelizedVariable& operator=(const LabelizedVariable& aLDRV);
122
123
125
128 Idx index(const std::string& label) const final;
129
131
132 // ############################################################################
134 // ############################################################################
136
142 bool isLabel(const std::string& aLabel) const;
143
146
151 LabelizedVariable& addLabel(const std::string& aLabel);
152
154
163 void changeLabel(Idx pos, const std::string& aLabel) const;
164
167
169
171 std::string label(Idx i) const final;
172
174 Idx posLabel(const std::string& label) const;
175
177 double numerical(Idx index) const final;
178
180 Idx closestIndex(double val) const final;
181
183 Size domainSize() const final;
184
186 VarType varType() const final;
187
189 std::string domain() const final;
190
192 std::string stype() const final { return "Labelized"; };
193
194 // returns the variable in fast syntax
195 std::string toFast() const final;
196
198
199
200 private:
202
205 bool _checkSameDomain_(const Variable& aRV) const final;
206
207 protected:
210
212
213 void copy_(const LabelizedVariable& aLDRV);
214
215 private:
218 };
219
220} /* namespace gum */
221
222/* ==============================================================================
223 */
224/* ==============================================================================
225 */
226/* === LabelizedVariable IMPLEMENTATION === */
227/* ==============================================================================
228 */
229/* ==============================================================================
230 */
231#ifndef GUM_NO_INLINE
233#endif /* GUM_NO_INLINE */
234
235#endif /* GUM_LABELIZED_DISCRETE_VARIABLE_H */
DiscreteVariable(const std::string &aName, const std::string &aDesc)
Default constructor.
std::vector< std::string > labels() const
vector of labels
Idx closestIndex(double val) const final
returns the closest index of the value
std::string domain() const final
Returns the domain.
void eraseLabels()
erase all the labels
LabelizedVariable * clone() const final
a virtual clone ...
Idx posLabel(const std::string &label) const
return the pos from label
bool isLabel(const std::string &aLabel) const
indicates whether the variable already has the label passed in argument
LabelizedVariable(const std::string &aName, const std::string &aDesc="", const Size nbrLabel=2)
constructor
void copy_(const LabelizedVariable &aLDRV)
copies the content of aLDRV
LabelizedVariable(const LabelizedVariable &aLDRV)
Copy constructor.
LabelizedVariable & addLabel(const std::string &aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
Idx index(const std::string &label) const final
returns the index of a given label
double numerical(Idx index) const final
get a numerical representation of the index-the value.
Size domainSize() const final
returns the size of the random discrete variable domain
VarType varType() const final
returns the type of variable
~LabelizedVariable() final
destructor
bool _checkSameDomain_(const Variable &aRV) const final
check the domain
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
Sequence< std::string > _labels_
the set of labels contained in the variable
std::string label(Idx i) const final
returns the ith label
LabelizedVariable(const std::string &aName, const std::string &aDesc, const std::vector< std::string > &labels)
constructor
std::string toFast() const final
indicates whether the variable already has the label passed in argument
std::string stype() const final
string represent the type of the variable
Base class for every random variable.
Definition variable.h:79
Base class for discrete random variable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Base class for labelized discrete random variables.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:60
STL namespace.
Header file of gum::Sequence, a class for storing (ordered) sequences of objects.