some renamings

This commit is contained in:
Tiago Gomes 2012-05-28 19:41:24 +01:00
parent 64b53e8180
commit 62283f353c
4 changed files with 98 additions and 104 deletions

View File

@ -85,13 +85,13 @@ BpSolver::getPosterioriOf (VarId vid)
const SpLinkSet& links = ninf(var)->getLinks();
if (Globals::logDomain) {
for (size_t i = 0; i < links.size(); i++) {
probs += links[i]->getMessage();
probs += links[i]->message();
}
LogAware::normalize (probs);
Util::exp (probs);
} else {
for (size_t i = 0; i < links.size(); i++) {
probs *= links[i]->getMessage();
probs *= links[i]->message();
}
LogAware::normalize (probs);
}
@ -122,9 +122,9 @@ BpSolver::getJointDistributionOf (const VarIds& jointVarIds)
Factor res (facNodes[idx]->factor());
const SpLinkSet& links = ninf(facNodes[idx])->getLinks();
for (size_t i = 0; i < links.size(); i++) {
Factor msg ({links[i]->getVariable()->varId()},
{links[i]->getVariable()->range()},
getVar2FactorMsg (links[i]));
Factor msg ({links[i]->varNode()->varId()},
{links[i]->varNode()->range()},
getVarToFactorMsg (links[i]));
res.multiply (msg);
}
res.sumOutAllExcept (jointVarIds);
@ -152,7 +152,7 @@ BpSolver::runSolver (void)
}
switch (BpOptions::schedule) {
case BpOptions::Schedule::SEQ_RANDOM:
random_shuffle (links_.begin(), links_.end());
std::random_shuffle (links_.begin(), links_.end());
// no break
case BpOptions::Schedule::SEQ_FIXED:
for (size_t i = 0; i < links_.size(); i++) {
@ -219,13 +219,13 @@ BpSolver::maxResidualSchedule (void)
for (SortedOrder::iterator it = sortedOrder_.begin();
it != sortedOrder_.end(); ++it) {
cout << " " << setw (30) << left << (*it)->toString();
cout << "residual = " << (*it)->getResidual() << endl;
cout << "residual = " << (*it)->residual() << endl;
}
}
SortedOrder::iterator it = sortedOrder_.begin();
SpLink* link = *it;
if (link->getResidual() < BpOptions::accuracy) {
if (link->residual() < BpOptions::accuracy) {
return;
}
updateMessage (link);
@ -234,12 +234,12 @@ BpSolver::maxResidualSchedule (void)
linkMap_.find (link)->second = sortedOrder_.insert (link);
// update the messages that depend on message source --> destin
const FacNodes& factorNeighbors = link->getVariable()->neighbors();
const FacNodes& factorNeighbors = link->varNode()->neighbors();
for (size_t i = 0; i < factorNeighbors.size(); i++) {
if (factorNeighbors[i] != link->getFactor()) {
if (factorNeighbors[i] != link->facNode()) {
const SpLinkSet& links = ninf(factorNeighbors[i])->getLinks();
for (size_t j = 0; j < links.size(); j++) {
if (links[j]->getVariable() != link->getVariable()) {
if (links[j]->varNode() != link->varNode()) {
calculateMessage (links[j]);
SpLinkMap::iterator iter = linkMap_.find (links[j]);
sortedOrder_.erase (iter->second);
@ -257,51 +257,51 @@ BpSolver::maxResidualSchedule (void)
void
BpSolver::calculateFactor2VariableMsg (SpLink* link)
BpSolver::calcFactorToVarMsg (SpLink* link)
{
FacNode* src = link->getFactor();
const VarNode* dst = link->getVariable();
FacNode* src = link->facNode();
const VarNode* dst = link->varNode();
const SpLinkSet& links = ninf(src)->getLinks();
// calculate the product of messages that were sent
// to factor `src', except from var `dst'
unsigned msgSize = 1;
for (size_t i = 0; i < links.size(); i++) {
msgSize *= links[i]->getVariable()->range();
msgSize *= links[i]->varNode()->range();
}
unsigned repetitions = 1;
Params msgProduct (msgSize, LogAware::multIdenty());
if (Globals::logDomain) {
for (size_t i = links.size(); i-- > 0; ) {
if (links[i]->getVariable() != dst) {
if (links[i]->varNode() != dst) {
if (Constants::SHOW_BP_CALCS) {
cout << " message from " << links[i]->getVariable()->label();
cout << " message from " << links[i]->varNode()->label();
cout << ": " ;
}
Util::add (msgProduct, getVar2FactorMsg (links[i]), repetitions);
repetitions *= links[i]->getVariable()->range();
Util::add (msgProduct, getVarToFactorMsg (links[i]), repetitions);
repetitions *= links[i]->varNode()->range();
if (Constants::SHOW_BP_CALCS) {
cout << endl;
}
} else {
unsigned range = links[i]->getVariable()->range();
unsigned range = links[i]->varNode()->range();
Util::add (msgProduct, Params (range, 0.0), repetitions);
repetitions *= range;
}
}
} else {
for (size_t i = links.size(); i-- > 0; ) {
if (links[i]->getVariable() != dst) {
if (links[i]->varNode() != dst) {
if (Constants::SHOW_BP_CALCS) {
cout << " message from " << links[i]->getVariable()->label();
cout << " message from " << links[i]->varNode()->label();
cout << ": " ;
}
Util::multiply (msgProduct, getVar2FactorMsg (links[i]), repetitions);
repetitions *= links[i]->getVariable()->range();
Util::multiply (msgProduct, getVarToFactorMsg (links[i]), repetitions);
repetitions *= links[i]->varNode()->range();
if (Constants::SHOW_BP_CALCS) {
cout << endl;
}
} else {
unsigned range = links[i]->getVariable()->range();
unsigned range = links[i]->varNode()->range();
Util::multiply (msgProduct, Params (range, 1.0), repetitions);
repetitions *= range;
}
@ -319,20 +319,20 @@ BpSolver::calculateFactor2VariableMsg (SpLink* link)
if (Constants::SHOW_BP_CALCS) {
cout << " marginalized: " << result.params() << endl;
}
link->getNextMessage() = result.params();
LogAware::normalize (link->getNextMessage());
link->nextMessage() = result.params();
LogAware::normalize (link->nextMessage());
if (Constants::SHOW_BP_CALCS) {
cout << " curr msg: " << link->getMessage() << endl;
cout << " next msg: " << link->getNextMessage() << endl;
cout << " curr msg: " << link->message() << endl;
cout << " next msg: " << link->nextMessage() << endl;
}
}
Params
BpSolver::getVar2FactorMsg (const SpLink* link) const
BpSolver::getVarToFactorMsg (const SpLink* link) const
{
const VarNode* src = link->getVariable();
const VarNode* src = link->varNode();
Params msg;
if (src->hasEvidence()) {
msg.resize (src->range(), LogAware::noEvidence());
@ -347,20 +347,20 @@ BpSolver::getVar2FactorMsg (const SpLink* link) const
const SpLinkSet& links = ninf (src)->getLinks();
if (Globals::logDomain) {
for (it = links.begin(); it != links.end(); ++it) {
msg += (*it)->getMessage();
msg += (*it)->message();
if (Constants::SHOW_BP_CALCS) {
cout << " x " << (*it)->getMessage();
cout << " x " << (*it)->message();
}
}
msg -= link->getMessage();
msg -= link->message();
} else {
for (it = links.begin(); it != links.end(); ++it) {
msg *= (*it)->getMessage();
msg *= (*it)->message();
if (Constants::SHOW_BP_CALCS) {
cout << " x " << (*it)->getMessage();
cout << " x " << (*it)->message();
}
}
msg /= link->getMessage();
msg /= link->message();
}
if (Constants::SHOW_BP_CALCS) {
cout << " = " << msg;
@ -439,8 +439,8 @@ BpSolver::initializeSolver (void)
}
createLinks();
for (size_t i = 0; i < links_.size(); i++) {
FacNode* src = links_[i]->getFactor();
VarNode* dst = links_[i]->getVariable();
FacNode* src = links_[i]->facNode();
VarNode* dst = links_[i]->varNode();
ninf (dst)->addSpLink (links_[i]);
ninf (src)->addSpLink (links_[i]);
}
@ -468,7 +468,7 @@ BpSolver::converged (void)
}
bool converged = true;
if (BpOptions::schedule == BpOptions::Schedule::MAX_RESIDUAL) {
double maxResidual = (*(sortedOrder_.begin()))->getResidual();
double maxResidual = (*(sortedOrder_.begin()))->residual();
if (maxResidual > BpOptions::accuracy) {
converged = false;
} else {
@ -476,7 +476,7 @@ BpSolver::converged (void)
}
} else {
for (size_t i = 0; i < links_.size(); i++) {
double residual = links_[i]->getResidual();
double residual = links_[i]->residual();
if (Globals::verbosity > 1) {
cout << links_[i]->toString() + " residual = " << residual << endl;
}
@ -503,10 +503,10 @@ BpSolver::printLinkInformation (void) const
SpLink* l = links_[i];
cout << l->toString() << ":" << endl;
cout << " curr msg = " ;
cout << l->getMessage() << endl;
cout << l->message() << endl;
cout << " next msg = " ;
cout << l->getNextMessage() << endl;
cout << " residual = " << l->getResidual() << endl;
cout << l->nextMessage() << endl;
cout << " residual = " << l->residual() << endl;
}
}

View File

@ -24,23 +24,20 @@ class SpLink
v2_.resize (vn->range(), LogAware::log (1.0 / vn->range()));
currMsg_ = &v1_;
nextMsg_ = &v2_;
msgSended_ = false;
residual_ = 0.0;
}
virtual ~SpLink (void) { };
FacNode* getFactor (void) const { return fac_; }
FacNode* facNode (void) const { return fac_; }
VarNode* getVariable (void) const { return var_; }
VarNode* varNode (void) const { return var_; }
const Params& getMessage (void) const { return *currMsg_; }
const Params& message (void) const { return *currMsg_; }
Params& getNextMessage (void) { return *nextMsg_; }
Params& nextMessage (void) { return *nextMsg_; }
bool messageWasSended (void) const { return msgSended_; }
double getResidual (void) const { return residual_; }
double residual (void) const { return residual_; }
void clearResidual (void) { residual_ = 0.0; }
@ -52,7 +49,6 @@ class SpLink
virtual void updateMessage (void)
{
swap (currMsg_, nextMsg_);
msgSended_ = true;
}
string toString (void) const
@ -71,7 +67,6 @@ class SpLink
Params v2_;
Params* currMsg_;
Params* nextMsg_;
bool msgSended_;
double residual_;
};
@ -110,9 +105,9 @@ class BpSolver : public Solver
virtual void maxResidualSchedule (void);
virtual void calculateFactor2VariableMsg (SpLink*);
virtual void calcFactorToVarMsg (SpLink*);
virtual Params getVar2FactorMsg (const SpLink*) const;
virtual Params getVarToFactorMsg (const SpLink*) const;
virtual Params getJointByConditioning (const VarIds&) const;
@ -131,7 +126,7 @@ class BpSolver : public Solver
if (Globals::verbosity > 2) {
cout << "calculating & updating " << link->toString() << endl;
}
calculateFactor2VariableMsg (link);
calcFactorToVarMsg (link);
if (calcResidual) {
link->updateResidual();
}
@ -143,7 +138,7 @@ class BpSolver : public Solver
if (Globals::verbosity > 2) {
cout << "calculating " << link->toString() << endl;
}
calculateFactor2VariableMsg (link);
calcFactorToVarMsg (link);
if (calcResidual) {
link->updateResidual();
}
@ -161,7 +156,7 @@ class BpSolver : public Solver
{
inline bool operator() (const SpLink* link1, const SpLink* link2)
{
return link1->getResidual() > link2->getResidual();
return link1->residual() > link2->residual();
}
};

View File

@ -63,14 +63,14 @@ CbpSolver::getPosterioriOf (VarId vid)
if (Globals::logDomain) {
for (size_t i = 0; i < links.size(); i++) {
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
probs += l->poweredMessage();
probs += l->powMessage();
}
LogAware::normalize (probs);
Util::exp (probs);
} else {
for (size_t i = 0; i < links.size(); i++) {
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
probs *= l->poweredMessage();
probs *= l->powMessage();
}
LogAware::normalize (probs);
}
@ -146,7 +146,7 @@ CbpSolver::maxResidualSchedule (void)
for (SortedOrder::iterator it = sortedOrder_.begin();
it != sortedOrder_.end(); ++it) {
cout << " " << setw (30) << left << (*it)->toString();
cout << "residual = " << (*it)->getResidual() << endl;
cout << "residual = " << (*it)->residual() << endl;
}
}
@ -155,7 +155,7 @@ CbpSolver::maxResidualSchedule (void)
if (Globals::verbosity >= 1) {
cout << "updating " << (*sortedOrder_.begin())->toString() << endl;
}
if (link->getResidual() < BpOptions::accuracy) {
if (link->residual() < BpOptions::accuracy) {
return;
}
link->updateMessage();
@ -164,11 +164,11 @@ CbpSolver::maxResidualSchedule (void)
linkMap_.find (link)->second = sortedOrder_.insert (link);
// update the messages that depend on message source --> destin
const FacNodes& factorNeighbors = link->getVariable()->neighbors();
const FacNodes& factorNeighbors = link->varNode()->neighbors();
for (size_t i = 0; i < factorNeighbors.size(); i++) {
const SpLinkSet& links = ninf(factorNeighbors[i])->getLinks();
for (size_t j = 0; j < links.size(); j++) {
if (links[j]->getVariable() != link->getVariable()) {
if (links[j]->varNode() != link->varNode()) {
if (Globals::verbosity > 1) {
cout << " calculating " << links[j]->toString() << endl;
}
@ -181,9 +181,9 @@ CbpSolver::maxResidualSchedule (void)
}
// 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();
const SpLinkSet& links = ninf(link->facNode())->getLinks();
for (size_t i = 0; i < links.size(); i++) {
if (links[i]->getVariable() != link->getVariable()) {
if (links[i]->varNode() != link->varNode()) {
if (Globals::verbosity > 1) {
cout << " calculating " << links[i]->toString() << endl;
}
@ -199,35 +199,35 @@ CbpSolver::maxResidualSchedule (void)
void
CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
CbpSolver::calcFactorToVarMsg (SpLink* _link)
{
CbpSolverLink* link = static_cast<CbpSolverLink*> (_link);
FacNode* src = link->getFactor();
const VarNode* dst = link->getVariable();
FacNode* src = link->facNode();
const VarNode* dst = link->varNode();
const SpLinkSet& links = ninf(src)->getLinks();
// calculate the product of messages that were sent
// to factor `src', except from var `dst'
unsigned msgSize = 1;
for (size_t i = 0; i < links.size(); i++) {
msgSize *= links[i]->getVariable()->range();
msgSize *= links[i]->varNode()->range();
}
unsigned repetitions = 1;
Params msgProduct (msgSize, LogAware::multIdenty());
if (Globals::logDomain) {
for (size_t i = links.size(); i-- > 0; ) {
const CbpSolverLink* cl = static_cast<const CbpSolverLink*> (links[i]);
if ( ! (cl->getVariable() == dst && cl->index() == link->index())) {
if ( ! (cl->varNode() == dst && cl->index() == link->index())) {
if (Constants::SHOW_BP_CALCS) {
cout << " message from " << links[i]->getVariable()->label();
cout << " message from " << links[i]->varNode()->label();
cout << ": " ;
}
Util::add (msgProduct, getVar2FactorMsg (links[i]), repetitions);
repetitions *= links[i]->getVariable()->range();
Util::add (msgProduct, getVarToFactorMsg (links[i]), repetitions);
repetitions *= links[i]->varNode()->range();
if (Constants::SHOW_BP_CALCS) {
cout << endl;
}
} else {
unsigned range = links[i]->getVariable()->range();
unsigned range = links[i]->varNode()->range();
Util::add (msgProduct, Params (range, 0.0), repetitions);
repetitions *= range;
}
@ -235,18 +235,18 @@ CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
} else {
for (size_t i = links.size(); i-- > 0; ) {
const CbpSolverLink* cl = static_cast<const CbpSolverLink*> (links[i]);
if ( ! (cl->getVariable() == dst && cl->index() == link->index())) {
if ( ! (cl->varNode() == dst && cl->index() == link->index())) {
if (Constants::SHOW_BP_CALCS) {
cout << " message from " << links[i]->getVariable()->label();
cout << " message from " << links[i]->varNode()->label();
cout << ": " ;
}
Util::multiply (msgProduct, getVar2FactorMsg (links[i]), repetitions);
repetitions *= links[i]->getVariable()->range();
Util::multiply (msgProduct, getVarToFactorMsg (links[i]), repetitions);
repetitions *= links[i]->varNode()->range();
if (Constants::SHOW_BP_CALCS) {
cout << endl;
}
} else {
unsigned range = links[i]->getVariable()->range();
unsigned range = links[i]->varNode()->range();
Util::multiply (msgProduct, Params (range, 1.0), repetitions);
repetitions *= range;
}
@ -267,35 +267,35 @@ CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
}
result.sumOutAllExceptIndex (link->index());
if (Constants::SHOW_BP_CALCS) {
cout << " marginalized: " << result.params() << endl;
cout << " marginalized: " << result.params() << endl;
}
link->getNextMessage() = result.params();
LogAware::normalize (link->getNextMessage());
link->nextMessage() = result.params();
LogAware::normalize (link->nextMessage());
if (Constants::SHOW_BP_CALCS) {
cout << " curr msg: " << link->getMessage() << endl;
cout << " next msg: " << link->getNextMessage() << endl;
cout << " curr msg: " << link->message() << endl;
cout << " next msg: " << link->nextMessage() << endl;
}
}
Params
CbpSolver::getVar2FactorMsg (const SpLink* _link) const
CbpSolver::getVarToFactorMsg (const SpLink* _link) const
{
const CbpSolverLink* link = static_cast<const CbpSolverLink*> (_link);
const VarNode* src = link->getVariable();
const FacNode* dst = link->getFactor();
const VarNode* src = link->varNode();
const FacNode* dst = link->facNode();
Params msg;
if (src->hasEvidence()) {
msg.resize (src->range(), LogAware::noEvidence());
double value = link->getMessage()[src->getEvidence()];
double value = link->message()[src->getEvidence()];
if (Constants::SHOW_BP_CALCS) {
msg[src->getEvidence()] = value;
cout << msg << "^" << link->nrEdges() << "-1" ;
}
msg[src->getEvidence()] = LogAware::pow (value, link->nrEdges() - 1);
} else {
msg = link->getMessage();
msg = link->message();
if (Constants::SHOW_BP_CALCS) {
cout << msg << "^" << link->nrEdges() << "-1" ;
}
@ -305,18 +305,18 @@ CbpSolver::getVar2FactorMsg (const SpLink* _link) const
if (Globals::logDomain) {
for (size_t i = 0; i < links.size(); i++) {
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
if ( ! (cl->facNode() == dst && cl->index() == link->index())) {
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
msg += cl->poweredMessage();
msg += cl->powMessage();
}
}
} else {
for (size_t i = 0; i < links.size(); i++) {
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
msg *= cl->poweredMessage();
if ( ! (cl->facNode() == dst && cl->index() == link->index())) {
msg *= cl->powMessage();
if (Constants::SHOW_BP_CALCS) {
cout << " x " << cl->getNextMessage() << "^" << link->nrEdges();
cout << " x " << cl->nextMessage() << "^" << link->nrEdges();
}
}
}
@ -335,12 +335,12 @@ CbpSolver::printLinkInformation (void) const
for (size_t i = 0; i < links_.size(); i++) {
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links_[i]);
cout << cl->toString() << ":" << endl;
cout << " curr msg = " << cl->getMessage() << endl;
cout << " next msg = " << cl->getNextMessage() << endl;
cout << " curr msg = " << cl->message() << endl;
cout << " next msg = " << cl->nextMessage() << endl;
cout << " index = " << cl->index() << endl;
cout << " nr edges = " << cl->nrEdges() << endl;
cout << " powered = " << cl->poweredMessage() << endl;
cout << " residual = " << cl->getResidual() << endl;
cout << " powered = " << cl->powMessage() << endl;
cout << " residual = " << cl->residual() << endl;
}
}

View File

@ -17,13 +17,12 @@ class CbpSolverLink : public SpLink
unsigned nrEdges (void) const { return nrEdges_; }
const Params& poweredMessage (void) const { return pwdMsg_; }
const Params& powMessage (void) const { return pwdMsg_; }
void updateMessage (void)
{
pwdMsg_ = *nextMsg_;
swap (currMsg_, nextMsg_);
msgSended_ = true;
LogAware::pow (pwdMsg_, nrEdges_);
}
@ -54,9 +53,9 @@ class CbpSolver : public BpSolver
void maxResidualSchedule (void);
void calculateFactor2VariableMsg (SpLink*);
void calcFactorToVarMsg (SpLink*);
Params getVar2FactorMsg (const SpLink*) const;
Params getVarToFactorMsg (const SpLink*) const;
void printLinkInformation (void) const;