size_t invasion
This commit is contained in:
parent
5124098b6c
commit
af6601837c
@ -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) {
|
||||||
|
@ -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,13 +86,13 @@ 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++) {
|
||||||
probs += links[i]->getMessage();
|
probs += links[i]->getMessage();
|
||||||
}
|
}
|
||||||
LogAware::normalize (probs);
|
LogAware::normalize (probs);
|
||||||
Util::exp (probs);
|
Util::exp (probs);
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < links.size(); i++) {
|
for (size_t i = 0; i < links.size(); i++) {
|
||||||
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]));
|
||||||
@ -157,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;
|
||||||
@ -193,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]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -207,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));
|
||||||
@ -215,7 +215,7 @@ 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();
|
||||||
@ -237,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]);
|
||||||
@ -267,7 +267,7 @@ 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;
|
||||||
@ -357,7 +357,7 @@ BpSolver::getVar2FactorMsg (const SpLink* link) const
|
|||||||
}
|
}
|
||||||
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) {
|
||||||
msg *= links[i]->getMessage();
|
msg *= links[i]->getMessage();
|
||||||
if (Constants::SHOW_BP_CALCS) {
|
if (Constants::SHOW_BP_CALCS) {
|
||||||
@ -378,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]));
|
||||||
}
|
}
|
||||||
@ -390,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);
|
StatesIndexer idx (observedRanges, false);
|
||||||
while (idx.valid()) {
|
while (idx.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 (idx[j]);
|
||||||
}
|
}
|
||||||
++ idx;
|
++ 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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 ++;
|
||||||
}
|
}
|
||||||
@ -431,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]);
|
||||||
@ -477,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;
|
||||||
@ -501,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 = " ;
|
||||||
|
@ -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()) {
|
||||||
@ -131,15 +129,15 @@ CFactorGraph::createGroups (void)
|
|||||||
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()) {
|
||||||
@ -151,7 +149,7 @@ CFactorGraph::createGroups (void)
|
|||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,7 +173,7 @@ CFactorGraph::createClusters (
|
|||||||
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);
|
||||||
@ -188,7 +186,7 @@ CFactorGraph::createClusters (
|
|||||||
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 ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -295,7 +293,7 @@ CFactorGraph::printGroups (
|
|||||||
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 ++;
|
||||||
@ -309,7 +307,7 @@ CFactorGraph::printGroups (
|
|||||||
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;
|
||||||
|
|
||||||
|
@ -13,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();
|
||||||
@ -61,14 +61,14 @@ 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]);
|
||||||
probs += l->poweredMessage();
|
probs += l->poweredMessage();
|
||||||
}
|
}
|
||||||
LogAware::normalize (probs);
|
LogAware::normalize (probs);
|
||||||
Util::exp (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]);
|
||||||
probs *= l->poweredMessage();
|
probs *= l->poweredMessage();
|
||||||
}
|
}
|
||||||
@ -84,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());
|
||||||
}
|
}
|
||||||
@ -103,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 " ;
|
||||||
@ -129,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));
|
||||||
@ -140,7 +140,7 @@ 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();
|
||||||
@ -165,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;
|
||||||
@ -182,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;
|
||||||
@ -208,7 +208,7 @@ 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;
|
||||||
@ -256,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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -307,7 +307,7 @@ 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]);
|
||||||
@ -315,7 +315,7 @@ CbpSolver::getVar2FactorMsg (const SpLink* _link) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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())) {
|
||||||
msg *= cl->poweredMessage();
|
msg *= cl->poweredMessage();
|
||||||
@ -336,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();
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ 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());
|
||||||
}
|
}
|
||||||
@ -51,8 +51,6 @@ Factor::Factor (
|
|||||||
void
|
void
|
||||||
Factor::sumOut (VarId vid)
|
Factor::sumOut (VarId vid)
|
||||||
{
|
{
|
||||||
int idx = indexOf (vid);
|
|
||||||
assert (idx != -1);
|
|
||||||
if (vid == args_.back()) {
|
if (vid == args_.back()) {
|
||||||
sumOutLastVariable(); // optimization
|
sumOutLastVariable(); // optimization
|
||||||
return;
|
return;
|
||||||
@ -61,7 +59,8 @@ Factor::sumOut (VarId vid)
|
|||||||
sumOutFirstVariable(); // optimization
|
sumOutFirstVariable(); // optimization
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sumOutIndex (idx);
|
assert (indexOf (vid) != args_.size());
|
||||||
|
sumOutIndex (indexOf (vid));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -69,7 +68,7 @@ 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) {
|
while (args_.back() != vid) {
|
||||||
sumOutLastVariable();
|
sumOutLastVariable();
|
||||||
}
|
}
|
||||||
@ -94,7 +93,7 @@ Factor::sumOutAllExcept (const VarIds& vids)
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Factor::sumOutIndex (unsigned idx)
|
Factor::sumOutIndex (size_t idx)
|
||||||
{
|
{
|
||||||
assert (idx < args_.size());
|
assert (idx < args_.size());
|
||||||
// number of parameters separating a different state of `var',
|
// number of parameters separating a different state of `var',
|
||||||
@ -149,13 +148,13 @@ Factor::sumOutIndex (unsigned idx)
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Factor::sumOutAllExceptIndex (unsigned idx)
|
Factor::sumOutAllExceptIndex (size_t idx)
|
||||||
{
|
{
|
||||||
assert (idx < args_.size());
|
assert (idx < args_.size());
|
||||||
while (args_.size() > idx + 1) {
|
while (args_.size() > idx + 1) {
|
||||||
sumOutLastVariable();
|
sumOutLastVariable();
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < idx; i++) {
|
for (size_t i = 0; i < idx; i++) {
|
||||||
sumOutFirstVariable();
|
sumOutFirstVariable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,11 +168,11 @@ Factor::sumOutFirstVariable (void)
|
|||||||
unsigned range = ranges_.front();
|
unsigned range = ranges_.front();
|
||||||
unsigned sep = params_.size() / range;
|
unsigned sep = params_.size() / range;
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = sep; i < params_.size(); i++) {
|
for (size_t i = sep; i < params_.size(); i++) {
|
||||||
params_[i % sep] = Util::logSum (params_[i % sep], params_[i]);
|
params_[i % sep] = Util::logSum (params_[i % sep], params_[i]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = sep; i < params_.size(); i++) {
|
for (size_t i = sep; i < params_.size(); i++) {
|
||||||
params_[i % sep] += params_[i];
|
params_[i % sep] += params_[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,9 +187,9 @@ void
|
|||||||
Factor::sumOutLastVariable (void)
|
Factor::sumOutLastVariable (void)
|
||||||
{
|
{
|
||||||
assert (args_.size() > 1);
|
assert (args_.size() > 1);
|
||||||
|
size_t idx1 = 0;
|
||||||
|
size_t idx2 = 0;
|
||||||
unsigned range = ranges_.back();
|
unsigned range = ranges_.back();
|
||||||
unsigned idx1 = 0;
|
|
||||||
unsigned idx2 = 0;
|
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
while (idx1 < params_.size()) {
|
while (idx1 < params_.size()) {
|
||||||
params_[idx2] = params_[idx1];
|
params_[idx2] = params_[idx1];
|
||||||
@ -246,7 +245,7 @@ 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 +259,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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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_; }
|
||||||
|
|
||||||
@ -41,31 +41,24 @@ class TFactor
|
|||||||
assert (params_.size() == Util::sizeExpected (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];
|
||||||
@ -83,10 +76,10 @@ class TFactor
|
|||||||
: params_ *= g_params;
|
: params_ *= g_params;
|
||||||
} else {
|
} else {
|
||||||
bool sharedArgs = false;
|
bool sharedArgs = false;
|
||||||
vector<unsigned> gvarpos;
|
vector<size_t> gvarpos;
|
||||||
for (unsigned i = 0; i < g_args.size(); i++) {
|
for (size_t i = 0; i < g_args.size(); i++) {
|
||||||
int idx = indexOf (g_args[i]);
|
size_t idx = indexOf (g_args[i]);
|
||||||
if (idx == -1) {
|
if (idx == g_args.size()) {
|
||||||
ullong newSize = params_.size() * g_ranges[i];
|
ullong newSize = params_.size() * g_ranges[i];
|
||||||
if (newSize > params_.max_size()) {
|
if (newSize > params_.max_size()) {
|
||||||
cerr << "error: an overflow occurred on factor multiplication" ;
|
cerr << "error: an overflow occurred on factor multiplication" ;
|
||||||
@ -103,8 +96,8 @@ class TFactor
|
|||||||
if (sharedArgs == false) {
|
if (sharedArgs == false) {
|
||||||
// optimization: if the original factors doesn't have common args,
|
// optimization: if the original factors doesn't have common args,
|
||||||
// we don't need to marry the states of the common args
|
// we don't need to marry the states of the common args
|
||||||
unsigned count = 0;
|
size_t count = 0;
|
||||||
for (unsigned i = 0; i < params_.size(); i++) {
|
for (size_t i = 0; i < params_.size(); i++) {
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
params_[i] += g_params[count];
|
params_[i] += g_params[count];
|
||||||
} else {
|
} else {
|
||||||
@ -118,9 +111,9 @@ class TFactor
|
|||||||
} else {
|
} else {
|
||||||
StatesIndexer indexer (ranges_, false);
|
StatesIndexer indexer (ranges_, false);
|
||||||
while (indexer.valid()) {
|
while (indexer.valid()) {
|
||||||
unsigned g_li = 0;
|
size_t g_li = 0;
|
||||||
unsigned prod = 1;
|
size_t prod = 1;
|
||||||
for (int j = gvarpos.size() - 1; j >= 0; j--) {
|
for (size_t j = gvarpos.size(); j-- > 0; ) {
|
||||||
g_li += indexer[gvarpos[j]] * prod;
|
g_li += indexer[gvarpos[j]] * prod;
|
||||||
prod *= g_ranges[j];
|
prod *= g_ranges[j];
|
||||||
}
|
}
|
||||||
@ -137,14 +130,14 @@ class TFactor
|
|||||||
|
|
||||||
void absorveEvidence (const T& arg, unsigned evidence)
|
void absorveEvidence (const T& arg, unsigned evidence)
|
||||||
{
|
{
|
||||||
int idx = indexOf (arg);
|
size_t idx = indexOf (arg);
|
||||||
assert (idx != -1);
|
assert (idx != args_.size());
|
||||||
assert (evidence < ranges_[idx]);
|
assert (evidence < ranges_[idx]);
|
||||||
Params copy = params_;
|
Params copy = params_;
|
||||||
params_.clear();
|
params_.clear();
|
||||||
params_.reserve (copy.size() / ranges_[idx]);
|
params_.reserve (copy.size() / ranges_[idx]);
|
||||||
StatesIndexer indexer (ranges_);
|
StatesIndexer indexer (ranges_);
|
||||||
for (unsigned i = 0; i < evidence; i++) {
|
for (size_t i = 0; i < evidence; i++) {
|
||||||
indexer.increment (idx);
|
indexer.increment (idx);
|
||||||
}
|
}
|
||||||
while (indexer.valid()) {
|
while (indexer.valid()) {
|
||||||
@ -162,16 +155,16 @@ class TFactor
|
|||||||
return; // already in the wanted order
|
return; // already in the wanted order
|
||||||
}
|
}
|
||||||
Ranges newRanges;
|
Ranges newRanges;
|
||||||
vector<unsigned> positions;
|
vector<size_t> positions;
|
||||||
for (unsigned i = 0; i < newArgs.size(); i++) {
|
for (size_t i = 0; i < newArgs.size(); i++) {
|
||||||
unsigned idx = indexOf (newArgs[i]);
|
size_t idx = indexOf (newArgs[i]);
|
||||||
newRanges.push_back (ranges_[idx]);
|
newRanges.push_back (ranges_[idx]);
|
||||||
positions.push_back (idx);
|
positions.push_back (idx);
|
||||||
}
|
}
|
||||||
unsigned N = ranges_.size();
|
size_t N = ranges_.size();
|
||||||
Params newParams (params_.size());
|
Params newParams (params_.size());
|
||||||
for (unsigned i = 0; i < params_.size(); i++) {
|
for (size_t i = 0; i < params_.size(); i++) {
|
||||||
unsigned li = i;
|
size_t li = i;
|
||||||
// calculate vector index corresponding to linear index
|
// calculate vector index corresponding to linear index
|
||||||
vector<unsigned> vi (N);
|
vector<unsigned> vi (N);
|
||||||
for (int k = N-1; k >= 0; k--) {
|
for (int k = N-1; k >= 0; k--) {
|
||||||
@ -179,17 +172,17 @@ class TFactor
|
|||||||
li /= ranges_[k];
|
li /= ranges_[k];
|
||||||
}
|
}
|
||||||
// convert permuted vector index to corresponding linear index
|
// convert permuted vector index to corresponding linear index
|
||||||
unsigned prod = 1;
|
size_t prod = 1;
|
||||||
unsigned new_li = 0;
|
size_t new_li = 0;
|
||||||
for (int k = N - 1; k >= 0; k--) {
|
for (int k = N - 1; k >= 0; k--) {
|
||||||
new_li += vi[positions[k]] * prod;
|
new_li += vi[positions[k]] * prod;
|
||||||
prod *= ranges_[positions[k]];
|
prod *= ranges_[positions[k]];
|
||||||
}
|
}
|
||||||
newParams[new_li] = params_[i];
|
newParams[new_li] = params_[i];
|
||||||
}
|
}
|
||||||
args_ = newArgs;
|
args_ = newArgs;
|
||||||
ranges_ = newRanges;
|
ranges_ = newRanges;
|
||||||
params_ = newParams;
|
params_ = newParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool contains (const T& arg) const
|
bool contains (const T& arg) const
|
||||||
@ -199,7 +192,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;
|
||||||
}
|
}
|
||||||
@ -207,7 +200,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];
|
||||||
@ -223,11 +216,11 @@ class TFactor
|
|||||||
private:
|
private:
|
||||||
void insertArgument (const T& arg, unsigned range)
|
void insertArgument (const T& arg, unsigned range)
|
||||||
{
|
{
|
||||||
assert (indexOf (arg) == -1);
|
assert (indexOf (arg) == args_.size());
|
||||||
Params copy = params_;
|
Params copy = params_;
|
||||||
params_.clear();
|
params_.clear();
|
||||||
params_.reserve (copy.size() * range);
|
params_.reserve (copy.size() * range);
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
for (size_t i = 0; i < copy.size(); i++) {
|
||||||
for (unsigned reps = 0; reps < range; reps++) {
|
for (unsigned reps = 0; reps < range; reps++) {
|
||||||
params_.push_back (copy[i]);
|
params_.push_back (copy[i]);
|
||||||
}
|
}
|
||||||
@ -240,15 +233,15 @@ class TFactor
|
|||||||
{
|
{
|
||||||
Params copy = params_;
|
Params copy = params_;
|
||||||
unsigned nrStates = 1;
|
unsigned nrStates = 1;
|
||||||
for (unsigned i = 0; i < args.size(); i++) {
|
for (size_t i = 0; i < args.size(); i++) {
|
||||||
assert (indexOf (args[i]) == -1);
|
assert (indexOf (args[i]) == args_.size());
|
||||||
args_.push_back (args[i]);
|
args_.push_back (args[i]);
|
||||||
ranges_.push_back (ranges[i]);
|
ranges_.push_back (ranges[i]);
|
||||||
nrStates *= ranges[i];
|
nrStates *= ranges[i];
|
||||||
}
|
}
|
||||||
params_.clear();
|
params_.clear();
|
||||||
params_.reserve (copy.size() * nrStates);
|
params_.reserve (copy.size() * nrStates);
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
for (size_t i = 0; i < copy.size(); i++) {
|
||||||
for (unsigned reps = 0; reps < nrStates; reps++) {
|
for (unsigned reps = 0; reps < nrStates; reps++) {
|
||||||
params_.push_back (copy[i]);
|
params_.push_back (copy[i]);
|
||||||
}
|
}
|
||||||
@ -277,9 +270,9 @@ class Factor : public TFactor<VarId>
|
|||||||
|
|
||||||
void sumOutAllExcept (const VarIds&);
|
void sumOutAllExcept (const VarIds&);
|
||||||
|
|
||||||
void sumOutIndex (unsigned idx);
|
void sumOutIndex (size_t idx);
|
||||||
|
|
||||||
void sumOutAllExceptIndex (unsigned idx);
|
void sumOutAllExceptIndex (size_t idx);
|
||||||
|
|
||||||
void sumOutFirstVariable (void);
|
void sumOutFirstVariable (void);
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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,26 +322,26 @@ 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++) {
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||||
out << varNodes_[i]->range() << " " ;
|
out << varNodes_[i]->range() << " " ;
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
out << facNodes_.size() << endl;
|
out << facNodes_.size() << endl;
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
const VarNodes& factorVars = facNodes_[i]->neighbors();
|
const VarNodes& factorVars = facNodes_[i]->neighbors();
|
||||||
out << factorVars.size();
|
out << factorVars.size();
|
||||||
for (unsigned j = 0; j < factorVars.size(); j++) {
|
for (size_t j = 0; j < factorVars.size(); j++) {
|
||||||
out << " " << factorVars[j]->getIndex();
|
out << " " << factorVars[j]->getIndex();
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < facNodes_.size(); i++) {
|
for (size_t 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::exp (params);
|
Util::exp (params);
|
||||||
}
|
}
|
||||||
out << endl << params.size() << endl << " " ;
|
out << endl << params.size() << endl << " " ;
|
||||||
for (unsigned j = 0; j < params.size(); j++) {
|
for (size_t j = 0; j < params.size(); j++) {
|
||||||
out << params[j] << " " ;
|
out << params[j] << " " ;
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
@ -360,14 +360,14 @@ 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();
|
const VarNodes& factorVars = facNodes_[i]->neighbors();
|
||||||
out << factorVars.size() << endl;
|
out << factorVars.size() << endl;
|
||||||
for (int j = factorVars.size() - 1; j >= 0; j--) {
|
for (int j = factorVars.size() - 1; j >= 0; j--) {
|
||||||
out << factorVars[j]->varId() << " " ;
|
out << factorVars[j]->varId() << " " ;
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
for (unsigned j = 0; j < factorVars.size(); j++) {
|
for (size_t j = 0; j < factorVars.size(); j++) {
|
||||||
out << factorVars[j]->range() << " " ;
|
out << factorVars[j]->range() << " " ;
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
@ -376,7 +376,7 @@ FactorGraph::exportToLibDaiFormat (const char* fileName) const
|
|||||||
Util::exp (params);
|
Util::exp (params);
|
||||||
}
|
}
|
||||||
out << params.size() << endl;
|
out << params.size() << endl;
|
||||||
for (unsigned j = 0; j < params.size(); j++) {
|
for (size_t j = 0; j < params.size(); j++) {
|
||||||
out << j << " " << params[j] << endl;
|
out << j << " " << params[j] << endl;
|
||||||
}
|
}
|
||||||
out << endl;
|
out << endl;
|
||||||
@ -402,7 +402,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 +424,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 +449,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)) {
|
||||||
|
@ -45,9 +45,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,7 +56,7 @@ class FacNode
|
|||||||
|
|
||||||
VarNodes neighs_;
|
VarNodes neighs_;
|
||||||
Factor factor_;
|
Factor factor_;
|
||||||
int index_;
|
size_t index_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -84,9 +84,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
|
||||||
{
|
{
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
@ -148,13 +148,13 @@ ProductOperator::validOp (Parfactor* g1, Parfactor* g2)
|
|||||||
TinySet<unsigned> g2_gs (g2->getAllGroups());
|
TinySet<unsigned> 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<unsigned> 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;
|
||||||
}
|
}
|
||||||
@ -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,14 +216,14 @@ 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->sumOut (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]->sumOut (fIdx);
|
||||||
pfList_.add (pfs[i]);
|
pfList_.add (pfs[i]);
|
||||||
}
|
}
|
||||||
@ -243,7 +243,7 @@ SumOutOperator::getValidOps (
|
|||||||
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;
|
||||||
@ -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();
|
||||||
@ -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;
|
||||||
@ -315,10 +315,10 @@ SumOutOperator::isToEliminate (
|
|||||||
unsigned group,
|
unsigned 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();
|
unsigned 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;
|
||||||
}
|
}
|
||||||
@ -459,7 +459,7 @@ GroundOperator::getLogCost (void)
|
|||||||
vector<pair<unsigned, unsigned>> affectedFormulas;
|
vector<pair<unsigned, 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;
|
||||||
@ -546,10 +546,10 @@ GroundOperator::getValidOps (ParfactorList& pfList)
|
|||||||
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});
|
||||||
@ -604,12 +604,12 @@ GroundOperator::getAffectedFormulas (void)
|
|||||||
pair<unsigned, unsigned> front = q.front();
|
pair<unsigned, 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<unsigned, unsigned> pair = make_pair (
|
||||||
fs[i].group(), fs[i].indexOf (X));
|
fs[i].group(), fs[i].indexOf (X));
|
||||||
@ -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]);
|
||||||
@ -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];
|
||||||
}
|
}
|
||||||
@ -832,7 +832,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
|
|||||||
{
|
{
|
||||||
queue<unsigned> todo; // groups to process
|
queue<unsigned> todo; // groups to process
|
||||||
set<unsigned> done; // processed or in queue
|
set<unsigned> 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]);
|
int group = (*it)->findGroup (query[i]);
|
||||||
@ -853,7 +853,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
|
|||||||
if (Util::contains (requiredPfs, *it) == false &&
|
if (Util::contains (requiredPfs, *it) == false &&
|
||||||
(*it)->containsGroup (group)) {
|
(*it)->containsGroup (group)) {
|
||||||
vector<unsigned> groups = (*it)->getAllGroups();
|
vector<unsigned> 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]);
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
@ -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) {
|
||||||
@ -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]);
|
||||||
|
@ -19,28 +19,10 @@ class StatesIndexer
|
|||||||
|
|
||||||
StatesIndexer (const Ranges& ranges, bool calcOffsets = true)
|
StatesIndexer (const Ranges& ranges, bool calcOffsets = true)
|
||||||
{
|
{
|
||||||
size_ = 1;
|
li_ = 0;
|
||||||
|
size_ = std::accumulate (ranges.begin(), ranges.end(), 0);
|
||||||
indices_.resize (ranges.size(), 0);
|
indices_.resize (ranges.size(), 0);
|
||||||
ranges_ = ranges;
|
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 +30,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;
|
||||||
@ -59,7 +41,7 @@ class StatesIndexer
|
|||||||
li_ ++;
|
li_ ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void increment (unsigned dim)
|
void increment (size_t dim)
|
||||||
{
|
{
|
||||||
assert (dim < ranges_.size());
|
assert (dim < ranges_.size());
|
||||||
assert (ranges_.size() == offsets_.size());
|
assert (ranges_.size() == offsets_.size());
|
||||||
@ -68,10 +50,10 @@ class StatesIndexer
|
|||||||
li_ += offsets_[dim];
|
li_ += offsets_[dim];
|
||||||
}
|
}
|
||||||
|
|
||||||
void incrementExcluding (unsigned skipDim)
|
void incrementExcluding (size_t skipDim)
|
||||||
{
|
{
|
||||||
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 != (int)skipDim) {
|
||||||
indices_[i] ++;
|
indices_[i] ++;
|
||||||
li_ += offsets_[i];
|
li_ += offsets_[i];
|
||||||
@ -86,7 +68,7 @@ class StatesIndexer
|
|||||||
li_ = size_;
|
li_ = size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned linearIndex (void) const
|
size_t linearIndex (void) const
|
||||||
{
|
{
|
||||||
return li_;
|
return li_;
|
||||||
}
|
}
|
||||||
@ -102,12 +84,12 @@ class StatesIndexer
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator unsigned (void) const
|
operator size_t (void) const
|
||||||
{
|
{
|
||||||
return li_;
|
return li_;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned operator[] (unsigned dim) const
|
unsigned operator[] (size_t dim) const
|
||||||
{
|
{
|
||||||
assert (valid());
|
assert (valid());
|
||||||
assert (dim < ranges_.size());
|
assert (dim < ranges_.size());
|
||||||
@ -125,13 +107,13 @@ class StatesIndexer
|
|||||||
li_ = 0;
|
li_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset (unsigned dim)
|
void reset (size_t dim)
|
||||||
{
|
{
|
||||||
indices_[dim] = 0;
|
indices_[dim] = 0;
|
||||||
li_ -= offsets_[dim] * ranges_[dim];
|
li_ -= offsets_[dim] * ranges_[dim];
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned size (void) const
|
size_t size (void) const
|
||||||
{
|
{
|
||||||
return size_ ;
|
return size_ ;
|
||||||
}
|
}
|
||||||
@ -146,19 +128,19 @@ class StatesIndexer
|
|||||||
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 li_;
|
||||||
unsigned size_;
|
size_t size_;
|
||||||
vector<unsigned> indices_;
|
vector<unsigned> indices_;
|
||||||
vector<unsigned> ranges_;
|
vector<unsigned> ranges_;
|
||||||
vector<unsigned> offsets_;
|
vector<size_t> offsets_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -169,9 +151,9 @@ class MapIndexer
|
|||||||
MapIndexer (const Ranges& ranges, const vector<bool>& mapDims)
|
MapIndexer (const Ranges& ranges, const vector<bool>& mapDims)
|
||||||
{
|
{
|
||||||
assert (ranges.size() == mapDims.size());
|
assert (ranges.size() == mapDims.size());
|
||||||
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; ) {
|
||||||
if (mapDims[i]) {
|
if (mapDims[i]) {
|
||||||
offsets_[i] = prod;
|
offsets_[i] = prod;
|
||||||
prod *= ranges[i];
|
prod *= ranges[i];
|
||||||
@ -183,12 +165,12 @@ class MapIndexer
|
|||||||
valid_ = true;
|
valid_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
MapIndexer (const Ranges& ranges, unsigned ignoreDim)
|
MapIndexer (const Ranges& ranges, size_t ignoreDim)
|
||||||
{
|
{
|
||||||
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; ) {
|
||||||
if (i != (int)ignoreDim) {
|
if (i != ignoreDim) {
|
||||||
offsets_[i] = prod;
|
offsets_[i] = prod;
|
||||||
prod *= ranges[i];
|
prod *= ranges[i];
|
||||||
}
|
}
|
||||||
@ -208,13 +190,13 @@ class MapIndexer
|
|||||||
{
|
{
|
||||||
unsigned prod = 1;
|
unsigned prod = 1;
|
||||||
vector<unsigned> offsets (mapRanges.size());
|
vector<unsigned> offsets (mapRanges.size());
|
||||||
for (int i = mapRanges.size() - 1; i >= 0; i--) {
|
for (size_t i = mapRanges.size(); i-- > 0; ) {
|
||||||
offsets[i] = prod;
|
offsets[i] = prod;
|
||||||
prod *= mapRanges[i];
|
prod *= mapRanges[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
offsets_.reserve (loopVids.size());
|
offsets_.reserve (loopVids.size());
|
||||||
for (unsigned i = 0; i < loopVids.size(); i++) {
|
for (size_t i = 0; i < loopVids.size(); i++) {
|
||||||
VarIds::const_iterator it =
|
VarIds::const_iterator it =
|
||||||
std::find (mapVids.begin(), mapVids.end(), loopVids[i]);
|
std::find (mapVids.begin(), mapVids.end(), loopVids[i]);
|
||||||
if (it != mapVids.end()) {
|
if (it != mapVids.end()) {
|
||||||
@ -234,7 +216,7 @@ class MapIndexer
|
|||||||
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 +230,17 @@ class MapIndexer
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned mappedIndex (void) const
|
size_t mappedIndex (void) const
|
||||||
{
|
{
|
||||||
return index_;
|
return index_;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator unsigned (void) const
|
operator size_t (void) const
|
||||||
{
|
{
|
||||||
return index_;
|
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());
|
||||||
@ -284,11 +266,11 @@ class MapIndexer
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned index_;
|
size_t index_;
|
||||||
bool valid_;
|
bool valid_;
|
||||||
vector<unsigned> ranges_;
|
vector<unsigned> ranges_;
|
||||||
vector<unsigned> indices_;
|
vector<unsigned> indices_;
|
||||||
vector<unsigned> offsets_;
|
vector<size_t> offsets_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
@ -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,10 +17,10 @@ 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]);
|
||||||
}
|
}
|
||||||
@ -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();
|
||||||
}
|
}
|
||||||
@ -133,7 +133,7 @@ Parfactor::setConstraintTree (ConstraintTree* newTree)
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Parfactor::sumOut (unsigned fIdx)
|
Parfactor::sumOut (size_t fIdx)
|
||||||
{
|
{
|
||||||
assert (fIdx < args_.size());
|
assert (fIdx < args_.size());
|
||||||
assert (args_[fIdx].contains (elimLogVars()));
|
assert (args_[fIdx].contains (elimLogVars()));
|
||||||
@ -160,12 +160,12 @@ Parfactor::sumOut (unsigned fIdx)
|
|||||||
params_.resize (copy.size() / ranges_[fIdx], LogAware::addIdenty());
|
params_.resize (copy.size() / ranges_[fIdx], LogAware::addIdenty());
|
||||||
MapIndexer indexer (ranges_, fIdx);
|
MapIndexer indexer (ranges_, fIdx);
|
||||||
if (Globals::logDomain) {
|
if (Globals::logDomain) {
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
for (size_t i = 0; i < copy.size(); i++) {
|
||||||
params_[indexer] = Util::logSum (params_[indexer], copy[i]);
|
params_[indexer] = Util::logSum (params_[indexer], copy[i]);
|
||||||
++ indexer;
|
++ indexer;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < copy.size(); i++) {
|
for (size_t i = 0; i < copy.size(); i++) {
|
||||||
params_[indexer] += copy[i];
|
params_[indexer] += copy[i];
|
||||||
++ indexer;
|
++ indexer;
|
||||||
}
|
}
|
||||||
@ -205,7 +205,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 +226,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));
|
||||||
@ -257,7 +257,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.mappedIndex();
|
||||||
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 +278,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 +298,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 +323,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);
|
||||||
@ -344,11 +340,7 @@ Parfactor::fullExpand (LogVar X)
|
|||||||
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 +367,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 +388,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 +403,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 +413,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()) {
|
||||||
@ -440,7 +432,7 @@ int
|
|||||||
Parfactor::findGroup (const Ground& ground) const
|
Parfactor::findGroup (const Ground& ground) const
|
||||||
{
|
{
|
||||||
int group = -1;
|
int group = -1;
|
||||||
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());
|
||||||
@ -466,7 +458,7 @@ Parfactor::containsGround (const Ground& ground) const
|
|||||||
bool
|
bool
|
||||||
Parfactor::containsGroup (unsigned group) const
|
Parfactor::containsGroup (unsigned 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 +472,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 +485,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;
|
||||||
@ -509,8 +501,8 @@ Parfactor::indexOfLogVar (LogVar X) const
|
|||||||
int
|
int
|
||||||
Parfactor::indexOfGroup (unsigned group) const
|
Parfactor::indexOfGroup (unsigned 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;
|
||||||
@ -525,7 +517,7 @@ unsigned
|
|||||||
Parfactor::nrFormulasWithGroup (unsigned group) const
|
Parfactor::nrFormulasWithGroup (unsigned 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 ++;
|
||||||
}
|
}
|
||||||
@ -539,7 +531,7 @@ vector<unsigned>
|
|||||||
Parfactor::getAllGroups (void) const
|
Parfactor::getAllGroups (void) const
|
||||||
{
|
{
|
||||||
vector<unsigned> groups (args_.size());
|
vector<unsigned> 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 +544,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 +561,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;
|
||||||
@ -609,7 +601,7 @@ Parfactor::printParameters (void) const
|
|||||||
StatesIndexer indexer (ranges_);
|
StatesIndexer 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 +620,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 +634,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 +644,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)
|
||||||
{
|
{
|
||||||
@ -674,16 +666,16 @@ Parfactor::expandPotential (
|
|||||||
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()) {
|
if (index >= copy.size()) {
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
assert (index < copy.size());
|
assert (index < copy.size());
|
||||||
params_.push_back (copy[index]);
|
params_.push_back (copy[index]);
|
||||||
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||||
indices[i] ++;
|
indices[i] ++;
|
||||||
if (i == fIdx) {
|
if (i == fIdx) {
|
||||||
if (indices[i] != ranges_[i]) {
|
if (indices[i] != ranges_[i]) {
|
||||||
@ -711,11 +703,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
|
||||||
@ -735,8 +727,8 @@ void
|
|||||||
Parfactor::simplifyGrounds (void)
|
Parfactor::simplifyGrounds (void)
|
||||||
{
|
{
|
||||||
LogVarSet singletons = constr_->singletons();
|
LogVarSet singletons = constr_->singletons();
|
||||||
for (int i = 0; i < (int)args_.size() - 1; i++) {
|
for (long i = 0; i < (int)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,7 +759,7 @@ 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 copy = params_;
|
||||||
params_.clear();
|
params_.clear();
|
||||||
@ -778,7 +770,7 @@ Parfactor::simplifyParfactor (unsigned fIdx1, unsigned fIdx2)
|
|||||||
}
|
}
|
||||||
++ 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 +787,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 +842,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 +857,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 +875,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 +885,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]);
|
||||||
|
@ -42,11 +42,11 @@ 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 setConstraintTree (ConstraintTree*);
|
||||||
|
|
||||||
void sumOut (unsigned fIdx);
|
void sumOut (size_t fIdx);
|
||||||
|
|
||||||
void multiply (Parfactor&);
|
void multiply (Parfactor&);
|
||||||
|
|
||||||
@ -96,14 +96,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();
|
||||||
@ -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" ;
|
||||||
@ -507,7 +506,7 @@ 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)
|
unsigned commGroup)
|
||||||
@ -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);
|
||||||
@ -562,7 +561,7 @@ ParfactorList::updateGroups (unsigned oldGroup, unsigned 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,13 +70,13 @@ 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*,
|
||||||
@ -113,7 +113,7 @@ class ParfactorList
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
list<Parfactor*> pfList_;
|
list<Parfactor*> pfList_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HORUS_PARFACTORLIST_H
|
#endif // HORUS_PARFACTORLIST_H
|
||||||
|
@ -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 << "#" ;
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
@ -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]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ nrCombinations (unsigned n, unsigned k)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned
|
size_t
|
||||||
sizeExpected (const Ranges& ranges)
|
sizeExpected (const Ranges& ranges)
|
||||||
{
|
{
|
||||||
return std::accumulate (
|
return std::accumulate (
|
||||||
@ -166,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];
|
||||||
}
|
}
|
||||||
@ -179,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());
|
||||||
|
}
|
||||||
|
StatesIndexer 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;
|
||||||
}
|
}
|
||||||
@ -320,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -346,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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -365,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;
|
||||||
|
@ -134,7 +134,7 @@ 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> std::string toString (const T&);
|
template <typename T> std::string toString (const T&);
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ double logFactorial (unsigned);
|
|||||||
|
|
||||||
unsigned nrCombinations (unsigned, unsigned);
|
unsigned nrCombinations (unsigned, unsigned);
|
||||||
|
|
||||||
unsigned sizeExpected (const Ranges&);
|
size_t sizeExpected (const Ranges&);
|
||||||
|
|
||||||
unsigned nrDigits (int);
|
unsigned nrDigits (int);
|
||||||
|
|
||||||
@ -205,7 +205,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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -236,12 +236,11 @@ 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));
|
||||||
return pos != (int)v.size() ? pos : -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -260,7 +259,7 @@ template <typename T>
|
|||||||
std::ostream& operator << (std::ostream& os, const vector<T>& v)
|
std::ostream& operator << (std::ostream& os, const vector<T>& v)
|
||||||
{
|
{
|
||||||
os << "[" ;
|
os << "[" ;
|
||||||
for (unsigned i = 0; i < v.size(); i++) {
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
os << ((i != 0) ? ", " : "") << v[i];
|
os << ((i != 0) ? ", " : "") << v[i];
|
||||||
}
|
}
|
||||||
os << "]" ;
|
os << "]" ;
|
||||||
@ -320,8 +319,8 @@ Util::logSum (double x, double y)
|
|||||||
inline void
|
inline void
|
||||||
Util::add (Params& v1, const Params& v2, unsigned repetitions)
|
Util::add (Params& v1, const Params& v2, unsigned repetitions)
|
||||||
{
|
{
|
||||||
for (unsigned count = 0; count < v1.size(); ) {
|
for (size_t count = 0; count < v1.size(); ) {
|
||||||
for (unsigned i = 0; i < v2.size(); i++) {
|
for (size_t 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 ++;
|
||||||
@ -335,8 +334,8 @@ Util::add (Params& v1, const Params& v2, unsigned repetitions)
|
|||||||
inline void
|
inline void
|
||||||
Util::multiply (Params& v1, const Params& v2, unsigned repetitions)
|
Util::multiply (Params& v1, const Params& v2, unsigned repetitions)
|
||||||
{
|
{
|
||||||
for (unsigned count = 0; count < v1.size(); ) {
|
for (size_t count = 0; count < v1.size(); ) {
|
||||||
for (unsigned i = 0; i < v2.size(); i++) {
|
for (size_t 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 ++;
|
||||||
|
@ -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;
|
||||||
|
@ -30,17 +30,17 @@ 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_; }
|
operator size_t (void) const { return index_; }
|
||||||
|
|
||||||
bool hasEvidence (void) const
|
bool hasEvidence (void) const
|
||||||
{
|
{
|
||||||
@ -98,7 +98,7 @@ 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_;
|
||||||
|
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
@ -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