29 template <
typename GUM_SCALAR,
class BNInferenceEngine >
48 template <
typename GUM_SCALAR,
class BNInferenceEngine >
54 template <
typename GUM_SCALAR,
class BNInferenceEngine >
125 #pragma omp parallel for 127 for (
int iter = 0; iter < int(psize); iter++) {
147 if (!this->
_modal.empty()) {
154 template <
typename GUM_SCALAR,
class BNInferenceEngine >
163 for (
auto node : tDag.
nodes()) {
167 std::vector< GUM_SCALAR > vertex;
170 vertex.push_back(potential[ins]);
183 template <
typename GUM_SCALAR,
class BNInferenceEngine >
194 template <
typename GUM_SCALAR,
class BNInferenceEngine >
210 template <
typename GUM_SCALAR,
class BNInferenceEngine >
242 #pragma omp critical(Init) 270 BNInferenceEngine* inference_engine =
271 new BNInferenceEngine((this->
_workingSet[this_thread]),
284 template <
typename GUM_SCALAR,
class BNInferenceEngine >
286 std::vector< bool >& toFill,
const Idx value)
const {
288 auto tfsize = toFill.size();
291 for (decltype(tfsize) i = 0; i < tfsize; i++) {
297 template <
typename GUM_SCALAR,
class BNInferenceEngine >
303 const auto cpt = &this->
_credalNet->credalNet_currentCpt();
305 using dBN = std::vector< std::vector< std::vector< bool > > >;
310 sample = dBN(this->
_l_optimalNet[this_thread]->getSampleDef());
317 for (
const auto& elt : t0) {
318 auto dSize = working_bn->
variable(elt.first).domainSize();
321 std::vector< GUM_SCALAR > var_cpt(potential->
domainSize());
323 Size pconfs =
Size((*cpt)[elt.first].size());
325 for (
Size pconf = 0; pconf < pconfs; pconf++) {
326 Size choosen_vertex = rand() % (*cpt)[elt.first][pconf].size();
329 __binaryRep(sample[elt.first][pconf], choosen_vertex);
332 for (
Size mod = 0; mod < dSize; mod++) {
333 var_cpt[pconf * dSize + mod] =
334 (*cpt)[elt.first][pconf][choosen_vertex][mod];
340 Size t0esize =
Size(elt.second.size());
342 for (
Size pos = 0; pos < t0esize; pos++) {
344 sample[elt.second[pos]] = sample[elt.first];
349 &working_bn->
cpt(elt.second[pos])));
354 for (
const auto& elt : t1) {
355 auto dSize = working_bn->
variable(elt.first).domainSize();
358 std::vector< GUM_SCALAR > var_cpt(potential->
domainSize());
360 for (
Size pconf = 0; pconf < (*cpt)[elt.first].size(); pconf++) {
361 Idx choosen_vertex =
Idx(rand() % (*cpt)[elt.first][pconf].size());
364 __binaryRep(sample[elt.first][pconf], choosen_vertex);
367 for (decltype(dSize) mod = 0; mod < dSize; mod++) {
368 var_cpt[pconf * dSize + mod] =
369 (*cpt)[elt.first][pconf][choosen_vertex][mod];
375 auto t1esize = elt.second.size();
377 for (decltype(t1esize) pos = 0; pos < t1esize; pos++) {
379 sample[elt.second[pos]] = sample[elt.first];
384 &working_bn->
cpt(elt.second[pos])));
393 for (
auto node : working_bn->
nodes()) {
394 auto dSize = working_bn->
variable(node).domainSize();
397 std::vector< GUM_SCALAR > var_cpt(potential->
domainSize());
399 auto pConfs = (*cpt)[node].size();
401 for (decltype(pConfs) pconf = 0; pconf < pConfs; pconf++) {
402 Size nVertices =
Size((*cpt)[node][pconf].size());
403 Idx choosen_vertex =
Idx(rand() % nVertices);
409 for (decltype(dSize) mod = 0; mod < dSize; mod++) {
410 var_cpt[pconf * dSize + mod] =
411 (*cpt)[node][pconf][choosen_vertex][mod];
424 template <
typename GUM_SCALAR,
class BNInferenceEngine >
438 if (evi_list->
size() > 0) {
439 for (
const auto pot : *evi_list)
440 inference_engine->addEvidence(*pot);
444 for (
const auto& elt : this->
_evidence) {
446 (*p) << working_bn->
variable(elt.first);
458 if (evi_list->
size() > 0) {
459 for (
const auto pot : *evi_list)
460 inference_engine->addEvidence(*pot);
aGrUM's Potential is a multi-dimensional array with tensor operators.
void __mcThreadDataCopy()
Initialize threads data.
Class representing a Bayesian Network.
std::vector< BNInferenceEngine *> _l_inferenceEngine
Threads BNInferenceEngine.
void _initThreadsData(const Size &num_threads, const bool __storeVertices, const bool __storeBNOpt)
Initialize threads data.
virtual Size domainSize() const final
Returns the product of the variables domain size.
void disableMinEpsilonRate()
Disable stopping criterion on epsilon rate.
__expes _l_expectationMin
Threads lower expectations, one per thread.
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
unsigned int getThreadNumber()
Get the calling thread id.
bool _storeBNOpt
Iterations limit stopping rule used by some algorithms such as CNMonteCarloSampling.
void __insertEvidence()
Insert CredalNet evidence into a thread BNInferenceEngine.
void _dynamicExpectations()
Rearrange lower and upper expectations to suit dynamic networks.
credalSet _marginalSets
Credal sets vertices, if enabled.
margi _marginalMin
Lower marginals.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
void _optFusion()
Fusion of threads optimal IBayesNet.
__margis _l_marginalMin
Threads lower marginals, one per thread.
void setPeriodSize(Size p)
How many samples between two stopping is enable.
void _expFusion()
Fusion of threads expectations.
virtual ~CNMonteCarloSampling()
Destructor.
void initApproximationScheme()
Initialise the scheme.
Generic doubly linked lists.
Class representing the minimal interface for Bayesian Network.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::vector< List< const Potential< GUM_SCALAR > *> *> _workingSetE
Threads evidence.
void _repetitiveInit()
Initialize _t0 and _t1 clusters.
expe _expectationMax
Upper expectations, if some variables modalities were inserted.
Size periodSize() const
Returns the period size.
void __threadInference()
Thread performs an inference using BNInferenceEngine.
cluster _t0
Clusters of nodes used with dynamic networks.
void setMaxTime(double timeout)
Stopping criterion on timeout.
CNMonteCarloSampling(const CredalNet< GUM_SCALAR > &credalNet)
Constructor.
std::vector< VarMod2BNsMap< GUM_SCALAR > *> _l_optimalNet
Threads optimal IBayesNet.
std::vector< __bnet *> _workingSet
Threads IBayesNet.
Class template representing a Credal Network.
void __threadUpdate()
Update thread data after a IBayesNet inference.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it's node id.
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
void _updateMarginals()
Fusion of threads marginals.
void makeInference()
Starts the inference.
void __mcInitApproximationScheme()
Initialize approximation Scheme.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const CredalNet< GUM_SCALAR > * _credalNet
A pointer to the Credal Net used.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src) const
copy a Potential data using name of variables and labels (not necessarily the same variables in the s...
Size size() const noexcept
Returns the number of elements in the list.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
void __verticesSampling()
Thread samples a IBayesNet from the CredalNet.
Base class for all aGrUM's exceptions.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
<agrum/CN/CNMonteCarloSampling.h>
__expes _l_expectationMax
Threads upper expectations, one per thread.
bool _repetitiveInd
True if using repetitive independence ( dynamic network only ), False otherwise.
dynExpe _modal
Variables modalities used to compute expectations.
Class for assigning/browsing values to tuples of discrete variables.
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
__clusters _l_clusters
Threads clusters.
margi _evidence
Holds observed variables states.
cluster _t1
Clusters of nodes used with dynamic networks.
expe _expectationMin
Lower expectations, if some variables modalities were inserted.
bool _updateThread(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...
void setFirst()
Assign the first values to the tuple of the Instantiation.
__modals _l_modal
Threads modalities.
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
void disableMaxIter()
Disable stopping criterion on max iterations.
Size Idx
Type for indexes.
Class template representing a CredalNet inference engine using one or more IBayesNet inference engine...
__margis _l_marginalMax
Threads upper marginals, one per thread.
bool _storeVertices
True if credal sets vertices are stored, False otherwise.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Class used to store optimum IBayesNet during some inference algorithms.
__credalSets _l_marginalSets
Threads vertices.
void __binaryRep(std::vector< bool > &toFill, const Idx value) const
Get the binary representation of a given value.
void enableMaxTime()
Enable stopping criterion on timeout.
margi _marginalMax
Upper marginals.
bool end() const
Returns true if the Instantiation reached the end.
const GUM_SCALAR _computeEpsilon()
Compute epsilon and update old marginals.
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
void enableEpsilon()
Enable stopping criterion on epsilon.