aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
nodeDatabase_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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#pragma once
42
43
50// =========================================================================
52
53// =========================================================================
54
55namespace gum {
56
57 // ==========================================================================
58 // Constructor & destructor.
59 // ==========================================================================
60
61 // ###################################################################
62 // Default constructor
63 // ###################################################################
64 template < TESTNAME AttributeSelection, bool isScalar >
66 const DiscreteVariable* value) :
67 _value_(value) {
68 GUM_CONSTRUCTOR(NodeDatabase);
69
71 varIter != attrSet->cendSafe();
72 ++varIter)
73 _attrTable_.insert(*varIter, new TestPolicy< ValueType >());
74
76 }
77
78 // ###################################################################
79 // Default destructor
80 // ###################################################################
81 template < TESTNAME AttributeSelection, bool isScalar >
83 for (auto varIter = _attrTable_.beginSafe(); varIter != _attrTable_.endSafe(); ++varIter)
84 delete varIter.val();
85
86 GUM_DESTRUCTOR(NodeDatabase);
87 }
88
89 // ==========================================================================
90 // Observation handling methods
91 // ==========================================================================
92
93 // ###################################################################
94 /* Updates database with new observation
95 *
96 * Calls either @fn _addObservation_( const Observation*, Int2Type<true>)
97 * or @fn _addObservation_( const Observation*, Int2Type<false>)
98 * depending on if we're learning reward function or transition probability
99 */
100 // ###################################################################
101 template < TESTNAME AttributeSelection, bool isScalar >
104 this->_addObservation_(newObs, Int2Type< isScalar >());
106
107 template < TESTNAME AttributeSelection, bool isScalar >
110 for (auto varIter = _attrTable_.cbeginSafe(); varIter != _attrTable_.cendSafe(); ++varIter)
111 varIter.val()->addObservation(newObs->rModality(varIter.key()), newObs->reward());
112
113 if (_valueCount_.exists(newObs->reward())) _valueCount_[newObs->reward()]++;
114 else _valueCount_.insert(newObs->reward(), 1);
115 }
116
117 template < TESTNAME AttributeSelection, bool isScalar >
120 for (auto varIter = _attrTable_.cbeginSafe(); varIter != _attrTable_.cendSafe(); ++varIter)
121 varIter.val()->addObservation(newObs->modality(varIter.key()), newObs->modality(_value_));
123 if (_valueCount_.exists(newObs->modality(_value_))) _valueCount_[newObs->modality(_value_)]++;
124 else _valueCount_.insert(newObs->modality(_value_), 1);
126
127 // ==========================================================================
128 // Aggregation Methods
129 // ==========================================================================
130
131
132 // ###################################################################
133 // Merges given NodeDatabase informations into current nDB.
134 // ###################################################################
135 template < TESTNAME AttributeSelection, bool isScalar >
139 this->_nbObservation_ += src.nbObservation();
140
141 for (auto varIter = _attrTable_.beginSafe(); varIter != _attrTable_.endSafe(); ++varIter)
142 varIter.val()->add(*(src.testPolicy(varIter.key())));
143
144 for (auto valIter = src.cbeginValues(); valIter != src.cendValues(); ++valIter)
145 if (_valueCount_.exists(valIter.key())) _valueCount_[valIter.key()] += valIter.val();
146 else _valueCount_.insert(valIter.key(), valIter.val());
147
148 return *this;
149 }
150
151 template < TESTNAME AttributeSelection, bool isScalar >
153 std::string ss = std::format("NbObservation : {}\n", this->nbObservation());
154 for (auto varIter = _attrTable_.beginSafe(); varIter != _attrTable_.endSafe(); ++varIter)
155 ss += std::format("\t\tVariable : {} - Associated Test : {}\n",
156 varIter.key()->name(),
157 _attrTable_[varIter.key()]->toString());
158 return ss;
159 }
160
161 // ==========================================================================
162 // Allocators and Deallocators redefinition
163 // ==========================================================================
164
165 template < TESTNAME AttributeSelection, bool isScalar >
169
170 template < TESTNAME AttributeSelection, bool isScalar >
174
175 // ==========================================================================
176 // Observation handling methods
177 // ==========================================================================
178
179 template < TESTNAME AttributeSelection, bool isScalar >
182 }
184 // ==========================================================================
185 // Variable Test Methods
186 // ==========================================================================
187
188 template < TESTNAME AttributeSelection, bool isScalar >
190 const DiscreteVariable* var) const {
191 return _attrTable_[var]->isTestRelevant();
192 }
194 template < TESTNAME AttributeSelection, bool isScalar >
195 double
197 return _attrTable_[var]->score();
198 }
199
200 template < TESTNAME AttributeSelection, bool isScalar >
202 const DiscreteVariable* var) const {
203 return _attrTable_[var]->secondaryscore();
204 }
205
206 // ==========================================================================
207 // Aggregation Methods
208 // ==========================================================================
209
210 template < TESTNAME AttributeSelection, bool isScalar >
215
216 template < TESTNAME AttributeSelection, bool isScalar >
221
222 template < TESTNAME AttributeSelection, bool isScalar >
227
228 template < TESTNAME AttributeSelection, bool isScalar >
232
233 template < TESTNAME AttributeSelection, bool isScalar >
237
238 template < TESTNAME AttributeSelection, bool isScalar >
242
243 template < TESTNAME AttributeSelection, bool isScalar >
247
248} // End of namespace gum
249
250// LEFT HERE ON PURPOSE
251// NOT TO BE DELETED
252
253/*template<TESTNAME AttributeSelection, bool isScalar>
254double *NodeDatabase<AttributeSelection, isScalar>::effectif(){
255 double* ret = static_cast<double*>(
256SmallObjectAllocator::instance().allocate(sizeof(double)* _value_->domainSize()));
257 for(Idx modality = 0; modality < _value_->domainSize(); ++modality)
258 if( _valueCount_.exists(modality) )
259 ret[modality] = (double) _valueCount_[modality];
260 else
261 ret[modality] = 0.0;
262 return ret;
263}*/
264
265/*template<TESTNAME AttributeSelection, bool isScalar>
266double NodeDatabase<AttributeSelection, isScalar>::reward(){
267 double ret = 0.0;
268 for(auto valuTer = _valueCount_.cbeginSafe(); valuTer !=
269 _valueCount_.cendSafe(); ++valuTer)
270 ret += valuTer.key() * (double) valuTer.val();
271 return ret / _nbObservation_;
272}*/
Base class for discrete random variable.
Safe Const Iterators for hashtables.
Definition hashTable.h:1662
const TestPolicy< ValueType > * testPolicy(const DiscreteVariable *var) const
Returns a reference to nDB test policy for given variable (so that test policy information can be mer...
NodeDatabase(const gum::VariableSet *, const DiscreteVariable *=nullptr)
Default constructor.
~NodeDatabase()
Default destructor.
INLINE bool isTestRelevant(const DiscreteVariable *var) const
Indicates wether or not, node has sufficient observation so that any statistic is relevant.
typename ValueSelect< isScalar, double, Idx >::type ValueType
INLINE Idx nbObservation() const
Nb observation taken into account by this instance.
const DiscreteVariable * _value_
So does this reference on the value observed.
Idx effectif(Idx moda) const
Idx _valueDomain_(Int2Type< true >) const
typename TestSelect< AttributeSelection, GTestPolicy< GUM_ELEMENT >, Chi2TestPolicy< GUM_ELEMENT >, LeastSquareTestPolicy< GUM_ELEMENT > >::type TestPolicy
const HashTableConstIteratorSafe< ValueType, Idx > cendValues() const
Merges given NodeDatabase informations into current nDB.
void _addObservation_(const Observation *, Int2Type< true >)
std::string toString() const
HashTable< ValueType, Idx > _valueCount_
INLINE double testValue(const DiscreteVariable *var) const
Returns the performance of given variables according to selection criterion.
INLINE double testOtherCriterion(const DiscreteVariable *var) const
Returns the performance of given variables according to selection secondary criterion (to break ties)...
NodeDatabase< AttributeSelection, isScalar > & operator+=(const NodeDatabase< AttributeSelection, isScalar > &src)
Merges given NodeDatabase informations into current nDB.
HashTable< const DiscreteVariable *, TestPolicy< ValueType > * > _attrTable_
Table giving for every variables its instantiation.
const HashTableConstIteratorSafe< ValueType, Idx > cbeginValues() const
Iterators on value count to recopy correctly its content.
void addObservation(const Observation *)
Nb observation taken into account by this instance.
Idx modality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
double reward() const
Returns the modality assumed by the given variable in this observation.
Idx rModality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
Safe iterators for the Set class.
Definition set.h:592
static const const_iterator_safe & cendSafe() noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:403
const_iterator_safe cbeginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:391
static SmallObjectAllocator & instance()
void * allocate(const size_t &objectSize)
Allocates a block.
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
Headers of the NodeDatabase class.