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 >
172 operator=(
const DBRowGeneratorEM< GUM_SCALAR, ALLOC >& from) {
175 __input_row = from.__input_row;
176 __missing_cols = from.__missing_cols;
177 __nb_miss = from.__nb_miss;
178 __joint_proba = from.__joint_proba;
179 __filled_row1 = from.__filled_row1;
180 __filled_row2 = from.__filled_row2;
181 __use_filled_row1 = from.__use_filled_row1;
182 __original_weight = from.__original_weight;
184 if (__joint_inst !=
nullptr) {
186 __joint_inst =
nullptr;
189 if (from.__joint_inst !=
nullptr) {
190 __joint_inst =
new Instantiation(__joint_proba);
191 const auto& var_seq = __joint_inst->variablesSequence();
192 const std::size_t size = var_seq.size();
193 for (std::size_t i = std::size_t(0); i < size; ++i) {
194 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
204 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
206 operator=(DBRowGeneratorEM< GUM_SCALAR, ALLOC >&& from) {
209 __input_row = from.__input_row;
210 __missing_cols = std::move(from.__missing_cols);
211 __nb_miss = from.__nb_miss;
212 __joint_proba = std::move(from.__joint_proba);
213 __filled_row1 = std::move(from.__filled_row1);
214 __filled_row2 = std::move(from.__filled_row2);
215 __use_filled_row1 = from.__use_filled_row1;
216 __original_weight = from.__original_weight;
218 if (__joint_inst !=
nullptr) {
220 __joint_inst =
nullptr;
223 if (from.__joint_inst !=
nullptr) {
224 __joint_inst =
new Instantiation(__joint_proba);
225 const auto& var_seq = __joint_inst->variablesSequence();
226 const std::size_t size = var_seq.size();
227 for (std::size_t i = std::size_t(0); i < size; ++i) {
228 __joint_inst->chgVal(
Idx(i), from.__joint_inst->val(i));
238 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
239 INLINE
const DBRow< DBTranslatedValue, ALLOC >&
244 if (__input_row !=
nullptr)
return *__input_row;
246 if (__use_filled_row1) {
248 __filled_row1.setWeight(__joint_proba.get(*__joint_inst)
249 * __original_weight);
252 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i)
253 __filled_row1[__missing_cols[i]].discr_val = __joint_inst->val(i);
256 __use_filled_row1 =
false;
258 return __filled_row1;
261 __filled_row2.setWeight(__joint_proba.get(*__joint_inst)
262 * __original_weight);
265 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i)
266 __filled_row2[__missing_cols[i]].discr_val = __joint_inst->val(i);
269 __use_filled_row1 =
true;
271 return __filled_row2;
277 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
279 const DBRow< DBTranslatedValue, ALLOC >& row) {
282 bool found_unobserved =
false;
283 const auto& xrow = row.row();
287 if (xrow[col].discr_val == std::numeric_limits< std::size_t >::max()) {
288 if (!found_unobserved) {
289 __missing_cols.clear();
290 found_unobserved =
true;
292 __missing_cols.push_back(col);
298 "The BDRowGeneratorEM does not handle yet continuous " 299 <<
"variables. But the variable in column" << col
300 <<
" is continuous.");
306 <<
" is not supported yet");
311 if (!found_unobserved) {
313 return std::size_t(1);
316 __input_row =
nullptr;
317 __nb_miss = __missing_cols.size();
318 __original_weight = row.weight();
322 VariableElimination< GUM_SCALAR > ve(this->
_bn);
327 std::size_t i = std::size_t(0);
328 bool end_miss =
false;
329 for (
const auto col : this->_columns_of_interest) {
330 if (!end_miss && (col == __missing_cols[i])) {
331 target_set.insert(
NodeId(col));
333 if (i == __nb_miss) end_miss =
true;
335 __filled_row1[col].discr_val = xrow[col].discr_val;
336 __filled_row2[col].discr_val = xrow[col].discr_val;
340 std::size_t i = std::size_t(0);
341 bool end_miss =
false;
342 for (
const auto col : this->_columns_of_interest) {
343 if (!end_miss && (col == __missing_cols[i])) {
346 if (i == __nb_miss) end_miss =
true;
348 __filled_row1[col].discr_val = xrow[col].discr_val;
349 __filled_row2[col].discr_val = xrow[col].discr_val;
354 ve.addJointTarget(target_set);
357 const std::size_t row_size = xrow.size();
359 for (std::size_t col = std::size_t(0); col < row_size; ++col) {
363 if (xrow[col].discr_val
364 != std::numeric_limits< std::size_t >::max()) {
365 ve.addEvidence(
NodeId(col), xrow[col].discr_val);
371 "The BDRowGeneratorEM does not handle yet continuous " 372 <<
"variables. But the variable in column" << col
373 <<
" is continuous.");
379 <<
" is not supported yet");
383 for (std::size_t col = std::size_t(0); col < row_size; ++col) {
387 if (xrow[col].discr_val
388 != std::numeric_limits< std::size_t >::max()) {
390 xrow[col].discr_val);
396 "The BDRowGeneratorEM does not handle yet continuous " 397 <<
"variables. But the variable in column" << col
398 <<
" is continuous.");
404 <<
" is not supported yet");
410 Potential< GUM_SCALAR >& pot =
411 const_cast< Potential< GUM_SCALAR >&
>(ve.jointPosterior(target_set));
412 __joint_proba = std::move(pot);
413 if (__joint_inst !=
nullptr)
delete __joint_inst;
414 __joint_inst =
new Instantiation(__joint_proba);
418 const auto& var_sequence = __joint_proba.variablesSequence();
420 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i) {
421 __missing_cols[i] = std::size_t(this->
_bn->nodeId(*(var_sequence[i])));
424 for (std::size_t i = std::size_t(0); i < __nb_miss; ++i) {
430 return std::size_t(__joint_proba.domainSize());
435 template <
typename GUM_SCALAR,
template <
typename >
class ALLOC >
437 const BayesNet< GUM_SCALAR >& new_bn) {
441 const DAG& dag = new_bn.dag();
445 if (!dag.existsNode(iter.first())) {
449 <<
" of the database is associated to Node ID " 451 <<
", which does not belong to the Bayesian network");
459 std::size_t size = std::size_t(0);
461 for (
auto node : new_bn.dag())
462 if (std::size_t(node) > size) size = std::size_t(node);
467 if (iter.second() > size) size = iter.second();
470 __filled_row1.resize(size + 1);
471 __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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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