24 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 # define LBP_DEFAULT_MAXITER 100 31 # define LBP_DEFAULT_EPSILON 1e-8 32 # define LBP_DEFAULT_MIN_EPSILON_RATE 1e-10 33 # define LBP_DEFAULT_PERIOD_SIZE 1 34 # define LBP_DEFAULT_VERBOSITY false 44 template <
typename GUM_SCALAR >
46 const IBayesNet< GUM_SCALAR >* bn) :
47 ApproximateInference< GUM_SCALAR >(bn) {
61 template <
typename GUM_SCALAR >
67 template <
typename GUM_SCALAR >
70 for (
const auto& tail : this->
BN().nodes()) {
71 Potential< GUM_SCALAR > p;
72 p.add(this->
BN().variable(tail));
73 p.fill(static_cast< GUM_SCALAR >(1));
75 for (
const auto& head : this->
BN().children(tail)) {
82 template <
typename GUM_SCALAR >
88 template <
typename GUM_SCALAR >
89 Potential< GUM_SCALAR >
91 const auto& varX = this->
BN().variable(X);
93 auto piX = this->
BN().cpt(X);
94 for (
const auto& U : this->
BN().parents(X)) {
97 piX = piX.margSumIn({&varX});
102 template <
typename GUM_SCALAR >
103 Potential< GUM_SCALAR >
106 const auto& varX = this->
BN().variable(X);
107 const auto& varExcept = this->
BN().variable(except);
108 auto piXexcept = this->
BN().cpt(X);
109 for (
const auto& U : this->
BN().parents(X)) {
110 if (U != except) { piXexcept *=
__messages[Arc(U, X)]; }
112 piXexcept = piXexcept.margSumIn({&varX, &varExcept});
117 template <
typename GUM_SCALAR >
118 Potential< GUM_SCALAR >
120 Potential< GUM_SCALAR > lamX;
124 lamX.add(this->
BN().variable(X));
127 for (
const auto& Y : this->
BN().children(X)) {
134 template <
typename GUM_SCALAR >
135 Potential< GUM_SCALAR >
138 Potential< GUM_SCALAR > lamXexcept;
142 lamXexcept.add(this->
BN().variable(X));
145 for (
const auto& Y : this->
BN().children(X)) {
146 if (Y != except) { lamXexcept *=
__messages[Arc(Y, X)]; }
153 template <
typename GUM_SCALAR >
162 for (
const auto& U : this->
BN().parents(X)) {
165 newLambda.normalize();
166 auto ekl =
static_cast< GUM_SCALAR
>(0);
169 }
catch (InvalidArgument&) {
170 GUM_ERROR(InvalidArgument,
"Not compatible pi during computation");
171 }
catch (FatalError&) {
172 ekl = std::numeric_limits< GUM_SCALAR >::infinity();
182 for (
const auto& Y : this->
BN().children(X)) {
188 }
catch (InvalidArgument&) {
189 GUM_ERROR(InvalidArgument,
"Not compatible pi during computation");
190 }
catch (FatalError&) {
191 ekl = std::numeric_limits< GUM_SCALAR >::infinity();
203 template <
typename GUM_SCALAR >
206 for (
const auto& node : this->
BN().topologicalOrder()) {
213 template <
typename GUM_SCALAR >
218 std::vector< NodeId > shuffleIds;
219 for (
const auto& node : this->
BN().nodes())
220 shuffleIds.push_back(node);
222 auto engine = std::default_random_engine{};
224 GUM_SCALAR error = 0.0;
226 std::shuffle(std::begin(shuffleIds), std::end(shuffleIds), engine);
228 for (
const auto& node : shuffleIds) {
230 if (e > error) error = e;
237 template <
typename GUM_SCALAR >
238 INLINE
const Potential< GUM_SCALAR >&
248 #endif // DOXYGEN_SHOULD_SKIP_THIS const NodeProperty< const Potential< GUM_SCALAR > *> & evidence() const
returns the set of evidence
This file contains gibbs sampling (for BNs) class definitions.
void setPeriodSize(Size p)
How many samples between two stopping is enable.
void initApproximationScheme()
Initialise the scheme.
gum is the global namespace for all aGrUM entities
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
Potential< GUM_SCALAR > __computeProdLambda(NodeId X)
void setVerbosity(bool v)
Set the verbosity on (true) or off (false).
virtual ~LoopyBeliefPropagation()
Destructor.
GUM_SCALAR __updateNodeMessage(NodeId X)
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
LoopyBeliefPropagation(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
KL is the base class for KL computation betweens 2 BNs.
ArcProperty< Potential< GUM_SCALAR > > __messages
virtual const Potential< GUM_SCALAR > & _posterior(NodeId id)
asks derived classes for the posterior of a given variable
void setMaxIter(Size max)
Stopping criterion on number of iterations.
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
NodeProperty< Potential< GUM_SCALAR > > __posteriors
virtual void _makeInference()
called when the inference has to be performed effectively
Potential< GUM_SCALAR > __computeProdPi(NodeId X)
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)
virtual void _updateOutdatedBNStructure()
prepares inference when the latter is in OutdatedBNStructure state
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.