aGrUM  0.16.0
paramEstimator.h
Go to the documentation of this file.
1 
28 #ifndef GUM_LEARNING_PARAM_ESTIMATOR_H
29 #define GUM_LEARNING_PARAM_ESTIMATOR_H
30 
31 #include <type_traits>
32 
33 #include <agrum/agrum.h>
38 
39 namespace gum {
40 
41  namespace learning {
42 
43 
49  template < template < typename > class ALLOC = std::allocator >
51  public:
53  using allocator_type = ALLOC< NodeId >;
54 
55  // ##########################################################################
57  // ##########################################################################
59 
61 
85  const DBRowGeneratorParser< ALLOC >& parser,
86  const Apriori< ALLOC >& external_apriori,
87  const Apriori< ALLOC >& score_internal__apriori,
88  const std::vector< std::pair< std::size_t, std::size_t >,
89  ALLOC< std::pair< std::size_t, std::size_t > > >&
90  ranges,
91  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
92  nodeId2columns =
93  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
94  const allocator_type& alloc = allocator_type());
95 
97 
115  const DBRowGeneratorParser< ALLOC >& parser,
116  const Apriori< ALLOC >& external_apriori,
117  const Apriori< ALLOC >& score_internal__apriori,
118  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
119  nodeId2columns =
120  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
121  const allocator_type& alloc = allocator_type());
122 
125 
128  const allocator_type& alloc);
129 
132 
135 
137  virtual ParamEstimator< ALLOC >* clone() const = 0;
138 
140  virtual ParamEstimator< ALLOC >*
141  clone(const allocator_type& alloc) const = 0;
142 
144  virtual ~ParamEstimator();
145 
147 
148 
149  // ##########################################################################
151  // ##########################################################################
153 
155  virtual void clear();
156 
158  virtual void setMaxNbThreads(std::size_t nb) const;
159 
161  virtual std::size_t nbThreads() const;
162 
172  virtual void setMinNbRowsPerThread(const std::size_t nb) const;
173 
175  virtual std::size_t minNbRowsPerThread() const;
176 
178 
184  template < template < typename > class XALLOC >
185  void setRanges(
186  const std::vector< std::pair< std::size_t, std::size_t >,
187  XALLOC< std::pair< std::size_t, std::size_t > > >&
188  new_ranges);
189 
191  void clearRanges();
192 
194  const std::vector< std::pair< std::size_t, std::size_t >,
195  ALLOC< std::pair< std::size_t, std::size_t > > >&
196  ranges() const;
197 
199  std::vector< double, ALLOC< double > > parameters(const NodeId target_node);
200 
202 
207  virtual std::vector< double, ALLOC< double > > parameters(
208  const NodeId target_node,
209  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes) = 0;
210 
212 
215  template < typename GUM_SCALAR >
216  void setParameters(
217  const NodeId target_node,
218  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
220 
222 
226  nodeId2Columns() const;
227 
229  const DatabaseTable< ALLOC >& database() const;
230 
232 
235  template < typename GUM_SCALAR >
236  void setBayesNet(const BayesNet< GUM_SCALAR >& new_bn);
237 
240 
242 
243  protected:
246 
250 
253 
255  const std::vector< NodeId, ALLOC< NodeId > > _empty_nodevect;
256 
257 
260 
263 
264  private:
265 #ifndef DOXYGEN_SHOULD_SKIP_THIS
266 
269  template < typename GUM_SCALAR >
270  void __checkParameters(
271  const NodeId target_node,
272  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
274 
275  // sets the CPT's parameters corresponding to a given Potential
276  // when the potential belongs to a BayesNet<GUM_SCALAR> when
277  // GUM_SCALAR is different from a double
278  template < typename GUM_SCALAR >
279  typename std::enable_if< !std::is_same< GUM_SCALAR, double >::value,
280  void >::type
281  __setParameters(
282  const NodeId target_node,
283  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
285 
286  // sets the CPT's parameters corresponding to a given Potential
287  // when the potential belongs to a BayesNet<GUM_SCALAR> when
288  // GUM_SCALAR is equal to double (the code is optimized for doubles)
289  template < typename GUM_SCALAR >
290  typename std::enable_if< std::is_same< GUM_SCALAR, double >::value,
291  void >::type
292  __setParameters(
293  const NodeId target_node,
294  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes,
296 
297 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
298  };
299 
300  } /* namespace learning */
301 
302 } /* namespace gum */
303 
306 
307 #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:60
Class representing a Bayesian Network.
Definition: BayesNet.h:78
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
the base class for all a priori
Definition: apriori.h:50
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
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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 ...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual ~ParamEstimator()
destructor
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
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:98
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