aGrUM  0.16.0
score.h
Go to the documentation of this file.
1 
28 #ifndef GUM_LEARNING_SCORE_H
29 #define GUM_LEARNING_SCORE_H
30 
31 #include <utility>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/core/math/math.h>
35 #include <agrum/core/OMPThreads.h>
36 
41 
42 namespace gum {
43 
44  namespace learning {
45 
51  template < template < typename > class ALLOC = std::allocator >
52  class Score {
53  public:
55  using allocator_type = ALLOC< NodeId >;
56 
57  // ##########################################################################
59  // ##########################################################################
61 
63 
85  const Apriori< ALLOC >& external_apriori,
86  const std::vector< std::pair< std::size_t, std::size_t >,
87  ALLOC< std::pair< std::size_t, std::size_t > > >&
88  ranges,
89  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
90  nodeId2columns =
91  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
92  const allocator_type& alloc = allocator_type());
93 
94 
96 
111  Score(const DBRowGeneratorParser< ALLOC >& parser,
112  const Apriori< ALLOC >& external_apriori,
113  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
114  nodeId2columns =
115  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
116  const allocator_type& alloc = allocator_type());
117 
119  virtual Score< ALLOC >* clone() const = 0;
120 
122  virtual Score< ALLOC >* clone(const allocator_type& alloc) const = 0;
123 
125  virtual ~Score();
126 
128 
129 
130  // ##########################################################################
132  // ##########################################################################
134 
136  virtual void setMaxNbThreads(std::size_t nb) const;
137 
139  virtual std::size_t nbThreads() const;
140 
150  virtual void setMinNbRowsPerThread(const std::size_t nb) const;
151 
153  virtual std::size_t minNbRowsPerThread() const;
154 
156 
162  template < template < typename > class XALLOC >
163  void setRanges(
164  const std::vector< std::pair< std::size_t, std::size_t >,
165  XALLOC< std::pair< std::size_t, std::size_t > > >&
166  new_ranges);
167 
169  void clearRanges();
170 
172  const std::vector< std::pair< std::size_t, std::size_t >,
173  ALLOC< std::pair< std::size_t, std::size_t > > >&
174  ranges() const;
175 
177  double score(const NodeId var);
178 
180 
183  double score(const NodeId var,
184  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids);
185 
187  void clear();
188 
190  void clearCache();
191 
193  void useCache(const bool on_off);
194 
196  bool isUsingCache() const;
197 
199 
203  nodeId2Columns() const;
204 
206  const DatabaseTable< ALLOC >& database() const;
207 
209 
216  virtual std::string isAprioriCompatible() const = 0;
217 
219 
229  virtual const Apriori< ALLOC >& internalApriori() const = 0;
230 
233 
235 
236 
237  protected:
239  const double _1log2{M_LOG2E};
240 
243 
246 
249 
251  bool _use_cache{true};
252 
254  const std::vector< NodeId, ALLOC< NodeId > > _empty_ids;
255 
256 
258  Score(const Score< ALLOC >& from);
259 
261  Score(const Score< ALLOC >& from, const allocator_type& alloc);
262 
264  Score(Score< ALLOC >&& from);
265 
267  Score(Score< ALLOC >&& from, const allocator_type& alloc);
268 
271 
274 
276 
279  virtual double _score(const IdSet< ALLOC >& idset) = 0;
280 
282 
286  std::vector< double, ALLOC< double > >
287  _marginalize(const NodeId X_id,
288  const std::vector< double, ALLOC< double > >& N_xyz) const;
289  };
290 
291  } /* namespace learning */
292 
293 } /* namespace gum */
294 
295 
296 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
297 extern template class gum::learning::Score<>;
298 #endif
299 
300 
303 
304 #endif /* GUM_LEARNING_SCORE_H */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:52
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const double _1log2
1 / log(2)
Definition: score.h:239
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:48
the base class for all a priori
Definition: apriori.h:50
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
Definition: score.h:55
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:248
const std::vector< NodeId, ALLOC< NodeId > > _empty_ids
an empty vector
Definition: score.h:254
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
#define M_LOG2E
Definition: math.h:36
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:1805
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void clearCache()
clears the current cache
a cache for caching scores and independence tests resultsCaching previously computed scores or the re...
Definition: scoringCache.h:60
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool _use_cache
a Boolean indicating whether we wish to use the cache
Definition: score.h:251
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:242
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
Definition: score.h:245
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:98