aGrUM  0.13.3
score.h
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  ***************************************************************************/
34 #ifndef GUM_LEARNING_SCORE_H
35 #define GUM_LEARNING_SCORE_H
36 
37 #include <agrum/agrum.h>
38 #include <agrum/core/math/math.h>
39 
43 
46 
47 namespace gum {
48 
49  namespace learning {
50 
51  /* =========================================================================
52  */
53  /* === SCORE CLASS ===
54  */
55  /* =========================================================================
56  */
71  template < typename IdSetAlloc = std::allocator< Idx >,
72  typename CountAlloc = std::allocator< double > >
73  class Score : private Counter< IdSetAlloc, CountAlloc > {
74  public:
75  // ##########################################################################
77  // ##########################################################################
79 
81 
89  template < typename RowFilter >
90  Score(const RowFilter& filter,
91  const std::vector< Size >& var_modalities,
93  Size min_range = 0,
94  Size max_range = std::numeric_limits< Size >::max());
95 
97  virtual Score< IdSetAlloc, CountAlloc >* copyFactory() const = 0;
98 
100  virtual ~Score();
101 
103 
104  // ##########################################################################
106  // ##########################################################################
108 
110 
123  Idx addNodeSet(Idx var);
124 
126 
140  Idx addNodeSet(Idx var, const std::vector< Idx >& conditioning_ids);
141 
143  void clear();
144 
146  void clearCache();
147 
149  void useCache(bool on_off) noexcept;
150 
153 
156 
158  virtual double score(Idx nodeset_index) = 0;
159 
162 
169  virtual std::string isAprioriCompatible() const = 0;
170 
172 
188  internalApriori() const noexcept = 0;
189 
191 
195  void setRange(Size min_range, Size max_range);
196 
198 
199  protected:
201  const double _1log2{M_LOG2E};
202 
205 
207 
219 
221 
223 
225 
228 
231 
233 
244  const std::vector< double, CountAlloc >& _getAllApriori(Idx index);
245 
247  const std::vector< double, CountAlloc >& _getConditioningApriori(Idx index);
248 
250  bool _isInCache(Idx nodeset_index) const noexcept;
251 
253  void _insertIntoCache(Idx nodeset_index, double score);
254 
256  double _cachedScore(Idx nodeset_index) const noexcept;
257 
259  bool _isUsingCache() const noexcept;
260 
262  Score(const Score< IdSetAlloc, CountAlloc >&);
263 
265  Score(Score< IdSetAlloc, CountAlloc >&&);
266 
267  private:
270 
272  bool __use_cache{true};
273 
275  std::vector< bool > __is_cached_score;
276 
278  std::vector< double > __cached_score;
279 
281  bool __apriori_computed{false};
282 
284  const std::vector< Idx > __empty_conditioning_set;
285 
286  // ##########################################################################
287  // ##########################################################################
288 
292  };
293 
294  } /* namespace learning */
295 
296 } /* namespace gum */
297 
298 
299 extern template class gum::learning::Score<>;
300 
301 
304 
305 #endif /* GUM_LEARNING_SCORE_H */
Score< IdSetAlloc, CountAlloc > & operator=(const Score< IdSetAlloc, CountAlloc > &)=delete
prevent copy operator
Useful macros for maths.
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
the base class for all the score&#39;s internal aprioris
void useCache(bool on_off) noexcept
turn on/off the use of a cache of the previously computed score
bool _isUsingCache() const noexcept
indicates whether we use the cache or not
The base class for all the scores used for learning (BIC, BDeu, etc)The class should be used as follo...
Definition: score.h:73
bool _isInCache(Idx nodeset_index) const noexcept
indicates whether a score belongs to the cache
the classes to account for structure changes in a graph
a cache for caching 3 points mutual information in 3off2Caching previously computed scores is very im...
Definition: cache4Score.h:59
virtual Score< IdSetAlloc, CountAlloc > * copyFactory() const =0
virtual copy factory
the base class for all a prioris
Apriori< IdSetAlloc, CountAlloc > * _apriori
the a priori used by the score
Definition: score.h:204
const std::vector< double, CountAlloc > & _getAllApriori(Idx index)
returns the apriori vector for a given (conditioned) target set
virtual std::string isAprioriCompatible() const =0
indicates whether the apriori is compatible (meaningful) with the score
the base class for all apriori
Definition: apriori.h:45
std::vector< bool > __is_cached_score
indicates whether the ith nodeset&#39;s score is in the cache or not
Definition: score.h:275
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void setRange(Size min_range, Size max_range)
sets the range of records taken into account by the counter
a cache for caching scores in Score classes (BIC, BDeu, K2, etc.)
bool __apriori_computed
has the a priori been computed
Definition: score.h:281
const std::vector< Idx > __empty_conditioning_set
an empty conditioning set
Definition: score.h:284
#define M_LOG2E
Definition: math.h:33
void clear()
clears all the data structures from memory
Idx addNodeSet(Idx var)
add a new single variable to be counted
virtual double score(Idx nodeset_index)=0
returns the score corresponding to a given nodeset
the base class for all the countings used for learning (BIC, BDeu, etc) as well as all the independen...
The counting class for all the scores used for learning (BIC, BDeu, etc) as well as for all the indep...
Definition: counter.h:108
Cache4Score __cache
a cache for the previously computed scores
Definition: score.h:269
the base class for all the score&#39;s internal apriorisSome scores include an apriori.
void _insertIntoCache(Idx nodeset_index, double score)
inserts a new score into the cache
double _cachedScore(Idx nodeset_index) const noexcept
returns a cached score
std::vector< double > __cached_score
the vector of scores for the current nodesets
Definition: score.h:278
virtual ~Score()
destructor
the base class for all the scores used for learning (BIC, BDeu, etc)
const std::vector< double, CountAlloc > & _getConditioningApriori(Idx index)
returns the apriori vector for a conditioning set
const double _1log2
1 / log(2)
Definition: score.h:201
bool __use_cache
a Boolean indicating whether we wish to use the cache
Definition: score.h:272
unsigned long Idx
Type for indexes.
Definition: types.h:43
void clearCache()
clears the current cache (clear nodesets as well)
virtual const ScoreInternalApriori< IdSetAlloc, CountAlloc > & internalApriori() const noexcept=0
returns the internal apriori of the score
Score(const RowFilter &filter, const std::vector< Size > &var_modalities, Apriori< IdSetAlloc, CountAlloc > &apriori, Size min_range=0, Size max_range=std::numeric_limits< Size >::max())
default constructor