This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/clpbn/bp/CbpSolver.cpp

272 lines
8.1 KiB
C++
Raw Normal View History

2011-12-12 15:29:51 +00:00
#include "CbpSolver.h"
2012-04-10 20:43:08 +01:00
CbpSolver::CbpSolver (const FactorGraph& fg) : BpSolver (fg)
{
2012-04-16 21:45:52 +01:00
unsigned nrGroundVars, nrGroundFacs, nrNeighborless;
2012-04-10 20:43:08 +01:00
if (Constants::COLLECT_STATS) {
2012-04-16 21:45:52 +01:00
nrGroundVars = fg_->varNodes().size();
nrGroundFacs = fg_->facNodes().size();
2012-04-10 20:43:08 +01:00
const VarNodes& vars = fg_->varNodes();
2012-04-16 21:45:52 +01:00
nrNeighborless = 0;
2012-04-10 20:43:08 +01:00
for (unsigned i = 0; i < vars.size(); i++) {
const FacNodes& factors = vars[i]->neighbors();
if (factors.size() == 1 && factors[0]->neighbors().size() == 1) {
2012-04-16 21:45:52 +01:00
nrNeighborless ++;
2012-04-10 20:43:08 +01:00
}
}
}
cfg_ = new CFactorGraph (fg);
fg_ = cfg_->getGroundFactorGraph();
if (Constants::COLLECT_STATS) {
2012-04-16 21:45:52 +01:00
unsigned nrClusterVars = fg_->varNodes().size();
unsigned nrClusterFacs = fg_->facNodes().size();
Statistics::updateCompressingStatistics (nrGroundVars,
nrGroundFacs, nrClusterVars, nrClusterFacs, nrNeighborless);
2012-04-10 20:43:08 +01:00
}
2012-04-16 21:45:52 +01:00
// Util::printHeader ("Compressed Factor Graph");
// fg_->print();
// Util::printHeader ("Uncompressed Factor Graph");
// fg.print();
2012-04-10 20:43:08 +01:00
}
2011-12-12 15:29:51 +00:00
CbpSolver::~CbpSolver (void)
{
2012-04-10 20:43:08 +01:00
delete cfg_;
delete fg_;
2011-12-12 15:29:51 +00:00
for (unsigned i = 0; i < links_.size(); i++) {
delete links_[i];
}
links_.clear();
}
void
CbpSolver::printSolverFlags (void) const
{
stringstream ss;
ss << "counting bp [" ;
ss << "schedule=" ;
typedef BpOptions::Schedule Sch;
switch (BpOptions::schedule) {
case Sch::SEQ_FIXED: ss << "seq_fixed"; break;
case Sch::SEQ_RANDOM: ss << "seq_random"; break;
case Sch::PARALLEL: ss << "parallel"; break;
case Sch::MAX_RESIDUAL: ss << "max_residual"; break;
}
ss << ",max_iter=" << BpOptions::maxIter;
ss << ",accuracy=" << BpOptions::accuracy;
ss << ",log_domain=" << Util::toString (Globals::logDomain);
ss << ",order_vars=" << Util::toString (FactorGraph::orderVariables);
ss << ",chkif=" <<
Util::toString (CFactorGraph::checkForIdenticalFactors);
ss << "]" ;
cout << ss.str() << endl;
}
2012-03-22 11:33:24 +00:00
Params
2011-12-12 15:29:51 +00:00
CbpSolver::getPosterioriOf (VarId vid)
{
2012-04-10 20:43:08 +01:00
if (runned_ == false) {
runSolver();
}
assert (cfg_->getEquivalentVariable (vid));
VarNode* var = cfg_->getEquivalentVariable (vid);
2012-03-22 11:33:24 +00:00
Params probs;
2011-12-12 15:29:51 +00:00
if (var->hasEvidence()) {
2012-04-05 18:38:56 +01:00
probs.resize (var->range(), LogAware::noEvidence());
2012-03-31 23:27:37 +01:00
probs[var->getEvidence()] = LogAware::withEvidence();
2011-12-12 15:29:51 +00:00
} else {
2012-04-05 18:38:56 +01:00
probs.resize (var->range(), LogAware::multIdenty());
2011-12-12 15:29:51 +00:00
const SpLinkSet& links = ninf(var)->getLinks();
2012-03-22 11:33:24 +00:00
if (Globals::logDomain) {
2012-04-10 20:43:08 +01:00
for (unsigned i = 0; i < links.size(); i++) {
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
Util::add (probs, l->poweredMessage());
}
LogAware::normalize (probs);
Util::fromLog (probs);
2012-03-22 11:33:24 +00:00
} else {
for (unsigned i = 0; i < links.size(); i++) {
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
2012-04-10 20:43:08 +01:00
Util::multiply (probs, l->poweredMessage());
2012-03-22 11:33:24 +00:00
}
2012-03-31 23:27:37 +01:00
LogAware::normalize (probs);
2011-12-12 15:29:51 +00:00
}
}
return probs;
}
2012-03-22 11:33:24 +00:00
Params
2012-04-10 20:43:08 +01:00
CbpSolver::getJointDistributionOf (const VarIds& jointVids)
2011-12-12 15:29:51 +00:00
{
2012-03-22 11:33:24 +00:00
VarIds eqVarIds;
2012-04-10 20:43:08 +01:00
for (unsigned i = 0; i < jointVids.size(); i++) {
VarNode* vn = cfg_->getEquivalentVariable (jointVids[i]);
eqVarIds.push_back (vn->varId());
2011-12-12 15:29:51 +00:00
}
2012-04-05 23:00:48 +01:00
return BpSolver::getJointDistributionOf (eqVarIds);
2011-12-12 15:29:51 +00:00
}
void
CbpSolver::createLinks (void)
{
2012-04-16 21:45:52 +01:00
const FacClusters& fcs = cfg_->facClusters();
2011-12-12 15:29:51 +00:00
for (unsigned i = 0; i < fcs.size(); i++) {
2012-04-16 21:45:52 +01:00
const VarClusters& vcs = fcs[i]->varClusters();
2011-12-12 15:29:51 +00:00
for (unsigned j = 0; j < vcs.size(); j++) {
unsigned c = cfg_->getEdgeCount (fcs[i], vcs[j]);
2012-04-10 20:43:08 +01:00
links_.push_back (new CbpSolverLink (
2012-04-16 21:45:52 +01:00
fcs[i]->getRepresentative(),
vcs[j]->getRepresentative(), c));
2011-12-12 15:29:51 +00:00
}
}
}
void
CbpSolver::maxResidualSchedule (void)
{
if (nIters_ == 1) {
for (unsigned i = 0; i < links_.size(); i++) {
calculateMessage (links_[i]);
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
linkMap_.insert (make_pair (links_[i], it));
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 2 && Constants::DEBUG < 5) {
2011-12-12 15:29:51 +00:00
cout << "calculating " << links_[i]->toString() << endl;
}
}
return;
}
for (unsigned c = 0; c < links_.size(); c++) {
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 2) {
2011-12-12 15:29:51 +00:00
cout << endl << "current residuals:" << endl;
for (SortedOrder::iterator it = sortedOrder_.begin();
it != sortedOrder_.end(); it ++) {
cout << " " << setw (30) << left << (*it)->toString();
cout << "residual = " << (*it)->getResidual() << endl;
}
}
SortedOrder::iterator it = sortedOrder_.begin();
SpLink* link = *it;
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 2) {
2011-12-12 15:29:51 +00:00
cout << "updating " << (*sortedOrder_.begin())->toString() << endl;
}
if (link->getResidual() < BpOptions::accuracy) {
return;
}
link->updateMessage();
link->clearResidual();
sortedOrder_.erase (it);
linkMap_.find (link)->second = sortedOrder_.insert (link);
// update the messages that depend on message source --> destin
2012-04-10 11:51:56 +01:00
const FacNodes& factorNeighbors = link->getVariable()->neighbors();
2011-12-12 15:29:51 +00:00
for (unsigned i = 0; i < factorNeighbors.size(); i++) {
const SpLinkSet& links = ninf(factorNeighbors[i])->getLinks();
for (unsigned j = 0; j < links.size(); j++) {
if (links[j]->getVariable() != link->getVariable()) {
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 2 && Constants::DEBUG < 5) {
2011-12-12 15:29:51 +00:00
cout << " calculating " << links[j]->toString() << endl;
}
calculateMessage (links[j]);
SpLinkMap::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
// to a factor F depends on the message that F sent to the X
const SpLinkSet& links = ninf(link->getFactor())->getLinks();
for (unsigned i = 0; i < links.size(); i++) {
if (links[i]->getVariable() != link->getVariable()) {
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 2 && Constants::DEBUG < 5) {
2011-12-12 15:29:51 +00:00
cout << " calculating " << links[i]->toString() << endl;
}
calculateMessage (links[i]);
SpLinkMap::iterator iter = linkMap_.find (links[i]);
sortedOrder_.erase (iter->second);
iter->second = sortedOrder_.insert (links[i]);
}
}
}
}
2012-03-22 11:33:24 +00:00
Params
2011-12-12 15:29:51 +00:00
CbpSolver::getVar2FactorMsg (const SpLink* link) const
{
2012-03-22 11:33:24 +00:00
Params msg;
2012-04-05 23:00:48 +01:00
const VarNode* src = link->getVariable();
2012-04-10 11:51:56 +01:00
const FacNode* dst = link->getFactor();
2011-12-12 15:29:51 +00:00
const CbpSolverLink* l = static_cast<const CbpSolverLink*> (link);
if (src->hasEvidence()) {
2012-04-05 18:38:56 +01:00
msg.resize (src->range(), LogAware::noEvidence());
2011-12-12 15:29:51 +00:00
double value = link->getMessage()[src->getEvidence()];
2012-04-10 20:43:08 +01:00
msg[src->getEvidence()] = LogAware::pow (value, l->nrEdges() - 1);
2011-12-12 15:29:51 +00:00
} else {
msg = link->getMessage();
2012-04-10 20:43:08 +01:00
LogAware::pow (msg, l->nrEdges() - 1);
2011-12-12 15:29:51 +00:00
}
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 5) {
2012-04-16 21:45:52 +01:00
cout << " " << "init: " << msg << " " << src->hasEvidence() << endl;
2011-12-12 15:29:51 +00:00
}
const SpLinkSet& links = ninf(src)->getLinks();
2012-03-22 11:33:24 +00:00
if (Globals::logDomain) {
for (unsigned i = 0; i < links.size(); i++) {
if (links[i]->getFactor() != dst) {
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
2012-04-10 20:43:08 +01:00
Util::add (msg, l->poweredMessage());
2011-12-12 15:29:51 +00:00
}
2012-03-22 11:33:24 +00:00
}
} else {
for (unsigned i = 0; i < links.size(); i++) {
if (links[i]->getFactor() != dst) {
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
2012-04-10 20:43:08 +01:00
Util::multiply (msg, l->poweredMessage());
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 5) {
2012-03-22 11:33:24 +00:00
cout << " msg from " << l->getFactor()->getLabel() << ": " ;
2012-04-10 20:43:08 +01:00
cout << l->poweredMessage() << endl;
2011-12-12 15:29:51 +00:00
}
}
2012-03-22 11:33:24 +00:00
}
2011-12-12 15:29:51 +00:00
}
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
if (Constants::DEBUG >= 5) {
cout << " result = " << msg << endl;
2011-12-12 15:29:51 +00:00
}
return msg;
}
void
CbpSolver::printLinkInformation (void) const
{
for (unsigned i = 0; i < links_.size(); i++) {
CbpSolverLink* l = static_cast<CbpSolverLink*> (links_[i]);
cout << l->toString() << ":" << endl;
2012-03-31 23:27:37 +01:00
cout << " curr msg = " << l->getMessage() << endl;
cout << " next msg = " << l->getNextMessage() << endl;
2012-04-16 21:45:52 +01:00
cout << " nr edges = " << l->nrEdges() << endl;
2012-04-10 20:43:08 +01:00
cout << " powered = " << l->poweredMessage() << endl;
2011-12-12 15:29:51 +00:00
cout << " residual = " << l->getResidual() << endl;
}
}