27 #ifndef DOXYGEN_SHOULD_SKIP_THIS 33 # define LBP_DEFAULT_MAXITER 100 34 # define LBP_DEFAULT_EPSILON 1e-8 35 # define LBP_DEFAULT_MIN_EPSILON_RATE 1e-10 36 # define LBP_DEFAULT_PERIOD_SIZE 1 37 # define LBP_DEFAULT_VERBOSITY false 47 template <
typename GUM_SCALAR >
49 const IBayesNet< GUM_SCALAR >* bn) :
50 ApproximateInference< GUM_SCALAR >(bn) {
64 template <
typename GUM_SCALAR >
70 template <
typename GUM_SCALAR >
73 for (
const auto& tail : this->
BN().nodes()) {
74 Potential< GUM_SCALAR > p;
75 p.add(this->
BN().variable(tail));
76 p.fill(static_cast< GUM_SCALAR >(1));
78 for (
const auto& head : this->
BN().children(tail)) {
85 template <
typename GUM_SCALAR >
91 template <
typename GUM_SCALAR >
92 Potential< GUM_SCALAR >
94 const auto& varX = this->
BN().variable(X);
96 auto piX = this->
BN().cpt(X);
97 for (
const auto& U : this->
BN().parents(X)) {
100 piX = piX.margSumIn({&varX});
105 template <
typename GUM_SCALAR >
106 Potential< GUM_SCALAR >
109 const auto& varX = this->
BN().variable(X);
110 const auto& varExcept = this->
BN().variable(except);
111 auto piXexcept = this->
BN().cpt(X);
112 for (
const auto& U : this->
BN().parents(X)) {
113 if (U != except) { piXexcept *=
__messages[Arc(U, X)]; }
115 piXexcept = piXexcept.margSumIn({&varX, &varExcept});
120 template <
typename GUM_SCALAR >
121 Potential< GUM_SCALAR >
123 Potential< GUM_SCALAR > lamX;
127 lamX.add(this->
BN().variable(X));
130 for (
const auto& Y : this->
BN().children(X)) {
137 template <
typename GUM_SCALAR >
138 Potential< GUM_SCALAR >
141 Potential< GUM_SCALAR > lamXexcept;
145 lamXexcept.add(this->
BN().variable(X));
148 for (
const auto& Y : this->
BN().children(X)) {
149 if (Y != except) { lamXexcept *=
__messages[Arc(Y, X)]; }
156 template <
typename GUM_SCALAR >
165 for (
const auto& U : this->
BN().parents(X)) {
168 newLambda.normalize();
169 auto ekl =
static_cast< GUM_SCALAR
>(0);
172 }
catch (InvalidArgument&) {
173 GUM_ERROR(InvalidArgument,
"Not compatible pi during computation");
174 }
catch (FatalError&) {
175 ekl = std::numeric_limits< GUM_SCALAR >::infinity();
185 for (
const auto& Y : this->
BN().children(X)) {
191 }
catch (InvalidArgument&) {
192 GUM_ERROR(InvalidArgument,
"Not compatible pi during computation");
193 }
catch (FatalError&) {
194 ekl = std::numeric_limits< GUM_SCALAR >::infinity();
206 template <
typename GUM_SCALAR >
209 for (
const auto& node : this->
BN().topologicalOrder()) {
216 template <
typename GUM_SCALAR >
221 std::vector< NodeId > shuffleIds;
222 for (
const auto& node : this->
BN().nodes())
223 shuffleIds.push_back(node);
225 auto engine = std::default_random_engine{};
227 GUM_SCALAR error = 0.0;
229 std::shuffle(std::begin(shuffleIds), std::end(shuffleIds), engine);
231 for (
const auto& node : shuffleIds) {
233 if (e > error) error = e;
240 template <
typename GUM_SCALAR >
241 INLINE
const Potential< GUM_SCALAR >&
251 #endif // DOXYGEN_SHOULD_SKIP_THIS const NodeProperty< const Potential< GUM_SCALAR > *> & evidence() const
returns the set of evidence
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void setPeriodSize(Size p)
How many samples between two stopping is enable.
void initApproximationScheme()
Initialise the scheme.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.