size_t invasion

This commit is contained in:
Tiago Gomes 2012-05-24 22:55:20 +01:00
parent 5124098b6c
commit af6601837c
36 changed files with 513 additions and 546 deletions

View File

@ -16,7 +16,7 @@ BayesBall::getMinimalFactorGraph (const VarIds& queryIds)
{
assert (fg_.isFromBayesNetwork());
Scheduling scheduling;
for (unsigned i = 0; i < queryIds.size(); i++) {
for (size_t i = 0; i < queryIds.size(); i++) {
assert (dag_.getNode (queryIds[i]));
DAGraphNode* n = dag_.getNode (queryIds[i]);
scheduling.push (ScheduleInfo (n, false, true));
@ -60,7 +60,7 @@ void
BayesBall::constructGraph (FactorGraph* fg) const
{
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 (
facNodes[i]->factor().argument (0));
if (n->isMarkedOnTop()) {
@ -74,7 +74,7 @@ BayesBall::constructGraph (FactorGraph* fg) const
}
}
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()) {
VarNode* vn = fg->getVarNode (varNodes[i]->varId());
if (vn) {

View File

@ -57,7 +57,7 @@ DAGraph::getNode (VarId vid)
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);
}
}
@ -67,7 +67,7 @@ DAGraph::setIndexes (void)
void
DAGraph::clear (void)
{
for (unsigned i = 0; i < nodes_.size(); i++) {
for (size_t i = 0; i < nodes_.size(); i++) {
nodes_[i]->clear();
}
}
@ -85,7 +85,7 @@ DAGraph::exportToGraphViz (const char* fileName)
}
out << "digraph {" << 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 << " [" ;
out << "label=\"" << nodes_[i]->label() << "\"" ;
@ -94,9 +94,9 @@ DAGraph::exportToGraphViz (const char* fileName)
}
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();
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 << " [style=bold]" << endl ;
}

View File

@ -22,13 +22,13 @@ BpSolver::BpSolver (const FactorGraph& fg) : Solver (fg)
BpSolver::~BpSolver (void)
{
for (unsigned i = 0; i < varsI_.size(); i++) {
for (size_t i = 0; i < varsI_.size(); i++) {
delete varsI_[i];
}
for (unsigned i = 0; i < facsI_.size(); i++) {
for (size_t i = 0; i < facsI_.size(); i++) {
delete facsI_[i];
}
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
delete links_[i];
}
}
@ -86,13 +86,13 @@ BpSolver::getPosterioriOf (VarId vid)
probs.resize (var->range(), LogAware::multIdenty());
const SpLinkSet& links = ninf(var)->getLinks();
if (Globals::logDomain) {
for (unsigned i = 0; i < links.size(); i++) {
for (size_t i = 0; i < links.size(); i++) {
probs += links[i]->getMessage();
}
LogAware::normalize (probs);
Util::exp (probs);
} else {
for (unsigned i = 0; i < links.size(); i++) {
for (size_t i = 0; i < links.size(); i++) {
probs *= links[i]->getMessage();
}
LogAware::normalize (probs);
@ -109,21 +109,21 @@ BpSolver::getJointDistributionOf (const VarIds& jointVarIds)
if (runned_ == false) {
runSolver();
}
int idx = -1;
VarNode* vn = fg_->getVarNode (jointVarIds[0]);
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)) {
idx = i;
break;
}
}
if (idx == -1) {
if (idx == facNodes.size()) {
return getJointByConditioning (jointVarIds);
} else {
Factor res (facNodes[idx]->factor());
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()},
{links[i]->getVariable()->range()},
getVar2FactorMsg (links[i]));
@ -157,15 +157,15 @@ BpSolver::runSolver (void)
random_shuffle (links_.begin(), links_.end());
// no break
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]);
}
break;
case BpOptions::Schedule::PARALLEL:
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
calculateMessage (links_[i]);
}
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
updateMessage(links_[i]);
}
break;
@ -193,9 +193,9 @@ void
BpSolver::createLinks (void)
{
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();
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]));
}
}
@ -207,7 +207,7 @@ void
BpSolver::maxResidualSchedule (void)
{
if (nIters_ == 1) {
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
calculateMessage (links_[i]);
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
linkMap_.insert (make_pair (links_[i], it));
@ -215,7 +215,7 @@ BpSolver::maxResidualSchedule (void)
return;
}
for (unsigned c = 0; c < links_.size(); c++) {
for (size_t c = 0; c < links_.size(); c++) {
if (Globals::verbosity > 1) {
cout << "current residuals:" << endl;
for (SortedOrder::iterator it = sortedOrder_.begin();
@ -237,10 +237,10 @@ BpSolver::maxResidualSchedule (void)
// update the messages that depend on message source --> destin
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()) {
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()) {
calculateMessage (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
// to factor `src', except from var `dst'
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();
}
unsigned repetitions = 1;
@ -357,7 +357,7 @@ BpSolver::getVar2FactorMsg (const SpLink* link) const
}
msg -= link->getMessage();
} else {
for (unsigned i = 0; i < links.size(); i++) {
for (size_t i = 0; i < links.size(); i++) {
if (links[i]->getFactor() != dst) {
msg *= links[i]->getMessage();
if (Constants::SHOW_BP_CALCS) {
@ -378,7 +378,7 @@ Params
BpSolver::getJointByConditioning (const VarIds& jointVarIds) const
{
VarNodes jointVars;
for (unsigned i = 0; i < jointVarIds.size(); i++) {
for (size_t i = 0; i < jointVarIds.size(); i++) {
assert (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()};
for (unsigned i = 1; i < jointVarIds.size(); i++) {
for (size_t i = 1; i < jointVarIds.size(); i++) {
assert (jointVars[i]->hasEvidence() == false);
Params newBeliefs;
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]));
observedRanges.push_back (observedVars.back()->range());
}
StatesIndexer idx (observedVars, false);
StatesIndexer idx (observedRanges, false);
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]);
}
++ idx;
BpSolver solver (*fg);
solver.runSolver();
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]);
}
}
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) {
count ++;
}
@ -431,16 +433,16 @@ BpSolver::initializeSolver (void)
{
const VarNodes& varNodes = fg_->varNodes();
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());
}
const FacNodes& facNodes = fg_->facNodes();
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());
}
createLinks();
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
FacNode* src = links_[i]->getFactor();
VarNode* dst = links_[i]->getVariable();
ninf (dst)->addSpLink (links_[i]);
@ -477,7 +479,7 @@ BpSolver::converged (void)
converged = true;
}
} else {
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
double residual = links_[i]->getResidual();
if (Globals::verbosity > 1) {
cout << links_[i]->toString() + " residual = " << residual << endl;
@ -501,7 +503,7 @@ BpSolver::converged (void)
void
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];
cout << l->toString() << ":" << endl;
cout << " curr msg = " ;

View File

@ -16,10 +16,10 @@ CFactorGraph::CFactorGraph (const FactorGraph& fg)
CFactorGraph::~CFactorGraph (void)
{
for (unsigned i = 0; i < varClusters_.size(); i++) {
for (size_t i = 0; i < varClusters_.size(); i++) {
delete varClusters_[i];
}
for (unsigned i = 0; i < facClusters_.size(); i++) {
for (size_t i = 0; i < facClusters_.size(); i++) {
delete facClusters_[i];
}
}
@ -29,21 +29,22 @@ CFactorGraph::~CFactorGraph (void)
void
CFactorGraph::findIdenticalFactors()
{
if (checkForIdenticalFactors == false) {
const FacNodes& facNodes = groundFg_->facNodes();
if (checkForIdenticalFactors == false ||
facNodes.size() == 1) {
return;
}
const FacNodes& facNodes = groundFg_->facNodes();
for (unsigned i = 0; i < facNodes.size(); i++) {
for (size_t i = 0; i < facNodes.size(); i++) {
facNodes[i]->factor().setDistId (Util::maxUnsigned());
}
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();
if (f1.distId() != Util::maxUnsigned()) {
continue;
}
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();
if (f2.distId() != Util::maxUnsigned()) {
continue;
@ -69,19 +70,16 @@ CFactorGraph::setInitialColors (void)
// create the initial variable colors
VarColorMap colorMap;
const VarNodes& varNodes = groundFg_->varNodes();
for (unsigned i = 0; i < varNodes.size(); i++) {
unsigned dsize = varNodes[i]->range();
VarColorMap::iterator it = colorMap.find (dsize);
for (size_t i = 0; i < varNodes.size(); i++) {
unsigned range = varNodes[i]->range();
VarColorMap::iterator it = colorMap.find (range);
if (it == colorMap.end()) {
it = colorMap.insert (make_pair (
dsize, Colors (dsize+1,-1))).first;
}
unsigned idx;
if (varNodes[i]->hasEvidence()) {
idx = varNodes[i]->getEvidence();
} else {
idx = dsize;
range, Colors (range + 1, -1))).first;
}
unsigned idx = varNodes[i]->hasEvidence()
? varNodes[i]->getEvidence()
: range;
Colors& stateColors = it->second;
if (stateColors[idx] == -1) {
stateColors[idx] = getFreeColor();
@ -91,7 +89,7 @@ CFactorGraph::setInitialColors (void)
const FacNodes& facNodes = groundFg_->facNodes();
// create the initial factor colors
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();
DistColorMap::iterator it = distColors.find (distId);
if (it == distColors.end()) {
@ -117,9 +115,9 @@ CFactorGraph::createGroups (void)
nIters ++;
// set a new color to the variables with the same signature
unsigned prevVarGroupsSize = varGroups.size();
size_t prevVarGroupsSize = varGroups.size();
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]);
VarSignMap::iterator it = varGroups.find (signature);
if (it == varGroups.end()) {
@ -131,15 +129,15 @@ CFactorGraph::createGroups (void)
it != varGroups.end(); it++) {
Color newColor = getFreeColor();
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);
}
}
unsigned prevFactorGroupsSize = facGroups.size();
size_t prevFactorGroupsSize = facGroups.size();
facGroups.clear();
// 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]);
FacSignMap::iterator it = facGroups.find (signature);
if (it == facGroups.end()) {
@ -151,7 +149,7 @@ CFactorGraph::createGroups (void)
it != facGroups.end(); it++) {
Color newColor = getFreeColor();
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);
}
}
@ -175,7 +173,7 @@ CFactorGraph::createClusters (
it != varGroups.end(); it++) {
const VarNodes& groupVars = it->second;
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));
}
varClusters_.push_back (vc);
@ -188,7 +186,7 @@ CFactorGraph::createClusters (
const VarNodes& neighs = groupFactor->neighbors();
VarClusters varClusters;
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();
varClusters.push_back (vid2VarCluster_.find (vid)->second);
}
@ -204,7 +202,7 @@ CFactorGraph::getSignature (const VarNode* varNode)
const FacNodes& neighs = varNode->neighbors();
VarSignature sign;
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 (
getColor (neighs[i]),
neighs[i]->factor().indexOf (varNode->varId())));
@ -222,7 +220,7 @@ CFactorGraph::getSignature (const FacNode* facNode)
const VarNodes& neighs = facNode->neighbors();
FacSignature sign;
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 (facNode));
@ -235,15 +233,15 @@ FactorGraph*
CFactorGraph::getGroundFactorGraph (void)
{
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());
varClusters_[i]->setRepresentative (newVar);
fg->addVarNode (newVar);
}
for (unsigned i = 0; i < facClusters_.size(); i++) {
for (size_t i = 0; i < facClusters_.size(); i++) {
Vars vars;
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());
}
const Factor& groundFac = facClusters_[i]->first()->factor();
@ -251,7 +249,7 @@ CFactorGraph::getGroundFactorGraph (void)
vars, groundFac.params(), groundFac.distId()));
facClusters_[i]->setRepresentative (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);
}
}
@ -264,17 +262,17 @@ unsigned
CFactorGraph::getEdgeCount (
const FacCluster* fc,
const VarCluster* vc,
unsigned index) const
size_t index) const
{
unsigned count = 0;
VarId reprVid = vc->representative()->varId();
VarNode* groundVar = groundFg_->getVarNode (reprVid);
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;
it = std::find (fc->members().begin(), fc->members().end(), neighs[i]);
if (it != fc->members().end() &&
(*it)->factor().indexOf (reprVid) == (int)index) {
(*it)->factor().indexOf (reprVid) == index) {
count ++;
}
}
@ -295,7 +293,7 @@ CFactorGraph::printGroups (
const VarNodes& groupMembers = it->second;
if (groupMembers.size() > 0) {
cout << count << ": " ;
for (unsigned i = 0; i < groupMembers.size(); i++) {
for (size_t i = 0; i < groupMembers.size(); i++) {
cout << groupMembers[i]->label() << " " ;
}
count ++;
@ -309,7 +307,7 @@ CFactorGraph::printGroups (
const FacNodes& groupMembers = it->second;
if (groupMembers.size() > 0) {
cout << ++count << ": " ;
for (unsigned i = 0; i < groupMembers.size(); i++) {
for (size_t i = 0; i < groupMembers.size(); i++) {
cout << groupMembers[i]->getLabel() << " " ;
}
count ++;

View File

@ -35,7 +35,7 @@ struct VarSignatureHash
size_t operator() (const VarSignature &sig) const
{
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].second);
}
@ -49,7 +49,7 @@ struct FacSignatureHash
size_t operator() (const FacSignature &sig) const
{
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]);
}
return val;
@ -120,7 +120,7 @@ class CFactorGraph
FactorGraph* getGroundFactorGraph (void);
unsigned getEdgeCount (const FacCluster*,
const VarCluster*, unsigned index) const;
const VarCluster*, size_t index) const;
static bool checkForIdenticalFactors;

View File

@ -13,7 +13,7 @@ CbpSolver::~CbpSolver (void)
{
delete cfg_;
delete fg_;
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
delete links_[i];
}
links_.clear();
@ -61,14 +61,14 @@ CbpSolver::getPosterioriOf (VarId vid)
probs.resize (var->range(), LogAware::multIdenty());
const SpLinkSet& links = ninf(var)->getLinks();
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]);
probs += l->poweredMessage();
}
LogAware::normalize (probs);
Util::exp (probs);
} 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]);
probs *= l->poweredMessage();
}
@ -84,7 +84,7 @@ Params
CbpSolver::getJointDistributionOf (const VarIds& jointVids)
{
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]);
eqVarIds.push_back (vn->varId());
}
@ -103,9 +103,9 @@ CbpSolver::createLinks (void)
cout << endl;
}
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();
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);
if (Globals::verbosity > 1) {
cout << "creating link " ;
@ -129,7 +129,7 @@ void
CbpSolver::maxResidualSchedule (void)
{
if (nIters_ == 1) {
for (unsigned i = 0; i < links_.size(); i++) {
for (size_t i = 0; i < links_.size(); i++) {
calculateMessage (links_[i]);
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
linkMap_.insert (make_pair (links_[i], it));
@ -140,7 +140,7 @@ CbpSolver::maxResidualSchedule (void)
return;
}
for (unsigned c = 0; c < links_.size(); c++) {
for (size_t c = 0; c < links_.size(); c++) {
if (Globals::verbosity > 1) {
cout << endl << "current residuals:" << endl;
for (SortedOrder::iterator it = sortedOrder_.begin();
@ -165,9 +165,9 @@ CbpSolver::maxResidualSchedule (void)
// update the messages that depend on message source --> destin
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();
for (unsigned j = 0; j < links.size(); j++) {
for (size_t j = 0; j < links.size(); j++) {
if (links[j]->getVariable() != link->getVariable()) {
if (Globals::verbosity > 1) {
cout << " calculating " << links[j]->toString() << endl;
@ -182,7 +182,7 @@ CbpSolver::maxResidualSchedule (void)
// in counting bp, the message that a variable X sends to
// to a factor F depends on the message that F sent to the X
const SpLinkSet& links = ninf(link->getFactor())->getLinks();
for (unsigned i = 0; i < links.size(); i++) {
for (size_t i = 0; i < links.size(); i++) {
if (links[i]->getVariable() != link->getVariable()) {
if (Globals::verbosity > 1) {
cout << " calculating " << links[i]->toString() << endl;
@ -208,7 +208,7 @@ CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
// calculate the product of messages that were sent
// to factor `src', except from var `dst'
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();
}
unsigned repetitions = 1;
@ -256,11 +256,11 @@ CbpSolver::calculateFactor2VariableMsg (SpLink* _link)
src->factor().ranges(), msgProduct);
assert (msgProduct.size() == src->factor().size());
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];
}
} else {
for (unsigned i = 0; i < result.size(); i++) {
for (size_t i = 0; i < result.size(); i++) {
result[i] *= src->factor()[i];
}
}
@ -307,7 +307,7 @@ CbpSolver::getVar2FactorMsg (const SpLink* _link) const
}
const SpLinkSet& links = ninf(src)->getLinks();
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]);
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
CbpSolverLink* cl = static_cast<CbpSolverLink*> (links[i]);
@ -315,7 +315,7 @@ CbpSolver::getVar2FactorMsg (const SpLink* _link) const
}
}
} 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]);
if ( ! (cl->getFactor() == dst && cl->index() == link->index())) {
msg *= cl->poweredMessage();
@ -336,7 +336,7 @@ CbpSolver::getVar2FactorMsg (const SpLink* _link) const
void
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]);
cout << cl->toString() << ":" << endl;
cout << " curr msg = " << cl->getMessage() << endl;

View File

@ -9,13 +9,13 @@ class Factor;
class CbpSolverLink : public SpLink
{
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),
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_; }
@ -28,7 +28,7 @@ class CbpSolverLink : public SpLink
}
private:
unsigned index_;
size_t index_;
unsigned nrEdges_;
Params pwdMsg_;
};

View File

@ -178,7 +178,7 @@ ConstraintTree::ConstraintTree (
root_ = new CTNode (0, 0);
logVars_ = logVars;
logVarSet_ = LogVarSet (logVars);
for (unsigned i = 0; i < tuples.size(); i++) {
for (size_t i = 0; i < tuples.size(); i++) {
addTuple (tuples[i]);
}
}
@ -205,7 +205,7 @@ void
ConstraintTree::addTuple (const Tuple& tuple)
{
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]);
if (it == prevNode->childs().end()) {
CTNode* newNode = new CTNode (tuple[i], i + 1);
@ -223,7 +223,7 @@ bool
ConstraintTree::containsTuple (const Tuple& tuple)
{
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]);
if (it == prevNode->childs().end()) {
return false;
@ -239,11 +239,11 @@ ConstraintTree::containsTuple (const Tuple& tuple)
void
ConstraintTree::moveToTop (const LogVars& lvs)
{
for (unsigned i = 0; i < lvs.size(); i++) {
int pos = Util::indexOf (logVars_, lvs[i]);
assert (pos != -1);
for (int j = pos; j > (int)i; j--) {
swapLogVar (logVars_[j-1]);
for (size_t i = 0; i < lvs.size(); i++) {
size_t pos = Util::indexOf (logVars_, lvs[i]);
assert (pos != logVars_.size());
for (size_t j = pos; j-- > i; ) {
swapLogVar (logVars_[j]);
}
}
}
@ -253,13 +253,11 @@ ConstraintTree::moveToTop (const LogVars& lvs)
void
ConstraintTree::moveToBottom (const LogVars& lvs)
{
for (int i = lvs.size() - 1; i >= 0; i--) {
LogVars::iterator it =
std::find (logVars_.begin(), logVars_.end(), lvs[i]);
assert (it != logVars_.end());
int pos = Util::indexOf (logVars_, lvs[i]);
int stop = logVars_.size() - (lvs.size() - i - 1);
for (int j = pos; j < stop - 1; j++) {
for (size_t i = lvs.size(); i-- > 0; ) {
size_t pos = Util::indexOf (logVars_, lvs[i]);
assert (pos != logVars_.size());
size_t stop = logVars_.size() - (lvs.size() - i - 1);
for (size_t j = pos; j < stop - 1; j++) {
swapLogVar (logVars_[j]);
}
}
@ -291,7 +289,7 @@ ConstraintTree::join (ConstraintTree* ct, bool oneTwoOne)
}
LogVarSet intersect = logVarSet_ & ct->logVarSet_;
if (intersect.empty()) {
// carteesian product
// cartesian product
appendOnBottom (root_, ct->root()->childs());
Util::addToVector (logVars_, ct->logVars_);
logVarSet_ |= ct->logVarSet_;
@ -305,7 +303,7 @@ ConstraintTree::join (ConstraintTree* ct, bool oneTwoOne)
tuples, appendNodes);
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);
if (oneTwoOne && tupleFounded == false) {
assert (false);
@ -338,7 +336,7 @@ ConstraintTree::rename (LogVar X_old, LogVar X_new)
assert (logVarSet_.contains (X_new) == false);
logVarSet_ -= X_old;
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) {
logVars_[i] = X_new;
return;
@ -352,7 +350,7 @@ ConstraintTree::rename (LogVar X_old, LogVar X_new)
void
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]);
}
logVarSet_ = LogVarSet (logVars_);
@ -421,7 +419,7 @@ LogVarSet
ConstraintTree::singletons (void)
{
LogVarSet singletons;
for (unsigned i = 0; i < logVars_.size(); i++) {
for (size_t i = 0; i < logVars_.size(); i++) {
if (isSingleton (logVars_[i])) {
singletons.insert (logVars_[i]);
}
@ -449,7 +447,7 @@ TupleSet
ConstraintTree::tupleSet (const LogVars& originalLvs)
{
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) {
uniqueLvs.push_back (originalLvs[i]);
}
@ -461,17 +459,17 @@ ConstraintTree::tupleSet (const LogVars& originalLvs)
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
if (originalLvs.size() != uniqueLvs.size()) {
vector<int> indexes;
vector<size_t> indexes;
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]));
}
Tuples tuples2;
tuples2.reserve (tuples.size());
for (unsigned i = 0; i < tuples.size(); i++) {
for (size_t i = 0; i < tuples.size(); i++) {
Tuple t;
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]]);
}
tuples2.push_back (t);
@ -518,14 +516,14 @@ ConstraintTree::exportToGraphViz (
}
if (showLogVars) {
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] << ", " ;
out << "shape=plaintext, fontsize=14]" << endl;
}
out << "Root -> " << copy.logVars_[0];
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 << " [style=invis]" << endl;
}
@ -618,9 +616,9 @@ ConstraintTree::isCartesianProduct (const LogVarSet& Xs)
return true;
}
moveToTop (Xs.elements());
for (unsigned i = 1; i < Xs.size(); i++) {
for (size_t i = 1; i < Xs.size(); 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()) {
return false;
}
@ -689,7 +687,7 @@ ConstraintTree::countNormalize (const LogVarSet& Ys)
chIt != childs.end(); ++ chIt) {
const vector<pair<CTNode*, unsigned>>& res =
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
= countMap.find (res[j].second);
if (it == countMap.end()) {
@ -725,7 +723,7 @@ ConstraintTree::jointCountNormalize (
ConstraintTrees normCts1 = commCt->countNormalize (X);
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);
// cout << "normCts1[" << i << "] #" << counts1[i] ;
// cout << " " << normCts1[i]->tupleSet() << endl;
@ -733,7 +731,7 @@ ConstraintTree::jointCountNormalize (
ConstraintTrees normCts2 = exclCt->countNormalize (X);
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);
// cout << "normCts2[" << i << "] #" << counts2[i] ;
// cout << " " << normCts2[i]->tupleSet() << endl;
@ -741,7 +739,7 @@ ConstraintTree::jointCountNormalize (
// cout << endl;
ConstraintTree* excl1 = 0;
for (unsigned i = 0; i < normCts1.size(); i++) {
for (size_t i = 0; i < normCts1.size(); i++) {
if (counts1[i] == N) {
excl1 = normCts1[i];
normCts1.erase (normCts1.begin() + i);
@ -752,7 +750,7 @@ ConstraintTree::jointCountNormalize (
}
ConstraintTree* excl2 = 0;
for (unsigned i = 0; i < normCts2.size(); i++) {
for (size_t i = 0; i < normCts2.size(); i++) {
if (counts2[i] == N) {
excl2 = normCts2[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;
for (j = 0; counts1[i] + counts2[j] != N; j++) ;
// cout << "joint-count(" << counts1[i] ;
@ -778,7 +776,7 @@ ConstraintTree::jointCountNormalize (
ConstraintTrees cts = normCts1;
commCt->rename (X, X_new1);
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]->join (commCt);
cts[i]->join (exclCt);
@ -814,7 +812,7 @@ ConstraintTree::expand (LogVar X)
(*it)->removeAndDeleteAllChilds();
CTNode* prev = *it;
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);
prev->mergeSubtree (newNode);
prev = newNode;
@ -822,7 +820,7 @@ ConstraintTree::expand (LogVar X)
}
LogVars newLvs;
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));
newLvs.push_back (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});
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 (
new CTNode (leafs[i]->symbol(), leafs[i]->level() + 1));
}
@ -972,8 +970,8 @@ ConstraintTree::appendOnBottom (CTNode* n, const CTChilds& childs)
void
ConstraintTree::swapLogVar (LogVar X)
{
int pos = Util::indexOf (logVars_, X);
assert (pos != -1);
size_t pos = Util::indexOf (logVars_, X);
assert (pos != logVars_.size());
const CTNodes& nodes = getNodesAtLevel (pos);
for (CTNodes::const_iterator nodeIt = nodes.begin();
nodeIt != nodes.end(); ++ nodeIt) {
@ -1003,7 +1001,7 @@ bool
ConstraintTree::join (
CTNode* currNode,
const Tuple& tuple,
unsigned currIdx,
size_t currIdx,
CTNode* appendNode)
{
bool tupleFounded = false;
@ -1033,12 +1031,12 @@ ConstraintTree::getTuples (
if (currTuples.size() == 0) {
currTuples.push_back ({ n->symbol()});
} else {
for (unsigned i = 0; i < currTuples.size(); i++) {
for (size_t i = 0; i < currTuples.size(); i++) {
currTuples[i].push_back (n->symbol());
}
}
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]);
continuationNodes.push_back (n);
}
@ -1088,7 +1086,7 @@ ConstraintTree::countNormalize (
chIt != childs.end(); ++ chIt) {
const vector<pair<CTNode*, unsigned>>& lowerRes =
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);
newNode->mergeSubtree (lowerRes[j].first);
res.push_back (make_pair (newNode, lowerRes[j].second));

View File

@ -58,7 +58,7 @@ class CTNode
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; }
@ -134,7 +134,7 @@ class ConstraintTree
return logVarSet_;
}
unsigned nrLogVars (void) const
size_t nrLogVars (void) const
{
return logVars_.size();
assert (LogVarSet (logVars_) == logVarSet_);
@ -211,7 +211,7 @@ class ConstraintTree
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;

View File

@ -9,18 +9,18 @@ ElimHeuristic ElimGraph::elimHeuristic = MIN_NEIGHBORS;
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
continue;
}
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]);
if (n1 == 0) {
n1 = new EgNode (vids[j], factors[i]->range (j));
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]);
if (n2 == 0) {
n2 = new EgNode (vids[k], factors[i]->range (k));
@ -43,7 +43,7 @@ ElimGraph::ElimGraph (const vector<Factor*>& factors)
ElimGraph::~ElimGraph (void)
{
for (unsigned i = 0; i < nodes_.size(); i++) {
for (size_t i = 0; i < nodes_.size(); i++) {
delete nodes_[i];
}
}
@ -55,17 +55,17 @@ ElimGraph::getEliminatingOrder (const VarIds& exclude)
{
VarIds elimOrder;
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) {
unmarked_.insert (nodes_[i]);
}
}
unsigned nVarsToEliminate = nodes_.size() - exclude.size();
for (unsigned i = 0; i < nVarsToEliminate; i++) {
size_t nrVarsToEliminate = nodes_.size() - exclude.size();
for (size_t i = 0; i < nrVarsToEliminate; i++) {
EgNode* node = getLowestCostNode();
unmarked_.remove (node);
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);
}
elimOrder.push_back (node->varId());
@ -79,10 +79,10 @@ ElimGraph::getEliminatingOrder (const VarIds& exclude)
void
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:" ;
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 << endl;
@ -106,13 +106,13 @@ ElimGraph::exportToGraphViz (
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) {
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]);
if (node) {
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();
for (unsigned j = 0; j < neighs.size(); j++) {
for (size_t j = 0; j < neighs.size(); j++) {
out << '"' << nodes_[i]->label() << '"' << " -- " ;
out << '"' << neighs[j]->label() << '"' << endl;
}
@ -210,8 +210,8 @@ ElimGraph::connectAllNeighbors (const EgNode* n)
{
const EGNeighs& neighs = n->neighbors();
if (neighs.size() > 0) {
for (unsigned i = 0; i < neighs.size() - 1; i++) {
for (unsigned j = i+1; j < neighs.size(); j++) {
for (size_t i = 0; i < neighs.size() - 1; i++) {
for (size_t j = i + 1; j < neighs.size(); j++) {
if ( ! neighbors (neighs[i], neighs[j])) {
addEdge (neighs[i], neighs[j]);
}

View File

@ -78,7 +78,7 @@ class ElimGraph
{
unsigned cost = 1;
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();
}
return cost;
@ -89,8 +89,8 @@ class ElimGraph
unsigned cost = 0;
const EGNeighs& neighs = n->neighbors();
if (neighs.size() > 0) {
for (unsigned i = 0; i < neighs.size() - 1; i++) {
for (unsigned j = i + 1; j < neighs.size(); j++) {
for (size_t i = 0; i < neighs.size() - 1; i++) {
for (size_t j = i + 1; j < neighs.size(); j++) {
if ( ! neighbors (neighs[i], neighs[j])) {
cost ++;
}
@ -105,8 +105,8 @@ class ElimGraph
unsigned cost = 0;
const EGNeighs& neighs = n->neighbors();
if (neighs.size() > 0) {
for (unsigned i = 0; i < neighs.size() - 1; i++) {
for (unsigned j = i+1; j < neighs.size(); j++) {
for (size_t i = 0; i < neighs.size() - 1; i++) {
for (size_t j = i + 1; j < neighs.size(); j++) {
if ( ! neighbors (neighs[i], neighs[j])) {
cost += neighs[i]->range() * neighs[j]->range();
}

View File

@ -37,7 +37,7 @@ Factor::Factor (
const Params& params,
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());
ranges_.push_back (vars[i]->range());
}
@ -51,8 +51,6 @@ Factor::Factor (
void
Factor::sumOut (VarId vid)
{
int idx = indexOf (vid);
assert (idx != -1);
if (vid == args_.back()) {
sumOutLastVariable(); // optimization
return;
@ -61,7 +59,8 @@ Factor::sumOut (VarId vid)
sumOutFirstVariable(); // optimization
return;
}
sumOutIndex (idx);
assert (indexOf (vid) != args_.size());
sumOutIndex (indexOf (vid));
}
@ -69,7 +68,7 @@ Factor::sumOut (VarId vid)
void
Factor::sumOutAllExcept (VarId vid)
{
assert (indexOf (vid) != -1);
assert (indexOf (vid) != args_.size());
while (args_.back() != vid) {
sumOutLastVariable();
}
@ -94,7 +93,7 @@ Factor::sumOutAllExcept (const VarIds& vids)
void
Factor::sumOutIndex (unsigned idx)
Factor::sumOutIndex (size_t idx)
{
assert (idx < args_.size());
// number of parameters separating a different state of `var',
@ -149,13 +148,13 @@ Factor::sumOutIndex (unsigned idx)
void
Factor::sumOutAllExceptIndex (unsigned idx)
Factor::sumOutAllExceptIndex (size_t idx)
{
assert (idx < args_.size());
while (args_.size() > idx + 1) {
sumOutLastVariable();
}
for (unsigned i = 0; i < idx; i++) {
for (size_t i = 0; i < idx; i++) {
sumOutFirstVariable();
}
}
@ -169,11 +168,11 @@ Factor::sumOutFirstVariable (void)
unsigned range = ranges_.front();
unsigned sep = params_.size() / range;
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]);
}
} else {
for (unsigned i = sep; i < params_.size(); i++) {
for (size_t i = sep; i < params_.size(); i++) {
params_[i % sep] += params_[i];
}
}
@ -188,9 +187,9 @@ void
Factor::sumOutLastVariable (void)
{
assert (args_.size() > 1);
size_t idx1 = 0;
size_t idx2 = 0;
unsigned range = ranges_.back();
unsigned idx1 = 0;
unsigned idx2 = 0;
if (Globals::logDomain) {
while (idx1 < params_.size()) {
params_[idx2] = params_[idx1];
@ -246,7 +245,7 @@ Factor::getLabel (void) const
{
stringstream ss;
ss << "f(" ;
for (unsigned i = 0; i < args_.size(); i++) {
for (size_t i = 0; i < args_.size(); i++) {
if (i != 0) ss << "," ;
ss << Var (args_[i], ranges_[i]).label();
}
@ -260,17 +259,17 @@ void
Factor::print (void) const
{
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]));
}
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 << "f(" << jointStrings[i] << ")" ;
cout << " = " << params_[i] << endl;
}
cout << endl;
for (unsigned i = 0; i < vars.size(); i++) {
for (size_t i = 0; i < vars.size(); i++) {
delete vars[i];
}
}

View File

@ -25,9 +25,9 @@ class TFactor
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_; }
@ -41,31 +41,24 @@ class TFactor
assert (params_.size() == Util::sizeExpected (ranges_));
}
int indexOf (const T& t) const
size_t indexOf (const T& t) const
{
int idx = -1;
for (unsigned i = 0; i < args_.size(); i++) {
if (args_[i] == t) {
idx = i;
break;
}
}
return idx;
return Util::indexOf (args_, t);
}
const T& argument (unsigned idx) const
const T& argument (size_t idx) const
{
assert (idx < args_.size());
return args_[idx];
}
T& argument (unsigned idx)
T& argument (size_t idx)
{
assert (idx < args_.size());
return args_[idx];
}
unsigned range (unsigned idx) const
unsigned range (size_t idx) const
{
assert (idx < ranges_.size());
return ranges_[idx];
@ -83,10 +76,10 @@ class TFactor
: params_ *= g_params;
} else {
bool sharedArgs = false;
vector<unsigned> gvarpos;
for (unsigned i = 0; i < g_args.size(); i++) {
int idx = indexOf (g_args[i]);
if (idx == -1) {
vector<size_t> gvarpos;
for (size_t i = 0; i < g_args.size(); i++) {
size_t idx = indexOf (g_args[i]);
if (idx == g_args.size()) {
ullong newSize = params_.size() * g_ranges[i];
if (newSize > params_.max_size()) {
cerr << "error: an overflow occurred on factor multiplication" ;
@ -103,8 +96,8 @@ class TFactor
if (sharedArgs == false) {
// optimization: if the original factors doesn't have common args,
// we don't need to marry the states of the common args
unsigned count = 0;
for (unsigned i = 0; i < params_.size(); i++) {
size_t count = 0;
for (size_t i = 0; i < params_.size(); i++) {
if (Globals::logDomain) {
params_[i] += g_params[count];
} else {
@ -118,9 +111,9 @@ class TFactor
} else {
StatesIndexer indexer (ranges_, false);
while (indexer.valid()) {
unsigned g_li = 0;
unsigned prod = 1;
for (int j = gvarpos.size() - 1; j >= 0; j--) {
size_t g_li = 0;
size_t prod = 1;
for (size_t j = gvarpos.size(); j-- > 0; ) {
g_li += indexer[gvarpos[j]] * prod;
prod *= g_ranges[j];
}
@ -137,14 +130,14 @@ class TFactor
void absorveEvidence (const T& arg, unsigned evidence)
{
int idx = indexOf (arg);
assert (idx != -1);
size_t idx = indexOf (arg);
assert (idx != args_.size());
assert (evidence < ranges_[idx]);
Params copy = params_;
params_.clear();
params_.reserve (copy.size() / ranges_[idx]);
StatesIndexer indexer (ranges_);
for (unsigned i = 0; i < evidence; i++) {
for (size_t i = 0; i < evidence; i++) {
indexer.increment (idx);
}
while (indexer.valid()) {
@ -162,16 +155,16 @@ class TFactor
return; // already in the wanted order
}
Ranges newRanges;
vector<unsigned> positions;
for (unsigned i = 0; i < newArgs.size(); i++) {
unsigned idx = indexOf (newArgs[i]);
vector<size_t> positions;
for (size_t i = 0; i < newArgs.size(); i++) {
size_t idx = indexOf (newArgs[i]);
newRanges.push_back (ranges_[idx]);
positions.push_back (idx);
}
unsigned N = ranges_.size();
size_t N = ranges_.size();
Params newParams (params_.size());
for (unsigned i = 0; i < params_.size(); i++) {
unsigned li = i;
for (size_t i = 0; i < params_.size(); i++) {
size_t li = i;
// calculate vector index corresponding to linear index
vector<unsigned> vi (N);
for (int k = N-1; k >= 0; k--) {
@ -179,17 +172,17 @@ class TFactor
li /= ranges_[k];
}
// convert permuted vector index to corresponding linear index
unsigned prod = 1;
unsigned new_li = 0;
size_t prod = 1;
size_t new_li = 0;
for (int k = N - 1; k >= 0; k--) {
new_li += vi[positions[k]] * prod;
prod *= ranges_[positions[k]];
}
newParams[new_li] = params_[i];
}
args_ = newArgs;
ranges_ = newRanges;
params_ = newParams;
args_ = newArgs;
ranges_ = newRanges;
params_ = newParams;
}
bool contains (const T& arg) const
@ -199,7 +192,7 @@ class TFactor
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) {
return false;
}
@ -207,7 +200,7 @@ class TFactor
return true;
}
double& operator[] (psize_t idx)
double& operator[] (size_t idx)
{
assert (idx < params_.size());
return params_[idx];
@ -223,11 +216,11 @@ class TFactor
private:
void insertArgument (const T& arg, unsigned range)
{
assert (indexOf (arg) == -1);
assert (indexOf (arg) == args_.size());
Params copy = params_;
params_.clear();
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++) {
params_.push_back (copy[i]);
}
@ -240,15 +233,15 @@ class TFactor
{
Params copy = params_;
unsigned nrStates = 1;
for (unsigned i = 0; i < args.size(); i++) {
assert (indexOf (args[i]) == -1);
for (size_t i = 0; i < args.size(); i++) {
assert (indexOf (args[i]) == args_.size());
args_.push_back (args[i]);
ranges_.push_back (ranges[i]);
nrStates *= ranges[i];
}
params_.clear();
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++) {
params_.push_back (copy[i]);
}
@ -277,9 +270,9 @@ class Factor : public TFactor<VarId>
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);

View File

@ -16,15 +16,15 @@
FactorGraph::FactorGraph (const FactorGraph& fg)
{
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]));
}
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());
addFacNode (facNode);
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);
}
}
@ -170,10 +170,10 @@ FactorGraph::readFromLibDaiFormat (const char* fileName)
FactorGraph::~FactorGraph (void)
{
for (unsigned i = 0; i < varNodes_.size(); i++) {
for (size_t i = 0; i < varNodes_.size(); i++) {
delete varNodes_[i];
}
for (unsigned i = 0; i < facNodes_.size(); i++) {
for (size_t i = 0; i < facNodes_.size(); i++) {
delete facNodes_[i];
}
}
@ -186,7 +186,7 @@ FactorGraph::addFactor (const Factor& factor)
FacNode* fn = new FacNode (factor);
addFacNode (fn);
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]);
if (it != varMap_.end()) {
addEdge (it->second, fn);
@ -241,12 +241,12 @@ FactorGraph::getStructure (void)
{
assert (fromBayesNet_);
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]));
}
for (unsigned i = 0; i < facNodes_.size(); i++) {
for (size_t i = 0; i < facNodes_.size(); i++) {
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]);
}
}
@ -259,18 +259,18 @@ FactorGraph::getStructure (void)
void
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 << "label = " << varNodes_[i]->label() << endl;
cout << "range = " << varNodes_[i]->range() << endl;
cout << "evidence = " << varNodes_[i]->getEvidence() << endl;
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 << endl << endl;
}
for (unsigned i = 0; i < facNodes_.size(); i++) {
for (size_t i = 0; i < facNodes_.size(); i++) {
facNodes_[i]->factor().print();
}
}
@ -287,20 +287,20 @@ FactorGraph::exportToGraphViz (const char* fileName) const
abort();
}
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()) {
out << '"' << varNodes_[i]->label() << '"' ;
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 << " [label=\"" << facNodes_[i]->getLabel();
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();
for (unsigned j = 0; j < myVars.size(); j++) {
for (size_t j = 0; j < myVars.size(); j++) {
out << '"' << facNodes_[i]->getLabel() << '"' ;
out << " -- " ;
out << '"' << myVars[j]->label() << '"' << endl;
@ -322,26 +322,26 @@ FactorGraph::exportToUaiFormat (const char* fileName) const
}
out << "MARKOV" << 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 << 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();
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 << endl;
}
for (unsigned i = 0; i < facNodes_.size(); i++) {
for (size_t i = 0; i < facNodes_.size(); i++) {
Params params = facNodes_[i]->factor().params();
if (Globals::logDomain) {
Util::exp (params);
}
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 << endl;
@ -360,14 +360,14 @@ FactorGraph::exportToLibDaiFormat (const char* fileName) const
abort();
}
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();
out << factorVars.size() << endl;
for (int j = factorVars.size() - 1; j >= 0; j--) {
out << factorVars[j]->varId() << " " ;
}
out << endl;
for (unsigned j = 0; j < factorVars.size(); j++) {
for (size_t j = 0; j < factorVars.size(); j++) {
out << factorVars[j]->range() << " " ;
}
out << endl;
@ -376,7 +376,7 @@ FactorGraph::exportToLibDaiFormat (const char* fileName) const
Util::exp (params);
}
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 << endl;
@ -402,7 +402,7 @@ FactorGraph::containsCycle (void) const
{
vector<bool> visitedVars (varNodes_.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();
if (!visitedVars[v]) {
if (containsCycle (varNodes_[i], 0, visitedVars, visitedFactors)) {
@ -424,7 +424,7 @@ FactorGraph::containsCycle (
{
visitedVars[v->getIndex()] = true;
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();
if (!visitedFactors[w]) {
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {
@ -449,7 +449,7 @@ FactorGraph::containsCycle (
{
visitedFactors[v->getIndex()] = true;
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();
if (!visitedVars[w]) {
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {

View File

@ -45,9 +45,9 @@ class FacNode
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(); }
@ -56,7 +56,7 @@ class FacNode
VarNodes neighs_;
Factor factor_;
int index_;
size_t index_;
};
@ -84,9 +84,9 @@ class FactorGraph
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
{

View File

@ -42,7 +42,7 @@ LiftedOperator::printValidOps (
{
vector<LiftedOperator*> validOps;
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();
delete validOps[i];
}
@ -148,13 +148,13 @@ ProductOperator::validOp (Parfactor* g1, Parfactor* g2)
TinySet<unsigned> g2_gs (g2->getAllGroups());
if (g1_gs.contains (g2_gs) || g2_gs.contains (g1_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 ||
g2->nrFormulasWithGroup (intersect[i]) != 1) {
return false;
}
int idx1 = g1->indexOfGroup (intersect[i]);
int idx2 = g2->indexOfGroup (intersect[i]);
size_t idx1 = g1->indexOfGroup (intersect[i]);
size_t idx2 = g2->indexOfGroup (intersect[i]);
if (g1->range (idx1) != g2->range (idx2)) {
return false;
}
@ -185,11 +185,11 @@ SumOutOperator::getLogCost (void)
return std::log (0.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();
while (pfIter != pfList_.end()) {
if ((*pfIter)->containsGroup (groupSet[i])) {
int idx = (*pfIter)->indexOfGroup (groupSet[i]);
size_t idx = (*pfIter)->indexOfGroup (groupSet[i]);
cost *= (*pfIter)->range (idx);
break;
}
@ -208,7 +208,7 @@ SumOutOperator::apply (void)
iters = getParfactorsWithGroup (pfList_, group_);
Parfactor* product = *(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]));
pfList_.removeAndDelete (iters[i]);
}
@ -216,14 +216,14 @@ SumOutOperator::apply (void)
delete product;
return;
}
int fIdx = product->indexOfGroup (group_);
size_t fIdx = product->indexOfGroup (group_);
LogVarSet excl = product->exclusiveLogVars (fIdx);
if (product->constr()->isCountNormalized (excl)) {
product->sumOut (fIdx);
pfList_.addShattered (product);
} else {
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);
pfList_.add (pfs[i]);
}
@ -243,7 +243,7 @@ SumOutOperator::getValidOps (
ParfactorList::const_iterator it = pfList.begin();
while (it != pfList.end()) {
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());
}
++ it;
@ -266,7 +266,7 @@ SumOutOperator::toString (void)
stringstream ss;
vector<ParfactorList::iterator> pfIters;
pfIters = getParfactorsWithGroup (pfList_, group_);
int idx = (*pfIters[0])->indexOfGroup (group_);
size_t idx = (*pfIters[0])->indexOfGroup (group_);
ProbFormula f = (*pfIters[0])->argument (idx);
TupleSet tupleSet = (*pfIters[0])->constr()->tupleSet (f.logVars());
ss << "sum out " << f.functor() << "/" << f.arity();
@ -289,11 +289,11 @@ SumOutOperator::validOp (
return false;
}
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) {
return false;
}
int fIdx = (*pfIters[i])->indexOfGroup (group);
size_t fIdx = (*pfIters[i])->indexOfGroup (group);
if ((*pfIters[i])->argument (fIdx).contains (
(*pfIters[i])->elimLogVars()) == false) {
return false;
@ -315,10 +315,10 @@ SumOutOperator::isToEliminate (
unsigned group,
const Grounds& query)
{
int fIdx = g->indexOfGroup (group);
size_t fIdx = g->indexOfGroup (group);
const ProbFormula& formula = g->argument (fIdx);
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() &&
formula.arity() == query[i].arity()) {
g->constr()->moveToTop (formula.logVars());
@ -337,23 +337,23 @@ double
CountingOperator::getLogCost (void)
{
double cost = 0.0;
int fIdx = (*pfIter_)->indexOfLogVar (X_);
size_t fIdx = (*pfIter_)->indexOfLogVar (X_);
unsigned range = (*pfIter_)->range (fIdx);
unsigned size = (*pfIter_)->size() / range;
TinySet<unsigned> counts;
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);
}
unsigned group = (*pfIter_)->argument (fIdx).group();
int lvIndex = Util::indexOf (
size_t lvIndex = Util::indexOf (
(*pfIter_)->argument (fIdx).logVars(), X_);
assert (lvIndex != -1);
assert (lvIndex != (*pfIter_)->argument (fIdx).logVars().size());
ParfactorList::iterator pfIter = pfList_.begin();
while (pfIter != pfList_.end()) {
if (pfIter != pfIter_) {
int fIdx2 = (*pfIter)->indexOfGroup (group);
if (fIdx2 != -1) {
size_t fIdx2 = (*pfIter)->indexOfGroup (group);
if (fIdx2 != (*pfIter)->nrArguments()) {
LogVar Y = ((*pfIter)->argument (fIdx2).logVars()[lvIndex]);
if ((*pfIter)->canCountConvert (Y) == false) {
// the real cost should be the cost of grounding Y
@ -377,7 +377,7 @@ CountingOperator::apply (void)
Parfactor* pf = *pfIter_;
pfList_.remove (pfIter_);
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_);
bool cartProduct = pfs[i]->constr()->isCartesianProduct (
pfs[i]->countedLogVars() | X_);
@ -399,7 +399,7 @@ CountingOperator::getValidOps (ParfactorList& pfList)
ParfactorList::iterator it = pfList.begin();
while (it != pfList.end()) {
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])) {
validOps.push_back (new CountingOperator (
it, candidates[i], pfList));
@ -422,11 +422,11 @@ CountingOperator::toString (void)
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
Parfactors pfs = FoveSolver::countNormalize (*pfIter_, X_);
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;
}
}
for (unsigned i = 0; i < pfs.size(); i++) {
for (size_t i = 0; i < pfs.size(); i++) {
delete pfs[i];
}
return ss.str();
@ -440,7 +440,7 @@ CountingOperator::validOp (Parfactor* g, LogVar X)
if (g->nrFormulas (X) != 1) {
return false;
}
int fIdx = g->indexOfLogVar (X);
size_t fIdx = g->indexOfLogVar (X);
if (g->argument (fIdx).isCounting()) {
return false;
}
@ -459,7 +459,7 @@ GroundOperator::getLogCost (void)
vector<pair<unsigned, unsigned>> affectedFormulas;
affectedFormulas = getAffectedFormulas();
// 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].second << " " ;
// }
@ -472,9 +472,9 @@ GroundOperator::getLogCost (void)
double pfSize = std::log (pf->size());
bool willBeAffected = false;
LogVarSet lvsToGround;
for (unsigned i = 0; i < affectedFormulas.size(); i++) {
int fIdx = pf->indexOfGroup (affectedFormulas[i].first);
if (fIdx != -1) {
for (size_t i = 0; i < affectedFormulas.size(); i++) {
size_t fIdx = pf->indexOfGroup (affectedFormulas[i].first);
if (fIdx != pf->nrArguments()) {
ProbFormula f = pf->argument (fIdx);
LogVar X = f.logVars()[affectedFormulas[i].second];
bool isCountingLv = pf->countedLogVars().contains (X);
@ -514,7 +514,7 @@ GroundOperator::apply (void)
ParfactorList::iterator pfIter;
pfIter = getParfactorsWithGroup (pfList_, group_).front();
Parfactor* pf = *pfIter;
int idx = pf->indexOfGroup (group_);
size_t idx = pf->indexOfGroup (group_);
ProbFormula f = pf->argument (idx);
LogVar X = f.logVars()[lvIndex_];
bool countedLv = pf->countedLogVars().contains (X);
@ -524,7 +524,7 @@ GroundOperator::apply (void)
pfList_.add (pf);
} else {
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]));
}
delete pf;
@ -546,10 +546,10 @@ GroundOperator::getValidOps (ParfactorList& pfList)
ParfactorList::const_iterator it = pfList.begin();
while (it != pfList.end()) {
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) {
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) {
validOps.push_back (new GroundOperator (
formulas[i].group(), j, pfList));
@ -572,7 +572,7 @@ GroundOperator::toString (void)
vector<ParfactorList::iterator> pfIters;
pfIters = getParfactorsWithGroup (pfList_, group_);
Parfactor* pf = *(getParfactorsWithGroup (pfList_, group_).front());
int idx = pf->indexOfGroup (group_);
size_t idx = pf->indexOfGroup (group_);
ProbFormula f = pf->argument (idx);
LogVar lv = f.logVars()[lvIndex_];
TupleSet tupleSet = pf->constr()->tupleSet ({lv});
@ -604,12 +604,12 @@ GroundOperator::getAffectedFormulas (void)
pair<unsigned, unsigned> front = q.front();
ParfactorList::iterator pflIt = pfList_.begin();
while (pflIt != pfList_.end()) {
int idx = (*pflIt)->indexOfGroup (front.first);
if (idx != -1) {
size_t idx = (*pflIt)->indexOfGroup (front.first);
if (idx != (*pflIt)->nrArguments()) {
ProbFormula f = (*pflIt)->argument (idx);
LogVar X = f.logVars()[front.second];
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)) {
pair<unsigned, unsigned> pair = make_pair (
fs[i].group(), fs[i].indexOf (X));
@ -668,7 +668,7 @@ FoveSolver::absorveEvidence (
ParfactorList& pfList,
ObservedFormulas& obsFormulas)
{
for (unsigned i = 0; i < obsFormulas.size(); i++) {
for (size_t i = 0; i < obsFormulas.size(); i++) {
Parfactors newPfs;
ParfactorList::iterator it = pfList.begin();
while (it != pfList.end()) {
@ -692,7 +692,7 @@ FoveSolver::absorveEvidence (
if (Globals::verbosity > 2 && obsFormulas.empty() == false) {
Util::printAsteriskLine();
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;
}
Util::printAsteriskLine();
@ -712,7 +712,7 @@ FoveSolver::countNormalize (
normPfs.push_back (new Parfactor (*g));
} else {
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]));
}
}
@ -727,17 +727,17 @@ FoveSolver::calcGroundMultiplication (Parfactor pf)
LogVarSet lvs = pf.constr()->logVarSet();
lvs -= pf.constr()->singletons();
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;
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]);
if (countedLv) {
pfs[j]->fullExpand (lvs[i]);
newPfs.push_back (pfs[j]);
} else {
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]));
}
delete pfs[j];
@ -746,7 +746,7 @@ FoveSolver::calcGroundMultiplication (Parfactor pf)
}
ParfactorList pfList (newPfs);
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]);
}
return Parfactor (*groundShatteredPfs[0]);
@ -807,7 +807,7 @@ FoveSolver::getBestOperation (const Grounds& query)
LiftedOperator* bestOp = 0;
vector<LiftedOperator*> validOps;
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();
if ((bestOp == 0) || (cost < bestCost)) {
bestOp = validOps[i];
@ -817,7 +817,7 @@ FoveSolver::getBestOperation (const Grounds& query)
if (bestCost > largestCost_) {
largestCost_ = bestCost;
}
for (unsigned i = 0; i < validOps.size(); i++) {
for (size_t i = 0; i < validOps.size(); i++) {
if (validOps[i] != bestOp) {
delete validOps[i];
}
@ -832,7 +832,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
{
queue<unsigned> todo; // groups to process
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();
while (it != pfList_.end()) {
int group = (*it)->findGroup (query[i]);
@ -853,7 +853,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
if (Util::contains (requiredPfs, *it) == false &&
(*it)->containsGroup (group)) {
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) {
todo.push (groups[i]);
done.insert (groups[i]);
@ -889,7 +889,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
void
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()) {
continue;
}
@ -927,7 +927,7 @@ FoveSolver::shatterAgainstQuery (const Grounds& query)
if (Globals::verbosity > 2) {
Util::printAsteriskLine();
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;
}
Util::printAsteriskLine();
@ -944,7 +944,7 @@ FoveSolver::absorve (
{
Parfactors absorvedPfs;
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() &&
obsFormula.arity() == formulas[i].arity()) {
@ -971,7 +971,7 @@ FoveSolver::absorve (
if (formulas.size() > 1) {
LogVarSet excl = g->exclusiveLogVars (i);
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 (
formulas[i], obsFormula.evidence());
absorvedPfs.push_back (countNormPfs[j]);

View File

@ -19,7 +19,7 @@ HistogramSet::HistogramSet (unsigned size, unsigned range)
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) {
hist_[i] --;
hist_[i + 1] = maxCount (i + 1);
@ -27,13 +27,14 @@ HistogramSet::nextHistogram (void)
break;
}
}
assert (std::accumulate (hist_.begin(), hist_.end(), 0) == (int)size_);
assert (std::accumulate (hist_.begin(), hist_.end(), 0)
== (int) size_);
}
unsigned
HistogramSet::operator[] (unsigned idx) const
HistogramSet::operator[] (size_t idx) const
{
assert (idx < hist_.size());
return hist_[idx];
@ -82,7 +83,7 @@ HistogramSet::nrHistograms (unsigned N, unsigned R)
unsigned
size_t
HistogramSet::findIndex (
const Histogram& h,
const vector<Histogram>& hists)
@ -126,10 +127,10 @@ ostream& operator<< (ostream &os, const HistogramSet& hs)
unsigned
HistogramSet::maxCount (unsigned idx) const
HistogramSet::maxCount (size_t idx) const
{
unsigned sum = 0;
for (unsigned i = 0; i < idx; i++) {
for (size_t i = 0; i < idx; i++) {
sum += hist_[i];
}
return size_ - sum;
@ -138,7 +139,7 @@ HistogramSet::maxCount (unsigned idx) const
void
HistogramSet::clearAfter (unsigned idx)
HistogramSet::clearAfter (size_t idx)
{
std::fill (hist_.begin() + idx + 1, hist_.end(), 0);
}

View File

@ -15,7 +15,7 @@ class HistogramSet
void nextHistogram (void);
unsigned operator[] (unsigned idx) const;
unsigned operator[] (size_t idx) const;
unsigned nrHistograms (void) const;
@ -25,7 +25,7 @@ class HistogramSet
static unsigned nrHistograms (unsigned, unsigned);
static unsigned findIndex (
static size_t findIndex (
const Histogram&, const vector<Histogram>&);
static vector<double> getNumAssigns (unsigned, unsigned);
@ -33,9 +33,9 @@ class HistogramSet
friend std::ostream& operator<< (ostream &os, const HistogramSet& hs);
private:
unsigned maxCount (unsigned) const;
unsigned maxCount (size_t) const;
void clearAfter (unsigned);
void clearAfter (size_t);
unsigned size_;
Histogram hist_;

View File

@ -25,7 +25,6 @@ typedef vector<FacNode*> FacNodes;
typedef vector<Factor*> Factors;
typedef vector<string> States;
typedef vector<unsigned> Ranges;
typedef Params::size_type psize_t;
typedef unsigned long long ullong;

View File

@ -66,7 +66,7 @@ int createLiftedNetwork (void)
// LiftedUtils::printSymbolDictionary();
if (Globals::verbosity > 2) {
Util::printHeader ("INITIAL PARFACTORS");
for (unsigned i = 0; i < parfactors.size(); i++) {
for (size_t i = 0; i < parfactors.size(); i++) {
parfactors[i]->print();
cout << endl;
}
@ -197,7 +197,7 @@ void readLiftedEvidence (
}
unsigned evidence = (unsigned) YAP_IntOfTerm (YAP_ArgOfTerm (2, pair));
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 &&
obsFormulas[i].arity() == args.size() &&
obsFormulas[i].evidence() == evidence) {
@ -382,7 +382,7 @@ void runVeSolver (
vector<Params>& results)
{
results.reserve (tasks.size());
for (unsigned i = 0; i < tasks.size(); i++) {
for (size_t i = 0; i < tasks.size(); i++) {
FactorGraph* mfg = fg;
if (fg->isFromBayesNetwork()) {
// mfg = BayesBall::getMinimalFactorGraph (*fg, tasks[i]);
@ -408,7 +408,7 @@ void runBpSolver (
vector<Params>& results)
{
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]);
}
Solver* solver = 0;
@ -431,7 +431,7 @@ void runBpSolver (
cout << endl;
}
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]));
}
if (fg->isFromBayesNetwork()) {
@ -482,7 +482,7 @@ setFactorsParams (void)
distList = YAP_TailOfTerm (distList);
}
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();
assert (Util::contains (paramsMap, distId));
facNodes[i]->factor().setParams (paramsMap[distId]);

View File

@ -19,28 +19,10 @@ class StatesIndexer
StatesIndexer (const Ranges& ranges, bool calcOffsets = true)
{
size_ = 1;
li_ = 0;
size_ = std::accumulate (ranges.begin(), ranges.end(), 0);
indices_.resize (ranges.size(), 0);
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) {
calculateOffsets();
}
@ -48,7 +30,7 @@ class StatesIndexer
void increment (void)
{
for (int i = ranges_.size() - 1; i >= 0; i--) {
for (size_t i = ranges_.size(); i-- > 0; ) {
indices_[i] ++;
if (indices_[i] != ranges_[i]) {
break;
@ -59,7 +41,7 @@ class StatesIndexer
li_ ++;
}
void increment (unsigned dim)
void increment (size_t dim)
{
assert (dim < ranges_.size());
assert (ranges_.size() == offsets_.size());
@ -68,10 +50,10 @@ class StatesIndexer
li_ += offsets_[dim];
}
void incrementExcluding (unsigned skipDim)
void incrementExcluding (size_t skipDim)
{
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) {
indices_[i] ++;
li_ += offsets_[i];
@ -86,7 +68,7 @@ class StatesIndexer
li_ = size_;
}
unsigned linearIndex (void) const
size_t linearIndex (void) const
{
return li_;
}
@ -102,12 +84,12 @@ class StatesIndexer
return *this;
}
operator unsigned (void) const
operator size_t (void) const
{
return li_;
}
unsigned operator[] (unsigned dim) const
unsigned operator[] (size_t dim) const
{
assert (valid());
assert (dim < ranges_.size());
@ -125,13 +107,13 @@ class StatesIndexer
li_ = 0;
}
void reset (unsigned dim)
void reset (size_t dim)
{
indices_[dim] = 0;
li_ -= offsets_[dim] * ranges_[dim];
}
unsigned size (void) const
size_t size (void) const
{
return size_ ;
}
@ -146,19 +128,19 @@ class StatesIndexer
private:
void calculateOffsets (void)
{
unsigned prod = 1;
size_t prod = 1;
offsets_.resize (ranges_.size());
for (int i = ranges_.size() - 1; i >= 0; i--) {
for (size_t i = ranges_.size(); i-- > 0; ) {
offsets_[i] = prod;
prod *= ranges_[i];
}
}
unsigned li_;
unsigned size_;
size_t li_;
size_t size_;
vector<unsigned> indices_;
vector<unsigned> ranges_;
vector<unsigned> offsets_;
vector<size_t> offsets_;
};
@ -169,9 +151,9 @@ class MapIndexer
MapIndexer (const Ranges& ranges, const vector<bool>& mapDims)
{
assert (ranges.size() == mapDims.size());
unsigned prod = 1;
size_t prod = 1;
offsets_.resize (ranges.size());
for (int i = ranges.size() - 1; i >= 0; i--) {
for (size_t i = ranges.size(); i-- > 0; ) {
if (mapDims[i]) {
offsets_[i] = prod;
prod *= ranges[i];
@ -183,12 +165,12 @@ class MapIndexer
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());
for (int i = ranges.size() - 1; i >= 0; i--) {
if (i != (int)ignoreDim) {
for (size_t i = ranges.size(); i-- > 0; ) {
if (i != ignoreDim) {
offsets_[i] = prod;
prod *= ranges[i];
}
@ -208,13 +190,13 @@ class MapIndexer
{
unsigned prod = 1;
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;
prod *= mapRanges[i];
}
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 =
std::find (mapVids.begin(), mapVids.end(), loopVids[i]);
if (it != mapVids.end()) {
@ -234,7 +216,7 @@ class MapIndexer
MapIndexer& operator ++ (void)
{
assert (valid_);
for (int i = ranges_.size() - 1; i >= 0; i--) {
for (size_t i = ranges_.size(); i-- > 0; ) {
indices_[i] ++;
index_ += offsets_[i];
if (indices_[i] != ranges_[i]) {
@ -248,17 +230,17 @@ class MapIndexer
return *this;
}
unsigned mappedIndex (void) const
size_t mappedIndex (void) const
{
return index_;
}
operator unsigned (void) const
operator size_t (void) const
{
return index_;
}
unsigned operator[] (unsigned dim) const
unsigned operator[] (size_t dim) const
{
assert (valid());
assert (dim < ranges_.size());
@ -284,11 +266,11 @@ class MapIndexer
}
private:
unsigned index_;
size_t index_;
bool valid_;
vector<unsigned> ranges_;
vector<unsigned> indices_;
vector<unsigned> offsets_;
vector<size_t> offsets_;
};

View File

@ -72,7 +72,7 @@ ostream& operator<< (ostream &os, const LogVar& X)
ostream& operator<< (ostream &os, const Tuple& t)
{
os << "(" ;
for (unsigned i = 0; i < t.size(); i++) {
for (size_t i = 0; i < t.size(); i++) {
os << ((i != 0) ? "," : "") << t[i];
}
os << ")" ;
@ -85,7 +85,7 @@ ostream& operator<< (ostream &os, const Ground& gr)
{
os << gr.functor();
os << "(" ;
for (unsigned i = 0; i < gr.args().size(); i++) {
for (size_t i = 0; i < gr.args().size(); i++) {
if (i != 0) os << ", " ;
os << gr.args()[i];
}

View File

@ -105,7 +105,7 @@ class Ground
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; }
@ -150,7 +150,7 @@ class Substitution
return Util::contains (subs_, X);
}
unsigned nrReplacements (void) const { return subs_.size(); }
size_t nrReplacements (void) const { return subs_.size(); }
LogVars getDiscardedLogVars (void) const;

View File

@ -17,10 +17,10 @@ Parfactor::Parfactor (
distId_ = distId;
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());
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) {
logVars.push_back (lvs[j]);
}
@ -79,7 +79,7 @@ LogVarSet
Parfactor::countedLogVars (void) const
{
LogVarSet set;
for (unsigned i = 0; i < args_.size(); i++) {
for (size_t i = 0; i < args_.size(); i++) {
if (args_[i].isCounting()) {
set.insert (args_[i].countedLogVar());
}
@ -109,11 +109,11 @@ Parfactor::elimLogVars (void) const
LogVarSet
Parfactor::exclusiveLogVars (unsigned fIdx) const
Parfactor::exclusiveLogVars (size_t fIdx) const
{
assert (fIdx < args_.size());
LogVarSet remaining;
for (unsigned i = 0; i < args_.size(); i++) {
for (size_t i = 0; i < args_.size(); i++) {
if (i != fIdx) {
remaining |= args_[i].logVarSet();
}
@ -133,7 +133,7 @@ Parfactor::setConstraintTree (ConstraintTree* newTree)
void
Parfactor::sumOut (unsigned fIdx)
Parfactor::sumOut (size_t fIdx)
{
assert (fIdx < args_.size());
assert (args_[fIdx].contains (elimLogVars()));
@ -160,12 +160,12 @@ Parfactor::sumOut (unsigned fIdx)
params_.resize (copy.size() / ranges_[fIdx], LogAware::addIdenty());
MapIndexer indexer (ranges_, fIdx);
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]);
++ indexer;
}
} else {
for (unsigned i = 0; i < copy.size(); i++) {
for (size_t i = 0; i < copy.size(); i++) {
params_[indexer] += copy[i];
++ indexer;
}
@ -205,7 +205,7 @@ Parfactor::canCountConvert (LogVar X)
if (nrFormulas (X) != 1) {
return false;
}
int fIdx = indexOfLogVar (X);
size_t fIdx = indexOfLogVar (X);
if (args_[fIdx].isCounting()) {
return false;
}
@ -226,7 +226,7 @@ Parfactor::canCountConvert (LogVar X)
void
Parfactor::countConvert (LogVar X)
{
int fIdx = indexOfLogVar (X);
size_t fIdx = indexOfLogVar (X);
assert (constr_->isCountNormalized (X));
assert (constr_->getConditionalCount (X) > 1);
assert (canCountConvert (X));
@ -257,7 +257,7 @@ Parfactor::countConvert (LogVar X)
MapIndexer mapIndexer (ranges_, fIdx);
while (mapIndexer.valid()) {
double prod = LogAware::multIdenty();
unsigned i = mapIndexer.mappedIndex();
size_t i = mapIndexer.mappedIndex();
unsigned h = mapIndexer[fIdx];
for (unsigned r = 0; r < R; r++) {
if (Globals::logDomain) {
@ -278,8 +278,8 @@ Parfactor::countConvert (LogVar X)
void
Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
{
int fIdx = indexOfLogVar (X);
assert (fIdx != -1);
size_t fIdx = indexOfLogVar (X);
assert (fIdx != args_.size());
assert (args_[fIdx].isCounting());
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 j = 0; j < H2; j++) {
Histogram hist = histograms1[i];
std::transform (
hist.begin(), hist.end(),
histograms2[j].begin(),
hist.begin(),
plus<int>());
hist += histograms2[j];
sumIndexes.push_back (HistogramSet::findIndex (hist, histograms));
}
}
@ -327,8 +323,8 @@ Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
void
Parfactor::fullExpand (LogVar X)
{
int fIdx = indexOfLogVar (X);
assert (fIdx != -1);
size_t fIdx = indexOfLogVar (X);
assert (fIdx != args_.size());
assert (args_[fIdx].isCounting());
unsigned N = constr_->getConditionalCount (X);
@ -344,11 +340,7 @@ Parfactor::fullExpand (LogVar X)
while (indexer.valid()) {
vector<unsigned> hist (R, 0);
for (unsigned n = 0; n < N; n++) {
std::transform (
hist.begin(), hist.end(),
expandHists[indexer[n]].begin(),
hist.begin(),
plus<int>());
hist += expandHists[indexer[n]];
}
sumIndexes.push_back (HistogramSet::findIndex (hist, originHists));
++ indexer;
@ -375,8 +367,8 @@ void
Parfactor::reorderAccordingGrounds (const Grounds& grounds)
{
ProbFormulas newFormulas;
for (unsigned i = 0; i < grounds.size(); i++) {
for (unsigned j = 0; j < args_.size(); j++) {
for (size_t i = 0; i < grounds.size(); i++) {
for (size_t j = 0; j < args_.size(); j++) {
if (grounds[i].functor() == args_[j].functor() &&
grounds[i].arity() == args_[j].arity()) {
constr_->moveToTop (args_[j].logVars());
@ -396,8 +388,8 @@ Parfactor::reorderAccordingGrounds (const Grounds& grounds)
void
Parfactor::absorveEvidence (const ProbFormula& formula, unsigned evidence)
{
int fIdx = indexOf (formula);
assert (fIdx != -1);
size_t fIdx = indexOf (formula);
assert (fIdx != args_.size());
LogVarSet excl = exclusiveLogVars (fIdx);
assert (args_[fIdx].isCounting() == false);
assert (constr_->isCountNormalized (excl));
@ -411,7 +403,7 @@ Parfactor::absorveEvidence (const ProbFormula& formula, unsigned evidence)
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());
}
}
@ -421,9 +413,9 @@ Parfactor::setNewGroups (void)
void
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();
for (unsigned j = 0; j < lvs.size(); j++) {
for (size_t j = 0; j < lvs.size(); j++) {
lvs[j] = theta.newNameFor (lvs[j]);
}
if (args_[i].isCounting()) {
@ -440,7 +432,7 @@ int
Parfactor::findGroup (const Ground& ground) const
{
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() &&
args_[i].arity() == ground.arity()) {
constr_->moveToTop (args_[i].logVars());
@ -466,7 +458,7 @@ Parfactor::containsGround (const Ground& ground) const
bool
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) {
return true;
}
@ -480,7 +472,7 @@ unsigned
Parfactor::nrFormulas (LogVar X) const
{
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)) {
count ++;
}
@ -493,9 +485,9 @@ Parfactor::nrFormulas (LogVar X) const
int
Parfactor::indexOfLogVar (LogVar X) const
{
int idx = -1;
size_t idx = args_.size();
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)) {
idx = i;
break;
@ -509,8 +501,8 @@ Parfactor::indexOfLogVar (LogVar X) const
int
Parfactor::indexOfGroup (unsigned group) const
{
int pos = -1;
for (unsigned i = 0; i < args_.size(); i++) {
size_t pos = args_.size();
for (size_t i = 0; i < args_.size(); i++) {
if (args_[i].group() == group) {
pos = i;
break;
@ -525,7 +517,7 @@ unsigned
Parfactor::nrFormulasWithGroup (unsigned group) const
{
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) {
count ++;
}
@ -539,7 +531,7 @@ vector<unsigned>
Parfactor::getAllGroups (void) const
{
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();
}
return groups;
@ -552,7 +544,7 @@ Parfactor::getLabel (void) const
{
stringstream ss;
ss << "phi(" ;
for (unsigned i = 0; i < args_.size(); i++) {
for (size_t i = 0; i < args_.size(); i++) {
if (i != 0) ss << "," ;
ss << args_[i];
}
@ -569,14 +561,14 @@ void
Parfactor::print (bool printParams) const
{
cout << "Formulas: " ;
for (unsigned i = 0; i < args_.size(); i++) {
for (size_t i = 0; i < args_.size(); i++) {
if (i != 0) cout << ", " ;
cout << args_[i];
}
cout << endl;
if (args_[0].group() != Util::maxUnsigned()) {
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()));
}
cout << "Groups: " << groups << endl;
@ -609,7 +601,7 @@ Parfactor::printParameters (void) const
StatesIndexer indexer (ranges_);
while (indexer.valid()) {
stringstream ss;
for (unsigned i = 0; i < args_.size(); i++) {
for (size_t i = 0; i < args_.size(); i++) {
if (i != 0) ss << ", " ;
if (args_[i].isCounting()) {
unsigned N = constr_->getConditionalCount (
@ -628,7 +620,7 @@ Parfactor::printParameters (void) const
jointStrings.push_back (ss.str());
++ indexer;
}
for (unsigned i = 0; i < params_.size(); i++) {
for (size_t i = 0; i < params_.size(); i++) {
cout << "f(" << jointStrings[i] << ")" ;
cout << " = " << params_[i] << endl;
}
@ -642,7 +634,7 @@ Parfactor::printProjections (void) const
ConstraintTree copy (*constr_);
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 << copy.tupleSet ({Xs[i]}) << endl;
}
@ -652,7 +644,7 @@ Parfactor::printProjections (void) const
void
Parfactor::expandPotential (
int fIdx,
size_t fIdx,
unsigned newRange,
const vector<unsigned>& sumIndexes)
{
@ -674,16 +666,16 @@ Parfactor::expandPotential (
prod *= ranges_[i];
}
unsigned index = 0;
size_t index = 0;
ranges_[fIdx] = newRange;
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()) {
abort();
}
assert (index < copy.size());
params_.push_back (copy[index]);
for (int i = ranges_.size() - 1; i >= 0; i--) {
for (size_t i = ranges_.size(); i-- > 0; ) {
indices[i] ++;
if (i == fIdx) {
if (indices[i] != ranges_[i]) {
@ -711,11 +703,11 @@ Parfactor::expandPotential (
void
Parfactor::simplifyCountingFormulas (int fIdx)
Parfactor::simplifyCountingFormulas (size_t fIdx)
{
// check if we can simplify the parfactor
for (unsigned i = 0; i < args_.size(); i++) {
if ((int)i != fIdx &&
for (size_t i = 0; i < args_.size(); i++) {
if (i != fIdx &&
args_[i].isCounting() &&
args_[i].group() == args_[fIdx].group()) {
// if they only differ in the name of the counting log var
@ -735,8 +727,8 @@ void
Parfactor::simplifyGrounds (void)
{
LogVarSet singletons = constr_->singletons();
for (int i = 0; i < (int)args_.size() - 1; i++) {
for (unsigned j = i + 1; j < args_.size(); j++) {
for (long i = 0; i < (int)args_.size() - 1; i++) {
for (size_t j = i + 1; j < args_.size(); j++) {
if (args_[i].group() == args_[j].group() &&
singletons.contains (args_[i].logVarSet()) &&
singletons.contains (args_[j].logVarSet())) {
@ -767,7 +759,7 @@ Parfactor::canMultiply (Parfactor* g1, Parfactor* g2)
void
Parfactor::simplifyParfactor (unsigned fIdx1, unsigned fIdx2)
Parfactor::simplifyParfactor (size_t fIdx1, size_t fIdx2)
{
Params copy = params_;
params_.clear();
@ -778,7 +770,7 @@ Parfactor::simplifyParfactor (unsigned fIdx1, unsigned fIdx2)
}
++ 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) {
constr_->remove ({ args_[fIdx2].logVars()[i] });
}
@ -795,12 +787,12 @@ Parfactor::getAlignLogVars (Parfactor* g1, Parfactor* g2)
g1->simplifyGrounds();
g2->simplifyGrounds();
LogVars Xs_1, Xs_2;
TinySet<unsigned> matchedI;
TinySet<unsigned> matchedJ;
TinySet<size_t> matchedI;
TinySet<size_t> matchedJ;
ProbFormulas& formulas1 = g1->arguments();
ProbFormulas& formulas2 = g2->arguments();
for (unsigned i = 0; i < formulas1.size(); i++) {
for (unsigned j = 0; j < formulas2.size(); j++) {
for (size_t i = 0; i < formulas1.size(); i++) {
for (size_t j = 0; j < formulas2.size(); j++) {
if (formulas1[i].group() == formulas2[j].group() &&
g1->range (i) == g2->range (j) &&
matchedI.contains (i) == false &&
@ -850,7 +842,7 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
// cout << "-> align lvs2 = " << alignLvs2 << endl;
LogVar freeLogVar (0);
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 b2 = theta2.containsReplacementFor (alignLvs2[i]);
if (b1 == false && b2 == false) {
@ -865,14 +857,14 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
}
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) {
theta1.add (allLvs1[i], freeLogVar);
++ freeLogVar;
}
}
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) {
theta2.add (allLvs2[i], freeLogVar);
++ freeLogVar;
@ -883,7 +875,7 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
// g1 = p(X), q(X) ; X in {(p1),(p2)}
// g2 = p(X), q(Y) ; (X,Y) in {(p1,p2),(p2,p1)}
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]) &&
g1->nrFormulas (discardedLvs1[i]) == 1) {
g1->constr()->remove (discardedLvs1[i]);
@ -893,7 +885,7 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
}
}
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]) &&
g2->nrFormulas (discardedLvs2[i]) == 1) {
g2->constr()->remove (discardedLvs2[i]);

View File

@ -42,11 +42,11 @@ class Parfactor : public TFactor<ProbFormula>
LogVarSet elimLogVars (void) const;
LogVarSet exclusiveLogVars (unsigned) const;
LogVarSet exclusiveLogVars (size_t fIdx) const;
void setConstraintTree (ConstraintTree*);
void sumOut (unsigned fIdx);
void sumOut (size_t fIdx);
void multiply (Parfactor&);
@ -96,14 +96,14 @@ class Parfactor : public TFactor<ProbFormula>
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 (
Parfactor* g1, Parfactor* g2);
void expandPotential (int fIdx, unsigned newRange,
void expandPotential (size_t fIdx, unsigned newRange,
const vector<unsigned>& sumIndexes);
static void alignAndExponentiate (Parfactor*, Parfactor*);

View File

@ -45,7 +45,7 @@ ParfactorList::add (Parfactor* pf)
void
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();
addToShatteredList (pfs[i]);
}
@ -98,11 +98,11 @@ ParfactorList::isAllShattered (void) const
return true;
}
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]));
}
for (unsigned i = 0; i < pfs.size() - 1; i++) {
for (unsigned j = i + 1; j < pfs.size(); j++) {
for (size_t i = 0; i < pfs.size() - 1; i++) {
for (size_t j = i + 1; j < pfs.size(); j++) {
if (isShattered (pfs[i], pfs[j]) == false) {
return false;
}
@ -118,7 +118,7 @@ ParfactorList::print (void) const
{
Parfactors pfVec (pfList_.begin(), pfList_.end());
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();
cout << endl;
}
@ -134,8 +134,8 @@ ParfactorList::isShattered (const Parfactor* g) const
return true;
}
ConstraintTree ct (*g->constr());
for (unsigned i = 0; i < formulas.size() - 1; i++) {
for (unsigned j = i + 1; j < formulas.size(); j++) {
for (size_t i = 0; i < formulas.size() - 1; i++) {
for (size_t j = i + 1; j < formulas.size(); j++) {
if (formulas[i].group() == formulas[j].group()) {
if (identical (
formulas[i], *(g->constr()),
@ -171,8 +171,8 @@ ParfactorList::isShattered (
const ProbFormulas& fms1 = g1->arguments();
const ProbFormulas& fms2 = g2->arguments();
for (unsigned i = 0; i < fms1.size(); i++) {
for (unsigned j = 0; j < fms2.size(); j++) {
for (size_t i = 0; i < fms1.size(); i++) {
for (size_t j = 0; j < fms2.size(); j++) {
if (fms1[i].group() == fms2[j].group()) {
if (identical (
fms1[i], *(g1->constr()),
@ -261,7 +261,7 @@ ParfactorList::shatterAgainstMySelf (Parfactor* g)
pfs.push_back (pf);
} else {
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);
residuals.push (res[i]);
}
@ -280,8 +280,8 @@ ParfactorList::shatterAgainstMySelf2 (Parfactor* g)
// slip a parfactor with overlapping formulas:
// e.g. {s(X),s(Y)}, with (X,Y) in {(p1,p2),(p1,p3),(p4,p1)}
const ProbFormulas& formulas = g->arguments();
for (unsigned i = 0; i < formulas.size() - 1; i++) {
for (unsigned j = i + 1; j < formulas.size(); j++) {
for (size_t i = 0; i < formulas.size() - 1; i++) {
for (size_t j = i + 1; j < formulas.size(); j++) {
if (formulas[i].sameSkeletonAs (formulas[j])) {
Parfactors res = shatterAgainstMySelf (g, i, j);
if (res.empty() == false) {
@ -298,8 +298,8 @@ ParfactorList::shatterAgainstMySelf2 (Parfactor* g)
Parfactors
ParfactorList::shatterAgainstMySelf (
Parfactor* g,
unsigned fIdx1,
unsigned fIdx2)
size_t fIdx1,
size_t fIdx2)
{
/*
Util::printDashedLine();
@ -348,7 +348,7 @@ ParfactorList::shatterAgainstMySelf (
Parfactors res;
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());
std::pair<ConstraintTree*, ConstraintTree*> split2;
split2 = res1[i]->constr()->split (f2.logVars(), &ctCopy, f1.logVars());
@ -370,7 +370,7 @@ ParfactorList::shatterAgainstMySelf (
}
} else {
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) {
delete res1[i];
@ -393,8 +393,8 @@ ParfactorList::shatter (Parfactor* g1, Parfactor* g2)
ProbFormulas& formulas1 = g1->arguments();
ProbFormulas& formulas2 = g2->arguments();
assert (g1 != 0 && g2 != 0 && g1 != g2);
for (unsigned i = 0; i < formulas1.size(); i++) {
for (unsigned j = 0; j < formulas2.size(); j++) {
for (size_t i = 0; i < formulas1.size(); i++) {
for (size_t j = 0; j < formulas2.size(); j++) {
if (formulas1[i].sameSkeletonAs (formulas2[j])) {
std::pair<Parfactors, Parfactors> res;
res = shatter (i, g1, j, g2);
@ -412,8 +412,8 @@ ParfactorList::shatter (Parfactor* g1, Parfactor* g2)
std::pair<Parfactors, Parfactors>
ParfactorList::shatter (
unsigned fIdx1, Parfactor* g1,
unsigned fIdx2, Parfactor* g2)
size_t fIdx1, Parfactor* g1,
size_t fIdx2, Parfactor* g2)
{
ProbFormula& f1 = g1->argument (fIdx1);
ProbFormula& f2 = g2->argument (fIdx2);
@ -464,7 +464,6 @@ ParfactorList::shatter (
assert (commCt1->tupleSet (f1.logVars()) ==
commCt2->tupleSet (f2.logVars()));
// unsigned static count = 0; count ++;
// stringstream ss1; ss1 << "" << count << "_A.dot" ;
// stringstream ss2; ss2 << "" << count << "_B.dot" ;
// stringstream ss3; ss3 << "" << count << "_A_comm.dot" ;
@ -507,7 +506,7 @@ ParfactorList::shatter (
Parfactors
ParfactorList::shatter (
Parfactor* g,
unsigned fIdx,
size_t fIdx,
ConstraintTree* commCt,
ConstraintTree* exclCt,
unsigned commGroup)
@ -526,7 +525,7 @@ ParfactorList::shatter (
LogVar X_new2 = g->constr()->logVarSet().back() + 2;
ConstraintTrees cts = g->constr()->jointCountNormalize (
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]);
if (cts[i]->nrLogVars() == g->constr()->nrLogVars() + 1) {
newPf->expand (f.countedLogVar(), X_new1, X_new2);
@ -562,7 +561,7 @@ ParfactorList::updateGroups (unsigned oldGroup, unsigned newGroup)
for (ParfactorList::iterator it = pfList_.begin();
it != pfList_.end(); it++) {
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) {
formulas[i].setGroup (newGroup);
}

View File

@ -26,7 +26,7 @@ class ParfactorList
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;
@ -70,13 +70,13 @@ class ParfactorList
Parfactors shatterAgainstMySelf2 (Parfactor* g);
Parfactors shatterAgainstMySelf (
Parfactor* g, unsigned fIdx1, unsigned fIdx2);
Parfactor* g, size_t fIdx1, size_t fIdx2);
std::pair<Parfactors, Parfactors> shatter (
Parfactor*, Parfactor*);
std::pair<Parfactors, Parfactors> shatter (
unsigned, Parfactor*, unsigned, Parfactor*);
size_t, Parfactor*, size_t, Parfactor*);
Parfactors shatter (
Parfactor*,
@ -113,7 +113,7 @@ class ParfactorList
}
};
list<Parfactor*> pfList_;
list<Parfactor*> pfList_;
};
#endif // HORUS_PARFACTORLIST_H

View File

@ -29,7 +29,7 @@ ProbFormula::contains (LogVarSet s) const
int
size_t
ProbFormula::indexOf (LogVar X) const
{
return Util::indexOf (logVars_, X);
@ -81,7 +81,7 @@ ProbFormula::clearCountedLogVar (void)
void
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) {
logVars_[i] = newName;
}
@ -105,7 +105,7 @@ std::ostream& operator<< (ostream &os, const ProbFormula& f)
os << f.functor_;
if (f.isAtom() == false) {
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 (f.isCounting() && f.logVars_[i] == f.countedLogVar_) {
os << "#" ;

View File

@ -41,7 +41,7 @@ class ProbFormula
bool contains (LogVarSet) const;
int indexOf (LogVar) const;
size_t indexOf (LogVar) const;
bool isAtom (void) const;

View File

@ -7,7 +7,7 @@ Solver::printAnswer (const VarIds& vids)
{
Vars unobservedVars;
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]);
if (vn->hasEvidence() == false) {
unobservedVars.push_back (vn);
@ -16,7 +16,7 @@ Solver::printAnswer (const VarIds& vids)
}
Params res = solveQuery (unobservedVids);
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 << std::setprecision (Constants::PRECISION) << res[i];
cout << endl;
@ -31,11 +31,11 @@ Solver::printAllPosterioris (void)
{
VarIds vids;
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());
}
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]});
}
}

View File

@ -125,7 +125,7 @@ nrCombinations (unsigned n, unsigned k)
unsigned
size_t
sizeExpected (const Ranges& ranges)
{
return std::accumulate (
@ -166,7 +166,7 @@ parametersToString (const Params& v, unsigned precision)
stringstream ss;
ss.precision (precision);
ss << "[" ;
for (unsigned i = 0; i < v.size(); i++) {
for (size_t i = 0; i < v.size(); i++) {
if (i != 0) ss << ", " ;
ss << v[i];
}
@ -179,16 +179,21 @@ parametersToString (const Params& v, unsigned precision)
vector<string>
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;
while (idx.valid()) {
while (indexer.valid()) {
stringstream ss;
for (unsigned i = 0; i < vars.size(); i++) {
for (size_t i = 0; i < vars.size(); i++) {
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());
++ idx;
++ indexer;
}
return jointStrings;
}
@ -320,19 +325,19 @@ normalize (Params& v)
{
double sum = LogAware::addIdenty();
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]);
}
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;
}
} else {
for (unsigned i = 0; i < v.size(); i++) {
for (size_t i = 0; i < v.size(); i++) {
sum += v[i];
}
assert (sum != 0.0);
for (unsigned i = 0; i < v.size(); i++) {
for (size_t i = 0; i < v.size(); i++) {
v[i] /= sum;
}
}
@ -346,11 +351,11 @@ getL1Distance (const Params& v1, const Params& v2)
assert (v1.size() == v2.size());
double dist = 0.0;
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]));
}
} else {
for (unsigned i = 0; i < v1.size(); i++) {
for (size_t i = 0; i < v1.size(); i++) {
dist += abs (v1[i] - v2[i]);
}
}
@ -365,14 +370,14 @@ getMaxNorm (const Params& v1, const Params& v2)
assert (v1.size() == v2.size());
double max = 0.0;
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]));
if (diff > max) {
max = diff;
}
}
} 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]);
if (diff > max) {
max = diff;

View File

@ -134,7 +134,7 @@ template <typename T> bool contains (const set<T>&, const T&);
template <typename K, typename V> bool contains (
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&);
@ -162,7 +162,7 @@ double logFactorial (unsigned);
unsigned nrCombinations (unsigned, unsigned);
unsigned sizeExpected (const Ranges&);
size_t sizeExpected (const Ranges&);
unsigned nrDigits (int);
@ -205,7 +205,7 @@ Util::addToSet (set<T>& s, const vector<T>& elements)
template <typename T> void
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]);
}
}
@ -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)
{
int pos = std::distance (v.begin(),
std::find (v.begin(), v.end(), e));
return pos != (int)v.size() ? pos : -1;
return std::distance (v.begin(),
std::find (v.begin(), v.end(), e));
}
@ -260,7 +259,7 @@ template <typename T>
std::ostream& operator << (std::ostream& os, const vector<T>& v)
{
os << "[" ;
for (unsigned i = 0; i < v.size(); i++) {
for (size_t i = 0; i < v.size(); i++) {
os << ((i != 0) ? ", " : "") << v[i];
}
os << "]" ;
@ -320,8 +319,8 @@ Util::logSum (double x, double y)
inline void
Util::add (Params& v1, const Params& v2, unsigned repetitions)
{
for (unsigned count = 0; count < v1.size(); ) {
for (unsigned i = 0; i < v2.size(); i++) {
for (size_t count = 0; count < v1.size(); ) {
for (size_t i = 0; i < v2.size(); i++) {
for (unsigned r = 0; r < repetitions; r++) {
v1[count] += v2[i];
count ++;
@ -335,8 +334,8 @@ Util::add (Params& v1, const Params& v2, unsigned repetitions)
inline void
Util::multiply (Params& v1, const Params& v2, unsigned repetitions)
{
for (unsigned count = 0; count < v1.size(); ) {
for (unsigned i = 0; i < v2.size(); i++) {
for (size_t count = 0; count < v1.size(); ) {
for (size_t i = 0; i < v2.size(); i++) {
for (unsigned r = 0; r < repetitions; r++) {
v1[count] *= v2[i];
count ++;

View File

@ -61,7 +61,7 @@ void
Var::setEvidence (const string& ev)
{
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) {
evidence_ = i;
return;

View File

@ -30,17 +30,17 @@ class Var
virtual ~Var (void) { };
unsigned varId (void) const { return varId_; }
VarId varId (void) const { return varId_; }
unsigned range (void) const { return range_; }
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
{
@ -98,7 +98,7 @@ class Var
VarId varId_;
unsigned range_;
int evidence_;
unsigned index_;
size_t index_;
static unordered_map<VarId, VarInfo> varsInfo_;

View File

@ -18,7 +18,7 @@ VarElimSolver::solveQuery (VarIds queryVids)
{
if (Globals::verbosity > 1) {
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 << ", " ;
cout << fg.getVarNode (queryVids[i])->label();
}
@ -65,15 +65,15 @@ VarElimSolver::createFactorList (void)
{
const FacNodes& facNodes = fg.facNodes();
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()));
const VarNodes& neighs = facNodes[i]->neighbors();
for (unsigned j = 0; j < neighs.size(); j++) {
unordered_map<VarId,vector<unsigned> >::iterator it
for (size_t j = 0; j < neighs.size(); j++) {
unordered_map<VarId, vector<size_t>>::iterator it
= varFactors_.find (neighs[j]->varId());
if (it == varFactors_.end()) {
it = varFactors_.insert (make_pair (
neighs[j]->varId(), vector<unsigned>())).first;
neighs[j]->varId(), vector<size_t>())).first;
}
it->second.push_back (i);
}
@ -91,16 +91,16 @@ VarElimSolver::absorveEvidence (void)
printActiveFactors();
}
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 (Globals::verbosity > 1) {
cout << "-> aborving evidence on ";
cout << varNodes[i]->label() << " = " ;
cout << varNodes[i]->getEvidence() << endl;
}
const vector<unsigned>& idxs =
const vector<size_t>& idxs =
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]];
if (factor->nrArguments() == 1) {
factorList_[idxs[j]] = 0;
@ -128,7 +128,7 @@ VarElimSolver::processFactorList (const VarIds& vids)
{
totalFactorSize_ = 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 >= 3) {
Util::printDashedLine();
@ -141,7 +141,7 @@ VarElimSolver::processFactorList (const VarIds& vids)
}
Factor* finalFactor = new Factor();
for (unsigned i = 0; i < factorList_.size(); i++) {
for (size_t i = 0; i < factorList_.size(); i++) {
if (factorList_[i]) {
finalFactor->multiply (*factorList_[i]);
delete factorList_[i];
@ -150,7 +150,7 @@ VarElimSolver::processFactorList (const VarIds& vids)
}
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) {
unobservedVids.push_back (vids[i]);
}
@ -172,9 +172,9 @@ void
VarElimSolver::eliminate (VarId elimVar)
{
Factor* result = 0;
vector<unsigned>& idxs = varFactors_.find (elimVar)->second;
for (unsigned i = 0; i < idxs.size(); i++) {
unsigned idx = idxs[i];
vector<size_t>& idxs = varFactors_.find (elimVar)->second;
for (size_t i = 0; i < idxs.size(); i++) {
size_t idx = idxs[i];
if (factorList_[idx]) {
if (result == 0) {
result = new Factor (*factorList_[idx]);
@ -193,8 +193,8 @@ VarElimSolver::eliminate (VarId elimVar)
result->sumOut (elimVar);
factorList_.push_back (result);
const VarIds& resultVarIds = result->arguments();
for (unsigned i = 0; i < resultVarIds.size(); i++) {
vector<unsigned>& idxs =
for (size_t i = 0; i < resultVarIds.size(); i++) {
vector<size_t>& idxs =
varFactors_.find (resultVarIds[i])->second;
idxs.push_back (factorList_.size() - 1);
}
@ -206,7 +206,7 @@ VarElimSolver::eliminate (VarId elimVar)
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) {
cout << factorList_[i]->getLabel() << " " ;
cout << factorList_[i]->params() << endl;

View File

@ -39,7 +39,7 @@ class VarElimSolver : public Solver
VarIds elimOrder_;
unsigned largestFactorSize_;
unsigned totalFactorSize_;
unordered_map<VarId, vector<unsigned>> varFactors_;
unordered_map<VarId, vector<size_t>> varFactors_;
};
#endif // HORUS_VARELIMSOLVER_H