2013-02-07 20:09:10 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <iostream>
|
2013-02-16 16:42:08 +00:00
|
|
|
#include <iomanip>
|
2013-02-07 20:09:10 +00:00
|
|
|
|
2012-06-12 16:29:57 +01:00
|
|
|
#include "WeightedBp.h"
|
2012-05-31 21:24:15 +01:00
|
|
|
|
2013-02-16 16:42:08 +00:00
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2012-05-31 21:24:15 +01:00
|
|
|
|
2013-02-20 23:59:03 +00:00
|
|
|
WeightedBp::WeightedBp (
|
|
|
|
const FactorGraph& fg,
|
|
|
|
const std::vector<std::vector<unsigned>>& weights)
|
|
|
|
: BeliefProp (fg), weights_(weights)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
WeightedBp::~WeightedBp()
|
2012-05-31 21:24:15 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < links_.size(); i++) {
|
|
|
|
delete links_[i];
|
|
|
|
}
|
|
|
|
links_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Params
|
2012-06-12 16:29:57 +01:00
|
|
|
WeightedBp::getPosterioriOf (VarId vid)
|
2012-05-31 21:24:15 +01:00
|
|
|
{
|
|
|
|
if (runned_ == false) {
|
|
|
|
runSolver();
|
|
|
|
}
|
|
|
|
VarNode* var = fg.getVarNode (vid);
|
2012-12-27 12:54:58 +00:00
|
|
|
assert (var);
|
2012-05-31 21:24:15 +01:00
|
|
|
Params probs;
|
|
|
|
if (var->hasEvidence()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
probs.resize (var->range(), LogAware::noEvidence());
|
|
|
|
probs[var->getEvidence()] = LogAware::withEvidence();
|
2012-05-31 21:24:15 +01:00
|
|
|
} else {
|
2013-02-08 21:01:53 +00:00
|
|
|
probs.resize (var->range(), LogAware::multIdenty());
|
2013-03-09 16:41:53 +00:00
|
|
|
const BpLinks& links = getLinks (var);
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::logDomain) {
|
2012-05-31 21:24:15 +01:00
|
|
|
for (size_t i = 0; i < links.size(); i++) {
|
|
|
|
WeightedLink* l = static_cast<WeightedLink*> (links[i]);
|
|
|
|
probs += l->powMessage();
|
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
LogAware::normalize (probs);
|
|
|
|
Util::exp (probs);
|
2012-05-31 21:24:15 +01:00
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < links.size(); i++) {
|
|
|
|
WeightedLink* l = static_cast<WeightedLink*> (links[i]);
|
|
|
|
probs *= l->powMessage();
|
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
LogAware::normalize (probs);
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return probs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-20 23:59:03 +00:00
|
|
|
WeightedBp::WeightedLink::WeightedLink (
|
|
|
|
FacNode* fn,
|
|
|
|
VarNode* vn,
|
|
|
|
size_t idx,
|
|
|
|
unsigned weight)
|
|
|
|
: BpLink (fn, vn), index_(idx), weight_(weight),
|
|
|
|
pwdMsg_(vn->range(), LogAware::one())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-31 21:24:15 +01:00
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
WeightedBp::createLinks()
|
2012-05-31 21:24:15 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 0) {
|
2012-05-31 21:24:15 +01:00
|
|
|
cout << "compressed factor graph contains " ;
|
|
|
|
cout << fg.nrVarNodes() << " variables and " ;
|
|
|
|
cout << fg.nrFacNodes() << " factors " << endl;
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
const FacNodes& facNodes = fg.facNodes();
|
|
|
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
|
|
|
const VarNodes& neighs = facNodes[i]->neighbors();
|
|
|
|
for (size_t j = 0; j < neighs.size(); j++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 1) {
|
2012-05-31 21:24:15 +01:00
|
|
|
cout << "creating link " ;
|
|
|
|
cout << facNodes[i]->getLabel();
|
|
|
|
cout << " -- " ;
|
|
|
|
cout << neighs[j]->label();
|
|
|
|
cout << " idx=" << j << ", weight=" << weights_[i][j] << endl;
|
|
|
|
}
|
|
|
|
links_.push_back (new WeightedLink (
|
|
|
|
facNodes[i], neighs[j], j, weights_[i][j]));
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 1) {
|
2012-05-31 21:24:15 +01:00
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
WeightedBp::maxResidualSchedule()
|
2012-05-31 21:24:15 +01:00
|
|
|
{
|
|
|
|
if (nIters_ == 1) {
|
|
|
|
for (size_t i = 0; i < links_.size(); i++) {
|
|
|
|
calculateMessage (links_[i]);
|
|
|
|
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
|
|
|
|
linkMap_.insert (make_pair (links_[i], it));
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity >= 1) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << "calculating " << links_[i]->toString() << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t c = 0; c < links_.size(); c++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 1) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << std::endl << "current residuals:" << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
for (SortedOrder::iterator it = sortedOrder_.begin();
|
|
|
|
it != sortedOrder_.end(); ++it) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " " << std::setw (30) << std::left;
|
|
|
|
std::cout << (*it)->toString();
|
|
|
|
std::cout << "residual = " << (*it)->residual() << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SortedOrder::iterator it = sortedOrder_.begin();
|
|
|
|
BpLink* link = *it;
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity >= 1) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << "updating " << (*sortedOrder_.begin())->toString();
|
|
|
|
std::cout << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
2012-12-27 15:44:40 +00:00
|
|
|
if (link->residual() < accuracy_) {
|
2012-05-31 21:24:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
link->updateMessage();
|
|
|
|
link->clearResidual();
|
|
|
|
sortedOrder_.erase (it);
|
|
|
|
linkMap_.find (link)->second = sortedOrder_.insert (link);
|
|
|
|
|
|
|
|
// update the messages that depend on message source --> destin
|
|
|
|
const FacNodes& factorNeighbors = link->varNode()->neighbors();
|
|
|
|
for (size_t i = 0; i < factorNeighbors.size(); i++) {
|
2013-03-09 16:41:53 +00:00
|
|
|
const BpLinks& links = getLinks (factorNeighbors[i]);
|
2012-05-31 21:24:15 +01:00
|
|
|
for (size_t j = 0; j < links.size(); j++) {
|
|
|
|
if (links[j]->varNode() != link->varNode()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 1) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " calculating " << links[j]->toString();
|
|
|
|
std::cout << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
calculateMessage (links[j]);
|
|
|
|
BpLinkMap::iterator iter = linkMap_.find (links[j]);
|
|
|
|
sortedOrder_.erase (iter->second);
|
|
|
|
iter->second = sortedOrder_.insert (links[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// in counting bp, the message that a variable X sends to
|
2012-12-20 23:19:10 +00:00
|
|
|
// to a factor F depends on the message that F sent to the X
|
2013-03-09 16:41:53 +00:00
|
|
|
const BpLinks& links = getLinks (link->facNode());
|
2012-05-31 21:24:15 +01:00
|
|
|
for (size_t i = 0; i < links.size(); i++) {
|
|
|
|
if (links[i]->varNode() != link->varNode()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 1) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " calculating " << links[i]->toString();
|
|
|
|
std::cout << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
calculateMessage (links[i]);
|
|
|
|
BpLinkMap::iterator iter = linkMap_.find (links[i]);
|
|
|
|
sortedOrder_.erase (iter->second);
|
|
|
|
iter->second = sortedOrder_.insert (links[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-06-12 16:29:57 +01:00
|
|
|
WeightedBp::calcFactorToVarMsg (BpLink* _link)
|
2012-05-31 21:24:15 +01:00
|
|
|
{
|
|
|
|
WeightedLink* link = static_cast<WeightedLink*> (_link);
|
|
|
|
FacNode* src = link->facNode();
|
|
|
|
const VarNode* dst = link->varNode();
|
2013-03-09 16:41:53 +00:00
|
|
|
const BpLinks& links = getLinks (src);
|
2012-05-31 21:24:15 +01:00
|
|
|
// calculate the product of messages that were sent
|
|
|
|
// to factor `src', except from var `dst'
|
|
|
|
unsigned reps = 1;
|
2013-02-08 21:01:53 +00:00
|
|
|
unsigned msgSize = Util::sizeExpected (src->factor().ranges());
|
|
|
|
Params msgProduct (msgSize, LogAware::multIdenty());
|
|
|
|
if (Globals::logDomain) {
|
2012-05-31 21:24:15 +01:00
|
|
|
for (size_t i = links.size(); i-- > 0; ) {
|
2012-05-31 23:06:53 +01:00
|
|
|
const WeightedLink* l = static_cast<const WeightedLink*> (links[i]);
|
|
|
|
if ( ! (l->varNode() == dst && l->index() == link->index())) {
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " message from " << links[i]->varNode()->label();
|
|
|
|
std::cout << ": " ;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
|
2012-05-31 21:24:15 +01:00
|
|
|
reps, std::plus<double>());
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
reps *= links[i]->varNode()->range();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (size_t i = links.size(); i-- > 0; ) {
|
2012-05-31 23:06:53 +01:00
|
|
|
const WeightedLink* l = static_cast<const WeightedLink*> (links[i]);
|
|
|
|
if ( ! (l->varNode() == dst && l->index() == link->index())) {
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " message from " << links[i]->varNode()->label();
|
|
|
|
std::cout << ": " ;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
|
2012-05-31 21:24:15 +01:00
|
|
|
reps, std::multiplies<double>());
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
reps *= links[i]->varNode()->range();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Factor result (src->factor().arguments(),
|
|
|
|
src->factor().ranges(), msgProduct);
|
|
|
|
assert (msgProduct.size() == src->factor().size());
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::logDomain) {
|
2012-05-31 21:24:15 +01:00
|
|
|
result.params() += src->factor().params();
|
|
|
|
} else {
|
|
|
|
result.params() *= src->factor().params();
|
|
|
|
}
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " message product: " ;
|
|
|
|
std::cout << msgProduct << std::endl;
|
|
|
|
std::cout << " original factor: " ;
|
|
|
|
std::cout << src->factor().params() << std::endl;
|
|
|
|
std::cout << " factor product: " ;
|
|
|
|
std::cout << result.params() << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
result.sumOutAllExceptIndex (link->index());
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " marginalized: " ;
|
|
|
|
std::cout << result.params() << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
link->nextMessage() = result.params();
|
2013-02-08 21:01:53 +00:00
|
|
|
LogAware::normalize (link->nextMessage());
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " curr msg: " ;
|
|
|
|
std::cout << link->message() << std::endl;
|
|
|
|
std::cout << " next msg: " ;
|
|
|
|
std::cout << link->nextMessage() << std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Params
|
2013-03-09 16:41:53 +00:00
|
|
|
WeightedBp::getVarToFactorMsg (const BpLink* _link)
|
2012-05-31 21:24:15 +01:00
|
|
|
{
|
|
|
|
const WeightedLink* link = static_cast<const WeightedLink*> (_link);
|
|
|
|
const VarNode* src = link->varNode();
|
|
|
|
const FacNode* dst = link->facNode();
|
|
|
|
Params msg;
|
|
|
|
if (src->hasEvidence()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
msg.resize (src->range(), LogAware::noEvidence());
|
2012-05-31 21:24:15 +01:00
|
|
|
double value = link->message()[src->getEvidence()];
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2012-05-31 21:24:15 +01:00
|
|
|
msg[src->getEvidence()] = value;
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << msg << "^" << link->weight() << "-1" ;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
msg[src->getEvidence()] = LogAware::pow (value, link->weight() - 1);
|
2012-05-31 21:24:15 +01:00
|
|
|
} else {
|
|
|
|
msg = link->message();
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << msg << "^" << link->weight() << "-1" ;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
LogAware::pow (msg, link->weight() - 1);
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
2013-03-09 16:41:53 +00:00
|
|
|
const BpLinks& links = getLinks (src);
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::logDomain) {
|
2012-05-31 21:24:15 +01:00
|
|
|
for (size_t i = 0; i < links.size(); i++) {
|
2012-05-31 23:06:53 +01:00
|
|
|
WeightedLink* l = static_cast<WeightedLink*> (links[i]);
|
|
|
|
if ( ! (l->facNode() == dst && l->index() == link->index())) {
|
|
|
|
msg += l->powMessage();
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < links.size(); i++) {
|
2012-05-31 23:06:53 +01:00
|
|
|
WeightedLink* l = static_cast<WeightedLink*> (links[i]);
|
|
|
|
if ( ! (l->facNode() == dst && l->index() == link->index())) {
|
|
|
|
msg *= l->powMessage();
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " x " << l->nextMessage() << "^" << link->weight();
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-13 18:54:15 +00:00
|
|
|
if (Constants::showBpCalcs) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " = " << msg;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
WeightedBp::printLinkInformation() const
|
2012-05-31 21:24:15 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
for (size_t i = 0; i < links_.size(); i++) {
|
2012-09-30 22:04:44 +01:00
|
|
|
WeightedLink* l = static_cast<WeightedLink*> (links_[i]);
|
2012-05-31 23:06:53 +01:00
|
|
|
cout << l->toString() << ":" << endl;
|
|
|
|
cout << " curr msg = " << l->message() << endl;
|
|
|
|
cout << " next msg = " << l->nextMessage() << endl;
|
|
|
|
cout << " pow msg = " << l->powMessage() << endl;
|
|
|
|
cout << " index = " << l->index() << endl;
|
|
|
|
cout << " weight = " << l->weight() << endl;
|
|
|
|
cout << " residual = " << l->residual() << endl;
|
2012-05-31 21:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|