aGrUM  0.14.2
score_tpl.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 DOXYGEN_SHOULD_SKIP_THIS
26 
27 namespace gum {
28 
29  namespace learning {
30 
32  template < template < typename > class ALLOC >
33  INLINE typename Score< ALLOC >::allocator_type
35  return _counter.getAllocator();
36  }
37 
38 
40  template < template < typename > class ALLOC >
41  INLINE Score< ALLOC >::Score(
42  const DBRowGeneratorParser< ALLOC >& parser,
43  const Apriori< ALLOC >& apriori,
44  const std::vector< std::pair< std::size_t, std::size_t >,
45  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
46  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
47  nodeId2columns,
48  const typename Score< ALLOC >::allocator_type& alloc) :
49  _apriori(apriori.clone(alloc)),
50  _counter(parser, ranges, nodeId2columns, alloc), _cache(alloc) {
51  GUM_CONSTRUCTOR(Score);
52  }
53 
54 
56  template < template < typename > class ALLOC >
57  INLINE Score< ALLOC >::Score(
58  const DBRowGeneratorParser< ALLOC >& parser,
59  const Apriori< ALLOC >& apriori,
60  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
61  nodeId2columns,
62  const typename Score< ALLOC >::allocator_type& alloc) :
63  _apriori(apriori.clone(alloc)),
64  _counter(parser, nodeId2columns, alloc), _cache(alloc) {
65  GUM_CONSTRUCTOR(Score);
66  }
67 
68 
70  template < template < typename > class ALLOC >
71  INLINE Score< ALLOC >::Score(
72  const Score< ALLOC >& from,
73  const typename Score< ALLOC >::allocator_type& alloc) :
74  _apriori(from._apriori->clone(alloc)),
75  _counter(from._counter, alloc), _cache(from._cache, alloc),
76  _use_cache(from._use_cache) {
77  GUM_CONS_CPY(Score);
78  }
79 
80 
82  template < template < typename > class ALLOC >
83  INLINE Score< ALLOC >::Score(const Score< ALLOC >& from) :
84  Score(from, from.getAllocator()) {}
85 
86 
88  template < template < typename > class ALLOC >
89  INLINE Score< ALLOC >::Score(
90  Score< ALLOC >&& from,
91  const typename Score< ALLOC >::allocator_type& alloc) :
92  _apriori(from._apriori),
93  _counter(std::move(from._counter), alloc),
94  _cache(std::move(from._cache), alloc), _use_cache(from._use_cache) {
95  from._apriori = nullptr;
96  GUM_CONS_MOV(Score);
97  }
98 
99 
101  template < template < typename > class ALLOC >
102  INLINE Score< ALLOC >::Score(Score< ALLOC >&& from) :
103  Score(std::move(from), from.getAllocator()) {}
104 
105 
107  template < template < typename > class ALLOC >
108  INLINE Score< ALLOC >::~Score() {
109  if (_apriori != nullptr) {
110  ALLOC< Apriori< ALLOC > > allocator(this->getAllocator());
111  allocator.destroy(_apriori);
112  allocator.deallocate(_apriori, 1);
113  }
114  GUM_DESTRUCTOR(Score);
115  }
116 
117 
119  template < template < typename > class ALLOC >
120  Score< ALLOC >& Score< ALLOC >::operator=(const Score< ALLOC >& from) {
121  if (this != &from) {
122  Apriori< ALLOC >* new_apriori = from._apriori->clone();
123  RecordCounter< ALLOC > new_counter = from._counter;
124  ScoringCache< ALLOC > new_cache = from._cache;
125 
126  if (_apriori != nullptr) {
127  ALLOC< Apriori< ALLOC > > allocator(this->getAllocator());
128  allocator.destroy(_apriori);
129  allocator.deallocate(_apriori, 1);
130  }
131 
132  _apriori = new_apriori;
133  _counter = std::move(new_counter);
134  _cache = std::move(new_cache);
135 
136  _use_cache = from._use_cache;
137  }
138  return *this;
139  }
140 
141 
143  template < template < typename > class ALLOC >
144  Score< ALLOC >& Score< ALLOC >::operator=(Score< ALLOC >&& from) {
145  if (this != &from) {
146  std::swap(_apriori, from._apriori);
147 
148  _counter = std::move(from._counter);
149  _cache = std::move(from._cache);
150  _use_cache = from._use_cache;
151  }
152  return *this;
153  }
154 
155 
157  template < template < typename > class ALLOC >
158  INLINE void Score< ALLOC >::setMaxNbThreads(std::size_t nb) const {
159  _counter.setMaxNbThreads(nb);
160  }
161 
162 
164  template < template < typename > class ALLOC >
165  INLINE std::size_t Score< ALLOC >::nbThreads() const {
166  return _counter.nbThreads();
167  }
168 
169 
172  template < template < typename > class ALLOC >
173  INLINE void Score< ALLOC >::setMinNbRowsPerThread(const std::size_t nb) const {
174  _counter.setMinNbRowsPerThread(nb);
175  }
176 
177 
179  template < template < typename > class ALLOC >
180  INLINE std::size_t Score< ALLOC >::minNbRowsPerThread() const {
181  return _counter.minNbRowsPerThread();
182  }
183 
184 
186 
192  template < template < typename > class ALLOC >
193  template < template < typename > class XALLOC >
195  const std::vector< std::pair< std::size_t, std::size_t >,
196  XALLOC< std::pair< std::size_t, std::size_t > > >&
197  new_ranges) {
198  std::vector< std::pair< std::size_t, std::size_t >,
199  ALLOC< std::pair< std::size_t, std::size_t > > >
200  old_ranges = ranges();
201  _counter.setRanges(new_ranges);
202  if (old_ranges != ranges()) clear();
203  }
204 
205 
207  template < template < typename > class ALLOC >
209  std::vector< std::pair< std::size_t, std::size_t >,
210  ALLOC< std::pair< std::size_t, std::size_t > > >
211  old_ranges = ranges();
212  _counter.clearRanges();
213  if (old_ranges != ranges()) clear();
214  }
215 
216 
218  template < template < typename > class ALLOC >
219  INLINE const std::vector< std::pair< std::size_t, std::size_t >,
220  ALLOC< std::pair< std::size_t, std::size_t > > >&
221  Score< ALLOC >::ranges() const {
222  return _counter.ranges();
223  }
224 
225 
227  template < template < typename > class ALLOC >
228  INLINE double Score< ALLOC >::score(const NodeId var) {
229  IdSet< ALLOC > idset(var, _empty_ids, true, this->getAllocator());
230  if (_use_cache) {
231  try {
232  return _cache.score(idset);
233  } catch (NotFound&) {}
234  double the_score = _score(idset);
235  _cache.insert(std::move(idset), the_score);
236  return the_score;
237  } else {
238  return _score(std::move(idset));
239  }
240  }
241 
242 
244 
247  template < template < typename > class ALLOC >
248  INLINE double Score< ALLOC >::score(
249  const NodeId var, const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids) {
250  IdSet< ALLOC > idset(var, rhs_ids, false, this->getAllocator());
251  if (_use_cache) {
252  try {
253  return _cache.score(idset);
254  } catch (NotFound&) {}
255  double the_score = _score(idset);
256  _cache.insert(std::move(idset), the_score);
257  return the_score;
258  } else {
259  return _score(idset);
260  }
261  }
262 
263 
265  template < template < typename > class ALLOC >
266  INLINE void Score< ALLOC >::clear() {
267  _counter.clear();
268  _cache.clear();
269  }
270 
271 
273  template < template < typename > class ALLOC >
274  INLINE void Score< ALLOC >::clearCache() {
275  _cache.clear();
276  }
277 
278 
280  template < template < typename > class ALLOC >
281  INLINE void Score< ALLOC >::useCache(const bool on_off) {
282  _use_cache = on_off;
283  }
284 
285 
287  template < template < typename > class ALLOC >
288  INLINE bool Score< ALLOC >::isUsingCache() const {
289  return _use_cache;
290  }
291 
292 
294  template < template < typename > class ALLOC >
295  INLINE const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
297  return _counter.nodeId2Columns();
298  }
299 
300 
302  template < template < typename > class ALLOC >
303  INLINE const DatabaseTable< ALLOC >& Score< ALLOC >::database() const {
304  return _counter.database();
305  }
306 
308 
312  template < template < typename > class ALLOC >
313  std::vector< double, ALLOC< double > > Score< ALLOC >::_marginalize(
314  const NodeId X_id,
315  const std::vector< double, ALLOC< double > >& N_xyz) const {
316  // compute the domain sizes of the varible on the left hand side
317  // of the conditioning bar
318  const auto& nodeId2cols = this->_counter.nodeId2Columns();
319  const auto& database = this->_counter.database();
320  const std::size_t X_size = database.domainSize(
321  nodeId2cols.empty() ? X_id : nodeId2cols.second(X_id));
322 
323  // determine the size of the output vector
324  std::size_t out_size = N_xyz.size() / X_size;
325 
326  // allocate the output vector
327  std::vector< double, ALLOC< double > > res(out_size, 0.0);
328 
329  // fill the vector:
330  std::size_t xyz = std::size_t(0);
331  for (std::size_t z = std::size_t(0); z < out_size; ++z) {
332  for (std::size_t x = std::size_t(0); x < X_size; ++x, ++xyz) {
333  res[z] += N_xyz[xyz];
334  }
335  }
336 
337  return res;
338  }
339 
340 
341  } /* namespace learning */
342 
343 } /* namespace gum */
344 
345 #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: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
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: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
Size NodeId
Type for node ids.
Definition: graphElements.h:97