27 #ifndef DOXYGEN_SHOULD_SKIP_THIS 34 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
42 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
46 const BayesNet< GUM_SCALAR >& bn,
47 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
51 DBRowGeneratorWithBN< GUM_SCALAR, ALLOC >(
57 __filled_row1(bn.size(), 1.0, alloc),
58 __filled_row2(bn.size(), 1.0, alloc) {
66 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
68 const DBRowGeneratorEM< GUM_SCALAR, ALLOC >& from,
72 __input_row(from.__input_row), __missing_cols(from.__missing_cols),
73 __nb_miss(from.__nb_miss), __joint_proba(from.__joint_proba),
74 __filled_row1(from.__filled_row1), __filled_row2(from.__filled_row2),
75 __use_filled_row1(from.__use_filled_row1),
76 __original_weight(from.__original_weight) {
77 if (from.__joint_inst !=
nullptr) {
78 __joint_inst =
new Instantiation(__joint_proba);
79 const auto& var_seq = __joint_inst->variablesSequence();
80 const std::size_t size = var_seq.size();
81 for (std::size_t i = std::size_t(0); i < size; ++i) {
82 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
91 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
93 const DBRowGeneratorEM< GUM_SCALAR, ALLOC >& from) :
98 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
100 DBRowGeneratorEM< GUM_SCALAR, ALLOC >&& from,
104 __input_row(from.__input_row),
105 __missing_cols(
std::move(from.__missing_cols)), __nb_miss(from.__nb_miss),
106 __joint_proba(
std::move(from.__joint_proba)),
107 __filled_row1(
std::move(from.__filled_row1)),
108 __filled_row2(
std::move(from.__filled_row2)),
109 __use_filled_row1(from.__use_filled_row1),
110 __original_weight(from.__original_weight) {
111 if (from.__joint_inst !=
nullptr) {
112 __joint_inst =
new Instantiation(__joint_proba);
113 const auto& var_seq = __joint_inst->variablesSequence();
114 const std::size_t size = var_seq.size();
115 for (std::size_t i = std::size_t(0); i < size; ++i) {
116 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
125 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
127 DBRowGeneratorEM< GUM_SCALAR, ALLOC >&& from) :
133 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
134 DBRowGeneratorEM< GUM_SCALAR, ALLOC >*
138 ALLOC< DBRowGeneratorEM< GUM_SCALAR, ALLOC > > allocator(alloc);
139 DBRowGeneratorEM< GUM_SCALAR, ALLOC >* generator = allocator.allocate(1);
141 allocator.construct(generator, *
this, alloc);
143 allocator.deallocate(generator, 1);
151 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
152 DBRowGeneratorEM< GUM_SCALAR, ALLOC >*
159 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
161 if (__joint_inst !=
nullptr)
delete __joint_inst;
167 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
169 operator=(
const DBRowGeneratorEM< GUM_SCALAR, ALLOC >& from) {
172 __input_row = from.__input_row;
173 __missing_cols = from.__missing_cols;
174 __nb_miss = from.__nb_miss;
175 __joint_proba = from.__joint_proba;
176 __filled_row1 = from.__filled_row1;
177 __filled_row2 = from.__filled_row2;
178 __use_filled_row1 = from.__use_filled_row1;
179 __original_weight = from.__original_weight;
181 if (__joint_inst !=
nullptr) {
183 __joint_inst =
nullptr;
186 if (from.__joint_inst !=
nullptr) {
187 __joint_inst =
new Instantiation(__joint_proba);
188 const auto& var_seq = __joint_inst->variablesSequence();
189 const std::size_t size = var_seq.size();
190 for (std::size_t i = std::size_t(0); i < size; ++i) {
191 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
201 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
203 operator=(DBRowGeneratorEM< GUM_SCALAR, ALLOC >&& from) {
206 __input_row = from.__input_row;
207 __missing_cols = std::move(from.__missing_cols);
208 __nb_miss = from.__nb_miss;
209 __joint_proba = std::move(from.__joint_proba);
210 __filled_row1 = std::move(from.__filled_row1);
211 __filled_row2 = std::move(from.__filled_row2);
212 __use_filled_row1 = from.__use_filled_row1;
213 __original_weight = from.__original_weight;
215 if (__joint_inst !=
nullptr) {
217 __joint_inst =
nullptr;
220 if (from.__joint_inst !=
nullptr) {
221 __joint_inst =
new Instantiation(__joint_proba);
222 const auto& var_seq = __joint_inst->variablesSequence();
223 const std::size_t size = var_seq.size();
224 for (std::size_t i = std::size_t(0); i < size; ++i) {
225 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
235 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
236 INLINE
const DBRow< DBTranslatedValue, ALLOC >&
241 if (__input_row !=
nullptr)
return *__input_row;
243 if (__use_filled_row1) {
245 __filled_row1.setWeight(__joint_proba.get(*__joint_inst)
246 * __original_weight);
249 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i)
250 __filled_row1[__missing_cols[i]].discr_val = __joint_inst->val(i);
253 __use_filled_row1 =
false;
255 return __filled_row1;
258 __filled_row2.setWeight(__joint_proba.get(*__joint_inst)
259 * __original_weight);
262 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i)
263 __filled_row2[__missing_cols[i]].discr_val = __joint_inst->val(i);
266 __use_filled_row1 =
true;
268 return __filled_row2;
274 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
276 const DBRow< DBTranslatedValue, ALLOC >& row) {
279 bool found_unobserved =
false;
280 const auto& xrow = row.row();
284 if (xrow[col].discr_val == std::numeric_limits< std::size_t >::max()) {
285 if (!found_unobserved) {
286 __missing_cols.clear();
287 found_unobserved =
true;
289 __missing_cols.push_back(col);
295 "The BDRowGeneratorEM does not handle yet continuous " 296 <<
"variables. But the variable in column" << col
297 <<
" is continuous.");
303 <<
" is not supported yet");
308 if (!found_unobserved) {
310 return std::size_t(1);
313 __input_row =
nullptr;
314 __nb_miss = __missing_cols.size();
315 __original_weight = row.weight();
319 VariableElimination< GUM_SCALAR > ve(this->
_bn);
324 std::size_t i = std::size_t(0);
325 bool end_miss =
false;
326 for (
const auto col : this->_columns_of_interest) {
327 if (!end_miss && (col == __missing_cols[i])) {
328 target_set.insert(
NodeId(col));
330 if (i == __nb_miss) end_miss =
true;
332 __filled_row1[col].discr_val = xrow[col].discr_val;
333 __filled_row2[col].discr_val = xrow[col].discr_val;
337 std::size_t i = std::size_t(0);
338 bool end_miss =
false;
339 for (
const auto col : this->_columns_of_interest) {
340 if (!end_miss && (col == __missing_cols[i])) {
343 if (i == __nb_miss) end_miss =
true;
345 __filled_row1[col].discr_val = xrow[col].discr_val;
346 __filled_row2[col].discr_val = xrow[col].discr_val;
351 ve.addJointTarget(target_set);
354 const std::size_t row_size = xrow.size();
356 for (std::size_t col = std::size_t(0); col < row_size; ++col) {
360 if (xrow[col].discr_val
361 != std::numeric_limits< std::size_t >::max()) {
362 ve.addEvidence(
NodeId(col), xrow[col].discr_val);
368 "The BDRowGeneratorEM does not handle yet continuous " 369 <<
"variables. But the variable in column" << col
370 <<
" is continuous.");
376 <<
" is not supported yet");
380 for (std::size_t col = std::size_t(0); col < row_size; ++col) {
384 if (xrow[col].discr_val
385 != std::numeric_limits< std::size_t >::max()) {
387 xrow[col].discr_val);
393 "The BDRowGeneratorEM does not handle yet continuous " 394 <<
"variables. But the variable in column" << col
395 <<
" is continuous.");
401 <<
" is not supported yet");
407 Potential< GUM_SCALAR >& pot =
408 const_cast< Potential< GUM_SCALAR >&
>(ve.jointPosterior(target_set));
409 __joint_proba = std::move(pot);
410 if (__joint_inst !=
nullptr)
delete __joint_inst;
411 __joint_inst =
new Instantiation(__joint_proba);
415 const auto& var_sequence = __joint_proba.variablesSequence();
417 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i) {
418 __missing_cols[i] = std::size_t(this->
_bn->nodeId(*(var_sequence[i])));
421 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i) {
427 return std::size_t(__joint_proba.domainSize());
432 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
434 const BayesNet< GUM_SCALAR >& new_bn) {
438 const DAG& dag = new_bn.dag();
442 if (!dag.existsNode(iter.first())) {
446 <<
" of the database is associated to Node ID " 448 <<
", which does not belong to the Bayesian network");
456 std::size_t size = std::size_t(0);
458 for (
auto node : new_bn.dag())
459 if (std::size_t(node) > size) size = std::size_t(node);
464 if (iter.second() > size) size = iter.second();
467 __filled_row1.resize(size + 1);
468 __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.
gum is the global namespace for all aGrUM entities
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
A DBRowGenerator class that returns exactly the rows it gets in input.
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