aGrUM  0.16.0
score_tpl.h
Go to the documentation of this file.
1 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32  namespace learning {
33 
35  template < template < typename > class ALLOC >
36  INLINE typename Score< ALLOC >::allocator_type
38  return _counter.getAllocator();
39  }
40 
41 
43  template < template < typename > class ALLOC >
44  INLINE Score< ALLOC >::Score(
45  const DBRowGeneratorParser< ALLOC >& parser,
46  const Apriori< ALLOC >& apriori,
47  const std::vector< std::pair< std::size_t, std::size_t >,
48  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
49  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
50  nodeId2columns,
51  const typename Score< ALLOC >::allocator_type& alloc) :
52  _apriori(apriori.clone(alloc)),
53  _counter(parser, ranges, nodeId2columns, alloc), _cache(alloc) {
54  GUM_CONSTRUCTOR(Score);
55  }
56 
57 
59  template < template < typename > class ALLOC >
60  INLINE Score< ALLOC >::Score(
61  const DBRowGeneratorParser< ALLOC >& parser,
62  const Apriori< ALLOC >& apriori,
63  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
64  nodeId2columns,
65  const typename Score< ALLOC >::allocator_type& alloc) :
66  _apriori(apriori.clone(alloc)),
67  _counter(parser, nodeId2columns, alloc), _cache(alloc) {
68  GUM_CONSTRUCTOR(Score);
69  }
70 
71 
73  template < template < typename > class ALLOC >
74  INLINE Score< ALLOC >::Score(
75  const Score< ALLOC >& from,
76  const typename Score< ALLOC >::allocator_type& alloc) :
77  _apriori(from._apriori->clone(alloc)),
78  _counter(from._counter, alloc), _cache(from._cache, alloc),
79  _use_cache(from._use_cache) {
80  GUM_CONS_CPY(Score);
81  }
82 
83 
85  template < template < typename > class ALLOC >
86  INLINE Score< ALLOC >::Score(const Score< ALLOC >& from) :
87  Score(from, from.getAllocator()) {}
88 
89 
91  template < template < typename > class ALLOC >
92  INLINE Score< ALLOC >::Score(
93  Score< ALLOC >&& from,
94  const typename Score< ALLOC >::allocator_type& alloc) :
95  _apriori(from._apriori),
96  _counter(std::move(from._counter), alloc),
97  _cache(std::move(from._cache), alloc), _use_cache(from._use_cache) {
98  from._apriori = nullptr;
99  GUM_CONS_MOV(Score);
100  }
101 
102 
104  template < template < typename > class ALLOC >
105  INLINE Score< ALLOC >::Score(Score< ALLOC >&& from) :
106  Score(std::move(from), from.getAllocator()) {}
107 
108 
110  template < template < typename > class ALLOC >
111  INLINE Score< ALLOC >::~Score() {
112  if (_apriori != nullptr) {
113  ALLOC< Apriori< ALLOC > > allocator(this->getAllocator());
114  allocator.destroy(_apriori);
115  allocator.deallocate(_apriori, 1);
116  }
117  GUM_DESTRUCTOR(Score);
118  }
119 
120 
122  template < template < typename > class ALLOC >
123  Score< ALLOC >& Score< ALLOC >::operator=(const Score< ALLOC >& from) {
124  if (this != &from) {
125  Apriori< ALLOC >* new_apriori = from._apriori->clone();
126  RecordCounter< ALLOC > new_counter = from._counter;
127  ScoringCache< ALLOC > new_cache = from._cache;
128 
129  if (_apriori != nullptr) {
130  ALLOC< Apriori< ALLOC > > allocator(this->getAllocator());
131  allocator.destroy(_apriori);
132  allocator.deallocate(_apriori, 1);
133  }
134 
135  _apriori = new_apriori;
136  _counter = std::move(new_counter);
137  _cache = std::move(new_cache);
138 
139  _use_cache = from._use_cache;
140  }
141  return *this;
142  }
143 
144 
146  template < template < typename > class ALLOC >
147  Score< ALLOC >& Score< ALLOC >::operator=(Score< ALLOC >&& from) {
148  if (this != &from) {
149  std::swap(_apriori, from._apriori);
150 
151  _counter = std::move(from._counter);
152  _cache = std::move(from._cache);
153  _use_cache = from._use_cache;
154  }
155  return *this;
156  }
157 
158 
160  template < template < typename > class ALLOC >
161  INLINE void Score< ALLOC >::setMaxNbThreads(std::size_t nb) const {
162  _counter.setMaxNbThreads(nb);
163  }
164 
165 
167  template < template < typename > class ALLOC >
168  INLINE std::size_t Score< ALLOC >::nbThreads() const {
169  return _counter.nbThreads();
170  }
171 
172 
175  template < template < typename > class ALLOC >
176  INLINE void Score< ALLOC >::setMinNbRowsPerThread(const std::size_t nb) const {
177  _counter.setMinNbRowsPerThread(nb);
178  }
179 
180 
182  template < template < typename > class ALLOC >
183  INLINE std::size_t Score< ALLOC >::minNbRowsPerThread() const {
184  return _counter.minNbRowsPerThread();
185  }
186 
187 
189 
195  template < template < typename > class ALLOC >
196  template < template < typename > class XALLOC >
198  const std::vector< std::pair< std::size_t, std::size_t >,
199  XALLOC< std::pair< std::size_t, std::size_t > > >&
200  new_ranges) {
201  std::vector< std::pair< std::size_t, std::size_t >,
202  ALLOC< std::pair< std::size_t, std::size_t > > >
203  old_ranges = ranges();
204  _counter.setRanges(new_ranges);
205  if (old_ranges != ranges()) clear();
206  }
207 
208 
210  template < template < typename > class ALLOC >
212  std::vector< std::pair< std::size_t, std::size_t >,
213  ALLOC< std::pair< std::size_t, std::size_t > > >
214  old_ranges = ranges();
215  _counter.clearRanges();
216  if (old_ranges != ranges()) clear();
217  }
218 
219 
221  template < template < typename > class ALLOC >
222  INLINE const std::vector< std::pair< std::size_t, std::size_t >,
223  ALLOC< std::pair< std::size_t, std::size_t > > >&
224  Score< ALLOC >::ranges() const {
225  return _counter.ranges();
226  }
227 
228 
230  template < template < typename > class ALLOC >
231  INLINE double Score< ALLOC >::score(const NodeId var) {
232  IdSet< ALLOC > idset(var, _empty_ids, true, this->getAllocator());
233  if (_use_cache) {
234  try {
235  return _cache.score(idset);
236  } catch (NotFound&) {}
237  double the_score = _score(idset);
238  _cache.insert(std::move(idset), the_score);
239  return the_score;
240  } else {
241  return _score(std::move(idset));
242  }
243  }
244 
245 
247 
250  template < template < typename > class ALLOC >
251  INLINE double Score< ALLOC >::score(
252  const NodeId var, const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids) {
253  IdSet< ALLOC > idset(var, rhs_ids, false, this->getAllocator());
254  if (_use_cache) {
255  try {
256  return _cache.score(idset);
257  } catch (NotFound&) {}
258  double the_score = _score(idset);
259  _cache.insert(std::move(idset), the_score);
260  return the_score;
261  } else {
262  return _score(idset);
263  }
264  }
265 
266 
268  template < template < typename > class ALLOC >
269  INLINE void Score< ALLOC >::clear() {
270  _counter.clear();
271  _cache.clear();
272  }
273 
274 
276  template < template < typename > class ALLOC >
277  INLINE void Score< ALLOC >::clearCache() {
278  _cache.clear();
279  }
280 
281 
283  template < template < typename > class ALLOC >
284  INLINE void Score< ALLOC >::useCache(const bool on_off) {
285  _use_cache = on_off;
286  }
287 
288 
290  template < template < typename > class ALLOC >
291  INLINE bool Score< ALLOC >::isUsingCache() const {
292  return _use_cache;
293  }
294 
295 
297  template < template < typename > class ALLOC >
298  INLINE const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
300  return _counter.nodeId2Columns();
301  }
302 
303 
305  template < template < typename > class ALLOC >
306  INLINE const DatabaseTable< ALLOC >& Score< ALLOC >::database() const {
307  return _counter.database();
308  }
309 
311 
315  template < template < typename > class ALLOC >
316  std::vector< double, ALLOC< double > > Score< ALLOC >::_marginalize(
317  const NodeId X_id,
318  const std::vector< double, ALLOC< double > >& N_xyz) const {
319  // compute the domain sizes of the varible on the left hand side
320  // of the conditioning bar
321  const auto& nodeId2cols = this->_counter.nodeId2Columns();
322  const auto& database = this->_counter.database();
323  const std::size_t X_size = database.domainSize(
324  nodeId2cols.empty() ? X_id : nodeId2cols.second(X_id));
325 
326  // determine the size of the output vector
327  std::size_t out_size = N_xyz.size() / X_size;
328 
329  // allocate the output vector
330  std::vector< double, ALLOC< double > > res(out_size, 0.0);
331 
332  // fill the vector:
333  std::size_t xyz = std::size_t(0);
334  for (std::size_t z = std::size_t(0); z < out_size; ++z) {
335  for (std::size_t x = std::size_t(0); x < X_size; ++x, ++xyz) {
336  res[z] += N_xyz[xyz];
337  }
338  }
339 
340  return res;
341  }
342 
343 
344  } /* namespace learning */
345 
346 } /* namespace gum */
347 
348 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
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
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
STL namespace.
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
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
virtual std::size_t nbThreads() const
returns the number of threads used to parse the database
virtual ~Score()
destructor
Score< ALLOC > & operator=(const Score< ALLOC > &from)
copy operator
void clear()
clears all the data structures from memory, including the cache
virtual double _score(const IdSet< ALLOC > &idset)=0
returns the score for a given IdSet
bool isUsingCache() const
indicates whether the score uses a cache
void clearCache()
clears the current cache
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
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
Size NodeId
Type for node ids.
Definition: graphElements.h:98