aGrUM 2.3.2
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-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#pragma once
41
42
49// =========================================================================
51
52// =========================================================================
53
54namespace gum {
55
56 // ==========================================================================
57 // Constructor & destructor.
58 // ==========================================================================
59
60 // ###################################################################
61 // Default constructor
62 // ###################################################################
63 template < TESTNAME AttributeSelection, bool isScalar >
65 const DiscreteVariable* value) :
66 _value_(value) {
67 GUM_CONSTRUCTOR(NodeDatabase);
68
70 varIter != attrSet->cendSafe();
71 ++varIter)
72 _attrTable_.insert(*varIter, new TestPolicy< ValueType >());
73
75 }
76
77 // ###################################################################
78 // Default destructor
79 // ###################################################################
80 template < TESTNAME AttributeSelection, bool isScalar >
82 for (auto varIter = _attrTable_.beginSafe(); varIter != _attrTable_.endSafe(); ++varIter)
83 delete varIter.val();
84
85 GUM_DESTRUCTOR(NodeDatabase);
86 }
87
88 // ==========================================================================
89 // Observation handling methods
90 // ==========================================================================
91
92 // ###################################################################
93 /* Updates database with new observation
94 *
95 * Calls either @fn _addObservation_( const Observation*, Int2Type<true>)
96 * or @fn _addObservation_( const Observation*, Int2Type<false>)
97 * depending on if we're learning reward function or transition probability
98 */
99 // ###################################################################
100 template < TESTNAME AttributeSelection, bool isScalar >
105
106 template < TESTNAME AttributeSelection, bool isScalar >
109 for (auto varIter = _attrTable_.cbeginSafe(); varIter != _attrTable_.cendSafe(); ++varIter)
110 varIter.val()->addObservation(newObs->rModality(varIter.key()), newObs->reward());
111
112 if (_valueCount_.exists(newObs->reward())) _valueCount_[newObs->reward()]++;
113 else _valueCount_.insert(newObs->reward(), 1);
114 }
115
116 template < TESTNAME AttributeSelection, bool isScalar >
119 for (auto varIter = _attrTable_.cbeginSafe(); varIter != _attrTable_.cendSafe(); ++varIter)
120 varIter.val()->addObservation(newObs->modality(varIter.key()), newObs->modality(_value_));
121
122 if (_valueCount_.exists(newObs->modality(_value_))) _valueCount_[newObs->modality(_value_)]++;
123 else _valueCount_.insert(newObs->modality(_value_), 1);
125
126 // ==========================================================================
127 // Aggregation Methods
128 // ==========================================================================
129
130
131 // ###################################################################
132 // Merges given NodeDatabase informations into current nDB.
133 // ###################################################################
134 template < TESTNAME AttributeSelection, bool isScalar >
138 this->_nbObservation_ += src.nbObservation();
139
140 for (auto varIter = _attrTable_.beginSafe(); varIter != _attrTable_.endSafe(); ++varIter)
141 varIter.val()->add(*(src.testPolicy(varIter.key())));
142
143 for (auto valIter = src.cbeginValues(); valIter != src.cendValues(); ++valIter)
144 if (_valueCount_.exists(valIter.key())) _valueCount_[valIter.key()] += valIter.val();
145 else _valueCount_.insert(valIter.key(), valIter.val());
146
147 return *this;
148 }
149
150 template < TESTNAME AttributeSelection, bool isScalar >
152 std::stringstream ss;
153
154 ss << "NbObservation : " << this->nbObservation() << std::endl;
155 for (auto varIter = _attrTable_.beginSafe(); varIter != _attrTable_.endSafe(); ++varIter)
156 ss << "\t\tVariable : " << varIter.key()->name()
157 << " - Associated Test : " << _attrTable_[varIter.key()]->toString() << std::endl;
158
159 return ss.str();
160 }
161} // End of namespace gum
162
163// LEFT HERE ON PURPOSE
164// NOT TO BE DELETED
165
166/*template<TESTNAME AttributeSelection, bool isScalar>
167double *NodeDatabase<AttributeSelection, isScalar>::effectif(){
168 double* ret = static_cast<double*>(
169SmallObjectAllocator::instance().allocate(sizeof(double)* _value_->domainSize()));
170 for(Idx modality = 0; modality < _value_->domainSize(); ++modality)
171 if( _valueCount_.exists(modality) )
172 ret[modality] = (double) _valueCount_[modality];
173 else
174 ret[modality] = 0.0;
175 return ret;
176}*/
177
178/*template<TESTNAME AttributeSelection, bool isScalar>
179double NodeDatabase<AttributeSelection, isScalar>::reward(){
180 double ret = 0.0;
181 for(auto valuTer = _valueCount_.cbeginSafe(); valuTer !=
182 _valueCount_.cendSafe(); ++valuTer)
183 ret += valuTer.key() * (double) valuTer.val();
184 return ret / _nbObservation_;
185}*/
Base class for discrete random variable.
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 Idx nbObservation() const
Nb observation taken into account by this instance.
const DiscreteVariable * _value_
So does this reference on the value observed.
void _addObservation_(const Observation *, Int2Type< true >)
Updates database with new observation.
typename TestSelect< AttributeSelection, GTestPolicy< GUM_SCALAR >, Chi2TestPolicy< GUM_SCALAR >, LeastSquareTestPolicy< GUM_SCALAR > >::type TestPolicy
std::string toString() const
const HashTableConstIteratorSafe< ValueType, Idx > cendValues() const
Merges given NodeDatabase informations into current nDB.
const HashTableConstIteratorSafe< ValueType, Idx > cbeginValues() const
Iterators on value count to recopy correctly its content.
HashTable< ValueType, Idx > _valueCount_
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.
void addObservation(const Observation *)
Nb observation taken into account by this instance.
INLINE Idx modality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
INLINE Idx rModality(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.
Safe iterators for the Set class.
Definition set.h:601
const_iterator_safe cbeginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:420
const const_iterator_safe & cendSafe() const noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:432
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
Headers of the NodeDatabase class.