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/horus/VarElim.cpp

199 lines
4.9 KiB
C++
Raw Normal View History

2012-05-23 14:56:01 +01:00
#include <algorithm>
#include "VarElim.h"
2012-05-23 14:56:01 +01:00
#include "ElimGraph.h"
#include "Factor.h"
#include "Util.h"
Params
VarElim::solveQuery (VarIds queryVids)
2012-05-23 14:56:01 +01:00
{
if (Globals::verbosity > 1) {
cout << "Solving query on " ;
2012-05-24 22:55:20 +01:00
for (size_t i = 0; i < queryVids.size(); i++) {
2012-05-23 14:56:01 +01:00
if (i != 0) cout << ", " ;
cout << fg.getVarNode (queryVids[i])->label();
}
cout << endl;
}
2012-12-26 22:55:48 +00:00
totalFactorSize_ = 0;
largestFactorSize_ = 0;
2012-05-23 14:56:01 +01:00
factorList_.clear();
2012-12-26 22:55:48 +00:00
varMap_.clear();
2012-05-23 14:56:01 +01:00
createFactorList();
absorveEvidence();
2012-12-26 22:55:48 +00:00
Params params = processFactorList (queryVids);
2012-05-23 14:56:01 +01:00
if (Globals::logDomain) {
2012-05-24 16:14:13 +01:00
Util::exp (params);
2012-05-23 14:56:01 +01:00
}
return params;
}
void
VarElim::printSolverFlags (void) const
2012-05-23 14:56:01 +01:00
{
stringstream ss;
ss << "variable elimination [" ;
ss << "elim_heuristic=" ;
switch (ElimGraph::elimHeuristic()) {
case ElimHeuristic::SEQUENTIAL: ss << "sequential"; break;
case ElimHeuristic::MIN_NEIGHBORS: ss << "min_neighbors"; break;
case ElimHeuristic::MIN_WEIGHT: ss << "min_weight"; break;
case ElimHeuristic::MIN_FILL: ss << "min_fill"; break;
case ElimHeuristic::WEIGHTED_MIN_FILL: ss << "weighted_min_fill"; break;
2012-05-23 14:56:01 +01:00
}
ss << ",log_domain=" << Util::toString (Globals::logDomain);
ss << "]" ;
cout << ss.str() << endl;
}
void
VarElim::createFactorList (void)
2012-05-23 14:56:01 +01:00
{
const FacNodes& facNodes = fg.facNodes();
factorList_.reserve (facNodes.size() * 2);
2012-05-24 22:55:20 +01:00
for (size_t i = 0; i < facNodes.size(); i++) {
2012-05-23 14:56:01 +01:00
factorList_.push_back (new Factor (facNodes[i]->factor()));
2012-12-26 22:55:48 +00:00
const VarIds& args = facNodes[i]->factor().arguments();
for (size_t j = 0; j < args.size(); j++) {
unordered_map<VarId, vector<size_t>>::iterator it;
it = varMap_.find (args[j]);
if (it != varMap_.end()) {
it->second.push_back (i);
} else {
varMap_[args[j]] = { i };
2012-05-23 14:56:01 +01:00
}
}
}
2012-12-17 18:39:42 +00:00
}
2012-05-23 14:56:01 +01:00
void
VarElim::absorveEvidence (void)
2012-05-23 14:56:01 +01:00
{
if (Globals::verbosity > 2) {
Util::printDashedLine();
cout << "(initial factor list)" << endl;
printActiveFactors();
}
const VarNodes& varNodes = fg.varNodes();
2012-05-24 22:55:20 +01:00
for (size_t i = 0; i < varNodes.size(); i++) {
2012-05-23 14:56:01 +01:00
if (varNodes[i]->hasEvidence()) {
if (Globals::verbosity > 1) {
cout << "-> aborving evidence on ";
cout << varNodes[i]->label() << " = " ;
cout << varNodes[i]->getEvidence() << endl;
}
2012-12-26 22:55:48 +00:00
const vector<size_t>& indices = varMap_[varNodes[i]->varId()];
for (size_t j = 0; j < indices.size(); j++) {
size_t idx = indices[j];
if (factorList_[idx]->nrArguments() > 1) {
factorList_[idx]->absorveEvidence (
2012-05-23 14:56:01 +01:00
varNodes[i]->varId(), varNodes[i]->getEvidence());
2012-12-26 22:55:48 +00:00
} else {
delete factorList_[idx];
factorList_[idx] = 0;
2012-05-23 14:56:01 +01:00
}
}
}
}
}
2012-12-26 22:55:48 +00:00
Params
VarElim::processFactorList (const VarIds& queryVids)
2012-05-23 14:56:01 +01:00
{
2012-12-26 22:55:48 +00:00
VarIds elimOrder = ElimGraph::getEliminationOrder (
factorList_, queryVids);
for (size_t i = 0; i < elimOrder.size(); i++) {
2012-05-23 14:56:01 +01:00
if (Globals::verbosity >= 2) {
if (Globals::verbosity >= 3) {
Util::printDashedLine();
printActiveFactors();
}
cout << "-> summing out " ;
2012-12-26 22:55:48 +00:00
cout << fg.getVarNode (elimOrder[i])->label() << endl;
2012-05-23 14:56:01 +01:00
}
2012-12-26 22:55:48 +00:00
eliminate (elimOrder[i]);
2012-05-23 14:56:01 +01:00
}
2012-12-26 22:55:48 +00:00
Factor result;
2012-05-24 22:55:20 +01:00
for (size_t i = 0; i < factorList_.size(); i++) {
2012-05-23 14:56:01 +01:00
if (factorList_[i]) {
2012-12-26 22:55:48 +00:00
result.multiply (*factorList_[i]);
2012-05-23 14:56:01 +01:00
delete factorList_[i];
factorList_[i] = 0;
}
}
VarIds unobservedVids;
2012-12-26 22:55:48 +00:00
for (size_t i = 0; i < queryVids.size(); i++) {
if (fg.getVarNode (queryVids[i])->hasEvidence() == false) {
unobservedVids.push_back (queryVids[i]);
2012-05-23 14:56:01 +01:00
}
}
2012-12-26 22:55:48 +00:00
result.reorderArguments (unobservedVids);
result.normalize();
2012-05-23 14:56:01 +01:00
if (Globals::verbosity > 0) {
cout << "total factor size: " << totalFactorSize_ << endl;
cout << "largest factor size: " << largestFactorSize_ << endl;
cout << endl;
}
2012-12-26 22:55:48 +00:00
return result.params();
2012-05-23 14:56:01 +01:00
}
void
2012-12-26 22:55:48 +00:00
VarElim::eliminate (VarId vid)
2012-05-23 14:56:01 +01:00
{
2012-12-26 22:55:48 +00:00
Factor* result = new Factor();
const vector<size_t>& indices = varMap_[vid];
for (size_t i = 0; i < indices.size(); i++) {
size_t idx = indices[i];
2012-05-23 14:56:01 +01:00
if (factorList_[idx]) {
2012-12-26 22:55:48 +00:00
result->multiply (*factorList_[idx]);
2012-05-23 14:56:01 +01:00
delete factorList_[idx];
factorList_[idx] = 0;
}
}
totalFactorSize_ += result->size();
if (result->size() > largestFactorSize_) {
largestFactorSize_ = result->size();
}
2012-12-26 22:55:48 +00:00
if (result->nrArguments() > 1) {
result->sumOut (vid);
const VarIds& args = result->arguments();
for (size_t i = 0; i < args.size(); i++) {
vector<size_t>& indices2 = varMap_[args[i]];
indices2.push_back (factorList_.size());
2012-05-23 14:56:01 +01:00
}
2012-12-26 22:55:48 +00:00
factorList_.push_back (result);
} else {
delete result;
2012-05-23 14:56:01 +01:00
}
}
void
VarElim::printActiveFactors (void)
2012-05-23 14:56:01 +01:00
{
2012-05-24 22:55:20 +01:00
for (size_t i = 0; i < factorList_.size(); i++) {
2012-12-26 22:55:48 +00:00
if (factorList_[i]) {
2012-05-23 14:56:01 +01:00
cout << factorList_[i]->getLabel() << " " ;
2012-12-26 22:55:48 +00:00
cout << factorList_[i]->params();
cout << endl;
2012-05-23 14:56:01 +01:00
}
}
}