2011-02-23 15:31:56 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "BpNode.h"
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
bool BpNode::calculateMessageResidual_ = true;
|
2011-02-23 15:31:56 +00:00
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
BpNode::BpNode (BayesNode* node)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
ds_ = node->getDomainSize();
|
|
|
|
const NodeSet& childs = node->getChilds();
|
|
|
|
piVals_.resize (ds_, 1);
|
|
|
|
ldVals_.resize (ds_, 1);
|
|
|
|
if (calculateMessageResidual_) {
|
|
|
|
piResiduals_.resize (childs.size(), 0.0);
|
|
|
|
ldResiduals_.resize (childs.size(), 0.0);
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
2011-05-17 12:00:33 +01:00
|
|
|
childs_ = &childs;
|
|
|
|
for (unsigned i = 0; i < childs.size(); i++) {
|
|
|
|
//indexMap_.insert (make_pair (childs[i]->getVarId(), i));
|
|
|
|
currPiMsgs_.push_back (ParamSet (ds_, 1));
|
|
|
|
currLdMsgs_.push_back (ParamSet (ds_, 1));
|
|
|
|
nextPiMsgs_.push_back (ParamSet (ds_, 1));
|
|
|
|
nextLdMsgs_.push_back (ParamSet (ds_, 1));
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
ParamSet
|
|
|
|
BpNode::getBeliefs (void) const
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
double sum = 0.0;
|
|
|
|
ParamSet beliefs (ds_);
|
|
|
|
for (int xi = 0; xi < ds_; xi++) {
|
|
|
|
double prod = piVals_[xi] * ldVals_[xi];
|
|
|
|
beliefs[xi] = prod;
|
|
|
|
sum += prod;
|
|
|
|
}
|
|
|
|
assert (sum);
|
|
|
|
//normalize the beliefs
|
|
|
|
for (int xi = 0; xi < ds_; xi++) {
|
|
|
|
beliefs[xi] /= sum;
|
|
|
|
}
|
|
|
|
return beliefs;
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
double
|
|
|
|
BpNode::getPiValue (int idx) const
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
assert (idx >=0 && idx < ds_);
|
|
|
|
return piVals_[idx];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
void
|
|
|
|
BpNode::setPiValue (int idx, double value)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
assert (idx >=0 && idx < ds_);
|
|
|
|
piVals_[idx] = value;
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double
|
2011-05-17 12:00:33 +01:00
|
|
|
BpNode::getLambdaValue (int idx) const
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
assert (idx >=0 && idx < ds_);
|
|
|
|
return ldVals_[idx];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-17 12:00:33 +01:00
|
|
|
BpNode::setLambdaValue (int idx, double value)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
assert (idx >=0 && idx < ds_);
|
|
|
|
ldVals_[idx] = value;
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
ParamSet&
|
|
|
|
BpNode::getPiValues (void)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
return piVals_;
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
ParamSet&
|
|
|
|
BpNode::getLambdaValues (void)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
return ldVals_;
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
double
|
|
|
|
BpNode::getPiMessageValue (const BayesNode* destination, int idx) const
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
assert (idx >=0 && idx < ds_);
|
|
|
|
return currPiMsgs_[getIndex(destination)][idx];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
double
|
|
|
|
BpNode::getLambdaMessageValue (const BayesNode* source, int idx) const
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
assert (idx >=0 && idx < ds_);
|
|
|
|
return currLdMsgs_[getIndex(source)][idx];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
const ParamSet&
|
|
|
|
BpNode::getPiMessage (const BayesNode* destination) const
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
return currPiMsgs_[getIndex(destination)];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
const ParamSet&
|
|
|
|
BpNode::getLambdaMessage (const BayesNode* source) const
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
return currLdMsgs_[getIndex(source)];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
ParamSet&
|
|
|
|
BpNode::piNextMessageReference (const BayesNode* destination)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
return nextPiMsgs_[getIndex(destination)];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
ParamSet&
|
|
|
|
BpNode::lambdaNextMessageReference (const BayesNode* source)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
return nextLdMsgs_[getIndex(source)];
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
void
|
|
|
|
BpNode::updatePiMessage (const BayesNode* destination)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
int idx = getIndex (destination);
|
|
|
|
currPiMsgs_[idx] = nextPiMsgs_[idx];
|
|
|
|
Util::normalize (currPiMsgs_[idx]);
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-17 12:00:33 +01:00
|
|
|
void
|
|
|
|
BpNode::updateLambdaMessage (const BayesNode* source)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
int idx = getIndex (source);
|
|
|
|
currLdMsgs_[idx] = nextLdMsgs_[idx];
|
|
|
|
Util::normalize (currLdMsgs_[idx]);
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double
|
|
|
|
BpNode::getBeliefChange (void)
|
|
|
|
{
|
|
|
|
double change = 0.0;
|
2011-05-17 12:00:33 +01:00
|
|
|
if (oldBeliefs_.size() == 0) {
|
2011-02-23 15:31:56 +00:00
|
|
|
oldBeliefs_ = getBeliefs();
|
2011-05-17 12:00:33 +01:00
|
|
|
change = 9999999999.0;
|
2011-02-23 15:31:56 +00:00
|
|
|
} else {
|
2011-05-17 12:00:33 +01:00
|
|
|
ParamSet currentBeliefs = getBeliefs();
|
|
|
|
for (int xi = 0; xi < ds_; xi++) {
|
2011-02-23 15:31:56 +00:00
|
|
|
change += abs (currentBeliefs[xi] - oldBeliefs_[xi]);
|
|
|
|
}
|
|
|
|
oldBeliefs_ = currentBeliefs;
|
|
|
|
}
|
|
|
|
return change;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-17 12:00:33 +01:00
|
|
|
BpNode::updatePiResidual (const BayesNode* destination)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
int idx = getIndex (destination);
|
|
|
|
Util::normalize (nextPiMsgs_[idx]);
|
|
|
|
//piResiduals_[idx] = Util::getL1dist (
|
|
|
|
// currPiMsgs_[idx], nextPiMsgs_[idx]);
|
|
|
|
piResiduals_[idx] = Util::getMaxNorm (
|
|
|
|
currPiMsgs_[idx], nextPiMsgs_[idx]);
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-17 12:00:33 +01:00
|
|
|
BpNode::updateLambdaResidual (const BayesNode* source)
|
2011-02-23 15:31:56 +00:00
|
|
|
{
|
2011-05-17 12:00:33 +01:00
|
|
|
int idx = getIndex (source);
|
|
|
|
Util::normalize (nextLdMsgs_[idx]);
|
|
|
|
//ldResiduals_[idx] = Util::getL1dist (
|
|
|
|
// currLdMsgs_[idx], nextLdMsgs_[idx]);
|
|
|
|
ldResiduals_[idx] = Util::getMaxNorm (
|
|
|
|
currLdMsgs_[idx], nextLdMsgs_[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BpNode::clearPiResidual (const BayesNode* destination)
|
|
|
|
{
|
|
|
|
piResiduals_[getIndex(destination)] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BpNode::clearLambdaResidual (const BayesNode* source)
|
|
|
|
{
|
|
|
|
ldResiduals_[getIndex(source)] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BpNode::hasReceivedChildInfluence (void) const
|
|
|
|
{
|
|
|
|
// if all lambda values are equal, then neither
|
|
|
|
// this node neither its descendents have evidence,
|
|
|
|
// we can use this to don't send lambda messages his parents
|
|
|
|
bool childInfluenced = false;
|
|
|
|
for (int xi = 1; xi < ds_; xi++) {
|
|
|
|
if (ldVals_[xi] != ldVals_[0]) {
|
|
|
|
childInfluenced = true;
|
|
|
|
break;
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-17 12:00:33 +01:00
|
|
|
return childInfluenced;
|
2011-02-23 15:31:56 +00:00
|
|
|
}
|
|
|
|
|