aGrUM 2.3.2
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-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
48// =========================================================================
50
51// =========================================================================
52
53namespace gum {
54
55 // ==========================================================================
56 // Constructor & destructor.
57 // ==========================================================================
58
59 // ###################################################################
60 // Default constructor
61 // ###################################################################
63 _remainingVars_(startingSet) {
64 GUM_CONSTRUCTOR(VariableSelector)
65 _remainingScores_.insert(0.0, 0.0);
67
68 for (auto varIter = _remainingVars_.cbeginSafe(); varIter != _remainingVars_.cendSafe();
69 ++varIter) {
70 _remainingVarsScore_.insert(*varIter, 0.0);
71 _remainingVarsOtherScore_.insert(*varIter, 0.0);
72 }
73 }
74
75 // ###################################################################
76 // Default constructor
77 // ###################################################################
79
80 // ###################################################################
81 //
82 // ###################################################################
84 double score,
85 double secondaryscore) {
86 _removeVar_(var);
87 _remainingVarsScore_[var] += score;
88 _addVar_(var);
89 _remainingVarsOtherScore_[var] += secondaryscore;
90 }
91
92 // ###################################################################
93 //
94 // ###################################################################
96 double score,
97 double secondaryscore) {
98 _removeVar_(var);
99 _remainingVarsScore_[var] -= score;
100 _addVar_(var);
101 _remainingVarsOtherScore_[var] -= secondaryscore;
102 }
103
104 // ###################################################################
105 // Select the most relevant variable
106 // ###################################################################
108 double bestScore = _remainingScores_.top();
109 const gum::VariableSet* bestSet = _remainingVarsByScore_[bestScore];
110 const DiscreteVariable* bestVar = nullptr;
111
112 for (auto varIter = bestSet->beginSafe(); varIter != bestSet->endSafe(); ++varIter) {
113 if (bestVar == nullptr
116 && bestVar->domainSize() < (*varIter)->domainSize()))
117 bestVar = *varIter;
118 }
119 _removeVar_(bestVar);
120 _remainingVars_ >> bestVar;
121 return bestVar;
122 }
123
124 // ###################################################################
125 // Select the most relevant variable
126 // ###################################################################
128 double varScore = _remainingVarsScore_[var];
129
130 if (!_remainingVarsByScore_.exists(varScore)) {
131 _remainingVarsByScore_.insert(varScore, new gum::VariableSet());
132 _remainingScores_.insert(varScore, varScore);
133 }
134 _remainingVarsByScore_[varScore]->insert(var);
135 }
136
137 // ###################################################################
138 // Select the most relevant variable
139 // ###################################################################
141 double varScore = _remainingVarsScore_[var];
142 gum::VariableSet* varSet = _remainingVarsByScore_[varScore];
143 *varSet >> var;
144 if (varSet->empty()) {
145 _remainingScores_.erase(varScore);
146 _remainingVarsByScore_.erase(varScore);
147 delete varSet;
148 }
149 }
150} // namespace gum
Base class for discrete random variable.
virtual Size domainSize() const =0
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:414
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:426
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:642
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.