30 #ifndef DOXYGEN_SHOULD_SKIP_THIS 37 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
45 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
49 const BayesNet< GUM_SCALAR >& bn,
50 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
54 DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >(
60 __filled_row1(bn.size(), 1.0, alloc),
61 __filled_row2(bn.size(), 1.0, alloc) {
69 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
71 const DBRowGeneratorEM< GUM_SCALAR, ALLOC >& from,
75 __input_row(from.__input_row), __missing_cols(from.__missing_cols),
76 __nb_miss(from.__nb_miss), __joint_proba(from.__joint_proba),
77 __filled_row1(from.__filled_row1), __filled_row2(from.__filled_row2),
78 __use_filled_row1(from.__use_filled_row1),
79 __original_weight(from.__original_weight) {
80 if (from.__joint_inst !=
nullptr) {
81 __joint_inst =
new Instantiation(__joint_proba);
82 const auto& var_seq = __joint_inst->variablesSequence();
83 const std::size_t size = var_seq.size();
84 for (std::size_t i = std::size_t(0); i < size; ++i) {
85 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
94 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
96 const DBRowGeneratorEM< GUM_SCALAR, ALLOC >& from) :
101 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
103 DBRowGeneratorEM< GUM_SCALAR, ALLOC >&& from,
107 __input_row(from.__input_row),
108 __missing_cols(
std::move(from.__missing_cols)), __nb_miss(from.__nb_miss),
109 __joint_proba(
std::move(from.__joint_proba)),
110 __filled_row1(
std::move(from.__filled_row1)),
111 __filled_row2(
std::move(from.__filled_row2)),
112 __use_filled_row1(from.__use_filled_row1),
113 __original_weight(from.__original_weight) {
114 if (from.__joint_inst !=
nullptr) {
115 __joint_inst =
new Instantiation(__joint_proba);
116 const auto& var_seq = __joint_inst->variablesSequence();
117 const std::size_t size = var_seq.size();
118 for (std::size_t i = std::size_t(0); i < size; ++i) {
119 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
128 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
130 DBRowGeneratorEM< GUM_SCALAR, ALLOC >&& from) :
136 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
137 DBRowGeneratorEM< GUM_SCALAR, ALLOC >*
141 ALLOC< DBRowGeneratorEM< GUM_SCALAR, ALLOC > > allocator(alloc);
142 DBRowGeneratorEM< GUM_SCALAR, ALLOC >* generator = allocator.allocate(1);
144 allocator.construct(generator, *
this, alloc);
146 allocator.deallocate(generator, 1);
154 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
155 DBRowGeneratorEM< GUM_SCALAR, ALLOC >*
162 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
164 if (__joint_inst !=
nullptr)
delete __joint_inst;
170 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
171 DBRowGeneratorEM< GUM_SCALAR, ALLOC >&
173 const DBRowGeneratorEM< GUM_SCALAR, ALLOC >& from) {
176 __input_row = from.__input_row;
177 __missing_cols = from.__missing_cols;
178 __nb_miss = from.__nb_miss;
179 __joint_proba = from.__joint_proba;
180 __filled_row1 = from.__filled_row1;
181 __filled_row2 = from.__filled_row2;
182 __use_filled_row1 = from.__use_filled_row1;
183 __original_weight = from.__original_weight;
185 if (__joint_inst !=
nullptr) {
187 __joint_inst =
nullptr;
190 if (from.__joint_inst !=
nullptr) {
191 __joint_inst =
new Instantiation(__joint_proba);
192 const auto& var_seq = __joint_inst->variablesSequence();
193 const std::size_t size = var_seq.size();
194 for (std::size_t i = std::size_t(0); i < size; ++i) {
195 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
205 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
206 DBRowGeneratorEM< GUM_SCALAR, ALLOC >&
208 DBRowGeneratorEM< GUM_SCALAR, ALLOC >&& from) {
211 __input_row = from.__input_row;
212 __missing_cols = std::move(from.__missing_cols);
213 __nb_miss = from.__nb_miss;
214 __joint_proba = std::move(from.__joint_proba);
215 __filled_row1 = std::move(from.__filled_row1);
216 __filled_row2 = std::move(from.__filled_row2);
217 __use_filled_row1 = from.__use_filled_row1;
218 __original_weight = from.__original_weight;
220 if (__joint_inst !=
nullptr) {
222 __joint_inst =
nullptr;
225 if (from.__joint_inst !=
nullptr) {
226 __joint_inst =
new Instantiation(__joint_proba);
227 const auto& var_seq = __joint_inst->variablesSequence();
228 const std::size_t size = var_seq.size();
229 for (std::size_t i = std::size_t(0); i < size; ++i) {
230 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
240 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
241 INLINE
const DBRow< DBTranslatedValue, ALLOC >&
246 if (__input_row !=
nullptr)
return *__input_row;
248 if (__use_filled_row1) {
250 __filled_row1.setWeight(__joint_proba.get(*__joint_inst)
251 * __original_weight);
254 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i)
255 __filled_row1[__missing_cols[i]].discr_val = __joint_inst->val(i);
258 __use_filled_row1 =
false;
260 return __filled_row1;
263 __filled_row2.setWeight(__joint_proba.get(*__joint_inst)
264 * __original_weight);
267 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i)
268 __filled_row2[__missing_cols[i]].discr_val = __joint_inst->val(i);
271 __use_filled_row1 =
true;
273 return __filled_row2;
279 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
281 const DBRow< DBTranslatedValue, ALLOC >& row) {
284 bool found_unobserved =
false;
285 const auto& xrow = row.row();
289 if (xrow[col].discr_val == std::numeric_limits< std::size_t >::max()) {
290 if (!found_unobserved) {
291 __missing_cols.clear();
292 found_unobserved =
true;
294 __missing_cols.push_back(col);
300 "The BDRowGeneratorEM does not handle yet continuous " 301 <<
"variables. But the variable in column" << col
302 <<
" is continuous.");
308 <<
" is not supported yet");
313 if (!found_unobserved) {
315 return std::size_t(1);
318 __input_row =
nullptr;
319 __nb_miss = __missing_cols.size();
320 __original_weight = row.weight();
324 VariableElimination< GUM_SCALAR > ve(this->
_bn);
329 std::size_t i = std::size_t(0);
330 bool end_miss =
false;
331 for (
const auto col: this->_columns_of_interest) {
332 if (!end_miss && (col == __missing_cols[i])) {
333 target_set.insert(
NodeId(col));
335 if (i == __nb_miss) end_miss =
true;
337 __filled_row1[col].discr_val = xrow[col].discr_val;
338 __filled_row2[col].discr_val = xrow[col].discr_val;
342 std::size_t i = std::size_t(0);
343 bool end_miss =
false;
344 for (
const auto col: this->_columns_of_interest) {
345 if (!end_miss && (col == __missing_cols[i])) {
348 if (i == __nb_miss) end_miss =
true;
350 __filled_row1[col].discr_val = xrow[col].discr_val;
351 __filled_row2[col].discr_val = xrow[col].discr_val;
356 ve.addJointTarget(target_set);
359 const std::size_t row_size = xrow.size();
361 for (std::size_t col = std::size_t(0); col < row_size; ++col) {
365 if (xrow[col].discr_val
366 != std::numeric_limits< std::size_t >::max()) {
367 ve.addEvidence(
NodeId(col), xrow[col].discr_val);
373 "The BDRowGeneratorEM does not handle yet continuous " 374 <<
"variables. But the variable in column" << col
375 <<
" is continuous.");
381 <<
" is not supported yet");
385 for (std::size_t col = std::size_t(0); col < row_size; ++col) {
389 if (xrow[col].discr_val
390 != std::numeric_limits< std::size_t >::max()) {
392 xrow[col].discr_val);
398 "The BDRowGeneratorEM does not handle yet continuous " 399 <<
"variables. But the variable in column" << col
400 <<
" is continuous.");
406 <<
" is not supported yet");
412 Potential< GUM_SCALAR >& pot =
413 const_cast< Potential< GUM_SCALAR >&
>(ve.jointPosterior(target_set));
414 __joint_proba = std::move(pot);
415 if (__joint_inst !=
nullptr)
delete __joint_inst;
416 __joint_inst =
new Instantiation(__joint_proba);
420 const auto& var_sequence = __joint_proba.variablesSequence();
422 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i) {
423 __missing_cols[i] = std::size_t(this->
_bn->nodeId(*(var_sequence[i])));
426 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i) {
432 return std::size_t(__joint_proba.domainSize());
437 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
439 const BayesNet< GUM_SCALAR >& new_bn) {
443 const DAG& dag = new_bn.dag();
447 if (!dag.existsNode(iter.first())) {
451 <<
" of the database is associated to Node ID " 453 <<
", which does not belong to the Bayesian network");
461 std::size_t size = std::size_t(0);
463 for (
auto node: new_bn.dag())
464 if (std::size_t(node) > size) size = std::size_t(node);
469 if (iter.second() > size) size = iter.second();
472 __filled_row1.resize(size + 1);
473 __filled_row2.resize(size + 1);
std::vector< std::size_t, ALLOC< std::size_t > > _columns_of_interest
the set of columns of interest
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
DBRowGeneratorGoal
the type of things that a DBRowGenerator is designed for
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
void decreaseRemainingRows()
decrease the number of remaining output rows
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
DBRowGeneratorEM(const std::vector< DBTranslatedValueType, ALLOC< DBTranslatedValueType > > column_types, const BayesNet< GUM_SCALAR > &bn, 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
const BayesNet< GUM_SCALAR > * _bn
the Bayesian network used to fill the unobserved values
iterator begin() const
Returns the unsafe iterator at the beginning of the gum::Bijection.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
const iterator & end() const noexcept
Returns the unsafe iterator at the end of the gum::Bijection.
virtual DBRowGeneratorEM< GUM_SCALAR, ALLOC > * clone() const override final
virtual copy constructor
bool empty() const noexcept
Returns true if the gum::Bijection doesn't contain any association.
DBTranslatedValueType
The nature of the elements handled by translators (discrete, continuous).
~DBRowGeneratorEM()
destructor
allocator_type getAllocator() const
returns the allocator used
DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > & operator=(const DBRowGeneratorWithBN< GUM_SCALAR, ALLOC > &from)
copy operator
Bijection< NodeId, std::size_t, ALLOC< std::size_t > > _nodeId2columns
the mapping betwen the BN's node ids and the database's columns
virtual void setBayesNet(const BayesNet< GUM_SCALAR > &new_bn) override final
assign a new Bayes net to the generator
virtual std::size_t _computeRows(const DBRow< DBTranslatedValue, ALLOC > &row) override final
computes the rows it will provide as output
virtual void setBayesNet(const BayesNet< GUM_SCALAR > &new_bn)
assign a new Bayes net to the generator
DBRowGeneratorWithBN(const std::vector< DBTranslatedValueType, ALLOC< DBTranslatedValueType > > column_types, const BayesNet< GUM_SCALAR > &bn, const DBRowGeneratorGoal goal, 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
virtual const DBRow< DBTranslatedValue, ALLOC > & generate() override final
generates one ouput DBRow for each DBRow passed to method setInputRow
std::vector< DBTranslatedValueType, ALLOC< DBTranslatedValueType > > _column_types
the types of the columns in the DatabaseTable
Size Idx
Type for indexes.
DBRowGeneratorEM< GUM_SCALAR, ALLOC > & operator=(const DBRowGeneratorEM< GUM_SCALAR, ALLOC > &from)
copy operator
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)
allocator_type getAllocator() const
returns the allocator used
ALLOC< DBTranslatedValue > allocator_type
type for the allocators passed in arguments of methods