aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
variableselector.cpp
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
48// =========================================================================
50
51#ifdef GUM_NO_INLINE
53#endif // GUM_NO_INLINE
54
55// =========================================================================
56
57namespace gum {
58
59 // ==========================================================================
60 // Constructor & destructor.
61 // ==========================================================================
62
63 // ###################################################################
64 // Default constructor
65 // ###################################################################
67 _remainingVars_(startingSet) {
68 GUM_CONSTRUCTOR(VariableSelector)
69 _remainingScores_.insert(0.0, 0.0);
71
72 for (auto varIter = _remainingVars_.cbeginSafe(); varIter != _remainingVars_.cendSafe();
73 ++varIter) {
74 _remainingVarsScore_.insert(*varIter, 0.0);
75 _remainingVarsOtherScore_.insert(*varIter, 0.0);
76 }
77 }
78
79 // ###################################################################
80 // Default constructor
81 // ###################################################################
83
84 // ###################################################################
85 //
86 // ###################################################################
88 double score,
89 double secondaryscore) {
90 _removeVar_(var);
91 _remainingVarsScore_[var] += score;
92 _addVar_(var);
93 _remainingVarsOtherScore_[var] += secondaryscore;
94 }
95
96 // ###################################################################
97 //
98 // ###################################################################
100 double score,
101 double secondaryscore) {
102 _removeVar_(var);
103 _remainingVarsScore_[var] -= score;
104 _addVar_(var);
105 _remainingVarsOtherScore_[var] -= secondaryscore;
106 }
107
108 // ###################################################################
109 // Select the most relevant variable
110 // ###################################################################
112 double bestScore = _remainingScores_.top();
113 const gum::VariableSet* bestSet = _remainingVarsByScore_[bestScore];
114 const DiscreteVariable* bestVar = nullptr;
115
116 for (auto varIter = bestSet->beginSafe(); varIter != bestSet->endSafe(); ++varIter) {
117 if (bestVar == nullptr
120 && bestVar->domainSize() < (*varIter)->domainSize()))
121 bestVar = *varIter;
122 }
123 _removeVar_(bestVar);
124 _remainingVars_ >> bestVar;
125 return bestVar;
126 }
127
128 // ###################################################################
129 // Select the most relevant variable
130 // ###################################################################
132 double varScore = _remainingVarsScore_[var];
133
134 if (!_remainingVarsByScore_.exists(varScore)) {
135 _remainingVarsByScore_.insert(varScore, new gum::VariableSet());
136 _remainingScores_.insert(varScore, varScore);
137 }
138 _remainingVarsByScore_[varScore]->insert(var);
139 }
140
141 // ###################################################################
142 // Select the most relevant variable
143 // ###################################################################
145 double varScore = _remainingVarsScore_[var];
146 gum::VariableSet* varSet = _remainingVarsByScore_[varScore];
147 *varSet >> var;
148 if (varSet->empty()) {
149 _remainingScores_.erase(varScore);
150 _remainingVarsByScore_.erase(varScore);
151 delete varSet;
152 }
153 }
154} // namespace gum
Base class for discrete random variable.
virtual Size domainSize() const =0
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:385
static const iterator_safe & endSafe() noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:397
HashTable< const DiscreteVariable *, double > _remainingVarsScore_
HashTable associating to each variable its score.
void _removeVar_(const DiscreteVariable *var)
The set of remaining vars to select among.
void updateScore(const DiscreteVariable *var, double score, double secondaryscore)
The set of remaining vars to select among.
void downdateScore(const DiscreteVariable *var, double score, double secondaryscore)
The set of remaining vars to select among.
~VariableSelector()
Default destructor.
VariableSelector(const gum::VariableSet &startingSet)
Default constructor.
gum::VariableSet _remainingVars_
The set of remaining vars to select among.
const DiscreteVariable * select()
Select the most relevant variable.
HashTable< const DiscreteVariable *, double > _remainingVarsOtherScore_
HashTable associating to each variable its 2nd score.
MultiPriorityQueue< double, double, std::greater< double > > _remainingScores_
Heap keeping best score on top for immediate access.
void _addVar_(const DiscreteVariable *var)
The set of remaining vars to select among.
HashTable< double, gum::VariableSet * > _remainingVarsByScore_
HashTable associating to each score the set of variable having that score.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
Headers of the Variable Selector class.