51 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
58 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
63 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
65 const Size& num_threads,
66 const bool _storeVertices_,
67 const bool _storeBNOpt_) {
85 if (_storeVertices_) {
111 generator.seed(seed);
116 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
120 const std::vector< GUM_SCALAR >& vertex,
121 const bool& elimRedund) {
124 std::string var_name =
workingSet_[tId]->variable(
id).name();
125 auto delim = var_name.find_first_of(
"_");
126 var_name = var_name.substr(0, delim);
128 if (
l_modal_[tId].exists(var_name)) {
132 for (
Size mod = 0; mod < vsize; mod++)
133 exp += vertex[mod] *
l_modal_[tId][var_name][mod];
152 for (
Size mod = 0; mod < vsize; mod++) {
158 std::vector< Size > key(3);
167 if (vertex[mod] > l_marginalMax_[tId][
id][mod]) {
168 l_marginalMax_[tId][id][mod] = vertex[mod];
171 if (_infE_::storeBNOpt_ && !_infE_::evidence_.exists(
id)) {
172 std::vector< Size > key(3);
177 if (l_optimalNet_[tId]->insert(key,
true)) result =
true;
179 }
else if (vertex[mod] == l_marginalMin_[tId][
id][mod]
180 || vertex[mod] == l_marginalMax_[tId][
id][mod]) {
185 std::vector< Size > key(3);
193 if (_infE_::storeBNOpt_ && vertex[mod] == l_marginalMax_[tId][
id][mod]
194 && !_infE_::evidence_.exists(
id)) {
195 std::vector< Size > key(3);
206 if (_infE_::storeVertices_ && !added && newOne) {
213 if (_infE_::storeBNOpt_ && result)
return true;
218 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
222 const std::vector< GUM_SCALAR >& vertex,
223 const bool& elimRedund) {
230 for (
auto it = nodeCredalSet.cbegin(), itEnd = nodeCredalSet.cend(); it != itEnd; ++it) {
233 for (
Size i = 0; i < dsize; i++) {
234 if (std::fabs(vertex[i] - (*it)[i]) >
epsilon) {
243 if (!eq || nodeCredalSet.size() == 0) {
244 nodeCredalSet.push_back(vertex);
250 if (nodeCredalSet.size() == 1)
return;
255 auto itEnd = std::remove_if(
256 nodeCredalSet.begin(),
258 [&](
const std::vector< GUM_SCALAR >& v) ->
bool {
259 for (auto jt = v.cbegin(),
261 minIt = l_marginalMin_[tId][id].cbegin(),
262 minItEnd = l_marginalMin_[tId][id].cend(),
263 maxIt = l_marginalMax_[tId][id].cbegin(),
264 maxItEnd = l_marginalMax_[tId][id].cend();
265 jt != jtEnd && minIt != minItEnd && maxIt != maxItEnd;
266 ++jt, ++minIt, ++maxIt) {
267 if ((std::fabs(*jt - *minIt) < epsilon || std::fabs(*jt - *maxIt) < epsilon)
268 && std::fabs(*minIt - *maxIt) > epsilon)
274 nodeCredalSet.erase(itEnd, nodeCredalSet.end());
277 if (!elimRedund || nodeCredalSet.size() <= 2)
return;
282 Size setSize =
Size(nodeCredalSet.size());
285 lrsWrapper.
setUpV(dsize, setSize);
287 for (
const auto& vtx: nodeCredalSet)
288 lrsWrapper.
fillV(vtx);
292 l_marginalSets_[tId][id] = lrsWrapper.
getOutput();
295 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
303 auto threadedExec = [
this](
const std::size_t this_thread,
304 const std::size_t nb_threads,
305 const std::vector< std::pair< Idx, Idx > >& ranges) {
309 const auto end_i = this->
threadRanges_[this_thread + 1].first;
311 const auto marginalMax_size = this->
marginalMax_.size();
314 while ((i < end_i) || (j < end_j)) {
316 for (
Idx tId = 0; tId < tsize; tId++) {
324 if (++j == domain_size) {
327 if (i < marginalMax_size) domain_size = this->
marginalMax_[i].size();
337 ? std::vector< std::pair< NodeId, Idx > >{{0, 0}, {this->
marginalMin_.size(), 0}}
370 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
377 std::vector< GUM_SCALAR > tEps(nb_threads, 0);
380 auto threadedExec = [
this, &tEps](
const std::size_t this_thread,
381 const std::size_t nb_threads,
382 const std::vector< std::pair< Idx, Idx > >& ranges) {
383 auto& this_tEps = tEps[this_thread];
384 GUM_SCALAR delta = 0;
389 const auto end_i = this->
threadRanges_[this_thread + 1].first;
391 const auto marginalMax_size = this->
marginalMax_.size();
393 while ((i < end_i) || (j < end_j)) {
396 delta = (delta < 0) ? (-delta) : delta;
397 if (this_tEps < delta) this_tEps = delta;
401 delta = (delta < 0) ? (-delta) : delta;
402 if (this_tEps < delta) this_tEps = delta;
407 if (++j == domain_size) {
410 if (i < marginalMax_size) domain_size = this->
marginalMax_[i].size();
420 ? std::vector< std::pair< NodeId, Idx > >{{0, 0}, {this->
marginalMin_.size(), 0}}
424 GUM_SCALAR eps = tEps[0];
425 for (
const auto nb: tEps)
426 if (eps < nb) eps = nb;
474 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
483 for (
long i = 0; i < nsize; i++) {
486 for (
Size j = 0; j < dSize; j++) {
490 for (
Size tId = 0; tId < tsize; tId++) {
502 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
514 auto threadedExec = [
this, tsize](
const std::size_t this_thread,
515 const std::size_t nb_threads,
517 const std::vector< std::pair< Idx, Idx > >& ranges) {
518 for (
Idx i = ranges[this_thread].first, end = ranges[this_thread].second; i < end; i++) {
520 for (
Size tId = 0; tId < tsize; ++tId) {
524 for (
const auto& vtx: nodeThreadCredalSet) {
536 for (
Idx work_index = 0; work_index < working_size; ++work_index) {
538 const auto nsize =
workingSet_[work_index]->size();
539 const auto real_nb_threads = std::min(nb_threads, nsize);
577 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
580 if (this->
modal_.empty())
return;
590 auto threadedExec = [
this](
const std::size_t this_thread,
591 const std::size_t nb_threads,
593 const std::vector< std::pair< Idx, Idx > >& ranges) {
594 for (
Idx i = ranges[this_thread].first, end = ranges[this_thread].second; i < end; i++) {
595 std::string var_name =
workingSet_[work_index]->variable(i).name();
596 auto delim = var_name.find_first_of(
"_");
597 var_name = var_name.substr(0, delim);
599 if (!
l_modal_[work_index].exists(var_name))
continue;
605 for (
Size mod = 0; mod < vsize; mod++)
606 exp += vertex[mod] *
l_modal_[work_index][var_name][mod];
616 for (
Idx work_index = 0; work_index < working_size; ++work_index) {
617 if (!this->
l_modal_[work_index].empty()) {
619 const auto nsize =
workingSet_[work_index]->size();
620 const auto real_nb_threads = std::min(nb_threads, nsize);
631 auto threadedExec = [
this](
const std::size_t this_thread,
632 const std::size_t nb_threads,
634 const std::vector< std::pair< Idx, Idx > >& ranges) {
635 for (
Idx i = ranges[this_thread].first, end = ranges[this_thread].second; i < end; i++) {
636 std::string var_name =
workingSet_[work_index]->variable(i).name();
637 auto delim = var_name.find_first_of(
"_");
638 var_name = var_name.substr(0, delim);
640 if (!
l_modal_[work_index].exists(var_name))
continue;
644 for (
Idx tId = 0; tId < tsize; tId++) {
655 for (
Idx work_index = 0; work_index < working_size; ++work_index) {
656 if (!this->
l_modal_[work_index].empty()) {
658 const auto real_nb_threads = std::min(nb_threads, nsize);
736 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
738 using dBN = std::vector< bool >;
743 for (
Idx i = 0; i < nsize; i++) {
749 for (
Size j = 0; j < dSize; j++) {
751 std::vector< Size > keymin(3);
755 std::vector< Size > keymax(keymin);
760 for (
Size tId = 0; tId < tsize; tId++) {
762 const std::vector< dBN* >& tOpts =
l_optimalNet_[tId]->getBNOptsFromKey(keymin);
765 for (
Size bn = 0; bn < osize; bn++) {
771 const std::vector< dBN* >& tOpts =
l_optimalNet_[tId]->getBNOptsFromKey(keymax);
774 for (
Size bn = 0; bn < osize; bn++) {
783 template < GUM_Numeric GUM_SCALAR,
class BNInferenceEngine >
789 for (
Size bn = 0; bn < tsize; bn++) {
double epsilon() const override
Returns the value of epsilon.
double epsilonStandard() const noexcept
Size getNumberOfThreads() const override
returns the current max number of threads used by the class containing this ThreadNumberManager
Class template representing a Credal Network.
margi oldMarginalMax_
Old upper marginals used to compute epsilon.
margi evidence_
Holds observed variables states.
bool storeBNOpt_
Iterations limit stopping rule used by some algorithms such as CNMonteCarloSampling.
margi marginalMax_
Upper marginals.
void updateCredalSets_(const NodeId &id, const std::vector< GUM_SCALAR > &vertex, const bool &elimRedund=false)
Given a node id and one of it's possible vertex, update it's credal set.
InferenceEngine(const CredalNet< GUM_SCALAR > &credalNet)
Construtor.
margi oldMarginalMin_
Old lower marginals used to compute epsilon.
bool storeVertices_
True if credal sets vertices are stored, False otherwise.
virtual void eraseAllEvidence()
removes all the evidence entered into the network
expe expectationMax_
Upper expectations, if some variables modalities were inserted.
credalSet marginalSets_
Credal sets vertices, if enabled.
const CredalNet< GUM_SCALAR > & credalNet() const
Get this credal network.
margi marginalMin_
Lower marginals.
dynExpe modal_
Variables modalities used to compute expectations.
expe expectationMin_
Lower expectations, if some variables modalities were inserted.
std::vector< std::pair< NodeId, Idx > > threadRanges_
the ranges of elements of marginalMin_ and marginalMax_ processed by each thread
VarMod2BNsMap< GUM_SCALAR > dbnOpt_
Object used to efficiently store optimal bayes net during inference, for some algorithms.
Class template acting as a wrapper for Lexicographic Reverse Search by David Avis.
const matrix & getOutput() const
Get the output matrix solution of the problem.
void setUpV(const Size &card, const Size &vertices)
Sets up a V-representation.
void elimRedundVrep()
V-Redundancy elimination.
void fillV(const std::vector< GUM_SCALAR > &vertex)
Creates the V-representation of a polytope by adding a vertex to the problem input _input_.
void updateMarginals_()
Fusion of threads marginals.
_margis_ l_marginalMin_
Threads lower marginals, one per thread.
_expes_ l_expectationMax_
Threads upper expectations, one per thread.
_expes_ l_expectationMin_
Threads lower expectations, one per thread.
std::vector< _bnet_ * > workingSet_
Threads IBayesNet.
std::vector< BNInferenceEngine * > l_inferenceEngine_
Threads BNInferenceEngine.
_credalSets_ l_marginalSets_
Threads vertices.
void optFusion_()
Fusion of threads optimal IBayesNet.
_modals_ l_modal_
Threads modalities.
std::vector< VarMod2BNsMap< GUM_SCALAR > * > l_optimalNet_
Threads optimal IBayesNet.
void eraseAllEvidence() override
Erase all inference related data to perform another one.
void updateOldMarginals_()
Update old marginals (from current marginals).
_margis_ l_marginalMax_
Threads upper marginals, one per thread.
MultipleInferenceEngine(const CredalNet< GUM_SCALAR > &credalNet)
Constructor.
void expFusion_()
Fusion of threads expectations.
void initThreadsData_(const Size &num_threads, const bool _storeVertices_, const bool _storeBNOpt_)
Initialize threads data.
bool updateThread_(Size this_thread, const NodeId &id, const std::vector< GUM_SCALAR > &vertex, const bool &elimRedund=false)
Update thread information (marginals, expectations, IBayesNet, vertices) for a given node id.
std::vector< List< const Tensor< GUM_SCALAR > * > * > workingSetE_
Threads evidence.
std::vector< std::mt19937 > generators_
_margis_ l_evidence_
Threads evidence.
void _updateThreadCredalSets_(Size this_thread, const NodeId &id, const std::vector< GUM_SCALAR > &vertex, const bool &elimRedund)
_clusters_ l_clusters_
Threads clusters.
~MultipleInferenceEngine() override
Destructor.
const GUM_SCALAR computeEpsilon_() override
Compute epsilon and update old marginals.
Typed, documented, sectioned configuration singleton read from an .ini content.
unsigned int getThreadNumber()
Get the calling thread id.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
GUM_SHARED_PUBLIC unsigned int currentRandomGeneratorValue()
returns the current generator's value
Abstract class representing CredalNet inference engines.
Abstract class representing CredalNet inference engines.
namespace for all credal networks entities
gum is the global namespace for all aGrUM entities
std::vector< std::pair< Idx, Idx > > dispatchRangeToThreads(const Idx beg, const Idx end, const unsigned int nb_threads)
returns a vector equally splitting elements of a range among threads
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...
static void execute(std::size_t nb_threads, FUNCTION exec_func, ARGS &&... func_args)
executes a function using several threads
static int nbRunningThreadsExecutors()
indicates how many threadExecutors are currently running