Merge branch 'master' of github.com:tacgomes/yap6.3
This commit is contained in:
commit
fb5edf6051
8
configure
vendored
8
configure
vendored
@ -10519,8 +10519,7 @@ mkdir -p packages/clib/maildrop/rfc822
|
|||||||
mkdir -p packages/clib/maildrop/rfc2045
|
mkdir -p packages/clib/maildrop/rfc2045
|
||||||
mkdir -p packages/CLPBN
|
mkdir -p packages/CLPBN
|
||||||
mkdir -p packages/CLPBN/clpbn
|
mkdir -p packages/CLPBN/clpbn
|
||||||
mkdir -p packages/CLPBN/clpbn/bp
|
mkdir -p packages/CLPBN/horus
|
||||||
mkdir -p packages/CLPBN/clpbn/bp/xmlParser
|
|
||||||
mkdir -p packages/clpqr
|
mkdir -p packages/clpqr
|
||||||
mkdir -p packages/cplint
|
mkdir -p packages/cplint
|
||||||
mkdir -p packages/cplint/approx
|
mkdir -p packages/cplint/approx
|
||||||
@ -10673,6 +10672,7 @@ ac_config_files="$ac_config_files packages/zlib/Makefile"
|
|||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
if test "$ENABLE_CUDD" = ""; then
|
if test "$ENABLE_CUDD" = ""; then
|
||||||
ac_config_files="$ac_config_files packages/bdd/Makefile"
|
ac_config_files="$ac_config_files packages/bdd/Makefile"
|
||||||
|
|
||||||
@ -10695,7 +10695,7 @@ ac_config_files="$ac_config_files packages/real/Makefile"
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if test "$ENABLE_CLPBN_BP" = ""; then
|
if test "$ENABLE_CLPBN_BP" = ""; then
|
||||||
ac_config_files="$ac_config_files packages/CLPBN/clpbn/bp/Makefile"
|
ac_config_files="$ac_config_files packages/CLPBN/horus/Makefile"
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -11464,7 +11464,7 @@ do
|
|||||||
"packages/swi-minisat2/Makefile") CONFIG_FILES="$CONFIG_FILES packages/swi-minisat2/Makefile" ;;
|
"packages/swi-minisat2/Makefile") CONFIG_FILES="$CONFIG_FILES packages/swi-minisat2/Makefile" ;;
|
||||||
"packages/swi-minisat2/C/Makefile") CONFIG_FILES="$CONFIG_FILES packages/swi-minisat2/C/Makefile" ;;
|
"packages/swi-minisat2/C/Makefile") CONFIG_FILES="$CONFIG_FILES packages/swi-minisat2/C/Makefile" ;;
|
||||||
"packages/real/Makefile") CONFIG_FILES="$CONFIG_FILES packages/real/Makefile" ;;
|
"packages/real/Makefile") CONFIG_FILES="$CONFIG_FILES packages/real/Makefile" ;;
|
||||||
"packages/CLPBN/clpbn/bp/Makefile") CONFIG_FILES="$CONFIG_FILES packages/CLPBN/clpbn/bp/Makefile" ;;
|
"packages/CLPBN/horus/Makefile") CONFIG_FILES="$CONFIG_FILES packages/CLPBN/horus/Makefile" ;;
|
||||||
"library/gecode/Makefile") CONFIG_FILES="$CONFIG_FILES library/gecode/Makefile" ;;
|
"library/gecode/Makefile") CONFIG_FILES="$CONFIG_FILES library/gecode/Makefile" ;;
|
||||||
"packages/prism/src/c/Makefile") CONFIG_FILES="$CONFIG_FILES packages/prism/src/c/Makefile" ;;
|
"packages/prism/src/c/Makefile") CONFIG_FILES="$CONFIG_FILES packages/prism/src/c/Makefile" ;;
|
||||||
"packages/prism/src/prolog/Makefile") CONFIG_FILES="$CONFIG_FILES packages/prism/src/prolog/Makefile" ;;
|
"packages/prism/src/prolog/Makefile") CONFIG_FILES="$CONFIG_FILES packages/prism/src/prolog/Makefile" ;;
|
||||||
|
@ -16,7 +16,7 @@ BayesBall::getMinimalFactorGraph (const VarIds& queryIds)
|
|||||||
{
|
{
|
||||||
assert (fg_.isFromBayesNetwork());
|
assert (fg_.isFromBayesNetwork());
|
||||||
Scheduling scheduling;
|
Scheduling scheduling;
|
||||||
for (unsigned i = 0; i < queryIds.size(); i++) {
|
for (size_t i = 0; i < queryIds.size(); i++) {
|
||||||
assert (dag_.getNode (queryIds[i]));
|
assert (dag_.getNode (queryIds[i]));
|
||||||
DAGraphNode* n = dag_.getNode (queryIds[i]);
|
DAGraphNode* n = dag_.getNode (queryIds[i]);
|
||||||
scheduling.push (ScheduleInfo (n, false, true));
|
scheduling.push (ScheduleInfo (n, false, true));
|
||||||
@ -60,7 +60,7 @@ void
|
|||||||
BayesBall::constructGraph (FactorGraph* fg) const
|
BayesBall::constructGraph (FactorGraph* fg) const
|
||||||
{
|
{
|
||||||
const FacNodes& facNodes = fg_.facNodes();
|
const FacNodes& facNodes = fg_.facNodes();
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
const DAGraphNode* n = dag_.getNode (
|
const DAGraphNode* n = dag_.getNode (
|
||||||
facNodes[i]->factor().argument (0));
|
facNodes[i]->factor().argument (0));
|
||||||
if (n->isMarkedOnTop()) {
|
if (n->isMarkedOnTop()) {
|
||||||
@ -74,7 +74,7 @@ BayesBall::constructGraph (FactorGraph* fg) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const VarNodes& varNodes = fg_.varNodes();
|
const VarNodes& varNodes = fg_.varNodes();
|
||||||
for (unsigned i = 0; i < varNodes.size(); i++) {
|
for (size_t i = 0; i < varNodes.size(); i++) {
|
||||||
if (varNodes[i]->hasEvidence()) {
|
if (varNodes[i]->hasEvidence()) {
|
||||||
VarNode* vn = fg->getVarNode (varNodes[i]->varId());
|
VarNode* vn = fg->getVarNode (varNodes[i]->varId());
|
||||||
if (vn) {
|
if (vn) {
|
||||||
|
@ -64,7 +64,7 @@ BayesBall::scheduleParents (const DAGraphNode* n, Scheduling& sch) const
|
|||||||
{
|
{
|
||||||
const vector<DAGraphNode*>& ps = n->parents();
|
const vector<DAGraphNode*>& ps = n->parents();
|
||||||
for (vector<DAGraphNode*>::const_iterator it = ps.begin();
|
for (vector<DAGraphNode*>::const_iterator it = ps.begin();
|
||||||
it != ps.end(); it++) {
|
it != ps.end(); ++it) {
|
||||||
sch.push (ScheduleInfo (*it, false, true));
|
sch.push (ScheduleInfo (*it, false, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ BayesBall::scheduleChilds (const DAGraphNode* n, Scheduling& sch) const
|
|||||||
{
|
{
|
||||||
const vector<DAGraphNode*>& cs = n->childs();
|
const vector<DAGraphNode*>& cs = n->childs();
|
||||||
for (vector<DAGraphNode*>::const_iterator it = cs.begin();
|
for (vector<DAGraphNode*>::const_iterator it = cs.begin();
|
||||||
it != cs.end(); it++) {
|
it != cs.end(); ++it) {
|
||||||
sch.push (ScheduleInfo (*it, true, false));
|
sch.push (ScheduleInfo (*it, true, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ DAGraph::getNode (VarId vid)
|
|||||||
void
|
void
|
||||||
DAGraph::setIndexes (void)
|
DAGraph::setIndexes (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
nodes_[i]->setIndex (i);
|
nodes_[i]->setIndex (i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ DAGraph::setIndexes (void)
|
|||||||
void
|
void
|
||||||
DAGraph::clear (void)
|
DAGraph::clear (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
nodes_[i]->clear();
|
nodes_[i]->clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@ DAGraph::exportToGraphViz (const char* fileName)
|
|||||||
}
|
}
|
||||||
out << "digraph {" << endl;
|
out << "digraph {" << endl;
|
||||||
out << "ranksep=1" << endl;
|
out << "ranksep=1" << endl;
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
out << nodes_[i]->varId() ;
|
out << nodes_[i]->varId() ;
|
||||||
out << " [" ;
|
out << " [" ;
|
||||||
out << "label=\"" << nodes_[i]->label() << "\"" ;
|
out << "label=\"" << nodes_[i]->label() << "\"" ;
|
||||||
@ -94,9 +94,9 @@ DAGraph::exportToGraphViz (const char* fileName)
|
|||||||
}
|
}
|
||||||
out << "]" << endl;
|
out << "]" << endl;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
const vector<DAGraphNode*>& childs = nodes_[i]->childs();
|
const vector<DAGraphNode*>& childs = nodes_[i]->childs();
|
||||||
for (unsigned j = 0; j < childs.size(); j++) {
|
for (size_t j = 0; j < childs.size(); j++) {
|
||||||
out << nodes_[i]->varId() << " -> " << childs[j]->varId();
|
out << nodes_[i]->varId() << " -> " << childs[j]->varId();
|
||||||
out << " [style=bold]" << endl ;
|
out << " [style=bold]" << endl ;
|
||||||
}
|
}
|
||||||
|
@ -22,13 +22,13 @@ BpSolver::BpSolver (const FactorGraph& fg) : Solver (fg)
|
|||||||
|
|
||||||
BpSolver::~BpSolver (void)
|
BpSolver::~BpSolver (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < varsI_.size(); i++) {
|
for (size_t i = 0; i < varsI_.size(); i++) {
|
||||||
delete varsI_[i];
|
delete varsI_[i];
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facsI_.size(); i++) {
|
for (size_t i = 0; i < facsI_.size(); i++) {
|
||||||
delete facsI_[i];
|
delete facsI_[i];
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
delete links_[i];
|
delete links_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,14 +86,14 @@ BpSolver::getPosterioriOf (VarId vid)
|
|||||||
probs.resize (var->range(), LogAware::multIdenty());
|
probs.resize (var->range(), LogAware::multIdenty());
|
||||||
const SpLinkSet& links = ninf(var)->getLinks();
|
const SpLinkSet& links = ninf(var)->getLinks();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
Util::add (probs, links[i]->getMessage());
|
probs += links[i]->getMessage();
|
||||||
}
|
}
|
||||||
LogAware::normalize (probs);
|
LogAware::normalize (probs);
|
||||||
Util::fromLog (probs);
|
Util::exp (probs);
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
Util::multiply (probs, links[i]->getMessage());
|
probs *= links[i]->getMessage();
|
||||||
}
|
}
|
||||||
LogAware::normalize (probs);
|
LogAware::normalize (probs);
|
||||||
}
|
}
|
||||||
@ -109,21 +109,21 @@ BpSolver::getJointDistributionOf (const VarIds& jointVarIds)
|
|||||||
if (runned_ == false) {
|
if (runned_ == false) {
|
||||||
runSolver();
|
runSolver();
|
||||||
}
|
}
|
||||||
int idx = -1;
|
|
||||||
VarNode* vn = fg_->getVarNode (jointVarIds[0]);
|
VarNode* vn = fg_->getVarNode (jointVarIds[0]);
|
||||||
const FacNodes& facNodes = vn->neighbors();
|
const FacNodes& facNodes = vn->neighbors();
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
size_t idx = facNodes.size();
|
||||||
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
if (facNodes[i]->factor().contains (jointVarIds)) {
|
if (facNodes[i]->factor().contains (jointVarIds)) {
|
||||||
idx = i;
|
idx = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (idx == -1) {
|
if (idx == facNodes.size()) {
|
||||||
return getJointByConditioning (jointVarIds);
|
return getJointByConditioning (jointVarIds);
|
||||||
} else {
|
} else {
|
||||||
Factor res (facNodes[idx]->factor());
|
Factor res (facNodes[idx]->factor());
|
||||||
const SpLinkSet& links = ninf(facNodes[idx])->getLinks();
|
const SpLinkSet& links = ninf(facNodes[idx])->getLinks();
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
Factor msg ({links[i]->getVariable()->varId()},
|
Factor msg ({links[i]->getVariable()->varId()},
|
||||||
{links[i]->getVariable()->range()},
|
{links[i]->getVariable()->range()},
|
||||||
getVar2FactorMsg (links[i]));
|
getVar2FactorMsg (links[i]));
|
||||||
@ -134,7 +134,7 @@ BpSolver::getJointDistributionOf (const VarIds& jointVarIds)
|
|||||||
res.normalize();
|
res.normalize();
|
||||||
Params jointDist = res.params();
|
Params jointDist = res.params();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::fromLog (jointDist);
|
Util::exp (jointDist);
|
||||||
}
|
}
|
||||||
return jointDist;
|
return jointDist;
|
||||||
}
|
}
|
||||||
@ -145,10 +145,6 @@ BpSolver::getJointDistributionOf (const VarIds& jointVarIds)
|
|||||||
void
|
void
|
||||||
BpSolver::runSolver (void)
|
BpSolver::runSolver (void)
|
||||||
{
|
{
|
||||||
clock_t start;
|
|
||||||
if (Constants::COLLECT_STATS) {
|
|
||||||
start = clock();
|
|
||||||
}
|
|
||||||
initializeSolver();
|
initializeSolver();
|
||||||
nIters_ = 0;
|
nIters_ = 0;
|
||||||
while (!converged() && nIters_ < BpOptions::maxIter) {
|
while (!converged() && nIters_ < BpOptions::maxIter) {
|
||||||
@ -161,15 +157,15 @@ BpSolver::runSolver (void)
|
|||||||
random_shuffle (links_.begin(), links_.end());
|
random_shuffle (links_.begin(), links_.end());
|
||||||
// no break
|
// no break
|
||||||
case BpOptions::Schedule::SEQ_FIXED:
|
case BpOptions::Schedule::SEQ_FIXED:
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
calculateAndUpdateMessage (links_[i]);
|
calculateAndUpdateMessage (links_[i]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BpOptions::Schedule::PARALLEL:
|
case BpOptions::Schedule::PARALLEL:
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
calculateMessage (links_[i]);
|
calculateMessage (links_[i]);
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
updateMessage(links_[i]);
|
updateMessage(links_[i]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -188,14 +184,6 @@ BpSolver::runSolver (void)
|
|||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
unsigned size = fg_->varNodes().size();
|
|
||||||
if (Constants::COLLECT_STATS) {
|
|
||||||
unsigned nIters = 0;
|
|
||||||
bool loopy = fg_->isTree() == false;
|
|
||||||
if (loopy) nIters = nIters_;
|
|
||||||
double time = (double (clock() - start)) / CLOCKS_PER_SEC;
|
|
||||||
Statistics::updateStatistics (size, loopy, nIters, time);
|
|
||||||
}
|
|
||||||
runned_ = true;
|
runned_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,9 +193,9 @@ void
|
|||||||
BpSolver::createLinks (void)
|
BpSolver::createLinks (void)
|
||||||
{
|
{
|
||||||
const FacNodes& facNodes = fg_->facNodes();
|
const FacNodes& facNodes = fg_->facNodes();
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
const VarNodes& neighbors = facNodes[i]->neighbors();
|
const VarNodes& neighbors = facNodes[i]->neighbors();
|
||||||
for (unsigned j = 0; j < neighbors.size(); j++) {
|
for (size_t j = 0; j < neighbors.size(); j++) {
|
||||||
links_.push_back (new SpLink (facNodes[i], neighbors[j]));
|
links_.push_back (new SpLink (facNodes[i], neighbors[j]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +207,7 @@ void
|
|||||||
BpSolver::maxResidualSchedule (void)
|
BpSolver::maxResidualSchedule (void)
|
||||||
{
|
{
|
||||||
if (nIters_ == 1) {
|
if (nIters_ == 1) {
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
calculateMessage (links_[i]);
|
calculateMessage (links_[i]);
|
||||||
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
|
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
|
||||||
linkMap_.insert (make_pair (links_[i], it));
|
linkMap_.insert (make_pair (links_[i], it));
|
||||||
@ -227,11 +215,11 @@ BpSolver::maxResidualSchedule (void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned c = 0; c < links_.size(); c++) {
|
for (size_t c = 0; c < links_.size(); c++) {
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << "current residuals:" << endl;
|
cout << "current residuals:" << endl;
|
||||||
for (SortedOrder::iterator it = sortedOrder_.begin();
|
for (SortedOrder::iterator it = sortedOrder_.begin();
|
||||||
it != sortedOrder_.end(); it ++) {
|
it != sortedOrder_.end(); ++it) {
|
||||||
cout << " " << setw (30) << left << (*it)->toString();
|
cout << " " << setw (30) << left << (*it)->toString();
|
||||||
cout << "residual = " << (*it)->getResidual() << endl;
|
cout << "residual = " << (*it)->getResidual() << endl;
|
||||||
}
|
}
|
||||||
@ -249,10 +237,10 @@ BpSolver::maxResidualSchedule (void)
|
|||||||
|
|
||||||
// update the messages that depend on message source --> destin
|
// update the messages that depend on message source --> destin
|
||||||
const FacNodes& factorNeighbors = link->getVariable()->neighbors();
|
const FacNodes& factorNeighbors = link->getVariable()->neighbors();
|
||||||
for (unsigned i = 0; i < factorNeighbors.size(); i++) {
|
for (size_t i = 0; i < factorNeighbors.size(); i++) {
|
||||||
if (factorNeighbors[i] != link->getFactor()) {
|
if (factorNeighbors[i] != link->getFactor()) {
|
||||||
const SpLinkSet& links = ninf(factorNeighbors[i])->getLinks();
|
const SpLinkSet& links = ninf(factorNeighbors[i])->getLinks();
|
||||||
for (unsigned j = 0; j < links.size(); j++) {
|
for (size_t j = 0; j < links.size(); j++) {
|
||||||
if (links[j]->getVariable() != link->getVariable()) {
|
if (links[j]->getVariable() != link->getVariable()) {
|
||||||
calculateMessage (links[j]);
|
calculateMessage (links[j]);
|
||||||
SpLinkMap::iterator iter = linkMap_.find (links[j]);
|
SpLinkMap::iterator iter = linkMap_.find (links[j]);
|
||||||
@ -279,13 +267,13 @@ BpSolver::calculateFactor2VariableMsg (SpLink* link)
|
|||||||
// calculate the product of messages that were sent
|
// calculate the product of messages that were sent
|
||||||
// to factor `src', except from var `dst'
|
// to factor `src', except from var `dst'
|
||||||
unsigned msgSize = 1;
|
unsigned msgSize = 1;
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
msgSize *= links[i]->getVariable()->range();
|
msgSize *= links[i]->getVariable()->range();
|
||||||
}
|
}
|
||||||
unsigned repetitions = 1;
|
unsigned repetitions = 1;
|
||||||
Params msgProduct (msgSize, LogAware::multIdenty());
|
Params msgProduct (msgSize, LogAware::multIdenty());
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (int i = links.size() - 1; i >= 0; i--) {
|
for (size_t i = links.size(); i-- > 0; ) {
|
||||||
if (links[i]->getVariable() != dst) {
|
if (links[i]->getVariable() != dst) {
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
cout << " message from " << links[i]->getVariable()->label();
|
cout << " message from " << links[i]->getVariable()->label();
|
||||||
@ -303,7 +291,7 @@ BpSolver::calculateFactor2VariableMsg (SpLink* link)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = links.size() - 1; i >= 0; i--) {
|
for (size_t i = links.size(); i-- > 0; ) {
|
||||||
if (links[i]->getVariable() != dst) {
|
if (links[i]->getVariable() != dst) {
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
cout << " message from " << links[i]->getVariable()->label();
|
cout << " message from " << links[i]->getVariable()->label();
|
||||||
@ -362,16 +350,16 @@ BpSolver::getVar2FactorMsg (const SpLink* link) const
|
|||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
SpLinkSet::const_iterator it;
|
SpLinkSet::const_iterator it;
|
||||||
for (it = links.begin(); it != links.end(); ++ it) {
|
for (it = links.begin(); it != links.end(); ++ it) {
|
||||||
Util::add (msg, (*it)->getMessage());
|
msg += (*it)->getMessage();
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
cout << " x " << (*it)->getMessage();
|
cout << " x " << (*it)->getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Util::subtract (msg, link->getMessage());
|
msg -= link->getMessage();
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
if (links[i]->getFactor() != dst) {
|
if (links[i]->getFactor() != dst) {
|
||||||
Util::multiply (msg, links[i]->getMessage());
|
msg *= links[i]->getMessage();
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
cout << " x " << links[i]->getMessage();
|
cout << " x " << links[i]->getMessage();
|
||||||
}
|
}
|
||||||
@ -390,7 +378,7 @@ Params
|
|||||||
BpSolver::getJointByConditioning (const VarIds& jointVarIds) const
|
BpSolver::getJointByConditioning (const VarIds& jointVarIds) const
|
||||||
{
|
{
|
||||||
VarNodes jointVars;
|
VarNodes jointVars;
|
||||||
for (unsigned i = 0; i < jointVarIds.size(); i++) {
|
for (size_t i = 0; i < jointVarIds.size(); i++) {
|
||||||
assert (fg_->getVarNode (jointVarIds[i]));
|
assert (fg_->getVarNode (jointVarIds[i]));
|
||||||
jointVars.push_back (fg_->getVarNode (jointVarIds[i]));
|
jointVars.push_back (fg_->getVarNode (jointVarIds[i]));
|
||||||
}
|
}
|
||||||
@ -402,29 +390,31 @@ BpSolver::getJointByConditioning (const VarIds& jointVarIds) const
|
|||||||
|
|
||||||
VarIds observedVids = {jointVars[0]->varId()};
|
VarIds observedVids = {jointVars[0]->varId()};
|
||||||
|
|
||||||
for (unsigned i = 1; i < jointVarIds.size(); i++) {
|
for (size_t i = 1; i < jointVarIds.size(); i++) {
|
||||||
assert (jointVars[i]->hasEvidence() == false);
|
assert (jointVars[i]->hasEvidence() == false);
|
||||||
Params newBeliefs;
|
Params newBeliefs;
|
||||||
Vars observedVars;
|
Vars observedVars;
|
||||||
for (unsigned j = 0; j < observedVids.size(); j++) {
|
Ranges observedRanges;
|
||||||
|
for (size_t j = 0; j < observedVids.size(); j++) {
|
||||||
observedVars.push_back (fg->getVarNode (observedVids[j]));
|
observedVars.push_back (fg->getVarNode (observedVids[j]));
|
||||||
|
observedRanges.push_back (observedVars.back()->range());
|
||||||
}
|
}
|
||||||
StatesIndexer idx (observedVars, false);
|
Indexer indexer (observedRanges, false);
|
||||||
while (idx.valid()) {
|
while (indexer.valid()) {
|
||||||
for (unsigned j = 0; j < observedVars.size(); j++) {
|
for (size_t j = 0; j < observedVars.size(); j++) {
|
||||||
observedVars[j]->setEvidence (idx[j]);
|
observedVars[j]->setEvidence (indexer[j]);
|
||||||
}
|
}
|
||||||
++ idx;
|
|
||||||
BpSolver solver (*fg);
|
BpSolver solver (*fg);
|
||||||
solver.runSolver();
|
solver.runSolver();
|
||||||
Params beliefs = solver.getPosterioriOf (jointVarIds[i]);
|
Params beliefs = solver.getPosterioriOf (jointVarIds[i]);
|
||||||
for (unsigned k = 0; k < beliefs.size(); k++) {
|
for (size_t k = 0; k < beliefs.size(); k++) {
|
||||||
newBeliefs.push_back (beliefs[k]);
|
newBeliefs.push_back (beliefs[k]);
|
||||||
}
|
}
|
||||||
|
++ indexer;
|
||||||
}
|
}
|
||||||
|
|
||||||
int count = -1;
|
int count = -1;
|
||||||
for (unsigned j = 0; j < newBeliefs.size(); j++) {
|
for (size_t j = 0; j < newBeliefs.size(); j++) {
|
||||||
if (j % jointVars[i]->range() == 0) {
|
if (j % jointVars[i]->range() == 0) {
|
||||||
count ++;
|
count ++;
|
||||||
}
|
}
|
||||||
@ -443,16 +433,16 @@ BpSolver::initializeSolver (void)
|
|||||||
{
|
{
|
||||||
const VarNodes& varNodes = fg_->varNodes();
|
const VarNodes& varNodes = fg_->varNodes();
|
||||||
varsI_.reserve (varNodes.size());
|
varsI_.reserve (varNodes.size());
|
||||||
for (unsigned i = 0; i < varNodes.size(); i++) {
|
for (size_t i = 0; i < varNodes.size(); i++) {
|
||||||
varsI_.push_back (new SPNodeInfo());
|
varsI_.push_back (new SPNodeInfo());
|
||||||
}
|
}
|
||||||
const FacNodes& facNodes = fg_->facNodes();
|
const FacNodes& facNodes = fg_->facNodes();
|
||||||
facsI_.reserve (facNodes.size());
|
facsI_.reserve (facNodes.size());
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
facsI_.push_back (new SPNodeInfo());
|
facsI_.push_back (new SPNodeInfo());
|
||||||
}
|
}
|
||||||
createLinks();
|
createLinks();
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
FacNode* src = links_[i]->getFactor();
|
FacNode* src = links_[i]->getFactor();
|
||||||
VarNode* dst = links_[i]->getVariable();
|
VarNode* dst = links_[i]->getVariable();
|
||||||
ninf (dst)->addSpLink (links_[i]);
|
ninf (dst)->addSpLink (links_[i]);
|
||||||
@ -489,7 +479,7 @@ BpSolver::converged (void)
|
|||||||
converged = true;
|
converged = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
double residual = links_[i]->getResidual();
|
double residual = links_[i]->getResidual();
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << links_[i]->toString() + " residual = " << residual << endl;
|
cout << links_[i]->toString() + " residual = " << residual << endl;
|
||||||
@ -513,7 +503,7 @@ BpSolver::converged (void)
|
|||||||
void
|
void
|
||||||
BpSolver::printLinkInformation (void) const
|
BpSolver::printLinkInformation (void) const
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
SpLink* l = links_[i];
|
SpLink* l = links_[i];
|
||||||
cout << l->toString() << ":" << endl;
|
cout << l->toString() << ":" << endl;
|
||||||
cout << " curr msg = " ;
|
cout << " curr msg = " ;
|
||||||
|
@ -20,8 +20,8 @@ class SpLink
|
|||||||
{
|
{
|
||||||
fac_ = fn;
|
fac_ = fn;
|
||||||
var_ = vn;
|
var_ = vn;
|
||||||
v1_.resize (vn->range(), LogAware::tl (1.0 / vn->range()));
|
v1_.resize (vn->range(), LogAware::log (1.0 / vn->range()));
|
||||||
v2_.resize (vn->range(), LogAware::tl (1.0 / vn->range()));
|
v2_.resize (vn->range(), LogAware::log (1.0 / vn->range()));
|
||||||
currMsg_ = &v1_;
|
currMsg_ = &v1_;
|
||||||
nextMsg_ = &v2_;
|
nextMsg_ = &v2_;
|
||||||
msgSended_ = false;
|
msgSended_ = false;
|
||||||
|
@ -16,10 +16,10 @@ CFactorGraph::CFactorGraph (const FactorGraph& fg)
|
|||||||
|
|
||||||
CFactorGraph::~CFactorGraph (void)
|
CFactorGraph::~CFactorGraph (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < varClusters_.size(); i++) {
|
for (size_t i = 0; i < varClusters_.size(); i++) {
|
||||||
delete varClusters_[i];
|
delete varClusters_[i];
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facClusters_.size(); i++) {
|
for (size_t i = 0; i < facClusters_.size(); i++) {
|
||||||
delete facClusters_[i];
|
delete facClusters_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,21 +29,22 @@ CFactorGraph::~CFactorGraph (void)
|
|||||||
void
|
void
|
||||||
CFactorGraph::findIdenticalFactors()
|
CFactorGraph::findIdenticalFactors()
|
||||||
{
|
{
|
||||||
if (checkForIdenticalFactors == false) {
|
const FacNodes& facNodes = groundFg_->facNodes();
|
||||||
|
if (checkForIdenticalFactors == false ||
|
||||||
|
facNodes.size() == 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const FacNodes& facNodes = groundFg_->facNodes();
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
|
||||||
facNodes[i]->factor().setDistId (Util::maxUnsigned());
|
facNodes[i]->factor().setDistId (Util::maxUnsigned());
|
||||||
}
|
}
|
||||||
unsigned groupCount = 1;
|
unsigned groupCount = 1;
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size() - 1; i++) {
|
||||||
Factor& f1 = facNodes[i]->factor();
|
Factor& f1 = facNodes[i]->factor();
|
||||||
if (f1.distId() != Util::maxUnsigned()) {
|
if (f1.distId() != Util::maxUnsigned()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
f1.setDistId (groupCount);
|
f1.setDistId (groupCount);
|
||||||
for (unsigned j = i + 1; j < facNodes.size(); j++) {
|
for (size_t j = i + 1; j < facNodes.size(); j++) {
|
||||||
Factor& f2 = facNodes[j]->factor();
|
Factor& f2 = facNodes[j]->factor();
|
||||||
if (f2.distId() != Util::maxUnsigned()) {
|
if (f2.distId() != Util::maxUnsigned()) {
|
||||||
continue;
|
continue;
|
||||||
@ -69,19 +70,16 @@ CFactorGraph::setInitialColors (void)
|
|||||||
// create the initial variable colors
|
// create the initial variable colors
|
||||||
VarColorMap colorMap;
|
VarColorMap colorMap;
|
||||||
const VarNodes& varNodes = groundFg_->varNodes();
|
const VarNodes& varNodes = groundFg_->varNodes();
|
||||||
for (unsigned i = 0; i < varNodes.size(); i++) {
|
for (size_t i = 0; i < varNodes.size(); i++) {
|
||||||
unsigned dsize = varNodes[i]->range();
|
unsigned range = varNodes[i]->range();
|
||||||
VarColorMap::iterator it = colorMap.find (dsize);
|
VarColorMap::iterator it = colorMap.find (range);
|
||||||
if (it == colorMap.end()) {
|
if (it == colorMap.end()) {
|
||||||
it = colorMap.insert (make_pair (
|
it = colorMap.insert (make_pair (
|
||||||
dsize, Colors (dsize+1,-1))).first;
|
range, Colors (range + 1, -1))).first;
|
||||||
}
|
|
||||||
unsigned idx;
|
|
||||||
if (varNodes[i]->hasEvidence()) {
|
|
||||||
idx = varNodes[i]->getEvidence();
|
|
||||||
} else {
|
|
||||||
idx = dsize;
|
|
||||||
}
|
}
|
||||||
|
unsigned idx = varNodes[i]->hasEvidence()
|
||||||
|
? varNodes[i]->getEvidence()
|
||||||
|
: range;
|
||||||
Colors& stateColors = it->second;
|
Colors& stateColors = it->second;
|
||||||
if (stateColors[idx] == -1) {
|
if (stateColors[idx] == -1) {
|
||||||
stateColors[idx] = getFreeColor();
|
stateColors[idx] = getFreeColor();
|
||||||
@ -91,7 +89,7 @@ CFactorGraph::setInitialColors (void)
|
|||||||
const FacNodes& facNodes = groundFg_->facNodes();
|
const FacNodes& facNodes = groundFg_->facNodes();
|
||||||
// create the initial factor colors
|
// create the initial factor colors
|
||||||
DistColorMap distColors;
|
DistColorMap distColors;
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
unsigned distId = facNodes[i]->factor().distId();
|
unsigned distId = facNodes[i]->factor().distId();
|
||||||
DistColorMap::iterator it = distColors.find (distId);
|
DistColorMap::iterator it = distColors.find (distId);
|
||||||
if (it == distColors.end()) {
|
if (it == distColors.end()) {
|
||||||
@ -117,9 +115,9 @@ CFactorGraph::createGroups (void)
|
|||||||
nIters ++;
|
nIters ++;
|
||||||
|
|
||||||
// set a new color to the variables with the same signature
|
// set a new color to the variables with the same signature
|
||||||
unsigned prevVarGroupsSize = varGroups.size();
|
size_t prevVarGroupsSize = varGroups.size();
|
||||||
varGroups.clear();
|
varGroups.clear();
|
||||||
for (unsigned i = 0; i < varNodes.size(); i++) {
|
for (size_t i = 0; i < varNodes.size(); i++) {
|
||||||
const VarSignature& signature = getSignature (varNodes[i]);
|
const VarSignature& signature = getSignature (varNodes[i]);
|
||||||
VarSignMap::iterator it = varGroups.find (signature);
|
VarSignMap::iterator it = varGroups.find (signature);
|
||||||
if (it == varGroups.end()) {
|
if (it == varGroups.end()) {
|
||||||
@ -128,18 +126,18 @@ CFactorGraph::createGroups (void)
|
|||||||
it->second.push_back (varNodes[i]);
|
it->second.push_back (varNodes[i]);
|
||||||
}
|
}
|
||||||
for (VarSignMap::iterator it = varGroups.begin();
|
for (VarSignMap::iterator it = varGroups.begin();
|
||||||
it != varGroups.end(); it++) {
|
it != varGroups.end(); ++it) {
|
||||||
Color newColor = getFreeColor();
|
Color newColor = getFreeColor();
|
||||||
VarNodes& groupMembers = it->second;
|
VarNodes& groupMembers = it->second;
|
||||||
for (unsigned i = 0; i < groupMembers.size(); i++) {
|
for (size_t i = 0; i < groupMembers.size(); i++) {
|
||||||
setColor (groupMembers[i], newColor);
|
setColor (groupMembers[i], newColor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned prevFactorGroupsSize = facGroups.size();
|
size_t prevFactorGroupsSize = facGroups.size();
|
||||||
facGroups.clear();
|
facGroups.clear();
|
||||||
// set a new color to the factors with the same signature
|
// set a new color to the factors with the same signature
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
const FacSignature& signature = getSignature (facNodes[i]);
|
const FacSignature& signature = getSignature (facNodes[i]);
|
||||||
FacSignMap::iterator it = facGroups.find (signature);
|
FacSignMap::iterator it = facGroups.find (signature);
|
||||||
if (it == facGroups.end()) {
|
if (it == facGroups.end()) {
|
||||||
@ -148,10 +146,10 @@ CFactorGraph::createGroups (void)
|
|||||||
it->second.push_back (facNodes[i]);
|
it->second.push_back (facNodes[i]);
|
||||||
}
|
}
|
||||||
for (FacSignMap::iterator it = facGroups.begin();
|
for (FacSignMap::iterator it = facGroups.begin();
|
||||||
it != facGroups.end(); it++) {
|
it != facGroups.end(); ++it) {
|
||||||
Color newColor = getFreeColor();
|
Color newColor = getFreeColor();
|
||||||
FacNodes& groupMembers = it->second;
|
FacNodes& groupMembers = it->second;
|
||||||
for (unsigned i = 0; i < groupMembers.size(); i++) {
|
for (size_t i = 0; i < groupMembers.size(); i++) {
|
||||||
setColor (groupMembers[i], newColor);
|
setColor (groupMembers[i], newColor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,10 +170,10 @@ CFactorGraph::createClusters (
|
|||||||
{
|
{
|
||||||
varClusters_.reserve (varGroups.size());
|
varClusters_.reserve (varGroups.size());
|
||||||
for (VarSignMap::const_iterator it = varGroups.begin();
|
for (VarSignMap::const_iterator it = varGroups.begin();
|
||||||
it != varGroups.end(); it++) {
|
it != varGroups.end(); ++it) {
|
||||||
const VarNodes& groupVars = it->second;
|
const VarNodes& groupVars = it->second;
|
||||||
VarCluster* vc = new VarCluster (groupVars);
|
VarCluster* vc = new VarCluster (groupVars);
|
||||||
for (unsigned i = 0; i < groupVars.size(); i++) {
|
for (size_t i = 0; i < groupVars.size(); i++) {
|
||||||
vid2VarCluster_.insert (make_pair (groupVars[i]->varId(), vc));
|
vid2VarCluster_.insert (make_pair (groupVars[i]->varId(), vc));
|
||||||
}
|
}
|
||||||
varClusters_.push_back (vc);
|
varClusters_.push_back (vc);
|
||||||
@ -183,12 +181,12 @@ CFactorGraph::createClusters (
|
|||||||
|
|
||||||
facClusters_.reserve (facGroups.size());
|
facClusters_.reserve (facGroups.size());
|
||||||
for (FacSignMap::const_iterator it = facGroups.begin();
|
for (FacSignMap::const_iterator it = facGroups.begin();
|
||||||
it != facGroups.end(); it++) {
|
it != facGroups.end(); ++it) {
|
||||||
FacNode* groupFactor = it->second[0];
|
FacNode* groupFactor = it->second[0];
|
||||||
const VarNodes& neighs = groupFactor->neighbors();
|
const VarNodes& neighs = groupFactor->neighbors();
|
||||||
VarClusters varClusters;
|
VarClusters varClusters;
|
||||||
varClusters.reserve (neighs.size());
|
varClusters.reserve (neighs.size());
|
||||||
for (unsigned i = 0; i < neighs.size(); i++) {
|
for (size_t i = 0; i < neighs.size(); i++) {
|
||||||
VarId vid = neighs[i]->varId();
|
VarId vid = neighs[i]->varId();
|
||||||
varClusters.push_back (vid2VarCluster_.find (vid)->second);
|
varClusters.push_back (vid2VarCluster_.find (vid)->second);
|
||||||
}
|
}
|
||||||
@ -204,7 +202,7 @@ CFactorGraph::getSignature (const VarNode* varNode)
|
|||||||
const FacNodes& neighs = varNode->neighbors();
|
const FacNodes& neighs = varNode->neighbors();
|
||||||
VarSignature sign;
|
VarSignature sign;
|
||||||
sign.reserve (neighs.size() + 1);
|
sign.reserve (neighs.size() + 1);
|
||||||
for (unsigned i = 0; i < neighs.size(); i++) {
|
for (size_t i = 0; i < neighs.size(); i++) {
|
||||||
sign.push_back (make_pair (
|
sign.push_back (make_pair (
|
||||||
getColor (neighs[i]),
|
getColor (neighs[i]),
|
||||||
neighs[i]->factor().indexOf (varNode->varId())));
|
neighs[i]->factor().indexOf (varNode->varId())));
|
||||||
@ -222,7 +220,7 @@ CFactorGraph::getSignature (const FacNode* facNode)
|
|||||||
const VarNodes& neighs = facNode->neighbors();
|
const VarNodes& neighs = facNode->neighbors();
|
||||||
FacSignature sign;
|
FacSignature sign;
|
||||||
sign.reserve (neighs.size() + 1);
|
sign.reserve (neighs.size() + 1);
|
||||||
for (unsigned i = 0; i < neighs.size(); i++) {
|
for (size_t i = 0; i < neighs.size(); i++) {
|
||||||
sign.push_back (getColor (neighs[i]));
|
sign.push_back (getColor (neighs[i]));
|
||||||
}
|
}
|
||||||
sign.push_back (getColor (facNode));
|
sign.push_back (getColor (facNode));
|
||||||
@ -235,15 +233,15 @@ FactorGraph*
|
|||||||
CFactorGraph::getGroundFactorGraph (void)
|
CFactorGraph::getGroundFactorGraph (void)
|
||||||
{
|
{
|
||||||
FactorGraph* fg = new FactorGraph();
|
FactorGraph* fg = new FactorGraph();
|
||||||
for (unsigned i = 0; i < varClusters_.size(); i++) {
|
for (size_t i = 0; i < varClusters_.size(); i++) {
|
||||||
VarNode* newVar = new VarNode (varClusters_[i]->first());
|
VarNode* newVar = new VarNode (varClusters_[i]->first());
|
||||||
varClusters_[i]->setRepresentative (newVar);
|
varClusters_[i]->setRepresentative (newVar);
|
||||||
fg->addVarNode (newVar);
|
fg->addVarNode (newVar);
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facClusters_.size(); i++) {
|
for (size_t i = 0; i < facClusters_.size(); i++) {
|
||||||
Vars vars;
|
Vars vars;
|
||||||
const VarClusters& clusters = facClusters_[i]->varClusters();
|
const VarClusters& clusters = facClusters_[i]->varClusters();
|
||||||
for (unsigned j = 0; j < clusters.size(); j++) {
|
for (size_t j = 0; j < clusters.size(); j++) {
|
||||||
vars.push_back (clusters[j]->representative());
|
vars.push_back (clusters[j]->representative());
|
||||||
}
|
}
|
||||||
const Factor& groundFac = facClusters_[i]->first()->factor();
|
const Factor& groundFac = facClusters_[i]->first()->factor();
|
||||||
@ -251,7 +249,7 @@ CFactorGraph::getGroundFactorGraph (void)
|
|||||||
vars, groundFac.params(), groundFac.distId()));
|
vars, groundFac.params(), groundFac.distId()));
|
||||||
facClusters_[i]->setRepresentative (fn);
|
facClusters_[i]->setRepresentative (fn);
|
||||||
fg->addFacNode (fn);
|
fg->addFacNode (fn);
|
||||||
for (unsigned j = 0; j < vars.size(); j++) {
|
for (size_t j = 0; j < vars.size(); j++) {
|
||||||
fg->addEdge (static_cast<VarNode*> (vars[j]), fn);
|
fg->addEdge (static_cast<VarNode*> (vars[j]), fn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -264,17 +262,17 @@ unsigned
|
|||||||
CFactorGraph::getEdgeCount (
|
CFactorGraph::getEdgeCount (
|
||||||
const FacCluster* fc,
|
const FacCluster* fc,
|
||||||
const VarCluster* vc,
|
const VarCluster* vc,
|
||||||
unsigned index) const
|
size_t index) const
|
||||||
{
|
{
|
||||||
unsigned count = 0;
|
unsigned count = 0;
|
||||||
VarId reprVid = vc->representative()->varId();
|
VarId reprVid = vc->representative()->varId();
|
||||||
VarNode* groundVar = groundFg_->getVarNode (reprVid);
|
VarNode* groundVar = groundFg_->getVarNode (reprVid);
|
||||||
const FacNodes& neighs = groundVar->neighbors();
|
const FacNodes& neighs = groundVar->neighbors();
|
||||||
for (unsigned i = 0; i < neighs.size(); i++) {
|
for (size_t i = 0; i < neighs.size(); i++) {
|
||||||
FacNodes::const_iterator it;
|
FacNodes::const_iterator it;
|
||||||
it = std::find (fc->members().begin(), fc->members().end(), neighs[i]);
|
it = std::find (fc->members().begin(), fc->members().end(), neighs[i]);
|
||||||
if (it != fc->members().end() &&
|
if (it != fc->members().end() &&
|
||||||
(*it)->factor().indexOf (reprVid) == (int)index) {
|
(*it)->factor().indexOf (reprVid) == index) {
|
||||||
count ++;
|
count ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,11 +289,11 @@ CFactorGraph::printGroups (
|
|||||||
unsigned count = 1;
|
unsigned count = 1;
|
||||||
cout << "variable groups:" << endl;
|
cout << "variable groups:" << endl;
|
||||||
for (VarSignMap::const_iterator it = varGroups.begin();
|
for (VarSignMap::const_iterator it = varGroups.begin();
|
||||||
it != varGroups.end(); it++) {
|
it != varGroups.end(); ++it) {
|
||||||
const VarNodes& groupMembers = it->second;
|
const VarNodes& groupMembers = it->second;
|
||||||
if (groupMembers.size() > 0) {
|
if (groupMembers.size() > 0) {
|
||||||
cout << count << ": " ;
|
cout << count << ": " ;
|
||||||
for (unsigned i = 0; i < groupMembers.size(); i++) {
|
for (size_t i = 0; i < groupMembers.size(); i++) {
|
||||||
cout << groupMembers[i]->label() << " " ;
|
cout << groupMembers[i]->label() << " " ;
|
||||||
}
|
}
|
||||||
count ++;
|
count ++;
|
||||||
@ -305,11 +303,11 @@ CFactorGraph::printGroups (
|
|||||||
count = 1;
|
count = 1;
|
||||||
cout << endl << "factor groups:" << endl;
|
cout << endl << "factor groups:" << endl;
|
||||||
for (FacSignMap::const_iterator it = facGroups.begin();
|
for (FacSignMap::const_iterator it = facGroups.begin();
|
||||||
it != facGroups.end(); it++) {
|
it != facGroups.end(); ++it) {
|
||||||
const FacNodes& groupMembers = it->second;
|
const FacNodes& groupMembers = it->second;
|
||||||
if (groupMembers.size() > 0) {
|
if (groupMembers.size() > 0) {
|
||||||
cout << ++count << ": " ;
|
cout << ++count << ": " ;
|
||||||
for (unsigned i = 0; i < groupMembers.size(); i++) {
|
for (size_t i = 0; i < groupMembers.size(); i++) {
|
||||||
cout << groupMembers[i]->getLabel() << " " ;
|
cout << groupMembers[i]->getLabel() << " " ;
|
||||||
}
|
}
|
||||||
count ++;
|
count ++;
|
||||||
|
@ -35,7 +35,7 @@ struct VarSignatureHash
|
|||||||
size_t operator() (const VarSignature &sig) const
|
size_t operator() (const VarSignature &sig) const
|
||||||
{
|
{
|
||||||
size_t val = hash<size_t>()(sig.size());
|
size_t val = hash<size_t>()(sig.size());
|
||||||
for (unsigned i = 0; i < sig.size(); i++) {
|
for (size_t i = 0; i < sig.size(); i++) {
|
||||||
val ^= hash<size_t>()(sig[i].first);
|
val ^= hash<size_t>()(sig[i].first);
|
||||||
val ^= hash<size_t>()(sig[i].second);
|
val ^= hash<size_t>()(sig[i].second);
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ struct FacSignatureHash
|
|||||||
size_t operator() (const FacSignature &sig) const
|
size_t operator() (const FacSignature &sig) const
|
||||||
{
|
{
|
||||||
size_t val = hash<size_t>()(sig.size());
|
size_t val = hash<size_t>()(sig.size());
|
||||||
for (unsigned i = 0; i < sig.size(); i++) {
|
for (size_t i = 0; i < sig.size(); i++) {
|
||||||
val ^= hash<size_t>()(sig[i]);
|
val ^= hash<size_t>()(sig[i]);
|
||||||
}
|
}
|
||||||
return val;
|
return val;
|
||||||
@ -120,7 +120,7 @@ class CFactorGraph
|
|||||||
FactorGraph* getGroundFactorGraph (void);
|
FactorGraph* getGroundFactorGraph (void);
|
||||||
|
|
||||||
unsigned getEdgeCount (const FacCluster*,
|
unsigned getEdgeCount (const FacCluster*,
|
||||||
const VarCluster*, unsigned index) const;
|
const VarCluster*, size_t index) const;
|
||||||
|
|
||||||
static bool checkForIdenticalFactors;
|
static bool checkForIdenticalFactors;
|
||||||
|
|
||||||
|
@ -3,27 +3,8 @@
|
|||||||
|
|
||||||
CbpSolver::CbpSolver (const FactorGraph& fg) : BpSolver (fg)
|
CbpSolver::CbpSolver (const FactorGraph& fg) : BpSolver (fg)
|
||||||
{
|
{
|
||||||
unsigned nrGroundVars, nrGroundFacs, nrNeighborless;
|
|
||||||
if (Constants::COLLECT_STATS) {
|
|
||||||
nrGroundVars = fg_->varNodes().size();
|
|
||||||
nrGroundFacs = fg_->facNodes().size();
|
|
||||||
const VarNodes& vars = fg_->varNodes();
|
|
||||||
nrNeighborless = 0;
|
|
||||||
for (unsigned i = 0; i < vars.size(); i++) {
|
|
||||||
const FacNodes& factors = vars[i]->neighbors();
|
|
||||||
if (factors.size() == 1 && factors[0]->neighbors().size() == 1) {
|
|
||||||
nrNeighborless ++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cfg_ = new CFactorGraph (fg);
|
cfg_ = new CFactorGraph (fg);
|
||||||
fg_ = cfg_->getGroundFactorGraph();
|
fg_ = cfg_->getGroundFactorGraph();
|
||||||
if (Constants::COLLECT_STATS) {
|
|
||||||
unsigned nrClusterVars = fg_->varNodes().size();
|
|
||||||
unsigned nrClusterFacs = fg_->facNodes().size();
|
|
||||||
Statistics::updateCompressingStatistics (nrGroundVars,
|
|
||||||
nrGroundFacs, nrClusterVars, nrClusterFacs, nrNeighborless);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -32,7 +13,7 @@ CbpSolver::~CbpSolver (void)
|
|||||||
{
|
{
|
||||||
delete cfg_;
|
delete cfg_;
|
||||||
delete fg_;
|
delete fg_;
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
delete links_[i];
|
delete links_[i];
|
||||||
}
|
}
|
||||||
links_.clear();
|
links_.clear();
|
||||||
@ -80,16 +61,16 @@ CbpSolver::getPosterioriOf (VarId vid)
|
|||||||
probs.resize (var->range(), LogAware::multIdenty());
|
probs.resize (var->range(), LogAware::multIdenty());
|
||||||
const SpLinkSet& links = ninf(var)->getLinks();
|
const SpLinkSet& links = ninf(var)->getLinks();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
|
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
|
||||||
Util::add (probs, l->poweredMessage());
|
probs += l->poweredMessage();
|
||||||
}
|
}
|
||||||
LogAware::normalize (probs);
|
LogAware::normalize (probs);
|
||||||
Util::fromLog (probs);
|
Util::exp (probs);
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
|
CbpSolverLink* l = static_cast<CbpSolverLink*> (links[i]);
|
||||||
Util::multiply (probs, l->poweredMessage());
|
probs *= l->poweredMessage();
|
||||||
}
|
}
|
||||||
LogAware::normalize (probs);
|
LogAware::normalize (probs);
|
||||||
}
|
}
|
||||||
@ -103,7 +84,7 @@ Params
|
|||||||
CbpSolver::getJointDistributionOf (const VarIds& jointVids)
|
CbpSolver::getJointDistributionOf (const VarIds& jointVids)
|
||||||
{
|
{
|
||||||
VarIds eqVarIds;
|
VarIds eqVarIds;
|
||||||
for (unsigned i = 0; i < jointVids.size(); i++) {
|
for (size_t i = 0; i < jointVids.size(); i++) {
|
||||||
VarNode* vn = cfg_->getEquivalent (jointVids[i]);
|
VarNode* vn = cfg_->getEquivalent (jointVids[i]);
|
||||||
eqVarIds.push_back (vn->varId());
|
eqVarIds.push_back (vn->varId());
|
||||||
}
|
}
|
||||||
@ -122,9 +103,9 @@ CbpSolver::createLinks (void)
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
const FacClusters& fcs = cfg_->facClusters();
|
const FacClusters& fcs = cfg_->facClusters();
|
||||||
for (unsigned i = 0; i < fcs.size(); i++) {
|
for (size_t i = 0; i < fcs.size(); i++) {
|
||||||
const VarClusters& vcs = fcs[i]->varClusters();
|
const VarClusters& vcs = fcs[i]->varClusters();
|
||||||
for (unsigned j = 0; j < vcs.size(); j++) {
|
for (size_t j = 0; j < vcs.size(); j++) {
|
||||||
unsigned count = cfg_->getEdgeCount (fcs[i], vcs[j], j);
|
unsigned count = cfg_->getEdgeCount (fcs[i], vcs[j], j);
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << "creating link " ;
|
cout << "creating link " ;
|
||||||
@ -148,7 +129,7 @@ void
|
|||||||
CbpSolver::maxResidualSchedule (void)
|
CbpSolver::maxResidualSchedule (void)
|
||||||
{
|
{
|
||||||
if (nIters_ == 1) {
|
if (nIters_ == 1) {
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
calculateMessage (links_[i]);
|
calculateMessage (links_[i]);
|
||||||
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
|
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
|
||||||
linkMap_.insert (make_pair (links_[i], it));
|
linkMap_.insert (make_pair (links_[i], it));
|
||||||
@ -159,11 +140,11 @@ CbpSolver::maxResidualSchedule (void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned c = 0; c < links_.size(); c++) {
|
for (size_t c = 0; c < links_.size(); c++) {
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << endl << "current residuals:" << endl;
|
cout << endl << "current residuals:" << endl;
|
||||||
for (SortedOrder::iterator it = sortedOrder_.begin();
|
for (SortedOrder::iterator it = sortedOrder_.begin();
|
||||||
it != sortedOrder_.end(); it ++) {
|
it != sortedOrder_.end(); ++it) {
|
||||||
cout << " " << setw (30) << left << (*it)->toString();
|
cout << " " << setw (30) << left << (*it)->toString();
|
||||||
cout << "residual = " << (*it)->getResidual() << endl;
|
cout << "residual = " << (*it)->getResidual() << endl;
|
||||||
}
|
}
|
||||||
@ -184,9 +165,9 @@ CbpSolver::maxResidualSchedule (void)
|
|||||||
|
|
||||||
// update the messages that depend on message source --> destin
|
// update the messages that depend on message source --> destin
|
||||||
const FacNodes& factorNeighbors = link->getVariable()->neighbors();
|
const FacNodes& factorNeighbors = link->getVariable()->neighbors();
|
||||||
for (unsigned i = 0; i < factorNeighbors.size(); i++) {
|
for (size_t i = 0; i < factorNeighbors.size(); i++) {
|
||||||
const SpLinkSet& links = ninf(factorNeighbors[i])->getLinks();
|
const SpLinkSet& links = ninf(factorNeighbors[i])->getLinks();
|
||||||
for (unsigned j = 0; j < links.size(); j++) {
|
for (size_t j = 0; j < links.size(); j++) {
|
||||||
if (links[j]->getVariable() != link->getVariable()) {
|
if (links[j]->getVariable() != link->getVariable()) {
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << " calculating " << links[j]->toString() << endl;
|
cout << " calculating " << links[j]->toString() << endl;
|
||||||
@ -201,7 +182,7 @@ CbpSolver::maxResidualSchedule (void)
|
|||||||
// in counting bp, the message that a variable X sends to
|
// 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
|
// 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->getFactor())->getLinks();
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
if (links[i]->getVariable() != link->getVariable()) {
|
if (links[i]->getVariable() != link->getVariable()) {
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << " calculating " << links[i]->toString() << endl;
|
cout << " calculating " << links[i]->toString() << endl;
|
||||||
@ -227,13 +208,13 @@ CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
|
|||||||
// calculate the product of messages that were sent
|
// calculate the product of messages that were sent
|
||||||
// to factor `src', except from var `dst'
|
// to factor `src', except from var `dst'
|
||||||
unsigned msgSize = 1;
|
unsigned msgSize = 1;
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
msgSize *= links[i]->getVariable()->range();
|
msgSize *= links[i]->getVariable()->range();
|
||||||
}
|
}
|
||||||
unsigned repetitions = 1;
|
unsigned repetitions = 1;
|
||||||
Params msgProduct (msgSize, LogAware::multIdenty());
|
Params msgProduct (msgSize, LogAware::multIdenty());
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (int i = links.size() - 1; i >= 0; i--) {
|
for (size_t i = links.size(); i-- > 0; ) {
|
||||||
const CbpSolverLink* cl = static_cast<const CbpSolverLink*> (links[i]);
|
const CbpSolverLink* cl = static_cast<const CbpSolverLink*> (links[i]);
|
||||||
if ( ! (cl->getVariable() == dst && cl->index() == link->index())) {
|
if ( ! (cl->getVariable() == dst && cl->index() == link->index())) {
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
@ -252,7 +233,7 @@ CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = links.size() - 1; i >= 0; i--) {
|
for (size_t i = links.size(); i-- > 0; ) {
|
||||||
const CbpSolverLink* cl = static_cast<const CbpSolverLink*> (links[i]);
|
const CbpSolverLink* cl = static_cast<const CbpSolverLink*> (links[i]);
|
||||||
if ( ! (cl->getVariable() == dst && cl->index() == link->index())) {
|
if ( ! (cl->getVariable() == dst && cl->index() == link->index())) {
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
@ -275,11 +256,11 @@ CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
|
|||||||
src->factor().ranges(), msgProduct);
|
src->factor().ranges(), msgProduct);
|
||||||
assert (msgProduct.size() == src->factor().size());
|
assert (msgProduct.size() == src->factor().size());
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < result.size(); i++) {
|
for (size_t i = 0; i < result.size(); i++) {
|
||||||
result[i] += src->factor()[i];
|
result[i] += src->factor()[i];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < result.size(); i++) {
|
for (size_t i = 0; i < result.size(); i++) {
|
||||||
result[i] *= src->factor()[i];
|
result[i] *= src->factor()[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -326,18 +307,18 @@ CbpSolver::getVar2FactorMsg (const SpLink* _link) const
|
|||||||
}
|
}
|
||||||
const SpLinkSet& links = ninf(src)->getLinks();
|
const SpLinkSet& links = ninf(src)->getLinks();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
|
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
|
||||||
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
|
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
|
||||||
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
|
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
|
||||||
Util::add (msg, cl->poweredMessage());
|
msg += cl->poweredMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
|
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
|
||||||
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
|
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
|
||||||
Util::multiply (msg, cl->poweredMessage());
|
msg *= cl->poweredMessage();
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
cout << " x " << cl->getNextMessage() << "^" << link->nrEdges();
|
cout << " x " << cl->getNextMessage() << "^" << link->nrEdges();
|
||||||
}
|
}
|
||||||
@ -355,7 +336,7 @@ CbpSolver::getVar2FactorMsg (const SpLink* _link) const
|
|||||||
void
|
void
|
||||||
CbpSolver::printLinkInformation (void) const
|
CbpSolver::printLinkInformation (void) const
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < links_.size(); i++) {
|
for (size_t i = 0; i < links_.size(); i++) {
|
||||||
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links_[i]);
|
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links_[i]);
|
||||||
cout << cl->toString() << ":" << endl;
|
cout << cl->toString() << ":" << endl;
|
||||||
cout << " curr msg = " << cl->getMessage() << endl;
|
cout << " curr msg = " << cl->getMessage() << endl;
|
||||||
|
@ -9,13 +9,13 @@ class Factor;
|
|||||||
class CbpSolverLink : public SpLink
|
class CbpSolverLink : public SpLink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CbpSolverLink (FacNode* fn, VarNode* vn, unsigned idx, unsigned count)
|
CbpSolverLink (FacNode* fn, VarNode* vn, size_t idx, unsigned count)
|
||||||
: SpLink (fn, vn), index_(idx), nrEdges_(count),
|
: SpLink (fn, vn), index_(idx), nrEdges_(count),
|
||||||
pwdMsg_(vn->range(), LogAware::one()) { }
|
pwdMsg_(vn->range(), LogAware::one()) { }
|
||||||
|
|
||||||
unsigned index (void) const { return index_; }
|
size_t index (void) const { return index_; }
|
||||||
|
|
||||||
unsigned nrEdges (void) const { return nrEdges_; }
|
unsigned nrEdges (void) const { return nrEdges_; }
|
||||||
|
|
||||||
const Params& poweredMessage (void) const { return pwdMsg_; }
|
const Params& poweredMessage (void) const { return pwdMsg_; }
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ class CbpSolverLink : public SpLink
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned index_;
|
size_t index_;
|
||||||
unsigned nrEdges_;
|
unsigned nrEdges_;
|
||||||
Params pwdMsg_;
|
Params pwdMsg_;
|
||||||
};
|
};
|
||||||
|
@ -178,7 +178,7 @@ ConstraintTree::ConstraintTree (
|
|||||||
root_ = new CTNode (0, 0);
|
root_ = new CTNode (0, 0);
|
||||||
logVars_ = logVars;
|
logVars_ = logVars;
|
||||||
logVarSet_ = LogVarSet (logVars);
|
logVarSet_ = LogVarSet (logVars);
|
||||||
for (unsigned i = 0; i < tuples.size(); i++) {
|
for (size_t i = 0; i < tuples.size(); i++) {
|
||||||
addTuple (tuples[i]);
|
addTuple (tuples[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -205,7 +205,7 @@ void
|
|||||||
ConstraintTree::addTuple (const Tuple& tuple)
|
ConstraintTree::addTuple (const Tuple& tuple)
|
||||||
{
|
{
|
||||||
CTNode* prevNode = root_;
|
CTNode* prevNode = root_;
|
||||||
for (unsigned i = 0; i < tuple.size(); i++) {
|
for (size_t i = 0; i < tuple.size(); i++) {
|
||||||
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
|
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
|
||||||
if (it == prevNode->childs().end()) {
|
if (it == prevNode->childs().end()) {
|
||||||
CTNode* newNode = new CTNode (tuple[i], i + 1);
|
CTNode* newNode = new CTNode (tuple[i], i + 1);
|
||||||
@ -223,7 +223,7 @@ bool
|
|||||||
ConstraintTree::containsTuple (const Tuple& tuple)
|
ConstraintTree::containsTuple (const Tuple& tuple)
|
||||||
{
|
{
|
||||||
CTNode* prevNode = root_;
|
CTNode* prevNode = root_;
|
||||||
for (unsigned i = 0; i < tuple.size(); i++) {
|
for (size_t i = 0; i < tuple.size(); i++) {
|
||||||
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
|
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
|
||||||
if (it == prevNode->childs().end()) {
|
if (it == prevNode->childs().end()) {
|
||||||
return false;
|
return false;
|
||||||
@ -239,11 +239,11 @@ ConstraintTree::containsTuple (const Tuple& tuple)
|
|||||||
void
|
void
|
||||||
ConstraintTree::moveToTop (const LogVars& lvs)
|
ConstraintTree::moveToTop (const LogVars& lvs)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < lvs.size(); i++) {
|
for (size_t i = 0; i < lvs.size(); i++) {
|
||||||
int pos = Util::indexOf (logVars_, lvs[i]);
|
size_t pos = Util::indexOf (logVars_, lvs[i]);
|
||||||
assert (pos != -1);
|
assert (pos != logVars_.size());
|
||||||
for (int j = pos; j > (int)i; j--) {
|
for (size_t j = pos; j-- > i; ) {
|
||||||
swapLogVar (logVars_[j-1]);
|
swapLogVar (logVars_[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,13 +253,11 @@ ConstraintTree::moveToTop (const LogVars& lvs)
|
|||||||
void
|
void
|
||||||
ConstraintTree::moveToBottom (const LogVars& lvs)
|
ConstraintTree::moveToBottom (const LogVars& lvs)
|
||||||
{
|
{
|
||||||
for (int i = lvs.size() - 1; i >= 0; i--) {
|
for (size_t i = lvs.size(); i-- > 0; ) {
|
||||||
LogVars::iterator it =
|
size_t pos = Util::indexOf (logVars_, lvs[i]);
|
||||||
std::find (logVars_.begin(), logVars_.end(), lvs[i]);
|
assert (pos != logVars_.size());
|
||||||
assert (it != logVars_.end());
|
size_t stop = logVars_.size() - (lvs.size() - i - 1);
|
||||||
int pos = Util::indexOf (logVars_, lvs[i]);
|
for (size_t j = pos; j < stop - 1; j++) {
|
||||||
int stop = logVars_.size() - (lvs.size() - i - 1);
|
|
||||||
for (int j = pos; j < stop - 1; j++) {
|
|
||||||
swapLogVar (logVars_[j]);
|
swapLogVar (logVars_[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,7 +289,7 @@ ConstraintTree::join (ConstraintTree* ct, bool oneTwoOne)
|
|||||||
}
|
}
|
||||||
LogVarSet intersect = logVarSet_ & ct->logVarSet_;
|
LogVarSet intersect = logVarSet_ & ct->logVarSet_;
|
||||||
if (intersect.empty()) {
|
if (intersect.empty()) {
|
||||||
// carteesian product
|
// cartesian product
|
||||||
appendOnBottom (root_, ct->root()->childs());
|
appendOnBottom (root_, ct->root()->childs());
|
||||||
Util::addToVector (logVars_, ct->logVars_);
|
Util::addToVector (logVars_, ct->logVars_);
|
||||||
logVarSet_ |= ct->logVarSet_;
|
logVarSet_ |= ct->logVarSet_;
|
||||||
@ -305,7 +303,7 @@ ConstraintTree::join (ConstraintTree* ct, bool oneTwoOne)
|
|||||||
tuples, appendNodes);
|
tuples, appendNodes);
|
||||||
|
|
||||||
CTNodes::const_iterator appendIt = appendNodes.begin();
|
CTNodes::const_iterator appendIt = appendNodes.begin();
|
||||||
for (unsigned i = 0; i < tuples.size(); ++ i, ++ appendIt) {
|
for (size_t i = 0; i < tuples.size(); ++ i, ++ appendIt) {
|
||||||
bool tupleFounded = join (root_, tuples[i], 0, *appendIt);
|
bool tupleFounded = join (root_, tuples[i], 0, *appendIt);
|
||||||
if (oneTwoOne && tupleFounded == false) {
|
if (oneTwoOne && tupleFounded == false) {
|
||||||
assert (false);
|
assert (false);
|
||||||
@ -338,7 +336,7 @@ ConstraintTree::rename (LogVar X_old, LogVar X_new)
|
|||||||
assert (logVarSet_.contains (X_new) == false);
|
assert (logVarSet_.contains (X_new) == false);
|
||||||
logVarSet_ -= X_old;
|
logVarSet_ -= X_old;
|
||||||
logVarSet_ |= X_new;
|
logVarSet_ |= X_new;
|
||||||
for (unsigned i = 0; i < logVars_.size(); i++) {
|
for (size_t i = 0; i < logVars_.size(); i++) {
|
||||||
if (logVars_[i] == X_old) {
|
if (logVars_[i] == X_old) {
|
||||||
logVars_[i] = X_new;
|
logVars_[i] = X_new;
|
||||||
return;
|
return;
|
||||||
@ -352,7 +350,7 @@ ConstraintTree::rename (LogVar X_old, LogVar X_new)
|
|||||||
void
|
void
|
||||||
ConstraintTree::applySubstitution (const Substitution& theta)
|
ConstraintTree::applySubstitution (const Substitution& theta)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < logVars_.size(); i++) {
|
for (size_t i = 0; i < logVars_.size(); i++) {
|
||||||
logVars_[i] = theta.newNameFor (logVars_[i]);
|
logVars_[i] = theta.newNameFor (logVars_[i]);
|
||||||
}
|
}
|
||||||
logVarSet_ = LogVarSet (logVars_);
|
logVarSet_ = LogVarSet (logVars_);
|
||||||
@ -421,7 +419,7 @@ LogVarSet
|
|||||||
ConstraintTree::singletons (void)
|
ConstraintTree::singletons (void)
|
||||||
{
|
{
|
||||||
LogVarSet singletons;
|
LogVarSet singletons;
|
||||||
for (unsigned i = 0; i < logVars_.size(); i++) {
|
for (size_t i = 0; i < logVars_.size(); i++) {
|
||||||
if (isSingleton (logVars_[i])) {
|
if (isSingleton (logVars_[i])) {
|
||||||
singletons.insert (logVars_[i]);
|
singletons.insert (logVars_[i]);
|
||||||
}
|
}
|
||||||
@ -449,7 +447,7 @@ TupleSet
|
|||||||
ConstraintTree::tupleSet (const LogVars& originalLvs)
|
ConstraintTree::tupleSet (const LogVars& originalLvs)
|
||||||
{
|
{
|
||||||
LogVars uniqueLvs;
|
LogVars uniqueLvs;
|
||||||
for (unsigned i = 0; i < originalLvs.size(); i++) {
|
for (size_t i = 0; i < originalLvs.size(); i++) {
|
||||||
if (Util::contains (uniqueLvs, originalLvs[i]) == false) {
|
if (Util::contains (uniqueLvs, originalLvs[i]) == false) {
|
||||||
uniqueLvs.push_back (originalLvs[i]);
|
uniqueLvs.push_back (originalLvs[i]);
|
||||||
}
|
}
|
||||||
@ -461,17 +459,17 @@ ConstraintTree::tupleSet (const LogVars& originalLvs)
|
|||||||
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
|
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
|
||||||
|
|
||||||
if (originalLvs.size() != uniqueLvs.size()) {
|
if (originalLvs.size() != uniqueLvs.size()) {
|
||||||
vector<int> indexes;
|
vector<size_t> indexes;
|
||||||
indexes.reserve (originalLvs.size());
|
indexes.reserve (originalLvs.size());
|
||||||
for (unsigned i = 0; i < originalLvs.size(); i++) {
|
for (size_t i = 0; i < originalLvs.size(); i++) {
|
||||||
indexes.push_back (Util::indexOf (uniqueLvs, originalLvs[i]));
|
indexes.push_back (Util::indexOf (uniqueLvs, originalLvs[i]));
|
||||||
}
|
}
|
||||||
Tuples tuples2;
|
Tuples tuples2;
|
||||||
tuples2.reserve (tuples.size());
|
tuples2.reserve (tuples.size());
|
||||||
for (unsigned i = 0; i < tuples.size(); i++) {
|
for (size_t i = 0; i < tuples.size(); i++) {
|
||||||
Tuple t;
|
Tuple t;
|
||||||
t.reserve (originalLvs.size());
|
t.reserve (originalLvs.size());
|
||||||
for (unsigned j = 0; j < originalLvs.size(); j++) {
|
for (size_t j = 0; j < originalLvs.size(); j++) {
|
||||||
t.push_back (tuples[i][indexes[j]]);
|
t.push_back (tuples[i][indexes[j]]);
|
||||||
}
|
}
|
||||||
tuples2.push_back (t);
|
tuples2.push_back (t);
|
||||||
@ -518,14 +516,14 @@ ConstraintTree::exportToGraphViz (
|
|||||||
}
|
}
|
||||||
if (showLogVars) {
|
if (showLogVars) {
|
||||||
out << "Root [label=\"\", shape=plaintext]" << endl;
|
out << "Root [label=\"\", shape=plaintext]" << endl;
|
||||||
for (unsigned i = 0; i < copy.logVars_.size(); i++) {
|
for (size_t i = 0; i < copy.logVars_.size(); i++) {
|
||||||
out << copy.logVars_[i] << " [label=" ;
|
out << copy.logVars_[i] << " [label=" ;
|
||||||
out << copy.logVars_[i] << ", " ;
|
out << copy.logVars_[i] << ", " ;
|
||||||
out << "shape=plaintext, fontsize=14]" << endl;
|
out << "shape=plaintext, fontsize=14]" << endl;
|
||||||
}
|
}
|
||||||
out << "Root -> " << copy.logVars_[0];
|
out << "Root -> " << copy.logVars_[0];
|
||||||
out << " [style=invis]" << endl;
|
out << " [style=invis]" << endl;
|
||||||
for (unsigned i = 0; i < copy.logVars_.size() - 1; i++) {
|
for (size_t i = 0; i < copy.logVars_.size() - 1; i++) {
|
||||||
out << copy.logVars_[i] << " -> " << copy.logVars_[i + 1];
|
out << copy.logVars_[i] << " -> " << copy.logVars_[i + 1];
|
||||||
out << " [style=invis]" << endl;
|
out << " [style=invis]" << endl;
|
||||||
}
|
}
|
||||||
@ -618,9 +616,9 @@ ConstraintTree::isCartesianProduct (const LogVarSet& Xs)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
moveToTop (Xs.elements());
|
moveToTop (Xs.elements());
|
||||||
for (unsigned i = 1; i < Xs.size(); i++) {
|
for (size_t i = 1; i < Xs.size(); i++) {
|
||||||
CTNodes nodes = getNodesAtLevel (i);
|
CTNodes nodes = getNodesAtLevel (i);
|
||||||
for (unsigned j = 1; j < nodes.size(); j++) {
|
for (size_t j = 1; j < nodes.size(); j++) {
|
||||||
if (nodes[j-1]->nrChilds() != nodes[ j ]->nrChilds()) {
|
if (nodes[j-1]->nrChilds() != nodes[ j ]->nrChilds()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -689,7 +687,7 @@ ConstraintTree::countNormalize (const LogVarSet& Ys)
|
|||||||
chIt != childs.end(); ++ chIt) {
|
chIt != childs.end(); ++ chIt) {
|
||||||
const vector<pair<CTNode*, unsigned>>& res =
|
const vector<pair<CTNode*, unsigned>>& res =
|
||||||
countNormalize (*chIt, stopLevel);
|
countNormalize (*chIt, stopLevel);
|
||||||
for (unsigned j = 0; j < res.size(); j++) {
|
for (size_t j = 0; j < res.size(); j++) {
|
||||||
unordered_map<unsigned, ConstraintTree*>::iterator it
|
unordered_map<unsigned, ConstraintTree*>::iterator it
|
||||||
= countMap.find (res[j].second);
|
= countMap.find (res[j].second);
|
||||||
if (it == countMap.end()) {
|
if (it == countMap.end()) {
|
||||||
@ -725,7 +723,7 @@ ConstraintTree::jointCountNormalize (
|
|||||||
|
|
||||||
ConstraintTrees normCts1 = commCt->countNormalize (X);
|
ConstraintTrees normCts1 = commCt->countNormalize (X);
|
||||||
vector<unsigned> counts1 (normCts1.size());
|
vector<unsigned> counts1 (normCts1.size());
|
||||||
for (unsigned i = 0; i < normCts1.size(); i++) {
|
for (size_t i = 0; i < normCts1.size(); i++) {
|
||||||
counts1[i] = normCts1[i]->getConditionalCount (X);
|
counts1[i] = normCts1[i]->getConditionalCount (X);
|
||||||
// cout << "normCts1[" << i << "] #" << counts1[i] ;
|
// cout << "normCts1[" << i << "] #" << counts1[i] ;
|
||||||
// cout << " " << normCts1[i]->tupleSet() << endl;
|
// cout << " " << normCts1[i]->tupleSet() << endl;
|
||||||
@ -733,7 +731,7 @@ ConstraintTree::jointCountNormalize (
|
|||||||
|
|
||||||
ConstraintTrees normCts2 = exclCt->countNormalize (X);
|
ConstraintTrees normCts2 = exclCt->countNormalize (X);
|
||||||
vector<unsigned> counts2 (normCts2.size());
|
vector<unsigned> counts2 (normCts2.size());
|
||||||
for (unsigned i = 0; i < normCts2.size(); i++) {
|
for (size_t i = 0; i < normCts2.size(); i++) {
|
||||||
counts2[i] = normCts2[i]->getConditionalCount (X);
|
counts2[i] = normCts2[i]->getConditionalCount (X);
|
||||||
// cout << "normCts2[" << i << "] #" << counts2[i] ;
|
// cout << "normCts2[" << i << "] #" << counts2[i] ;
|
||||||
// cout << " " << normCts2[i]->tupleSet() << endl;
|
// cout << " " << normCts2[i]->tupleSet() << endl;
|
||||||
@ -741,7 +739,7 @@ ConstraintTree::jointCountNormalize (
|
|||||||
// cout << endl;
|
// cout << endl;
|
||||||
|
|
||||||
ConstraintTree* excl1 = 0;
|
ConstraintTree* excl1 = 0;
|
||||||
for (unsigned i = 0; i < normCts1.size(); i++) {
|
for (size_t i = 0; i < normCts1.size(); i++) {
|
||||||
if (counts1[i] == N) {
|
if (counts1[i] == N) {
|
||||||
excl1 = normCts1[i];
|
excl1 = normCts1[i];
|
||||||
normCts1.erase (normCts1.begin() + i);
|
normCts1.erase (normCts1.begin() + i);
|
||||||
@ -752,7 +750,7 @@ ConstraintTree::jointCountNormalize (
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConstraintTree* excl2 = 0;
|
ConstraintTree* excl2 = 0;
|
||||||
for (unsigned i = 0; i < normCts2.size(); i++) {
|
for (size_t i = 0; i < normCts2.size(); i++) {
|
||||||
if (counts2[i] == N) {
|
if (counts2[i] == N) {
|
||||||
excl2 = normCts2[i];
|
excl2 = normCts2[i];
|
||||||
normCts2.erase (normCts2.begin() + i);
|
normCts2.erase (normCts2.begin() + i);
|
||||||
@ -762,7 +760,7 @@ ConstraintTree::jointCountNormalize (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = 0; i < normCts1.size(); i++) {
|
for (size_t i = 0; i < normCts1.size(); i++) {
|
||||||
unsigned j;
|
unsigned j;
|
||||||
for (j = 0; counts1[i] + counts2[j] != N; j++) ;
|
for (j = 0; counts1[i] + counts2[j] != N; j++) ;
|
||||||
// cout << "joint-count(" << counts1[i] ;
|
// cout << "joint-count(" << counts1[i] ;
|
||||||
@ -778,7 +776,7 @@ ConstraintTree::jointCountNormalize (
|
|||||||
ConstraintTrees cts = normCts1;
|
ConstraintTrees cts = normCts1;
|
||||||
commCt->rename (X, X_new1);
|
commCt->rename (X, X_new1);
|
||||||
exclCt->rename (X, X_new2);
|
exclCt->rename (X, X_new2);
|
||||||
for (unsigned i = 0; i < cts.size(); i++) {
|
for (size_t i = 0; i < cts.size(); i++) {
|
||||||
cts[i]->remove (X);
|
cts[i]->remove (X);
|
||||||
cts[i]->join (commCt);
|
cts[i]->join (commCt);
|
||||||
cts[i]->join (exclCt);
|
cts[i]->join (exclCt);
|
||||||
@ -814,7 +812,7 @@ ConstraintTree::expand (LogVar X)
|
|||||||
(*it)->removeAndDeleteAllChilds();
|
(*it)->removeAndDeleteAllChilds();
|
||||||
CTNode* prev = *it;
|
CTNode* prev = *it;
|
||||||
assert (symbols.size() == nrSymbols);
|
assert (symbols.size() == nrSymbols);
|
||||||
for (unsigned j = 0; j < nrSymbols; j++) {
|
for (size_t j = 0; j < nrSymbols; j++) {
|
||||||
CTNode* newNode = new CTNode (symbols[j], (*it)->level() + j);
|
CTNode* newNode = new CTNode (symbols[j], (*it)->level() + j);
|
||||||
prev->mergeSubtree (newNode);
|
prev->mergeSubtree (newNode);
|
||||||
prev = newNode;
|
prev = newNode;
|
||||||
@ -822,7 +820,7 @@ ConstraintTree::expand (LogVar X)
|
|||||||
}
|
}
|
||||||
LogVars newLvs;
|
LogVars newLvs;
|
||||||
logVars_.pop_back();
|
logVars_.pop_back();
|
||||||
for (unsigned i = 0; i < nrSymbols; i++) {
|
for (size_t i = 0; i < nrSymbols; i++) {
|
||||||
logVars_.push_back (LogVar (logVarSet_.back() + 1));
|
logVars_.push_back (LogVar (logVarSet_.back() + 1));
|
||||||
newLvs.push_back (LogVar (logVarSet_.back() + 1));
|
newLvs.push_back (LogVar (logVarSet_.back() + 1));
|
||||||
logVarSet_.insert (LogVar (logVarSet_.back() + 1));
|
logVarSet_.insert (LogVar (logVarSet_.back() + 1));
|
||||||
@ -857,7 +855,7 @@ ConstraintTree::copyLogVar (LogVar X_1, LogVar X_2)
|
|||||||
{
|
{
|
||||||
moveToBottom ({X_1});
|
moveToBottom ({X_1});
|
||||||
CTNodes leafs = getNodesAtLevel (logVars_.size());
|
CTNodes leafs = getNodesAtLevel (logVars_.size());
|
||||||
for (unsigned i = 0; i < leafs.size(); i++) {
|
for (size_t i = 0; i < leafs.size(); i++) {
|
||||||
leafs[i]->childs().insert_sorted (
|
leafs[i]->childs().insert_sorted (
|
||||||
new CTNode (leafs[i]->symbol(), leafs[i]->level() + 1));
|
new CTNode (leafs[i]->symbol(), leafs[i]->level() + 1));
|
||||||
}
|
}
|
||||||
@ -972,8 +970,8 @@ ConstraintTree::appendOnBottom (CTNode* n, const CTChilds& childs)
|
|||||||
void
|
void
|
||||||
ConstraintTree::swapLogVar (LogVar X)
|
ConstraintTree::swapLogVar (LogVar X)
|
||||||
{
|
{
|
||||||
int pos = Util::indexOf (logVars_, X);
|
size_t pos = Util::indexOf (logVars_, X);
|
||||||
assert (pos != -1);
|
assert (pos != logVars_.size());
|
||||||
const CTNodes& nodes = getNodesAtLevel (pos);
|
const CTNodes& nodes = getNodesAtLevel (pos);
|
||||||
for (CTNodes::const_iterator nodeIt = nodes.begin();
|
for (CTNodes::const_iterator nodeIt = nodes.begin();
|
||||||
nodeIt != nodes.end(); ++ nodeIt) {
|
nodeIt != nodes.end(); ++ nodeIt) {
|
||||||
@ -1003,7 +1001,7 @@ bool
|
|||||||
ConstraintTree::join (
|
ConstraintTree::join (
|
||||||
CTNode* currNode,
|
CTNode* currNode,
|
||||||
const Tuple& tuple,
|
const Tuple& tuple,
|
||||||
unsigned currIdx,
|
size_t currIdx,
|
||||||
CTNode* appendNode)
|
CTNode* appendNode)
|
||||||
{
|
{
|
||||||
bool tupleFounded = false;
|
bool tupleFounded = false;
|
||||||
@ -1033,12 +1031,12 @@ ConstraintTree::getTuples (
|
|||||||
if (currTuples.size() == 0) {
|
if (currTuples.size() == 0) {
|
||||||
currTuples.push_back ({ n->symbol()});
|
currTuples.push_back ({ n->symbol()});
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < currTuples.size(); i++) {
|
for (size_t i = 0; i < currTuples.size(); i++) {
|
||||||
currTuples[i].push_back (n->symbol());
|
currTuples[i].push_back (n->symbol());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (n->level() == stopLevel) {
|
if (n->level() == stopLevel) {
|
||||||
for (unsigned i = 0; i < currTuples.size(); i++) {
|
for (size_t i = 0; i < currTuples.size(); i++) {
|
||||||
tuplesCollected.push_back (currTuples[i]);
|
tuplesCollected.push_back (currTuples[i]);
|
||||||
continuationNodes.push_back (n);
|
continuationNodes.push_back (n);
|
||||||
}
|
}
|
||||||
@ -1088,7 +1086,7 @@ ConstraintTree::countNormalize (
|
|||||||
chIt != childs.end(); ++ chIt) {
|
chIt != childs.end(); ++ chIt) {
|
||||||
const vector<pair<CTNode*, unsigned>>& lowerRes =
|
const vector<pair<CTNode*, unsigned>>& lowerRes =
|
||||||
countNormalize (*chIt, stopLevel);
|
countNormalize (*chIt, stopLevel);
|
||||||
for (unsigned j = 0; j < lowerRes.size(); j++) {
|
for (size_t j = 0; j < lowerRes.size(); j++) {
|
||||||
CTNode* newNode = new CTNode (*n);
|
CTNode* newNode = new CTNode (*n);
|
||||||
newNode->mergeSubtree (lowerRes[j].first);
|
newNode->mergeSubtree (lowerRes[j].first);
|
||||||
res.push_back (make_pair (newNode, lowerRes[j].second));
|
res.push_back (make_pair (newNode, lowerRes[j].second));
|
||||||
|
@ -58,7 +58,7 @@ class CTNode
|
|||||||
|
|
||||||
const CTChilds_& childs (void) const { return childs_; }
|
const CTChilds_& childs (void) const { return childs_; }
|
||||||
|
|
||||||
unsigned nrChilds (void) const { return childs_.size(); }
|
size_t nrChilds (void) const { return childs_.size(); }
|
||||||
|
|
||||||
bool isRoot (void) const { return level_ == 0; }
|
bool isRoot (void) const { return level_ == 0; }
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ class ConstraintTree
|
|||||||
return logVarSet_;
|
return logVarSet_;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned nrLogVars (void) const
|
size_t nrLogVars (void) const
|
||||||
{
|
{
|
||||||
return logVars_.size();
|
return logVars_.size();
|
||||||
assert (LogVarSet (logVars_) == logVarSet_);
|
assert (LogVarSet (logVars_) == logVarSet_);
|
||||||
@ -211,7 +211,7 @@ class ConstraintTree
|
|||||||
|
|
||||||
void swapLogVar (LogVar);
|
void swapLogVar (LogVar);
|
||||||
|
|
||||||
bool join (CTNode*, const Tuple&, unsigned, CTNode*);
|
bool join (CTNode*, const Tuple&, size_t, CTNode*);
|
||||||
|
|
||||||
void getTuples (CTNode*, Tuples, unsigned, Tuples&, CTNodes&) const;
|
void getTuples (CTNode*, Tuples, unsigned, Tuples&, CTNodes&) const;
|
||||||
|
|
||||||
|
@ -9,18 +9,18 @@ ElimHeuristic ElimGraph::elimHeuristic = MIN_NEIGHBORS;
|
|||||||
|
|
||||||
ElimGraph::ElimGraph (const vector<Factor*>& factors)
|
ElimGraph::ElimGraph (const vector<Factor*>& factors)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < factors.size(); i++) {
|
for (size_t i = 0; i < factors.size(); i++) {
|
||||||
if (factors[i] == 0) { // if contained just one var with evidence
|
if (factors[i] == 0) { // if contained just one var with evidence
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const VarIds& vids = factors[i]->arguments();
|
const VarIds& vids = factors[i]->arguments();
|
||||||
for (unsigned j = 0; j < vids.size() - 1; j++) {
|
for (size_t j = 0; j < vids.size() - 1; j++) {
|
||||||
EgNode* n1 = getEgNode (vids[j]);
|
EgNode* n1 = getEgNode (vids[j]);
|
||||||
if (n1 == 0) {
|
if (n1 == 0) {
|
||||||
n1 = new EgNode (vids[j], factors[i]->range (j));
|
n1 = new EgNode (vids[j], factors[i]->range (j));
|
||||||
addNode (n1);
|
addNode (n1);
|
||||||
}
|
}
|
||||||
for (unsigned k = j + 1; k < vids.size(); k++) {
|
for (size_t k = j + 1; k < vids.size(); k++) {
|
||||||
EgNode* n2 = getEgNode (vids[k]);
|
EgNode* n2 = getEgNode (vids[k]);
|
||||||
if (n2 == 0) {
|
if (n2 == 0) {
|
||||||
n2 = new EgNode (vids[k], factors[i]->range (k));
|
n2 = new EgNode (vids[k], factors[i]->range (k));
|
||||||
@ -43,7 +43,7 @@ ElimGraph::ElimGraph (const vector<Factor*>& factors)
|
|||||||
|
|
||||||
ElimGraph::~ElimGraph (void)
|
ElimGraph::~ElimGraph (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
delete nodes_[i];
|
delete nodes_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,17 +55,17 @@ ElimGraph::getEliminatingOrder (const VarIds& exclude)
|
|||||||
{
|
{
|
||||||
VarIds elimOrder;
|
VarIds elimOrder;
|
||||||
unmarked_.reserve (nodes_.size());
|
unmarked_.reserve (nodes_.size());
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
if (Util::contains (exclude, nodes_[i]->varId()) == false) {
|
if (Util::contains (exclude, nodes_[i]->varId()) == false) {
|
||||||
unmarked_.insert (nodes_[i]);
|
unmarked_.insert (nodes_[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsigned nVarsToEliminate = nodes_.size() - exclude.size();
|
size_t nrVarsToEliminate = nodes_.size() - exclude.size();
|
||||||
for (unsigned i = 0; i < nVarsToEliminate; i++) {
|
for (size_t i = 0; i < nrVarsToEliminate; i++) {
|
||||||
EgNode* node = getLowestCostNode();
|
EgNode* node = getLowestCostNode();
|
||||||
unmarked_.remove (node);
|
unmarked_.remove (node);
|
||||||
const EGNeighs& neighs = node->neighbors();
|
const EGNeighs& neighs = node->neighbors();
|
||||||
for (unsigned j = 0; j < neighs.size(); j++) {
|
for (size_t j = 0; j < neighs.size(); j++) {
|
||||||
neighs[j]->removeNeighbor (node);
|
neighs[j]->removeNeighbor (node);
|
||||||
}
|
}
|
||||||
elimOrder.push_back (node->varId());
|
elimOrder.push_back (node->varId());
|
||||||
@ -79,10 +79,10 @@ ElimGraph::getEliminatingOrder (const VarIds& exclude)
|
|||||||
void
|
void
|
||||||
ElimGraph::print (void) const
|
ElimGraph::print (void) const
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
cout << "node " << nodes_[i]->label() << " neighs:" ;
|
cout << "node " << nodes_[i]->label() << " neighs:" ;
|
||||||
EGNeighs neighs = nodes_[i]->neighbors();
|
EGNeighs neighs = nodes_[i]->neighbors();
|
||||||
for (unsigned j = 0; j < neighs.size(); j++) {
|
for (size_t j = 0; j < neighs.size(); j++) {
|
||||||
cout << " " << neighs[j]->label();
|
cout << " " << neighs[j]->label();
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
@ -106,13 +106,13 @@ ElimGraph::exportToGraphViz (
|
|||||||
|
|
||||||
out << "strict graph {" << endl;
|
out << "strict graph {" << endl;
|
||||||
|
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
if (showNeighborless || nodes_[i]->neighbors().size() != 0) {
|
if (showNeighborless || nodes_[i]->neighbors().size() != 0) {
|
||||||
out << '"' << nodes_[i]->label() << '"' << endl;
|
out << '"' << nodes_[i]->label() << '"' << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = 0; i < highlightVarIds.size(); i++) {
|
for (size_t i = 0; i < highlightVarIds.size(); i++) {
|
||||||
EgNode* node =getEgNode (highlightVarIds[i]);
|
EgNode* node =getEgNode (highlightVarIds[i]);
|
||||||
if (node) {
|
if (node) {
|
||||||
out << '"' << node->label() << '"' ;
|
out << '"' << node->label() << '"' ;
|
||||||
@ -123,9 +123,9 @@ ElimGraph::exportToGraphViz (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = 0; i < nodes_.size(); i++) {
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||||
EGNeighs neighs = nodes_[i]->neighbors();
|
EGNeighs neighs = nodes_[i]->neighbors();
|
||||||
for (unsigned j = 0; j < neighs.size(); j++) {
|
for (size_t j = 0; j < neighs.size(); j++) {
|
||||||
out << '"' << nodes_[i]->label() << '"' << " -- " ;
|
out << '"' << nodes_[i]->label() << '"' << " -- " ;
|
||||||
out << '"' << neighs[j]->label() << '"' << endl;
|
out << '"' << neighs[j]->label() << '"' << endl;
|
||||||
}
|
}
|
||||||
@ -210,8 +210,8 @@ ElimGraph::connectAllNeighbors (const EgNode* n)
|
|||||||
{
|
{
|
||||||
const EGNeighs& neighs = n->neighbors();
|
const EGNeighs& neighs = n->neighbors();
|
||||||
if (neighs.size() > 0) {
|
if (neighs.size() > 0) {
|
||||||
for (unsigned i = 0; i < neighs.size() - 1; i++) {
|
for (size_t i = 0; i < neighs.size() - 1; i++) {
|
||||||
for (unsigned j = i+1; j < neighs.size(); j++) {
|
for (size_t j = i + 1; j < neighs.size(); j++) {
|
||||||
if ( ! neighbors (neighs[i], neighs[j])) {
|
if ( ! neighbors (neighs[i], neighs[j])) {
|
||||||
addEdge (neighs[i], neighs[j]);
|
addEdge (neighs[i], neighs[j]);
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ class ElimGraph
|
|||||||
{
|
{
|
||||||
unsigned cost = 1;
|
unsigned cost = 1;
|
||||||
const EGNeighs& neighs = n->neighbors();
|
const EGNeighs& neighs = n->neighbors();
|
||||||
for (unsigned i = 0; i < neighs.size(); i++) {
|
for (size_t i = 0; i < neighs.size(); i++) {
|
||||||
cost *= neighs[i]->range();
|
cost *= neighs[i]->range();
|
||||||
}
|
}
|
||||||
return cost;
|
return cost;
|
||||||
@ -89,8 +89,8 @@ class ElimGraph
|
|||||||
unsigned cost = 0;
|
unsigned cost = 0;
|
||||||
const EGNeighs& neighs = n->neighbors();
|
const EGNeighs& neighs = n->neighbors();
|
||||||
if (neighs.size() > 0) {
|
if (neighs.size() > 0) {
|
||||||
for (unsigned i = 0; i < neighs.size() - 1; i++) {
|
for (size_t i = 0; i < neighs.size() - 1; i++) {
|
||||||
for (unsigned j = i + 1; j < neighs.size(); j++) {
|
for (size_t j = i + 1; j < neighs.size(); j++) {
|
||||||
if ( ! neighbors (neighs[i], neighs[j])) {
|
if ( ! neighbors (neighs[i], neighs[j])) {
|
||||||
cost ++;
|
cost ++;
|
||||||
}
|
}
|
||||||
@ -105,8 +105,8 @@ class ElimGraph
|
|||||||
unsigned cost = 0;
|
unsigned cost = 0;
|
||||||
const EGNeighs& neighs = n->neighbors();
|
const EGNeighs& neighs = n->neighbors();
|
||||||
if (neighs.size() > 0) {
|
if (neighs.size() > 0) {
|
||||||
for (unsigned i = 0; i < neighs.size() - 1; i++) {
|
for (size_t i = 0; i < neighs.size() - 1; i++) {
|
||||||
for (unsigned j = i+1; j < neighs.size(); j++) {
|
for (size_t j = i + 1; j < neighs.size(); j++) {
|
||||||
if ( ! neighbors (neighs[i], neighs[j])) {
|
if ( ! neighbors (neighs[i], neighs[j])) {
|
||||||
cost += neighs[i]->range() * neighs[j]->range();
|
cost += neighs[i]->range() * neighs[j]->range();
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
Factor::Factor (const Factor& g)
|
Factor::Factor (const Factor& g)
|
||||||
{
|
{
|
||||||
copyFromFactor (g);
|
clone (g);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ Factor::Factor (
|
|||||||
ranges_ = ranges;
|
ranges_ = ranges;
|
||||||
params_ = params;
|
params_ = params;
|
||||||
distId_ = distId;
|
distId_ = distId;
|
||||||
assert (params_.size() == Util::expectedSize (ranges_));
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -37,13 +37,13 @@ Factor::Factor (
|
|||||||
const Params& params,
|
const Params& params,
|
||||||
unsigned distId)
|
unsigned distId)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < vars.size(); i++) {
|
for (size_t i = 0; i < vars.size(); i++) {
|
||||||
args_.push_back (vars[i]->varId());
|
args_.push_back (vars[i]->varId());
|
||||||
ranges_.push_back (vars[i]->range());
|
ranges_.push_back (vars[i]->range());
|
||||||
}
|
}
|
||||||
params_ = params;
|
params_ = params;
|
||||||
distId_ = distId;
|
distId_ = distId;
|
||||||
assert (params_.size() == Util::expectedSize (ranges_));
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -51,17 +51,16 @@ Factor::Factor (
|
|||||||
void
|
void
|
||||||
Factor::sumOut (VarId vid)
|
Factor::sumOut (VarId vid)
|
||||||
{
|
{
|
||||||
int idx = indexOf (vid);
|
if (vid == args_.front() && ranges_.front() == 2) {
|
||||||
assert (idx != -1);
|
// optimization
|
||||||
if (vid == args_.back()) {
|
sumOutFirstVariable();
|
||||||
sumOutLastVariable(); // optimization
|
} else if (vid == args_.back() && ranges_.back() == 2) {
|
||||||
return;
|
// optimization
|
||||||
|
sumOutLastVariable();
|
||||||
|
} else {
|
||||||
|
assert (indexOf (vid) != args_.size());
|
||||||
|
sumOutIndex (indexOf (vid));
|
||||||
}
|
}
|
||||||
if (vid == args_.front()) {
|
|
||||||
sumOutFirstVariable(); // optimization
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
sumOutIndex (idx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -69,13 +68,8 @@ Factor::sumOut (VarId vid)
|
|||||||
void
|
void
|
||||||
Factor::sumOutAllExcept (VarId vid)
|
Factor::sumOutAllExcept (VarId vid)
|
||||||
{
|
{
|
||||||
assert (indexOf (vid) != -1);
|
assert (indexOf (vid) != args_.size());
|
||||||
while (args_.back() != vid) {
|
sumOutAllExceptIndex (indexOf (vid));
|
||||||
sumOutLastVariable();
|
|
||||||
}
|
|
||||||
while (args_.front() != vid) {
|
|
||||||
sumOutFirstVariable();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -83,147 +77,31 @@ Factor::sumOutAllExcept (VarId vid)
|
|||||||
void
|
void
|
||||||
Factor::sumOutAllExcept (const VarIds& vids)
|
Factor::sumOutAllExcept (const VarIds& vids)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < (int)args_.size(); i++) {
|
vector<bool> mask (args_.size(), false);
|
||||||
if (Util::contains (vids, args_[i]) == false) {
|
for (unsigned i = 0; i < vids.size(); i++) {
|
||||||
sumOut (args_[i]);
|
assert (indexOf (vids[i]) != args_.size());
|
||||||
i --;
|
mask[indexOf (vids[i])] = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sumOutArgs (mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Factor::sumOutIndex (unsigned idx)
|
Factor::sumOutAllExceptIndex (size_t idx)
|
||||||
{
|
{
|
||||||
assert (idx < args_.size());
|
assert (idx < args_.size());
|
||||||
// number of parameters separating a different state of `var',
|
vector<bool> mask (args_.size(), false);
|
||||||
// with the states of the remaining variables fixed
|
mask[idx] = true;
|
||||||
unsigned varOffset = 1;
|
sumOutArgs (mask);
|
||||||
|
|
||||||
// number of parameters separating a different state of the variable
|
|
||||||
// on the left of `var', with the states of the remaining vars fixed
|
|
||||||
unsigned leftVarOffset = 1;
|
|
||||||
|
|
||||||
for (int i = args_.size() - 1; i > (int)idx; i--) {
|
|
||||||
varOffset *= ranges_[i];
|
|
||||||
leftVarOffset *= ranges_[i];
|
|
||||||
}
|
|
||||||
leftVarOffset *= ranges_[idx];
|
|
||||||
|
|
||||||
unsigned offset = 0;
|
|
||||||
unsigned count1 = 0;
|
|
||||||
unsigned count2 = 0;
|
|
||||||
unsigned newpsSize = params_.size() / ranges_[idx];
|
|
||||||
|
|
||||||
Params newps;
|
|
||||||
newps.reserve (newpsSize);
|
|
||||||
|
|
||||||
while (newps.size() < newpsSize) {
|
|
||||||
double sum = LogAware::addIdenty();
|
|
||||||
for (unsigned i = 0; i < ranges_[idx]; i++) {
|
|
||||||
if (Globals::logDomain) {
|
|
||||||
sum = Util::logSum (sum, params_[offset]);
|
|
||||||
} else {
|
|
||||||
sum += params_[offset];
|
|
||||||
}
|
|
||||||
offset += varOffset;
|
|
||||||
}
|
|
||||||
newps.push_back (sum);
|
|
||||||
count1 ++;
|
|
||||||
if (idx == args_.size() - 1) {
|
|
||||||
offset = count1 * ranges_[idx];
|
|
||||||
} else {
|
|
||||||
if (((offset - varOffset + 1) % leftVarOffset) == 0) {
|
|
||||||
count1 = 0;
|
|
||||||
count2 ++;
|
|
||||||
}
|
|
||||||
offset = (leftVarOffset * count2) + count1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
args_.erase (args_.begin() + idx);
|
|
||||||
ranges_.erase (ranges_.begin() + idx);
|
|
||||||
params_ = newps;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Factor::sumOutAllExceptIndex (unsigned idx)
|
|
||||||
{
|
|
||||||
assert (idx < args_.size());
|
|
||||||
while (args_.size() > idx + 1) {
|
|
||||||
sumOutLastVariable();
|
|
||||||
}
|
|
||||||
for (unsigned i = 0; i < idx; i++) {
|
|
||||||
sumOutFirstVariable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Factor::sumOutFirstVariable (void)
|
|
||||||
{
|
|
||||||
assert (args_.size() > 1);
|
|
||||||
unsigned range = ranges_.front();
|
|
||||||
unsigned sep = params_.size() / range;
|
|
||||||
if (Globals::logDomain) {
|
|
||||||
for (unsigned i = sep; i < params_.size(); i++) {
|
|
||||||
params_[i % sep] = Util::logSum (params_[i % sep], params_[i]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (unsigned i = sep; i < params_.size(); i++) {
|
|
||||||
params_[i % sep] += params_[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
params_.resize (sep);
|
|
||||||
args_.erase (args_.begin());
|
|
||||||
ranges_.erase (ranges_.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Factor::sumOutLastVariable (void)
|
|
||||||
{
|
|
||||||
assert (args_.size() > 1);
|
|
||||||
unsigned range = ranges_.back();
|
|
||||||
unsigned idx1 = 0;
|
|
||||||
unsigned idx2 = 0;
|
|
||||||
if (Globals::logDomain) {
|
|
||||||
while (idx1 < params_.size()) {
|
|
||||||
params_[idx2] = params_[idx1];
|
|
||||||
idx1 ++;
|
|
||||||
for (unsigned j = 1; j < range; j++) {
|
|
||||||
params_[idx2] = Util::logSum (params_[idx2], params_[idx1]);
|
|
||||||
idx1 ++;
|
|
||||||
}
|
|
||||||
idx2 ++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
while (idx1 < params_.size()) {
|
|
||||||
params_[idx2] = params_[idx1];
|
|
||||||
idx1 ++;
|
|
||||||
for (unsigned j = 1; j < range; j++) {
|
|
||||||
params_[idx2] += params_[idx1];
|
|
||||||
idx1 ++;
|
|
||||||
}
|
|
||||||
idx2 ++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
params_.resize (idx2);
|
|
||||||
args_.pop_back();
|
|
||||||
ranges_.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Factor::multiply (Factor& g)
|
Factor::multiply (Factor& g)
|
||||||
{
|
{
|
||||||
if (args_.size() == 0) {
|
if (args_.size() == 0) {
|
||||||
copyFromFactor (g);
|
clone (g);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
TFactor<VarId>::multiply (g);
|
TFactor<VarId>::multiply (g);
|
||||||
@ -231,22 +109,12 @@ Factor::multiply (Factor& g)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Factor::reorderAccordingVarIds (void)
|
|
||||||
{
|
|
||||||
VarIds sortedVarIds = args_;
|
|
||||||
sort (sortedVarIds.begin(), sortedVarIds.end());
|
|
||||||
reorderArguments (sortedVarIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
string
|
string
|
||||||
Factor::getLabel (void) const
|
Factor::getLabel (void) const
|
||||||
{
|
{
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "f(" ;
|
ss << "f(" ;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (i != 0) ss << "," ;
|
if (i != 0) ss << "," ;
|
||||||
ss << Var (args_[i], ranges_[i]).label();
|
ss << Var (args_[i], ranges_[i]).label();
|
||||||
}
|
}
|
||||||
@ -260,17 +128,17 @@ void
|
|||||||
Factor::print (void) const
|
Factor::print (void) const
|
||||||
{
|
{
|
||||||
Vars vars;
|
Vars vars;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
vars.push_back (new Var (args_[i], ranges_[i]));
|
vars.push_back (new Var (args_[i], ranges_[i]));
|
||||||
}
|
}
|
||||||
vector<string> jointStrings = Util::getStateLines (vars);
|
vector<string> jointStrings = Util::getStateLines (vars);
|
||||||
for (unsigned i = 0; i < params_.size(); i++) {
|
for (size_t i = 0; i < params_.size(); i++) {
|
||||||
// cout << "[" << distId_ << "] " ;
|
// cout << "[" << distId_ << "] " ;
|
||||||
cout << "f(" << jointStrings[i] << ")" ;
|
cout << "f(" << jointStrings[i] << ")" ;
|
||||||
cout << " = " << params_[i] << endl;
|
cout << " = " << params_[i] << endl;
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
for (unsigned i = 0; i < vars.size(); i++) {
|
for (size_t i = 0; i < vars.size(); i++) {
|
||||||
delete vars[i];
|
delete vars[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -278,7 +146,88 @@ Factor::print (void) const
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Factor::copyFromFactor (const Factor& g)
|
Factor::sumOutFirstVariable (void)
|
||||||
|
{
|
||||||
|
size_t sep = params_.size() / 2;
|
||||||
|
if (Globals::logDomain) {
|
||||||
|
std::transform (
|
||||||
|
params_.begin(), params_.begin() + sep,
|
||||||
|
params_.begin() + sep, params_.begin(),
|
||||||
|
Util::logSum);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
std::transform (
|
||||||
|
params_.begin(), params_.begin() + sep,
|
||||||
|
params_.begin() + sep, params_.begin(),
|
||||||
|
std::plus<double>());
|
||||||
|
}
|
||||||
|
params_.resize (sep);
|
||||||
|
args_.erase (args_.begin());
|
||||||
|
ranges_.erase (ranges_.begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
Factor::sumOutLastVariable (void)
|
||||||
|
{
|
||||||
|
Params::iterator first1 = params_.begin();
|
||||||
|
Params::iterator first2 = params_.begin();
|
||||||
|
Params::iterator last = params_.end();
|
||||||
|
if (Globals::logDomain) {
|
||||||
|
while (first2 != last) {
|
||||||
|
// the arguments can be swaped, but that is ok
|
||||||
|
*first1++ = Util::logSum (*first2++, *first2++);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (first2 != last) {
|
||||||
|
*first1++ = (*first2++) + (*first2++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
params_.resize (params_.size() / 2);
|
||||||
|
args_.pop_back();
|
||||||
|
ranges_.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
Factor::sumOutArgs (const vector<bool>& mask)
|
||||||
|
{
|
||||||
|
assert (mask.size() == args_.size());
|
||||||
|
size_t new_size = 1;
|
||||||
|
Ranges oldRanges = ranges_;
|
||||||
|
args_.clear();
|
||||||
|
ranges_.clear();
|
||||||
|
for (unsigned i = 0; i < mask.size(); i++) {
|
||||||
|
if (mask[i]) {
|
||||||
|
new_size *= ranges_[i];
|
||||||
|
args_.push_back (args_[i]);
|
||||||
|
ranges_.push_back (ranges_[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Params newps (new_size, LogAware::addIdenty());
|
||||||
|
Params::const_iterator first = params_.begin();
|
||||||
|
Params::const_iterator last = params_.end();
|
||||||
|
MapIndexer indexer (oldRanges, mask);
|
||||||
|
if (Globals::logDomain) {
|
||||||
|
while (first != last) {
|
||||||
|
newps[indexer] = Util::logSum (newps[indexer], *first++);
|
||||||
|
++ indexer;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (first != last) {
|
||||||
|
newps[indexer] += *first++;
|
||||||
|
++ indexer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
params_ = newps;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
Factor::clone (const Factor& g)
|
||||||
{
|
{
|
||||||
args_ = g.arguments();
|
args_ = g.arguments();
|
||||||
ranges_ = g.ranges();
|
ranges_ = g.ranges();
|
||||||
|
@ -25,9 +25,9 @@ class TFactor
|
|||||||
|
|
||||||
Params& params (void) { return params_; }
|
Params& params (void) { return params_; }
|
||||||
|
|
||||||
unsigned nrArguments (void) const { return args_.size(); }
|
size_t nrArguments (void) const { return args_.size(); }
|
||||||
|
|
||||||
unsigned size (void) const { return params_.size(); }
|
size_t size (void) const { return params_.size(); }
|
||||||
|
|
||||||
unsigned distId (void) const { return distId_; }
|
unsigned distId (void) const { return distId_; }
|
||||||
|
|
||||||
@ -35,37 +35,37 @@ class TFactor
|
|||||||
|
|
||||||
void normalize (void) { LogAware::normalize (params_); }
|
void normalize (void) { LogAware::normalize (params_); }
|
||||||
|
|
||||||
|
void randomize (void)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < params_.size(); ++i) {
|
||||||
|
params_[i] = (double) std::rand() / RAND_MAX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setParams (const Params& newParams)
|
void setParams (const Params& newParams)
|
||||||
{
|
{
|
||||||
params_ = newParams;
|
params_ = newParams;
|
||||||
assert (params_.size() == Util::expectedSize (ranges_));
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
||||||
}
|
}
|
||||||
|
|
||||||
int indexOf (const T& t) const
|
size_t indexOf (const T& t) const
|
||||||
{
|
{
|
||||||
int idx = -1;
|
return Util::indexOf (args_, t);
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
|
||||||
if (args_[i] == t) {
|
|
||||||
idx = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return idx;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const T& argument (unsigned idx) const
|
const T& argument (size_t idx) const
|
||||||
{
|
{
|
||||||
assert (idx < args_.size());
|
assert (idx < args_.size());
|
||||||
return args_[idx];
|
return args_[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
T& argument (unsigned idx)
|
T& argument (size_t idx)
|
||||||
{
|
{
|
||||||
assert (idx < args_.size());
|
assert (idx < args_.size());
|
||||||
return args_[idx];
|
return args_[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned range (unsigned idx) const
|
unsigned range (size_t idx) const
|
||||||
{
|
{
|
||||||
assert (idx < ranges_.size());
|
assert (idx < ranges_.size());
|
||||||
return ranges_[idx];
|
return ranges_[idx];
|
||||||
@ -73,126 +73,111 @@ class TFactor
|
|||||||
|
|
||||||
void multiply (TFactor<T>& g)
|
void multiply (TFactor<T>& g)
|
||||||
{
|
{
|
||||||
|
if (args_ == g.arguments()) {
|
||||||
|
// optimization
|
||||||
|
Globals::logDomain
|
||||||
|
? params_ += g.params()
|
||||||
|
: params_ *= g.params();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsigned range_prod = 1;
|
||||||
|
bool share_arguments = false;
|
||||||
const vector<T>& g_args = g.arguments();
|
const vector<T>& g_args = g.arguments();
|
||||||
const Ranges& g_ranges = g.ranges();
|
const Ranges& g_ranges = g.ranges();
|
||||||
const Params& g_params = g.params();
|
const Params& g_params = g.params();
|
||||||
if (args_ == g_args) {
|
for (size_t i = 0; i < g_args.size(); i++) {
|
||||||
// optimization: if the factors contain the same set of args,
|
size_t idx = indexOf (g_args[i]);
|
||||||
// we can do a 1 to 1 operation on the parameters
|
if (idx == args_.size()) {
|
||||||
if (Globals::logDomain) {
|
range_prod *= g_ranges[i];
|
||||||
Util::add (params_, g_params);
|
args_.push_back (g_args[i]);
|
||||||
|
ranges_.push_back (g_ranges[i]);
|
||||||
} else {
|
} else {
|
||||||
Util::multiply (params_, g_params);
|
share_arguments = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (share_arguments == false) {
|
||||||
|
// optimization
|
||||||
|
cartesianProduct (g_params.begin(), g_params.end());
|
||||||
} else {
|
} else {
|
||||||
bool sharedArgs = false;
|
extend (range_prod);
|
||||||
vector<unsigned> gvarpos;
|
Params::iterator it = params_.begin();
|
||||||
for (unsigned i = 0; i < g_args.size(); i++) {
|
MapIndexer indexer (args_, ranges_, g_args, g_ranges);
|
||||||
int idx = indexOf (g_args[i]);
|
if (Globals::logDomain) {
|
||||||
if (idx == -1) {
|
for (; indexer.valid(); ++it, ++indexer) {
|
||||||
ullong newSize = params_.size() * g_ranges[i];
|
*it += g_params[indexer];
|
||||||
if (newSize > params_.max_size()) {
|
|
||||||
cerr << "error: an overflow occurred on factor multiplication" ;
|
|
||||||
cerr << endl;
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
insertArgument (g_args[i], g_ranges[i]);
|
|
||||||
gvarpos.push_back (args_.size() - 1);
|
|
||||||
} else {
|
|
||||||
sharedArgs = true;
|
|
||||||
gvarpos.push_back (idx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (sharedArgs == false) {
|
|
||||||
// optimization: if the original factors doesn't have common args,
|
|
||||||
// we don't need to marry the states of the common args
|
|
||||||
unsigned count = 0;
|
|
||||||
for (unsigned i = 0; i < params_.size(); i++) {
|
|
||||||
if (Globals::logDomain) {
|
|
||||||
params_[i] += g_params[count];
|
|
||||||
} else {
|
|
||||||
params_[i] *= g_params[count];
|
|
||||||
}
|
|
||||||
count ++;
|
|
||||||
if (count >= g_params.size()) {
|
|
||||||
count = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
StatesIndexer indexer (ranges_, false);
|
for (; indexer.valid(); ++it, ++indexer) {
|
||||||
while (indexer.valid()) {
|
*it *= g_params[indexer];
|
||||||
unsigned g_li = 0;
|
|
||||||
unsigned prod = 1;
|
|
||||||
for (int j = gvarpos.size() - 1; j >= 0; j--) {
|
|
||||||
g_li += indexer[gvarpos[j]] * prod;
|
|
||||||
prod *= g_ranges[j];
|
|
||||||
}
|
|
||||||
if (Globals::logDomain) {
|
|
||||||
params_[indexer] += g_params[g_li];
|
|
||||||
} else {
|
|
||||||
params_[indexer] *= g_params[g_li];
|
|
||||||
}
|
|
||||||
++ indexer;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void absorveEvidence (const T& arg, unsigned evidence)
|
void sumOutIndex (size_t idx)
|
||||||
{
|
{
|
||||||
int idx = indexOf (arg);
|
assert (idx < args_.size());
|
||||||
assert (idx != -1);
|
assert (args_.size() > 1);
|
||||||
assert (evidence < ranges_[idx]);
|
size_t new_size = params_.size() / ranges_[idx];
|
||||||
Params copy = params_;
|
Params newps (new_size, LogAware::addIdenty());
|
||||||
params_.clear();
|
Params::const_iterator first = params_.begin();
|
||||||
params_.reserve (copy.size() / ranges_[idx]);
|
Params::const_iterator last = params_.end();
|
||||||
StatesIndexer indexer (ranges_);
|
MapIndexer indexer (ranges_, idx);
|
||||||
for (unsigned i = 0; i < evidence; i++) {
|
if (Globals::logDomain) {
|
||||||
indexer.increment (idx);
|
for (; first != last; ++indexer) {
|
||||||
}
|
newps[indexer] = Util::logSum (newps[indexer], *first++);
|
||||||
while (indexer.valid()) {
|
}
|
||||||
params_.push_back (copy[indexer]);
|
} else {
|
||||||
indexer.incrementExcluding (idx);
|
for (; first != last; ++indexer) {
|
||||||
|
newps[indexer] += *first++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
params_ = newps;
|
||||||
args_.erase (args_.begin() + idx);
|
args_.erase (args_.begin() + idx);
|
||||||
ranges_.erase (ranges_.begin() + idx);
|
ranges_.erase (ranges_.begin() + idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reorderArguments (const vector<T> newArgs)
|
void absorveEvidence (const T& arg, unsigned obsIdx)
|
||||||
{
|
{
|
||||||
assert (newArgs.size() == args_.size());
|
size_t idx = indexOf (arg);
|
||||||
if (newArgs == args_) {
|
assert (idx != args_.size());
|
||||||
return; // already in the wanted order
|
assert (obsIdx < ranges_[idx]);
|
||||||
|
Params newps;
|
||||||
|
newps.reserve (params_.size() / ranges_[idx]);
|
||||||
|
Indexer indexer (ranges_);
|
||||||
|
for (unsigned i = 0; i < obsIdx; ++i) {
|
||||||
|
indexer.incrementDimension (idx);
|
||||||
}
|
}
|
||||||
Ranges newRanges;
|
while (indexer.valid()) {
|
||||||
vector<unsigned> positions;
|
newps.push_back (params_[indexer]);
|
||||||
for (unsigned i = 0; i < newArgs.size(); i++) {
|
indexer.incrementExceptDimension (idx);
|
||||||
unsigned idx = indexOf (newArgs[i]);
|
|
||||||
newRanges.push_back (ranges_[idx]);
|
|
||||||
positions.push_back (idx);
|
|
||||||
}
|
}
|
||||||
unsigned N = ranges_.size();
|
params_ = newps;
|
||||||
Params newParams (params_.size());
|
args_.erase (args_.begin() + idx);
|
||||||
for (unsigned i = 0; i < params_.size(); i++) {
|
ranges_.erase (ranges_.begin() + idx);
|
||||||
unsigned li = i;
|
}
|
||||||
// calculate vector index corresponding to linear index
|
|
||||||
vector<unsigned> vi (N);
|
void reorderArguments (const vector<T> new_args)
|
||||||
for (int k = N-1; k >= 0; k--) {
|
{
|
||||||
vi[k] = li % ranges_[k];
|
assert (new_args.size() == args_.size());
|
||||||
li /= ranges_[k];
|
if (new_args == args_) {
|
||||||
}
|
return; // already on the desired order
|
||||||
// convert permuted vector index to corresponding linear index
|
|
||||||
unsigned prod = 1;
|
|
||||||
unsigned new_li = 0;
|
|
||||||
for (int k = N - 1; k >= 0; k--) {
|
|
||||||
new_li += vi[positions[k]] * prod;
|
|
||||||
prod *= ranges_[positions[k]];
|
|
||||||
}
|
|
||||||
newParams[new_li] = params_[i];
|
|
||||||
}
|
}
|
||||||
args_ = newArgs;
|
Ranges new_ranges;
|
||||||
ranges_ = newRanges;
|
for (size_t i = 0; i < new_args.size(); i++) {
|
||||||
params_ = newParams;
|
size_t idx = indexOf (new_args[i]);
|
||||||
|
assert (idx != args_.size());
|
||||||
|
new_ranges.push_back (ranges_[idx]);
|
||||||
|
}
|
||||||
|
Params newps;
|
||||||
|
newps.reserve (params_.size());
|
||||||
|
MapIndexer indexer (new_args, new_ranges, args_, ranges_);
|
||||||
|
for (; indexer.valid(); ++indexer) {
|
||||||
|
newps.push_back (params_[indexer]);
|
||||||
|
}
|
||||||
|
params_ = newps;
|
||||||
|
args_ = new_args;
|
||||||
|
ranges_ = new_ranges;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool contains (const T& arg) const
|
bool contains (const T& arg) const
|
||||||
@ -202,7 +187,7 @@ class TFactor
|
|||||||
|
|
||||||
bool contains (const vector<T>& args) const
|
bool contains (const vector<T>& args) const
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (contains (args[i]) == false) {
|
if (contains (args[i]) == false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -210,7 +195,7 @@ class TFactor
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
double& operator[] (psize_t idx)
|
double& operator[] (size_t idx)
|
||||||
{
|
{
|
||||||
assert (idx < params_.size());
|
assert (idx < params_.size());
|
||||||
return params_[idx];
|
return params_[idx];
|
||||||
@ -224,39 +209,45 @@ class TFactor
|
|||||||
unsigned distId_;
|
unsigned distId_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void insertArgument (const T& arg, unsigned range)
|
void extend (unsigned range_prod)
|
||||||
{
|
{
|
||||||
assert (indexOf (arg) == -1);
|
Params backup = params_;
|
||||||
Params copy = params_;
|
|
||||||
params_.clear();
|
params_.clear();
|
||||||
params_.reserve (copy.size() * range);
|
params_.reserve (backup.size() * range_prod);
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
Params::const_iterator first = backup.begin();
|
||||||
for (unsigned reps = 0; reps < range; reps++) {
|
Params::const_iterator last = backup.end();
|
||||||
params_.push_back (copy[i]);
|
for (; first != last; ++first) {
|
||||||
|
for (unsigned reps = 0; reps < range_prod; ++reps) {
|
||||||
|
params_.push_back (*first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
args_.push_back (arg);
|
|
||||||
ranges_.push_back (range);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void insertArguments (const vector<T>& args, const Ranges& ranges)
|
void cartesianProduct (
|
||||||
|
Params::const_iterator first2,
|
||||||
|
Params::const_iterator last2)
|
||||||
{
|
{
|
||||||
Params copy = params_;
|
Params backup = params_;
|
||||||
unsigned nrStates = 1;
|
|
||||||
for (unsigned i = 0; i < args.size(); i++) {
|
|
||||||
assert (indexOf (args[i]) == -1);
|
|
||||||
args_.push_back (args[i]);
|
|
||||||
ranges_.push_back (ranges[i]);
|
|
||||||
nrStates *= ranges[i];
|
|
||||||
}
|
|
||||||
params_.clear();
|
params_.clear();
|
||||||
params_.reserve (copy.size() * nrStates);
|
params_.reserve (params_.size() * (last2 - first2));
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
Params::const_iterator first1 = backup.begin();
|
||||||
for (unsigned reps = 0; reps < nrStates; reps++) {
|
Params::const_iterator last1 = backup.end();
|
||||||
params_.push_back (copy[i]);
|
Params::const_iterator tmp;
|
||||||
|
if (Globals::logDomain) {
|
||||||
|
for (; first1 != last1; ++first1) {
|
||||||
|
for (tmp = first2; tmp != last2; ++tmp) {
|
||||||
|
params_.push_back ((*first1) + (*tmp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; first1 != last1; ++first1) {
|
||||||
|
for (tmp = first2; tmp != last2; ++tmp) {
|
||||||
|
params_.push_back ((*first1) * (*tmp));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -280,24 +271,22 @@ class Factor : public TFactor<VarId>
|
|||||||
|
|
||||||
void sumOutAllExcept (const VarIds&);
|
void sumOutAllExcept (const VarIds&);
|
||||||
|
|
||||||
void sumOutIndex (unsigned idx);
|
void sumOutAllExceptIndex (size_t idx);
|
||||||
|
|
||||||
void sumOutAllExceptIndex (unsigned idx);
|
|
||||||
|
|
||||||
void sumOutFirstVariable (void);
|
|
||||||
|
|
||||||
void sumOutLastVariable (void);
|
|
||||||
|
|
||||||
void multiply (Factor&);
|
void multiply (Factor&);
|
||||||
|
|
||||||
void reorderAccordingVarIds (void);
|
|
||||||
|
|
||||||
string getLabel (void) const;
|
string getLabel (void) const;
|
||||||
|
|
||||||
void print (void) const;
|
void print (void) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void copyFromFactor (const Factor& f);
|
void sumOutFirstVariable (void);
|
||||||
|
|
||||||
|
void sumOutLastVariable (void);
|
||||||
|
|
||||||
|
void sumOutArgs (const vector<bool>& mask);
|
||||||
|
|
||||||
|
void clone (const Factor& f);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -16,15 +16,15 @@
|
|||||||
FactorGraph::FactorGraph (const FactorGraph& fg)
|
FactorGraph::FactorGraph (const FactorGraph& fg)
|
||||||
{
|
{
|
||||||
const VarNodes& varNodes = fg.varNodes();
|
const VarNodes& varNodes = fg.varNodes();
|
||||||
for (unsigned i = 0; i < varNodes.size(); i++) {
|
for (size_t i = 0; i < varNodes.size(); i++) {
|
||||||
addVarNode (new VarNode (varNodes[i]));
|
addVarNode (new VarNode (varNodes[i]));
|
||||||
}
|
}
|
||||||
const FacNodes& facNodes = fg.facNodes();
|
const FacNodes& facNodes = fg.facNodes();
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
FacNode* facNode = new FacNode (facNodes[i]->factor());
|
FacNode* facNode = new FacNode (facNodes[i]->factor());
|
||||||
addFacNode (facNode);
|
addFacNode (facNode);
|
||||||
const VarNodes& neighs = facNodes[i]->neighbors();
|
const VarNodes& neighs = facNodes[i]->neighbors();
|
||||||
for (unsigned j = 0; j < neighs.size(); j++) {
|
for (size_t j = 0; j < neighs.size(); j++) {
|
||||||
addEdge (varNodes_[neighs[j]->getIndex()], facNode);
|
addEdge (varNodes_[neighs[j]->getIndex()], facNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,9 +86,9 @@ FactorGraph::readFromUaiFormat (const char* fileName)
|
|||||||
for (unsigned i = 0; i < nrFactors; i++) {
|
for (unsigned i = 0; i < nrFactors; i++) {
|
||||||
ignoreLines (is);
|
ignoreLines (is);
|
||||||
is >> nrParams;
|
is >> nrParams;
|
||||||
if (nrParams != Util::expectedSize (factorRanges[i])) {
|
if (nrParams != Util::sizeExpected (factorRanges[i])) {
|
||||||
cerr << "error: invalid number of parameters for factor nº " << i ;
|
cerr << "error: invalid number of parameters for factor nº " << i ;
|
||||||
cerr << ", expected: " << Util::expectedSize (factorRanges[i]);
|
cerr << ", expected: " << Util::sizeExpected (factorRanges[i]);
|
||||||
cerr << ", given: " << nrParams << endl;
|
cerr << ", given: " << nrParams << endl;
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
@ -97,7 +97,7 @@ FactorGraph::readFromUaiFormat (const char* fileName)
|
|||||||
is >> params[j];
|
is >> params[j];
|
||||||
}
|
}
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::toLog (params);
|
Util::log (params);
|
||||||
}
|
}
|
||||||
addFactor (Factor (factorVarIds[i], factorRanges[i], params));
|
addFactor (Factor (factorVarIds[i], factorRanges[i], params));
|
||||||
}
|
}
|
||||||
@ -144,7 +144,7 @@ FactorGraph::readFromLibDaiFormat (const char* fileName)
|
|||||||
ignoreLines (is);
|
ignoreLines (is);
|
||||||
unsigned nNonzeros;
|
unsigned nNonzeros;
|
||||||
is >> nNonzeros;
|
is >> nNonzeros;
|
||||||
Params params (Util::expectedSize (ranges), 0);
|
Params params (Util::sizeExpected (ranges), 0);
|
||||||
for (unsigned j = 0; j < nNonzeros; j++) {
|
for (unsigned j = 0; j < nNonzeros; j++) {
|
||||||
ignoreLines (is);
|
ignoreLines (is);
|
||||||
unsigned index;
|
unsigned index;
|
||||||
@ -155,11 +155,11 @@ FactorGraph::readFromLibDaiFormat (const char* fileName)
|
|||||||
params[index] = val;
|
params[index] = val;
|
||||||
}
|
}
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::toLog (params);
|
Util::log (params);
|
||||||
}
|
}
|
||||||
reverse (vids.begin(), vids.end());
|
std::reverse (vids.begin(), vids.end());
|
||||||
Factor f (vids, ranges, params);
|
Factor f (vids, ranges, params);
|
||||||
reverse (vids.begin(), vids.end());
|
std::reverse (vids.begin(), vids.end());
|
||||||
f.reorderArguments (vids);
|
f.reorderArguments (vids);
|
||||||
addFactor (f);
|
addFactor (f);
|
||||||
}
|
}
|
||||||
@ -170,10 +170,10 @@ FactorGraph::readFromLibDaiFormat (const char* fileName)
|
|||||||
|
|
||||||
FactorGraph::~FactorGraph (void)
|
FactorGraph::~FactorGraph (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < varNodes_.size(); i++) {
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||||
delete varNodes_[i];
|
delete varNodes_[i];
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
delete facNodes_[i];
|
delete facNodes_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -186,7 +186,7 @@ FactorGraph::addFactor (const Factor& factor)
|
|||||||
FacNode* fn = new FacNode (factor);
|
FacNode* fn = new FacNode (factor);
|
||||||
addFacNode (fn);
|
addFacNode (fn);
|
||||||
const VarIds& vids = fn->factor().arguments();
|
const VarIds& vids = fn->factor().arguments();
|
||||||
for (unsigned i = 0; i < vids.size(); i++) {
|
for (size_t i = 0; i < vids.size(); i++) {
|
||||||
VarMap::const_iterator it = varMap_.find (vids[i]);
|
VarMap::const_iterator it = varMap_.find (vids[i]);
|
||||||
if (it != varMap_.end()) {
|
if (it != varMap_.end()) {
|
||||||
addEdge (it->second, fn);
|
addEdge (it->second, fn);
|
||||||
@ -241,12 +241,12 @@ FactorGraph::getStructure (void)
|
|||||||
{
|
{
|
||||||
assert (fromBayesNet_);
|
assert (fromBayesNet_);
|
||||||
if (structure_.empty()) {
|
if (structure_.empty()) {
|
||||||
for (unsigned i = 0; i < varNodes_.size(); i++) {
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||||
structure_.addNode (new DAGraphNode (varNodes_[i]));
|
structure_.addNode (new DAGraphNode (varNodes_[i]));
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
const VarIds& vids = facNodes_[i]->factor().arguments();
|
const VarIds& vids = facNodes_[i]->factor().arguments();
|
||||||
for (unsigned j = 1; j < vids.size(); j++) {
|
for (size_t j = 1; j < vids.size(); j++) {
|
||||||
structure_.addEdge (vids[j], vids[0]);
|
structure_.addEdge (vids[j], vids[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -259,18 +259,18 @@ FactorGraph::getStructure (void)
|
|||||||
void
|
void
|
||||||
FactorGraph::print (void) const
|
FactorGraph::print (void) const
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < varNodes_.size(); i++) {
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||||
cout << "var id = " << varNodes_[i]->varId() << endl;
|
cout << "var id = " << varNodes_[i]->varId() << endl;
|
||||||
cout << "label = " << varNodes_[i]->label() << endl;
|
cout << "label = " << varNodes_[i]->label() << endl;
|
||||||
cout << "range = " << varNodes_[i]->range() << endl;
|
cout << "range = " << varNodes_[i]->range() << endl;
|
||||||
cout << "evidence = " << varNodes_[i]->getEvidence() << endl;
|
cout << "evidence = " << varNodes_[i]->getEvidence() << endl;
|
||||||
cout << "factors = " ;
|
cout << "factors = " ;
|
||||||
for (unsigned j = 0; j < varNodes_[i]->neighbors().size(); j++) {
|
for (size_t j = 0; j < varNodes_[i]->neighbors().size(); j++) {
|
||||||
cout << varNodes_[i]->neighbors()[j]->getLabel() << " " ;
|
cout << varNodes_[i]->neighbors()[j]->getLabel() << " " ;
|
||||||
}
|
}
|
||||||
cout << endl << endl;
|
cout << endl << endl;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
facNodes_[i]->factor().print();
|
facNodes_[i]->factor().print();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -287,20 +287,20 @@ FactorGraph::exportToGraphViz (const char* fileName) const
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
out << "graph \"" << fileName << "\" {" << endl;
|
out << "graph \"" << fileName << "\" {" << endl;
|
||||||
for (unsigned i = 0; i < varNodes_.size(); i++) {
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||||
if (varNodes_[i]->hasEvidence()) {
|
if (varNodes_[i]->hasEvidence()) {
|
||||||
out << '"' << varNodes_[i]->label() << '"' ;
|
out << '"' << varNodes_[i]->label() << '"' ;
|
||||||
out << " [style=filled, fillcolor=yellow]" << endl;
|
out << " [style=filled, fillcolor=yellow]" << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
||||||
out << " [label=\"" << facNodes_[i]->getLabel();
|
out << " [label=\"" << facNodes_[i]->getLabel();
|
||||||
out << "\"" << ", shape=box]" << endl;
|
out << "\"" << ", shape=box]" << endl;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
const VarNodes& myVars = facNodes_[i]->neighbors();
|
const VarNodes& myVars = facNodes_[i]->neighbors();
|
||||||
for (unsigned j = 0; j < myVars.size(); j++) {
|
for (size_t j = 0; j < myVars.size(); j++) {
|
||||||
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
||||||
out << " -- " ;
|
out << " -- " ;
|
||||||
out << '"' << myVars[j]->label() << '"' << endl;
|
out << '"' << myVars[j]->label() << '"' << endl;
|
||||||
@ -322,29 +322,24 @@ FactorGraph::exportToUaiFormat (const char* fileName) const
|
|||||||
}
|
}
|
||||||
out << "MARKOV" << endl;
|
out << "MARKOV" << endl;
|
||||||
out << varNodes_.size() << endl;
|
out << varNodes_.size() << endl;
|
||||||
for (unsigned i = 0; i < varNodes_.size(); i++) {
|
VarNodes sortedVns = varNodes_;
|
||||||
out << varNodes_[i]->range() << " " ;
|
std::sort (sortedVns.begin(), sortedVns.end(), sortByVarId());
|
||||||
|
for (size_t i = 0; i < sortedVns.size(); i++) {
|
||||||
|
out << ((i != 0) ? " " : "") << sortedVns[i]->range();
|
||||||
|
}
|
||||||
|
out << endl << facNodes_.size() << endl;
|
||||||
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
|
VarIds args = facNodes_[i]->factor().arguments();
|
||||||
|
out << args.size() << " " << Util::elementsToString (args) << endl;
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
out << facNodes_.size() << endl;
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
|
||||||
const VarNodes& factorVars = facNodes_[i]->neighbors();
|
|
||||||
out << factorVars.size();
|
|
||||||
for (unsigned j = 0; j < factorVars.size(); j++) {
|
|
||||||
out << " " << factorVars[j]->getIndex();
|
|
||||||
}
|
|
||||||
out << endl;
|
|
||||||
}
|
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
|
||||||
Params params = facNodes_[i]->factor().params();
|
Params params = facNodes_[i]->factor().params();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::fromLog (params);
|
Util::exp (params);
|
||||||
}
|
}
|
||||||
out << endl << params.size() << endl << " " ;
|
out << params.size() << endl << " " ;
|
||||||
for (unsigned j = 0; j < params.size(); j++) {
|
out << Util::elementsToString (params) << endl << endl;
|
||||||
out << params[j] << " " ;
|
|
||||||
}
|
|
||||||
out << endl;
|
|
||||||
}
|
}
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
@ -360,24 +355,20 @@ FactorGraph::exportToLibDaiFormat (const char* fileName) const
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
out << facNodes_.size() << endl << endl;
|
out << facNodes_.size() << endl << endl;
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
const VarNodes& factorVars = facNodes_[i]->neighbors();
|
Factor f (facNodes_[i]->factor());
|
||||||
out << factorVars.size() << endl;
|
out << f.nrArguments() << endl;
|
||||||
for (int j = factorVars.size() - 1; j >= 0; j--) {
|
out << Util::elementsToString (f.arguments()) << endl;
|
||||||
out << factorVars[j]->varId() << " " ;
|
out << Util::elementsToString (f.ranges()) << endl;
|
||||||
}
|
VarIds args = f.arguments();
|
||||||
out << endl;
|
std::reverse (args.begin(), args.end());
|
||||||
for (unsigned j = 0; j < factorVars.size(); j++) {
|
f.reorderArguments (args);
|
||||||
out << factorVars[j]->range() << " " ;
|
|
||||||
}
|
|
||||||
out << endl;
|
|
||||||
Params params = facNodes_[i]->factor().params();
|
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::fromLog (params);
|
Util::exp (f.params());
|
||||||
}
|
}
|
||||||
out << params.size() << endl;
|
out << f.size() << endl;
|
||||||
for (unsigned j = 0; j < params.size(); j++) {
|
for (size_t j = 0; j < f.size(); j++) {
|
||||||
out << j << " " << params[j] << endl;
|
out << j << " " << f[j] << endl;
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
}
|
}
|
||||||
@ -402,7 +393,7 @@ FactorGraph::containsCycle (void) const
|
|||||||
{
|
{
|
||||||
vector<bool> visitedVars (varNodes_.size(), false);
|
vector<bool> visitedVars (varNodes_.size(), false);
|
||||||
vector<bool> visitedFactors (facNodes_.size(), false);
|
vector<bool> visitedFactors (facNodes_.size(), false);
|
||||||
for (unsigned i = 0; i < varNodes_.size(); i++) {
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||||
int v = varNodes_[i]->getIndex();
|
int v = varNodes_[i]->getIndex();
|
||||||
if (!visitedVars[v]) {
|
if (!visitedVars[v]) {
|
||||||
if (containsCycle (varNodes_[i], 0, visitedVars, visitedFactors)) {
|
if (containsCycle (varNodes_[i], 0, visitedVars, visitedFactors)) {
|
||||||
@ -424,7 +415,7 @@ FactorGraph::containsCycle (
|
|||||||
{
|
{
|
||||||
visitedVars[v->getIndex()] = true;
|
visitedVars[v->getIndex()] = true;
|
||||||
const FacNodes& adjacencies = v->neighbors();
|
const FacNodes& adjacencies = v->neighbors();
|
||||||
for (unsigned i = 0; i < adjacencies.size(); i++) {
|
for (size_t i = 0; i < adjacencies.size(); i++) {
|
||||||
int w = adjacencies[i]->getIndex();
|
int w = adjacencies[i]->getIndex();
|
||||||
if (!visitedFactors[w]) {
|
if (!visitedFactors[w]) {
|
||||||
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {
|
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {
|
||||||
@ -449,7 +440,7 @@ FactorGraph::containsCycle (
|
|||||||
{
|
{
|
||||||
visitedFactors[v->getIndex()] = true;
|
visitedFactors[v->getIndex()] = true;
|
||||||
const VarNodes& adjacencies = v->neighbors();
|
const VarNodes& adjacencies = v->neighbors();
|
||||||
for (unsigned i = 0; i < adjacencies.size(); i++) {
|
for (size_t i = 0; i < adjacencies.size(); i++) {
|
||||||
int w = adjacencies[i]->getIndex();
|
int w = adjacencies[i]->getIndex();
|
||||||
if (!visitedVars[w]) {
|
if (!visitedVars[w]) {
|
||||||
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {
|
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {
|
||||||
|
@ -32,6 +32,7 @@ class VarNode : public Var
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FacNode
|
class FacNode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -45,9 +46,9 @@ class FacNode
|
|||||||
|
|
||||||
const VarNodes& neighbors (void) const { return neighs_; }
|
const VarNodes& neighbors (void) const { return neighs_; }
|
||||||
|
|
||||||
int getIndex (void) const { return index_; }
|
size_t getIndex (void) const { return index_; }
|
||||||
|
|
||||||
void setIndex (int index) { index_ = index; }
|
void setIndex (size_t index) { index_ = index; }
|
||||||
|
|
||||||
string getLabel (void) { return factor_.getLabel(); }
|
string getLabel (void) { return factor_.getLabel(); }
|
||||||
|
|
||||||
@ -56,18 +57,10 @@ class FacNode
|
|||||||
|
|
||||||
VarNodes neighs_;
|
VarNodes neighs_;
|
||||||
Factor factor_;
|
Factor factor_;
|
||||||
int index_;
|
size_t index_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct CompVarId
|
|
||||||
{
|
|
||||||
bool operator() (const Var* v1, const Var* v2) const
|
|
||||||
{
|
|
||||||
return v1->varId() < v2->varId();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class FactorGraph
|
class FactorGraph
|
||||||
{
|
{
|
||||||
@ -84,9 +77,9 @@ class FactorGraph
|
|||||||
|
|
||||||
bool isFromBayesNetwork (void) const { return fromBayesNet_ ; }
|
bool isFromBayesNetwork (void) const { return fromBayesNet_ ; }
|
||||||
|
|
||||||
unsigned nrVarNodes (void) const { return varNodes_.size(); }
|
size_t nrVarNodes (void) const { return varNodes_.size(); }
|
||||||
|
|
||||||
unsigned nrFacNodes (void) const { return facNodes_.size(); }
|
size_t nrFacNodes (void) const { return facNodes_.size(); }
|
||||||
|
|
||||||
VarNode* getVarNode (VarId vid) const
|
VarNode* getVarNode (VarId vid) const
|
||||||
{
|
{
|
||||||
@ -141,5 +134,15 @@ class FactorGraph
|
|||||||
VarMap varMap_;
|
VarMap varMap_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct sortByVarId
|
||||||
|
{
|
||||||
|
bool operator()(VarNode* vn1, VarNode* vn2) {
|
||||||
|
return vn1->varId() < vn2->varId();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // HORUS_FACTORGRAPH_H
|
#endif // HORUS_FACTORGRAPH_H
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ LiftedOperator::printValidOps (
|
|||||||
{
|
{
|
||||||
vector<LiftedOperator*> validOps;
|
vector<LiftedOperator*> validOps;
|
||||||
validOps = LiftedOperator::getValidOps (pfList, query);
|
validOps = LiftedOperator::getValidOps (pfList, query);
|
||||||
for (unsigned i = 0; i < validOps.size(); i++) {
|
for (size_t i = 0; i < validOps.size(); i++) {
|
||||||
cout << "-> " << validOps[i]->toString();
|
cout << "-> " << validOps[i]->toString();
|
||||||
delete validOps[i];
|
delete validOps[i];
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ LiftedOperator::printValidOps (
|
|||||||
|
|
||||||
vector<ParfactorList::iterator>
|
vector<ParfactorList::iterator>
|
||||||
LiftedOperator::getParfactorsWithGroup (
|
LiftedOperator::getParfactorsWithGroup (
|
||||||
ParfactorList& pfList, unsigned group)
|
ParfactorList& pfList, PrvGroup group)
|
||||||
{
|
{
|
||||||
vector<ParfactorList::iterator> iters;
|
vector<ParfactorList::iterator> iters;
|
||||||
ParfactorList::iterator pflIt = pfList.begin();
|
ParfactorList::iterator pflIt = pfList.begin();
|
||||||
@ -144,17 +144,17 @@ ProductOperator::toString (void)
|
|||||||
bool
|
bool
|
||||||
ProductOperator::validOp (Parfactor* g1, Parfactor* g2)
|
ProductOperator::validOp (Parfactor* g1, Parfactor* g2)
|
||||||
{
|
{
|
||||||
TinySet<unsigned> g1_gs (g1->getAllGroups());
|
TinySet<PrvGroup> g1_gs (g1->getAllGroups());
|
||||||
TinySet<unsigned> g2_gs (g2->getAllGroups());
|
TinySet<PrvGroup> g2_gs (g2->getAllGroups());
|
||||||
if (g1_gs.contains (g2_gs) || g2_gs.contains (g1_gs)) {
|
if (g1_gs.contains (g2_gs) || g2_gs.contains (g1_gs)) {
|
||||||
TinySet<unsigned> intersect = g1_gs & g2_gs;
|
TinySet<PrvGroup> intersect = g1_gs & g2_gs;
|
||||||
for (unsigned i = 0; i < intersect.size(); i++) {
|
for (size_t i = 0; i < intersect.size(); i++) {
|
||||||
if (g1->nrFormulasWithGroup (intersect[i]) != 1 ||
|
if (g1->nrFormulasWithGroup (intersect[i]) != 1 ||
|
||||||
g2->nrFormulasWithGroup (intersect[i]) != 1) {
|
g2->nrFormulasWithGroup (intersect[i]) != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int idx1 = g1->indexOfGroup (intersect[i]);
|
size_t idx1 = g1->indexOfGroup (intersect[i]);
|
||||||
int idx2 = g2->indexOfGroup (intersect[i]);
|
size_t idx2 = g2->indexOfGroup (intersect[i]);
|
||||||
if (g1->range (idx1) != g2->range (idx2)) {
|
if (g1->range (idx1) != g2->range (idx2)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -169,13 +169,13 @@ ProductOperator::validOp (Parfactor* g1, Parfactor* g2)
|
|||||||
double
|
double
|
||||||
SumOutOperator::getLogCost (void)
|
SumOutOperator::getLogCost (void)
|
||||||
{
|
{
|
||||||
TinySet<unsigned> groupSet;
|
TinySet<PrvGroup> groupSet;
|
||||||
ParfactorList::const_iterator pfIter = pfList_.begin();
|
ParfactorList::const_iterator pfIter = pfList_.begin();
|
||||||
unsigned nrProdFactors = 0;
|
unsigned nrProdFactors = 0;
|
||||||
while (pfIter != pfList_.end()) {
|
while (pfIter != pfList_.end()) {
|
||||||
if ((*pfIter)->containsGroup (group_)) {
|
if ((*pfIter)->containsGroup (group_)) {
|
||||||
vector<unsigned> groups = (*pfIter)->getAllGroups();
|
vector<PrvGroup> groups = (*pfIter)->getAllGroups();
|
||||||
groupSet |= TinySet<unsigned> (groups);
|
groupSet |= TinySet<PrvGroup> (groups);
|
||||||
++ nrProdFactors;
|
++ nrProdFactors;
|
||||||
}
|
}
|
||||||
++ pfIter;
|
++ pfIter;
|
||||||
@ -185,11 +185,11 @@ SumOutOperator::getLogCost (void)
|
|||||||
return std::log (0.0);
|
return std::log (0.0);
|
||||||
}
|
}
|
||||||
double cost = 1.0;
|
double cost = 1.0;
|
||||||
for (unsigned i = 0; i < groupSet.size(); i++) {
|
for (size_t i = 0; i < groupSet.size(); i++) {
|
||||||
pfIter = pfList_.begin();
|
pfIter = pfList_.begin();
|
||||||
while (pfIter != pfList_.end()) {
|
while (pfIter != pfList_.end()) {
|
||||||
if ((*pfIter)->containsGroup (groupSet[i])) {
|
if ((*pfIter)->containsGroup (groupSet[i])) {
|
||||||
int idx = (*pfIter)->indexOfGroup (groupSet[i]);
|
size_t idx = (*pfIter)->indexOfGroup (groupSet[i]);
|
||||||
cost *= (*pfIter)->range (idx);
|
cost *= (*pfIter)->range (idx);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -208,7 +208,7 @@ SumOutOperator::apply (void)
|
|||||||
iters = getParfactorsWithGroup (pfList_, group_);
|
iters = getParfactorsWithGroup (pfList_, group_);
|
||||||
Parfactor* product = *(iters[0]);
|
Parfactor* product = *(iters[0]);
|
||||||
pfList_.remove (iters[0]);
|
pfList_.remove (iters[0]);
|
||||||
for (unsigned i = 1; i < iters.size(); i++) {
|
for (size_t i = 1; i < iters.size(); i++) {
|
||||||
product->multiply (**(iters[i]));
|
product->multiply (**(iters[i]));
|
||||||
pfList_.removeAndDelete (iters[i]);
|
pfList_.removeAndDelete (iters[i]);
|
||||||
}
|
}
|
||||||
@ -216,15 +216,15 @@ SumOutOperator::apply (void)
|
|||||||
delete product;
|
delete product;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int fIdx = product->indexOfGroup (group_);
|
size_t fIdx = product->indexOfGroup (group_);
|
||||||
LogVarSet excl = product->exclusiveLogVars (fIdx);
|
LogVarSet excl = product->exclusiveLogVars (fIdx);
|
||||||
if (product->constr()->isCountNormalized (excl)) {
|
if (product->constr()->isCountNormalized (excl)) {
|
||||||
product->sumOut (fIdx);
|
product->sumOutIndex (fIdx);
|
||||||
pfList_.addShattered (product);
|
pfList_.addShattered (product);
|
||||||
} else {
|
} else {
|
||||||
Parfactors pfs = FoveSolver::countNormalize (product, excl);
|
Parfactors pfs = FoveSolver::countNormalize (product, excl);
|
||||||
for (unsigned i = 0; i < pfs.size(); i++) {
|
for (size_t i = 0; i < pfs.size(); i++) {
|
||||||
pfs[i]->sumOut (fIdx);
|
pfs[i]->sumOutIndex (fIdx);
|
||||||
pfList_.add (pfs[i]);
|
pfList_.add (pfs[i]);
|
||||||
}
|
}
|
||||||
delete product;
|
delete product;
|
||||||
@ -239,16 +239,16 @@ SumOutOperator::getValidOps (
|
|||||||
const Grounds& query)
|
const Grounds& query)
|
||||||
{
|
{
|
||||||
vector<SumOutOperator*> validOps;
|
vector<SumOutOperator*> validOps;
|
||||||
set<unsigned> allGroups;
|
set<PrvGroup> allGroups;
|
||||||
ParfactorList::const_iterator it = pfList.begin();
|
ParfactorList::const_iterator it = pfList.begin();
|
||||||
while (it != pfList.end()) {
|
while (it != pfList.end()) {
|
||||||
const ProbFormulas& formulas = (*it)->arguments();
|
const ProbFormulas& formulas = (*it)->arguments();
|
||||||
for (unsigned i = 0; i < formulas.size(); i++) {
|
for (size_t i = 0; i < formulas.size(); i++) {
|
||||||
allGroups.insert (formulas[i].group());
|
allGroups.insert (formulas[i].group());
|
||||||
}
|
}
|
||||||
++ it;
|
++ it;
|
||||||
}
|
}
|
||||||
set<unsigned>::const_iterator groupIt = allGroups.begin();
|
set<PrvGroup>::const_iterator groupIt = allGroups.begin();
|
||||||
while (groupIt != allGroups.end()) {
|
while (groupIt != allGroups.end()) {
|
||||||
if (validOp (*groupIt, pfList, query)) {
|
if (validOp (*groupIt, pfList, query)) {
|
||||||
validOps.push_back (new SumOutOperator (*groupIt, pfList));
|
validOps.push_back (new SumOutOperator (*groupIt, pfList));
|
||||||
@ -266,7 +266,7 @@ SumOutOperator::toString (void)
|
|||||||
stringstream ss;
|
stringstream ss;
|
||||||
vector<ParfactorList::iterator> pfIters;
|
vector<ParfactorList::iterator> pfIters;
|
||||||
pfIters = getParfactorsWithGroup (pfList_, group_);
|
pfIters = getParfactorsWithGroup (pfList_, group_);
|
||||||
int idx = (*pfIters[0])->indexOfGroup (group_);
|
size_t idx = (*pfIters[0])->indexOfGroup (group_);
|
||||||
ProbFormula f = (*pfIters[0])->argument (idx);
|
ProbFormula f = (*pfIters[0])->argument (idx);
|
||||||
TupleSet tupleSet = (*pfIters[0])->constr()->tupleSet (f.logVars());
|
TupleSet tupleSet = (*pfIters[0])->constr()->tupleSet (f.logVars());
|
||||||
ss << "sum out " << f.functor() << "/" << f.arity();
|
ss << "sum out " << f.functor() << "/" << f.arity();
|
||||||
@ -279,7 +279,7 @@ SumOutOperator::toString (void)
|
|||||||
|
|
||||||
bool
|
bool
|
||||||
SumOutOperator::validOp (
|
SumOutOperator::validOp (
|
||||||
unsigned group,
|
PrvGroup group,
|
||||||
ParfactorList& pfList,
|
ParfactorList& pfList,
|
||||||
const Grounds& query)
|
const Grounds& query)
|
||||||
{
|
{
|
||||||
@ -289,11 +289,11 @@ SumOutOperator::validOp (
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int range = -1;
|
int range = -1;
|
||||||
for (unsigned i = 0; i < pfIters.size(); i++) {
|
for (size_t i = 0; i < pfIters.size(); i++) {
|
||||||
if ((*pfIters[i])->nrFormulasWithGroup (group) > 1) {
|
if ((*pfIters[i])->nrFormulasWithGroup (group) > 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int fIdx = (*pfIters[i])->indexOfGroup (group);
|
size_t fIdx = (*pfIters[i])->indexOfGroup (group);
|
||||||
if ((*pfIters[i])->argument (fIdx).contains (
|
if ((*pfIters[i])->argument (fIdx).contains (
|
||||||
(*pfIters[i])->elimLogVars()) == false) {
|
(*pfIters[i])->elimLogVars()) == false) {
|
||||||
return false;
|
return false;
|
||||||
@ -312,13 +312,13 @@ SumOutOperator::validOp (
|
|||||||
bool
|
bool
|
||||||
SumOutOperator::isToEliminate (
|
SumOutOperator::isToEliminate (
|
||||||
Parfactor* g,
|
Parfactor* g,
|
||||||
unsigned group,
|
PrvGroup group,
|
||||||
const Grounds& query)
|
const Grounds& query)
|
||||||
{
|
{
|
||||||
int fIdx = g->indexOfGroup (group);
|
size_t fIdx = g->indexOfGroup (group);
|
||||||
const ProbFormula& formula = g->argument (fIdx);
|
const ProbFormula& formula = g->argument (fIdx);
|
||||||
bool toElim = true;
|
bool toElim = true;
|
||||||
for (unsigned i = 0; i < query.size(); i++) {
|
for (size_t i = 0; i < query.size(); i++) {
|
||||||
if (formula.functor() == query[i].functor() &&
|
if (formula.functor() == query[i].functor() &&
|
||||||
formula.arity() == query[i].arity()) {
|
formula.arity() == query[i].arity()) {
|
||||||
g->constr()->moveToTop (formula.logVars());
|
g->constr()->moveToTop (formula.logVars());
|
||||||
@ -337,23 +337,23 @@ double
|
|||||||
CountingOperator::getLogCost (void)
|
CountingOperator::getLogCost (void)
|
||||||
{
|
{
|
||||||
double cost = 0.0;
|
double cost = 0.0;
|
||||||
int fIdx = (*pfIter_)->indexOfLogVar (X_);
|
size_t fIdx = (*pfIter_)->indexOfLogVar (X_);
|
||||||
unsigned range = (*pfIter_)->range (fIdx);
|
unsigned range = (*pfIter_)->range (fIdx);
|
||||||
unsigned size = (*pfIter_)->size() / range;
|
unsigned size = (*pfIter_)->size() / range;
|
||||||
TinySet<unsigned> counts;
|
TinySet<unsigned> counts;
|
||||||
counts = (*pfIter_)->constr()->getConditionalCounts (X_);
|
counts = (*pfIter_)->constr()->getConditionalCounts (X_);
|
||||||
for (unsigned i = 0; i < counts.size(); i++) {
|
for (size_t i = 0; i < counts.size(); i++) {
|
||||||
cost += size * HistogramSet::nrHistograms (counts[i], range);
|
cost += size * HistogramSet::nrHistograms (counts[i], range);
|
||||||
}
|
}
|
||||||
unsigned group = (*pfIter_)->argument (fIdx).group();
|
PrvGroup group = (*pfIter_)->argument (fIdx).group();
|
||||||
int lvIndex = Util::indexOf (
|
size_t lvIndex = Util::indexOf (
|
||||||
(*pfIter_)->argument (fIdx).logVars(), X_);
|
(*pfIter_)->argument (fIdx).logVars(), X_);
|
||||||
assert (lvIndex != -1);
|
assert (lvIndex != (*pfIter_)->argument (fIdx).logVars().size());
|
||||||
ParfactorList::iterator pfIter = pfList_.begin();
|
ParfactorList::iterator pfIter = pfList_.begin();
|
||||||
while (pfIter != pfList_.end()) {
|
while (pfIter != pfList_.end()) {
|
||||||
if (pfIter != pfIter_) {
|
if (pfIter != pfIter_) {
|
||||||
int fIdx2 = (*pfIter)->indexOfGroup (group);
|
size_t fIdx2 = (*pfIter)->indexOfGroup (group);
|
||||||
if (fIdx2 != -1) {
|
if (fIdx2 != (*pfIter)->nrArguments()) {
|
||||||
LogVar Y = ((*pfIter)->argument (fIdx2).logVars()[lvIndex]);
|
LogVar Y = ((*pfIter)->argument (fIdx2).logVars()[lvIndex]);
|
||||||
if ((*pfIter)->canCountConvert (Y) == false) {
|
if ((*pfIter)->canCountConvert (Y) == false) {
|
||||||
// the real cost should be the cost of grounding Y
|
// the real cost should be the cost of grounding Y
|
||||||
@ -377,7 +377,7 @@ CountingOperator::apply (void)
|
|||||||
Parfactor* pf = *pfIter_;
|
Parfactor* pf = *pfIter_;
|
||||||
pfList_.remove (pfIter_);
|
pfList_.remove (pfIter_);
|
||||||
Parfactors pfs = FoveSolver::countNormalize (pf, X_);
|
Parfactors pfs = FoveSolver::countNormalize (pf, X_);
|
||||||
for (unsigned i = 0; i < pfs.size(); i++) {
|
for (size_t i = 0; i < pfs.size(); i++) {
|
||||||
unsigned condCount = pfs[i]->constr()->getConditionalCount (X_);
|
unsigned condCount = pfs[i]->constr()->getConditionalCount (X_);
|
||||||
bool cartProduct = pfs[i]->constr()->isCartesianProduct (
|
bool cartProduct = pfs[i]->constr()->isCartesianProduct (
|
||||||
pfs[i]->countedLogVars() | X_);
|
pfs[i]->countedLogVars() | X_);
|
||||||
@ -399,7 +399,7 @@ CountingOperator::getValidOps (ParfactorList& pfList)
|
|||||||
ParfactorList::iterator it = pfList.begin();
|
ParfactorList::iterator it = pfList.begin();
|
||||||
while (it != pfList.end()) {
|
while (it != pfList.end()) {
|
||||||
LogVarSet candidates = (*it)->uncountedLogVars();
|
LogVarSet candidates = (*it)->uncountedLogVars();
|
||||||
for (unsigned i = 0; i < candidates.size(); i++) {
|
for (size_t i = 0; i < candidates.size(); i++) {
|
||||||
if (validOp (*it, candidates[i])) {
|
if (validOp (*it, candidates[i])) {
|
||||||
validOps.push_back (new CountingOperator (
|
validOps.push_back (new CountingOperator (
|
||||||
it, candidates[i], pfList));
|
it, candidates[i], pfList));
|
||||||
@ -422,11 +422,11 @@ CountingOperator::toString (void)
|
|||||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
|
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
|
||||||
Parfactors pfs = FoveSolver::countNormalize (*pfIter_, X_);
|
Parfactors pfs = FoveSolver::countNormalize (*pfIter_, X_);
|
||||||
if ((*pfIter_)->constr()->isCountNormalized (X_) == false) {
|
if ((*pfIter_)->constr()->isCountNormalized (X_) == false) {
|
||||||
for (unsigned i = 0; i < pfs.size(); i++) {
|
for (size_t i = 0; i < pfs.size(); i++) {
|
||||||
ss << " º " << pfs[i]->getLabel() << endl;
|
ss << " º " << pfs[i]->getLabel() << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < pfs.size(); i++) {
|
for (size_t i = 0; i < pfs.size(); i++) {
|
||||||
delete pfs[i];
|
delete pfs[i];
|
||||||
}
|
}
|
||||||
return ss.str();
|
return ss.str();
|
||||||
@ -440,7 +440,7 @@ CountingOperator::validOp (Parfactor* g, LogVar X)
|
|||||||
if (g->nrFormulas (X) != 1) {
|
if (g->nrFormulas (X) != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int fIdx = g->indexOfLogVar (X);
|
size_t fIdx = g->indexOfLogVar (X);
|
||||||
if (g->argument (fIdx).isCounting()) {
|
if (g->argument (fIdx).isCounting()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -456,10 +456,10 @@ CountingOperator::validOp (Parfactor* g, LogVar X)
|
|||||||
double
|
double
|
||||||
GroundOperator::getLogCost (void)
|
GroundOperator::getLogCost (void)
|
||||||
{
|
{
|
||||||
vector<pair<unsigned, unsigned>> affectedFormulas;
|
vector<pair<PrvGroup, unsigned>> affectedFormulas;
|
||||||
affectedFormulas = getAffectedFormulas();
|
affectedFormulas = getAffectedFormulas();
|
||||||
// cout << "affected formulas: " ;
|
// cout << "affected formulas: " ;
|
||||||
// for (unsigned i = 0; i < affectedFormulas.size(); i++) {
|
// for (size_t i = 0; i < affectedFormulas.size(); i++) {
|
||||||
// cout << affectedFormulas[i].first << ":" ;
|
// cout << affectedFormulas[i].first << ":" ;
|
||||||
// cout << affectedFormulas[i].second << " " ;
|
// cout << affectedFormulas[i].second << " " ;
|
||||||
// }
|
// }
|
||||||
@ -472,9 +472,9 @@ GroundOperator::getLogCost (void)
|
|||||||
double pfSize = std::log (pf->size());
|
double pfSize = std::log (pf->size());
|
||||||
bool willBeAffected = false;
|
bool willBeAffected = false;
|
||||||
LogVarSet lvsToGround;
|
LogVarSet lvsToGround;
|
||||||
for (unsigned i = 0; i < affectedFormulas.size(); i++) {
|
for (size_t i = 0; i < affectedFormulas.size(); i++) {
|
||||||
int fIdx = pf->indexOfGroup (affectedFormulas[i].first);
|
size_t fIdx = pf->indexOfGroup (affectedFormulas[i].first);
|
||||||
if (fIdx != -1) {
|
if (fIdx != pf->nrArguments()) {
|
||||||
ProbFormula f = pf->argument (fIdx);
|
ProbFormula f = pf->argument (fIdx);
|
||||||
LogVar X = f.logVars()[affectedFormulas[i].second];
|
LogVar X = f.logVars()[affectedFormulas[i].second];
|
||||||
bool isCountingLv = pf->countedLogVars().contains (X);
|
bool isCountingLv = pf->countedLogVars().contains (X);
|
||||||
@ -514,7 +514,7 @@ GroundOperator::apply (void)
|
|||||||
ParfactorList::iterator pfIter;
|
ParfactorList::iterator pfIter;
|
||||||
pfIter = getParfactorsWithGroup (pfList_, group_).front();
|
pfIter = getParfactorsWithGroup (pfList_, group_).front();
|
||||||
Parfactor* pf = *pfIter;
|
Parfactor* pf = *pfIter;
|
||||||
int idx = pf->indexOfGroup (group_);
|
size_t idx = pf->indexOfGroup (group_);
|
||||||
ProbFormula f = pf->argument (idx);
|
ProbFormula f = pf->argument (idx);
|
||||||
LogVar X = f.logVars()[lvIndex_];
|
LogVar X = f.logVars()[lvIndex_];
|
||||||
bool countedLv = pf->countedLogVars().contains (X);
|
bool countedLv = pf->countedLogVars().contains (X);
|
||||||
@ -524,7 +524,7 @@ GroundOperator::apply (void)
|
|||||||
pfList_.add (pf);
|
pfList_.add (pf);
|
||||||
} else {
|
} else {
|
||||||
ConstraintTrees cts = pf->constr()->ground (X);
|
ConstraintTrees cts = pf->constr()->ground (X);
|
||||||
for (unsigned i = 0; i < cts.size(); i++) {
|
for (size_t i = 0; i < cts.size(); i++) {
|
||||||
pfList_.add (new Parfactor (pf, cts[i]));
|
pfList_.add (new Parfactor (pf, cts[i]));
|
||||||
}
|
}
|
||||||
delete pf;
|
delete pf;
|
||||||
@ -542,14 +542,14 @@ vector<GroundOperator*>
|
|||||||
GroundOperator::getValidOps (ParfactorList& pfList)
|
GroundOperator::getValidOps (ParfactorList& pfList)
|
||||||
{
|
{
|
||||||
vector<GroundOperator*> validOps;
|
vector<GroundOperator*> validOps;
|
||||||
set<unsigned> allGroups;
|
set<PrvGroup> allGroups;
|
||||||
ParfactorList::const_iterator it = pfList.begin();
|
ParfactorList::const_iterator it = pfList.begin();
|
||||||
while (it != pfList.end()) {
|
while (it != pfList.end()) {
|
||||||
const ProbFormulas& formulas = (*it)->arguments();
|
const ProbFormulas& formulas = (*it)->arguments();
|
||||||
for (unsigned i = 0; i < formulas.size(); i++) {
|
for (size_t i = 0; i < formulas.size(); i++) {
|
||||||
if (Util::contains (allGroups, formulas[i].group()) == false) {
|
if (Util::contains (allGroups, formulas[i].group()) == false) {
|
||||||
const LogVars& lvs = formulas[i].logVars();
|
const LogVars& lvs = formulas[i].logVars();
|
||||||
for (unsigned j = 0; j < lvs.size(); j++) {
|
for (size_t j = 0; j < lvs.size(); j++) {
|
||||||
if ((*it)->constr()->isSingleton (lvs[j]) == false) {
|
if ((*it)->constr()->isSingleton (lvs[j]) == false) {
|
||||||
validOps.push_back (new GroundOperator (
|
validOps.push_back (new GroundOperator (
|
||||||
formulas[i].group(), j, pfList));
|
formulas[i].group(), j, pfList));
|
||||||
@ -572,7 +572,7 @@ GroundOperator::toString (void)
|
|||||||
vector<ParfactorList::iterator> pfIters;
|
vector<ParfactorList::iterator> pfIters;
|
||||||
pfIters = getParfactorsWithGroup (pfList_, group_);
|
pfIters = getParfactorsWithGroup (pfList_, group_);
|
||||||
Parfactor* pf = *(getParfactorsWithGroup (pfList_, group_).front());
|
Parfactor* pf = *(getParfactorsWithGroup (pfList_, group_).front());
|
||||||
int idx = pf->indexOfGroup (group_);
|
size_t idx = pf->indexOfGroup (group_);
|
||||||
ProbFormula f = pf->argument (idx);
|
ProbFormula f = pf->argument (idx);
|
||||||
LogVar lv = f.logVars()[lvIndex_];
|
LogVar lv = f.logVars()[lvIndex_];
|
||||||
TupleSet tupleSet = pf->constr()->tupleSet ({lv});
|
TupleSet tupleSet = pf->constr()->tupleSet ({lv});
|
||||||
@ -593,25 +593,25 @@ GroundOperator::toString (void)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
vector<pair<unsigned, unsigned>>
|
vector<pair<PrvGroup, unsigned>>
|
||||||
GroundOperator::getAffectedFormulas (void)
|
GroundOperator::getAffectedFormulas (void)
|
||||||
{
|
{
|
||||||
vector<pair<unsigned, unsigned>> affectedFormulas;
|
vector<pair<PrvGroup, unsigned>> affectedFormulas;
|
||||||
affectedFormulas.push_back (make_pair (group_, lvIndex_));
|
affectedFormulas.push_back (make_pair (group_, lvIndex_));
|
||||||
queue<pair<unsigned, unsigned>> q;
|
queue<pair<PrvGroup, unsigned>> q;
|
||||||
q.push (make_pair (group_, lvIndex_));
|
q.push (make_pair (group_, lvIndex_));
|
||||||
while (q.empty() == false) {
|
while (q.empty() == false) {
|
||||||
pair<unsigned, unsigned> front = q.front();
|
pair<PrvGroup, unsigned> front = q.front();
|
||||||
ParfactorList::iterator pflIt = pfList_.begin();
|
ParfactorList::iterator pflIt = pfList_.begin();
|
||||||
while (pflIt != pfList_.end()) {
|
while (pflIt != pfList_.end()) {
|
||||||
int idx = (*pflIt)->indexOfGroup (front.first);
|
size_t idx = (*pflIt)->indexOfGroup (front.first);
|
||||||
if (idx != -1) {
|
if (idx != (*pflIt)->nrArguments()) {
|
||||||
ProbFormula f = (*pflIt)->argument (idx);
|
ProbFormula f = (*pflIt)->argument (idx);
|
||||||
LogVar X = f.logVars()[front.second];
|
LogVar X = f.logVars()[front.second];
|
||||||
const ProbFormulas& fs = (*pflIt)->arguments();
|
const ProbFormulas& fs = (*pflIt)->arguments();
|
||||||
for (unsigned i = 0; i < fs.size(); i++) {
|
for (size_t i = 0; i < fs.size(); i++) {
|
||||||
if ((int)i != idx && fs[i].contains (X)) {
|
if ((int)i != idx && fs[i].contains (X)) {
|
||||||
pair<unsigned, unsigned> pair = make_pair (
|
pair<PrvGroup, unsigned> pair = make_pair (
|
||||||
fs[i].group(), fs[i].indexOf (X));
|
fs[i].group(), fs[i].indexOf (X));
|
||||||
if (Util::contains (affectedFormulas, pair) == false) {
|
if (Util::contains (affectedFormulas, pair) == false) {
|
||||||
q.push (pair);
|
q.push (pair);
|
||||||
@ -644,7 +644,7 @@ FoveSolver::getJointDistributionOf (const Grounds& query)
|
|||||||
(*pfList_.begin())->normalize();
|
(*pfList_.begin())->normalize();
|
||||||
Params params = (*pfList_.begin())->params();
|
Params params = (*pfList_.begin())->params();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::fromLog (params);
|
Util::exp (params);
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
@ -668,7 +668,7 @@ FoveSolver::absorveEvidence (
|
|||||||
ParfactorList& pfList,
|
ParfactorList& pfList,
|
||||||
ObservedFormulas& obsFormulas)
|
ObservedFormulas& obsFormulas)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < obsFormulas.size(); i++) {
|
for (size_t i = 0; i < obsFormulas.size(); i++) {
|
||||||
Parfactors newPfs;
|
Parfactors newPfs;
|
||||||
ParfactorList::iterator it = pfList.begin();
|
ParfactorList::iterator it = pfList.begin();
|
||||||
while (it != pfList.end()) {
|
while (it != pfList.end()) {
|
||||||
@ -692,7 +692,7 @@ FoveSolver::absorveEvidence (
|
|||||||
if (Globals::verbosity > 2 && obsFormulas.empty() == false) {
|
if (Globals::verbosity > 2 && obsFormulas.empty() == false) {
|
||||||
Util::printAsteriskLine();
|
Util::printAsteriskLine();
|
||||||
cout << "AFTER EVIDENCE ABSORVED" << endl;
|
cout << "AFTER EVIDENCE ABSORVED" << endl;
|
||||||
for (unsigned i = 0; i < obsFormulas.size(); i++) {
|
for (size_t i = 0; i < obsFormulas.size(); i++) {
|
||||||
cout << " -> " << obsFormulas[i] << endl;
|
cout << " -> " << obsFormulas[i] << endl;
|
||||||
}
|
}
|
||||||
Util::printAsteriskLine();
|
Util::printAsteriskLine();
|
||||||
@ -712,7 +712,7 @@ FoveSolver::countNormalize (
|
|||||||
normPfs.push_back (new Parfactor (*g));
|
normPfs.push_back (new Parfactor (*g));
|
||||||
} else {
|
} else {
|
||||||
ConstraintTrees normCts = g->constr()->countNormalize (set);
|
ConstraintTrees normCts = g->constr()->countNormalize (set);
|
||||||
for (unsigned i = 0; i < normCts.size(); i++) {
|
for (size_t i = 0; i < normCts.size(); i++) {
|
||||||
normPfs.push_back (new Parfactor (g, normCts[i]));
|
normPfs.push_back (new Parfactor (g, normCts[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -727,17 +727,17 @@ FoveSolver::calcGroundMultiplication (Parfactor pf)
|
|||||||
LogVarSet lvs = pf.constr()->logVarSet();
|
LogVarSet lvs = pf.constr()->logVarSet();
|
||||||
lvs -= pf.constr()->singletons();
|
lvs -= pf.constr()->singletons();
|
||||||
Parfactors newPfs = {new Parfactor (pf)};
|
Parfactors newPfs = {new Parfactor (pf)};
|
||||||
for (unsigned i = 0; i < lvs.size(); i++) {
|
for (size_t i = 0; i < lvs.size(); i++) {
|
||||||
Parfactors pfs = newPfs;
|
Parfactors pfs = newPfs;
|
||||||
newPfs.clear();
|
newPfs.clear();
|
||||||
for (unsigned j = 0; j < pfs.size(); j++) {
|
for (size_t j = 0; j < pfs.size(); j++) {
|
||||||
bool countedLv = pfs[j]->countedLogVars().contains (lvs[i]);
|
bool countedLv = pfs[j]->countedLogVars().contains (lvs[i]);
|
||||||
if (countedLv) {
|
if (countedLv) {
|
||||||
pfs[j]->fullExpand (lvs[i]);
|
pfs[j]->fullExpand (lvs[i]);
|
||||||
newPfs.push_back (pfs[j]);
|
newPfs.push_back (pfs[j]);
|
||||||
} else {
|
} else {
|
||||||
ConstraintTrees cts = pfs[j]->constr()->ground (lvs[i]);
|
ConstraintTrees cts = pfs[j]->constr()->ground (lvs[i]);
|
||||||
for (unsigned k = 0; k < cts.size(); k++) {
|
for (size_t k = 0; k < cts.size(); k++) {
|
||||||
newPfs.push_back (new Parfactor (pfs[j], cts[k]));
|
newPfs.push_back (new Parfactor (pfs[j], cts[k]));
|
||||||
}
|
}
|
||||||
delete pfs[j];
|
delete pfs[j];
|
||||||
@ -746,7 +746,7 @@ FoveSolver::calcGroundMultiplication (Parfactor pf)
|
|||||||
}
|
}
|
||||||
ParfactorList pfList (newPfs);
|
ParfactorList pfList (newPfs);
|
||||||
Parfactors groundShatteredPfs (pfList.begin(),pfList.end());
|
Parfactors groundShatteredPfs (pfList.begin(),pfList.end());
|
||||||
for (unsigned i = 1; i < groundShatteredPfs.size(); i++) {
|
for (size_t i = 1; i < groundShatteredPfs.size(); i++) {
|
||||||
groundShatteredPfs[0]->multiply (*groundShatteredPfs[i]);
|
groundShatteredPfs[0]->multiply (*groundShatteredPfs[i]);
|
||||||
}
|
}
|
||||||
return Parfactor (*groundShatteredPfs[0]);
|
return Parfactor (*groundShatteredPfs[0]);
|
||||||
@ -784,7 +784,7 @@ FoveSolver::runSolver (const Grounds& query)
|
|||||||
assert (pfList_.size() > 0);
|
assert (pfList_.size() > 0);
|
||||||
if (pfList_.size() > 1) {
|
if (pfList_.size() > 1) {
|
||||||
ParfactorList::iterator pfIter = pfList_.begin();
|
ParfactorList::iterator pfIter = pfList_.begin();
|
||||||
pfIter ++;
|
++ pfIter;
|
||||||
while (pfIter != pfList_.end()) {
|
while (pfIter != pfList_.end()) {
|
||||||
(*pfList_.begin())->multiply (**pfIter);
|
(*pfList_.begin())->multiply (**pfIter);
|
||||||
++ pfIter;
|
++ pfIter;
|
||||||
@ -807,7 +807,7 @@ FoveSolver::getBestOperation (const Grounds& query)
|
|||||||
LiftedOperator* bestOp = 0;
|
LiftedOperator* bestOp = 0;
|
||||||
vector<LiftedOperator*> validOps;
|
vector<LiftedOperator*> validOps;
|
||||||
validOps = LiftedOperator::getValidOps (pfList_, query);
|
validOps = LiftedOperator::getValidOps (pfList_, query);
|
||||||
for (unsigned i = 0; i < validOps.size(); i++) {
|
for (size_t i = 0; i < validOps.size(); i++) {
|
||||||
double cost = validOps[i]->getLogCost();
|
double cost = validOps[i]->getLogCost();
|
||||||
if ((bestOp == 0) || (cost < bestCost)) {
|
if ((bestOp == 0) || (cost < bestCost)) {
|
||||||
bestOp = validOps[i];
|
bestOp = validOps[i];
|
||||||
@ -817,7 +817,7 @@ FoveSolver::getBestOperation (const Grounds& query)
|
|||||||
if (bestCost > largestCost_) {
|
if (bestCost > largestCost_) {
|
||||||
largestCost_ = bestCost;
|
largestCost_ = bestCost;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < validOps.size(); i++) {
|
for (size_t i = 0; i < validOps.size(); i++) {
|
||||||
if (validOps[i] != bestOp) {
|
if (validOps[i] != bestOp) {
|
||||||
delete validOps[i];
|
delete validOps[i];
|
||||||
}
|
}
|
||||||
@ -830,13 +830,13 @@ FoveSolver::getBestOperation (const Grounds& query)
|
|||||||
void
|
void
|
||||||
FoveSolver::runWeakBayesBall (const Grounds& query)
|
FoveSolver::runWeakBayesBall (const Grounds& query)
|
||||||
{
|
{
|
||||||
queue<unsigned> todo; // groups to process
|
queue<PrvGroup> todo; // groups to process
|
||||||
set<unsigned> done; // processed or in queue
|
set<PrvGroup> done; // processed or in queue
|
||||||
for (unsigned i = 0; i < query.size(); i++) {
|
for (size_t i = 0; i < query.size(); i++) {
|
||||||
ParfactorList::iterator it = pfList_.begin();
|
ParfactorList::iterator it = pfList_.begin();
|
||||||
while (it != pfList_.end()) {
|
while (it != pfList_.end()) {
|
||||||
int group = (*it)->findGroup (query[i]);
|
PrvGroup group = (*it)->findGroup (query[i]);
|
||||||
if (group != -1) {
|
if (group != numeric_limits<PrvGroup>::max()) {
|
||||||
todo.push (group);
|
todo.push (group);
|
||||||
done.insert (group);
|
done.insert (group);
|
||||||
break;
|
break;
|
||||||
@ -847,13 +847,13 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
|
|||||||
|
|
||||||
set<Parfactor*> requiredPfs;
|
set<Parfactor*> requiredPfs;
|
||||||
while (todo.empty() == false) {
|
while (todo.empty() == false) {
|
||||||
unsigned group = todo.front();
|
PrvGroup group = todo.front();
|
||||||
ParfactorList::iterator it = pfList_.begin();
|
ParfactorList::iterator it = pfList_.begin();
|
||||||
while (it != pfList_.end()) {
|
while (it != pfList_.end()) {
|
||||||
if (Util::contains (requiredPfs, *it) == false &&
|
if (Util::contains (requiredPfs, *it) == false &&
|
||||||
(*it)->containsGroup (group)) {
|
(*it)->containsGroup (group)) {
|
||||||
vector<unsigned> groups = (*it)->getAllGroups();
|
vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||||
for (unsigned i = 0; i < groups.size(); i++) {
|
for (size_t i = 0; i < groups.size(); i++) {
|
||||||
if (Util::contains (done, groups[i]) == false) {
|
if (Util::contains (done, groups[i]) == false) {
|
||||||
todo.push (groups[i]);
|
todo.push (groups[i]);
|
||||||
done.insert (groups[i]);
|
done.insert (groups[i]);
|
||||||
@ -889,7 +889,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
|
|||||||
void
|
void
|
||||||
FoveSolver::shatterAgainstQuery (const Grounds& query)
|
FoveSolver::shatterAgainstQuery (const Grounds& query)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < query.size(); i++) {
|
for (size_t i = 0; i < query.size(); i++) {
|
||||||
if (query[i].isAtom()) {
|
if (query[i].isAtom()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -927,7 +927,7 @@ FoveSolver::shatterAgainstQuery (const Grounds& query)
|
|||||||
if (Globals::verbosity > 2) {
|
if (Globals::verbosity > 2) {
|
||||||
Util::printAsteriskLine();
|
Util::printAsteriskLine();
|
||||||
cout << "SHATTERED AGAINST THE QUERY" << endl;
|
cout << "SHATTERED AGAINST THE QUERY" << endl;
|
||||||
for (unsigned i = 0; i < query.size(); i++) {
|
for (size_t i = 0; i < query.size(); i++) {
|
||||||
cout << " -> " << query[i] << endl;
|
cout << " -> " << query[i] << endl;
|
||||||
}
|
}
|
||||||
Util::printAsteriskLine();
|
Util::printAsteriskLine();
|
||||||
@ -944,7 +944,7 @@ FoveSolver::absorve (
|
|||||||
{
|
{
|
||||||
Parfactors absorvedPfs;
|
Parfactors absorvedPfs;
|
||||||
const ProbFormulas& formulas = g->arguments();
|
const ProbFormulas& formulas = g->arguments();
|
||||||
for (unsigned i = 0; i < formulas.size(); i++) {
|
for (size_t i = 0; i < formulas.size(); i++) {
|
||||||
if (obsFormula.functor() == formulas[i].functor() &&
|
if (obsFormula.functor() == formulas[i].functor() &&
|
||||||
obsFormula.arity() == formulas[i].arity()) {
|
obsFormula.arity() == formulas[i].arity()) {
|
||||||
|
|
||||||
@ -971,7 +971,7 @@ FoveSolver::absorve (
|
|||||||
if (formulas.size() > 1) {
|
if (formulas.size() > 1) {
|
||||||
LogVarSet excl = g->exclusiveLogVars (i);
|
LogVarSet excl = g->exclusiveLogVars (i);
|
||||||
Parfactors countNormPfs = countNormalize (g, excl);
|
Parfactors countNormPfs = countNormalize (g, excl);
|
||||||
for (unsigned j = 0; j < countNormPfs.size(); j++) {
|
for (size_t j = 0; j < countNormPfs.size(); j++) {
|
||||||
countNormPfs[j]->absorveEvidence (
|
countNormPfs[j]->absorveEvidence (
|
||||||
formulas[i], obsFormula.evidence());
|
formulas[i], obsFormula.evidence());
|
||||||
absorvedPfs.push_back (countNormPfs[j]);
|
absorvedPfs.push_back (countNormPfs[j]);
|
||||||
|
@ -20,7 +20,7 @@ class LiftedOperator
|
|||||||
static void printValidOps (ParfactorList&, const Grounds&);
|
static void printValidOps (ParfactorList&, const Grounds&);
|
||||||
|
|
||||||
static vector<ParfactorList::iterator> getParfactorsWithGroup (
|
static vector<ParfactorList::iterator> getParfactorsWithGroup (
|
||||||
ParfactorList&, unsigned group);
|
ParfactorList&, PrvGroup group);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ class ProductOperator : public LiftedOperator
|
|||||||
class SumOutOperator : public LiftedOperator
|
class SumOutOperator : public LiftedOperator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SumOutOperator (unsigned group, ParfactorList& pfList)
|
SumOutOperator (PrvGroup group, ParfactorList& pfList)
|
||||||
: group_(group), pfList_(pfList) { }
|
: group_(group), pfList_(pfList) { }
|
||||||
|
|
||||||
double getLogCost (void);
|
double getLogCost (void);
|
||||||
@ -66,11 +66,11 @@ class SumOutOperator : public LiftedOperator
|
|||||||
string toString (void);
|
string toString (void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool validOp (unsigned, ParfactorList&, const Grounds&);
|
static bool validOp (PrvGroup, ParfactorList&, const Grounds&);
|
||||||
|
|
||||||
static bool isToEliminate (Parfactor*, unsigned, const Grounds&);
|
static bool isToEliminate (Parfactor*, PrvGroup, const Grounds&);
|
||||||
|
|
||||||
unsigned group_;
|
PrvGroup group_;
|
||||||
ParfactorList& pfList_;
|
ParfactorList& pfList_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -107,7 +107,7 @@ class GroundOperator : public LiftedOperator
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GroundOperator (
|
GroundOperator (
|
||||||
unsigned group,
|
PrvGroup group,
|
||||||
unsigned lvIndex,
|
unsigned lvIndex,
|
||||||
ParfactorList& pfList)
|
ParfactorList& pfList)
|
||||||
: group_(group), lvIndex_(lvIndex), pfList_(pfList) { }
|
: group_(group), lvIndex_(lvIndex), pfList_(pfList) { }
|
||||||
@ -121,9 +121,9 @@ class GroundOperator : public LiftedOperator
|
|||||||
string toString (void);
|
string toString (void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
vector<pair<unsigned, unsigned>> getAffectedFormulas (void);
|
vector<pair<PrvGroup, unsigned>> getAffectedFormulas (void);
|
||||||
|
|
||||||
unsigned group_;
|
PrvGroup group_;
|
||||||
unsigned lvIndex_;
|
unsigned lvIndex_;
|
||||||
ParfactorList& pfList_;
|
ParfactorList& pfList_;
|
||||||
};
|
};
|
||||||
|
@ -19,7 +19,7 @@ HistogramSet::HistogramSet (unsigned size, unsigned range)
|
|||||||
void
|
void
|
||||||
HistogramSet::nextHistogram (void)
|
HistogramSet::nextHistogram (void)
|
||||||
{
|
{
|
||||||
for (int i = hist_.size() - 2; i >= 0; i--) {
|
for (size_t i = hist_.size() - 1; i-- > 0; ) {
|
||||||
if (hist_[i] > 0) {
|
if (hist_[i] > 0) {
|
||||||
hist_[i] --;
|
hist_[i] --;
|
||||||
hist_[i + 1] = maxCount (i + 1);
|
hist_[i + 1] = maxCount (i + 1);
|
||||||
@ -27,13 +27,14 @@ HistogramSet::nextHistogram (void)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert (std::accumulate (hist_.begin(), hist_.end(), 0) == (int)size_);
|
assert (std::accumulate (hist_.begin(), hist_.end(), 0)
|
||||||
|
== (int) size_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
HistogramSet::operator[] (unsigned idx) const
|
HistogramSet::operator[] (size_t idx) const
|
||||||
{
|
{
|
||||||
assert (idx < hist_.size());
|
assert (idx < hist_.size());
|
||||||
return hist_[idx];
|
return hist_[idx];
|
||||||
@ -82,7 +83,7 @@ HistogramSet::nrHistograms (unsigned N, unsigned R)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
size_t
|
||||||
HistogramSet::findIndex (
|
HistogramSet::findIndex (
|
||||||
const Histogram& h,
|
const Histogram& h,
|
||||||
const vector<Histogram>& hists)
|
const vector<Histogram>& hists)
|
||||||
@ -126,10 +127,10 @@ ostream& operator<< (ostream &os, const HistogramSet& hs)
|
|||||||
|
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
HistogramSet::maxCount (unsigned idx) const
|
HistogramSet::maxCount (size_t idx) const
|
||||||
{
|
{
|
||||||
unsigned sum = 0;
|
unsigned sum = 0;
|
||||||
for (unsigned i = 0; i < idx; i++) {
|
for (size_t i = 0; i < idx; i++) {
|
||||||
sum += hist_[i];
|
sum += hist_[i];
|
||||||
}
|
}
|
||||||
return size_ - sum;
|
return size_ - sum;
|
||||||
@ -138,7 +139,7 @@ HistogramSet::maxCount (unsigned idx) const
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
HistogramSet::clearAfter (unsigned idx)
|
HistogramSet::clearAfter (size_t idx)
|
||||||
{
|
{
|
||||||
std::fill (hist_.begin() + idx + 1, hist_.end(), 0);
|
std::fill (hist_.begin() + idx + 1, hist_.end(), 0);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ class HistogramSet
|
|||||||
|
|
||||||
void nextHistogram (void);
|
void nextHistogram (void);
|
||||||
|
|
||||||
unsigned operator[] (unsigned idx) const;
|
unsigned operator[] (size_t idx) const;
|
||||||
|
|
||||||
unsigned nrHistograms (void) const;
|
unsigned nrHistograms (void) const;
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ class HistogramSet
|
|||||||
|
|
||||||
static unsigned nrHistograms (unsigned, unsigned);
|
static unsigned nrHistograms (unsigned, unsigned);
|
||||||
|
|
||||||
static unsigned findIndex (
|
static size_t findIndex (
|
||||||
const Histogram&, const vector<Histogram>&);
|
const Histogram&, const vector<Histogram>&);
|
||||||
|
|
||||||
static vector<double> getNumAssigns (unsigned, unsigned);
|
static vector<double> getNumAssigns (unsigned, unsigned);
|
||||||
@ -33,9 +33,9 @@ class HistogramSet
|
|||||||
friend std::ostream& operator<< (ostream &os, const HistogramSet& hs);
|
friend std::ostream& operator<< (ostream &os, const HistogramSet& hs);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned maxCount (unsigned) const;
|
unsigned maxCount (size_t) const;
|
||||||
|
|
||||||
void clearAfter (unsigned);
|
void clearAfter (size_t);
|
||||||
|
|
||||||
unsigned size_;
|
unsigned size_;
|
||||||
Histogram hist_;
|
Histogram hist_;
|
||||||
|
@ -25,7 +25,6 @@ typedef vector<FacNode*> FacNodes;
|
|||||||
typedef vector<Factor*> Factors;
|
typedef vector<Factor*> Factors;
|
||||||
typedef vector<string> States;
|
typedef vector<string> States;
|
||||||
typedef vector<unsigned> Ranges;
|
typedef vector<unsigned> Ranges;
|
||||||
typedef Params::size_type psize_t;
|
|
||||||
typedef unsigned long long ullong;
|
typedef unsigned long long ullong;
|
||||||
|
|
||||||
|
|
||||||
@ -59,8 +58,6 @@ const int NO_EVIDENCE = -1;
|
|||||||
// number of digits to show when printing a parameter
|
// number of digits to show when printing a parameter
|
||||||
const unsigned PRECISION = 6;
|
const unsigned PRECISION = 6;
|
||||||
|
|
||||||
const bool COLLECT_STATS = false;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ int createLiftedNetwork (void)
|
|||||||
// LiftedUtils::printSymbolDictionary();
|
// LiftedUtils::printSymbolDictionary();
|
||||||
if (Globals::verbosity > 2) {
|
if (Globals::verbosity > 2) {
|
||||||
Util::printHeader ("INITIAL PARFACTORS");
|
Util::printHeader ("INITIAL PARFACTORS");
|
||||||
for (unsigned i = 0; i < parfactors.size(); i++) {
|
for (size_t i = 0; i < parfactors.size(); i++) {
|
||||||
parfactors[i]->print();
|
parfactors[i]->print();
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
@ -197,7 +197,7 @@ void readLiftedEvidence (
|
|||||||
}
|
}
|
||||||
unsigned evidence = (unsigned) YAP_IntOfTerm (YAP_ArgOfTerm (2, pair));
|
unsigned evidence = (unsigned) YAP_IntOfTerm (YAP_ArgOfTerm (2, pair));
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (unsigned i = 0; i < obsFormulas.size(); i++) {
|
for (size_t i = 0; i < obsFormulas.size(); i++) {
|
||||||
if (obsFormulas[i].functor() == functor &&
|
if (obsFormulas[i].functor() == functor &&
|
||||||
obsFormulas[i].arity() == args.size() &&
|
obsFormulas[i].arity() == args.size() &&
|
||||||
obsFormulas[i].evidence() == evidence) {
|
obsFormulas[i].evidence() == evidence) {
|
||||||
@ -268,7 +268,7 @@ readParameters (YAP_Term paramL)
|
|||||||
paramL = YAP_TailOfTerm (paramL);
|
paramL = YAP_TailOfTerm (paramL);
|
||||||
}
|
}
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::toLog (params);
|
Util::log (params);
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
@ -322,10 +322,10 @@ runLiftedSolver (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
YAP_Term list = YAP_TermNil();
|
YAP_Term list = YAP_TermNil();
|
||||||
for (int i = results.size() - 1; i >= 0; i--) {
|
for (size_t i = results.size(); i-- > 0; ) {
|
||||||
const Params& beliefs = results[i];
|
const Params& beliefs = results[i];
|
||||||
YAP_Term queryBeliefsL = YAP_TermNil();
|
YAP_Term queryBeliefsL = YAP_TermNil();
|
||||||
for (int j = beliefs.size() - 1; j >= 0; j--) {
|
for (size_t j = beliefs.size(); j-- > 0; ) {
|
||||||
YAP_Int sl1 = YAP_InitSlot (list);
|
YAP_Int sl1 = YAP_InitSlot (list);
|
||||||
YAP_Term belief = YAP_MkFloatTerm (beliefs[j]);
|
YAP_Term belief = YAP_MkFloatTerm (beliefs[j]);
|
||||||
queryBeliefsL = YAP_MkPairTerm (belief, queryBeliefsL);
|
queryBeliefsL = YAP_MkPairTerm (belief, queryBeliefsL);
|
||||||
@ -359,10 +359,10 @@ runGroundSolver (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
YAP_Term list = YAP_TermNil();
|
YAP_Term list = YAP_TermNil();
|
||||||
for (int i = results.size() - 1; i >= 0; i--) {
|
for (size_t i = results.size(); i-- > 0; ) {
|
||||||
const Params& beliefs = results[i];
|
const Params& beliefs = results[i];
|
||||||
YAP_Term queryBeliefsL = YAP_TermNil();
|
YAP_Term queryBeliefsL = YAP_TermNil();
|
||||||
for (int j = beliefs.size() - 1; j >= 0; j--) {
|
for (size_t j = beliefs.size(); j-- > 0; ) {
|
||||||
YAP_Int sl1 = YAP_InitSlot (list);
|
YAP_Int sl1 = YAP_InitSlot (list);
|
||||||
YAP_Term belief = YAP_MkFloatTerm (beliefs[j]);
|
YAP_Term belief = YAP_MkFloatTerm (beliefs[j]);
|
||||||
queryBeliefsL = YAP_MkPairTerm (belief, queryBeliefsL);
|
queryBeliefsL = YAP_MkPairTerm (belief, queryBeliefsL);
|
||||||
@ -382,7 +382,7 @@ void runVeSolver (
|
|||||||
vector<Params>& results)
|
vector<Params>& results)
|
||||||
{
|
{
|
||||||
results.reserve (tasks.size());
|
results.reserve (tasks.size());
|
||||||
for (unsigned i = 0; i < tasks.size(); i++) {
|
for (size_t i = 0; i < tasks.size(); i++) {
|
||||||
FactorGraph* mfg = fg;
|
FactorGraph* mfg = fg;
|
||||||
if (fg->isFromBayesNetwork()) {
|
if (fg->isFromBayesNetwork()) {
|
||||||
// mfg = BayesBall::getMinimalFactorGraph (*fg, tasks[i]);
|
// mfg = BayesBall::getMinimalFactorGraph (*fg, tasks[i]);
|
||||||
@ -408,7 +408,7 @@ void runBpSolver (
|
|||||||
vector<Params>& results)
|
vector<Params>& results)
|
||||||
{
|
{
|
||||||
std::set<VarId> vids;
|
std::set<VarId> vids;
|
||||||
for (unsigned i = 0; i < tasks.size(); i++) {
|
for (size_t i = 0; i < tasks.size(); i++) {
|
||||||
Util::addToSet (vids, tasks[i]);
|
Util::addToSet (vids, tasks[i]);
|
||||||
}
|
}
|
||||||
Solver* solver = 0;
|
Solver* solver = 0;
|
||||||
@ -431,7 +431,7 @@ void runBpSolver (
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
results.reserve (tasks.size());
|
results.reserve (tasks.size());
|
||||||
for (unsigned i = 0; i < tasks.size(); i++) {
|
for (size_t i = 0; i < tasks.size(); i++) {
|
||||||
results.push_back (solver->solveQuery (tasks[i]));
|
results.push_back (solver->solveQuery (tasks[i]));
|
||||||
}
|
}
|
||||||
if (fg->isFromBayesNetwork()) {
|
if (fg->isFromBayesNetwork()) {
|
||||||
@ -482,7 +482,7 @@ setFactorsParams (void)
|
|||||||
distList = YAP_TailOfTerm (distList);
|
distList = YAP_TailOfTerm (distList);
|
||||||
}
|
}
|
||||||
const FacNodes& facNodes = fg->facNodes();
|
const FacNodes& facNodes = fg->facNodes();
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
unsigned distId = facNodes[i]->factor().distId();
|
unsigned distId = facNodes[i]->factor().distId();
|
||||||
assert (Util::contains (paramsMap, distId));
|
assert (Util::contains (paramsMap, distId));
|
||||||
facNodes[i]->factor().setParams (paramsMap[distId]);
|
facNodes[i]->factor().setParams (paramsMap[distId]);
|
||||||
@ -572,7 +572,7 @@ extern "C" void
|
|||||||
init_predicates (void)
|
init_predicates (void)
|
||||||
{
|
{
|
||||||
YAP_UserCPredicate ("cpp_create_lifted_network", createLiftedNetwork, 3);
|
YAP_UserCPredicate ("cpp_create_lifted_network", createLiftedNetwork, 3);
|
||||||
YAP_UserCPredicate ("cpp_create_ground_network", createGroundNetwork, 4);
|
YAP_UserCPredicate ("cpp_create_ground_network", createGroundNetwork, 4);
|
||||||
YAP_UserCPredicate ("cpp_run_lifted_solver", runLiftedSolver, 3);
|
YAP_UserCPredicate ("cpp_run_lifted_solver", runLiftedSolver, 3);
|
||||||
YAP_UserCPredicate ("cpp_run_ground_solver", runGroundSolver, 3);
|
YAP_UserCPredicate ("cpp_run_ground_solver", runGroundSolver, 3);
|
||||||
YAP_UserCPredicate ("cpp_set_parfactors_params", setParfactorsParams, 2);
|
YAP_UserCPredicate ("cpp_set_parfactors_params", setParfactorsParams, 2);
|
||||||
|
@ -1,46 +1,23 @@
|
|||||||
#ifndef HORUS_STATESINDEXER_H
|
#ifndef HORUS_INDEXER_H
|
||||||
#define HORUS_STATESINDEXER_H
|
#define HORUS_INDEXER_H
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <numeric>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
#include "Var.h"
|
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
|
|
||||||
|
class Indexer
|
||||||
class StatesIndexer
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Indexer (const Ranges& ranges, bool calcOffsets = true)
|
||||||
StatesIndexer (const Ranges& ranges, bool calcOffsets = true)
|
: index_(0), indices_(ranges.size(), 0), ranges_(ranges)
|
||||||
{
|
{
|
||||||
size_ = 1;
|
size_ = std::accumulate (ranges.begin(), ranges.end(), 1,
|
||||||
indices_.resize (ranges.size(), 0);
|
std::multiplies<unsigned>());
|
||||||
ranges_ = ranges;
|
|
||||||
for (unsigned i = 0; i < ranges.size(); i++) {
|
|
||||||
size_ *= ranges[i];
|
|
||||||
}
|
|
||||||
li_ = 0;
|
|
||||||
if (calcOffsets) {
|
|
||||||
calculateOffsets();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
StatesIndexer (const Vars& vars, bool calcOffsets = true)
|
|
||||||
{
|
|
||||||
size_ = 1;
|
|
||||||
indices_.resize (vars.size(), 0);
|
|
||||||
ranges_.reserve (vars.size());
|
|
||||||
for (unsigned i = 0; i < vars.size(); i++) {
|
|
||||||
ranges_.push_back (vars[i]->range());
|
|
||||||
size_ *= vars[i]->range();
|
|
||||||
}
|
|
||||||
li_ = 0;
|
|
||||||
if (calcOffsets) {
|
if (calcOffsets) {
|
||||||
calculateOffsets();
|
calculateOffsets();
|
||||||
}
|
}
|
||||||
@ -48,7 +25,7 @@ class StatesIndexer
|
|||||||
|
|
||||||
void increment (void)
|
void increment (void)
|
||||||
{
|
{
|
||||||
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||||
indices_[i] ++;
|
indices_[i] ++;
|
||||||
if (indices_[i] != ranges_[i]) {
|
if (indices_[i] != ranges_[i]) {
|
||||||
break;
|
break;
|
||||||
@ -56,58 +33,48 @@ class StatesIndexer
|
|||||||
indices_[i] = 0;
|
indices_[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
li_ ++;
|
index_ ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void increment (unsigned dim)
|
void incrementDimension (size_t dim)
|
||||||
{
|
{
|
||||||
assert (dim < ranges_.size());
|
assert (dim < ranges_.size());
|
||||||
assert (ranges_.size() == offsets_.size());
|
assert (ranges_.size() == offsets_.size());
|
||||||
assert (indices_[dim] < ranges_[dim]);
|
assert (indices_[dim] < ranges_[dim]);
|
||||||
indices_[dim] ++;
|
indices_[dim] ++;
|
||||||
li_ += offsets_[dim];
|
index_ += offsets_[dim];
|
||||||
}
|
}
|
||||||
|
|
||||||
void incrementExcluding (unsigned skipDim)
|
void incrementExceptDimension (size_t dim)
|
||||||
{
|
{
|
||||||
assert (ranges_.size() == offsets_.size());
|
assert (ranges_.size() == offsets_.size());
|
||||||
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||||
if (i != (int)skipDim) {
|
if (i != dim) {
|
||||||
indices_[i] ++;
|
indices_[i] ++;
|
||||||
li_ += offsets_[i];
|
index_ += offsets_[i];
|
||||||
if (indices_[i] != ranges_[i]) {
|
if (indices_[i] != ranges_[i]) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
indices_[i] = 0;
|
indices_[i] = 0;
|
||||||
li_ -= offsets_[i] * ranges_[i];
|
index_ -= offsets_[i] * ranges_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
li_ = size_;
|
index_ = size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned linearIndex (void) const
|
Indexer& operator++ (void)
|
||||||
{
|
|
||||||
return li_;
|
|
||||||
}
|
|
||||||
|
|
||||||
const vector<unsigned>& indices (void) const
|
|
||||||
{
|
|
||||||
return indices_;
|
|
||||||
}
|
|
||||||
|
|
||||||
StatesIndexer& operator ++ (void)
|
|
||||||
{
|
{
|
||||||
increment();
|
increment();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator unsigned (void) const
|
operator size_t (void) const
|
||||||
{
|
{
|
||||||
return li_;
|
return index_;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned operator[] (unsigned dim) const
|
unsigned operator[] (size_t dim) const
|
||||||
{
|
{
|
||||||
assert (valid());
|
assert (valid());
|
||||||
assert (dim < ranges_.size());
|
assert (dim < ranges_.size());
|
||||||
@ -116,125 +83,118 @@ class StatesIndexer
|
|||||||
|
|
||||||
bool valid (void) const
|
bool valid (void) const
|
||||||
{
|
{
|
||||||
return li_ < size_;
|
return index_ < size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset (void)
|
void reset (void)
|
||||||
{
|
{
|
||||||
std::fill (indices_.begin(), indices_.end(), 0);
|
std::fill (indices_.begin(), indices_.end(), 0);
|
||||||
li_ = 0;
|
index_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset (unsigned dim)
|
void resetDimension (size_t dim)
|
||||||
{
|
{
|
||||||
indices_[dim] = 0;
|
indices_[dim] = 0;
|
||||||
li_ -= offsets_[dim] * ranges_[dim];
|
index_ -= offsets_[dim] * ranges_[dim];
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned size (void) const
|
size_t size (void) const
|
||||||
{
|
{
|
||||||
return size_ ;
|
return size_ ;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend ostream& operator<< (ostream &os, const StatesIndexer& idx)
|
friend std::ostream& operator<< (std::ostream&, const Indexer&);
|
||||||
{
|
|
||||||
os << "(" << std::setw (2) << std::setfill('0') << idx.li_ << ") " ;
|
|
||||||
os << idx.indices_;
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void calculateOffsets (void)
|
void calculateOffsets (void)
|
||||||
{
|
{
|
||||||
unsigned prod = 1;
|
size_t prod = 1;
|
||||||
offsets_.resize (ranges_.size());
|
offsets_.resize (ranges_.size());
|
||||||
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||||
offsets_[i] = prod;
|
offsets_[i] = prod;
|
||||||
prod *= ranges_[i];
|
prod *= ranges_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned li_;
|
size_t index_;
|
||||||
unsigned size_;
|
Ranges indices_;
|
||||||
vector<unsigned> indices_;
|
const Ranges& ranges_;
|
||||||
vector<unsigned> ranges_;
|
size_t size_;
|
||||||
vector<unsigned> offsets_;
|
vector<size_t> offsets_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
inline std::ostream&
|
||||||
|
operator<< (std::ostream& os, const Indexer& indexer)
|
||||||
|
{
|
||||||
|
os << "(" ;
|
||||||
|
os << std::setw (2) << std::setfill('0') << indexer.index_;
|
||||||
|
os << ") " ;
|
||||||
|
os << indexer.indices_;
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MapIndexer
|
class MapIndexer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MapIndexer (const Ranges& ranges, const vector<bool>& mapDims)
|
MapIndexer (const Ranges& ranges, const vector<bool>& mask)
|
||||||
|
: index_(0), indices_(ranges.size(), 0), ranges_(ranges),
|
||||||
|
valid_(true)
|
||||||
{
|
{
|
||||||
assert (ranges.size() == mapDims.size());
|
size_t prod = 1;
|
||||||
unsigned prod = 1;
|
offsets_.resize (ranges.size(), 0);
|
||||||
offsets_.resize (ranges.size());
|
for (size_t i = ranges.size(); i-- > 0; ) {
|
||||||
for (int i = ranges.size() - 1; i >= 0; i--) {
|
if (mask[i]) {
|
||||||
if (mapDims[i]) {
|
|
||||||
offsets_[i] = prod;
|
offsets_[i] = prod;
|
||||||
prod *= ranges[i];
|
prod *= ranges[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
indices_.resize (ranges.size(), 0);
|
assert (ranges.size() == mask.size());
|
||||||
ranges_ = ranges;
|
|
||||||
index_ = 0;
|
|
||||||
valid_ = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MapIndexer (const Ranges& ranges, unsigned ignoreDim)
|
MapIndexer (const Ranges& ranges, size_t dim)
|
||||||
|
: index_(0), indices_(ranges.size(), 0), ranges_(ranges),
|
||||||
|
valid_(true)
|
||||||
{
|
{
|
||||||
unsigned prod = 1;
|
size_t prod = 1;
|
||||||
offsets_.resize (ranges.size());
|
offsets_.resize (ranges.size(), 0);
|
||||||
for (int i = ranges.size() - 1; i >= 0; i--) {
|
for (size_t i = ranges.size(); i-- > 0; ) {
|
||||||
if (i != (int)ignoreDim) {
|
if (i != dim) {
|
||||||
offsets_[i] = prod;
|
offsets_[i] = prod;
|
||||||
prod *= ranges[i];
|
prod *= ranges[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
indices_.resize (ranges.size(), 0);
|
|
||||||
ranges_ = ranges;
|
|
||||||
index_ = 0;
|
|
||||||
valid_ = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
template <typename T>
|
||||||
MapIndexer (
|
MapIndexer (
|
||||||
const VarIds& loopVids,
|
const vector<T>& allArgs,
|
||||||
const Ranges& loopRanges,
|
const Ranges& allRanges,
|
||||||
const VarIds& mapVids,
|
const vector<T>& wantedArgs,
|
||||||
const Ranges& mapRanges)
|
const Ranges& wantedRanges)
|
||||||
|
: index_(0), indices_(allArgs.size(), 0), ranges_(allRanges),
|
||||||
|
valid_(true)
|
||||||
{
|
{
|
||||||
unsigned prod = 1;
|
size_t prod = 1;
|
||||||
vector<unsigned> offsets (mapRanges.size());
|
vector<size_t> offsets (wantedRanges.size());
|
||||||
for (int i = mapRanges.size() - 1; i >= 0; i--) {
|
for (size_t i = wantedRanges.size(); i-- > 0; ) {
|
||||||
offsets[i] = prod;
|
offsets[i] = prod;
|
||||||
prod *= mapRanges[i];
|
prod *= wantedRanges[i];
|
||||||
}
|
}
|
||||||
|
offsets_.reserve (allArgs.size());
|
||||||
offsets_.reserve (loopVids.size());
|
for (size_t i = 0; i < allArgs.size(); i++) {
|
||||||
for (unsigned i = 0; i < loopVids.size(); i++) {
|
size_t idx = Util::indexOf (wantedArgs, allArgs[i]);
|
||||||
VarIds::const_iterator it =
|
offsets_.push_back (idx != wantedArgs.size() ? offsets[idx] : 0);
|
||||||
std::find (mapVids.begin(), mapVids.end(), loopVids[i]);
|
|
||||||
if (it != mapVids.end()) {
|
|
||||||
offsets_.push_back (offsets[it - mapVids.begin()]);
|
|
||||||
} else {
|
|
||||||
offsets_.push_back (0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
indices_.resize (loopVids.size(), 0);
|
|
||||||
ranges_ = loopRanges;
|
|
||||||
index_ = 0;
|
|
||||||
size_ = prod;
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
MapIndexer& operator ++ (void)
|
MapIndexer& operator++ (void)
|
||||||
{
|
{
|
||||||
assert (valid_);
|
assert (valid_);
|
||||||
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||||
indices_[i] ++;
|
indices_[i] ++;
|
||||||
index_ += offsets_[i];
|
index_ += offsets_[i];
|
||||||
if (indices_[i] != ranges_[i]) {
|
if (indices_[i] != ranges_[i]) {
|
||||||
@ -248,17 +208,13 @@ class MapIndexer
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned mappedIndex (void) const
|
operator size_t (void) const
|
||||||
{
|
{
|
||||||
|
assert (valid());
|
||||||
return index_;
|
return index_;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator unsigned (void) const
|
unsigned operator[] (size_t dim) const
|
||||||
{
|
|
||||||
return index_;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned operator[] (unsigned dim) const
|
|
||||||
{
|
{
|
||||||
assert (valid());
|
assert (valid());
|
||||||
assert (dim < ranges_.size());
|
assert (dim < ranges_.size());
|
||||||
@ -276,21 +232,28 @@ class MapIndexer
|
|||||||
index_ = 0;
|
index_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend ostream& operator<< (ostream &os, const MapIndexer& idx)
|
friend std::ostream& operator<< (std::ostream&, const MapIndexer&);
|
||||||
{
|
|
||||||
os << "(" << std::setw (2) << std::setfill('0') << idx.index_ << ") " ;
|
|
||||||
os << idx.indices_;
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned index_;
|
size_t index_;
|
||||||
bool valid_;
|
Ranges indices_;
|
||||||
vector<unsigned> ranges_;
|
const Ranges& ranges_;
|
||||||
vector<unsigned> indices_;
|
bool valid_;
|
||||||
vector<unsigned> offsets_;
|
vector<size_t> offsets_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // HORUS_STATESINDEXER_H
|
|
||||||
|
inline std::ostream&
|
||||||
|
operator<< (std::ostream &os, const MapIndexer& indexer)
|
||||||
|
{
|
||||||
|
os << "(" ;
|
||||||
|
os << std::setw (2) << std::setfill('0') << indexer.index_;
|
||||||
|
os << ") " ;
|
||||||
|
os << indexer.indices_;
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // HORUS_INDEXER_H
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ printSymbolDictionary (void)
|
|||||||
= symbolDict.begin();
|
= symbolDict.begin();
|
||||||
while (it != symbolDict.end()) {
|
while (it != symbolDict.end()) {
|
||||||
cout << it->first << " -> " << it->second << endl;
|
cout << it->first << " -> " << it->second << endl;
|
||||||
it ++;
|
++ it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ ostream& operator<< (ostream &os, const Symbol& s)
|
|||||||
unordered_map<string, unsigned>::const_iterator it
|
unordered_map<string, unsigned>::const_iterator it
|
||||||
= LiftedUtils::symbolDict.begin();
|
= LiftedUtils::symbolDict.begin();
|
||||||
while (it != LiftedUtils::symbolDict.end() && it->second != s) {
|
while (it != LiftedUtils::symbolDict.end() && it->second != s) {
|
||||||
it ++;
|
++ it;
|
||||||
}
|
}
|
||||||
assert (it != LiftedUtils::symbolDict.end());
|
assert (it != LiftedUtils::symbolDict.end());
|
||||||
os << it->first;
|
os << it->first;
|
||||||
@ -72,7 +72,7 @@ ostream& operator<< (ostream &os, const LogVar& X)
|
|||||||
ostream& operator<< (ostream &os, const Tuple& t)
|
ostream& operator<< (ostream &os, const Tuple& t)
|
||||||
{
|
{
|
||||||
os << "(" ;
|
os << "(" ;
|
||||||
for (unsigned i = 0; i < t.size(); i++) {
|
for (size_t i = 0; i < t.size(); i++) {
|
||||||
os << ((i != 0) ? "," : "") << t[i];
|
os << ((i != 0) ? "," : "") << t[i];
|
||||||
}
|
}
|
||||||
os << ")" ;
|
os << ")" ;
|
||||||
@ -85,7 +85,7 @@ ostream& operator<< (ostream &os, const Ground& gr)
|
|||||||
{
|
{
|
||||||
os << gr.functor();
|
os << gr.functor();
|
||||||
os << "(" ;
|
os << "(" ;
|
||||||
for (unsigned i = 0; i < gr.args().size(); i++) {
|
for (size_t i = 0; i < gr.args().size(); i++) {
|
||||||
if (i != 0) os << ", " ;
|
if (i != 0) os << ", " ;
|
||||||
os << gr.args()[i];
|
os << gr.args()[i];
|
||||||
}
|
}
|
||||||
@ -108,7 +108,7 @@ Substitution::getDiscardedLogVars (void) const
|
|||||||
} else {
|
} else {
|
||||||
doneLvs.insert (it->second);
|
doneLvs.insert (it->second);
|
||||||
}
|
}
|
||||||
it ++;
|
++ it;
|
||||||
}
|
}
|
||||||
return discardedLvs;
|
return discardedLvs;
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ class Ground
|
|||||||
|
|
||||||
Symbols args (void) const { return args_; }
|
Symbols args (void) const { return args_; }
|
||||||
|
|
||||||
unsigned arity (void) const { return args_.size(); }
|
size_t arity (void) const { return args_.size(); }
|
||||||
|
|
||||||
bool isAtom (void) const { return args_.size() == 0; }
|
bool isAtom (void) const { return args_.size() == 0; }
|
||||||
|
|
||||||
@ -150,7 +150,7 @@ class Substitution
|
|||||||
return Util::contains (subs_, X);
|
return Util::contains (subs_, X);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned nrReplacements (void) const { return subs_.size(); }
|
size_t nrReplacements (void) const { return subs_.size(); }
|
||||||
|
|
||||||
LogVars getDiscardedLogVars (void) const;
|
LogVars getDiscardedLogVars (void) const;
|
||||||
|
|
||||||
|
@ -17,17 +17,17 @@ Parfactor::Parfactor (
|
|||||||
distId_ = distId;
|
distId_ = distId;
|
||||||
|
|
||||||
LogVars logVars;
|
LogVars logVars;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
ranges_.push_back (args_[i].range());
|
ranges_.push_back (args_[i].range());
|
||||||
const LogVars& lvs = args_[i].logVars();
|
const LogVars& lvs = args_[i].logVars();
|
||||||
for (unsigned j = 0; j < lvs.size(); j++) {
|
for (size_t j = 0; j < lvs.size(); j++) {
|
||||||
if (Util::contains (logVars, lvs[j]) == false) {
|
if (Util::contains (logVars, lvs[j]) == false) {
|
||||||
logVars.push_back (lvs[j]);
|
logVars.push_back (lvs[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
constr_ = new ConstraintTree (logVars, tuples);
|
constr_ = new ConstraintTree (logVars, tuples);
|
||||||
assert (params_.size() == Util::expectedSize (ranges_));
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ Parfactor::Parfactor (const Parfactor* g, const Tuple& tuple)
|
|||||||
ranges_ = g->ranges();
|
ranges_ = g->ranges();
|
||||||
distId_ = g->distId();
|
distId_ = g->distId();
|
||||||
constr_ = new ConstraintTree (g->logVars(), {tuple});
|
constr_ = new ConstraintTree (g->logVars(), {tuple});
|
||||||
assert (params_.size() == Util::expectedSize (ranges_));
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ Parfactor::Parfactor (const Parfactor* g, ConstraintTree* constr)
|
|||||||
ranges_ = g->ranges();
|
ranges_ = g->ranges();
|
||||||
distId_ = g->distId();
|
distId_ = g->distId();
|
||||||
constr_ = constr;
|
constr_ = constr;
|
||||||
assert (params_.size() == Util::expectedSize (ranges_));
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ Parfactor::Parfactor (const Parfactor& g)
|
|||||||
ranges_ = g.ranges();
|
ranges_ = g.ranges();
|
||||||
distId_ = g.distId();
|
distId_ = g.distId();
|
||||||
constr_ = new ConstraintTree (*g.constr());
|
constr_ = new ConstraintTree (*g.constr());
|
||||||
assert (params_.size() == Util::expectedSize (ranges_));
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ LogVarSet
|
|||||||
Parfactor::countedLogVars (void) const
|
Parfactor::countedLogVars (void) const
|
||||||
{
|
{
|
||||||
LogVarSet set;
|
LogVarSet set;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (args_[i].isCounting()) {
|
if (args_[i].isCounting()) {
|
||||||
set.insert (args_[i].countedLogVar());
|
set.insert (args_[i].countedLogVar());
|
||||||
}
|
}
|
||||||
@ -109,11 +109,11 @@ Parfactor::elimLogVars (void) const
|
|||||||
|
|
||||||
|
|
||||||
LogVarSet
|
LogVarSet
|
||||||
Parfactor::exclusiveLogVars (unsigned fIdx) const
|
Parfactor::exclusiveLogVars (size_t fIdx) const
|
||||||
{
|
{
|
||||||
assert (fIdx < args_.size());
|
assert (fIdx < args_.size());
|
||||||
LogVarSet remaining;
|
LogVarSet remaining;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (i != fIdx) {
|
if (i != fIdx) {
|
||||||
remaining |= args_[i].logVarSet();
|
remaining |= args_[i].logVarSet();
|
||||||
}
|
}
|
||||||
@ -124,16 +124,7 @@ Parfactor::exclusiveLogVars (unsigned fIdx) const
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Parfactor::setConstraintTree (ConstraintTree* newTree)
|
Parfactor::sumOutIndex (size_t fIdx)
|
||||||
{
|
|
||||||
delete constr_;
|
|
||||||
constr_ = newTree;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Parfactor::sumOut (unsigned fIdx)
|
|
||||||
{
|
{
|
||||||
assert (fIdx < args_.size());
|
assert (fIdx < args_.size());
|
||||||
assert (args_[fIdx].contains (elimLogVars()));
|
assert (args_[fIdx].contains (elimLogVars()));
|
||||||
@ -143,46 +134,29 @@ Parfactor::sumOut (unsigned fIdx)
|
|||||||
args_[fIdx].countedLogVar());
|
args_[fIdx].countedLogVar());
|
||||||
unsigned R = args_[fIdx].range();
|
unsigned R = args_[fIdx].range();
|
||||||
vector<double> numAssigns = HistogramSet::getNumAssigns (N, R);
|
vector<double> numAssigns = HistogramSet::getNumAssigns (N, R);
|
||||||
StatesIndexer sindexer (ranges_, fIdx);
|
Indexer indexer (ranges_, fIdx);
|
||||||
while (sindexer.valid()) {
|
while (indexer.valid()) {
|
||||||
unsigned h = sindexer[fIdx];
|
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
params_[sindexer] += numAssigns[h];
|
params_[indexer] += numAssigns[ indexer[fIdx] ];
|
||||||
} else {
|
} else {
|
||||||
params_[sindexer] *= numAssigns[h];
|
params_[indexer] *= numAssigns[ indexer[fIdx] ];
|
||||||
}
|
}
|
||||||
++ sindexer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Params copy = params_;
|
|
||||||
params_.clear();
|
|
||||||
params_.resize (copy.size() / ranges_[fIdx], LogAware::addIdenty());
|
|
||||||
MapIndexer indexer (ranges_, fIdx);
|
|
||||||
if (Globals::logDomain) {
|
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
|
||||||
params_[indexer] = Util::logSum (params_[indexer], copy[i]);
|
|
||||||
++ indexer;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
|
||||||
params_[indexer] += copy[i];
|
|
||||||
++ indexer;
|
++ indexer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LogVarSet excl = exclusiveLogVars (fIdx);
|
LogVarSet excl = exclusiveLogVars (fIdx);
|
||||||
|
unsigned exp;
|
||||||
if (args_[fIdx].isCounting()) {
|
if (args_[fIdx].isCounting()) {
|
||||||
// counting log vars were already raised on counting conversion
|
// counting log vars were already raised on counting conversion
|
||||||
LogAware::pow (params_, constr_->getConditionalCount (
|
exp = constr_->getConditionalCount (excl - args_[fIdx].countedLogVar());
|
||||||
excl - args_[fIdx].countedLogVar()));
|
|
||||||
} else {
|
} else {
|
||||||
LogAware::pow (params_, constr_->getConditionalCount (excl));
|
exp = constr_->getConditionalCount (excl);
|
||||||
}
|
}
|
||||||
constr_->remove (excl);
|
constr_->remove (excl);
|
||||||
|
|
||||||
args_.erase (args_.begin() + fIdx);
|
TFactor<ProbFormula>::sumOutIndex (fIdx);
|
||||||
ranges_.erase (ranges_.begin() + fIdx);
|
LogAware::pow (params_, exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -205,7 +179,7 @@ Parfactor::canCountConvert (LogVar X)
|
|||||||
if (nrFormulas (X) != 1) {
|
if (nrFormulas (X) != 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int fIdx = indexOfLogVar (X);
|
size_t fIdx = indexOfLogVar (X);
|
||||||
if (args_[fIdx].isCounting()) {
|
if (args_[fIdx].isCounting()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -226,7 +200,7 @@ Parfactor::canCountConvert (LogVar X)
|
|||||||
void
|
void
|
||||||
Parfactor::countConvert (LogVar X)
|
Parfactor::countConvert (LogVar X)
|
||||||
{
|
{
|
||||||
int fIdx = indexOfLogVar (X);
|
size_t fIdx = indexOfLogVar (X);
|
||||||
assert (constr_->isCountNormalized (X));
|
assert (constr_->isCountNormalized (X));
|
||||||
assert (constr_->getConditionalCount (X) > 1);
|
assert (constr_->getConditionalCount (X) > 1);
|
||||||
assert (canCountConvert (X));
|
assert (canCountConvert (X));
|
||||||
@ -236,18 +210,18 @@ Parfactor::countConvert (LogVar X)
|
|||||||
unsigned H = HistogramSet::nrHistograms (N, R);
|
unsigned H = HistogramSet::nrHistograms (N, R);
|
||||||
vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
||||||
|
|
||||||
StatesIndexer indexer (ranges_);
|
Indexer indexer (ranges_);
|
||||||
vector<Params> sumout (params_.size() / R);
|
vector<Params> sumout (params_.size() / R);
|
||||||
unsigned count = 0;
|
unsigned count = 0;
|
||||||
while (indexer.valid()) {
|
while (indexer.valid()) {
|
||||||
sumout[count].reserve (R);
|
sumout[count].reserve (R);
|
||||||
for (unsigned r = 0; r < R; r++) {
|
for (unsigned r = 0; r < R; r++) {
|
||||||
sumout[count].push_back (params_[indexer]);
|
sumout[count].push_back (params_[indexer]);
|
||||||
indexer.increment (fIdx);
|
indexer.incrementDimension (fIdx);
|
||||||
}
|
}
|
||||||
count ++;
|
count ++;
|
||||||
indexer.reset (fIdx);
|
indexer.resetDimension (fIdx);
|
||||||
indexer.incrementExcluding (fIdx);
|
indexer.incrementExceptDimension (fIdx);
|
||||||
}
|
}
|
||||||
|
|
||||||
params_.clear();
|
params_.clear();
|
||||||
@ -257,7 +231,7 @@ Parfactor::countConvert (LogVar X)
|
|||||||
MapIndexer mapIndexer (ranges_, fIdx);
|
MapIndexer mapIndexer (ranges_, fIdx);
|
||||||
while (mapIndexer.valid()) {
|
while (mapIndexer.valid()) {
|
||||||
double prod = LogAware::multIdenty();
|
double prod = LogAware::multIdenty();
|
||||||
unsigned i = mapIndexer.mappedIndex();
|
size_t i = mapIndexer;
|
||||||
unsigned h = mapIndexer[fIdx];
|
unsigned h = mapIndexer[fIdx];
|
||||||
for (unsigned r = 0; r < R; r++) {
|
for (unsigned r = 0; r < R; r++) {
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
@ -278,8 +252,8 @@ Parfactor::countConvert (LogVar X)
|
|||||||
void
|
void
|
||||||
Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
|
Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
|
||||||
{
|
{
|
||||||
int fIdx = indexOfLogVar (X);
|
size_t fIdx = indexOfLogVar (X);
|
||||||
assert (fIdx != -1);
|
assert (fIdx != args_.size());
|
||||||
assert (args_[fIdx].isCounting());
|
assert (args_[fIdx].isCounting());
|
||||||
|
|
||||||
unsigned N1 = constr_->getConditionalCount (X_new1);
|
unsigned N1 = constr_->getConditionalCount (X_new1);
|
||||||
@ -298,11 +272,7 @@ Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
|
|||||||
for (unsigned i = 0; i < H1; i++) {
|
for (unsigned i = 0; i < H1; i++) {
|
||||||
for (unsigned j = 0; j < H2; j++) {
|
for (unsigned j = 0; j < H2; j++) {
|
||||||
Histogram hist = histograms1[i];
|
Histogram hist = histograms1[i];
|
||||||
std::transform (
|
hist += histograms2[j];
|
||||||
hist.begin(), hist.end(),
|
|
||||||
histograms2[j].begin(),
|
|
||||||
hist.begin(),
|
|
||||||
plus<int>());
|
|
||||||
sumIndexes.push_back (HistogramSet::findIndex (hist, histograms));
|
sumIndexes.push_back (HistogramSet::findIndex (hist, histograms));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -327,8 +297,8 @@ Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
|
|||||||
void
|
void
|
||||||
Parfactor::fullExpand (LogVar X)
|
Parfactor::fullExpand (LogVar X)
|
||||||
{
|
{
|
||||||
int fIdx = indexOfLogVar (X);
|
size_t fIdx = indexOfLogVar (X);
|
||||||
assert (fIdx != -1);
|
assert (fIdx != args_.size());
|
||||||
assert (args_[fIdx].isCounting());
|
assert (args_[fIdx].isCounting());
|
||||||
|
|
||||||
unsigned N = constr_->getConditionalCount (X);
|
unsigned N = constr_->getConditionalCount (X);
|
||||||
@ -340,15 +310,11 @@ Parfactor::fullExpand (LogVar X)
|
|||||||
sumIndexes.reserve (N * R);
|
sumIndexes.reserve (N * R);
|
||||||
|
|
||||||
Ranges expandRanges (N, R);
|
Ranges expandRanges (N, R);
|
||||||
StatesIndexer indexer (expandRanges);
|
Indexer indexer (expandRanges);
|
||||||
while (indexer.valid()) {
|
while (indexer.valid()) {
|
||||||
vector<unsigned> hist (R, 0);
|
vector<unsigned> hist (R, 0);
|
||||||
for (unsigned n = 0; n < N; n++) {
|
for (unsigned n = 0; n < N; n++) {
|
||||||
std::transform (
|
hist += expandHists[indexer[n]];
|
||||||
hist.begin(), hist.end(),
|
|
||||||
expandHists[indexer[n]].begin(),
|
|
||||||
hist.begin(),
|
|
||||||
plus<int>());
|
|
||||||
}
|
}
|
||||||
sumIndexes.push_back (HistogramSet::findIndex (hist, originHists));
|
sumIndexes.push_back (HistogramSet::findIndex (hist, originHists));
|
||||||
++ indexer;
|
++ indexer;
|
||||||
@ -375,8 +341,8 @@ void
|
|||||||
Parfactor::reorderAccordingGrounds (const Grounds& grounds)
|
Parfactor::reorderAccordingGrounds (const Grounds& grounds)
|
||||||
{
|
{
|
||||||
ProbFormulas newFormulas;
|
ProbFormulas newFormulas;
|
||||||
for (unsigned i = 0; i < grounds.size(); i++) {
|
for (size_t i = 0; i < grounds.size(); i++) {
|
||||||
for (unsigned j = 0; j < args_.size(); j++) {
|
for (size_t j = 0; j < args_.size(); j++) {
|
||||||
if (grounds[i].functor() == args_[j].functor() &&
|
if (grounds[i].functor() == args_[j].functor() &&
|
||||||
grounds[i].arity() == args_[j].arity()) {
|
grounds[i].arity() == args_[j].arity()) {
|
||||||
constr_->moveToTop (args_[j].logVars());
|
constr_->moveToTop (args_[j].logVars());
|
||||||
@ -396,8 +362,8 @@ Parfactor::reorderAccordingGrounds (const Grounds& grounds)
|
|||||||
void
|
void
|
||||||
Parfactor::absorveEvidence (const ProbFormula& formula, unsigned evidence)
|
Parfactor::absorveEvidence (const ProbFormula& formula, unsigned evidence)
|
||||||
{
|
{
|
||||||
int fIdx = indexOf (formula);
|
size_t fIdx = indexOf (formula);
|
||||||
assert (fIdx != -1);
|
assert (fIdx != args_.size());
|
||||||
LogVarSet excl = exclusiveLogVars (fIdx);
|
LogVarSet excl = exclusiveLogVars (fIdx);
|
||||||
assert (args_[fIdx].isCounting() == false);
|
assert (args_[fIdx].isCounting() == false);
|
||||||
assert (constr_->isCountNormalized (excl));
|
assert (constr_->isCountNormalized (excl));
|
||||||
@ -411,7 +377,7 @@ Parfactor::absorveEvidence (const ProbFormula& formula, unsigned evidence)
|
|||||||
void
|
void
|
||||||
Parfactor::setNewGroups (void)
|
Parfactor::setNewGroups (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
args_[i].setGroup (ProbFormula::getNewGroup());
|
args_[i].setGroup (ProbFormula::getNewGroup());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -421,9 +387,9 @@ Parfactor::setNewGroups (void)
|
|||||||
void
|
void
|
||||||
Parfactor::applySubstitution (const Substitution& theta)
|
Parfactor::applySubstitution (const Substitution& theta)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
LogVars& lvs = args_[i].logVars();
|
LogVars& lvs = args_[i].logVars();
|
||||||
for (unsigned j = 0; j < lvs.size(); j++) {
|
for (size_t j = 0; j < lvs.size(); j++) {
|
||||||
lvs[j] = theta.newNameFor (lvs[j]);
|
lvs[j] = theta.newNameFor (lvs[j]);
|
||||||
}
|
}
|
||||||
if (args_[i].isCounting()) {
|
if (args_[i].isCounting()) {
|
||||||
@ -436,11 +402,11 @@ Parfactor::applySubstitution (const Substitution& theta)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int
|
PrvGroup
|
||||||
Parfactor::findGroup (const Ground& ground) const
|
Parfactor::findGroup (const Ground& ground) const
|
||||||
{
|
{
|
||||||
int group = -1;
|
PrvGroup group = numeric_limits<PrvGroup>::max();
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (args_[i].functor() == ground.functor() &&
|
if (args_[i].functor() == ground.functor() &&
|
||||||
args_[i].arity() == ground.arity()) {
|
args_[i].arity() == ground.arity()) {
|
||||||
constr_->moveToTop (args_[i].logVars());
|
constr_->moveToTop (args_[i].logVars());
|
||||||
@ -458,15 +424,15 @@ Parfactor::findGroup (const Ground& ground) const
|
|||||||
bool
|
bool
|
||||||
Parfactor::containsGround (const Ground& ground) const
|
Parfactor::containsGround (const Ground& ground) const
|
||||||
{
|
{
|
||||||
return findGroup (ground) != -1;
|
return findGroup (ground) != numeric_limits<PrvGroup>::max();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Parfactor::containsGroup (unsigned group) const
|
Parfactor::containsGroup (PrvGroup group) const
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (args_[i].group() == group) {
|
if (args_[i].group() == group) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -480,7 +446,7 @@ unsigned
|
|||||||
Parfactor::nrFormulas (LogVar X) const
|
Parfactor::nrFormulas (LogVar X) const
|
||||||
{
|
{
|
||||||
unsigned count = 0;
|
unsigned count = 0;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (args_[i].contains (X)) {
|
if (args_[i].contains (X)) {
|
||||||
count ++;
|
count ++;
|
||||||
}
|
}
|
||||||
@ -493,9 +459,9 @@ Parfactor::nrFormulas (LogVar X) const
|
|||||||
int
|
int
|
||||||
Parfactor::indexOfLogVar (LogVar X) const
|
Parfactor::indexOfLogVar (LogVar X) const
|
||||||
{
|
{
|
||||||
int idx = -1;
|
size_t idx = args_.size();
|
||||||
assert (nrFormulas (X) == 1);
|
assert (nrFormulas (X) == 1);
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (args_[i].contains (X)) {
|
if (args_[i].contains (X)) {
|
||||||
idx = i;
|
idx = i;
|
||||||
break;
|
break;
|
||||||
@ -507,10 +473,10 @@ Parfactor::indexOfLogVar (LogVar X) const
|
|||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
Parfactor::indexOfGroup (unsigned group) const
|
Parfactor::indexOfGroup (PrvGroup group) const
|
||||||
{
|
{
|
||||||
int pos = -1;
|
size_t pos = args_.size();
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (args_[i].group() == group) {
|
if (args_[i].group() == group) {
|
||||||
pos = i;
|
pos = i;
|
||||||
break;
|
break;
|
||||||
@ -522,10 +488,10 @@ Parfactor::indexOfGroup (unsigned group) const
|
|||||||
|
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
Parfactor::nrFormulasWithGroup (unsigned group) const
|
Parfactor::nrFormulasWithGroup (PrvGroup group) const
|
||||||
{
|
{
|
||||||
unsigned count = 0;
|
unsigned count = 0;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (args_[i].group() == group) {
|
if (args_[i].group() == group) {
|
||||||
count ++;
|
count ++;
|
||||||
}
|
}
|
||||||
@ -535,11 +501,11 @@ Parfactor::nrFormulasWithGroup (unsigned group) const
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
vector<unsigned>
|
vector<PrvGroup>
|
||||||
Parfactor::getAllGroups (void) const
|
Parfactor::getAllGroups (void) const
|
||||||
{
|
{
|
||||||
vector<unsigned> groups (args_.size());
|
vector<PrvGroup> groups (args_.size());
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
groups[i] = args_[i].group();
|
groups[i] = args_[i].group();
|
||||||
}
|
}
|
||||||
return groups;
|
return groups;
|
||||||
@ -552,7 +518,7 @@ Parfactor::getLabel (void) const
|
|||||||
{
|
{
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "phi(" ;
|
ss << "phi(" ;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (i != 0) ss << "," ;
|
if (i != 0) ss << "," ;
|
||||||
ss << args_[i];
|
ss << args_[i];
|
||||||
}
|
}
|
||||||
@ -569,14 +535,14 @@ void
|
|||||||
Parfactor::print (bool printParams) const
|
Parfactor::print (bool printParams) const
|
||||||
{
|
{
|
||||||
cout << "Formulas: " ;
|
cout << "Formulas: " ;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (i != 0) cout << ", " ;
|
if (i != 0) cout << ", " ;
|
||||||
cout << args_[i];
|
cout << args_[i];
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
if (args_[0].group() != Util::maxUnsigned()) {
|
if (args_[0].group() != Util::maxUnsigned()) {
|
||||||
vector<string> groups;
|
vector<string> groups;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
groups.push_back (string ("g") + Util::toString (args_[i].group()));
|
groups.push_back (string ("g") + Util::toString (args_[i].group()));
|
||||||
}
|
}
|
||||||
cout << "Groups: " << groups << endl;
|
cout << "Groups: " << groups << endl;
|
||||||
@ -606,10 +572,10 @@ void
|
|||||||
Parfactor::printParameters (void) const
|
Parfactor::printParameters (void) const
|
||||||
{
|
{
|
||||||
vector<string> jointStrings;
|
vector<string> jointStrings;
|
||||||
StatesIndexer indexer (ranges_);
|
Indexer indexer (ranges_);
|
||||||
while (indexer.valid()) {
|
while (indexer.valid()) {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if (i != 0) ss << ", " ;
|
if (i != 0) ss << ", " ;
|
||||||
if (args_[i].isCounting()) {
|
if (args_[i].isCounting()) {
|
||||||
unsigned N = constr_->getConditionalCount (
|
unsigned N = constr_->getConditionalCount (
|
||||||
@ -628,7 +594,7 @@ Parfactor::printParameters (void) const
|
|||||||
jointStrings.push_back (ss.str());
|
jointStrings.push_back (ss.str());
|
||||||
++ indexer;
|
++ indexer;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < params_.size(); i++) {
|
for (size_t i = 0; i < params_.size(); i++) {
|
||||||
cout << "f(" << jointStrings[i] << ")" ;
|
cout << "f(" << jointStrings[i] << ")" ;
|
||||||
cout << " = " << params_[i] << endl;
|
cout << " = " << params_[i] << endl;
|
||||||
}
|
}
|
||||||
@ -642,7 +608,7 @@ Parfactor::printProjections (void) const
|
|||||||
ConstraintTree copy (*constr_);
|
ConstraintTree copy (*constr_);
|
||||||
|
|
||||||
LogVarSet Xs = copy.logVarSet();
|
LogVarSet Xs = copy.logVarSet();
|
||||||
for (unsigned i = 0; i < Xs.size(); i++) {
|
for (size_t i = 0; i < Xs.size(); i++) {
|
||||||
cout << "-> projection of " << Xs[i] << ": " ;
|
cout << "-> projection of " << Xs[i] << ": " ;
|
||||||
cout << copy.tupleSet ({Xs[i]}) << endl;
|
cout << copy.tupleSet ({Xs[i]}) << endl;
|
||||||
}
|
}
|
||||||
@ -652,7 +618,7 @@ Parfactor::printProjections (void) const
|
|||||||
|
|
||||||
void
|
void
|
||||||
Parfactor::expandPotential (
|
Parfactor::expandPotential (
|
||||||
int fIdx,
|
size_t fIdx,
|
||||||
unsigned newRange,
|
unsigned newRange,
|
||||||
const vector<unsigned>& sumIndexes)
|
const vector<unsigned>& sumIndexes)
|
||||||
{
|
{
|
||||||
@ -663,44 +629,41 @@ Parfactor::expandPotential (
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
Params copy = params_;
|
Params backup = params_;
|
||||||
params_.clear();
|
params_.clear();
|
||||||
params_.reserve (newSize);
|
params_.reserve (newSize);
|
||||||
|
|
||||||
unsigned prod = 1;
|
size_t prod = 1;
|
||||||
vector<unsigned> offsets_ (ranges_.size());
|
vector<size_t> offsets (ranges_.size());
|
||||||
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||||
offsets_[i] = prod;
|
offsets[i] = prod;
|
||||||
prod *= ranges_[i];
|
prod *= ranges_[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned index = 0;
|
size_t index = 0;
|
||||||
ranges_[fIdx] = newRange;
|
ranges_[fIdx] = newRange;
|
||||||
vector<unsigned> indices (ranges_.size(), 0);
|
vector<unsigned> indices (ranges_.size(), 0);
|
||||||
for (unsigned k = 0; k < newSize; k++) {
|
for (size_t k = 0; k < newSize; k++) {
|
||||||
if (index >= copy.size()) {
|
assert (index < backup.size());
|
||||||
abort();
|
params_.push_back (backup[index]);
|
||||||
}
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||||
assert (index < copy.size());
|
|
||||||
params_.push_back (copy[index]);
|
|
||||||
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
|
||||||
indices[i] ++;
|
indices[i] ++;
|
||||||
if (i == fIdx) {
|
if (i == fIdx) {
|
||||||
if (indices[i] != ranges_[i]) {
|
if (indices[i] != ranges_[i]) {
|
||||||
int diff = sumIndexes[indices[i]] - sumIndexes[indices[i] - 1];
|
int diff = sumIndexes[indices[i]] - sumIndexes[indices[i] - 1];
|
||||||
index += diff * offsets_[i];
|
index += diff * offsets[i];
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
// last index contains the old range minus 1
|
// last index contains the old range minus 1
|
||||||
index -= sumIndexes.back() * offsets_[i];
|
index -= sumIndexes.back() * offsets[i];
|
||||||
indices[i] = 0;
|
indices[i] = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (indices[i] != ranges_[i]) {
|
if (indices[i] != ranges_[i]) {
|
||||||
index += offsets_[i];
|
index += offsets[i];
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
index -= (ranges_[i] - 1) * offsets_[i];
|
index -= (ranges_[i] - 1) * offsets[i];
|
||||||
indices[i] = 0;
|
indices[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -711,11 +674,11 @@ Parfactor::expandPotential (
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Parfactor::simplifyCountingFormulas (int fIdx)
|
Parfactor::simplifyCountingFormulas (size_t fIdx)
|
||||||
{
|
{
|
||||||
// check if we can simplify the parfactor
|
// check if we can simplify the parfactor
|
||||||
for (unsigned i = 0; i < args_.size(); i++) {
|
for (size_t i = 0; i < args_.size(); i++) {
|
||||||
if ((int)i != fIdx &&
|
if (i != fIdx &&
|
||||||
args_[i].isCounting() &&
|
args_[i].isCounting() &&
|
||||||
args_[i].group() == args_[fIdx].group()) {
|
args_[i].group() == args_[fIdx].group()) {
|
||||||
// if they only differ in the name of the counting log var
|
// if they only differ in the name of the counting log var
|
||||||
@ -734,9 +697,12 @@ Parfactor::simplifyCountingFormulas (int fIdx)
|
|||||||
void
|
void
|
||||||
Parfactor::simplifyGrounds (void)
|
Parfactor::simplifyGrounds (void)
|
||||||
{
|
{
|
||||||
|
if (args_.size() == 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
LogVarSet singletons = constr_->singletons();
|
LogVarSet singletons = constr_->singletons();
|
||||||
for (int i = 0; i < (int)args_.size() - 1; i++) {
|
for (long i = 0; i < (long)args_.size() - 1; i++) {
|
||||||
for (unsigned j = i + 1; j < args_.size(); j++) {
|
for (size_t j = i + 1; j < args_.size(); j++) {
|
||||||
if (args_[i].group() == args_[j].group() &&
|
if (args_[i].group() == args_[j].group() &&
|
||||||
singletons.contains (args_[i].logVarSet()) &&
|
singletons.contains (args_[i].logVarSet()) &&
|
||||||
singletons.contains (args_[j].logVarSet())) {
|
singletons.contains (args_[j].logVarSet())) {
|
||||||
@ -767,18 +733,18 @@ Parfactor::canMultiply (Parfactor* g1, Parfactor* g2)
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Parfactor::simplifyParfactor (unsigned fIdx1, unsigned fIdx2)
|
Parfactor::simplifyParfactor (size_t fIdx1, size_t fIdx2)
|
||||||
{
|
{
|
||||||
Params copy = params_;
|
Params backup = params_;
|
||||||
params_.clear();
|
params_.clear();
|
||||||
StatesIndexer indexer (ranges_);
|
Indexer indexer (ranges_);
|
||||||
while (indexer.valid()) {
|
while (indexer.valid()) {
|
||||||
if (indexer[fIdx1] == indexer[fIdx2]) {
|
if (indexer[fIdx1] == indexer[fIdx2]) {
|
||||||
params_.push_back (copy[indexer]);
|
params_.push_back (backup[indexer]);
|
||||||
}
|
}
|
||||||
++ indexer;
|
++ indexer;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < args_[fIdx2].logVars().size(); i++) {
|
for (size_t i = 0; i < args_[fIdx2].logVars().size(); i++) {
|
||||||
if (nrFormulas (args_[fIdx2].logVars()[i]) == 1) {
|
if (nrFormulas (args_[fIdx2].logVars()[i]) == 1) {
|
||||||
constr_->remove ({ args_[fIdx2].logVars()[i] });
|
constr_->remove ({ args_[fIdx2].logVars()[i] });
|
||||||
}
|
}
|
||||||
@ -795,12 +761,12 @@ Parfactor::getAlignLogVars (Parfactor* g1, Parfactor* g2)
|
|||||||
g1->simplifyGrounds();
|
g1->simplifyGrounds();
|
||||||
g2->simplifyGrounds();
|
g2->simplifyGrounds();
|
||||||
LogVars Xs_1, Xs_2;
|
LogVars Xs_1, Xs_2;
|
||||||
TinySet<unsigned> matchedI;
|
TinySet<size_t> matchedI;
|
||||||
TinySet<unsigned> matchedJ;
|
TinySet<size_t> matchedJ;
|
||||||
ProbFormulas& formulas1 = g1->arguments();
|
ProbFormulas& formulas1 = g1->arguments();
|
||||||
ProbFormulas& formulas2 = g2->arguments();
|
ProbFormulas& formulas2 = g2->arguments();
|
||||||
for (unsigned i = 0; i < formulas1.size(); i++) {
|
for (size_t i = 0; i < formulas1.size(); i++) {
|
||||||
for (unsigned j = 0; j < formulas2.size(); j++) {
|
for (size_t j = 0; j < formulas2.size(); j++) {
|
||||||
if (formulas1[i].group() == formulas2[j].group() &&
|
if (formulas1[i].group() == formulas2[j].group() &&
|
||||||
g1->range (i) == g2->range (j) &&
|
g1->range (i) == g2->range (j) &&
|
||||||
matchedI.contains (i) == false &&
|
matchedI.contains (i) == false &&
|
||||||
@ -850,7 +816,7 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
|
|||||||
// cout << "-> align lvs2 = " << alignLvs2 << endl;
|
// cout << "-> align lvs2 = " << alignLvs2 << endl;
|
||||||
LogVar freeLogVar (0);
|
LogVar freeLogVar (0);
|
||||||
Substitution theta1, theta2;
|
Substitution theta1, theta2;
|
||||||
for (unsigned i = 0; i < alignLvs1.size(); i++) {
|
for (size_t i = 0; i < alignLvs1.size(); i++) {
|
||||||
bool b1 = theta1.containsReplacementFor (alignLvs1[i]);
|
bool b1 = theta1.containsReplacementFor (alignLvs1[i]);
|
||||||
bool b2 = theta2.containsReplacementFor (alignLvs2[i]);
|
bool b2 = theta2.containsReplacementFor (alignLvs2[i]);
|
||||||
if (b1 == false && b2 == false) {
|
if (b1 == false && b2 == false) {
|
||||||
@ -865,14 +831,14 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const LogVarSet& allLvs1 = g1->logVarSet();
|
const LogVarSet& allLvs1 = g1->logVarSet();
|
||||||
for (unsigned i = 0; i < allLvs1.size(); i++) {
|
for (size_t i = 0; i < allLvs1.size(); i++) {
|
||||||
if (theta1.containsReplacementFor (allLvs1[i]) == false) {
|
if (theta1.containsReplacementFor (allLvs1[i]) == false) {
|
||||||
theta1.add (allLvs1[i], freeLogVar);
|
theta1.add (allLvs1[i], freeLogVar);
|
||||||
++ freeLogVar;
|
++ freeLogVar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const LogVarSet& allLvs2 = g2->logVarSet();
|
const LogVarSet& allLvs2 = g2->logVarSet();
|
||||||
for (unsigned i = 0; i < allLvs2.size(); i++) {
|
for (size_t i = 0; i < allLvs2.size(); i++) {
|
||||||
if (theta2.containsReplacementFor (allLvs2[i]) == false) {
|
if (theta2.containsReplacementFor (allLvs2[i]) == false) {
|
||||||
theta2.add (allLvs2[i], freeLogVar);
|
theta2.add (allLvs2[i], freeLogVar);
|
||||||
++ freeLogVar;
|
++ freeLogVar;
|
||||||
@ -883,7 +849,7 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
|
|||||||
// g1 = p(X), q(X) ; X in {(p1),(p2)}
|
// g1 = p(X), q(X) ; X in {(p1),(p2)}
|
||||||
// g2 = p(X), q(Y) ; (X,Y) in {(p1,p2),(p2,p1)}
|
// g2 = p(X), q(Y) ; (X,Y) in {(p1,p2),(p2,p1)}
|
||||||
LogVars discardedLvs1 = theta1.getDiscardedLogVars();
|
LogVars discardedLvs1 = theta1.getDiscardedLogVars();
|
||||||
for (unsigned i = 0; i < discardedLvs1.size(); i++) {
|
for (size_t i = 0; i < discardedLvs1.size(); i++) {
|
||||||
if (g1->constr()->isSingleton (discardedLvs1[i]) &&
|
if (g1->constr()->isSingleton (discardedLvs1[i]) &&
|
||||||
g1->nrFormulas (discardedLvs1[i]) == 1) {
|
g1->nrFormulas (discardedLvs1[i]) == 1) {
|
||||||
g1->constr()->remove (discardedLvs1[i]);
|
g1->constr()->remove (discardedLvs1[i]);
|
||||||
@ -893,7 +859,7 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
LogVars discardedLvs2 = theta2.getDiscardedLogVars();
|
LogVars discardedLvs2 = theta2.getDiscardedLogVars();
|
||||||
for (unsigned i = 0; i < discardedLvs2.size(); i++) {
|
for (size_t i = 0; i < discardedLvs2.size(); i++) {
|
||||||
if (g2->constr()->isSingleton (discardedLvs2[i]) &&
|
if (g2->constr()->isSingleton (discardedLvs2[i]) &&
|
||||||
g2->nrFormulas (discardedLvs2[i]) == 1) {
|
g2->nrFormulas (discardedLvs2[i]) == 1) {
|
||||||
g2->constr()->remove (discardedLvs2[i]);
|
g2->constr()->remove (discardedLvs2[i]);
|
||||||
|
@ -18,7 +18,7 @@ class Parfactor : public TFactor<ProbFormula>
|
|||||||
const ProbFormulas&,
|
const ProbFormulas&,
|
||||||
const Params&,
|
const Params&,
|
||||||
const Tuples&,
|
const Tuples&,
|
||||||
unsigned);
|
unsigned distId);
|
||||||
|
|
||||||
Parfactor (const Parfactor*, const Tuple&);
|
Parfactor (const Parfactor*, const Tuple&);
|
||||||
|
|
||||||
@ -42,11 +42,9 @@ class Parfactor : public TFactor<ProbFormula>
|
|||||||
|
|
||||||
LogVarSet elimLogVars (void) const;
|
LogVarSet elimLogVars (void) const;
|
||||||
|
|
||||||
LogVarSet exclusiveLogVars (unsigned) const;
|
LogVarSet exclusiveLogVars (size_t fIdx) const;
|
||||||
|
|
||||||
void setConstraintTree (ConstraintTree*);
|
void sumOutIndex (size_t fIdx);
|
||||||
|
|
||||||
void sumOut (unsigned fIdx);
|
|
||||||
|
|
||||||
void multiply (Parfactor&);
|
void multiply (Parfactor&);
|
||||||
|
|
||||||
@ -66,21 +64,21 @@ class Parfactor : public TFactor<ProbFormula>
|
|||||||
|
|
||||||
void applySubstitution (const Substitution&);
|
void applySubstitution (const Substitution&);
|
||||||
|
|
||||||
int findGroup (const Ground&) const;
|
PrvGroup findGroup (const Ground&) const;
|
||||||
|
|
||||||
bool containsGround (const Ground&) const;
|
bool containsGround (const Ground&) const;
|
||||||
|
|
||||||
bool containsGroup (unsigned) const;
|
bool containsGroup (PrvGroup) const;
|
||||||
|
|
||||||
unsigned nrFormulas (LogVar) const;
|
unsigned nrFormulas (LogVar) const;
|
||||||
|
|
||||||
int indexOfLogVar (LogVar) const;
|
int indexOfLogVar (LogVar) const;
|
||||||
|
|
||||||
int indexOfGroup (unsigned) const;
|
int indexOfGroup (PrvGroup) const;
|
||||||
|
|
||||||
unsigned nrFormulasWithGroup (unsigned) const;
|
unsigned nrFormulasWithGroup (PrvGroup) const;
|
||||||
|
|
||||||
vector<unsigned> getAllGroups (void) const;
|
vector<PrvGroup> getAllGroups (void) const;
|
||||||
|
|
||||||
void print (bool = false) const;
|
void print (bool = false) const;
|
||||||
|
|
||||||
@ -96,14 +94,14 @@ class Parfactor : public TFactor<ProbFormula>
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void simplifyCountingFormulas (int fIdx);
|
void simplifyCountingFormulas (size_t fIdx);
|
||||||
|
|
||||||
void simplifyParfactor (unsigned fIdx1, unsigned fIdx2);
|
void simplifyParfactor (size_t fIdx1, size_t fIdx2);
|
||||||
|
|
||||||
static std::pair<LogVars, LogVars> getAlignLogVars (
|
static std::pair<LogVars, LogVars> getAlignLogVars (
|
||||||
Parfactor* g1, Parfactor* g2);
|
Parfactor* g1, Parfactor* g2);
|
||||||
|
|
||||||
void expandPotential (int fIdx, unsigned newRange,
|
void expandPotential (size_t fIdx, unsigned newRange,
|
||||||
const vector<unsigned>& sumIndexes);
|
const vector<unsigned>& sumIndexes);
|
||||||
|
|
||||||
static void alignAndExponentiate (Parfactor*, Parfactor*);
|
static void alignAndExponentiate (Parfactor*, Parfactor*);
|
||||||
|
@ -45,7 +45,7 @@ ParfactorList::add (Parfactor* pf)
|
|||||||
void
|
void
|
||||||
ParfactorList::add (const Parfactors& pfs)
|
ParfactorList::add (const Parfactors& pfs)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < pfs.size(); i++) {
|
for (size_t i = 0; i < pfs.size(); i++) {
|
||||||
pfs[i]->setNewGroups();
|
pfs[i]->setNewGroups();
|
||||||
addToShatteredList (pfs[i]);
|
addToShatteredList (pfs[i]);
|
||||||
}
|
}
|
||||||
@ -98,11 +98,11 @@ ParfactorList::isAllShattered (void) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
vector<Parfactor*> pfs (pfList_.begin(), pfList_.end());
|
vector<Parfactor*> pfs (pfList_.begin(), pfList_.end());
|
||||||
for (unsigned i = 0; i < pfs.size(); i++) {
|
for (size_t i = 0; i < pfs.size(); i++) {
|
||||||
assert (isShattered (pfs[i]));
|
assert (isShattered (pfs[i]));
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < pfs.size() - 1; i++) {
|
for (size_t i = 0; i < pfs.size() - 1; i++) {
|
||||||
for (unsigned j = i + 1; j < pfs.size(); j++) {
|
for (size_t j = i + 1; j < pfs.size(); j++) {
|
||||||
if (isShattered (pfs[i], pfs[j]) == false) {
|
if (isShattered (pfs[i], pfs[j]) == false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -118,7 +118,7 @@ ParfactorList::print (void) const
|
|||||||
{
|
{
|
||||||
Parfactors pfVec (pfList_.begin(), pfList_.end());
|
Parfactors pfVec (pfList_.begin(), pfList_.end());
|
||||||
std::sort (pfVec.begin(), pfVec.end(), sortByParams());
|
std::sort (pfVec.begin(), pfVec.end(), sortByParams());
|
||||||
for (unsigned i = 0; i < pfVec.size(); i++) {
|
for (size_t i = 0; i < pfVec.size(); i++) {
|
||||||
pfVec[i]->print();
|
pfVec[i]->print();
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
@ -134,8 +134,8 @@ ParfactorList::isShattered (const Parfactor* g) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
ConstraintTree ct (*g->constr());
|
ConstraintTree ct (*g->constr());
|
||||||
for (unsigned i = 0; i < formulas.size() - 1; i++) {
|
for (size_t i = 0; i < formulas.size() - 1; i++) {
|
||||||
for (unsigned j = i + 1; j < formulas.size(); j++) {
|
for (size_t j = i + 1; j < formulas.size(); j++) {
|
||||||
if (formulas[i].group() == formulas[j].group()) {
|
if (formulas[i].group() == formulas[j].group()) {
|
||||||
if (identical (
|
if (identical (
|
||||||
formulas[i], *(g->constr()),
|
formulas[i], *(g->constr()),
|
||||||
@ -171,8 +171,8 @@ ParfactorList::isShattered (
|
|||||||
const ProbFormulas& fms1 = g1->arguments();
|
const ProbFormulas& fms1 = g1->arguments();
|
||||||
const ProbFormulas& fms2 = g2->arguments();
|
const ProbFormulas& fms2 = g2->arguments();
|
||||||
|
|
||||||
for (unsigned i = 0; i < fms1.size(); i++) {
|
for (size_t i = 0; i < fms1.size(); i++) {
|
||||||
for (unsigned j = 0; j < fms2.size(); j++) {
|
for (size_t j = 0; j < fms2.size(); j++) {
|
||||||
if (fms1[i].group() == fms2[j].group()) {
|
if (fms1[i].group() == fms2[j].group()) {
|
||||||
if (identical (
|
if (identical (
|
||||||
fms1[i], *(g1->constr()),
|
fms1[i], *(g1->constr()),
|
||||||
@ -261,7 +261,7 @@ ParfactorList::shatterAgainstMySelf (Parfactor* g)
|
|||||||
pfs.push_back (pf);
|
pfs.push_back (pf);
|
||||||
} else {
|
} else {
|
||||||
shattered = false;
|
shattered = false;
|
||||||
for (unsigned i = 0; i < res.size(); i++) {
|
for (size_t i = 0; i < res.size(); i++) {
|
||||||
assert (res[i]->constr()->empty() == false);
|
assert (res[i]->constr()->empty() == false);
|
||||||
residuals.push (res[i]);
|
residuals.push (res[i]);
|
||||||
}
|
}
|
||||||
@ -280,8 +280,8 @@ ParfactorList::shatterAgainstMySelf2 (Parfactor* g)
|
|||||||
// slip a parfactor with overlapping formulas:
|
// slip a parfactor with overlapping formulas:
|
||||||
// e.g. {s(X),s(Y)}, with (X,Y) in {(p1,p2),(p1,p3),(p4,p1)}
|
// e.g. {s(X),s(Y)}, with (X,Y) in {(p1,p2),(p1,p3),(p4,p1)}
|
||||||
const ProbFormulas& formulas = g->arguments();
|
const ProbFormulas& formulas = g->arguments();
|
||||||
for (unsigned i = 0; i < formulas.size() - 1; i++) {
|
for (size_t i = 0; i < formulas.size() - 1; i++) {
|
||||||
for (unsigned j = i + 1; j < formulas.size(); j++) {
|
for (size_t j = i + 1; j < formulas.size(); j++) {
|
||||||
if (formulas[i].sameSkeletonAs (formulas[j])) {
|
if (formulas[i].sameSkeletonAs (formulas[j])) {
|
||||||
Parfactors res = shatterAgainstMySelf (g, i, j);
|
Parfactors res = shatterAgainstMySelf (g, i, j);
|
||||||
if (res.empty() == false) {
|
if (res.empty() == false) {
|
||||||
@ -298,8 +298,8 @@ ParfactorList::shatterAgainstMySelf2 (Parfactor* g)
|
|||||||
Parfactors
|
Parfactors
|
||||||
ParfactorList::shatterAgainstMySelf (
|
ParfactorList::shatterAgainstMySelf (
|
||||||
Parfactor* g,
|
Parfactor* g,
|
||||||
unsigned fIdx1,
|
size_t fIdx1,
|
||||||
unsigned fIdx2)
|
size_t fIdx2)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
Util::printDashedLine();
|
Util::printDashedLine();
|
||||||
@ -340,7 +340,7 @@ ParfactorList::shatterAgainstMySelf (
|
|||||||
return { };
|
return { };
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned newGroup = ProbFormula::getNewGroup();
|
PrvGroup newGroup = ProbFormula::getNewGroup();
|
||||||
Parfactors res1 = shatter (g, fIdx1, commCt1, exclCt1, newGroup);
|
Parfactors res1 = shatter (g, fIdx1, commCt1, exclCt1, newGroup);
|
||||||
if (res1.empty()) {
|
if (res1.empty()) {
|
||||||
res1.push_back (g);
|
res1.push_back (g);
|
||||||
@ -348,7 +348,7 @@ ParfactorList::shatterAgainstMySelf (
|
|||||||
|
|
||||||
Parfactors res;
|
Parfactors res;
|
||||||
ctCopy.moveToTop (f1.logVars());
|
ctCopy.moveToTop (f1.logVars());
|
||||||
for (unsigned i = 0; i < res1.size(); i++) {
|
for (size_t i = 0; i < res1.size(); i++) {
|
||||||
res1[i]->constr()->moveToTop (f2.logVars());
|
res1[i]->constr()->moveToTop (f2.logVars());
|
||||||
std::pair<ConstraintTree*, ConstraintTree*> split2;
|
std::pair<ConstraintTree*, ConstraintTree*> split2;
|
||||||
split2 = res1[i]->constr()->split (f2.logVars(), &ctCopy, f1.logVars());
|
split2 = res1[i]->constr()->split (f2.logVars(), &ctCopy, f1.logVars());
|
||||||
@ -370,7 +370,7 @@ ParfactorList::shatterAgainstMySelf (
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Util::addToVector (res, res2);
|
Util::addToVector (res, res2);
|
||||||
for (unsigned j = 0; j < res2.size(); j++) {
|
for (size_t j = 0; j < res2.size(); j++) {
|
||||||
}
|
}
|
||||||
if (res1[i] != g) {
|
if (res1[i] != g) {
|
||||||
delete res1[i];
|
delete res1[i];
|
||||||
@ -393,8 +393,8 @@ ParfactorList::shatter (Parfactor* g1, Parfactor* g2)
|
|||||||
ProbFormulas& formulas1 = g1->arguments();
|
ProbFormulas& formulas1 = g1->arguments();
|
||||||
ProbFormulas& formulas2 = g2->arguments();
|
ProbFormulas& formulas2 = g2->arguments();
|
||||||
assert (g1 != 0 && g2 != 0 && g1 != g2);
|
assert (g1 != 0 && g2 != 0 && g1 != g2);
|
||||||
for (unsigned i = 0; i < formulas1.size(); i++) {
|
for (size_t i = 0; i < formulas1.size(); i++) {
|
||||||
for (unsigned j = 0; j < formulas2.size(); j++) {
|
for (size_t j = 0; j < formulas2.size(); j++) {
|
||||||
if (formulas1[i].sameSkeletonAs (formulas2[j])) {
|
if (formulas1[i].sameSkeletonAs (formulas2[j])) {
|
||||||
std::pair<Parfactors, Parfactors> res;
|
std::pair<Parfactors, Parfactors> res;
|
||||||
res = shatter (i, g1, j, g2);
|
res = shatter (i, g1, j, g2);
|
||||||
@ -412,8 +412,8 @@ ParfactorList::shatter (Parfactor* g1, Parfactor* g2)
|
|||||||
|
|
||||||
std::pair<Parfactors, Parfactors>
|
std::pair<Parfactors, Parfactors>
|
||||||
ParfactorList::shatter (
|
ParfactorList::shatter (
|
||||||
unsigned fIdx1, Parfactor* g1,
|
size_t fIdx1, Parfactor* g1,
|
||||||
unsigned fIdx2, Parfactor* g2)
|
size_t fIdx2, Parfactor* g2)
|
||||||
{
|
{
|
||||||
ProbFormula& f1 = g1->argument (fIdx1);
|
ProbFormula& f1 = g1->argument (fIdx1);
|
||||||
ProbFormula& f2 = g2->argument (fIdx2);
|
ProbFormula& f2 = g2->argument (fIdx2);
|
||||||
@ -464,7 +464,6 @@ ParfactorList::shatter (
|
|||||||
assert (commCt1->tupleSet (f1.logVars()) ==
|
assert (commCt1->tupleSet (f1.logVars()) ==
|
||||||
commCt2->tupleSet (f2.logVars()));
|
commCt2->tupleSet (f2.logVars()));
|
||||||
|
|
||||||
// unsigned static count = 0; count ++;
|
|
||||||
// stringstream ss1; ss1 << "" << count << "_A.dot" ;
|
// stringstream ss1; ss1 << "" << count << "_A.dot" ;
|
||||||
// stringstream ss2; ss2 << "" << count << "_B.dot" ;
|
// stringstream ss2; ss2 << "" << count << "_B.dot" ;
|
||||||
// stringstream ss3; ss3 << "" << count << "_A_comm.dot" ;
|
// stringstream ss3; ss3 << "" << count << "_A_comm.dot" ;
|
||||||
@ -489,7 +488,7 @@ ParfactorList::shatter (
|
|||||||
return { };
|
return { };
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned group;
|
PrvGroup group;
|
||||||
if (exclCt1->empty()) {
|
if (exclCt1->empty()) {
|
||||||
group = f1.group();
|
group = f1.group();
|
||||||
} else if (exclCt2->empty()) {
|
} else if (exclCt2->empty()) {
|
||||||
@ -507,10 +506,10 @@ ParfactorList::shatter (
|
|||||||
Parfactors
|
Parfactors
|
||||||
ParfactorList::shatter (
|
ParfactorList::shatter (
|
||||||
Parfactor* g,
|
Parfactor* g,
|
||||||
unsigned fIdx,
|
size_t fIdx,
|
||||||
ConstraintTree* commCt,
|
ConstraintTree* commCt,
|
||||||
ConstraintTree* exclCt,
|
ConstraintTree* exclCt,
|
||||||
unsigned commGroup)
|
PrvGroup commGroup)
|
||||||
{
|
{
|
||||||
ProbFormula& f = g->argument (fIdx);
|
ProbFormula& f = g->argument (fIdx);
|
||||||
if (exclCt->empty()) {
|
if (exclCt->empty()) {
|
||||||
@ -526,7 +525,7 @@ ParfactorList::shatter (
|
|||||||
LogVar X_new2 = g->constr()->logVarSet().back() + 2;
|
LogVar X_new2 = g->constr()->logVarSet().back() + 2;
|
||||||
ConstraintTrees cts = g->constr()->jointCountNormalize (
|
ConstraintTrees cts = g->constr()->jointCountNormalize (
|
||||||
commCt, exclCt, f.countedLogVar(), X_new1, X_new2);
|
commCt, exclCt, f.countedLogVar(), X_new1, X_new2);
|
||||||
for (unsigned i = 0; i < cts.size(); i++) {
|
for (size_t i = 0; i < cts.size(); i++) {
|
||||||
Parfactor* newPf = new Parfactor (g, cts[i]);
|
Parfactor* newPf = new Parfactor (g, cts[i]);
|
||||||
if (cts[i]->nrLogVars() == g->constr()->nrLogVars() + 1) {
|
if (cts[i]->nrLogVars() == g->constr()->nrLogVars() + 1) {
|
||||||
newPf->expand (f.countedLogVar(), X_new1, X_new2);
|
newPf->expand (f.countedLogVar(), X_new1, X_new2);
|
||||||
@ -557,12 +556,12 @@ ParfactorList::shatter (
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ParfactorList::updateGroups (unsigned oldGroup, unsigned newGroup)
|
ParfactorList::updateGroups (PrvGroup oldGroup, PrvGroup newGroup)
|
||||||
{
|
{
|
||||||
for (ParfactorList::iterator it = pfList_.begin();
|
for (ParfactorList::iterator it = pfList_.begin();
|
||||||
it != pfList_.end(); it++) {
|
it != pfList_.end(); ++it) {
|
||||||
ProbFormulas& formulas = (*it)->arguments();
|
ProbFormulas& formulas = (*it)->arguments();
|
||||||
for (unsigned i = 0; i < formulas.size(); i++) {
|
for (size_t i = 0; i < formulas.size(); i++) {
|
||||||
if (formulas[i].group() == oldGroup) {
|
if (formulas[i].group() == oldGroup) {
|
||||||
formulas[i].setGroup (newGroup);
|
formulas[i].setGroup (newGroup);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ class ParfactorList
|
|||||||
|
|
||||||
void clear (void) { pfList_.clear(); }
|
void clear (void) { pfList_.clear(); }
|
||||||
|
|
||||||
unsigned size (void) const { return pfList_.size(); }
|
size_t size (void) const { return pfList_.size(); }
|
||||||
|
|
||||||
typedef std::list<Parfactor*>::iterator iterator;
|
typedef std::list<Parfactor*>::iterator iterator;
|
||||||
|
|
||||||
@ -70,22 +70,22 @@ class ParfactorList
|
|||||||
Parfactors shatterAgainstMySelf2 (Parfactor* g);
|
Parfactors shatterAgainstMySelf2 (Parfactor* g);
|
||||||
|
|
||||||
Parfactors shatterAgainstMySelf (
|
Parfactors shatterAgainstMySelf (
|
||||||
Parfactor* g, unsigned fIdx1, unsigned fIdx2);
|
Parfactor* g, size_t fIdx1, size_t fIdx2);
|
||||||
|
|
||||||
std::pair<Parfactors, Parfactors> shatter (
|
std::pair<Parfactors, Parfactors> shatter (
|
||||||
Parfactor*, Parfactor*);
|
Parfactor*, Parfactor*);
|
||||||
|
|
||||||
std::pair<Parfactors, Parfactors> shatter (
|
std::pair<Parfactors, Parfactors> shatter (
|
||||||
unsigned, Parfactor*, unsigned, Parfactor*);
|
size_t, Parfactor*, size_t, Parfactor*);
|
||||||
|
|
||||||
Parfactors shatter (
|
Parfactors shatter (
|
||||||
Parfactor*,
|
Parfactor*,
|
||||||
unsigned,
|
size_t,
|
||||||
ConstraintTree*,
|
ConstraintTree*,
|
||||||
ConstraintTree*,
|
ConstraintTree*,
|
||||||
unsigned);
|
PrvGroup);
|
||||||
|
|
||||||
void updateGroups (unsigned group1, unsigned group2);
|
void updateGroups (PrvGroup group1, PrvGroup group2);
|
||||||
|
|
||||||
bool proper (
|
bool proper (
|
||||||
const ProbFormula&, ConstraintTree,
|
const ProbFormula&, ConstraintTree,
|
||||||
@ -113,7 +113,7 @@ class ParfactorList
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
list<Parfactor*> pfList_;
|
list<Parfactor*> pfList_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HORUS_PARFACTORLIST_H
|
#endif // HORUS_PARFACTORLIST_H
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "ProbFormula.h"
|
#include "ProbFormula.h"
|
||||||
|
|
||||||
|
|
||||||
int ProbFormula::freeGroup_ = 0;
|
PrvGroup ProbFormula::freeGroup_ = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ ProbFormula::contains (LogVarSet s) const
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int
|
size_t
|
||||||
ProbFormula::indexOf (LogVar X) const
|
ProbFormula::indexOf (LogVar X) const
|
||||||
{
|
{
|
||||||
return Util::indexOf (logVars_, X);
|
return Util::indexOf (logVars_, X);
|
||||||
@ -81,7 +81,7 @@ ProbFormula::clearCountedLogVar (void)
|
|||||||
void
|
void
|
||||||
ProbFormula::rename (LogVar oldName, LogVar newName)
|
ProbFormula::rename (LogVar oldName, LogVar newName)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < logVars_.size(); i++) {
|
for (size_t i = 0; i < logVars_.size(); i++) {
|
||||||
if (logVars_[i] == oldName) {
|
if (logVars_[i] == oldName) {
|
||||||
logVars_[i] = newName;
|
logVars_[i] = newName;
|
||||||
}
|
}
|
||||||
@ -105,7 +105,7 @@ std::ostream& operator<< (ostream &os, const ProbFormula& f)
|
|||||||
os << f.functor_;
|
os << f.functor_;
|
||||||
if (f.isAtom() == false) {
|
if (f.isAtom() == false) {
|
||||||
os << "(" ;
|
os << "(" ;
|
||||||
for (unsigned i = 0; i < f.logVars_.size(); i++) {
|
for (size_t i = 0; i < f.logVars_.size(); i++) {
|
||||||
if (i != 0) os << ",";
|
if (i != 0) os << ",";
|
||||||
if (f.isCounting() && f.logVars_[i] == f.countedLogVar_) {
|
if (f.isCounting() && f.logVars_[i] == f.countedLogVar_) {
|
||||||
os << "#" ;
|
os << "#" ;
|
||||||
@ -120,10 +120,11 @@ std::ostream& operator<< (ostream &os, const ProbFormula& f)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
PrvGroup
|
||||||
ProbFormula::getNewGroup (void)
|
ProbFormula::getNewGroup (void)
|
||||||
{
|
{
|
||||||
freeGroup_ ++;
|
freeGroup_ ++;
|
||||||
|
assert (freeGroup_ != numeric_limits<PrvGroup>::max());
|
||||||
return freeGroup_;
|
return freeGroup_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,17 +7,17 @@
|
|||||||
#include "LiftedUtils.h"
|
#include "LiftedUtils.h"
|
||||||
#include "Horus.h"
|
#include "Horus.h"
|
||||||
|
|
||||||
typedef unsigned PrvGroup;
|
typedef unsigned long PrvGroup;
|
||||||
|
|
||||||
class ProbFormula
|
class ProbFormula
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ProbFormula (Symbol f, const LogVars& lvs, unsigned range)
|
ProbFormula (Symbol f, const LogVars& lvs, unsigned range)
|
||||||
: functor_(f), logVars_(lvs), range_(range),
|
: functor_(f), logVars_(lvs), range_(range),
|
||||||
countedLogVar_(), group_(Util::maxUnsigned()) { }
|
countedLogVar_(), group_(numeric_limits<PrvGroup>::max()) { }
|
||||||
|
|
||||||
ProbFormula (Symbol f, unsigned r)
|
ProbFormula (Symbol f, unsigned r)
|
||||||
: functor_(f), range_(r), group_(Util::maxUnsigned()) { }
|
: functor_(f), range_(r), group_(numeric_limits<PrvGroup>::max()) { }
|
||||||
|
|
||||||
Symbol functor (void) const { return functor_; }
|
Symbol functor (void) const { return functor_; }
|
||||||
|
|
||||||
@ -31,9 +31,9 @@ class ProbFormula
|
|||||||
|
|
||||||
LogVarSet logVarSet (void) const { return LogVarSet (logVars_); }
|
LogVarSet logVarSet (void) const { return LogVarSet (logVars_); }
|
||||||
|
|
||||||
unsigned group (void) const { return group_; }
|
PrvGroup group (void) const { return group_; }
|
||||||
|
|
||||||
void setGroup (unsigned g) { group_ = g; }
|
void setGroup (PrvGroup g) { group_ = g; }
|
||||||
|
|
||||||
bool sameSkeletonAs (const ProbFormula&) const;
|
bool sameSkeletonAs (const ProbFormula&) const;
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ class ProbFormula
|
|||||||
|
|
||||||
bool contains (LogVarSet) const;
|
bool contains (LogVarSet) const;
|
||||||
|
|
||||||
int indexOf (LogVar) const;
|
size_t indexOf (LogVar) const;
|
||||||
|
|
||||||
bool isAtom (void) const;
|
bool isAtom (void) const;
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ class ProbFormula
|
|||||||
|
|
||||||
void rename (LogVar, LogVar);
|
void rename (LogVar, LogVar);
|
||||||
|
|
||||||
static unsigned getNewGroup (void);
|
static PrvGroup getNewGroup (void);
|
||||||
|
|
||||||
friend std::ostream& operator<< (ostream &os, const ProbFormula& f);
|
friend std::ostream& operator<< (ostream &os, const ProbFormula& f);
|
||||||
|
|
||||||
@ -66,8 +66,8 @@ class ProbFormula
|
|||||||
LogVars logVars_;
|
LogVars logVars_;
|
||||||
unsigned range_;
|
unsigned range_;
|
||||||
LogVar countedLogVar_;
|
LogVar countedLogVar_;
|
||||||
unsigned group_;
|
PrvGroup group_;
|
||||||
static int freeGroup_;
|
static PrvGroup freeGroup_;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef vector<ProbFormula> ProbFormulas;
|
typedef vector<ProbFormula> ProbFormulas;
|
||||||
|
@ -7,7 +7,7 @@ Solver::printAnswer (const VarIds& vids)
|
|||||||
{
|
{
|
||||||
Vars unobservedVars;
|
Vars unobservedVars;
|
||||||
VarIds unobservedVids;
|
VarIds unobservedVids;
|
||||||
for (unsigned i = 0; i < vids.size(); i++) {
|
for (size_t i = 0; i < vids.size(); i++) {
|
||||||
VarNode* vn = fg.getVarNode (vids[i]);
|
VarNode* vn = fg.getVarNode (vids[i]);
|
||||||
if (vn->hasEvidence() == false) {
|
if (vn->hasEvidence() == false) {
|
||||||
unobservedVars.push_back (vn);
|
unobservedVars.push_back (vn);
|
||||||
@ -16,7 +16,7 @@ Solver::printAnswer (const VarIds& vids)
|
|||||||
}
|
}
|
||||||
Params res = solveQuery (unobservedVids);
|
Params res = solveQuery (unobservedVids);
|
||||||
vector<string> stateLines = Util::getStateLines (unobservedVars);
|
vector<string> stateLines = Util::getStateLines (unobservedVars);
|
||||||
for (unsigned i = 0; i < res.size(); i++) {
|
for (size_t i = 0; i < res.size(); i++) {
|
||||||
cout << "P(" << stateLines[i] << ") = " ;
|
cout << "P(" << stateLines[i] << ") = " ;
|
||||||
cout << std::setprecision (Constants::PRECISION) << res[i];
|
cout << std::setprecision (Constants::PRECISION) << res[i];
|
||||||
cout << endl;
|
cout << endl;
|
||||||
@ -31,11 +31,11 @@ Solver::printAllPosterioris (void)
|
|||||||
{
|
{
|
||||||
VarIds vids;
|
VarIds vids;
|
||||||
const VarNodes& vars = fg.varNodes();
|
const VarNodes& vars = fg.varNodes();
|
||||||
for (unsigned i = 0; i < vars.size(); i++) {
|
for (size_t i = 0; i < vars.size(); i++) {
|
||||||
vids.push_back (vars[i]->varId());
|
vids.push_back (vars[i]->varId());
|
||||||
}
|
}
|
||||||
std::sort (vids.begin(), vids.end());
|
std::sort (vids.begin(), vids.end());
|
||||||
for (unsigned i = 0; i < vids.size(); i++) {
|
for (size_t i = 0; i < vids.size(); i++) {
|
||||||
printAnswer ({vids[i]});
|
printAnswer ({vids[i]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
- Refactor sum out in factor
|
|
||||||
- Add a way to sum out several vars at the same time
|
|
||||||
- Receive ranges as a constant reference in Indexer
|
|
||||||
- Check if evidence remains in the compressed factor graph
|
- Check if evidence remains in the compressed factor graph
|
||||||
- Consider using hashs instead of vectors of colors to calculate the groups in
|
- Consider using hashs instead of vectors of colors to calculate the groups in
|
||||||
counting bp
|
counting bp
|
||||||
- use more psize_t instead of unsigned for looping through params
|
|
||||||
- Find a way to decrease the time required to find an
|
- Find a way to decrease the time required to find an
|
||||||
elimination order for variable elimination
|
elimination order for variable elimination
|
||||||
- Add a sequential elimination heuristic
|
- Add a sequential elimination heuristic
|
||||||
|
@ -30,11 +30,6 @@ unsigned maxIter = 1000;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
vector<NetInfo> Statistics::netInfo_;
|
|
||||||
vector<CompressInfo> Statistics::compressInfo_;
|
|
||||||
unsigned Statistics::primaryNetCount_;
|
|
||||||
|
|
||||||
|
|
||||||
namespace Util {
|
namespace Util {
|
||||||
|
|
||||||
|
|
||||||
@ -76,25 +71,6 @@ stringToDouble (string str)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
toLog (Params& v)
|
|
||||||
{
|
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
|
||||||
v[i] = log (v[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
fromLog (Params& v)
|
|
||||||
{
|
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
|
||||||
v[i] = exp (v[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double
|
double
|
||||||
factorial (unsigned num)
|
factorial (unsigned num)
|
||||||
@ -149,20 +125,17 @@ nrCombinations (unsigned n, unsigned k)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
size_t
|
||||||
expectedSize (const Ranges& ranges)
|
sizeExpected (const Ranges& ranges)
|
||||||
{
|
{
|
||||||
unsigned prod = 1;
|
return std::accumulate (
|
||||||
for (unsigned i = 0; i < ranges.size(); i++) {
|
ranges.begin(), ranges.end(), 1, multiplies<unsigned>());
|
||||||
prod *= ranges[i];
|
|
||||||
}
|
|
||||||
return prod;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
getNumberOfDigits (int num)
|
nrDigits (int num)
|
||||||
{
|
{
|
||||||
unsigned count = 1;
|
unsigned count = 1;
|
||||||
while (num >= 10) {
|
while (num >= 10) {
|
||||||
@ -193,7 +166,7 @@ parametersToString (const Params& v, unsigned precision)
|
|||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss.precision (precision);
|
ss.precision (precision);
|
||||||
ss << "[" ;
|
ss << "[" ;
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
if (i != 0) ss << ", " ;
|
if (i != 0) ss << ", " ;
|
||||||
ss << v[i];
|
ss << v[i];
|
||||||
}
|
}
|
||||||
@ -206,16 +179,21 @@ parametersToString (const Params& v, unsigned precision)
|
|||||||
vector<string>
|
vector<string>
|
||||||
getStateLines (const Vars& vars)
|
getStateLines (const Vars& vars)
|
||||||
{
|
{
|
||||||
StatesIndexer idx (vars);
|
Ranges ranges;
|
||||||
|
for (size_t i = 0; i < vars.size(); i++) {
|
||||||
|
ranges.push_back (vars[i]->range());
|
||||||
|
}
|
||||||
|
Indexer indexer (ranges);
|
||||||
vector<string> jointStrings;
|
vector<string> jointStrings;
|
||||||
while (idx.valid()) {
|
while (indexer.valid()) {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
for (unsigned i = 0; i < vars.size(); i++) {
|
for (size_t i = 0; i < vars.size(); i++) {
|
||||||
if (i != 0) ss << ", " ;
|
if (i != 0) ss << ", " ;
|
||||||
ss << vars[i]->label() << "=" << vars[i]->states()[(idx[i])];
|
ss << vars[i]->label() << "=" ;
|
||||||
|
ss << vars[i]->states()[(indexer[i])];
|
||||||
}
|
}
|
||||||
jointStrings.push_back (ss.str());
|
jointStrings.push_back (ss.str());
|
||||||
++ idx;
|
++ indexer;
|
||||||
}
|
}
|
||||||
return jointStrings;
|
return jointStrings;
|
||||||
}
|
}
|
||||||
@ -347,19 +325,19 @@ normalize (Params& v)
|
|||||||
{
|
{
|
||||||
double sum = LogAware::addIdenty();
|
double sum = LogAware::addIdenty();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
sum = Util::logSum (sum, v[i]);
|
sum = Util::logSum (sum, v[i]);
|
||||||
}
|
}
|
||||||
assert (sum != -numeric_limits<double>::infinity());
|
assert (sum != -numeric_limits<double>::infinity());
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
v[i] -= sum;
|
v[i] -= sum;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
sum += v[i];
|
sum += v[i];
|
||||||
}
|
}
|
||||||
assert (sum != 0.0);
|
assert (sum != 0.0);
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
v[i] /= sum;
|
v[i] /= sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -373,11 +351,11 @@ getL1Distance (const Params& v1, const Params& v2)
|
|||||||
assert (v1.size() == v2.size());
|
assert (v1.size() == v2.size());
|
||||||
double dist = 0.0;
|
double dist = 0.0;
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < v1.size(); i++) {
|
for (size_t i = 0; i < v1.size(); i++) {
|
||||||
dist += abs (exp(v1[i]) - exp(v2[i]));
|
dist += abs (exp(v1[i]) - exp(v2[i]));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < v1.size(); i++) {
|
for (size_t i = 0; i < v1.size(); i++) {
|
||||||
dist += abs (v1[i] - v2[i]);
|
dist += abs (v1[i] - v2[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -392,14 +370,14 @@ getMaxNorm (const Params& v1, const Params& v2)
|
|||||||
assert (v1.size() == v2.size());
|
assert (v1.size() == v2.size());
|
||||||
double max = 0.0;
|
double max = 0.0;
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < v1.size(); i++) {
|
for (size_t i = 0; i < v1.size(); i++) {
|
||||||
double diff = abs (exp(v1[i]) - exp(v2[i]));
|
double diff = abs (exp(v1[i]) - exp(v2[i]));
|
||||||
if (diff > max) {
|
if (diff > max) {
|
||||||
max = diff;
|
max = diff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < v1.size(); i++) {
|
for (size_t i = 0; i < v1.size(); i++) {
|
||||||
double diff = abs (v1[i] - v2[i]);
|
double diff = abs (v1[i] - v2[i]);
|
||||||
if (diff > max) {
|
if (diff > max) {
|
||||||
max = diff;
|
max = diff;
|
||||||
@ -410,234 +388,41 @@ getMaxNorm (const Params& v1, const Params& v2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double
|
double
|
||||||
pow (double p, unsigned expoent)
|
pow (double base, unsigned iexp)
|
||||||
{
|
{
|
||||||
return Globals::logDomain ? p * expoent : std::pow (p, expoent);
|
return Globals::logDomain ? base * iexp : std::pow (base, iexp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double
|
double
|
||||||
pow (double p, double expoent)
|
pow (double base, double exp)
|
||||||
{
|
{
|
||||||
// assumes that `expoent' is never in log domain
|
// assumes that `expoent' is never in log domain
|
||||||
return Globals::logDomain ? p * expoent : std::pow (p, expoent);
|
return Globals::logDomain ? base * exp : std::pow (base, exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
pow (Params& v, unsigned expoent)
|
pow (Params& v, unsigned iexp)
|
||||||
{
|
{
|
||||||
if (expoent == 1) {
|
if (iexp == 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Globals::logDomain) {
|
Globals::logDomain ? v *= iexp : v ^= (int)iexp;
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
|
||||||
v[i] *= expoent;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
|
||||||
v[i] = std::pow (v[i], expoent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
pow (Params& v, double expoent)
|
pow (Params& v, double exp)
|
||||||
{
|
{
|
||||||
// assumes that `expoent' is never in log domain
|
// `expoent' should not be in log domain
|
||||||
if (Globals::logDomain) {
|
Globals::logDomain ? v *= exp : v ^= exp;
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
|
||||||
v[i] *= expoent;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
|
||||||
v[i] = std::pow (v[i], expoent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
|
||||||
Statistics::getSolvedNetworksCounting (void)
|
|
||||||
{
|
|
||||||
return netInfo_.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Statistics::incrementPrimaryNetworksCounting (void)
|
|
||||||
{
|
|
||||||
primaryNetCount_ ++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
|
||||||
Statistics::getPrimaryNetworksCounting (void)
|
|
||||||
{
|
|
||||||
return primaryNetCount_;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Statistics::updateStatistics (
|
|
||||||
unsigned size,
|
|
||||||
bool loopy,
|
|
||||||
unsigned nIters,
|
|
||||||
double time)
|
|
||||||
{
|
|
||||||
netInfo_.push_back (NetInfo (size, loopy, nIters, time));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Statistics::printStatistics (void)
|
|
||||||
{
|
|
||||||
cout << getStatisticString();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Statistics::writeStatistics (const char* fileName)
|
|
||||||
{
|
|
||||||
ofstream out (fileName);
|
|
||||||
if (!out.is_open()) {
|
|
||||||
cerr << "error: cannot open file to write at " ;
|
|
||||||
cerr << "Statistics::writeStats()" << endl;
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
out << getStatisticString();
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
Statistics::updateCompressingStatistics (
|
|
||||||
unsigned nrGroundVars,
|
|
||||||
unsigned nrGroundFactors,
|
|
||||||
unsigned nrClusterVars,
|
|
||||||
unsigned nrClusterFactors,
|
|
||||||
unsigned nrNeighborless)
|
|
||||||
{
|
|
||||||
compressInfo_.push_back (CompressInfo (nrGroundVars, nrGroundFactors,
|
|
||||||
nrClusterVars, nrClusterFactors, nrNeighborless));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
string
|
|
||||||
Statistics::getStatisticString (void)
|
|
||||||
{
|
|
||||||
stringstream ss2, ss3, ss4, ss1;
|
|
||||||
ss1 << "running mode: " ;
|
|
||||||
switch (Globals::infAlgorithm) {
|
|
||||||
case InfAlgorithms::VE: ss1 << "ve" << endl; break;
|
|
||||||
case InfAlgorithms::BP: ss1 << "bp" << endl; break;
|
|
||||||
case InfAlgorithms::CBP: ss1 << "cbp" << endl; break;
|
|
||||||
}
|
|
||||||
ss1 << "message schedule: " ;
|
|
||||||
switch (BpOptions::schedule) {
|
|
||||||
case BpOptions::Schedule::SEQ_FIXED:
|
|
||||||
ss1 << "sequential fixed" << endl;
|
|
||||||
break;
|
|
||||||
case BpOptions::Schedule::SEQ_RANDOM:
|
|
||||||
ss1 << "sequential random" << endl;
|
|
||||||
break;
|
|
||||||
case BpOptions::Schedule::PARALLEL:
|
|
||||||
ss1 << "parallel" << endl;
|
|
||||||
break;
|
|
||||||
case BpOptions::Schedule::MAX_RESIDUAL:
|
|
||||||
ss1 << "max residual" << endl;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ss1 << "max iterations: " << BpOptions::maxIter << endl;
|
|
||||||
ss1 << "accuracy " << BpOptions::accuracy << endl;
|
|
||||||
ss1 << endl << endl;
|
|
||||||
Util::printSubHeader ("Network information", ss2);
|
|
||||||
ss2 << left;
|
|
||||||
ss2 << setw (15) << "Network Size" ;
|
|
||||||
ss2 << setw (9) << "Loopy" ;
|
|
||||||
ss2 << setw (15) << "Iterations" ;
|
|
||||||
ss2 << setw (15) << "Solving Time" ;
|
|
||||||
ss2 << endl;
|
|
||||||
unsigned nLoopyNets = 0;
|
|
||||||
unsigned nUnconvergedRuns = 0;
|
|
||||||
double totalSolvingTime = 0.0;
|
|
||||||
for (unsigned i = 0; i < netInfo_.size(); i++) {
|
|
||||||
ss2 << setw (15) << netInfo_[i].size;
|
|
||||||
if (netInfo_[i].loopy) {
|
|
||||||
ss2 << setw (9) << "yes";
|
|
||||||
nLoopyNets ++;
|
|
||||||
} else {
|
|
||||||
ss2 << setw (9) << "no";
|
|
||||||
}
|
|
||||||
if (netInfo_[i].nIters == 0) {
|
|
||||||
ss2 << setw (15) << "n/a" ;
|
|
||||||
} else {
|
|
||||||
ss2 << setw (15) << netInfo_[i].nIters;
|
|
||||||
if (netInfo_[i].nIters > BpOptions::maxIter) {
|
|
||||||
nUnconvergedRuns ++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ss2 << setw (15) << netInfo_[i].time;
|
|
||||||
totalSolvingTime += netInfo_[i].time;
|
|
||||||
ss2 << endl;
|
|
||||||
}
|
|
||||||
ss2 << endl << endl;
|
|
||||||
|
|
||||||
unsigned c1 = 0, c2 = 0, c3 = 0, c4 = 0;
|
|
||||||
if (compressInfo_.size() > 0) {
|
|
||||||
Util::printSubHeader ("Compress information", ss3);
|
|
||||||
ss3 << left;
|
|
||||||
ss3 << "Ground Cluster Ground Cluster Neighborless" << endl;
|
|
||||||
ss3 << "Vars Vars Factors Factors Vars" << endl;
|
|
||||||
for (unsigned i = 0; i < compressInfo_.size(); i++) {
|
|
||||||
ss3 << setw (9) << compressInfo_[i].nrGroundVars;
|
|
||||||
ss3 << setw (10) << compressInfo_[i].nrClusterVars;
|
|
||||||
ss3 << setw (10) << compressInfo_[i].nrGroundFactors;
|
|
||||||
ss3 << setw (10) << compressInfo_[i].nrClusterFactors;
|
|
||||||
ss3 << setw (10) << compressInfo_[i].nrNeighborless;
|
|
||||||
ss3 << endl;
|
|
||||||
c1 += compressInfo_[i].nrGroundVars - compressInfo_[i].nrNeighborless;
|
|
||||||
c2 += compressInfo_[i].nrClusterVars;
|
|
||||||
c3 += compressInfo_[i].nrGroundFactors - compressInfo_[i].nrNeighborless;
|
|
||||||
c4 += compressInfo_[i].nrClusterFactors;
|
|
||||||
if (compressInfo_[i].nrNeighborless != 0) {
|
|
||||||
c2 --;
|
|
||||||
c4 --;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ss3 << endl << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ss4 << "primary networks: " << primaryNetCount_ << endl;
|
|
||||||
ss4 << "solved networks: " << netInfo_.size() << endl;
|
|
||||||
ss4 << "loopy networks: " << nLoopyNets << endl;
|
|
||||||
ss4 << "unconverged runs: " << nUnconvergedRuns << endl;
|
|
||||||
ss4 << "total solving time: " << totalSolvingTime << endl;
|
|
||||||
if (compressInfo_.size() > 0) {
|
|
||||||
double pc1 = (1.0 - (c2 / (double)c1)) * 100.0;
|
|
||||||
double pc2 = (1.0 - (c4 / (double)c3)) * 100.0;
|
|
||||||
ss4 << setprecision (5);
|
|
||||||
ss4 << "variable compression: " << pc1 << "%" << endl;
|
|
||||||
ss4 << "factor compression: " << pc2 << "%" << endl;
|
|
||||||
}
|
|
||||||
ss4 << endl << endl;
|
|
||||||
|
|
||||||
ss1 << ss4.str() << ss2.str() << ss3.str();
|
|
||||||
return ss1.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -19,6 +19,11 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
const double NEG_INF = -numeric_limits<double>::infinity();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
namespace Util {
|
namespace Util {
|
||||||
|
|
||||||
template <typename T> void addToVector (vector<T>&, const vector<T>&);
|
template <typename T> void addToVector (vector<T>&, const vector<T>&);
|
||||||
@ -34,43 +39,40 @@ template <typename T> bool contains (const set<T>&, const T&);
|
|||||||
template <typename K, typename V> bool contains (
|
template <typename K, typename V> bool contains (
|
||||||
const unordered_map<K, V>&, const K&);
|
const unordered_map<K, V>&, const K&);
|
||||||
|
|
||||||
template <typename T> int indexOf (const vector<T>&, const T&);
|
template <typename T> size_t indexOf (const vector<T>&, const T&);
|
||||||
|
|
||||||
|
template <typename T> void log (vector<T>&);
|
||||||
|
|
||||||
|
template <typename T> void exp (vector<T>&);
|
||||||
|
|
||||||
|
template <typename T> string elementsToString (
|
||||||
|
const vector<T>& v, string sep = " ");
|
||||||
|
|
||||||
template <typename T> std::string toString (const T&);
|
template <typename T> std::string toString (const T&);
|
||||||
|
|
||||||
template <> std::string toString (const bool&);
|
template <> std::string toString (const bool&);
|
||||||
|
|
||||||
unsigned stringToUnsigned (string);
|
|
||||||
|
|
||||||
double stringToDouble (string);
|
|
||||||
|
|
||||||
void toLog (Params&);
|
|
||||||
|
|
||||||
void fromLog (Params&);
|
|
||||||
|
|
||||||
double logSum (double, double);
|
double logSum (double, double);
|
||||||
|
|
||||||
void multiply (Params&, const Params&);
|
|
||||||
|
|
||||||
void multiply (Params&, const Params&, unsigned);
|
|
||||||
|
|
||||||
void add (Params&, const Params&);
|
|
||||||
|
|
||||||
void subtract (Params&, const Params&);
|
|
||||||
|
|
||||||
void add (Params&, const Params&, unsigned);
|
void add (Params&, const Params&, unsigned);
|
||||||
|
|
||||||
|
void multiply (Params&, const Params&, unsigned);
|
||||||
|
|
||||||
unsigned maxUnsigned (void);
|
unsigned maxUnsigned (void);
|
||||||
|
|
||||||
|
unsigned stringToUnsigned (string);
|
||||||
|
|
||||||
|
double stringToDouble (string);
|
||||||
|
|
||||||
double factorial (unsigned);
|
double factorial (unsigned);
|
||||||
|
|
||||||
double logFactorial (unsigned);
|
double logFactorial (unsigned);
|
||||||
|
|
||||||
unsigned nrCombinations (unsigned, unsigned);
|
unsigned nrCombinations (unsigned, unsigned);
|
||||||
|
|
||||||
unsigned expectedSize (const Ranges&);
|
size_t sizeExpected (const Ranges&);
|
||||||
|
|
||||||
unsigned getNumberOfDigits (int);
|
unsigned nrDigits (int);
|
||||||
|
|
||||||
bool isInteger (const string&);
|
bool isInteger (const string&);
|
||||||
|
|
||||||
@ -111,7 +113,7 @@ Util::addToSet (set<T>& s, const vector<T>& elements)
|
|||||||
template <typename T> void
|
template <typename T> void
|
||||||
Util::addToQueue (queue<T>& q, const vector<T>& elements)
|
Util::addToQueue (queue<T>& q, const vector<T>& elements)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < elements.size(); i++) {
|
for (size_t i = 0; i < elements.size(); i++) {
|
||||||
q.push (elements[i]);
|
q.push (elements[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,15 +144,39 @@ Util::contains (const unordered_map<K, V>& m, const K& k)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T> int
|
template <typename T> size_t
|
||||||
Util::indexOf (const vector<T>& v, const T& e)
|
Util::indexOf (const vector<T>& v, const T& e)
|
||||||
{
|
{
|
||||||
int pos = std::distance (v.begin(),
|
return std::distance (v.begin(),
|
||||||
std::find (v.begin(), v.end(), e));
|
std::find (v.begin(), v.end(), e));
|
||||||
if (pos == (int)v.size()) {
|
}
|
||||||
pos = -1;
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T> void
|
||||||
|
Util::log (vector<T>& v)
|
||||||
|
{
|
||||||
|
transform (v.begin(), v.end(), v.begin(), ::log);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T> void
|
||||||
|
Util::exp (vector<T>& v)
|
||||||
|
{
|
||||||
|
transform (v.begin(), v.end(), v.begin(), ::exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T> string
|
||||||
|
Util::elementsToString (const vector<T>& v, string sep)
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
|
ss << ((i != 0) ? sep : "") << v[i];
|
||||||
}
|
}
|
||||||
return pos;
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -165,23 +191,6 @@ Util::toString (const T& t)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
std::ostream& operator << (std::ostream& os, const vector<T>& v)
|
|
||||||
{
|
|
||||||
os << "[" ;
|
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
|
||||||
os << ((i != 0) ? ", " : "") << v[i];
|
|
||||||
}
|
|
||||||
os << "]" ;
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
const double NEG_INF = -numeric_limits<double>::infinity();
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
inline double
|
inline double
|
||||||
Util::logSum (double x, double y)
|
Util::logSum (double x, double y)
|
||||||
{
|
{
|
||||||
@ -216,23 +225,12 @@ Util::logSum (double x, double y)
|
|||||||
|
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
Util::multiply (Params& v1, const Params& v2)
|
Util::add (Params& v1, const Params& v2, unsigned repetitions)
|
||||||
{
|
{
|
||||||
assert (v1.size() == v2.size());
|
for (size_t count = 0; count < v1.size(); ) {
|
||||||
for (unsigned i = 0; i < v1.size(); i++) {
|
for (size_t i = 0; i < v2.size(); i++) {
|
||||||
v1[i] *= v2[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline void
|
|
||||||
Util::multiply (Params& v1, const Params& v2, unsigned repetitions)
|
|
||||||
{
|
|
||||||
for (unsigned count = 0; count < v1.size(); ) {
|
|
||||||
for (unsigned i = 0; i < v2.size(); i++) {
|
|
||||||
for (unsigned r = 0; r < repetitions; r++) {
|
for (unsigned r = 0; r < repetitions; r++) {
|
||||||
v1[count] *= v2[i];
|
v1[count] += v2[i];
|
||||||
count ++;
|
count ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -242,32 +240,12 @@ Util::multiply (Params& v1, const Params& v2, unsigned repetitions)
|
|||||||
|
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
Util::add (Params& v1, const Params& v2)
|
Util::multiply (Params& v1, const Params& v2, unsigned repetitions)
|
||||||
{
|
{
|
||||||
assert (v1.size() == v2.size());
|
for (size_t count = 0; count < v1.size(); ) {
|
||||||
std::transform (v1.begin(), v1.end(), v2.begin(),
|
for (size_t i = 0; i < v2.size(); i++) {
|
||||||
v1.begin(), plus<double>());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline void
|
|
||||||
Util::subtract (Params& v1, const Params& v2)
|
|
||||||
{
|
|
||||||
assert (v1.size() == v2.size());
|
|
||||||
std::transform (v1.begin(), v1.end(), v2.begin(),
|
|
||||||
v1.begin(), minus<double>());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline void
|
|
||||||
Util::add (Params& v1, const Params& v2, unsigned repetitions)
|
|
||||||
{
|
|
||||||
for (unsigned count = 0; count < v1.size(); ) {
|
|
||||||
for (unsigned i = 0; i < v2.size(); i++) {
|
|
||||||
for (unsigned r = 0; r < repetitions; r++) {
|
for (unsigned r = 0; r < repetitions; r++) {
|
||||||
v1[count] += v2[i];
|
v1[count] *= v2[i];
|
||||||
count ++;
|
count ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -286,58 +264,14 @@ Util::maxUnsigned (void)
|
|||||||
|
|
||||||
namespace LogAware {
|
namespace LogAware {
|
||||||
|
|
||||||
inline double
|
inline double one() { return Globals::logDomain ? 0.0 : 1.0; }
|
||||||
one()
|
inline double zero() { return Globals::logDomain ? NEG_INF : 0.0; }
|
||||||
{
|
inline double addIdenty() { return Globals::logDomain ? NEG_INF : 0.0; }
|
||||||
return Globals::logDomain ? 0.0 : 1.0;
|
inline double multIdenty() { return Globals::logDomain ? 0.0 : 1.0; }
|
||||||
}
|
inline double withEvidence() { return Globals::logDomain ? 0.0 : 1.0; }
|
||||||
|
inline double noEvidence() { return Globals::logDomain ? NEG_INF : 0.0; }
|
||||||
|
inline double log (double v) { return Globals::logDomain ? ::log (v) : v; }
|
||||||
inline double
|
inline double exp (double v) { return Globals::logDomain ? ::exp (v) : v; }
|
||||||
zero() {
|
|
||||||
return Globals::logDomain ? NEG_INF : 0.0 ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline double
|
|
||||||
addIdenty()
|
|
||||||
{
|
|
||||||
return Globals::logDomain ? NEG_INF : 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline double
|
|
||||||
multIdenty()
|
|
||||||
{
|
|
||||||
return Globals::logDomain ? 0.0 : 1.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline double
|
|
||||||
withEvidence()
|
|
||||||
{
|
|
||||||
return Globals::logDomain ? 0.0 : 1.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline double
|
|
||||||
noEvidence() {
|
|
||||||
return Globals::logDomain ? NEG_INF : 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline double
|
|
||||||
tl (double v)
|
|
||||||
{
|
|
||||||
return Globals::logDomain ? log (v) : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline double
|
|
||||||
fl (double v)
|
|
||||||
{
|
|
||||||
return Globals::logDomain ? exp (v) : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void normalize (Params&);
|
void normalize (Params&);
|
||||||
@ -358,65 +292,108 @@ void pow (Params&, double);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct NetInfo
|
template <typename T>
|
||||||
|
void operator+=(std::vector<T>& v, double val)
|
||||||
{
|
{
|
||||||
NetInfo (unsigned size, bool loopy, unsigned nIters, double time)
|
std::transform (v.begin(), v.end(), v.begin(),
|
||||||
{
|
std::bind1st (plus<double>(), val));
|
||||||
this->size = size;
|
}
|
||||||
this->loopy = loopy;
|
|
||||||
this->nIters = nIters;
|
|
||||||
this->time = time;
|
|
||||||
}
|
|
||||||
unsigned size;
|
|
||||||
bool loopy;
|
|
||||||
unsigned nIters;
|
|
||||||
double time;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct CompressInfo
|
|
||||||
|
template <typename T>
|
||||||
|
void operator-=(std::vector<T>& v, double val)
|
||||||
{
|
{
|
||||||
CompressInfo (unsigned a, unsigned b, unsigned c, unsigned d, unsigned e)
|
std::transform (v.begin(), v.end(), v.begin(),
|
||||||
{
|
std::bind1st (minus<double>(), val));
|
||||||
nrGroundVars = a;
|
}
|
||||||
nrGroundFactors = b;
|
|
||||||
nrClusterVars = c;
|
|
||||||
nrClusterFactors = d;
|
|
||||||
nrNeighborless = e;
|
|
||||||
}
|
|
||||||
unsigned nrGroundVars;
|
|
||||||
unsigned nrGroundFactors;
|
|
||||||
unsigned nrClusterVars;
|
|
||||||
unsigned nrClusterFactors;
|
|
||||||
unsigned nrNeighborless;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class Statistics
|
|
||||||
|
template <typename T>
|
||||||
|
void operator*=(std::vector<T>& v, double val)
|
||||||
{
|
{
|
||||||
public:
|
std::transform (v.begin(), v.end(), v.begin(),
|
||||||
static unsigned getSolvedNetworksCounting (void);
|
std::bind1st (multiplies<double>(), val));
|
||||||
|
}
|
||||||
|
|
||||||
static void incrementPrimaryNetworksCounting (void);
|
|
||||||
|
|
||||||
static unsigned getPrimaryNetworksCounting (void);
|
|
||||||
|
|
||||||
static void updateStatistics (unsigned, bool, unsigned, double);
|
template <typename T>
|
||||||
|
void operator/=(std::vector<T>& v, double val)
|
||||||
|
{
|
||||||
|
std::transform (v.begin(), v.end(), v.begin(),
|
||||||
|
std::bind1st (divides<double>(), val));
|
||||||
|
}
|
||||||
|
|
||||||
static void printStatistics (void);
|
|
||||||
|
|
||||||
static void writeStatistics (const char*);
|
|
||||||
|
|
||||||
static void updateCompressingStatistics (
|
template <typename T>
|
||||||
unsigned, unsigned, unsigned, unsigned, unsigned);
|
void operator+=(std::vector<T>& a, const std::vector<T>& b)
|
||||||
|
{
|
||||||
|
assert (a.size() == b.size());
|
||||||
|
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||||
|
plus<double>());
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
static string getStatisticString (void);
|
|
||||||
|
|
||||||
static vector<NetInfo> netInfo_;
|
|
||||||
static vector<CompressInfo> compressInfo_;
|
template <typename T>
|
||||||
static unsigned primaryNetCount_;
|
void operator-=(std::vector<T>& a, const std::vector<T>& b)
|
||||||
};
|
{
|
||||||
|
assert (a.size() == b.size());
|
||||||
|
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||||
|
minus<double>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void operator*=(std::vector<T>& a, const std::vector<T>& b)
|
||||||
|
{
|
||||||
|
assert (a.size() == b.size());
|
||||||
|
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||||
|
multiplies<double>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void operator/=(std::vector<T>& a, const std::vector<T>& b)
|
||||||
|
{
|
||||||
|
assert (a.size() == b.size());
|
||||||
|
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||||
|
divides<double>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void operator^=(std::vector<T>& v, double exp)
|
||||||
|
{
|
||||||
|
std::transform (v.begin(), v.end(), v.begin(),
|
||||||
|
std::bind2nd (ptr_fun<double, double, double> (std::pow), exp));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void operator^=(std::vector<T>& v, int iexp)
|
||||||
|
{
|
||||||
|
std::transform (v.begin(), v.end(), v.begin(),
|
||||||
|
std::bind2nd (ptr_fun<double, int, double> (std::pow), iexp));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::ostream& operator << (std::ostream& os, const vector<T>& v)
|
||||||
|
{
|
||||||
|
os << "[" ;
|
||||||
|
os << Util::elementsToString (v, ", ");
|
||||||
|
os << "]" ;
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // HORUS_UTIL_H
|
#endif // HORUS_UTIL_H
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ void
|
|||||||
Var::setEvidence (const string& ev)
|
Var::setEvidence (const string& ev)
|
||||||
{
|
{
|
||||||
States states = Var::getVarInfo (varId_).states;
|
States states = Var::getVarInfo (varId_).states;
|
||||||
for (unsigned i = 0; i < states.size(); i++) {
|
for (size_t i = 0; i < states.size(); i++) {
|
||||||
if (states[i] == ev) {
|
if (states[i] == ev) {
|
||||||
evidence_ = i;
|
evidence_ = i;
|
||||||
return;
|
return;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#ifndef HORUS_Var_H
|
#ifndef HORUS_VAR_H
|
||||||
#define HORUS_Var_H
|
#define HORUS_VAR_H
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@ -30,23 +30,23 @@ class Var
|
|||||||
|
|
||||||
virtual ~Var (void) { };
|
virtual ~Var (void) { };
|
||||||
|
|
||||||
unsigned varId (void) const { return varId_; }
|
VarId varId (void) const { return varId_; }
|
||||||
|
|
||||||
unsigned range (void) const { return range_; }
|
unsigned range (void) const { return range_; }
|
||||||
|
|
||||||
int getEvidence (void) const { return evidence_; }
|
int getEvidence (void) const { return evidence_; }
|
||||||
|
|
||||||
unsigned getIndex (void) const { return index_; }
|
size_t getIndex (void) const { return index_; }
|
||||||
|
|
||||||
void setIndex (unsigned idx) { index_ = idx; }
|
void setIndex (size_t idx) { index_ = idx; }
|
||||||
|
|
||||||
operator unsigned () const { return index_; }
|
|
||||||
|
|
||||||
bool hasEvidence (void) const
|
bool hasEvidence (void) const
|
||||||
{
|
{
|
||||||
return evidence_ != Constants::NO_EVIDENCE;
|
return evidence_ != Constants::NO_EVIDENCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
operator size_t (void) const { return index_; }
|
||||||
|
|
||||||
bool operator== (const Var& var) const
|
bool operator== (const Var& var) const
|
||||||
{
|
{
|
||||||
assert (!(varId_ == var.varId() && range_ != var.range()));
|
assert (!(varId_ == var.varId() && range_ != var.range()));
|
||||||
@ -98,11 +98,11 @@ class Var
|
|||||||
VarId varId_;
|
VarId varId_;
|
||||||
unsigned range_;
|
unsigned range_;
|
||||||
int evidence_;
|
int evidence_;
|
||||||
unsigned index_;
|
size_t index_;
|
||||||
|
|
||||||
static unordered_map<VarId, VarInfo> varsInfo_;
|
static unordered_map<VarId, VarInfo> varsInfo_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BP_Var_H
|
#endif // HORUS_VAR_H
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ VarElimSolver::solveQuery (VarIds queryVids)
|
|||||||
{
|
{
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << "Solving query on " ;
|
cout << "Solving query on " ;
|
||||||
for (unsigned i = 0; i < queryVids.size(); i++) {
|
for (size_t i = 0; i < queryVids.size(); i++) {
|
||||||
if (i != 0) cout << ", " ;
|
if (i != 0) cout << ", " ;
|
||||||
cout << fg.getVarNode (queryVids[i])->label();
|
cout << fg.getVarNode (queryVids[i])->label();
|
||||||
}
|
}
|
||||||
@ -33,7 +33,7 @@ VarElimSolver::solveQuery (VarIds queryVids)
|
|||||||
processFactorList (queryVids);
|
processFactorList (queryVids);
|
||||||
Params params = factorList_.back()->params();
|
Params params = factorList_.back()->params();
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
Util::fromLog (params);
|
Util::exp (params);
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
@ -65,15 +65,15 @@ VarElimSolver::createFactorList (void)
|
|||||||
{
|
{
|
||||||
const FacNodes& facNodes = fg.facNodes();
|
const FacNodes& facNodes = fg.facNodes();
|
||||||
factorList_.reserve (facNodes.size() * 2);
|
factorList_.reserve (facNodes.size() * 2);
|
||||||
for (unsigned i = 0; i < facNodes.size(); i++) {
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
||||||
factorList_.push_back (new Factor (facNodes[i]->factor()));
|
factorList_.push_back (new Factor (facNodes[i]->factor()));
|
||||||
const VarNodes& neighs = facNodes[i]->neighbors();
|
const VarNodes& neighs = facNodes[i]->neighbors();
|
||||||
for (unsigned j = 0; j < neighs.size(); j++) {
|
for (size_t j = 0; j < neighs.size(); j++) {
|
||||||
unordered_map<VarId,vector<unsigned> >::iterator it
|
unordered_map<VarId, vector<size_t>>::iterator it
|
||||||
= varFactors_.find (neighs[j]->varId());
|
= varFactors_.find (neighs[j]->varId());
|
||||||
if (it == varFactors_.end()) {
|
if (it == varFactors_.end()) {
|
||||||
it = varFactors_.insert (make_pair (
|
it = varFactors_.insert (make_pair (
|
||||||
neighs[j]->varId(), vector<unsigned>())).first;
|
neighs[j]->varId(), vector<size_t>())).first;
|
||||||
}
|
}
|
||||||
it->second.push_back (i);
|
it->second.push_back (i);
|
||||||
}
|
}
|
||||||
@ -91,16 +91,16 @@ VarElimSolver::absorveEvidence (void)
|
|||||||
printActiveFactors();
|
printActiveFactors();
|
||||||
}
|
}
|
||||||
const VarNodes& varNodes = fg.varNodes();
|
const VarNodes& varNodes = fg.varNodes();
|
||||||
for (unsigned i = 0; i < varNodes.size(); i++) {
|
for (size_t i = 0; i < varNodes.size(); i++) {
|
||||||
if (varNodes[i]->hasEvidence()) {
|
if (varNodes[i]->hasEvidence()) {
|
||||||
if (Globals::verbosity > 1) {
|
if (Globals::verbosity > 1) {
|
||||||
cout << "-> aborving evidence on ";
|
cout << "-> aborving evidence on ";
|
||||||
cout << varNodes[i]->label() << " = " ;
|
cout << varNodes[i]->label() << " = " ;
|
||||||
cout << varNodes[i]->getEvidence() << endl;
|
cout << varNodes[i]->getEvidence() << endl;
|
||||||
}
|
}
|
||||||
const vector<unsigned>& idxs =
|
const vector<size_t>& idxs =
|
||||||
varFactors_.find (varNodes[i]->varId())->second;
|
varFactors_.find (varNodes[i]->varId())->second;
|
||||||
for (unsigned j = 0; j < idxs.size(); j++) {
|
for (size_t j = 0; j < idxs.size(); j++) {
|
||||||
Factor* factor = factorList_[idxs[j]];
|
Factor* factor = factorList_[idxs[j]];
|
||||||
if (factor->nrArguments() == 1) {
|
if (factor->nrArguments() == 1) {
|
||||||
factorList_[idxs[j]] = 0;
|
factorList_[idxs[j]] = 0;
|
||||||
@ -128,7 +128,7 @@ VarElimSolver::processFactorList (const VarIds& vids)
|
|||||||
{
|
{
|
||||||
totalFactorSize_ = 0;
|
totalFactorSize_ = 0;
|
||||||
largestFactorSize_ = 0;
|
largestFactorSize_ = 0;
|
||||||
for (unsigned i = 0; i < elimOrder_.size(); i++) {
|
for (size_t i = 0; i < elimOrder_.size(); i++) {
|
||||||
if (Globals::verbosity >= 2) {
|
if (Globals::verbosity >= 2) {
|
||||||
if (Globals::verbosity >= 3) {
|
if (Globals::verbosity >= 3) {
|
||||||
Util::printDashedLine();
|
Util::printDashedLine();
|
||||||
@ -141,7 +141,7 @@ VarElimSolver::processFactorList (const VarIds& vids)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Factor* finalFactor = new Factor();
|
Factor* finalFactor = new Factor();
|
||||||
for (unsigned i = 0; i < factorList_.size(); i++) {
|
for (size_t i = 0; i < factorList_.size(); i++) {
|
||||||
if (factorList_[i]) {
|
if (factorList_[i]) {
|
||||||
finalFactor->multiply (*factorList_[i]);
|
finalFactor->multiply (*factorList_[i]);
|
||||||
delete factorList_[i];
|
delete factorList_[i];
|
||||||
@ -150,7 +150,7 @@ VarElimSolver::processFactorList (const VarIds& vids)
|
|||||||
}
|
}
|
||||||
|
|
||||||
VarIds unobservedVids;
|
VarIds unobservedVids;
|
||||||
for (unsigned i = 0; i < vids.size(); i++) {
|
for (size_t i = 0; i < vids.size(); i++) {
|
||||||
if (fg.getVarNode (vids[i])->hasEvidence() == false) {
|
if (fg.getVarNode (vids[i])->hasEvidence() == false) {
|
||||||
unobservedVids.push_back (vids[i]);
|
unobservedVids.push_back (vids[i]);
|
||||||
}
|
}
|
||||||
@ -172,9 +172,9 @@ void
|
|||||||
VarElimSolver::eliminate (VarId elimVar)
|
VarElimSolver::eliminate (VarId elimVar)
|
||||||
{
|
{
|
||||||
Factor* result = 0;
|
Factor* result = 0;
|
||||||
vector<unsigned>& idxs = varFactors_.find (elimVar)->second;
|
vector<size_t>& idxs = varFactors_.find (elimVar)->second;
|
||||||
for (unsigned i = 0; i < idxs.size(); i++) {
|
for (size_t i = 0; i < idxs.size(); i++) {
|
||||||
unsigned idx = idxs[i];
|
size_t idx = idxs[i];
|
||||||
if (factorList_[idx]) {
|
if (factorList_[idx]) {
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
result = new Factor (*factorList_[idx]);
|
result = new Factor (*factorList_[idx]);
|
||||||
@ -193,8 +193,8 @@ VarElimSolver::eliminate (VarId elimVar)
|
|||||||
result->sumOut (elimVar);
|
result->sumOut (elimVar);
|
||||||
factorList_.push_back (result);
|
factorList_.push_back (result);
|
||||||
const VarIds& resultVarIds = result->arguments();
|
const VarIds& resultVarIds = result->arguments();
|
||||||
for (unsigned i = 0; i < resultVarIds.size(); i++) {
|
for (size_t i = 0; i < resultVarIds.size(); i++) {
|
||||||
vector<unsigned>& idxs =
|
vector<size_t>& idxs =
|
||||||
varFactors_.find (resultVarIds[i])->second;
|
varFactors_.find (resultVarIds[i])->second;
|
||||||
idxs.push_back (factorList_.size() - 1);
|
idxs.push_back (factorList_.size() - 1);
|
||||||
}
|
}
|
||||||
@ -206,7 +206,7 @@ VarElimSolver::eliminate (VarId elimVar)
|
|||||||
void
|
void
|
||||||
VarElimSolver::printActiveFactors (void)
|
VarElimSolver::printActiveFactors (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < factorList_.size(); i++) {
|
for (size_t i = 0; i < factorList_.size(); i++) {
|
||||||
if (factorList_[i] != 0) {
|
if (factorList_[i] != 0) {
|
||||||
cout << factorList_[i]->getLabel() << " " ;
|
cout << factorList_[i]->getLabel() << " " ;
|
||||||
cout << factorList_[i]->params() << endl;
|
cout << factorList_[i]->params() << endl;
|
||||||
|
@ -39,7 +39,7 @@ class VarElimSolver : public Solver
|
|||||||
VarIds elimOrder_;
|
VarIds elimOrder_;
|
||||||
unsigned largestFactorSize_;
|
unsigned largestFactorSize_;
|
||||||
unsigned totalFactorSize_;
|
unsigned totalFactorSize_;
|
||||||
unordered_map<VarId, vector<unsigned>> varFactors_;
|
unordered_map<VarId, vector<size_t>> varFactors_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HORUS_VARELIMSOLVER_H
|
#endif // HORUS_VARELIMSOLVER_H
|
||||||
|
Reference in New Issue
Block a user