aGrUM  0.14.1
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  ***************************************************************************/
25 #ifndef GUM_LEARNING_SCORE_H
26 #define GUM_LEARNING_SCORE_H
27 
28 #include <utility>
29 
30 #include <agrum/agrum.h>
31 #include <agrum/core/math/math.h>
32 #include <agrum/core/OMPThreads.h>
33 
38 
39 namespace gum {
40 
41  namespace learning {
42 
48  template < template < typename > class ALLOC = std::allocator >
49  class Score {
50  public:
52  using allocator_type = ALLOC< NodeId >;
53 
54  // ##########################################################################
56  // ##########################################################################
58 
60 
82  const Apriori< ALLOC >& external_apriori,
83  const std::vector< std::pair< std::size_t, std::size_t >,
84  ALLOC< std::pair< std::size_t, std::size_t > > >&
85  ranges,
86  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
87  nodeId2columns =
88  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
89  const allocator_type& alloc = allocator_type());
90 
91 
93 
108  Score(const DBRowGeneratorParser< ALLOC >& parser,
109  const Apriori< ALLOC >& external_apriori,
110  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
111  nodeId2columns =
112  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
113  const allocator_type& alloc = allocator_type());
114 
116  virtual Score< ALLOC >* clone() const = 0;
117 
119  virtual Score< ALLOC >* clone(const allocator_type& alloc) const = 0;
120 
122  virtual ~Score();
123 
125 
126 
127  // ##########################################################################
129  // ##########################################################################
131 
133  virtual void setMaxNbThreads(std::size_t nb) const;
134 
136  virtual std::size_t nbThreads() const;
137 
147  virtual void setMinNbRowsPerThread(const std::size_t nb) const;
148 
150  virtual std::size_t minNbRowsPerThread() const;
151 
153 
159  template < template < typename > class XALLOC >
160  void setRanges(
161  const std::vector< std::pair< std::size_t, std::size_t >,
162  XALLOC< std::pair< std::size_t, std::size_t > > >&
163  new_ranges);
164 
166  void clearRanges();
167 
169  const std::vector< std::pair< std::size_t, std::size_t >,
170  ALLOC< std::pair< std::size_t, std::size_t > > >&
171  ranges() const;
172 
174  double score(const NodeId var);
175 
177 
180  double score(const NodeId var,
181  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids);
182 
184  void clear();
185 
187  void clearCache();
188 
190  void useCache(const bool on_off);
191 
193  bool isUsingCache() const;
194 
196 
200  nodeId2Columns() const;
201 
203  const DatabaseTable< ALLOC >& database() const;
204 
206 
213  virtual std::string isAprioriCompatible() const = 0;
214 
216 
226  virtual const Apriori< ALLOC >& internalApriori() const = 0;
227 
230 
232 
233 
234  protected:
236  const double _1log2{M_LOG2E};
237 
240 
243 
246 
248  bool _use_cache{true};
249 
251  const std::vector< NodeId, ALLOC< NodeId > > _empty_ids;
252 
253 
255  Score(const Score< ALLOC >& from);
256 
258  Score(const Score< ALLOC >& from, const allocator_type& alloc);
259 
261  Score(Score< ALLOC >&& from);
262 
264  Score(Score< ALLOC >&& from, const allocator_type& alloc);
265 
268 
271 
273 
276  virtual double _score(const IdSet< ALLOC >& idset) = 0;
277 
279 
283  std::vector< double, ALLOC< double > >
284  _marginalize(const NodeId X_id,
285  const std::vector< double, ALLOC< double > >& N_xyz) const;
286  };
287 
288  } /* namespace learning */
289 
290 } /* namespace gum */
291 
292 
293 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
294 extern template class gum::learning::Score<>;
295 #endif
296 
297 
300 
301 #endif /* GUM_LEARNING_SCORE_H */
Useful macros for maths.
const DatabaseTable< ALLOC > & database() const
return the database used by the score
double score(const NodeId var)
returns the score of a single node
void setRanges(const std::vector< std::pair< std::size_t, std::size_t >, XALLOC< std::pair< std::size_t, std::size_t > > > &new_ranges)
sets new ranges to perform the countings used by the score
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > & nodeId2Columns() const
return the mapping between the columns of the database and the node ids
The base class for all the scores used for learning (BIC, BDeu, etc)
Definition: score.h:49
the classes to account for structure changes in a graph
Score(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
virtual void setMaxNbThreads(std::size_t nb) const
changes the max number of threads used to parse the database
the base class for all a priori
const double _1log2
1 / log(2)
Definition: score.h:236
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set...
Definition: idSet.h:45
the base class for all a priori
Definition: apriori.h:47
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
Definition: score.h:52
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > & ranges() const
returns the current ranges
ScoringCache< ALLOC > _cache
the scoring cache
Definition: score.h:245
const std::vector< NodeId, ALLOC< NodeId > > _empty_ids
an empty vector
Definition: score.h:251
std::vector< double, ALLOC< double > > _marginalize(const NodeId X_id, const std::vector< double, ALLOC< double > > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
The class that computes countings of observations from the database.
virtual std::size_t nbThreads() const
returns the number of threads used to parse the database
The class that computes countings of observations from the database.
a cache for caching scores and independence tests results
#define M_LOG2E
Definition: math.h:33
virtual ~Score()
destructor
Score< ALLOC > & operator=(const Score< ALLOC > &from)
copy operator
void clear()
clears all the data structures from memory, including the cache
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
virtual const Apriori< ALLOC > & internalApriori() const =0
returns the internal apriori of the score
virtual double _score(const IdSet< ALLOC > &idset)=0
returns the score for a given IdSet
The class representing a tabular database as used by learning tasks.
bool isUsingCache() const
indicates whether the score uses a cache
Wrappers for OpenMP.
void clearCache()
clears the current cache
a cache for caching scores and independence tests resultsCaching previously computed scores or the re...
Definition: scoringCache.h:57
virtual std::string isAprioriCompatible() const =0
indicates whether the apriori is compatible (meaningful) with the score
void useCache(const bool on_off)
turn on/off the use of a cache of the previously computed score
void clearRanges()
reset the ranges to the one range corresponding to the whole database
allocator_type getAllocator() const
returns the allocator used by the score
the base class for all the scores used for learning (BIC, BDeu, etc)
bool _use_cache
a Boolean indicating whether we wish to use the cache
Definition: score.h:248
virtual void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context ...
Apriori< ALLOC > * _apriori
the expert knowledge a priori we add to the score
Definition: score.h:239
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
Definition: score.h:242
virtual Score< ALLOC > * clone() const =0
virtual copy constructor
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
Size NodeId
Type for node ids.
Definition: graphElements.h:97