aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scoringCache.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief a cache for caching scores and independence tests results
24  *
25  * Caching previously computed scores or the results of conditional independence
26  * tests is very important for learning algorithms because computing a score or
27  * an independence test requires parsing the database and this is the most
28  * time consuming operation in learning. This class provides an efficient cache
29  * that can significantly alleviate the learning computational burden.
30  *
31  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
32  */
33 #ifndef GUM_LEARNING_SCORING_CACHE_H
34 #define GUM_LEARNING_SCORING_CACHE_H
35 
36 #include <utility>
37 
38 #include <agrum/agrum.h>
39 #include <agrum/tools/stattests/idCondSet.h>
40 
41 namespace gum {
42 
43  namespace learning {
44 
45 
46  /** @class ScoringCache
47  * @brief a cache for caching scores and independence tests results
48  * @headerfile scoringCache.h <agrum/tools/database/scoringCache.h>
49  * @ingroup learning_scores
50  *
51  * Caching previously computed scores or the results of conditional
52  * independence tests is very important for learning algorithms because
53  * computing a score or an independence test requires parsing the database
54  * and this is the most time consuming operation in learning. This class
55  * provides an efficient cache that can significantly alleviate the
56  * learning computational burden.
57  */
58  template < template < typename > class ALLOC = std::allocator >
59  class ScoringCache: private ALLOC< NodeId > {
60  public:
61  /// type for the allocators passed in arguments of methods
63 
64  // ##########################################################################
65  /// @name Constructors / Destructors
66  // ##########################################################################
67  /// @{
68 
69  /// default constructor
70  ScoringCache(const allocator_type& alloc = allocator_type());
71 
72  /// copy constructor
73  ScoringCache(const ScoringCache< ALLOC >& from);
74 
75  /// copy constructor with a given allocator
76  ScoringCache(const ScoringCache< ALLOC >& from, const allocator_type& alloc);
77 
78  /// move constructor
79  ScoringCache(ScoringCache< ALLOC >&& from);
80 
81  /// move constructor with a given allocator
82  ScoringCache(ScoringCache< ALLOC >&& from, const allocator_type& alloc);
83 
84  /// virtual copy constructor
85  virtual ScoringCache< ALLOC >* clone() const;
86 
87  /// virtual copy constructor with a given allocator
88  virtual ScoringCache< ALLOC >* clone(const allocator_type& alloc) const;
89 
90  /// destructor
91  virtual ~ScoringCache();
92 
93  /// @}
94 
95 
96  // ##########################################################################
97  /// @name Operators
98  // ##########################################################################
99  /// @{
100 
101  /// copy operator
103 
104  /// move operator
106 
107  /// @}
108 
109 
110  // ##########################################################################
111  /// @name Accessors / Modifiers
112  // ##########################################################################
113  /// @{
114 
115  /// insert a new score into the cache
116  /** @param idset the IdCondSet storing the sets of variables
117  * @param score the score assigned to the IdCondSet
118  * @throws DuplicateElement exception is raised if a score for the same
119  * variables already exists */
120  void insert(const IdCondSet< ALLOC >& idset, double score);
121 
122  /// insert a new score into the cache
123  /** @param idset the IdCondSet storing the sets of variables
124  * @param score the score assigned to the IdCondSet
125  * @throws DuplicateElement exception is raised if a score for the same
126  * variables already exists */
127  void insert(IdCondSet< ALLOC >&& idset, double score);
128 
129  /// removes a score (if it exists)
130  /** @param idset the IdCondSet storing the sets of variables
131  * @warning If the score does not exist, nothing is done. In particular,
132  * no exception is raised */
133  void erase(const IdCondSet< ALLOC >& idset);
134 
135  /// indicates whether a given score exists
136  /** @param idset the IdCondSet storing the sets of variables */
137  bool exists(const IdCondSet< ALLOC >& idset);
138 
139  /// returns a given score
140  /** @param idset the IdCondSet storing the sets of variables
141  * @throws NotFound is raised if the score is not cached */
142  double score(const IdCondSet< ALLOC >& idset);
143 
144  /// removes all the stored scores
145  void clear();
146 
147  /// returns the number of scores saved in the cache
148  std::size_t size() const;
149 
150  /// returns the allocator used by the translator
152 
153  /// @}
154 
155 
156 #ifndef DOXYGEN_SHOULD_SKIP_THIS
157 
158  private:
159  /// the scores stored into the cache
160  HashTable< IdCondSet< ALLOC >, double, ALLOC< std::pair< IdCondSet< ALLOC >, double > > >
161  _scores_;
162 
163 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
164  };
165 
166  } /* namespace learning */
167 
168 } /* namespace gum */
169 
170 
171 // always include the template implementation
172 #include <agrum/BN/learning/scores_and_tests/scoringCache_tpl.h>
173 
174 
175 #endif /* GUM_LEARNING_SCORING_CACHE_H */
virtual ~ScoringCache()
destructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual ScoringCache< ALLOC > * clone(const allocator_type &alloc) const
virtual copy constructor with a given allocator
ScoringCache< ALLOC > & operator=(ScoringCache< ALLOC > &&from)
move operator
ScoringCache(ScoringCache< ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
void erase(const IdCondSet< ALLOC > &idset)
removes a score (if it exists)
std::size_t size() const
returns the number of scores saved in the cache
ScoringCache(const ScoringCache< ALLOC > &from)
copy constructor
double score(const IdCondSet< ALLOC > &idset)
returns a given score
void clear()
removes all the stored scores
bool exists(const IdCondSet< ALLOC > &idset)
indicates whether a given score exists
ScoringCache< ALLOC > & operator=(const ScoringCache< ALLOC > &from)
copy operator
ScoringCache(ScoringCache< ALLOC > &&from)
move constructor
virtual ScoringCache< ALLOC > * clone() const
virtual copy constructor
a cache for caching scores and independence tests resultsCaching previously computed scores or the re...
Definition: scoringCache.h:59
allocator_type getAllocator() const
returns the allocator used by the translator
void insert(IdCondSet< ALLOC > &&idset, double score)
insert a new score into the cache
void insert(const IdCondSet< ALLOC > &idset, double score)
insert a new score into the cache
ScoringCache(const ScoringCache< ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)