aGrUM  0.14.2
paramEstimator.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 GUM_LEARNING_PARAM_ESTIMATOR_H
26 #define GUM_LEARNING_PARAM_ESTIMATOR_H
27 
28 #include <type_traits>
29 
30 #include <agrum/agrum.h>
35 
36 namespace gum {
37 
38  namespace learning {
39 
40 
46  template < template < typename > class ALLOC = std::allocator >
48  public:
50  using allocator_type = ALLOC< NodeId >;
51 
52  // ##########################################################################
54  // ##########################################################################
56 
58 
82  const DBRowGeneratorParser< ALLOC >& parser,
83  const Apriori< ALLOC >& external_apriori,
84  const Apriori< ALLOC >& score_internal__apriori,
85  const std::vector< std::pair< std::size_t, std::size_t >,
86  ALLOC< std::pair< std::size_t, std::size_t > > >&
87  ranges,
88  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
89  nodeId2columns =
90  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
91  const allocator_type& alloc = allocator_type());
92 
94 
112  const DBRowGeneratorParser< ALLOC >& parser,
113  const Apriori< ALLOC >& external_apriori,
114  const Apriori< ALLOC >& score_internal__apriori,
115  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
116  nodeId2columns =
117  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
118  const allocator_type& alloc = allocator_type());
119 
122 
125  const allocator_type& alloc);
126 
129 
132 
134  virtual ParamEstimator< ALLOC >* clone() const = 0;
135 
137  virtual ParamEstimator< ALLOC >*
138  clone(const allocator_type& alloc) const = 0;
139 
141  virtual ~ParamEstimator();
142 
144 
145 
146  // ##########################################################################
148  // ##########################################################################
150 
152  virtual void clear();
153 
155  virtual void setMaxNbThreads(std::size_t nb) const;
156 
158  virtual std::size_t nbThreads() const;
159 
169  virtual void setMinNbRowsPerThread(const std::size_t nb) const;
170 
172  virtual std::size_t minNbRowsPerThread() const;
173 
175 
181  template < template < typename > class XALLOC >
182  void setRanges(
183  const std::vector< std::pair< std::size_t, std::size_t >,
184  XALLOC< std::pair< std::size_t, std::size_t > > >&
185  new_ranges);
186 
188  void clearRanges();
189 
191  const std::vector< std::pair< std::size_t, std::size_t >,
192  ALLOC< std::pair< std::size_t, std::size_t > > >&
193  ranges() const;
194 
196  std::vector< double, ALLOC< double > > parameters(const NodeId target_node);
197 
199 
204  virtual std::vector< double, ALLOC< double > > parameters(
205  const NodeId target_node,
206  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes) = 0;
207 
209 
212  template < typename GUM_SCALAR >
213  void setParameters(
214  const NodeId target_node,
215  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
217 
219 
223  nodeId2Columns() const;
224 
226  const DatabaseTable< ALLOC >& database() const;
227 
229 
232  template < typename GUM_SCALAR >
233  void setBayesNet(const BayesNet< GUM_SCALAR >& new_bn);
234 
237 
239 
240  protected:
243 
247 
250 
252  const std::vector< NodeId, ALLOC< NodeId > > _empty_nodevect;
253 
254 
257 
260 
261  private:
262 #ifndef DOXYGEN_SHOULD_SKIP_THIS
263 
266  template < typename GUM_SCALAR >
267  void __checkParameters(
268  const NodeId target_node,
269  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
271 
272  // sets the CPT's parameters corresponding to a given Potential
273  // when the potential belongs to a BayesNet<GUM_SCALAR> when
274  // GUM_SCALAR is different from a double
275  template < typename GUM_SCALAR >
276  typename std::enable_if< !std::is_same< GUM_SCALAR, double >::value,
277  void >::type
278  __setParameters(
279  const NodeId target_node,
280  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
282 
283  // sets the CPT's parameters corresponding to a given Potential
284  // when the potential belongs to a BayesNet<GUM_SCALAR> when
285  // GUM_SCALAR is equal to double (the code is optimized for doubles)
286  template < typename GUM_SCALAR >
287  typename std::enable_if< std::is_same< GUM_SCALAR, double >::value,
288  void >::type
289  __setParameters(
290  const NodeId target_node,
291  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
293 
294 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
295  };
296 
297  } /* namespace learning */
298 
299 } /* namespace gum */
300 
303 
304 #endif /* GUM_LEARNING_PARAM_ESTIMATOR_H */
ParamEstimator< ALLOC > & operator=(const ParamEstimator< ALLOC > &from)
copy operator
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
Class representing a Bayesian Network.
Definition: BayesNet.h:76
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > & nodeId2Columns() const
returns the mapping from ids to column positions in the database
void setParameters(const NodeId target_node, const std::vector< NodeId, ALLOC< NodeId > > &conditioning_nodes, Potential< GUM_SCALAR > &pot)
sets the CPT&#39;s parameters corresponding to a given Potential
The class representing a tabular database stored in RAM.
void clearRanges()
reset the ranges to the one range corresponding to the whole database
ParamEstimator(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const Apriori< ALLOC > &score_internal__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
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 parameter estimator
the base class for all a priori
the base class for all a priori
Definition: apriori.h:47
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
void setBayesNet(const BayesNet< GUM_SCALAR > &new_bn)
assign a new Bayes net to all the counter&#39;s generators depending on a BN
Apriori< ALLOC > * _score_internal_apriori
if a score was used for learning the structure of the PGM, this is the a priori internal to the score...
Apriori< ALLOC > * _external_apriori
an external a priori
The class that computes countings of observations from the database.
allocator_type getAllocator() const
returns the allocator used by the score
The class that computes countings of observations from the database.
virtual void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context ...
Header of the Potential class.
the base class for estimating parameters of CPTs
virtual ~ParamEstimator()
destructor
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
const DatabaseTable< ALLOC > & database() const
returns the database on which we perform the counts
std::vector< double, ALLOC< double > > parameters(const NodeId target_node)
returns the CPT&#39;s parameters corresponding to a given target node
The class representing a tabular database as used by learning tasks.
virtual void clear()
clears all the data structures from memory
virtual std::size_t nbThreads() const
returns the number of threads used to parse the database
virtual ParamEstimator< ALLOC > * clone() const =0
virtual copy constructor
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
virtual void setMaxNbThreads(std::size_t nb) const
changes the max number of threads used to parse the database
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
The base class for estimating parameters of CPTs.
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
Size NodeId
Type for node ids.
Definition: graphElements.h:97
RecordCounter< ALLOC > _counter
the record counter used to parse the database
const std::vector< NodeId, ALLOC< NodeId > > _empty_nodevect
an empty vector of nodes, used for empty conditioning