aGrUM  0.13.2
counter.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  ***************************************************************************/
53 #ifndef GUM_LEARNING_COUNTER_H
54 #define GUM_LEARNING_COUNTER_H
55 
56 #include <agrum/agrum.h>
57 #include <agrum/core/math/math.h>
59 #include <vector>
60 
61 namespace gum {
62 
63  namespace learning {
64 
65  /* =========================================================================
66  */
67  /* === BASIC SCORE CLASS ===
68  */
69  /* =========================================================================
70  */
106  template < typename IdSetAlloc = std::allocator< Idx >,
107  typename CountAlloc = std::allocator< double > >
108  class Counter {
109  public:
110  // ##########################################################################
112  // ##########################################################################
114 
116 
122  template < typename RowFilter >
123  Counter(const RowFilter& filter,
124  const std::vector< Size >& var_modalities,
125  Size min_range = 0,
126  Size max_range = std::numeric_limits< Size >::max());
129 
132 
134  virtual ~Counter();
135 
137 
138  // ##########################################################################
140  // ##########################################################################
142 
145 
147 
160  Idx addNodeSet(Idx var);
161 
163 
164  // ##########################################################################
166  // ##########################################################################
168 
170 
183  Idx addNodeSet(Idx var1, Idx var2);
184 
186 
198  Idx addNodeSet(const std::pair< Idx, Idx >& vars);
199 
201 
215  Idx addNodeSet(Idx var, const std::vector< Idx >& conditioning_ids);
216 
218 
232  Idx addNodeSet(Idx var, std::vector< Idx >&& conditioning_ids);
233 
235 
253  Idx
254  addNodeSet(Idx var1, Idx var2, const std::vector< Idx >& conditioning_ids);
255 
257 
275  Idx addNodeSet(Idx var1, Idx var2, std::vector< Idx >&& conditioning_ids);
276 
278 
293  Idx addNodeSet(const std::pair< Idx, Idx >& vars,
294  const std::vector< Idx >& conditioning_ids);
295 
297 
312  Idx addNodeSet(const std::pair< Idx, Idx >& vars,
313  std::vector< Idx >&& conditioning_ids);
314 
316 
317  // ##########################################################################
319  // ##########################################################################
321 
323  void clear();
324 
326  const std::vector< Size >& modalities() const noexcept;
327 
329  void setMaxNbThreads(Size nb) noexcept;
330 
332 
336  void setRange(Size min_range, Size max_range);
337 
339 
340  protected:
342  const double _1log2{M_LOG2E};
343 
345  const std::vector< Size >& _modalities;
346 
349  bool _counts_computed{false};
350 
353 
355  std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx >* >
357 
360  std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx >* >
362 
363 
365  void _count();
366 
368 
380  const std::vector< double, CountAlloc >& _getAllCounts(Idx index);
381 
383 
385  const std::vector< double, CountAlloc >& _getConditioningCounts(Idx index);
386 
388 
392  std::vector< std::vector< double, CountAlloc > >& _getCounts() noexcept;
393 
395 
397  const std::vector< Idx, IdSetAlloc >& _getAllNodes(Idx index) const noexcept;
398 
401 
403  const std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx >* >&
404  _getAllNodes() const noexcept;
405 
407  const std::vector< Idx, IdSetAlloc >* _getConditioningNodes(Idx index) const
408  noexcept;
409 
411  const std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx >* >&
412  _getConditioningNodes() const noexcept;
413 
414  // ##########################################################################
415  // ##########################################################################
416 
418  Counter< IdSetAlloc, CountAlloc >&
419  operator=(const Counter< IdSetAlloc, CountAlloc >&) = delete;
420  };
421 
422  } /* namespace learning */
423 
424 } /* namespace gum */
425 
426 
427 extern template class gum::learning::Counter<>;
428 
429 
431 #include <agrum/learning/scores_and_tests/counter_tpl.h>
432 
433 #endif /* GUM_LEARNING_COUNTER_H */
const std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx > * > & _getAllNodes() const noexcept
returns all the sets of target + cond nodes, and their counting indices
Useful macros for maths.
RecordCounter< IdSetAlloc, CountAlloc > _record_counter
the recordCounter that will parse the database
Definition: counter.h:352
void setRange(Size min_range, Size max_range)
sets the range of records taken into account by the counter
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
const double _1log2
1 / log(2)
Definition: counter.h:342
const std::vector< double, CountAlloc > & _getAllCounts(Idx index)
returns the counting vector for a given (conditioned) target set
STL namespace.
std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx > * > _target_nodesets
the target id sets to count and their indices in the record counter
Definition: counter.h:356
const std::vector< Size > & modalities() const noexcept
returns the modalities of the variables
Idx addNodeSet(Idx var)
add a new single variable to be counted
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class that computes countings of observations from the database.
The class that computes countings of observations from the database.
const std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx > * > & _getConditioningNodes() const noexcept
returns all the sets of conditioning nodes
void setMaxNbThreads(Size nb) noexcept
sets the maximum number of threads used to perform countings
const std::vector< double, CountAlloc > & _getConditioningCounts(Idx index)
returns the counting vector for a conditioning set
#define M_LOG2E
Definition: math.h:33
const std::vector< Size > & _modalities
the modalities of the variables
Definition: counter.h:345
The counting class for all the scores used for learning (BIC, BDeu, etc) as well as for all the indep...
Definition: counter.h:108
std::vector< std::pair< std::vector< Idx, IdSetAlloc >, Idx > * > _conditioning_nodesets
the conditioning id sets to count and their indices in the record counter
Definition: counter.h:361
virtual ~Counter()
destructor
Idx addEmptyNodeSet()
adds an empty set of variables to count
void _count()
perform the computation of the countings
std::vector< std::vector< double, CountAlloc > > & _getCounts() noexcept
returns all the countings performed (both targets and conditioned)
bool _counts_computed
indicates whether we have already computed the countings of the nodesets
Definition: counter.h:349
Counter(const RowFilter &filter, const std::vector< Size > &var_modalities, Size min_range=0, Size max_range=std::numeric_limits< Size >::max())
default constructor
unsigned long Idx
Type for indexes.
Definition: types.h:43
void clear()
clears all the data structures from memory