29 #ifndef DOXYGEN_SHOULD_SKIP_THIS 36 template <
template <
typename >
class ALLOC >
39 return _counter.getAllocator();
44 template <
template <
typename >
class ALLOC >
46 const DBRowGeneratorParser< ALLOC >& parser,
47 const Apriori< ALLOC >& external_apriori,
48 const Apriori< ALLOC >& score_internal_apriori,
49 const std::vector< std::pair< std::size_t, std::size_t >,
50 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
51 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
54 _counter(parser, ranges, nodeId2columns, alloc) {
60 ALLOC< Apriori< ALLOC > > allocator(alloc);
71 template <
template <
typename >
class ALLOC >
73 const DBRowGeneratorParser< ALLOC >& parser,
74 const Apriori< ALLOC >& external_apriori,
75 const Apriori< ALLOC >& score_internal_apriori,
76 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
79 _counter(parser, nodeId2columns, alloc) {
85 ALLOC< Apriori< ALLOC > > allocator(alloc);
96 template <
template <
typename >
class ALLOC >
98 const ParamEstimator< ALLOC >& from,
108 template <
template <
typename >
class ALLOC >
110 const ParamEstimator< ALLOC >& from) :
115 template <
template <
typename >
class ALLOC >
117 ParamEstimator< ALLOC >&& from,
122 from._external_apriori =
nullptr;
123 from._score_internal_apriori =
nullptr;
129 template <
template <
typename >
class ALLOC >
136 template <
template <
typename >
class ALLOC >
138 ALLOC< Apriori< ALLOC > > allocator(this->
getAllocator());
154 template <
template <
typename >
class ALLOC >
156 operator=(
const ParamEstimator< ALLOC >& from) {
158 ALLOC< Apriori< ALLOC > > allocator(this->
getAllocator());
172 from._score_internal_apriori->clone(this->
getAllocator());
181 template <
template <
typename >
class ALLOC >
183 operator=(ParamEstimator< ALLOC >&& from) {
187 _counter = std::move(from._counter);
188 from._external_apriori =
nullptr;
189 from._score_internal_apriori =
nullptr;
196 template <
template <
typename >
class ALLOC >
203 template <
template <
typename >
class ALLOC >
210 template <
template <
typename >
class ALLOC >
218 template <
template <
typename >
class ALLOC >
226 template <
template <
typename >
class ALLOC >
228 return _counter.minNbRowsPerThread();
239 template <
template <
typename >
class ALLOC >
240 template <
template <
typename >
class XALLOC >
242 const std::vector< std::pair< std::size_t, std::size_t >,
243 XALLOC< std::pair< std::size_t, std::size_t > > >&
245 std::vector< std::pair< std::size_t, std::size_t >,
246 ALLOC< std::pair< std::size_t, std::size_t > > >
254 template <
template <
typename >
class ALLOC >
256 std::vector< std::pair< std::size_t, std::size_t >,
257 ALLOC< std::pair< std::size_t, std::size_t > > >
265 template <
template <
typename >
class ALLOC >
266 INLINE
const std::vector< std::pair< std::size_t, std::size_t >,
267 ALLOC< std::pair< std::size_t, std::size_t > > >&
274 template <
template <
typename >
class ALLOC >
275 INLINE std::vector< double, ALLOC< double > >
282 template <
template <
typename >
class ALLOC >
283 template <
typename GUM_SCALAR >
284 void ParamEstimator< ALLOC >::__checkParameters(
286 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
287 Potential< GUM_SCALAR >& pot) {
289 const Sequence< const DiscreteVariable* >& vars = pot.variablesSequence();
290 if (vars.size() == 0) {
291 GUM_ERROR(SizeError,
"the potential contains no variable");
295 const auto& node2cols =
_counter.nodeId2Columns();
296 if (node2cols.empty()) {
297 if (
database.domainSize(target_node) != vars[0]->domainSize()) {
300 << vars[0]->name() <<
"of the potential to be filled " 301 <<
"has a domain size of " << vars[0]->domainSize()
302 <<
", which is different from that of node " << target_node
303 <<
" which is equal to " 304 <<
database.domainSize(target_node));
306 for (std::size_t i = 1; i < vars.size(); ++i) {
307 if (
database.domainSize(conditioning_nodes[i - 1])
308 != vars[i]->domainSize()) {
311 << vars[i]->name() <<
"of the potential to be filled " 312 <<
"has a domain size of " << vars[i]->domainSize()
313 <<
", which is different from that of node " 314 << conditioning_nodes[i - 1] <<
" which is equal to " 315 <<
database.domainSize(conditioning_nodes[i - 1]));
319 std::size_t col = node2cols.second(target_node);
320 if (
database.domainSize(col) != vars[0]->domainSize()) {
323 << vars[0]->name() <<
"of the potential to be filled " 324 <<
"has a domain size of " << vars[0]->domainSize()
325 <<
", which is different from that of node " << target_node
326 <<
" which is equal to " <<
database.domainSize(col));
328 for (std::size_t i = 1; i < vars.size(); ++i) {
329 col = node2cols.second(conditioning_nodes[i - 1]);
330 if (
database.domainSize(col) != vars[i]->domainSize()) {
333 << vars[i]->name() <<
"of the potential to be filled " 334 <<
"has a domain size of " << vars[i]->domainSize()
335 <<
", which is different from that of node " 336 << conditioning_nodes[i - 1] <<
" which is equal to " 345 template <
template <
typename >
class ALLOC >
346 template <
typename GUM_SCALAR >
347 INLINE
typename std::enable_if< !std::is_same< GUM_SCALAR, double >::value,
349 ParamEstimator< ALLOC >::__setParameters(
351 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
352 Potential< GUM_SCALAR >& pot) {
353 __checkParameters(target_node, conditioning_nodes, pot);
355 const std::vector< double, ALLOC< double > > params(
359 const std::size_t size = params.size();
360 std::vector< GUM_SCALAR, ALLOC< GUM_SCALAR > > xparams(size);
361 for (std::size_t i = std::size_t(0); i < size; ++i)
362 xparams[i] = GUM_SCALAR(params[i]);
364 pot.fillWith(xparams);
369 template <
template <
typename >
class ALLOC >
370 template <
typename GUM_SCALAR >
371 INLINE
typename std::enable_if< std::is_same< GUM_SCALAR, double >::value,
373 ParamEstimator< ALLOC >::__setParameters(
375 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
376 Potential< GUM_SCALAR >& pot) {
377 __checkParameters(target_node, conditioning_nodes, pot);
379 const std::vector< double, ALLOC< double > > params(
381 pot.fillWith(params);
386 template <
template <
typename >
class ALLOC >
387 template <
typename GUM_SCALAR >
390 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
391 Potential< GUM_SCALAR >& pot) {
392 __setParameters(target_node, conditioning_nodes, pot);
397 template <
template <
typename >
class ALLOC >
398 INLINE
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
405 template <
template <
typename >
class ALLOC >
406 INLINE
const DatabaseTable< ALLOC >&
413 template <
template <
typename >
class ALLOC >
414 template <
typename GUM_SCALAR >
ParamEstimator< ALLOC > & operator=(const ParamEstimator< ALLOC > &from)
copy operator
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's parameters corresponding to a given Potential
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.
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'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
allocator_type getAllocator() const
returns the allocator used by the score
virtual void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context ...
virtual ~ParamEstimator()
destructor
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's parameters corresponding to a given target node
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
Size NodeId
Type for node ids.
RecordCounter< ALLOC > _counter
the record counter used to parse the database
#define GUM_ERROR(type, msg)
const std::vector< NodeId, ALLOC< NodeId > > _empty_nodevect
an empty vector of nodes, used for empty conditioning