aGrUM  0.14.2
variableselector.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 // =========================================================================
28 // =========================================================================
29 
30 namespace gum {
31 
32  // ==========================================================================
33  // Constructor & destructor.
34  // ==========================================================================
35 
36  // ###################################################################
37  // Default constructor
38  // ###################################################################
40  __remainingVars(startingSet) {
41  GUM_CONSTRUCTOR(VariableSelector);
42  __remainingScores.insert(0.0, 0.0);
45 
46  for (auto varIter = __remainingVars.cbeginSafe();
47  varIter != __remainingVars.cendSafe();
48  ++varIter) {
49  __remainingVarsScore.insert(*varIter, 0.0);
50  __remainingVarsOtherScore.insert(*varIter, 0.0);
51  }
52  }
53 
54  // ###################################################################
55  // Default constructor
56  // ###################################################################
58 
59 
60  // ###################################################################
61  //
62  // ###################################################################
64  double score,
65  double secondaryscore) {
66  __removeVar(var);
67  __remainingVarsScore[var] += score;
68  __addVar(var);
69  __remainingVarsOtherScore[var] += secondaryscore;
70  }
71 
72 
73  // ###################################################################
74  //
75  // ###################################################################
77  double score,
78  double secondaryscore) {
79  __removeVar(var);
80  __remainingVarsScore[var] -= score;
81  __addVar(var);
82  __remainingVarsOtherScore[var] -= secondaryscore;
83  }
84 
85 
86  // ###################################################################
87  // Select the most relevant variable
88  // ###################################################################
90  double bestScore = __remainingScores.top();
92  const DiscreteVariable* bestVar = nullptr;
93 
94  for (auto varIter = bestSet->beginSafe(); varIter != bestSet->endSafe();
95  ++varIter) {
96  if (bestVar == nullptr
97  || __remainingVarsOtherScore[bestVar]
98  < __remainingVarsOtherScore[*varIter]
99  || (__remainingVarsOtherScore[bestVar]
100  == __remainingVarsOtherScore[*varIter]
101  && bestVar->domainSize() < (*varIter)->domainSize()))
102  bestVar = *varIter;
103  }
104  __removeVar(bestVar);
105  __remainingVars >> bestVar;
106  return bestVar;
107  }
108 
109 
110  // ###################################################################
111  // Select the most relevant variable
112  // ###################################################################
114  double varScore = __remainingVarsScore[var];
115 
116  if (!__remainingVarsByScore.exists(varScore)) {
117  __remainingVarsByScore.insert(varScore,
119  __remainingScores.insert(varScore, varScore);
120  }
121  __remainingVarsByScore[varScore]->insert(var);
122  }
123 
124 
125  // ###################################################################
126  // Select the most relevant variable
127  // ###################################################################
129  double varScore = __remainingVarsScore[var];
131  *varSet >> var;
132  if (varSet->empty()) {
133  __remainingScores.erase(varScore);
134  __remainingVarsByScore.erase(varScore);
135  delete varSet;
136  }
137  }
138 } // namespace gum
HashTable< const DiscreteVariable *, double > __remainingVarsScore
HashTable associating to each variable its score.
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:704
HashTable< const DiscreteVariable *, double > __remainingVarsOtherScore
HashTable associating to each variable its 2nd score.
const Val & top() const
Returns the element at the top of the priority queue.
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).
void downdateScore(const DiscreteVariable *var, double score, double secondaryscore)
The set of remaining vars to select among.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
MultiPriorityQueue< double, double, std::greater< double > > __remainingScores
Heap keeping best score on top for immediate access.
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
Definition: set_tpl.h:499
virtual Size domainSize() const =0
~VariableSelector()
Default destructor.
Set< const DiscreteVariable *> __remainingVars
The set of remaining vars to select among.
void __removeVar(const DiscreteVariable *var)
The set of remaining vars to select among.
const DiscreteVariable * select()
Select the most relevant variable.
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
void __addVar(const DiscreteVariable *var)
The set of remaining vars to select among.
Headers of the Variable Selector class.
<agrum/FMDP/planning/FunctionGraph/variableselector.h>
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition: set_tpl.h:485
void updateScore(const DiscreteVariable *var, double score, double secondaryscore)
The set of remaining vars to select among.
HashTable< double, Set< const DiscreteVariable *> *> __remainingVarsByScore
HashTable associating to each score the set of variable having that score.
VariableSelector(Set< const DiscreteVariable * > &startingSet)
Default constructor.