26 #ifndef DOXYGEN_SHOULD_SKIP_THIS 33 template <
template <
typename >
class ALLOC >
36 return _counter.getAllocator();
41 template <
template <
typename >
class ALLOC >
43 const DBRowGeneratorParser< ALLOC >& parser,
44 const Apriori< ALLOC >& external_apriori,
45 const Apriori< ALLOC >& score_internal_apriori,
46 const std::vector< std::pair< std::size_t, std::size_t >,
47 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
48 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
51 _counter(parser, ranges, nodeId2columns, alloc) {
57 ALLOC< Apriori< ALLOC > > allocator(alloc);
68 template <
template <
typename >
class ALLOC >
70 const DBRowGeneratorParser< ALLOC >& parser,
71 const Apriori< ALLOC >& external_apriori,
72 const Apriori< ALLOC >& score_internal_apriori,
73 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
76 _counter(parser, nodeId2columns, alloc) {
82 ALLOC< Apriori< ALLOC > > allocator(alloc);
93 template <
template <
typename >
class ALLOC >
95 const ParamEstimator< ALLOC >& from,
105 template <
template <
typename >
class ALLOC >
107 const ParamEstimator< ALLOC >& from) :
112 template <
template <
typename >
class ALLOC >
114 ParamEstimator< ALLOC >&& from,
119 from._external_apriori =
nullptr;
120 from._score_internal_apriori =
nullptr;
126 template <
template <
typename >
class ALLOC >
133 template <
template <
typename >
class ALLOC >
135 ALLOC< Apriori< ALLOC > > allocator(this->
getAllocator());
151 template <
template <
typename >
class ALLOC >
153 operator=(
const ParamEstimator< ALLOC >& from) {
155 ALLOC< Apriori< ALLOC > > allocator(this->
getAllocator());
169 from._score_internal_apriori->clone(this->
getAllocator());
178 template <
template <
typename >
class ALLOC >
180 operator=(ParamEstimator< ALLOC >&& from) {
184 _counter = std::move(from._counter);
185 from._external_apriori =
nullptr;
186 from._score_internal_apriori =
nullptr;
193 template <
template <
typename >
class ALLOC >
200 template <
template <
typename >
class ALLOC >
207 template <
template <
typename >
class ALLOC >
215 template <
template <
typename >
class ALLOC >
223 template <
template <
typename >
class ALLOC >
225 return _counter.minNbRowsPerThread();
236 template <
template <
typename >
class ALLOC >
237 template <
template <
typename >
class XALLOC >
239 const std::vector< std::pair< std::size_t, std::size_t >,
240 XALLOC< std::pair< std::size_t, std::size_t > > >&
242 std::vector< std::pair< std::size_t, std::size_t >,
243 ALLOC< std::pair< std::size_t, std::size_t > > >
251 template <
template <
typename >
class ALLOC >
253 std::vector< std::pair< std::size_t, std::size_t >,
254 ALLOC< std::pair< std::size_t, std::size_t > > >
262 template <
template <
typename >
class ALLOC >
263 INLINE
const std::vector< std::pair< std::size_t, std::size_t >,
264 ALLOC< std::pair< std::size_t, std::size_t > > >&
271 template <
template <
typename >
class ALLOC >
272 INLINE std::vector< double, ALLOC< double > >
279 template <
template <
typename >
class ALLOC >
280 template <
typename GUM_SCALAR >
281 void ParamEstimator< ALLOC >::__checkParameters(
283 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
284 Potential< GUM_SCALAR >& pot) {
286 const Sequence< const DiscreteVariable* >& vars = pot.variablesSequence();
287 if (vars.size() == 0) {
288 GUM_ERROR(SizeError,
"the potential contains no variable");
292 const auto& node2cols =
_counter.nodeId2Columns();
293 if (node2cols.empty()) {
294 if (
database.domainSize(target_node) != vars[0]->domainSize()) {
297 << vars[0]->name() <<
"of the potential to be filled " 298 <<
"has a domain size of " << vars[0]->domainSize()
299 <<
", which is different from that of node " << target_node
300 <<
" which is equal to " 301 <<
database.domainSize(target_node));
303 for (std::size_t i = 1; i < vars.size(); ++i) {
304 if (
database.domainSize(conditioning_nodes[i - 1])
305 != vars[i]->domainSize()) {
308 << vars[i]->name() <<
"of the potential to be filled " 309 <<
"has a domain size of " << vars[i]->domainSize()
310 <<
", which is different from that of node " 311 << conditioning_nodes[i - 1] <<
" which is equal to " 312 <<
database.domainSize(conditioning_nodes[i - 1]));
316 std::size_t col = node2cols.second(target_node);
317 if (
database.domainSize(col) != vars[0]->domainSize()) {
320 << vars[0]->name() <<
"of the potential to be filled " 321 <<
"has a domain size of " << vars[0]->domainSize()
322 <<
", which is different from that of node " << target_node
323 <<
" which is equal to " <<
database.domainSize(col));
325 for (std::size_t i = 1; i < vars.size(); ++i) {
326 col = node2cols.second(conditioning_nodes[i - 1]);
327 if (
database.domainSize(col) != vars[i]->domainSize()) {
330 << vars[i]->name() <<
"of the potential to be filled " 331 <<
"has a domain size of " << vars[i]->domainSize()
332 <<
", which is different from that of node " 333 << conditioning_nodes[i - 1] <<
" which is equal to " 342 template <
template <
typename >
class ALLOC >
343 template <
typename GUM_SCALAR >
344 INLINE
typename std::enable_if< !std::is_same< GUM_SCALAR, double >::value,
346 ParamEstimator< ALLOC >::__setParameters(
348 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
349 Potential< GUM_SCALAR >& pot) {
350 __checkParameters(target_node, conditioning_nodes, pot);
352 const std::vector< double, ALLOC< double > > params(
356 const std::size_t size = params.size();
357 std::vector< GUM_SCALAR, ALLOC< GUM_SCALAR > > xparams(size);
358 for (std::size_t i = std::size_t(0); i < size; ++i)
359 xparams[i] = GUM_SCALAR(params[i]);
361 pot.fillWith(xparams);
366 template <
template <
typename >
class ALLOC >
367 template <
typename GUM_SCALAR >
368 INLINE
typename std::enable_if< std::is_same< GUM_SCALAR, double >::value,
370 ParamEstimator< ALLOC >::__setParameters(
372 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
373 Potential< GUM_SCALAR >& pot) {
374 __checkParameters(target_node, conditioning_nodes, pot);
376 const std::vector< double, ALLOC< double > > params(
378 pot.fillWith(params);
383 template <
template <
typename >
class ALLOC >
384 template <
typename GUM_SCALAR >
387 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes,
388 Potential< GUM_SCALAR >& pot) {
389 __setParameters(target_node, conditioning_nodes, pot);
394 template <
template <
typename >
class ALLOC >
395 INLINE
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
402 template <
template <
typename >
class ALLOC >
403 INLINE
const DatabaseTable< ALLOC >&
410 template <
template <
typename >
class ALLOC >
411 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
gum is the global namespace for all aGrUM entities
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