62 template < GUM_Numeric GUM_SCALAR >
65 const std::string& section) :
73 * static_cast<
Size >(
74 Configuration::instance().typed<
std::int64_t >(section,
"percentDrawnSample"))
78 template < GUM_Numeric GUM_SCALAR >
87 template < GUM_Numeric GUM_SCALAR >
92 template < GUM_Numeric GUM_SCALAR >
104 template < GUM_Numeric GUM_SCALAR >
112 "The hard evidence has a null probability (incompatible with the model)")
128 std::vector< NodeId > sampled;
130 if (
hardEv_ ==
nullptr || !
hardEv_->exists(node)) sampled.push_back(node);
141 for (
const auto node: sampled)
148 "No sample with a positive probability found: the evidence may be incompatible "
152 template < GUM_Numeric GUM_SCALAR >
156 for (
const auto nod:
samplingBn_.topologicalOrder()) {
167 template < GUM_Numeric GUM_SCALAR >
170 if (
samplingBn_.cpt(node).get(I) == GUM_SCALAR(0))
return false;
174 template < GUM_Numeric GUM_SCALAR >
177 GUM_SCALAR epsilon) {
182 auto smoothed = [
this, epsilon](
NodeId node, Tensor< GUM_SCALAR > t) {
183 const GUM_SCALAR replacement = epsilon *
_minNonZero_[node];
186 if (t[J] == GUM_SCALAR(0)) t.set(J, replacement);
191 p *= smoothed(nod,
samplingBn_.cpt(nod).extract(Itop));
197 template < GUM_Numeric GUM_SCALAR >
201 const auto domainSize =
samplingBn_.variable(node).domainSize();
203 std::vector< bool > allowed(domainSize,
false);
204 allowed[(*hardEv_)[node]] =
true;
205 _allowed_.insert(node, std::move(allowed));
207 _allowed_.insert(node, std::vector< bool >(domainSize,
true));
214 std::vector< NodeId > queue;
216 if (
samplingBn_.cpt(node).min() == GUM_SCALAR(0)) {
219 queue.push_back(node);
222 while (!queue.empty()) {
223 const NodeId f = queue.back();
229 const auto& vars = cpt.variablesSequence();
230 std::vector< NodeId > ids;
231 std::vector< std::vector< bool > > supported;
232 for (
const auto var: vars) {
234 supported.emplace_back(var->domainSize(),
false);
238 if (cpt[I] == GUM_SCALAR(0))
continue;
240 for (
Idx k = 0; k < ids.size() && allowed; ++k)
243 for (
Idx k = 0; k < ids.size(); ++k)
244 supported[k][I.
val(k)] =
true;
248 for (
Idx k = 0; k < ids.size(); ++k) {
250 bool changed =
false;
252 for (
Idx val = 0; val < allowed.size(); ++val) {
253 if (allowed[val] && !supported[k][val]) {
254 allowed[val] =
false;
257 if (allowed[val]) empty =
false;
259 if (empty)
return false;
262 auto revise = [&](
NodeId g) {
269 for (
const auto child:
samplingBn_.children(ids[k]))
277 template < GUM_Numeric GUM_SCALAR >
279 const auto allowed =
_allowed_.tryGet(node);
280 if (!allowed)
return;
283 if (!(*allowed)[J.
val(0)]) p.set(J, GUM_SCALAR(0));
286 template < GUM_Numeric GUM_SCALAR >
293 if (p.sum() == GUM_SCALAR(0)) p =
samplingBn_.cpt(nod).extract(Itop);
298 template < GUM_Numeric GUM_SCALAR >
300 for (
Idx i = 0; i <
nbr_; i++) {
311 template < GUM_Numeric GUM_SCALAR >
318 GUM_ASSERT(p.
nbrDim() == 1);
325 template < GUM_Numeric GUM_SCALAR >
330 template < GUM_Numeric GUM_SCALAR >
335 template < GUM_Numeric GUM_SCALAR >
340 template < GUM_Numeric GUM_SCALAR >
Sectioned key/value configuration with typed and documented keys.
NodeProperty< std::vector< bool > > _allowed_
the values of each node that may belong to a sample with a positive probability
void setDrawnAtRandom(bool atRandom)
void _smoothedGibbsSample_(NodeId id, Instantiation *I, GUM_SCALAR epsilon)
Size nbrDrawnVar() const
Getters and setters.
void _drawVarMonteCarlo_(NodeId nod, Instantiation *I)
static constexpr Size _nbrSweepsBySmoothingLevel_
maximal number of sweeps (over the non-evidence nodes) for each smoothing level
void setNbrDrawnVar(Size nbr)
bool _isCompatible_(const Instantiation &I) const
static constexpr std::array< double, 4 > _smoothingLevels_
growing smoothing levels: a null probability is replaced by epsilon times the smallest positive proba...
const IBayesNet< GUM_SCALAR > & samplingBn_
void _GibbsSample_(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
Instantiation nextSample(Instantiation prev)
draws next sample of Gibbs sampling
Instantiation _forwardSample_()
Sequence< NodeId > samplingNodes_
Instantiation monteCarloSample()
draws a first sample with a positive probability (starting point of a Gibbs chain)
const NodeProperty< Idx > * hardEv_
static constexpr Size _nbrSmoothingRestarts_
number of restarts (from a new forward sample) of the smoothed Gibbs moves
bool isDrawnAtRandom() const
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
void _mask_(NodeId node, Tensor< GUM_SCALAR > &p) const
virtual ~GibbsOperator()
Destructor.
static constexpr Size _nbrRejectionTries_
number of forward samples drawn before searching a compatible sample by smoothed Gibbs moves
void updateSamplingNodes_()
NodeProperty< GUM_SCALAR > _minNonZero_
the smallest positive probability of the CPT of each node (for the smoothed Gibbs moves)
Class representing the minimal interface for Bayesian network with no numerical data.
Exception : several evidence are incompatible together (proba=0).
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
bool end() const
Returns true if the Instantiation reached the end.
void inc()
Operator increment.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
Exception: at least one argument passed to a function is not what was expected.
Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
Exception : operation not allowed.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
void insert(const Key &k)
Inserts a new element into the set.
void erase(const Key &k)
Erases an element from the set.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Idx draw() const
get a value at random from a 1-D distribution
Tensor< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Tensor extracted from *this given a partial instantiation
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
GUM_SCALAR sum() const
sum of all elements in the Tensor
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
This file contains Gibbs sampling (for BNs) class definitions.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
GUM_SHARED_PUBLIC Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
gum is the global namespace for all aGrUM entities
Contains useful methods for random stuff.