aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
pseudoCount_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 the base class for all the independence tests used for learning
24 
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #include <agrum/tools/stattests/pseudoCount.h>
28 #include <agrum/tools/stattests/idCondSet.h>
29 
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 
32 namespace gum {
33 
34  namespace learning {
35 
36  /// returns the allocator used by the independence test
37  template < template < typename > class ALLOC >
39  PseudoCount< ALLOC >::getAllocator() const {
40  return counter_.getAllocator();
41  }
42 
43  /// default constructor
44  template < template < typename > class ALLOC >
47  const Apriori< ALLOC >& apriori,
48  const std::vector< std::pair< std::size_t, std::size_t >,
49  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
50  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
52  const typename PseudoCount< ALLOC >::allocator_type& alloc) :
56  }
57 
58 
59  /// default constructor
60  template < template < typename > class ALLOC >
63  const Apriori< ALLOC >& apriori,
64  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
66  const typename PseudoCount< ALLOC >::allocator_type& alloc) :
70  }
71 
72 
73  /// copy constructor with a given allocator
74  template < template < typename > class ALLOC >
76  const PseudoCount< ALLOC >& from,
77  const typename PseudoCount< ALLOC >::allocator_type& alloc) :
81  }
82 
83 
84  /// copy constructor
85  template < template < typename > class ALLOC >
88 
89 
90  /// move constructor
91  template < template < typename > class ALLOC >
93  PseudoCount< ALLOC >&& from,
94  const typename PseudoCount< ALLOC >::allocator_type& alloc) :
97  from.apriori_ = nullptr;
99  }
100 
101 
102  /// move constructor
103  template < template < typename > class ALLOC >
106 
107 
108  /// destructor
109  template < template < typename > class ALLOC >
111  if (apriori_ != nullptr) {
112  ALLOC< Apriori< ALLOC > > allocator(this->getAllocator());
115  }
117  }
118 
119 
120  /// copy operator
121  template < template < typename > class ALLOC >
122  PseudoCount< ALLOC >&
123  PseudoCount< ALLOC >::operator=(const PseudoCount< ALLOC >& from) {
124  if (this != &from) {
127 
128  if (apriori_ != nullptr) {
129  ALLOC< Apriori< ALLOC > > allocator(this->getAllocator());
132  }
133 
136  }
137  return *this;
138  }
139 
140 
141  /// move operator
142  template < template < typename > class ALLOC >
143  PseudoCount< ALLOC >&
145  if (this != &from) {
147 
149  }
150  return *this;
151  }
152 
153 
154  /// changes the max number of threads used to parse the database
155  template < template < typename > class ALLOC >
156  INLINE void PseudoCount< ALLOC >::setMaxNbThreads(std::size_t nb) const {
158  }
159 
160 
161  /// returns the number of threads used to parse the database
162  template < template < typename > class ALLOC >
163  INLINE std::size_t PseudoCount< ALLOC >::nbThreads() const {
164  return counter_.nbThreads();
165  }
166 
167 
168  /** @brief changes the number min of rows a thread should process in a
169  * multithreading context */
170  template < template < typename > class ALLOC >
171  INLINE void
172  PseudoCount< ALLOC >::setMinNbRowsPerThread(const std::size_t nb) const {
174  }
175 
176 
177  /// returns the minimum of rows that each thread should process
178  template < template < typename > class ALLOC >
180  return counter_.minNbRowsPerThread();
181  }
182 
183 
184  /// sets new ranges to perform the countings used by the score
185  /** @param ranges a set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows
186  * indices. The countings are then performed only on the union of the
187  * rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing
188  * cross validation tasks, in which part of the database should be ignored.
189  * An empty set of ranges is equivalent to an interval [X,Y) ranging over
190  * the whole database. */
191  template < template < typename > class ALLOC >
192  template < template < typename > class XALLOC >
193  void PseudoCount< ALLOC >::setRanges(
194  const std::vector< std::pair< std::size_t, std::size_t >,
195  XALLOC< std::pair< std::size_t, std::size_t > > >&
196  new_ranges) {
197  std::vector< std::pair< std::size_t, std::size_t >,
198  ALLOC< std::pair< std::size_t, std::size_t > > >
199  old_ranges = ranges();
201  if (old_ranges != ranges()) clear();
202  }
203 
204 
205  /// reset the ranges to the one range corresponding to the whole database
206  template < template < typename > class ALLOC >
207  void PseudoCount< ALLOC >::clearRanges() {
208  std::vector< std::pair< std::size_t, std::size_t >,
209  ALLOC< std::pair< std::size_t, std::size_t > > >
210  old_ranges = ranges();
212  }
213 
214 
215  /// returns the current ranges
216  template < template < typename > class ALLOC >
217  INLINE const std::vector< std::pair< std::size_t, std::size_t >,
218  ALLOC< std::pair< std::size_t, std::size_t > > >&
219  PseudoCount< ALLOC >::ranges() const {
220  return counter_.ranges();
221  }
222 
223 
224  /// clears all the data structures from memory
225  template < template < typename > class ALLOC >
226  INLINE void PseudoCount< ALLOC >::clear() {
227  counter_.clear();
228  }
229 
230 
231  /// return the mapping between the columns of the database and the node ids
232  template < template < typename > class ALLOC >
233  INLINE const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
234  PseudoCount< ALLOC >::nodeId2Columns() const {
235  return counter_.nodeId2Columns();
236  }
237 
238 
239  /// return the database used by the score
240  template < template < typename > class ALLOC >
241  INLINE const DatabaseTable< ALLOC >& PseudoCount< ALLOC >::database() const {
242  return counter_.database();
243  }
244 
245 
246  /// returns a counting vector where variables are marginalized from N_xyz
247  /** @param node_2_marginalize indicates which node(s) shall be marginalized:
248  * - 0 means that X should be marginalized
249  * - 1 means that Y should be marginalized
250  * - 2 means that Z should be marginalized
251  */
252  template < template < typename > class ALLOC >
253  std::vector< double, ALLOC< double > > PseudoCount< ALLOC >::get(
254  const std::vector< NodeId, ALLOC< NodeId > >& ids) {
255  IdCondSet< ALLOC > idset(ids, false, true);
256  std::vector< double, ALLOC< double > > N_xyz(
257  this->counter_.counts(idset, true));
261  return N_xyz;
262  }
263 
264  } /* namespace learning */
265 
266 } /* namespace gum */
267 
268 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)