Avoid importing the std namespace
This commit is contained in:
parent
7b60e88545
commit
bb7a530da3
@ -9,8 +9,6 @@
|
||||
#include "BayesBallGraph.h"
|
||||
#include "Horus.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct ScheduleInfo
|
||||
{
|
||||
@ -23,7 +21,7 @@ struct ScheduleInfo
|
||||
};
|
||||
|
||||
|
||||
typedef queue<ScheduleInfo, list<ScheduleInfo>> Scheduling;
|
||||
typedef std::queue<ScheduleInfo, std::list<ScheduleInfo>> Scheduling;
|
||||
|
||||
|
||||
class BayesBall
|
||||
@ -53,8 +51,8 @@ class BayesBall
|
||||
inline void
|
||||
BayesBall::scheduleParents (const BBNode* n, Scheduling& sch) const
|
||||
{
|
||||
const vector<BBNode*>& ps = n->parents();
|
||||
for (vector<BBNode*>::const_iterator it = ps.begin();
|
||||
const std::vector<BBNode*>& ps = n->parents();
|
||||
for (std::vector<BBNode*>::const_iterator it = ps.begin();
|
||||
it != ps.end(); ++it) {
|
||||
sch.push (ScheduleInfo (*it, false, true));
|
||||
}
|
||||
@ -65,8 +63,8 @@ BayesBall::scheduleParents (const BBNode* n, Scheduling& sch) const
|
||||
inline void
|
||||
BayesBall::scheduleChilds (const BBNode* n, Scheduling& sch) const
|
||||
{
|
||||
const vector<BBNode*>& cs = n->childs();
|
||||
for (vector<BBNode*>::const_iterator it = cs.begin();
|
||||
const std::vector<BBNode*>& cs = n->childs();
|
||||
for (std::vector<BBNode*>::const_iterator it = cs.begin();
|
||||
it != cs.end(); ++it) {
|
||||
sch.push (ScheduleInfo (*it, true, false));
|
||||
}
|
||||
|
@ -22,8 +22,8 @@ BayesBallGraph::addNode (BBNode* n)
|
||||
void
|
||||
BayesBallGraph::addEdge (VarId vid1, VarId vid2)
|
||||
{
|
||||
unordered_map<VarId, BBNode*>::iterator it1;
|
||||
unordered_map<VarId, BBNode*>::iterator it2;
|
||||
std::unordered_map<VarId, BBNode*>::iterator it1;
|
||||
std::unordered_map<VarId, BBNode*>::iterator it2;
|
||||
it1 = varMap_.find (vid1);
|
||||
it2 = varMap_.find (vid2);
|
||||
assert (it1 != varMap_.end());
|
||||
@ -37,7 +37,7 @@ BayesBallGraph::addEdge (VarId vid1, VarId vid2)
|
||||
const BBNode*
|
||||
BayesBallGraph::getNode (VarId vid) const
|
||||
{
|
||||
unordered_map<VarId, BBNode*>::const_iterator it;
|
||||
std::unordered_map<VarId, BBNode*>::const_iterator it;
|
||||
it = varMap_.find (vid);
|
||||
return it != varMap_.end() ? it->second : 0;
|
||||
}
|
||||
@ -47,7 +47,7 @@ BayesBallGraph::getNode (VarId vid) const
|
||||
BBNode*
|
||||
BayesBallGraph::getNode (VarId vid)
|
||||
{
|
||||
unordered_map<VarId, BBNode*>::const_iterator it;
|
||||
std::unordered_map<VarId, BBNode*>::const_iterator it;
|
||||
it = varMap_.find (vid);
|
||||
return it != varMap_.end() ? it->second : 0;
|
||||
}
|
||||
@ -77,13 +77,13 @@ BayesBallGraph::clear (void)
|
||||
void
|
||||
BayesBallGraph::exportToGraphViz (const char* fileName)
|
||||
{
|
||||
ofstream out (fileName);
|
||||
std::ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
return;
|
||||
}
|
||||
out << "digraph {" << endl;
|
||||
out << "ranksep=1" << endl;
|
||||
out << "digraph {" << std::endl;
|
||||
out << "ranksep=1" << std::endl;
|
||||
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||
out << nodes_[i]->varId() ;
|
||||
out << " [" ;
|
||||
@ -91,16 +91,16 @@ BayesBallGraph::exportToGraphViz (const char* fileName)
|
||||
if (nodes_[i]->hasEvidence()) {
|
||||
out << ",style=filled, fillcolor=yellow" ;
|
||||
}
|
||||
out << "]" << endl;
|
||||
out << "]" << std::endl;
|
||||
}
|
||||
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||
const vector<BBNode*>& childs = nodes_[i]->childs();
|
||||
const std::vector<BBNode*>& childs = nodes_[i]->childs();
|
||||
for (size_t j = 0; j < childs.size(); j++) {
|
||||
out << nodes_[i]->varId() << " -> " << childs[j]->varId();
|
||||
out << " [style=bold]" << endl ;
|
||||
out << " [style=bold]" << std::endl;
|
||||
}
|
||||
}
|
||||
out << "}" << endl;
|
||||
out << "}" << std::endl;
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
@ -7,21 +7,19 @@
|
||||
#include "Var.h"
|
||||
#include "Horus.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class BBNode : public Var
|
||||
{
|
||||
public:
|
||||
BBNode (Var* v) : Var (v), visited_(false),
|
||||
markedOnTop_(false), markedOnBottom_(false) { }
|
||||
|
||||
const vector<BBNode*>& childs (void) const { return childs_; }
|
||||
const std::vector<BBNode*>& childs (void) const { return childs_; }
|
||||
|
||||
vector<BBNode*>& childs (void) { return childs_; }
|
||||
std::vector<BBNode*>& childs (void) { return childs_; }
|
||||
|
||||
const vector<BBNode*>& parents (void) const { return parents_; }
|
||||
const std::vector<BBNode*>& parents (void) const { return parents_; }
|
||||
|
||||
vector<BBNode*>& parents (void) { return parents_; }
|
||||
std::vector<BBNode*>& parents (void) { return parents_; }
|
||||
|
||||
void addParent (BBNode* p) { parents_.push_back (p); }
|
||||
|
||||
@ -46,8 +44,8 @@ class BBNode : public Var
|
||||
bool markedOnTop_;
|
||||
bool markedOnBottom_;
|
||||
|
||||
vector<BBNode*> childs_;
|
||||
vector<BBNode*> parents_;
|
||||
std::vector<BBNode*> childs_;
|
||||
std::vector<BBNode*> parents_;
|
||||
};
|
||||
|
||||
|
||||
@ -73,9 +71,9 @@ class BayesBallGraph
|
||||
void exportToGraphViz (const char*);
|
||||
|
||||
private:
|
||||
vector<BBNode*> nodes_;
|
||||
std::vector<BBNode*> nodes_;
|
||||
|
||||
unordered_map<VarId, BBNode*> varMap_;
|
||||
std::unordered_map<VarId, BBNode*> varMap_;
|
||||
};
|
||||
|
||||
#endif // PACKAGES_CLPBN_HORUS_BAYESBALLGRAPH_H
|
||||
|
@ -46,10 +46,10 @@ BpLink::updateMessage (void)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
BpLink::toString (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << fac_->getLabel();
|
||||
ss << " -- " ;
|
||||
ss << var_->label();
|
||||
@ -100,7 +100,7 @@ BeliefProp::solveQuery (VarIds queryVids)
|
||||
void
|
||||
BeliefProp::printSolverFlags (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "belief propagation [" ;
|
||||
ss << "bp_msg_schedule=" ;
|
||||
switch (schedule_) {
|
||||
@ -113,7 +113,7 @@ BeliefProp::printSolverFlags (void) const
|
||||
ss << ",bp_accuracy=" << Util::toString (accuracy_);
|
||||
ss << ",log_domain=" << Util::toString (Globals::logDomain);
|
||||
ss << "]" ;
|
||||
cout << ss.str() << endl;
|
||||
std::cout << ss.str() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -206,7 +206,8 @@ void
|
||||
BeliefProp::calculateAndUpdateMessage (BpLink* link, bool calcResidual)
|
||||
{
|
||||
if (Globals::verbosity > 2) {
|
||||
cout << "calculating & updating " << link->toString() << endl;
|
||||
std::cout << "calculating & updating " << link->toString();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
calcFactorToVarMsg (link);
|
||||
if (calcResidual) {
|
||||
@ -221,7 +222,8 @@ void
|
||||
BeliefProp::calculateMessage (BpLink* link, bool calcResidual)
|
||||
{
|
||||
if (Globals::verbosity > 2) {
|
||||
cout << "calculating " << link->toString() << endl;
|
||||
std::cout << "calculating " << link->toString();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
calcFactorToVarMsg (link);
|
||||
if (calcResidual) {
|
||||
@ -236,7 +238,8 @@ BeliefProp::updateMessage (BpLink* link)
|
||||
{
|
||||
link->updateMessage();
|
||||
if (Globals::verbosity > 2) {
|
||||
cout << "updating " << link->toString() << endl;
|
||||
std::cout << "updating " << link->toString();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -250,7 +253,8 @@ BeliefProp::runSolver (void)
|
||||
while (!converged() && nIters_ < maxIter_) {
|
||||
nIters_ ++;
|
||||
if (Globals::verbosity > 1) {
|
||||
Util::printHeader (string ("Iteration ") + Util::toString (nIters_));
|
||||
Util::printHeader (std::string ("Iteration ")
|
||||
+ Util::toString (nIters_));
|
||||
}
|
||||
switch (schedule_) {
|
||||
case MsgSchedule::SEQ_RANDOM:
|
||||
@ -276,13 +280,13 @@ BeliefProp::runSolver (void)
|
||||
}
|
||||
if (Globals::verbosity > 0) {
|
||||
if (nIters_ < maxIter_) {
|
||||
cout << "Belief propagation converged in " ;
|
||||
cout << nIters_ << " iterations" << endl;
|
||||
std::cout << "Belief propagation converged in " ;
|
||||
std::cout << nIters_ << " iterations" << std::endl;
|
||||
} else {
|
||||
cout << "The maximum number of iterations was hit, terminating..." ;
|
||||
cout << endl;
|
||||
std::cout << "The maximum number of iterations was hit, terminating..." ;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
runned_ = true;
|
||||
}
|
||||
@ -317,11 +321,13 @@ BeliefProp::maxResidualSchedule (void)
|
||||
|
||||
for (size_t c = 0; c < links_.size(); c++) {
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << "current residuals:" << endl;
|
||||
std::cout << "current residuals:" << std::endl;
|
||||
for (SortedOrder::iterator it = sortedOrder_.begin();
|
||||
it != sortedOrder_.end(); ++it) {
|
||||
cout << " " << setw (30) << left << (*it)->toString();
|
||||
cout << "residual = " << (*it)->residual() << endl;
|
||||
std::cout << " " << std::setw (30) << std::left;
|
||||
std::cout << (*it)->toString();
|
||||
std::cout << "residual = " << (*it)->residual();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -373,13 +379,13 @@ BeliefProp::calcFactorToVarMsg (BpLink* link)
|
||||
for (size_t i = links.size(); i-- > 0; ) {
|
||||
if (links[i]->varNode() != dst) {
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " message from " << links[i]->varNode()->label();
|
||||
cout << ": " ;
|
||||
std::cout << " message from " << links[i]->varNode()->label();
|
||||
std::cout << ": " ;
|
||||
}
|
||||
Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
|
||||
reps, std::plus<double>());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
reps *= links[i]->varNode()->range();
|
||||
@ -388,13 +394,13 @@ BeliefProp::calcFactorToVarMsg (BpLink* link)
|
||||
for (size_t i = links.size(); i-- > 0; ) {
|
||||
if (links[i]->varNode() != dst) {
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " message from " << links[i]->varNode()->label();
|
||||
cout << ": " ;
|
||||
std::cout << " message from " << links[i]->varNode()->label();
|
||||
std::cout << ": " ;
|
||||
}
|
||||
Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
|
||||
reps, std::multiplies<double>());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
reps *= links[i]->varNode()->range();
|
||||
@ -404,19 +410,20 @@ BeliefProp::calcFactorToVarMsg (BpLink* link)
|
||||
src->factor().ranges(), msgProduct);
|
||||
result.multiply (src->factor());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " message product: " << msgProduct << endl;
|
||||
cout << " original factor: " << src->factor().params() << endl;
|
||||
cout << " factor product: " << result.params() << endl;
|
||||
std::cout << " message product: " << msgProduct << std::endl;
|
||||
std::cout << " original factor: " << src->factor().params();
|
||||
std::cout << std::endl;
|
||||
std::cout << " factor product: " << result.params() << std::endl;
|
||||
}
|
||||
result.sumOutAllExcept (dst->varId());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " marginalized: " << result.params() << endl;
|
||||
std::cout << " marginalized: " << result.params() << std::endl;
|
||||
}
|
||||
link->nextMessage() = result.params();
|
||||
LogAware::normalize (link->nextMessage());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " curr msg: " << link->message() << endl;
|
||||
cout << " next msg: " << link->nextMessage() << endl;
|
||||
std::cout << " curr msg: " << link->message() << std::endl;
|
||||
std::cout << " next msg: " << link->nextMessage() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -434,7 +441,7 @@ BeliefProp::getVarToFactorMsg (const BpLink* link) const
|
||||
msg.resize (src->range(), LogAware::one());
|
||||
}
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << msg;
|
||||
std::cout << msg;
|
||||
}
|
||||
BpLinks::const_iterator it;
|
||||
const BpLinks& links = ninf (src)->getLinks();
|
||||
@ -444,7 +451,7 @@ BeliefProp::getVarToFactorMsg (const BpLink* link) const
|
||||
msg += (*it)->message();
|
||||
}
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " x " << (*it)->message();
|
||||
std::cout << " x " << (*it)->message();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -453,12 +460,12 @@ BeliefProp::getVarToFactorMsg (const BpLink* link) const
|
||||
msg *= (*it)->message();
|
||||
}
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " x " << (*it)->message();
|
||||
std::cout << " x " << (*it)->message();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " = " << msg;
|
||||
std::cout << " = " << msg;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
@ -508,11 +515,11 @@ BeliefProp::converged (void)
|
||||
return false;
|
||||
}
|
||||
if (Globals::verbosity > 2) {
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (nIters_ == 1) {
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << "no residuals" << endl << endl;
|
||||
std::cout << "no residuals" << std::endl << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -528,7 +535,8 @@ BeliefProp::converged (void)
|
||||
for (size_t i = 0; i < links_.size(); i++) {
|
||||
double residual = links_[i]->residual();
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << links_[i]->toString() + " residual = " << residual << endl;
|
||||
std::cout << links_[i]->toString() + " residual = " << residual;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (residual > accuracy_) {
|
||||
converged = false;
|
||||
@ -538,7 +546,7 @@ BeliefProp::converged (void)
|
||||
}
|
||||
}
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
return converged;
|
||||
@ -549,6 +557,8 @@ BeliefProp::converged (void)
|
||||
void
|
||||
BeliefProp::printLinkInformation (void) const
|
||||
{
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
for (size_t i = 0; i < links_.size(); i++) {
|
||||
BpLink* l = links_[i];
|
||||
cout << l->toString() << ":" << endl;
|
||||
|
@ -10,9 +10,6 @@
|
||||
#include "FactorGraph.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
enum MsgSchedule {
|
||||
SEQ_FIXED,
|
||||
SEQ_RANDOM,
|
||||
@ -44,7 +41,7 @@ class BpLink
|
||||
|
||||
virtual void updateMessage (void);
|
||||
|
||||
string toString (void) const;
|
||||
std::string toString (void) const;
|
||||
|
||||
protected:
|
||||
FacNode* fac_;
|
||||
@ -59,7 +56,7 @@ class BpLink
|
||||
DISALLOW_COPY_AND_ASSIGN (BpLink);
|
||||
};
|
||||
|
||||
typedef vector<BpLink*> BpLinks;
|
||||
typedef std::vector<BpLink*> BpLinks;
|
||||
|
||||
|
||||
class SPNodeInfo
|
||||
@ -134,16 +131,16 @@ class BeliefProp : public GroundSolver
|
||||
|
||||
virtual Params getJointByConditioning (const VarIds&) const;
|
||||
|
||||
BpLinks links_;
|
||||
unsigned nIters_;
|
||||
vector<SPNodeInfo*> varsI_;
|
||||
vector<SPNodeInfo*> facsI_;
|
||||
bool runned_;
|
||||
BpLinks links_;
|
||||
unsigned nIters_;
|
||||
std::vector<SPNodeInfo*> varsI_;
|
||||
std::vector<SPNodeInfo*> facsI_;
|
||||
bool runned_;
|
||||
|
||||
typedef multiset<BpLink*, CompareResidual> SortedOrder;
|
||||
typedef std::multiset<BpLink*, CompareResidual> SortedOrder;
|
||||
SortedOrder sortedOrder_;
|
||||
|
||||
typedef unordered_map<BpLink*, SortedOrder::iterator> BpLinkMap;
|
||||
typedef std::unordered_map<BpLink*, SortedOrder::iterator> BpLinkMap;
|
||||
BpLinkMap linkMap_;
|
||||
|
||||
static double accuracy_;
|
||||
|
@ -106,14 +106,14 @@ CTNode::copySubtree (const CTNode* root1)
|
||||
return new CTNode (*root1);
|
||||
}
|
||||
CTNode* root2 = new CTNode (*root1);
|
||||
typedef pair<const CTNode*, CTNode*> StackPair;
|
||||
vector<StackPair> stack = { StackPair (root1, root2) };
|
||||
typedef std::pair<const CTNode*, CTNode*> StackPair;
|
||||
std::vector<StackPair> stack = { StackPair (root1, root2) };
|
||||
while (stack.empty() == false) {
|
||||
const CTNode* n1 = stack.back().first;
|
||||
CTNode* n2 = stack.back().second;
|
||||
stack.pop_back();
|
||||
// cout << "n2 childs: " << n2->childs();
|
||||
// cout << "n1 childs: " << n1->childs();
|
||||
// std::cout << "n2 childs: " << n2->childs();
|
||||
// std::cout << "n1 childs: " << n1->childs();
|
||||
n2->childs().reserve (n1->nrChilds());
|
||||
stack.reserve (n1->nrChilds());
|
||||
for (CTChilds::const_iterator chIt = n1->childs().begin();
|
||||
@ -144,7 +144,7 @@ CTNode::deleteSubtree (CTNode* n)
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &out, const CTNode& n)
|
||||
std::ostream& operator<< (std::ostream &out, const CTNode& n)
|
||||
{
|
||||
out << "(" << n.level() << ") " ;
|
||||
out << n.symbol();
|
||||
@ -187,7 +187,8 @@ ConstraintTree::ConstraintTree (
|
||||
|
||||
|
||||
|
||||
ConstraintTree::ConstraintTree (vector<vector<string>> names)
|
||||
ConstraintTree::ConstraintTree (
|
||||
std::vector<std::vector<std::string>> names)
|
||||
{
|
||||
assert (names.empty() == false);
|
||||
assert (names.front().empty() == false);
|
||||
@ -502,7 +503,7 @@ ConstraintTree::tupleSet (const LogVars& originalLvs)
|
||||
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
|
||||
|
||||
if (originalLvs.size() != uniqueLvs.size()) {
|
||||
vector<size_t> indexes;
|
||||
std::vector<size_t> indexes;
|
||||
indexes.reserve (originalLvs.size());
|
||||
for (size_t i = 0; i < originalLvs.size(); i++) {
|
||||
indexes.push_back (Util::indexOf (uniqueLvs, originalLvs[i]));
|
||||
@ -530,21 +531,21 @@ ConstraintTree::exportToGraphViz (
|
||||
const char* fileName,
|
||||
bool showLogVars) const
|
||||
{
|
||||
ofstream out (fileName);
|
||||
std::ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
return;
|
||||
}
|
||||
out << "digraph {" << endl;
|
||||
out << "digraph {" << std::endl;
|
||||
ConstraintTree copy (*this);
|
||||
copy.moveToTop (copy.logVarSet_.elements());
|
||||
CTNodes nodes = getNodesBelow (copy.root_);
|
||||
out << "\"" << copy.root_ << "\"" << " [label=\"R\"]" << endl;
|
||||
out << "\"" << copy.root_ << "\"" << " [label=\"R\"]" << std::endl;
|
||||
for (CTNodes::const_iterator it = ++ nodes.begin();
|
||||
it != nodes.end(); ++ it) {
|
||||
out << "\"" << *it << "\"";
|
||||
out << " [label=\"" << **it << "\"]" ;
|
||||
out << endl;
|
||||
out << std::endl;
|
||||
}
|
||||
for (CTNodes::const_iterator it = nodes.begin();
|
||||
it != nodes.end(); ++ it) {
|
||||
@ -553,24 +554,24 @@ ConstraintTree::exportToGraphViz (
|
||||
chIt != childs.end(); ++ chIt) {
|
||||
out << "\"" << *it << "\"" ;
|
||||
out << " -> " ;
|
||||
out << "\"" << *chIt << "\"" << endl ;
|
||||
out << "\"" << *chIt << "\"" << std::endl ;
|
||||
}
|
||||
}
|
||||
if (showLogVars) {
|
||||
out << "Root [label=\"\", shape=plaintext]" << endl;
|
||||
out << "Root [label=\"\", shape=plaintext]" << std::endl;
|
||||
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 << "shape=plaintext, fontsize=14]" << std::endl;
|
||||
}
|
||||
out << "Root -> " << copy.logVars_[0];
|
||||
out << " [style=invis]" << endl;
|
||||
out << " [style=invis]" << std::endl;
|
||||
for (size_t i = 0; i < copy.logVars_.size() - 1; i++) {
|
||||
out << copy.logVars_[i] << " -> " << copy.logVars_[i + 1];
|
||||
out << " [style=invis]" << endl;
|
||||
out << " [style=invis]" << std::endl;
|
||||
}
|
||||
}
|
||||
out << "}" << endl;
|
||||
out << "}" <<std::endl;
|
||||
out.close();
|
||||
}
|
||||
|
||||
@ -701,9 +702,9 @@ ConstraintTree::split (
|
||||
split (root_, ct->root(), commChilds, exclChilds, stopLevel);
|
||||
ConstraintTree* commCt = new ConstraintTree (commChilds, logVars_);
|
||||
ConstraintTree* exclCt = new ConstraintTree (exclChilds, logVars_);
|
||||
// cout << commCt->tupleSet() << " + " ;
|
||||
// cout << exclCt->tupleSet() << " = " ;
|
||||
// cout << tupleSet() << endl;
|
||||
// std::cout << commCt->tupleSet() << " + " ;
|
||||
// std::cout << exclCt->tupleSet() << " = " ;
|
||||
// std::cout << tupleSet() << std::endl;
|
||||
assert ((commCt->tupleSet() | exclCt->tupleSet()) == tupleSet());
|
||||
assert ((exclCt->tupleSet (stopLevel) & ct->tupleSet (stopLevel)).empty());
|
||||
return {commCt, exclCt};
|
||||
@ -721,20 +722,20 @@ ConstraintTree::countNormalize (const LogVarSet& Ys)
|
||||
}
|
||||
moveToTop (Zs.elements());
|
||||
ConstraintTrees cts;
|
||||
unordered_map<unsigned, ConstraintTree*> countMap;
|
||||
std::unordered_map<unsigned, ConstraintTree*> countMap;
|
||||
unsigned stopLevel = getLevel (Zs.back());
|
||||
const CTChilds& childs = root_->childs();
|
||||
|
||||
for (CTChilds::const_iterator chIt = childs.begin();
|
||||
chIt != childs.end(); ++ chIt) {
|
||||
const vector<pair<CTNode*, unsigned>>& res =
|
||||
const std::vector<std::pair<CTNode*, unsigned>>& res =
|
||||
countNormalize (*chIt, stopLevel);
|
||||
for (size_t j = 0; j < res.size(); j++) {
|
||||
unordered_map<unsigned, ConstraintTree*>::iterator it
|
||||
std::unordered_map<unsigned, ConstraintTree*>::iterator it
|
||||
= countMap.find (res[j].second);
|
||||
if (it == countMap.end()) {
|
||||
ConstraintTree* newCt = new ConstraintTree (logVars_);
|
||||
it = countMap.insert (make_pair (res[j].second, newCt)).first;
|
||||
it = countMap.insert (std::make_pair (res[j].second, newCt)).first;
|
||||
cts.push_back (newCt);
|
||||
}
|
||||
it->second->root_->mergeSubtree (res[j].first);
|
||||
@ -754,31 +755,31 @@ ConstraintTree::jointCountNormalize (
|
||||
LogVar X_new2)
|
||||
{
|
||||
unsigned N = getConditionalCount (X);
|
||||
// cout << "My tuples: " << tupleSet() << endl;
|
||||
// cout << "CommCt tuples: " << commCt->tupleSet() << endl;
|
||||
// cout << "ExclCt tuples: " << exclCt->tupleSet() << endl;
|
||||
// cout << "Counted Lv: " << X << endl;
|
||||
// cout << "X_new1: " << X_new1 << endl;
|
||||
// cout << "X_new2: " << X_new2 << endl;
|
||||
// cout << "Original N: " << N << endl;
|
||||
// cout << endl;
|
||||
// std::cout << "My tuples: " << tupleSet() << std::endl;
|
||||
// std::cout << "CommCt tuples: " << commCt->tupleSet() << std::endl;
|
||||
// std::cout << "ExclCt tuples: " << exclCt->tupleSet() << std::endl;
|
||||
// std::cout << "Counted Lv: " << X << std::endl;
|
||||
// std::cout << "X_new1: " << X_new1 << std::endl;
|
||||
// std::cout << "X_new2: " << X_new2 << std::endl;
|
||||
// std::cout << "Original N: " << N << std::endl;
|
||||
// std::cout << endl;
|
||||
|
||||
ConstraintTrees normCts1 = commCt->countNormalize (X);
|
||||
vector<unsigned> counts1 (normCts1.size());
|
||||
std::vector<unsigned> counts1 (normCts1.size());
|
||||
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;
|
||||
// std::cout << "normCts1[" << i << "] #" << counts1[i] ;
|
||||
// std::cout << " " << normCts1[i]->tupleSet() << std::endl;
|
||||
}
|
||||
|
||||
ConstraintTrees normCts2 = exclCt->countNormalize (X);
|
||||
vector<unsigned> counts2 (normCts2.size());
|
||||
std::vector<unsigned> counts2 (normCts2.size());
|
||||
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;
|
||||
// std::cout << "normCts2[" << i << "] #" << counts2[i] ;
|
||||
// std::cout << " " << normCts2[i]->tupleSet() << std::endl;
|
||||
}
|
||||
// cout << endl;
|
||||
// std::cout << std::endl;
|
||||
|
||||
ConstraintTree* excl1 = 0;
|
||||
for (size_t i = 0; i < normCts1.size(); i++) {
|
||||
@ -786,7 +787,7 @@ ConstraintTree::jointCountNormalize (
|
||||
excl1 = normCts1[i];
|
||||
normCts1.erase (normCts1.begin() + i);
|
||||
counts1.erase (counts1.begin() + i);
|
||||
// cout << "joint-count(" << N << ",0)" << endl;
|
||||
// std::cout << "joint-count(" << N << ",0)" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -797,7 +798,7 @@ ConstraintTree::jointCountNormalize (
|
||||
excl2 = normCts2[i];
|
||||
normCts2.erase (normCts2.begin() + i);
|
||||
counts2.erase (counts2.begin() + i);
|
||||
// cout << "joint-count(0," << N << ")" << endl;
|
||||
// std::cout << "joint-count(0," << N << ")" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -805,8 +806,8 @@ ConstraintTree::jointCountNormalize (
|
||||
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] ;
|
||||
// cout << "," << counts2[j] << ")" << endl;
|
||||
// std::cout << "joint-count(" << counts1[i] ;
|
||||
// std::cout << "," << counts2[j] << ")" << std::endl;
|
||||
const CTChilds& childs = normCts2[j]->root_->childs();
|
||||
for (CTChilds::const_iterator chIt = childs.begin();
|
||||
chIt != childs.end(); ++ chIt) {
|
||||
@ -941,7 +942,7 @@ CTNodes
|
||||
ConstraintTree::getNodesBelow (CTNode* fromHere) const
|
||||
{
|
||||
CTNodes nodes;
|
||||
queue<CTNode*> queue;
|
||||
std::queue<CTNode*> queue;
|
||||
queue.push (fromHere);
|
||||
while (queue.empty() == false) {
|
||||
CTNode* node = queue.front();
|
||||
@ -1125,26 +1126,26 @@ ConstraintTree::nrSymbols (LogVar X)
|
||||
|
||||
|
||||
|
||||
vector<pair<CTNode*, unsigned>>
|
||||
std::vector<std::pair<CTNode*, unsigned>>
|
||||
ConstraintTree::countNormalize (
|
||||
const CTNode* n,
|
||||
unsigned stopLevel)
|
||||
{
|
||||
if (n->level() == stopLevel) {
|
||||
return vector<pair<CTNode*, unsigned>>() = {
|
||||
make_pair (CTNode::copySubtree (n), countTuples (n))
|
||||
return std::vector<std::pair<CTNode*, unsigned>>() = {
|
||||
std::make_pair (CTNode::copySubtree (n), countTuples (n))
|
||||
};
|
||||
}
|
||||
vector<pair<CTNode*, unsigned>> res;
|
||||
std::vector<std::pair<CTNode*, unsigned>> res;
|
||||
const CTChilds& childs = n->childs();
|
||||
for (CTChilds::const_iterator chIt = childs.begin();
|
||||
chIt != childs.end(); ++ chIt) {
|
||||
const vector<pair<CTNode*, unsigned>>& lowerRes =
|
||||
const std::vector<std::pair<CTNode*, unsigned>>& lowerRes =
|
||||
countNormalize (*chIt, stopLevel);
|
||||
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));
|
||||
res.push_back (std::make_pair (newNode, lowerRes[j].second));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
@ -10,14 +10,12 @@
|
||||
#include "TinySet.h"
|
||||
#include "LiftedUtils.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class CTNode;
|
||||
typedef vector<CTNode*> CTNodes;
|
||||
typedef std::vector<CTNode*> CTNodes;
|
||||
|
||||
class ConstraintTree;
|
||||
typedef vector<ConstraintTree*> ConstraintTrees;
|
||||
typedef std::vector<ConstraintTree*> ConstraintTrees;
|
||||
|
||||
|
||||
class CTNode
|
||||
@ -99,7 +97,7 @@ CTNode::findSymbol (Symbol symb)
|
||||
}
|
||||
|
||||
|
||||
ostream& operator<< (ostream &out, const CTNode&);
|
||||
std::ostream& operator<< (std::ostream &out, const CTNode&);
|
||||
|
||||
|
||||
class ConstraintTree
|
||||
@ -111,7 +109,7 @@ class ConstraintTree
|
||||
|
||||
ConstraintTree (const LogVars&, const Tuples&);
|
||||
|
||||
ConstraintTree (vector<vector<string>> names);
|
||||
ConstraintTree (std::vector<std::vector<std::string>> names);
|
||||
|
||||
ConstraintTree (const ConstraintTree&);
|
||||
|
||||
@ -208,7 +206,7 @@ class ConstraintTree
|
||||
|
||||
void getTuples (CTNode*, Tuples, unsigned, Tuples&, CTNodes&) const;
|
||||
|
||||
vector<std::pair<CTNode*, unsigned>> countNormalize (
|
||||
std::vector<std::pair<CTNode*, unsigned>> countNormalize (
|
||||
const CTNode*, unsigned);
|
||||
|
||||
static void split (
|
||||
|
@ -34,7 +34,7 @@ CountingBp::~CountingBp (void)
|
||||
void
|
||||
CountingBp::printSolverFlags (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "counting bp [" ;
|
||||
ss << "bp_msg_schedule=" ;
|
||||
switch (WeightedBp::msgSchedule()) {
|
||||
@ -48,7 +48,7 @@ CountingBp::printSolverFlags (void) const
|
||||
ss << ",log_domain=" << Util::toString (Globals::logDomain);
|
||||
ss << ",fif=" << Util::toString (CountingBp::fif_);
|
||||
ss << "]" ;
|
||||
cout << ss.str() << endl;
|
||||
std::cout << ss.str() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -69,7 +69,6 @@ CountingBp::solveQuery (VarIds queryVids)
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
if (idx == facNodes.size()) {
|
||||
res = GroundSolver::getJointByConditioning (
|
||||
@ -135,7 +134,7 @@ CountingBp::setInitialColors (void)
|
||||
unsigned range = varNodes[i]->range();
|
||||
VarColorMap::iterator it = colorMap.find (range);
|
||||
if (it == colorMap.end()) {
|
||||
it = colorMap.insert (make_pair (
|
||||
it = colorMap.insert (std::make_pair (
|
||||
range, Colors (range + 1, -1))).first;
|
||||
}
|
||||
unsigned idx = varNodes[i]->hasEvidence()
|
||||
@ -154,7 +153,8 @@ CountingBp::setInitialColors (void)
|
||||
unsigned distId = facNodes[i]->factor().distId();
|
||||
DistColorMap::iterator it = distColors.find (distId);
|
||||
if (it == distColors.end()) {
|
||||
it = distColors.insert (make_pair (distId, getNewColor())).first;
|
||||
it = distColors.insert (std::make_pair (
|
||||
distId, getNewColor())).first;
|
||||
}
|
||||
setColor (facNodes[i], it->second);
|
||||
}
|
||||
@ -182,7 +182,8 @@ CountingBp::createGroups (void)
|
||||
const VarSignature& signature = getSignature (varNodes[i]);
|
||||
VarSignMap::iterator it = varGroups.find (signature);
|
||||
if (it == varGroups.end()) {
|
||||
it = varGroups.insert (make_pair (signature, VarNodes())).first;
|
||||
it = varGroups.insert (std::make_pair (
|
||||
signature, VarNodes())).first;
|
||||
}
|
||||
it->second.push_back (varNodes[i]);
|
||||
}
|
||||
@ -202,7 +203,8 @@ CountingBp::createGroups (void)
|
||||
const FacSignature& signature = getSignature (facNodes[i]);
|
||||
FacSignMap::iterator it = facGroups.find (signature);
|
||||
if (it == facGroups.end()) {
|
||||
it = facGroups.insert (make_pair (signature, FacNodes())).first;
|
||||
it = facGroups.insert (std::make_pair (
|
||||
signature, FacNodes())).first;
|
||||
}
|
||||
it->second.push_back (facNodes[i]);
|
||||
}
|
||||
@ -235,7 +237,8 @@ CountingBp::createClusters (
|
||||
const VarNodes& groupVars = it->second;
|
||||
VarCluster* vc = new VarCluster (groupVars);
|
||||
for (size_t i = 0; i < groupVars.size(); i++) {
|
||||
varClusterMap_.insert (make_pair (groupVars[i]->varId(), vc));
|
||||
varClusterMap_.insert (std::make_pair (
|
||||
groupVars[i]->varId(), vc));
|
||||
}
|
||||
varClusters_.push_back (vc);
|
||||
}
|
||||
@ -264,12 +267,12 @@ CountingBp::getSignature (const VarNode* varNode)
|
||||
VarSignature sign;
|
||||
sign.reserve (neighs.size() + 1);
|
||||
for (size_t i = 0; i < neighs.size(); i++) {
|
||||
sign.push_back (make_pair (
|
||||
sign.push_back (std::make_pair (
|
||||
getColor (neighs[i]),
|
||||
neighs[i]->factor().indexOf (varNode->varId())));
|
||||
}
|
||||
std::sort (sign.begin(), sign.end());
|
||||
sign.push_back (make_pair (getColor (varNode), 0));
|
||||
sign.push_back (std::make_pair (getColor (varNode), 0));
|
||||
return sign;
|
||||
}
|
||||
|
||||
@ -342,10 +345,10 @@ CountingBp::getCompressedFactorGraph (void)
|
||||
|
||||
|
||||
|
||||
vector<vector<unsigned>>
|
||||
std::vector<std::vector<unsigned>>
|
||||
CountingBp::getWeights (void) const
|
||||
{
|
||||
vector<vector<unsigned>> weights;
|
||||
std::vector<std::vector<unsigned>> weights;
|
||||
weights.reserve (facClusters_.size());
|
||||
for (size_t i = 0; i < facClusters_.size(); i++) {
|
||||
const VarClusters& neighs = facClusters_[i]->varClusters();
|
||||
@ -390,31 +393,31 @@ CountingBp::printGroups (
|
||||
const FacSignMap& facGroups) const
|
||||
{
|
||||
unsigned count = 1;
|
||||
cout << "variable groups:" << endl;
|
||||
std::cout << "variable groups:" << std::endl;
|
||||
for (VarSignMap::const_iterator it = varGroups.begin();
|
||||
it != varGroups.end(); ++it) {
|
||||
const VarNodes& groupMembers = it->second;
|
||||
if (groupMembers.size() > 0) {
|
||||
cout << count << ": " ;
|
||||
std::cout << count << ": " ;
|
||||
for (size_t i = 0; i < groupMembers.size(); i++) {
|
||||
cout << groupMembers[i]->label() << " " ;
|
||||
std::cout << groupMembers[i]->label() << " " ;
|
||||
}
|
||||
count ++;
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
count = 1;
|
||||
cout << endl << "factor groups:" << endl;
|
||||
std::cout << std::endl << "factor groups:" << std::endl;
|
||||
for (FacSignMap::const_iterator it = facGroups.begin();
|
||||
it != facGroups.end(); ++it) {
|
||||
const FacNodes& groupMembers = it->second;
|
||||
if (groupMembers.size() > 0) {
|
||||
cout << ++count << ": " ;
|
||||
std::cout << ++count << ": " ;
|
||||
for (size_t i = 0; i < groupMembers.size(); i++) {
|
||||
cout << groupMembers[i]->getLabel() << " " ;
|
||||
std::cout << groupMembers[i]->getLabel() << " " ;
|
||||
}
|
||||
count ++;
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,31 +7,32 @@
|
||||
#include "FactorGraph.h"
|
||||
#include "Horus.h"
|
||||
|
||||
|
||||
class VarCluster;
|
||||
class FacCluster;
|
||||
class WeightedBp;
|
||||
|
||||
typedef long Color;
|
||||
typedef vector<Color> Colors;
|
||||
typedef vector<std::pair<Color,unsigned>> VarSignature;
|
||||
typedef vector<Color> FacSignature;
|
||||
typedef std::vector<Color> Colors;
|
||||
typedef std::vector<std::pair<Color,unsigned>> VarSignature;
|
||||
typedef std::vector<Color> FacSignature;
|
||||
|
||||
typedef unordered_map<unsigned, Color> DistColorMap;
|
||||
typedef unordered_map<unsigned, Colors> VarColorMap;
|
||||
typedef std::unordered_map<unsigned, Color> DistColorMap;
|
||||
typedef std::unordered_map<unsigned, Colors> VarColorMap;
|
||||
|
||||
typedef unordered_map<VarSignature, VarNodes> VarSignMap;
|
||||
typedef unordered_map<FacSignature, FacNodes> FacSignMap;
|
||||
typedef std::unordered_map<VarSignature, VarNodes> VarSignMap;
|
||||
typedef std::unordered_map<FacSignature, FacNodes> FacSignMap;
|
||||
|
||||
typedef unordered_map<VarId, VarCluster*> VarClusterMap;
|
||||
typedef std::unordered_map<VarId, VarCluster*> VarClusterMap;
|
||||
|
||||
typedef vector<VarCluster*> VarClusters;
|
||||
typedef vector<FacCluster*> FacClusters;
|
||||
typedef std::vector<VarCluster*> VarClusters;
|
||||
typedef std::vector<FacCluster*> FacClusters;
|
||||
|
||||
|
||||
template <class T>
|
||||
inline size_t hash_combine (size_t seed, const T& v)
|
||||
{
|
||||
return seed ^ (hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
|
||||
return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
|
||||
}
|
||||
|
||||
|
||||
@ -50,8 +51,8 @@ template <typename T> struct hash<std::vector<T>>
|
||||
size_t operator() (const std::vector<T>& vec) const
|
||||
{
|
||||
size_t h = 0;
|
||||
typename vector<T>::const_iterator first = vec.begin();
|
||||
typename vector<T>::const_iterator last = vec.end();
|
||||
typename std::vector<T>::const_iterator first = vec.begin();
|
||||
typename std::vector<T>::const_iterator last = vec.end();
|
||||
for (; first != last; ++first) {
|
||||
h = hash_combine (h, *first);
|
||||
}
|
||||
@ -152,7 +153,7 @@ class CountingBp : public GroundSolver
|
||||
|
||||
FactorGraph* getCompressedFactorGraph (void);
|
||||
|
||||
vector<vector<unsigned>> getWeights (void) const;
|
||||
std::vector<std::vector<unsigned>> getWeights (void) const;
|
||||
|
||||
unsigned getWeight (const FacCluster*,
|
||||
const VarCluster*, size_t index) const;
|
||||
|
@ -5,7 +5,7 @@
|
||||
ElimHeuristic ElimGraph::elimHeuristic_ = MIN_NEIGHBORS;
|
||||
|
||||
|
||||
ElimGraph::ElimGraph (const vector<Factor*>& factors)
|
||||
ElimGraph::ElimGraph (const std::vector<Factor*>& factors)
|
||||
{
|
||||
for (size_t i = 0; i < factors.size(); i++) {
|
||||
if (factors[i]) {
|
||||
@ -75,12 +75,12 @@ void
|
||||
ElimGraph::print (void) const
|
||||
{
|
||||
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||
cout << "node " << nodes_[i]->label() << " neighs:" ;
|
||||
std::cout << "node " << nodes_[i]->label() << " neighs:" ;
|
||||
EGNeighs neighs = nodes_[i]->neighbors();
|
||||
for (size_t j = 0; j < neighs.size(); j++) {
|
||||
cout << " " << neighs[j]->label();
|
||||
std::cout << " " << neighs[j]->label();
|
||||
}
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,25 +92,26 @@ ElimGraph::exportToGraphViz (
|
||||
bool showNeighborless,
|
||||
const VarIds& highlightVarIds) const
|
||||
{
|
||||
ofstream out (fileName);
|
||||
std::ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
return;
|
||||
}
|
||||
out << "strict graph {" << endl;
|
||||
out << "strict graph {" << std::endl;
|
||||
for (size_t i = 0; i < nodes_.size(); i++) {
|
||||
if (showNeighborless || nodes_[i]->neighbors().empty() == false) {
|
||||
out << '"' << nodes_[i]->label() << '"' << endl;
|
||||
out << '"' << nodes_[i]->label() << '"' << std::endl;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < highlightVarIds.size(); i++) {
|
||||
EgNode* node =getEgNode (highlightVarIds[i]);
|
||||
if (node) {
|
||||
out << '"' << node->label() << '"' ;
|
||||
out << " [shape=box3d]" << endl;
|
||||
out << " [shape=box3d]" << std::endl;
|
||||
} else {
|
||||
cerr << "Error: invalid variable id: " << highlightVarIds[i] << "." ;
|
||||
cerr << endl;
|
||||
std::cerr << "Error: invalid variable id: " ;
|
||||
std::cerr << highlightVarIds[i] << "." ;
|
||||
std::cerr << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@ -118,10 +119,10 @@ ElimGraph::exportToGraphViz (
|
||||
EGNeighs neighs = nodes_[i]->neighbors();
|
||||
for (size_t j = 0; j < neighs.size(); j++) {
|
||||
out << '"' << nodes_[i]->label() << '"' << " -- " ;
|
||||
out << '"' << neighs[j]->label() << '"' << endl;
|
||||
out << '"' << neighs[j]->label() << '"' << std::endl;
|
||||
}
|
||||
}
|
||||
out << "}" << endl;
|
||||
out << "}" << std::endl;
|
||||
out.close();
|
||||
}
|
||||
|
||||
@ -154,7 +155,7 @@ ElimGraph::addNode (EgNode* n)
|
||||
{
|
||||
nodes_.push_back (n);
|
||||
n->setIndex (nodes_.size() - 1);
|
||||
varMap_.insert (make_pair (n->varId(), n));
|
||||
varMap_.insert (std::make_pair (n->varId(), n));
|
||||
}
|
||||
|
||||
|
||||
@ -162,7 +163,7 @@ ElimGraph::addNode (EgNode* n)
|
||||
EgNode*
|
||||
ElimGraph::getEgNode (VarId vid) const
|
||||
{
|
||||
unordered_map<VarId, EgNode*>::const_iterator it;
|
||||
std::unordered_map<VarId, EgNode*>::const_iterator it;
|
||||
it = varMap_.find (vid);
|
||||
return (it != varMap_.end()) ? it->second : 0;
|
||||
}
|
||||
|
@ -7,7 +7,11 @@
|
||||
#include "TinySet.h"
|
||||
#include "Horus.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class EgNode;
|
||||
|
||||
typedef TinySet<EgNode*> EGNeighs;
|
||||
|
||||
|
||||
enum ElimHeuristic
|
||||
{
|
||||
@ -19,11 +23,6 @@ enum ElimHeuristic
|
||||
};
|
||||
|
||||
|
||||
class EgNode;
|
||||
|
||||
typedef TinySet<EgNode*> EGNeighs;
|
||||
|
||||
|
||||
class EgNode : public Var
|
||||
{
|
||||
public:
|
||||
@ -83,9 +82,9 @@ class ElimGraph
|
||||
|
||||
void connectAllNeighbors (const EgNode*);
|
||||
|
||||
vector<EgNode*> nodes_;
|
||||
TinySet<EgNode*> unmarked_;
|
||||
unordered_map<VarId, EgNode*> varMap_;
|
||||
std::vector<EgNode*> nodes_;
|
||||
TinySet<EgNode*> unmarked_;
|
||||
std::unordered_map<VarId, EgNode*> varMap_;
|
||||
|
||||
static ElimHeuristic elimHeuristic_;
|
||||
|
||||
|
@ -77,7 +77,7 @@ Factor::sumOutAllExcept (VarId vid)
|
||||
void
|
||||
Factor::sumOutAllExcept (const VarIds& vids)
|
||||
{
|
||||
vector<bool> mask (args_.size(), false);
|
||||
std::vector<bool> mask (args_.size(), false);
|
||||
for (unsigned i = 0; i < vids.size(); i++) {
|
||||
assert (indexOf (vids[i]) != args_.size());
|
||||
mask[indexOf (vids[i])] = true;
|
||||
@ -91,7 +91,7 @@ void
|
||||
Factor::sumOutAllExceptIndex (size_t idx)
|
||||
{
|
||||
assert (idx < args_.size());
|
||||
vector<bool> mask (args_.size(), false);
|
||||
std::vector<bool> mask (args_.size(), false);
|
||||
mask[idx] = true;
|
||||
sumOutArgs (mask);
|
||||
}
|
||||
@ -109,10 +109,10 @@ Factor::multiply (Factor& g)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
Factor::getLabel (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "f(" ;
|
||||
for (size_t i = 0; i < args_.size(); i++) {
|
||||
if (i != 0) ss << "," ;
|
||||
@ -131,13 +131,13 @@ Factor::print (void) const
|
||||
for (size_t i = 0; i < args_.size(); i++) {
|
||||
vars.push_back (new Var (args_[i], ranges_[i]));
|
||||
}
|
||||
vector<string> jointStrings = Util::getStateLines (vars);
|
||||
std::vector<std::string> jointStrings = Util::getStateLines (vars);
|
||||
for (size_t i = 0; i < params_.size(); i++) {
|
||||
// cout << "[" << distId_ << "] " ;
|
||||
cout << "f(" << jointStrings[i] << ")" ;
|
||||
cout << " = " << params_[i] << endl;
|
||||
std::cout << "f(" << jointStrings[i] << ")" ;
|
||||
std::cout << " = " << params_[i] << std::endl;
|
||||
}
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
for (size_t i = 0; i < vars.size(); i++) {
|
||||
delete vars[i];
|
||||
}
|
||||
@ -192,7 +192,7 @@ Factor::sumOutLastVariable (void)
|
||||
|
||||
|
||||
void
|
||||
Factor::sumOutArgs (const vector<bool>& mask)
|
||||
Factor::sumOutArgs (const std::vector<bool>& mask)
|
||||
{
|
||||
assert (mask.size() == args_.size());
|
||||
size_t new_size = 1;
|
||||
|
@ -6,17 +6,13 @@
|
||||
#include "Indexer.h"
|
||||
#include "Util.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
template <typename T>
|
||||
class TFactor
|
||||
{
|
||||
public:
|
||||
const vector<T>& arguments (void) const { return args_; }
|
||||
const std::vector<T>& arguments (void) const { return args_; }
|
||||
|
||||
vector<T>& arguments (void) { return args_; }
|
||||
std::vector<T>& arguments (void) { return args_; }
|
||||
|
||||
const Ranges& ranges (void) const { return ranges_; }
|
||||
|
||||
@ -52,19 +48,19 @@ class TFactor
|
||||
|
||||
void absorveEvidence (const T& arg, unsigned obsIdx);
|
||||
|
||||
void reorderArguments (const vector<T>& new_args);
|
||||
void reorderArguments (const std::vector<T>& new_args);
|
||||
|
||||
bool contains (const T& arg) const;
|
||||
|
||||
bool contains (const vector<T>& args) const;
|
||||
bool contains (const std::vector<T>& args) const;
|
||||
|
||||
double& operator[] (size_t idx);
|
||||
|
||||
protected:
|
||||
vector<T> args_;
|
||||
Ranges ranges_;
|
||||
Params params_;
|
||||
unsigned distId_;
|
||||
std::vector<T> args_;
|
||||
Ranges ranges_;
|
||||
Params params_;
|
||||
unsigned distId_;
|
||||
|
||||
private:
|
||||
void extend (unsigned range_prod);
|
||||
@ -141,7 +137,7 @@ TFactor<T>::multiply (TFactor<T>& g)
|
||||
}
|
||||
unsigned range_prod = 1;
|
||||
bool share_arguments = false;
|
||||
const vector<T>& g_args = g.arguments();
|
||||
const std::vector<T>& g_args = g.arguments();
|
||||
const Ranges& g_ranges = g.ranges();
|
||||
const Params& g_params = g.params();
|
||||
for (size_t i = 0; i < g_args.size(); i++) {
|
||||
@ -225,7 +221,7 @@ TFactor<T>::absorveEvidence (const T& arg, unsigned obsIdx)
|
||||
|
||||
|
||||
template <typename T> inline void
|
||||
TFactor<T>::reorderArguments (const vector<T>& new_args)
|
||||
TFactor<T>::reorderArguments (const std::vector<T>& new_args)
|
||||
{
|
||||
assert (new_args.size() == args_.size());
|
||||
if (new_args == args_) {
|
||||
@ -259,7 +255,7 @@ TFactor<T>::contains (const T& arg) const
|
||||
|
||||
|
||||
template <typename T> inline bool
|
||||
TFactor<T>::contains (const vector<T>& args) const
|
||||
TFactor<T>::contains (const std::vector<T>& args) const
|
||||
{
|
||||
for (size_t i = 0; i < args.size(); i++) {
|
||||
if (contains (args[i]) == false) {
|
||||
@ -348,7 +344,7 @@ class Factor : public TFactor<VarId>
|
||||
|
||||
void multiply (Factor&);
|
||||
|
||||
string getLabel (void) const;
|
||||
std::string getLabel (void) const;
|
||||
|
||||
void print (void) const;
|
||||
|
||||
@ -357,7 +353,7 @@ class Factor : public TFactor<VarId>
|
||||
|
||||
void sumOutLastVariable (void);
|
||||
|
||||
void sumOutArgs (const vector<bool>& mask);
|
||||
void sumOutArgs (const std::vector<bool>& mask);
|
||||
|
||||
void clone (const Factor& f);
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include <iostream>
|
||||
@ -55,18 +54,18 @@ FactorGraph::readFromUaiFormat (const char* fileName)
|
||||
{
|
||||
std::ifstream is (fileName);
|
||||
if (!is.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
ignoreLines (is);
|
||||
string line;
|
||||
std::string line;
|
||||
getline (is, line);
|
||||
if (line == "BAYES") {
|
||||
bayesFactors_ = true;
|
||||
} else if (line == "MARKOV") {
|
||||
bayesFactors_ = false;
|
||||
} else {
|
||||
cerr << "Error: the type of network is missing." << endl;
|
||||
std::cerr << "Error: the type of network is missing." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// read the number of vars
|
||||
@ -83,8 +82,8 @@ FactorGraph::readFromUaiFormat (const char* fileName)
|
||||
unsigned nrArgs;
|
||||
unsigned vid;
|
||||
is >> nrFactors;
|
||||
vector<VarIds> allVarIds;
|
||||
vector<Ranges> allRanges;
|
||||
std::vector<VarIds> allVarIds;
|
||||
std::vector<Ranges> allRanges;
|
||||
for (unsigned i = 0; i < nrFactors; i++) {
|
||||
ignoreLines (is);
|
||||
is >> nrArgs;
|
||||
@ -93,9 +92,10 @@ FactorGraph::readFromUaiFormat (const char* fileName)
|
||||
for (unsigned j = 0; j < nrArgs; j++) {
|
||||
is >> vid;
|
||||
if (vid >= ranges.size()) {
|
||||
cerr << "Error: invalid variable identifier `" << vid << "'. " ;
|
||||
cerr << "Identifiers must be between 0 and " << ranges.size() - 1 ;
|
||||
cerr << "." << endl;
|
||||
std::cerr << "Error: invalid variable identifier `" << vid << "'. " ;
|
||||
std::cerr << "Identifiers must be between 0 and " ;
|
||||
std::cerr << ranges.size() - 1 ;
|
||||
std::cerr << "." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
allVarIds.back().push_back (vid);
|
||||
@ -108,9 +108,9 @@ FactorGraph::readFromUaiFormat (const char* fileName)
|
||||
ignoreLines (is);
|
||||
is >> nrParams;
|
||||
if (nrParams != Util::sizeExpected (allRanges[i])) {
|
||||
cerr << "Error: invalid number of parameters for factor nº " << i ;
|
||||
cerr << ", " << Util::sizeExpected (allRanges[i]);
|
||||
cerr << " expected, " << nrParams << " given." << endl;
|
||||
std::cerr << "Error: invalid number of parameters for factor nº " << i ;
|
||||
std::cerr << ", " << Util::sizeExpected (allRanges[i]);
|
||||
std::cerr << " expected, " << nrParams << " given." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
Params params (nrParams);
|
||||
@ -139,7 +139,7 @@ FactorGraph::readFromLibDaiFormat (const char* fileName)
|
||||
{
|
||||
std::ifstream is (fileName);
|
||||
if (!is.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
ignoreLines (is);
|
||||
@ -164,8 +164,8 @@ FactorGraph::readFromLibDaiFormat (const char* fileName)
|
||||
is >> ranges[j];
|
||||
VarNode* var = getVarNode (vids[j]);
|
||||
if (var && ranges[j] != var->range()) {
|
||||
cerr << "Error: variable `" << vids[j] << "' appears in two or " ;
|
||||
cerr << "more factors with a different range." << endl;
|
||||
std::cerr << "Error: variable `" << vids[j] << "' appears in two or " ;
|
||||
std::cerr << "more factors with a different range." << std::endl;
|
||||
}
|
||||
}
|
||||
// read parameters
|
||||
@ -221,7 +221,7 @@ FactorGraph::addVarNode (VarNode* vn)
|
||||
{
|
||||
varNodes_.push_back (vn);
|
||||
vn->setIndex (varNodes_.size() - 1);
|
||||
varMap_.insert (make_pair (vn->varId(), vn));
|
||||
varMap_.insert (std::make_pair (vn->varId(), vn));
|
||||
}
|
||||
|
||||
|
||||
@ -275,6 +275,8 @@ FactorGraph::getStructure (void)
|
||||
void
|
||||
FactorGraph::print (void) const
|
||||
{
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||
cout << "var id = " << varNodes_[i]->varId() << endl;
|
||||
cout << "label = " << varNodes_[i]->label() << endl;
|
||||
@ -296,28 +298,28 @@ FactorGraph::print (void) const
|
||||
void
|
||||
FactorGraph::exportToLibDai (const char* fileName) const
|
||||
{
|
||||
ofstream out (fileName);
|
||||
std::ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
return;
|
||||
}
|
||||
out << facNodes_.size() << endl << endl;
|
||||
out << facNodes_.size() << std::endl << std::endl;
|
||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||
Factor f (facNodes_[i]->factor());
|
||||
out << f.nrArguments() << endl;
|
||||
out << Util::elementsToString (f.arguments()) << endl;
|
||||
out << Util::elementsToString (f.ranges()) << endl;
|
||||
out << f.nrArguments() << std::endl;
|
||||
out << Util::elementsToString (f.arguments()) << std::endl;
|
||||
out << Util::elementsToString (f.ranges()) << std::endl;
|
||||
VarIds args = f.arguments();
|
||||
std::reverse (args.begin(), args.end());
|
||||
f.reorderArguments (args);
|
||||
if (Globals::logDomain) {
|
||||
Util::exp (f.params());
|
||||
}
|
||||
out << f.size() << endl;
|
||||
out << f.size() << std::endl;
|
||||
for (size_t j = 0; j < f.size(); j++) {
|
||||
out << j << " " << f[j] << endl;
|
||||
out << j << " " << f[j] << std::endl;
|
||||
}
|
||||
out << endl;
|
||||
out << std::endl;
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
@ -327,28 +329,29 @@ FactorGraph::exportToLibDai (const char* fileName) const
|
||||
void
|
||||
FactorGraph::exportToUai (const char* fileName) const
|
||||
{
|
||||
ofstream out (fileName);
|
||||
std::ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
return;
|
||||
}
|
||||
out << (bayesFactors_ ? "BAYES" : "MARKOV") ;
|
||||
out << endl << endl;
|
||||
out << varNodes_.size() << endl;
|
||||
out << std::endl << std::endl;
|
||||
out << varNodes_.size() << std::endl;
|
||||
VarNodes sortedVns = varNodes_;
|
||||
std::sort (sortedVns.begin(), sortedVns.end(), sortByVarId());
|
||||
for (size_t i = 0; i < sortedVns.size(); i++) {
|
||||
out << ((i != 0) ? " " : "") << sortedVns[i]->range();
|
||||
}
|
||||
out << endl << facNodes_.size() << endl;
|
||||
out << std::endl << facNodes_.size() << std::endl;
|
||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||
VarIds args = facNodes_[i]->factor().arguments();
|
||||
if (bayesFactors_) {
|
||||
std::swap (args.front(), args.back());
|
||||
}
|
||||
out << args.size() << " " << Util::elementsToString (args) << endl;
|
||||
out << args.size() << " " << Util::elementsToString (args);
|
||||
out << std::endl;
|
||||
}
|
||||
out << endl;
|
||||
out << std::endl;
|
||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||
Factor f = facNodes_[i]->factor();
|
||||
if (bayesFactors_) {
|
||||
@ -360,8 +363,9 @@ FactorGraph::exportToUai (const char* fileName) const
|
||||
if (Globals::logDomain) {
|
||||
Util::exp (params);
|
||||
}
|
||||
out << params.size() << endl << " " ;
|
||||
out << Util::elementsToString (params) << endl << endl;
|
||||
out << params.size() << std::endl << " " ;
|
||||
out << Util::elementsToString (params);
|
||||
out << std::endl << std::endl;
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
@ -371,32 +375,32 @@ FactorGraph::exportToUai (const char* fileName) const
|
||||
void
|
||||
FactorGraph::exportToGraphViz (const char* fileName) const
|
||||
{
|
||||
ofstream out (fileName);
|
||||
std::ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
return;
|
||||
}
|
||||
out << "graph \"" << fileName << "\" {" << endl;
|
||||
out << "graph \"" << fileName << "\" {" << std::endl;
|
||||
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||
if (varNodes_[i]->hasEvidence()) {
|
||||
out << '"' << varNodes_[i]->label() << '"' ;
|
||||
out << " [style=filled, fillcolor=yellow]" << endl;
|
||||
out << " [style=filled, fillcolor=yellow]" << std::endl;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
||||
out << " [label=\"" << facNodes_[i]->getLabel();
|
||||
out << "\"" << ", shape=box]" << endl;
|
||||
out << "\"" << ", shape=box]" << std::endl;
|
||||
}
|
||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||
const VarNodes& myVars = facNodes_[i]->neighbors();
|
||||
for (size_t j = 0; j < myVars.size(); j++) {
|
||||
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
||||
out << " -- " ;
|
||||
out << '"' << myVars[j]->label() << '"' << endl;
|
||||
out << '"' << myVars[j]->label() << '"' << std::endl;
|
||||
}
|
||||
}
|
||||
out << "}" << endl;
|
||||
out << "}" << std::endl;
|
||||
out.close();
|
||||
}
|
||||
|
||||
@ -405,7 +409,7 @@ FactorGraph::exportToGraphViz (const char* fileName) const
|
||||
void
|
||||
FactorGraph::ignoreLines (std::ifstream& is) const
|
||||
{
|
||||
string ignoreStr;
|
||||
std::string ignoreStr;
|
||||
while (is.peek() == '#' || is.peek() == '\n') {
|
||||
getline (is, ignoreStr);
|
||||
}
|
||||
@ -416,8 +420,8 @@ FactorGraph::ignoreLines (std::ifstream& is) const
|
||||
bool
|
||||
FactorGraph::containsCycle (void) const
|
||||
{
|
||||
vector<bool> visitedVars (varNodes_.size(), false);
|
||||
vector<bool> visitedFactors (facNodes_.size(), false);
|
||||
std::vector<bool> visitedVars (varNodes_.size(), false);
|
||||
std::vector<bool> visitedFactors (facNodes_.size(), false);
|
||||
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||
int v = varNodes_[i]->getIndex();
|
||||
if (!visitedVars[v]) {
|
||||
@ -435,8 +439,8 @@ bool
|
||||
FactorGraph::containsCycle (
|
||||
const VarNode* v,
|
||||
const FacNode* p,
|
||||
vector<bool>& visitedVars,
|
||||
vector<bool>& visitedFactors) const
|
||||
std::vector<bool>& visitedVars,
|
||||
std::vector<bool>& visitedFactors) const
|
||||
{
|
||||
visitedVars[v->getIndex()] = true;
|
||||
const FacNodes& adjacencies = v->neighbors();
|
||||
@ -460,8 +464,8 @@ bool
|
||||
FactorGraph::containsCycle (
|
||||
const FacNode* v,
|
||||
const VarNode* p,
|
||||
vector<bool>& visitedVars,
|
||||
vector<bool>& visitedFactors) const
|
||||
std::vector<bool>& visitedVars,
|
||||
std::vector<bool>& visitedFactors) const
|
||||
{
|
||||
visitedFactors[v->getIndex()] = true;
|
||||
const VarNodes& adjacencies = v->neighbors();
|
||||
|
@ -7,10 +7,9 @@
|
||||
#include "BayesBallGraph.h"
|
||||
#include "Horus.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class FacNode;
|
||||
|
||||
|
||||
class VarNode : public Var
|
||||
{
|
||||
public:
|
||||
@ -49,7 +48,7 @@ class FacNode
|
||||
|
||||
void setIndex (size_t index) { index_ = index; }
|
||||
|
||||
string getLabel (void) { return factor_.getLabel(); }
|
||||
std::string getLabel (void) { return factor_.getLabel(); }
|
||||
|
||||
private:
|
||||
VarNodes neighs_;
|
||||
@ -138,10 +137,10 @@ class FactorGraph
|
||||
bool containsCycle (void) const;
|
||||
|
||||
bool containsCycle (const VarNode*, const FacNode*,
|
||||
vector<bool>&, vector<bool>&) const;
|
||||
std::vector<bool>&, std::vector<bool>&) const;
|
||||
|
||||
bool containsCycle (const FacNode*, const VarNode*,
|
||||
vector<bool>&, vector<bool>&) const;
|
||||
std::vector<bool>&, std::vector<bool>&) const;
|
||||
|
||||
VarNodes varNodes_;
|
||||
FacNodes facNodes_;
|
||||
@ -149,7 +148,7 @@ class FactorGraph
|
||||
BayesBallGraph structure_;
|
||||
bool bayesFactors_;
|
||||
|
||||
typedef unordered_map<unsigned, VarNode*> VarMap;
|
||||
typedef std::unordered_map<unsigned, VarNode*> VarMap;
|
||||
VarMap varMap_;
|
||||
|
||||
static bool exportLd_;
|
||||
|
@ -19,13 +19,14 @@ GroundSolver::printAnswer (const VarIds& vids)
|
||||
}
|
||||
if (unobservedVids.empty() == false) {
|
||||
Params res = solveQuery (unobservedVids);
|
||||
vector<string> stateLines = Util::getStateLines (unobservedVars);
|
||||
std::vector<std::string> stateLines =
|
||||
Util::getStateLines (unobservedVars);
|
||||
for (size_t i = 0; i < res.size(); i++) {
|
||||
cout << "P(" << stateLines[i] << ") = " ;
|
||||
cout << std::setprecision (Constants::PRECISION) << res[i];
|
||||
cout << endl;
|
||||
std::cout << "P(" << stateLines[i] << ") = " ;
|
||||
std::cout << std::setprecision (Constants::PRECISION) << res[i];
|
||||
std::cout << std::endl;
|
||||
}
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,10 @@
|
||||
#ifndef PACKAGES_CLPBN_HORUS_GROUNDSOLVER_H
|
||||
#define PACKAGES_CLPBN_HORUS_GROUNDSOLVER_H
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
#include "FactorGraph.h"
|
||||
#include "Horus.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
class GroundSolver
|
||||
{
|
||||
public:
|
||||
|
@ -59,12 +59,12 @@ HistogramSet::reset (void)
|
||||
|
||||
|
||||
|
||||
vector<Histogram>
|
||||
std::vector<Histogram>
|
||||
HistogramSet::getHistograms (unsigned N, unsigned R)
|
||||
{
|
||||
HistogramSet hs (N, R);
|
||||
unsigned H = hs.nrHistograms();
|
||||
vector<Histogram> histograms;
|
||||
std::vector<Histogram> histograms;
|
||||
histograms.reserve (H);
|
||||
for (unsigned i = 0; i < H; i++) {
|
||||
histograms.push_back (hs.hist_);
|
||||
@ -86,9 +86,9 @@ HistogramSet::nrHistograms (unsigned N, unsigned R)
|
||||
size_t
|
||||
HistogramSet::findIndex (
|
||||
const Histogram& h,
|
||||
const vector<Histogram>& hists)
|
||||
const std::vector<Histogram>& hists)
|
||||
{
|
||||
vector<Histogram>::const_iterator it = std::lower_bound (
|
||||
std::vector<Histogram>::const_iterator it = std::lower_bound (
|
||||
hists.begin(), hists.end(), h, std::greater<Histogram>());
|
||||
assert (it != hists.end() && *it == h);
|
||||
return std::distance (hists.begin(), it);
|
||||
@ -96,13 +96,13 @@ HistogramSet::findIndex (
|
||||
|
||||
|
||||
|
||||
vector<double>
|
||||
std::vector<double>
|
||||
HistogramSet::getNumAssigns (unsigned N, unsigned R)
|
||||
{
|
||||
HistogramSet hs (N, R);
|
||||
double N_fac = Util::logFactorial (N);
|
||||
unsigned H = hs.nrHistograms();
|
||||
vector<double> numAssigns;
|
||||
std::vector<double> numAssigns;
|
||||
numAssigns.reserve (H);
|
||||
for (unsigned h = 0; h < H; h++) {
|
||||
double prod = 0.0;
|
||||
@ -138,7 +138,7 @@ HistogramSet::clearAfter (size_t idx)
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const HistogramSet& hs)
|
||||
std::ostream& operator<< (std::ostream &os, const HistogramSet& hs)
|
||||
{
|
||||
os << "#" << hs.hist_;
|
||||
return os;
|
||||
|
@ -7,9 +7,8 @@
|
||||
|
||||
#include "Horus.h"
|
||||
|
||||
using namespace std;
|
||||
typedef std::vector<unsigned> Histogram;
|
||||
|
||||
typedef vector<unsigned> Histogram;
|
||||
|
||||
class HistogramSet
|
||||
{
|
||||
@ -24,14 +23,14 @@ class HistogramSet
|
||||
|
||||
void reset (void);
|
||||
|
||||
static vector<Histogram> getHistograms (unsigned, unsigned);
|
||||
static std::vector<Histogram> getHistograms (unsigned, unsigned);
|
||||
|
||||
static unsigned nrHistograms (unsigned, unsigned);
|
||||
|
||||
static size_t findIndex (
|
||||
const Histogram&, const vector<Histogram>&);
|
||||
const Histogram&, const std::vector<Histogram>&);
|
||||
|
||||
static vector<double> getNumAssigns (unsigned, unsigned);
|
||||
static std::vector<double> getNumAssigns (unsigned, unsigned);
|
||||
|
||||
private:
|
||||
unsigned maxCount (size_t) const;
|
||||
@ -41,7 +40,7 @@ class HistogramSet
|
||||
unsigned size_;
|
||||
Histogram hist_;
|
||||
|
||||
friend std::ostream& operator<< (ostream &os, const HistogramSet& hs);
|
||||
friend std::ostream& operator<< (std::ostream &os, const HistogramSet& hs);
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN (HistogramSet);
|
||||
};
|
||||
|
@ -1,22 +1,20 @@
|
||||
#include <cstdlib>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "FactorGraph.h"
|
||||
#include "VarElim.h"
|
||||
#include "BeliefProp.h"
|
||||
#include "CountingBp.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int readHorusFlags (int, const char* []);
|
||||
|
||||
void readFactorGraph (FactorGraph&, const char*);
|
||||
|
||||
VarIds readQueryAndEvidence (FactorGraph&, int, const char* [], int);
|
||||
|
||||
void runSolver (const FactorGraph&, const VarIds&);
|
||||
|
||||
const string USAGE = "usage: ./hcli [solver=hve|bp|cbp] \
|
||||
const std::string USAGE = "usage: ./hcli [solver=hve|bp|cbp] \
|
||||
[<OPTION>=<VALUE>]... <FILE> [<VAR>|<VAR>=<EVIDENCE>]... " ;
|
||||
|
||||
|
||||
@ -24,8 +22,9 @@ int
|
||||
main (int argc, const char* argv[])
|
||||
{
|
||||
if (argc <= 1) {
|
||||
cerr << "Error: no probabilistic graphical model was given." << endl;
|
||||
cerr << USAGE << endl;
|
||||
std::cerr << "Error: no probabilistic graphical model was given." ;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << USAGE << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
int idx = readHorusFlags (argc, argv);
|
||||
@ -45,9 +44,9 @@ main (int argc, const char* argv[])
|
||||
fg.print();
|
||||
}
|
||||
if (Globals::verbosity > 0) {
|
||||
cout << "factor graph contains " ;
|
||||
cout << fg.nrVarNodes() << " variables and " ;
|
||||
cout << fg.nrFacNodes() << " factors " << endl;
|
||||
std::cout << "factor graph contains " ;
|
||||
std::cout << fg.nrVarNodes() << " variables and " ;
|
||||
std::cout << fg.nrFacNodes() << " factors " << std::endl;
|
||||
}
|
||||
runSolver (fg, queryIds);
|
||||
return 0;
|
||||
@ -60,21 +59,21 @@ readHorusFlags (int argc, const char* argv[])
|
||||
{
|
||||
int i = 1;
|
||||
for (; i < argc; i++) {
|
||||
const string& arg = argv[i];
|
||||
const std::string& arg = argv[i];
|
||||
size_t pos = arg.find ('=');
|
||||
if (pos == std::string::npos) {
|
||||
return i;
|
||||
}
|
||||
string leftArg = arg.substr (0, pos);
|
||||
string rightArg = arg.substr (pos + 1);
|
||||
std::string leftArg = arg.substr (0, pos);
|
||||
std::string rightArg = arg.substr (pos + 1);
|
||||
if (leftArg.empty()) {
|
||||
cerr << "Error: missing left argument." << endl;
|
||||
cerr << USAGE << endl;
|
||||
std::cerr << "Error: missing left argument." << std::endl;
|
||||
std::cerr << USAGE << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (rightArg.empty()) {
|
||||
cerr << "Error: missing right argument." << endl;
|
||||
cerr << USAGE << endl;
|
||||
std::cerr << "Error: missing right argument." << std::endl;
|
||||
std::cerr << USAGE << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
Util::setHorusFlag (leftArg, rightArg);
|
||||
@ -87,15 +86,15 @@ readHorusFlags (int argc, const char* argv[])
|
||||
void
|
||||
readFactorGraph (FactorGraph& fg, const char* s)
|
||||
{
|
||||
string fileName (s);
|
||||
string extension = fileName.substr (fileName.find_last_of ('.') + 1);
|
||||
std::string fileName (s);
|
||||
std::string extension = fileName.substr (fileName.find_last_of ('.') + 1);
|
||||
if (extension == "uai") {
|
||||
fg.readFromUaiFormat (fileName.c_str());
|
||||
} else if (extension == "fg") {
|
||||
fg.readFromLibDaiFormat (fileName.c_str());
|
||||
} else {
|
||||
cerr << "Error: the probabilistic graphical model must be " ;
|
||||
cerr << "defined either in a UAI or libDAI file." << endl;
|
||||
std::cerr << "Error: the probabilistic graphical model must be " ;
|
||||
std::cerr << "defined either in a UAI or libDAI file." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@ -111,58 +110,58 @@ readQueryAndEvidence (
|
||||
{
|
||||
VarIds queryIds;
|
||||
for (int i = start; i < argc; i++) {
|
||||
const string& arg = argv[i];
|
||||
const std::string& arg = argv[i];
|
||||
if (arg.find ('=') == std::string::npos) {
|
||||
if (Util::isInteger (arg) == false) {
|
||||
cerr << "Error: `" << arg << "' " ;
|
||||
cerr << "is not a variable id." ;
|
||||
cerr << endl;
|
||||
std::cerr << "Error: `" << arg << "' " ;
|
||||
std::cerr << "is not a variable id." ;
|
||||
std::cerr << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
VarId vid = Util::stringToUnsigned (arg);
|
||||
VarNode* queryVar = fg.getVarNode (vid);
|
||||
if (queryVar == false) {
|
||||
cerr << "Error: unknow variable with id " ;
|
||||
cerr << "`" << vid << "'." << endl;
|
||||
std::cerr << "Error: unknow variable with id " ;
|
||||
std::cerr << "`" << vid << "'." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
queryIds.push_back (vid);
|
||||
} else {
|
||||
size_t pos = arg.find ('=');
|
||||
string leftArg = arg.substr (0, pos);
|
||||
string rightArg = arg.substr (pos + 1);
|
||||
std::string leftArg = arg.substr (0, pos);
|
||||
std::string rightArg = arg.substr (pos + 1);
|
||||
if (leftArg.empty()) {
|
||||
cerr << "Error: missing left argument." << endl;
|
||||
cerr << USAGE << endl;
|
||||
std::cerr << "Error: missing left argument." << std::endl;
|
||||
std::cerr << USAGE << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (Util::isInteger (leftArg) == false) {
|
||||
cerr << "Error: `" << leftArg << "' " ;
|
||||
cerr << "is not a variable id." << endl ;
|
||||
std::cerr << "Error: `" << leftArg << "' " ;
|
||||
std::cerr << "is not a variable id." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
VarId vid = Util::stringToUnsigned (leftArg);
|
||||
VarNode* observedVar = fg.getVarNode (vid);
|
||||
if (observedVar == false) {
|
||||
cerr << "Error: unknow variable with id " ;
|
||||
cerr << "`" << vid << "'." << endl;
|
||||
std::cerr << "Error: unknow variable with id " ;
|
||||
std::cerr << "`" << vid << "'." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (rightArg.empty()) {
|
||||
cerr << "Error: missing right argument." << endl;
|
||||
cerr << USAGE << endl;
|
||||
std::cerr << "Error: missing right argument." << std::endl;
|
||||
std::cerr << USAGE << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (Util::isInteger (rightArg) == false) {
|
||||
cerr << "Error: `" << rightArg << "' " ;
|
||||
cerr << "is not a state index." << endl ;
|
||||
std::cerr << "Error: `" << rightArg << "' " ;
|
||||
std::cerr << "is not a state index." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
unsigned stateIdx = Util::stringToUnsigned (rightArg);
|
||||
if (observedVar->isValidState (stateIdx) == false) {
|
||||
cerr << "Error: `" << stateIdx << "' " ;
|
||||
cerr << "is not a valid state index for variable with id " ;
|
||||
cerr << "`" << vid << "'." << endl;
|
||||
std::cerr << "Error: `" << stateIdx << "' " ;
|
||||
std::cerr << "is not a valid state index for variable with id " ;
|
||||
std::cerr << "`" << vid << "'." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
observedVar->setEvidence (stateIdx);
|
||||
@ -192,7 +191,7 @@ runSolver (const FactorGraph& fg, const VarIds& queryIds)
|
||||
}
|
||||
if (Globals::verbosity > 0) {
|
||||
solver->printSolverFlags();
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (queryIds.empty()) {
|
||||
solver->printAllPosterioris();
|
||||
|
@ -20,19 +20,17 @@
|
||||
#include "BayesBall.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef std::pair<ParfactorList*, ObservedFormulas*> LiftedNetwork;
|
||||
|
||||
Parfactor* readParfactor (YAP_Term);
|
||||
|
||||
void readLiftedEvidence (YAP_Term, ObservedFormulas&);
|
||||
|
||||
vector<unsigned> readUnsignedList (YAP_Term list);
|
||||
std::vector<unsigned> readUnsignedList (YAP_Term list);
|
||||
|
||||
Params readParameters (YAP_Term);
|
||||
|
||||
YAP_Term fillAnswersPrologList (vector<Params>& results);
|
||||
YAP_Term fillAnswersPrologList (std::vector<Params>& results);
|
||||
|
||||
|
||||
|
||||
@ -52,7 +50,7 @@ createLiftedNetwork (void)
|
||||
Util::printHeader ("INITIAL PARFACTORS");
|
||||
for (size_t i = 0; i < parfactors.size(); i++) {
|
||||
parfactors[i]->print();
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,7 +76,7 @@ createLiftedNetwork (void)
|
||||
int
|
||||
createGroundNetwork (void)
|
||||
{
|
||||
string factorsType ((char*) YAP_AtomName (YAP_AtomOfTerm (YAP_ARG1)));
|
||||
std::string factorsType ((char*) YAP_AtomName (YAP_AtomOfTerm (YAP_ARG1)));
|
||||
FactorGraph* fg = new FactorGraph();
|
||||
if (factorsType == "bayes") {
|
||||
fg->setFactorsAsBayesian();
|
||||
@ -121,9 +119,9 @@ createGroundNetwork (void)
|
||||
fg->print();
|
||||
}
|
||||
if (Globals::verbosity > 0) {
|
||||
cout << "factor graph contains " ;
|
||||
cout << fg->nrVarNodes() << " variables and " ;
|
||||
cout << fg->nrFacNodes() << " factors " << endl;
|
||||
std::cout << "factor graph contains " ;
|
||||
std::cout << fg->nrVarNodes() << " variables and " ;
|
||||
std::cout << fg->nrFacNodes() << " factors " << std::endl;
|
||||
}
|
||||
YAP_Int p = (YAP_Int) (fg);
|
||||
return YAP_Unify (YAP_MkIntTerm (p), YAP_ARG4);
|
||||
@ -147,30 +145,31 @@ runLiftedSolver (void)
|
||||
|
||||
if (Globals::verbosity > 0) {
|
||||
solver->printSolverFlags();
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
YAP_Term taskList = YAP_ARG2;
|
||||
vector<Params> results;
|
||||
std::vector<Params> results;
|
||||
while (taskList != YAP_TermNil()) {
|
||||
Grounds queryVars;
|
||||
YAP_Term jointList = YAP_HeadOfTerm (taskList);
|
||||
while (jointList != YAP_TermNil()) {
|
||||
YAP_Term ground = YAP_HeadOfTerm (jointList);
|
||||
if (YAP_IsAtomTerm (ground)) {
|
||||
string name ((char*) YAP_AtomName (YAP_AtomOfTerm (ground)));
|
||||
std::string name ((char*) YAP_AtomName (YAP_AtomOfTerm (ground)));
|
||||
queryVars.push_back (Ground (LiftedUtils::getSymbol (name)));
|
||||
} else {
|
||||
assert (YAP_IsApplTerm (ground));
|
||||
YAP_Functor yapFunctor = YAP_FunctorOfTerm (ground);
|
||||
string name ((char*) (YAP_AtomName (YAP_NameOfFunctor (yapFunctor))));
|
||||
std::string name ((char*) (YAP_AtomName (
|
||||
YAP_NameOfFunctor (yapFunctor))));
|
||||
unsigned arity = (unsigned) YAP_ArityOfFunctor (yapFunctor);
|
||||
Symbol functor = LiftedUtils::getSymbol (name);
|
||||
Symbols args;
|
||||
for (unsigned i = 1; i <= arity; i++) {
|
||||
YAP_Term ti = YAP_ArgOfTerm (i, ground);
|
||||
assert (YAP_IsAtomTerm (ti));
|
||||
string arg ((char *) YAP_AtomName (YAP_AtomOfTerm (ti)));
|
||||
std::string arg ((char *) YAP_AtomName (YAP_AtomOfTerm (ti)));
|
||||
args.push_back (LiftedUtils::getSymbol (arg));
|
||||
}
|
||||
queryVars.push_back (Ground (functor, args));
|
||||
@ -193,7 +192,7 @@ runGroundSolver (void)
|
||||
{
|
||||
FactorGraph* fg = (FactorGraph*) YAP_IntOfTerm (YAP_ARG1);
|
||||
|
||||
vector<VarIds> tasks;
|
||||
std::vector<VarIds> tasks;
|
||||
YAP_Term taskList = YAP_ARG2;
|
||||
while (taskList != YAP_TermNil()) {
|
||||
tasks.push_back (readUnsignedList (YAP_HeadOfTerm (taskList)));
|
||||
@ -220,10 +219,10 @@ runGroundSolver (void)
|
||||
|
||||
if (Globals::verbosity > 0) {
|
||||
solver->printSolverFlags();
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
vector<Params> results;
|
||||
std::vector<Params> results;
|
||||
results.reserve (tasks.size());
|
||||
for (size_t i = 0; i < tasks.size(); i++) {
|
||||
results.push_back (solver->solveQuery (tasks[i]));
|
||||
@ -246,7 +245,7 @@ setParfactorsParams (void)
|
||||
ParfactorList* pfList = network->first;
|
||||
YAP_Term distIdsList = YAP_ARG2;
|
||||
YAP_Term paramsList = YAP_ARG3;
|
||||
unordered_map<unsigned, Params> paramsMap;
|
||||
std::unordered_map<unsigned, Params> paramsMap;
|
||||
while (distIdsList != YAP_TermNil()) {
|
||||
unsigned distId = (unsigned) YAP_IntOfTerm (
|
||||
YAP_HeadOfTerm (distIdsList));
|
||||
@ -272,7 +271,7 @@ setFactorsParams (void)
|
||||
FactorGraph* fg = (FactorGraph*) YAP_IntOfTerm (YAP_ARG1);
|
||||
YAP_Term distIdsList = YAP_ARG2;
|
||||
YAP_Term paramsList = YAP_ARG3;
|
||||
unordered_map<unsigned, Params> paramsMap;
|
||||
std::unordered_map<unsigned, Params> paramsMap;
|
||||
while (distIdsList != YAP_TermNil()) {
|
||||
unsigned distId = (unsigned) YAP_IntOfTerm (
|
||||
YAP_HeadOfTerm (distIdsList));
|
||||
@ -296,7 +295,7 @@ int
|
||||
setVarsInformation (void)
|
||||
{
|
||||
Var::clearVarsInfo();
|
||||
vector<string> labels;
|
||||
std::vector<std::string> labels;
|
||||
YAP_Term labelsL = YAP_ARG1;
|
||||
while (labelsL != YAP_TermNil()) {
|
||||
YAP_Atom atom = YAP_AtomOfTerm (YAP_HeadOfTerm (labelsL));
|
||||
@ -325,18 +324,18 @@ setVarsInformation (void)
|
||||
int
|
||||
setHorusFlag (void)
|
||||
{
|
||||
string option ((char*) YAP_AtomName (YAP_AtomOfTerm (YAP_ARG1)));
|
||||
string value;
|
||||
std::string option ((char*) YAP_AtomName (YAP_AtomOfTerm (YAP_ARG1)));
|
||||
std::string value;
|
||||
if (option == "verbosity") {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << (int) YAP_IntOfTerm (YAP_ARG2);
|
||||
ss >> value;
|
||||
} else if (option == "bp_accuracy") {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << (float) YAP_FloatOfTerm (YAP_ARG2);
|
||||
ss >> value;
|
||||
} else if (option == "bp_max_iter") {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << (int) YAP_IntOfTerm (YAP_ARG2);
|
||||
ss >> value;
|
||||
} else {
|
||||
@ -386,23 +385,23 @@ readParfactor (YAP_Term pfTerm)
|
||||
// read parametric random vars
|
||||
ProbFormulas formulas;
|
||||
unsigned count = 0;
|
||||
unordered_map<YAP_Term, LogVar> lvMap;
|
||||
std::unordered_map<YAP_Term, LogVar> lvMap;
|
||||
YAP_Term pvList = YAP_ArgOfTerm (2, pfTerm);
|
||||
while (pvList != YAP_TermNil()) {
|
||||
YAP_Term formulaTerm = YAP_HeadOfTerm (pvList);
|
||||
if (YAP_IsAtomTerm (formulaTerm)) {
|
||||
string name ((char*) YAP_AtomName (YAP_AtomOfTerm (formulaTerm)));
|
||||
std::string name ((char*) YAP_AtomName (YAP_AtomOfTerm (formulaTerm)));
|
||||
Symbol functor = LiftedUtils::getSymbol (name);
|
||||
formulas.push_back (ProbFormula (functor, ranges[count]));
|
||||
} else {
|
||||
LogVars logVars;
|
||||
YAP_Functor yapFunctor = YAP_FunctorOfTerm (formulaTerm);
|
||||
string name ((char*) YAP_AtomName (YAP_NameOfFunctor (yapFunctor)));
|
||||
std::string name ((char*) YAP_AtomName (YAP_NameOfFunctor (yapFunctor)));
|
||||
Symbol functor = LiftedUtils::getSymbol (name);
|
||||
unsigned arity = (unsigned) YAP_ArityOfFunctor (yapFunctor);
|
||||
for (unsigned i = 1; i <= arity; i++) {
|
||||
YAP_Term ti = YAP_ArgOfTerm (i, formulaTerm);
|
||||
unordered_map<YAP_Term, LogVar>::iterator it = lvMap.find (ti);
|
||||
std::unordered_map<YAP_Term, LogVar>::iterator it = lvMap.find (ti);
|
||||
if (it != lvMap.end()) {
|
||||
logVars.push_back (it->second);
|
||||
} else {
|
||||
@ -434,10 +433,11 @@ readParfactor (YAP_Term pfTerm)
|
||||
for (unsigned i = 1; i <= arity; i++) {
|
||||
YAP_Term ti = YAP_ArgOfTerm (i, term);
|
||||
if (YAP_IsAtomTerm (ti) == false) {
|
||||
cerr << "Error: the constraint contains free variables." << endl;
|
||||
std::cerr << "Error: the constraint contains free variables." ;
|
||||
std::cerr << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
string name ((char*) YAP_AtomName (YAP_AtomOfTerm (ti)));
|
||||
std::string name ((char*) YAP_AtomName (YAP_AtomOfTerm (ti)));
|
||||
tuple[i - 1] = LiftedUtils::getSymbol (name);
|
||||
}
|
||||
tuples.push_back (tuple);
|
||||
@ -460,18 +460,19 @@ readLiftedEvidence (
|
||||
Symbol functor;
|
||||
Symbols args;
|
||||
if (YAP_IsAtomTerm (ground)) {
|
||||
string name ((char*) YAP_AtomName (YAP_AtomOfTerm (ground)));
|
||||
std::string name ((char*) YAP_AtomName (YAP_AtomOfTerm (ground)));
|
||||
functor = LiftedUtils::getSymbol (name);
|
||||
} else {
|
||||
assert (YAP_IsApplTerm (ground));
|
||||
YAP_Functor yapFunctor = YAP_FunctorOfTerm (ground);
|
||||
string name ((char*) (YAP_AtomName (YAP_NameOfFunctor (yapFunctor))));
|
||||
std::string name ((char*) (YAP_AtomName (
|
||||
YAP_NameOfFunctor (yapFunctor))));
|
||||
functor = LiftedUtils::getSymbol (name);
|
||||
unsigned arity = (unsigned) YAP_ArityOfFunctor (yapFunctor);
|
||||
for (unsigned i = 1; i <= arity; i++) {
|
||||
YAP_Term ti = YAP_ArgOfTerm (i, ground);
|
||||
assert (YAP_IsAtomTerm (ti));
|
||||
string arg ((char *) YAP_AtomName (YAP_AtomOfTerm (ti)));
|
||||
std::string arg ((char *) YAP_AtomName (YAP_AtomOfTerm (ti)));
|
||||
args.push_back (LiftedUtils::getSymbol (arg));
|
||||
}
|
||||
}
|
||||
@ -494,10 +495,10 @@ readLiftedEvidence (
|
||||
|
||||
|
||||
|
||||
vector<unsigned>
|
||||
std::vector<unsigned>
|
||||
readUnsignedList (YAP_Term list)
|
||||
{
|
||||
vector<unsigned> vec;
|
||||
std::vector<unsigned> vec;
|
||||
while (list != YAP_TermNil()) {
|
||||
vec.push_back ((unsigned) YAP_IntOfTerm (YAP_HeadOfTerm (list)));
|
||||
list = YAP_TailOfTerm (list);
|
||||
@ -525,7 +526,7 @@ readParameters (YAP_Term paramL)
|
||||
|
||||
|
||||
YAP_Term
|
||||
fillAnswersPrologList (vector<Params>& results)
|
||||
fillAnswersPrologList (std::vector<Params>& results)
|
||||
{
|
||||
YAP_Term list = YAP_TermNil();
|
||||
for (size_t i = results.size(); i-- > 0; ) {
|
||||
|
@ -38,11 +38,11 @@ class Indexer
|
||||
private:
|
||||
void calculateOffsets (void);
|
||||
|
||||
size_t index_;
|
||||
Ranges indices_;
|
||||
const Ranges& ranges_;
|
||||
size_t size_;
|
||||
vector<size_t> offsets_;
|
||||
size_t index_;
|
||||
Ranges indices_;
|
||||
const Ranges& ranges_;
|
||||
size_t size_;
|
||||
std::vector<size_t> offsets_;
|
||||
|
||||
friend std::ostream& operator<< (std::ostream&, const Indexer&);
|
||||
|
||||
@ -201,15 +201,15 @@ operator<< (std::ostream& os, const Indexer& indexer)
|
||||
class MapIndexer
|
||||
{
|
||||
public:
|
||||
MapIndexer (const Ranges& ranges, const vector<bool>& mask);
|
||||
MapIndexer (const Ranges& ranges, const std::vector<bool>& mask);
|
||||
|
||||
MapIndexer (const Ranges& ranges, size_t dim);
|
||||
|
||||
template <typename T>
|
||||
MapIndexer (
|
||||
const vector<T>& allArgs,
|
||||
const std::vector<T>& allArgs,
|
||||
const Ranges& allRanges,
|
||||
const vector<T>& wantedArgs,
|
||||
const std::vector<T>& wantedArgs,
|
||||
const Ranges& wantedRanges);
|
||||
|
||||
MapIndexer& operator++ (void);
|
||||
@ -223,11 +223,11 @@ class MapIndexer
|
||||
void reset (void);
|
||||
|
||||
private:
|
||||
size_t index_;
|
||||
Ranges indices_;
|
||||
const Ranges& ranges_;
|
||||
bool valid_;
|
||||
vector<size_t> offsets_;
|
||||
size_t index_;
|
||||
Ranges indices_;
|
||||
const Ranges& ranges_;
|
||||
bool valid_;
|
||||
std::vector<size_t> offsets_;
|
||||
|
||||
friend std::ostream& operator<< (std::ostream&, const MapIndexer&);
|
||||
|
||||
@ -237,7 +237,7 @@ class MapIndexer
|
||||
|
||||
|
||||
inline
|
||||
MapIndexer::MapIndexer (const Ranges& ranges, const vector<bool>& mask)
|
||||
MapIndexer::MapIndexer (const Ranges& ranges, const std::vector<bool>& mask)
|
||||
: index_(0), indices_(ranges.size(), 0), ranges_(ranges),
|
||||
valid_(true)
|
||||
{
|
||||
@ -273,15 +273,15 @@ MapIndexer::MapIndexer (const Ranges& ranges, size_t dim)
|
||||
|
||||
template <typename T> inline
|
||||
MapIndexer::MapIndexer (
|
||||
const vector<T>& allArgs,
|
||||
const std::vector<T>& allArgs,
|
||||
const Ranges& allRanges,
|
||||
const vector<T>& wantedArgs,
|
||||
const std::vector<T>& wantedArgs,
|
||||
const Ranges& wantedRanges)
|
||||
: index_(0), indices_(allArgs.size(), 0), ranges_(allRanges),
|
||||
valid_(true)
|
||||
{
|
||||
size_t prod = 1;
|
||||
vector<size_t> offsets (wantedRanges.size());
|
||||
std::vector<size_t> offsets (wantedRanges.size());
|
||||
for (size_t i = wantedRanges.size(); i-- > 0; ) {
|
||||
offsets[i] = prod;
|
||||
prod *= wantedRanges[i];
|
||||
|
@ -27,7 +27,7 @@ LiftedBp::solveQuery (const Grounds& query)
|
||||
{
|
||||
assert (query.empty() == false);
|
||||
Params res;
|
||||
vector<PrvGroup> groups = getQueryGroups (query);
|
||||
std::vector<PrvGroup> groups = getQueryGroups (query);
|
||||
if (query.size() == 1) {
|
||||
res = solver_->getPosterioriOf (groups[0]);
|
||||
} else {
|
||||
@ -60,7 +60,7 @@ LiftedBp::solveQuery (const Grounds& query)
|
||||
void
|
||||
LiftedBp::printSolverFlags (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "lifted bp [" ;
|
||||
ss << "bp_msg_schedule=" ;
|
||||
switch (WeightedBp::msgSchedule()) {
|
||||
@ -73,7 +73,7 @@ LiftedBp::printSolverFlags (void) const
|
||||
ss << ",bp_accuracy=" << WeightedBp::accuracy();
|
||||
ss << ",log_domain=" << Util::toString (Globals::logDomain);
|
||||
ss << "]" ;
|
||||
cout << ss.str() << endl;
|
||||
std::cout << ss.str() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -114,10 +114,10 @@ LiftedBp::iterate (void)
|
||||
|
||||
|
||||
|
||||
vector<PrvGroup>
|
||||
std::vector<PrvGroup>
|
||||
LiftedBp::getQueryGroups (const Grounds& query)
|
||||
{
|
||||
vector<PrvGroup> queryGroups;
|
||||
std::vector<PrvGroup> queryGroups;
|
||||
for (unsigned i = 0; i < query.size(); i++) {
|
||||
ParfactorList::const_iterator it = pfList_.begin();
|
||||
for (; it != pfList_.end(); ++it) {
|
||||
@ -139,7 +139,7 @@ LiftedBp::createFactorGraph (void)
|
||||
fg_ = new FactorGraph();
|
||||
ParfactorList::const_iterator it = pfList_.begin();
|
||||
for (; it != pfList_.end(); ++it) {
|
||||
vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||
std::vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||
VarIds varIds;
|
||||
for (size_t i = 0; i < groups.size(); i++) {
|
||||
varIds.push_back (groups[i]);
|
||||
@ -150,10 +150,10 @@ LiftedBp::createFactorGraph (void)
|
||||
|
||||
|
||||
|
||||
vector<vector<unsigned>>
|
||||
std::vector<std::vector<unsigned>>
|
||||
LiftedBp::getWeights (void) const
|
||||
{
|
||||
vector<vector<unsigned>> weights;
|
||||
std::vector<std::vector<unsigned>> weights;
|
||||
weights.reserve (pfList_.size());
|
||||
ParfactorList::const_iterator it = pfList_.begin();
|
||||
for (; it != pfList_.end(); ++it) {
|
||||
@ -196,7 +196,7 @@ LiftedBp::getJointByConditioning (
|
||||
Grounds obsGrounds = {query[0]};
|
||||
for (size_t i = 1; i < query.size(); i++) {
|
||||
Params newBeliefs;
|
||||
vector<ObservedFormula> obsFs;
|
||||
std::vector<ObservedFormula> obsFs;
|
||||
Ranges obsRanges;
|
||||
for (size_t j = 0; j < obsGrounds.size(); j++) {
|
||||
obsFs.push_back (ObservedFormula (
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "LiftedSolver.h"
|
||||
#include "ParfactorList.h"
|
||||
|
||||
|
||||
class FactorGraph;
|
||||
class WeightedBp;
|
||||
|
||||
@ -23,11 +24,11 @@ class LiftedBp : public LiftedSolver
|
||||
|
||||
bool iterate (void);
|
||||
|
||||
vector<PrvGroup> getQueryGroups (const Grounds&);
|
||||
std::vector<PrvGroup> getQueryGroups (const Grounds&);
|
||||
|
||||
void createFactorGraph (void);
|
||||
|
||||
vector<vector<unsigned>> getWeights (void) const;
|
||||
std::vector<std::vector<unsigned>> getWeights (void) const;
|
||||
|
||||
unsigned rangeOfGround (const Ground&);
|
||||
|
||||
@ -38,7 +39,6 @@ class LiftedBp : public LiftedSolver
|
||||
FactorGraph* fg_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN (LiftedBp);
|
||||
|
||||
};
|
||||
|
||||
#endif // PACKAGES_CLPBN_HORUS_LIFTEDBP_H
|
||||
|
@ -234,10 +234,11 @@ LiftedCircuit::LiftedCircuit (const LiftedWCNF* lwcnf)
|
||||
if (Globals::verbosity > 1) {
|
||||
if (compilationSucceeded_) {
|
||||
double wmc = LogAware::exp (getWeightedModelCount());
|
||||
cout << "Weighted model count = " << wmc << endl << endl;
|
||||
std::cout << "Weighted model count = " << wmc;
|
||||
std::cout << std::endl << std::endl;
|
||||
}
|
||||
cout << "Exporting circuit to graphviz (circuit.dot)..." ;
|
||||
cout << endl << endl;
|
||||
std::cout << "Exporting circuit to graphviz (circuit.dot)..." ;
|
||||
std::cout << std::endl << std::endl;
|
||||
exportToGraphViz ("circuit.dot");
|
||||
}
|
||||
}
|
||||
@ -247,8 +248,8 @@ LiftedCircuit::LiftedCircuit (const LiftedWCNF* lwcnf)
|
||||
LiftedCircuit::~LiftedCircuit (void)
|
||||
{
|
||||
delete root_;
|
||||
unordered_map<CircuitNode*, Clauses>::iterator it;
|
||||
it = originClausesMap_.begin();
|
||||
std::unordered_map<CircuitNode*, Clauses>::iterator it
|
||||
= originClausesMap_.begin();
|
||||
while (it != originClausesMap_.end()) {
|
||||
Clause::deleteClauses (it->second);
|
||||
++ it;
|
||||
@ -277,15 +278,15 @@ LiftedCircuit::getWeightedModelCount (void) const
|
||||
void
|
||||
LiftedCircuit::exportToGraphViz (const char* fileName)
|
||||
{
|
||||
ofstream out (fileName);
|
||||
std::ofstream out (fileName);
|
||||
if (!out.is_open()) {
|
||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||
return;
|
||||
}
|
||||
out << "digraph {" << endl;
|
||||
out << "ranksep=1" << endl;
|
||||
out << "digraph {" << std::endl;
|
||||
out << "ranksep=1" << std::endl;
|
||||
exportToGraphViz (root_, out);
|
||||
out << "}" << endl;
|
||||
out << "}" << std::endl;
|
||||
out.close();
|
||||
}
|
||||
|
||||
@ -389,7 +390,7 @@ LiftedCircuit::tryUnitPropagation (
|
||||
AndNode* andNode = new AndNode();
|
||||
if (Globals::verbosity > 1) {
|
||||
originClausesMap_[andNode] = backupClauses_;
|
||||
stringstream explanation;
|
||||
std::stringstream explanation;
|
||||
explanation << " UP on " << clauses[i]->literals()[0];
|
||||
explanationMap_[andNode] = explanation.str();
|
||||
}
|
||||
@ -478,7 +479,7 @@ LiftedCircuit::tryShannonDecomp (
|
||||
OrNode* orNode = new OrNode();
|
||||
if (Globals::verbosity > 1) {
|
||||
originClausesMap_[orNode] = backupClauses_;
|
||||
stringstream explanation;
|
||||
std::stringstream explanation;
|
||||
explanation << " SD on " << literals[j];
|
||||
explanationMap_[orNode] = explanation.str();
|
||||
}
|
||||
@ -558,7 +559,7 @@ LiftedCircuit::tryInclusionExclusion (
|
||||
IncExcNode* ieNode = new IncExcNode();
|
||||
if (Globals::verbosity > 1) {
|
||||
originClausesMap_[ieNode] = backupClauses_;
|
||||
stringstream explanation;
|
||||
std::stringstream explanation;
|
||||
explanation << " IncExc on clause nº " << i + 1;
|
||||
explanationMap_[ieNode] = explanation.str();
|
||||
}
|
||||
@ -635,13 +636,13 @@ LiftedCircuit::tryIndepPartialGroundingAux (
|
||||
}
|
||||
}
|
||||
// verifies if the IPG logical vars appear in the same positions
|
||||
unordered_map<LiteralId, size_t> positions;
|
||||
std::unordered_map<LiteralId, size_t> positions;
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
const Literals& literals = clauses[i]->literals();
|
||||
for (size_t j = 0; j < literals.size(); j++) {
|
||||
size_t idx = literals[j].indexOfLogVar (rootLogVars[i]);
|
||||
assert (idx != literals[j].nrLogVars());
|
||||
unordered_map<LiteralId, size_t>::iterator it;
|
||||
std::unordered_map<LiteralId, size_t>::iterator it;
|
||||
it = positions.find (literals[j].lid());
|
||||
if (it != positions.end()) {
|
||||
if (it->second != idx) {
|
||||
@ -835,14 +836,15 @@ LiftedCircuit::smoothCircuit (CircuitNode* node)
|
||||
case CircuitNodeType::SET_OR_NODE: {
|
||||
SetOrNode* casted = dynamic_cast<SetOrNode*>(node);
|
||||
propagLits = smoothCircuit (*casted->follow());
|
||||
TinySet<pair<LiteralId,unsigned>> litSet;
|
||||
TinySet<std::pair<LiteralId,unsigned>> litSet;
|
||||
for (size_t i = 0; i < propagLits.size(); i++) {
|
||||
litSet.insert (make_pair (propagLits[i].lid(),
|
||||
litSet.insert (std::make_pair (propagLits[i].lid(),
|
||||
propagLits[i].logVarTypes().size()));
|
||||
}
|
||||
LitLvTypesSet missingLids;
|
||||
for (size_t i = 0; i < litSet.size(); i++) {
|
||||
vector<LogVarTypes> allTypes = getAllPossibleTypes (litSet[i].second);
|
||||
std::vector<LogVarTypes> allTypes
|
||||
= getAllPossibleTypes (litSet[i].second);
|
||||
for (size_t j = 0; j < allTypes.size(); j++) {
|
||||
bool typeFound = false;
|
||||
for (size_t k = 0; k < propagLits.size(); k++) {
|
||||
@ -911,8 +913,8 @@ LiftedCircuit::createSmoothNode (
|
||||
{
|
||||
if (missingLits.empty() == false) {
|
||||
if (Globals::verbosity > 1) {
|
||||
unordered_map<CircuitNode*, Clauses>::iterator it;
|
||||
it = originClausesMap_.find (*prev);
|
||||
std::unordered_map<CircuitNode*, Clauses>::iterator it
|
||||
= originClausesMap_.find (*prev);
|
||||
if (it != originClausesMap_.end()) {
|
||||
backupClauses_ = it->second;
|
||||
} else {
|
||||
@ -947,10 +949,10 @@ LiftedCircuit::createSmoothNode (
|
||||
|
||||
|
||||
|
||||
vector<LogVarTypes>
|
||||
std::vector<LogVarTypes>
|
||||
LiftedCircuit::getAllPossibleTypes (unsigned nrLogVars) const
|
||||
{
|
||||
vector<LogVarTypes> res;
|
||||
std::vector<LogVarTypes> res;
|
||||
if (nrLogVars == 0) {
|
||||
// do nothing
|
||||
} else if (nrLogVars == 1) {
|
||||
@ -1031,16 +1033,16 @@ LiftedCircuit::getCircuitNodeType (const CircuitNode* node) const
|
||||
|
||||
|
||||
void
|
||||
LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
LiftedCircuit::exportToGraphViz (CircuitNode* node, std::ofstream& os)
|
||||
{
|
||||
assert (node);
|
||||
|
||||
static unsigned nrAuxNodes = 0;
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "n" << nrAuxNodes;
|
||||
string auxNode = ss.str();
|
||||
std::string auxNode = ss.str();
|
||||
nrAuxNodes ++;
|
||||
string opStyle = "shape=circle,width=0.7,margin=\"0.0,0.0\"," ;
|
||||
std::string opStyle = "shape=circle,width=0.7,margin=\"0.0,0.0\"," ;
|
||||
|
||||
switch (getCircuitNodeType (node)) {
|
||||
|
||||
@ -1048,20 +1050,21 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
OrNode* casted = dynamic_cast<OrNode*>(node);
|
||||
printClauses (casted, os);
|
||||
|
||||
os << auxNode << " [" << opStyle << "label=\"∨\"]" << endl;
|
||||
os << auxNode << " [" << opStyle << "label=\"∨\"]" ;
|
||||
os << std::endl;
|
||||
os << escapeNode (node) << " -> " << auxNode;
|
||||
os << " [label=\"" << getExplanationString (node) << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->leftBranch());
|
||||
os << " [label=\" " << (*casted->leftBranch())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->rightBranch());
|
||||
os << " [label=\" " << (*casted->rightBranch())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
exportToGraphViz (*casted->leftBranch(), os);
|
||||
exportToGraphViz (*casted->rightBranch(), os);
|
||||
@ -1072,20 +1075,21 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
AndNode* casted = dynamic_cast<AndNode*>(node);
|
||||
printClauses (casted, os);
|
||||
|
||||
os << auxNode << " [" << opStyle << "label=\"∧\"]" << endl;
|
||||
os << auxNode << " [" << opStyle << "label=\"∧\"]" ;
|
||||
os << std::endl;
|
||||
os << escapeNode (node) << " -> " << auxNode;
|
||||
os << " [label=\"" << getExplanationString (node) << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->leftBranch());
|
||||
os << " [label=\" " << (*casted->leftBranch())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->rightBranch()) << endl;
|
||||
os << escapeNode (*casted->rightBranch());
|
||||
os << " [label=\" " << (*casted->rightBranch())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
exportToGraphViz (*casted->leftBranch(), os);
|
||||
exportToGraphViz (*casted->rightBranch(), os);
|
||||
@ -1096,15 +1100,16 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
SetOrNode* casted = dynamic_cast<SetOrNode*>(node);
|
||||
printClauses (casted, os);
|
||||
|
||||
os << auxNode << " [" << opStyle << "label=\"∨(X)\"]" << endl;
|
||||
os << auxNode << " [" << opStyle << "label=\"∨(X)\"]" ;
|
||||
os << std::endl;
|
||||
os << escapeNode (node) << " -> " << auxNode;
|
||||
os << " [label=\"" << getExplanationString (node) << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->follow());
|
||||
os << " [label=\" " << (*casted->follow())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
exportToGraphViz (*casted->follow(), os);
|
||||
break;
|
||||
@ -1114,15 +1119,16 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
SetAndNode* casted = dynamic_cast<SetAndNode*>(node);
|
||||
printClauses (casted, os);
|
||||
|
||||
os << auxNode << " [" << opStyle << "label=\"∧(X)\"]" << endl;
|
||||
os << auxNode << " [" << opStyle << "label=\"∧(X)\"]" ;
|
||||
os << std::endl;
|
||||
os << escapeNode (node) << " -> " << auxNode;
|
||||
os << " [label=\"" << getExplanationString (node) << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->follow());
|
||||
os << " [label=\" " << (*casted->follow())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
exportToGraphViz (*casted->follow(), os);
|
||||
break;
|
||||
@ -1133,25 +1139,25 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
printClauses (casted, os);
|
||||
|
||||
os << auxNode << " [" << opStyle << "label=\"+ - +\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
os << escapeNode (node) << " -> " << auxNode;
|
||||
os << " [label=\"" << getExplanationString (node) << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->plus1Branch());
|
||||
os << " [label=\" " << (*casted->plus1Branch())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->minusBranch()) << endl;
|
||||
os << escapeNode (*casted->minusBranch()) << std::endl;
|
||||
os << " [label=\" " << (*casted->minusBranch())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
os << auxNode << " -> " ;
|
||||
os << escapeNode (*casted->plus2Branch());
|
||||
os << " [label=\" " << (*casted->plus2Branch())->weight() << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
|
||||
exportToGraphViz (*casted->plus1Branch(), os);
|
||||
exportToGraphViz (*casted->plus2Branch(), os);
|
||||
@ -1172,7 +1178,7 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
case TRUE_NODE: {
|
||||
os << escapeNode (node);
|
||||
os << " [shape=box,label=\"⊤\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -1188,17 +1194,17 @@ LiftedCircuit::exportToGraphViz (CircuitNode* node, ofstream& os)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
LiftedCircuit::escapeNode (const CircuitNode* node) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "\"" << node << "\"" ;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
LiftedCircuit::getExplanationString (CircuitNode* node)
|
||||
{
|
||||
return Util::contains (explanationMap_, node)
|
||||
@ -1211,8 +1217,8 @@ LiftedCircuit::getExplanationString (CircuitNode* node)
|
||||
void
|
||||
LiftedCircuit::printClauses (
|
||||
CircuitNode* node,
|
||||
ofstream& os,
|
||||
string extraOptions)
|
||||
std::ofstream& os,
|
||||
std::string extraOptions)
|
||||
{
|
||||
Clauses clauses;
|
||||
if (Util::contains (originClausesMap_, node)) {
|
||||
@ -1230,7 +1236,7 @@ LiftedCircuit::printClauses (
|
||||
os << *clauses[i];
|
||||
}
|
||||
os << "\"]" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -1252,10 +1258,11 @@ LiftedKc::solveQuery (const Grounds& query)
|
||||
lwcnf_ = new LiftedWCNF (pfList_);
|
||||
circuit_ = new LiftedCircuit (lwcnf_);
|
||||
if (circuit_->isCompilationSucceeded() == false) {
|
||||
cerr << "Error: the circuit compilation has failed." << endl;
|
||||
std::cerr << "Error: the circuit compilation has failed." ;
|
||||
std::cerr << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
vector<PrvGroup> groups;
|
||||
std::vector<PrvGroup> groups;
|
||||
Ranges ranges;
|
||||
for (size_t i = 0; i < query.size(); i++) {
|
||||
ParfactorList::const_iterator it = pfList_.begin();
|
||||
@ -1274,7 +1281,7 @@ LiftedKc::solveQuery (const Grounds& query)
|
||||
Indexer indexer (ranges);
|
||||
while (indexer.valid()) {
|
||||
for (size_t i = 0; i < groups.size(); i++) {
|
||||
vector<LiteralId> litIds = lwcnf_->prvGroupLiterals (groups[i]);
|
||||
std::vector<LiteralId> litIds = lwcnf_->prvGroupLiterals (groups[i]);
|
||||
for (size_t j = 0; j < litIds.size(); j++) {
|
||||
if (indexer[i] == j) {
|
||||
lwcnf_->addWeight (litIds[j], LogAware::one(),
|
||||
@ -1300,10 +1307,10 @@ LiftedKc::solveQuery (const Grounds& query)
|
||||
void
|
||||
LiftedKc::printSolverFlags (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "lifted kc [" ;
|
||||
ss << "log_domain=" << Util::toString (Globals::logDomain);
|
||||
ss << "]" ;
|
||||
cout << ss.str() << endl;
|
||||
std::cout << ss.str() << std::endl;
|
||||
}
|
||||
|
||||
|
@ -250,28 +250,28 @@ class LiftedCircuit
|
||||
void createSmoothNode (const LitLvTypesSet& lids,
|
||||
CircuitNode** prev);
|
||||
|
||||
vector<LogVarTypes> getAllPossibleTypes (unsigned nrLogVars) const;
|
||||
std::vector<LogVarTypes> getAllPossibleTypes (unsigned nrLogVars) const;
|
||||
|
||||
bool containsTypes (const LogVarTypes& typesA,
|
||||
const LogVarTypes& typesB) const;
|
||||
|
||||
CircuitNodeType getCircuitNodeType (const CircuitNode* node) const;
|
||||
|
||||
void exportToGraphViz (CircuitNode* node, ofstream&);
|
||||
void exportToGraphViz (CircuitNode* node, std::ofstream&);
|
||||
|
||||
void printClauses (CircuitNode* node, ofstream&,
|
||||
string extraOptions = "");
|
||||
void printClauses (CircuitNode* node, std::ofstream&,
|
||||
std::string extraOptions = "");
|
||||
|
||||
string escapeNode (const CircuitNode* node) const;
|
||||
std::string escapeNode (const CircuitNode* node) const;
|
||||
|
||||
string getExplanationString (CircuitNode* node);
|
||||
std::string getExplanationString (CircuitNode* node);
|
||||
|
||||
CircuitNode* root_;
|
||||
const LiftedWCNF* lwcnf_;
|
||||
bool compilationSucceeded_;
|
||||
Clauses backupClauses_;
|
||||
unordered_map<CircuitNode*, Clauses> originClausesMap_;
|
||||
unordered_map<CircuitNode*, string> explanationMap_;
|
||||
std::unordered_map<CircuitNode*, Clauses> originClausesMap_;
|
||||
std::unordered_map<CircuitNode*, std::string> explanationMap_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN (LiftedCircuit);
|
||||
};
|
||||
|
@ -35,17 +35,17 @@ LiftedOperations::shatterAgainstQuery (
|
||||
}
|
||||
}
|
||||
if (found == false) {
|
||||
cerr << "Error: could not find a parfactor with ground " ;
|
||||
cerr << "`" << query[i] << "'." << endl;
|
||||
std::cerr << "Error: could not find a parfactor with ground " ;
|
||||
std::cerr << "`" << query[i] << "'." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
pfList.add (newPfs);
|
||||
}
|
||||
if (Globals::verbosity > 2) {
|
||||
Util::printAsteriskLine();
|
||||
cout << "SHATTERED AGAINST THE QUERY" << endl;
|
||||
std::cout << "SHATTERED AGAINST THE QUERY" << std::endl;
|
||||
for (size_t i = 0; i < query.size(); i++) {
|
||||
cout << " -> " << query[i] << endl;
|
||||
std::cout << " -> " << query[i] << std::endl;
|
||||
}
|
||||
Util::printAsteriskLine();
|
||||
pfList.print();
|
||||
@ -59,8 +59,8 @@ LiftedOperations::runWeakBayesBall (
|
||||
ParfactorList& pfList,
|
||||
const Grounds& query)
|
||||
{
|
||||
queue<PrvGroup> todo; // groups to process
|
||||
set<PrvGroup> done; // processed or in queue
|
||||
std::queue<PrvGroup> todo; // groups to process
|
||||
std::set<PrvGroup> done; // processed or in queue
|
||||
for (size_t i = 0; i < query.size(); i++) {
|
||||
ParfactorList::iterator it = pfList.begin();
|
||||
while (it != pfList.end()) {
|
||||
@ -74,14 +74,14 @@ LiftedOperations::runWeakBayesBall (
|
||||
}
|
||||
}
|
||||
|
||||
set<Parfactor*> requiredPfs;
|
||||
std::set<Parfactor*> requiredPfs;
|
||||
while (todo.empty() == false) {
|
||||
PrvGroup group = todo.front();
|
||||
ParfactorList::iterator it = pfList.begin();
|
||||
while (it != pfList.end()) {
|
||||
if (Util::contains (requiredPfs, *it) == false &&
|
||||
(*it)->containsGroup (group)) {
|
||||
vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||
std::vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||
for (size_t i = 0; i < groups.size(); i++) {
|
||||
if (Util::contains (done, groups[i]) == false) {
|
||||
todo.push (groups[i]);
|
||||
@ -143,9 +143,9 @@ LiftedOperations::absorveEvidence (
|
||||
}
|
||||
if (Globals::verbosity > 2 && obsFormulas.empty() == false) {
|
||||
Util::printAsteriskLine();
|
||||
cout << "AFTER EVIDENCE ABSORVED" << endl;
|
||||
std::cout << "AFTER EVIDENCE ABSORVED" << std::endl;
|
||||
for (size_t i = 0; i < obsFormulas.size(); i++) {
|
||||
cout << " -> " << obsFormulas[i] << endl;
|
||||
std::cout << " -> " << obsFormulas[i] << std::endl;
|
||||
}
|
||||
Util::printAsteriskLine();
|
||||
pfList.print();
|
||||
|
@ -4,9 +4,6 @@
|
||||
#include "ParfactorList.h"
|
||||
#include "Horus.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
class LiftedSolver
|
||||
{
|
||||
public:
|
||||
|
@ -9,14 +9,13 @@
|
||||
|
||||
namespace LiftedUtils {
|
||||
|
||||
|
||||
unordered_map<string, unsigned> symbolDict;
|
||||
std::unordered_map<std::string, unsigned> symbolDict;
|
||||
|
||||
|
||||
Symbol
|
||||
getSymbol (const string& symbolName)
|
||||
getSymbol (const std::string& symbolName)
|
||||
{
|
||||
unordered_map<string, unsigned>::iterator it
|
||||
std::unordered_map<std::string, unsigned>::iterator it
|
||||
= symbolDict.find (symbolName);
|
||||
if (it != symbolDict.end()) {
|
||||
return it->second;
|
||||
@ -31,10 +30,10 @@ getSymbol (const string& symbolName)
|
||||
void
|
||||
printSymbolDictionary (void)
|
||||
{
|
||||
unordered_map<string, unsigned>::const_iterator it
|
||||
std::unordered_map<std::string, unsigned>::const_iterator it
|
||||
= symbolDict.begin();
|
||||
while (it != symbolDict.end()) {
|
||||
cout << it->first << " -> " << it->second << endl;
|
||||
std::cout << it->first << " -> " << it->second << std::endl;
|
||||
++ it;
|
||||
}
|
||||
}
|
||||
@ -43,9 +42,9 @@ printSymbolDictionary (void)
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const Symbol& s)
|
||||
std::ostream& operator<< (std::ostream &os, const Symbol& s)
|
||||
{
|
||||
unordered_map<string, unsigned>::const_iterator it
|
||||
std::unordered_map<std::string, unsigned>::const_iterator it
|
||||
= LiftedUtils::symbolDict.begin();
|
||||
while (it != LiftedUtils::symbolDict.end() && it->second != s) {
|
||||
++ it;
|
||||
@ -57,9 +56,9 @@ ostream& operator<< (ostream &os, const Symbol& s)
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const LogVar& X)
|
||||
std::ostream& operator<< (std::ostream &os, const LogVar& X)
|
||||
{
|
||||
const string labels[] = {
|
||||
const std::string labels[] = {
|
||||
"A", "B", "C", "D", "E", "F",
|
||||
"G", "H", "I", "J", "K", "M" };
|
||||
(X >= 12) ? os << "X_" << X.id_ : os << labels[X];
|
||||
@ -68,7 +67,7 @@ ostream& operator<< (ostream &os, const LogVar& X)
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const Tuple& t)
|
||||
std::ostream& operator<< (std::ostream &os, const Tuple& t)
|
||||
{
|
||||
os << "(" ;
|
||||
for (size_t i = 0; i < t.size(); i++) {
|
||||
@ -80,7 +79,7 @@ ostream& operator<< (ostream &os, const Tuple& t)
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const Ground& gr)
|
||||
std::ostream& operator<< (std::ostream &os, const Ground& gr)
|
||||
{
|
||||
os << gr.functor();
|
||||
os << "(" ;
|
||||
@ -98,9 +97,9 @@ LogVars
|
||||
Substitution::getDiscardedLogVars (void) const
|
||||
{
|
||||
LogVars discardedLvs;
|
||||
set<LogVar> doneLvs;
|
||||
unordered_map<LogVar, LogVar>::const_iterator it;
|
||||
it = subs_.begin();
|
||||
std::set<LogVar> doneLvs;
|
||||
std::unordered_map<LogVar, LogVar>::const_iterator it
|
||||
= subs_.begin();
|
||||
while (it != subs_.end()) {
|
||||
if (Util::contains (doneLvs, it->second)) {
|
||||
discardedLvs.push_back (it->first);
|
||||
@ -114,9 +113,9 @@ Substitution::getDiscardedLogVars (void) const
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const Substitution& theta)
|
||||
std::ostream& operator<< (std::ostream &os, const Substitution& theta)
|
||||
{
|
||||
unordered_map<LogVar, LogVar>::const_iterator it;
|
||||
std::unordered_map<LogVar, LogVar>::const_iterator it;
|
||||
os << "[" ;
|
||||
it = theta.subs_.begin();
|
||||
while (it != theta.subs_.end()) {
|
||||
|
@ -10,9 +10,6 @@
|
||||
#include "Util.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class Symbol
|
||||
{
|
||||
public:
|
||||
@ -29,7 +26,7 @@ class Symbol
|
||||
private:
|
||||
unsigned id_;
|
||||
|
||||
friend ostream& operator<< (ostream &os, const Symbol& s);
|
||||
friend std::ostream& operator<< (std::ostream &os, const Symbol& s);
|
||||
};
|
||||
|
||||
|
||||
@ -49,7 +46,7 @@ class LogVar
|
||||
private:
|
||||
unsigned id_;
|
||||
|
||||
friend ostream& operator<< (ostream &os, const LogVar& X);
|
||||
friend std::ostream& operator<< (std::ostream &os, const LogVar& X);
|
||||
};
|
||||
|
||||
|
||||
@ -89,21 +86,21 @@ template <> struct hash<LogVar> {
|
||||
};
|
||||
|
||||
|
||||
typedef vector<Symbol> Symbols;
|
||||
typedef vector<Symbol> Tuple;
|
||||
typedef vector<Tuple> Tuples;
|
||||
typedef vector<LogVar> LogVars;
|
||||
typedef TinySet<Symbol> SymbolSet;
|
||||
typedef TinySet<LogVar> LogVarSet;
|
||||
typedef TinySet<Tuple> TupleSet;
|
||||
typedef std::vector<Symbol> Symbols;
|
||||
typedef std::vector<Symbol> Tuple;
|
||||
typedef std::vector<Tuple> Tuples;
|
||||
typedef std::vector<LogVar> LogVars;
|
||||
typedef TinySet<Symbol> SymbolSet;
|
||||
typedef TinySet<LogVar> LogVarSet;
|
||||
typedef TinySet<Tuple> TupleSet;
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const Tuple& t);
|
||||
std::ostream& operator<< (std::ostream &os, const Tuple& t);
|
||||
|
||||
|
||||
namespace LiftedUtils {
|
||||
|
||||
Symbol getSymbol (const string&);
|
||||
Symbol getSymbol (const std::string&);
|
||||
|
||||
void printSymbolDictionary (void);
|
||||
|
||||
@ -130,10 +127,10 @@ class Ground
|
||||
Symbol functor_;
|
||||
Symbols args_;
|
||||
|
||||
friend ostream& operator<< (ostream &os, const Ground& gr);
|
||||
friend std::ostream& operator<< (std::ostream &os, const Ground& gr);
|
||||
};
|
||||
|
||||
typedef vector<Ground> Grounds;
|
||||
typedef std::vector<Ground> Grounds;
|
||||
|
||||
|
||||
|
||||
@ -153,9 +150,10 @@ class Substitution
|
||||
LogVars getDiscardedLogVars (void) const;
|
||||
|
||||
private:
|
||||
unordered_map<LogVar, LogVar> subs_;
|
||||
std::unordered_map<LogVar, LogVar> subs_;
|
||||
|
||||
friend ostream& operator<< (ostream &os, const Substitution& theta);
|
||||
friend std::ostream& operator<< (
|
||||
std::ostream &os, const Substitution& theta);
|
||||
|
||||
};
|
||||
|
||||
@ -166,7 +164,7 @@ inline void
|
||||
Substitution::add (LogVar X_old, LogVar X_new)
|
||||
{
|
||||
assert (Util::contains (subs_, X_old) == false);
|
||||
subs_.insert (make_pair (X_old, X_new));
|
||||
subs_.insert (std::make_pair (X_old, X_new));
|
||||
}
|
||||
|
||||
|
||||
@ -183,7 +181,7 @@ Substitution::rename (LogVar X_old, LogVar X_new)
|
||||
inline LogVar
|
||||
Substitution::newNameFor (LogVar X) const
|
||||
{
|
||||
unordered_map<LogVar, LogVar>::const_iterator it;
|
||||
std::unordered_map<LogVar, LogVar>::const_iterator it;
|
||||
it = subs_.find (X);
|
||||
if (it != subs_.end()) {
|
||||
return subs_.find (X)->second;
|
||||
|
@ -8,21 +8,21 @@
|
||||
#include "Util.h"
|
||||
|
||||
|
||||
vector<LiftedOperator*>
|
||||
std::vector<LiftedOperator*>
|
||||
LiftedOperator::getValidOps (
|
||||
ParfactorList& pfList,
|
||||
const Grounds& query)
|
||||
{
|
||||
vector<LiftedOperator*> validOps;
|
||||
vector<ProductOperator*> multOps;
|
||||
std::vector<LiftedOperator*> validOps;
|
||||
std::vector<ProductOperator*> multOps;
|
||||
|
||||
multOps = ProductOperator::getValidOps (pfList);
|
||||
validOps.insert (validOps.end(), multOps.begin(), multOps.end());
|
||||
|
||||
if (Globals::verbosity > 1 || multOps.empty()) {
|
||||
vector<SumOutOperator*> sumOutOps;
|
||||
vector<CountingOperator*> countOps;
|
||||
vector<GroundOperator*> groundOps;
|
||||
std::vector<SumOutOperator*> sumOutOps;
|
||||
std::vector<CountingOperator*> countOps;
|
||||
std::vector<GroundOperator*> groundOps;
|
||||
sumOutOps = SumOutOperator::getValidOps (pfList, query);
|
||||
countOps = CountingOperator::getValidOps (pfList);
|
||||
groundOps = GroundOperator::getValidOps (pfList);
|
||||
@ -41,21 +41,21 @@ LiftedOperator::printValidOps (
|
||||
ParfactorList& pfList,
|
||||
const Grounds& query)
|
||||
{
|
||||
vector<LiftedOperator*> validOps;
|
||||
std::vector<LiftedOperator*> validOps;
|
||||
validOps = LiftedOperator::getValidOps (pfList, query);
|
||||
for (size_t i = 0; i < validOps.size(); i++) {
|
||||
cout << "-> " << validOps[i]->toString();
|
||||
std::cout << "-> " << validOps[i]->toString();
|
||||
delete validOps[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector<ParfactorList::iterator>
|
||||
std::vector<ParfactorList::iterator>
|
||||
LiftedOperator::getParfactorsWithGroup (
|
||||
ParfactorList& pfList, PrvGroup group)
|
||||
{
|
||||
vector<ParfactorList::iterator> iters;
|
||||
std::vector<ParfactorList::iterator> iters;
|
||||
ParfactorList::iterator pflIt = pfList.begin();
|
||||
while (pflIt != pfList.end()) {
|
||||
if ((*pflIt)->containsGroup (group)) {
|
||||
@ -89,13 +89,13 @@ ProductOperator::apply (void)
|
||||
|
||||
|
||||
|
||||
vector<ProductOperator*>
|
||||
std::vector<ProductOperator*>
|
||||
ProductOperator::getValidOps (ParfactorList& pfList)
|
||||
{
|
||||
vector<ProductOperator*> validOps;
|
||||
std::vector<ProductOperator*> validOps;
|
||||
ParfactorList::iterator it1 = pfList.begin();
|
||||
ParfactorList::iterator penultimate = -- pfList.end();
|
||||
set<Parfactor*> pfs;
|
||||
std::set<Parfactor*> pfs;
|
||||
while (it1 != penultimate) {
|
||||
if (Util::contains (pfs, *it1)) {
|
||||
++ it1;
|
||||
@ -128,15 +128,15 @@ ProductOperator::getValidOps (ParfactorList& pfList)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
ProductOperator::toString (void)
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "just multiplicate " ;
|
||||
ss << (*g1_)->getAllGroups();
|
||||
ss << " x " ;
|
||||
ss << (*g2_)->getAllGroups();
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << std::endl;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
@ -175,7 +175,7 @@ SumOutOperator::getLogCost (void)
|
||||
unsigned nrProdFactors = 0;
|
||||
while (pfIter != pfList_.end()) {
|
||||
if ((*pfIter)->containsGroup (group_)) {
|
||||
vector<PrvGroup> groups = (*pfIter)->getAllGroups();
|
||||
std::vector<PrvGroup> groups = (*pfIter)->getAllGroups();
|
||||
groupSet |= TinySet<PrvGroup> (groups);
|
||||
++ nrProdFactors;
|
||||
}
|
||||
@ -205,7 +205,7 @@ SumOutOperator::getLogCost (void)
|
||||
void
|
||||
SumOutOperator::apply (void)
|
||||
{
|
||||
vector<ParfactorList::iterator> iters;
|
||||
std::vector<ParfactorList::iterator> iters;
|
||||
iters = getParfactorsWithGroup (pfList_, group_);
|
||||
Parfactor* product = *(iters[0]);
|
||||
pfList_.remove (iters[0]);
|
||||
@ -234,13 +234,13 @@ SumOutOperator::apply (void)
|
||||
|
||||
|
||||
|
||||
vector<SumOutOperator*>
|
||||
std::vector<SumOutOperator*>
|
||||
SumOutOperator::getValidOps (
|
||||
ParfactorList& pfList,
|
||||
const Grounds& query)
|
||||
{
|
||||
vector<SumOutOperator*> validOps;
|
||||
set<PrvGroup> allGroups;
|
||||
std::vector<SumOutOperator*> validOps;
|
||||
std::set<PrvGroup> allGroups;
|
||||
ParfactorList::const_iterator it = pfList.begin();
|
||||
while (it != pfList.end()) {
|
||||
const ProbFormulas& formulas = (*it)->arguments();
|
||||
@ -249,7 +249,7 @@ SumOutOperator::getValidOps (
|
||||
}
|
||||
++ it;
|
||||
}
|
||||
set<PrvGroup>::const_iterator groupIt = allGroups.begin();
|
||||
std::set<PrvGroup>::const_iterator groupIt = allGroups.begin();
|
||||
while (groupIt != allGroups.end()) {
|
||||
if (validOp (*groupIt, pfList, query)) {
|
||||
validOps.push_back (new SumOutOperator (*groupIt, pfList));
|
||||
@ -261,18 +261,18 @@ SumOutOperator::getValidOps (
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
SumOutOperator::toString (void)
|
||||
{
|
||||
stringstream ss;
|
||||
vector<ParfactorList::iterator> pfIters;
|
||||
std::stringstream ss;
|
||||
std::vector<ParfactorList::iterator> pfIters;
|
||||
pfIters = getParfactorsWithGroup (pfList_, 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();
|
||||
ss << "|" << tupleSet << " (group " << group_ << ")";
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << std::endl;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
@ -284,7 +284,7 @@ SumOutOperator::validOp (
|
||||
ParfactorList& pfList,
|
||||
const Grounds& query)
|
||||
{
|
||||
vector<ParfactorList::iterator> pfIters;
|
||||
std::vector<ParfactorList::iterator> pfIters;
|
||||
pfIters = getParfactorsWithGroup (pfList, group);
|
||||
if (isToEliminate (*pfIters[0], group, query) == false) {
|
||||
return false;
|
||||
@ -393,10 +393,10 @@ CountingOperator::apply (void)
|
||||
|
||||
|
||||
|
||||
vector<CountingOperator*>
|
||||
std::vector<CountingOperator*>
|
||||
CountingOperator::getValidOps (ParfactorList& pfList)
|
||||
{
|
||||
vector<CountingOperator*> validOps;
|
||||
std::vector<CountingOperator*> validOps;
|
||||
ParfactorList::iterator it = pfList.begin();
|
||||
while (it != pfList.end()) {
|
||||
LogVarSet candidates = (*it)->uncountedLogVars();
|
||||
@ -414,17 +414,17 @@ CountingOperator::getValidOps (ParfactorList& pfList)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
CountingOperator::toString (void)
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "count convert " << X_ << " in " ;
|
||||
ss << (*pfIter_)->getLabel();
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << std::endl;
|
||||
Parfactors pfs = LiftedOperations::countNormalize (*pfIter_, X_);
|
||||
if ((*pfIter_)->constr()->isCountNormalized (X_) == false) {
|
||||
for (size_t i = 0; i < pfs.size(); i++) {
|
||||
ss << " º " << pfs[i]->getLabel() << endl;
|
||||
ss << " º " << pfs[i]->getLabel() << std::endl;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < pfs.size(); i++) {
|
||||
@ -457,14 +457,14 @@ CountingOperator::validOp (Parfactor* g, LogVar X)
|
||||
double
|
||||
GroundOperator::getLogCost (void)
|
||||
{
|
||||
vector<pair<PrvGroup, unsigned>> affectedFormulas;
|
||||
std::vector<std::pair<PrvGroup, unsigned>> affectedFormulas;
|
||||
affectedFormulas = getAffectedFormulas();
|
||||
// cout << "affected formulas: " ;
|
||||
// std::cout << "affected formulas: " ;
|
||||
// for (size_t i = 0; i < affectedFormulas.size(); i++) {
|
||||
// cout << affectedFormulas[i].first << ":" ;
|
||||
// cout << affectedFormulas[i].second << " " ;
|
||||
// std::cout << affectedFormulas[i].first << ":" ;
|
||||
// std::cout << affectedFormulas[i].second << " " ;
|
||||
// }
|
||||
// cout << "cost =" ;
|
||||
// std::cout << "cost =" ;
|
||||
double totalCost = std::log (0.0);
|
||||
ParfactorList::iterator pflIt = pfList_.begin();
|
||||
while (pflIt != pfList_.end()) {
|
||||
@ -495,13 +495,13 @@ GroundOperator::getLogCost (void)
|
||||
}
|
||||
}
|
||||
if (willBeAffected) {
|
||||
// cout << " + " << std::exp (reps) << "x" << std::exp (pfSize);
|
||||
// std::cout << " + " << std::exp (reps) << "x" << std::exp (pfSize);
|
||||
double pfCost = reps + pfSize;
|
||||
totalCost = Util::logSum (totalCost, pfCost);
|
||||
}
|
||||
++ pflIt;
|
||||
}
|
||||
// cout << endl;
|
||||
// std::cout << std::endl;
|
||||
return totalCost + 3;
|
||||
}
|
||||
|
||||
@ -537,11 +537,11 @@ GroundOperator::apply (void)
|
||||
|
||||
|
||||
|
||||
vector<GroundOperator*>
|
||||
std::vector<GroundOperator*>
|
||||
GroundOperator::getValidOps (ParfactorList& pfList)
|
||||
{
|
||||
vector<GroundOperator*> validOps;
|
||||
set<PrvGroup> allGroups;
|
||||
std::vector<GroundOperator*> validOps;
|
||||
std::set<PrvGroup> allGroups;
|
||||
ParfactorList::const_iterator it = pfList.begin();
|
||||
while (it != pfList.end()) {
|
||||
const ProbFormulas& formulas = (*it)->arguments();
|
||||
@ -564,18 +564,18 @@ GroundOperator::getValidOps (ParfactorList& pfList)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
GroundOperator::toString (void)
|
||||
{
|
||||
stringstream ss;
|
||||
vector<ParfactorList::iterator> pfIters;
|
||||
std::stringstream ss;
|
||||
std::vector<ParfactorList::iterator> pfIters;
|
||||
pfIters = getParfactorsWithGroup (pfList_, group_);
|
||||
Parfactor* pf = *(getParfactorsWithGroup (pfList_, group_).front());
|
||||
size_t idx = pf->indexOfGroup (group_);
|
||||
ProbFormula f = pf->argument (idx);
|
||||
LogVar lv = f.logVars()[lvIndex_];
|
||||
TupleSet tupleSet = pf->constr()->tupleSet ({lv});
|
||||
string pos = "th";
|
||||
std::string pos = "th";
|
||||
if (lvIndex_ == 0) {
|
||||
pos = "st" ;
|
||||
} else if (lvIndex_ == 1) {
|
||||
@ -586,21 +586,21 @@ GroundOperator::toString (void)
|
||||
ss << "grounding " << lvIndex_ + 1 << pos << " log var in " ;
|
||||
ss << f.functor() << "/" << f.arity();
|
||||
ss << "|" << tupleSet << " (group " << group_ << ")";
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
|
||||
ss << " [cost=" << std::exp (getLogCost()) << "]" << std::endl;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector<pair<PrvGroup, unsigned>>
|
||||
std::vector<std::pair<PrvGroup, unsigned>>
|
||||
GroundOperator::getAffectedFormulas (void)
|
||||
{
|
||||
vector<pair<PrvGroup, unsigned>> affectedFormulas;
|
||||
affectedFormulas.push_back (make_pair (group_, lvIndex_));
|
||||
queue<pair<PrvGroup, unsigned>> q;
|
||||
q.push (make_pair (group_, lvIndex_));
|
||||
std::vector<std::pair<PrvGroup, unsigned>> affectedFormulas;
|
||||
affectedFormulas.push_back (std::make_pair (group_, lvIndex_));
|
||||
std::queue<std::pair<PrvGroup, unsigned>> q;
|
||||
q.push (std::make_pair (group_, lvIndex_));
|
||||
while (q.empty() == false) {
|
||||
pair<PrvGroup, unsigned> front = q.front();
|
||||
std::pair<PrvGroup, unsigned> front = q.front();
|
||||
ParfactorList::iterator pflIt = pfList_.begin();
|
||||
while (pflIt != pfList_.end()) {
|
||||
size_t idx = (*pflIt)->indexOfGroup (front.first);
|
||||
@ -610,7 +610,7 @@ GroundOperator::getAffectedFormulas (void)
|
||||
const ProbFormulas& fs = (*pflIt)->arguments();
|
||||
for (size_t i = 0; i < fs.size(); i++) {
|
||||
if (i != idx && fs[i].contains (X)) {
|
||||
pair<PrvGroup, unsigned> pair = make_pair (
|
||||
std::pair<PrvGroup, unsigned> pair = std::make_pair (
|
||||
fs[i].group(), fs[i].indexOf (X));
|
||||
if (Util::contains (affectedFormulas, pair) == false) {
|
||||
q.push (pair);
|
||||
@ -647,11 +647,11 @@ LiftedVe::solveQuery (const Grounds& query)
|
||||
void
|
||||
LiftedVe::printSolverFlags (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "lve [" ;
|
||||
ss << "log_domain=" << Util::toString (Globals::logDomain);
|
||||
ss << "]" ;
|
||||
cout << ss.str() << endl;
|
||||
std::cout << ss.str() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -675,9 +675,9 @@ LiftedVe::runSolver (const Grounds& query)
|
||||
break;
|
||||
}
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << "best operation: " << op->toString();
|
||||
std::cout << "best operation: " << op->toString();
|
||||
if (Globals::verbosity > 2) {
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
op->apply();
|
||||
@ -693,8 +693,9 @@ LiftedVe::runSolver (const Grounds& query)
|
||||
}
|
||||
}
|
||||
if (Globals::verbosity > 0) {
|
||||
cout << "largest cost = " << std::exp (largestCost_) << endl;
|
||||
cout << endl;
|
||||
std::cout << "largest cost = " << std::exp (largestCost_);
|
||||
std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
(*pfList_.begin())->simplifyGrounds();
|
||||
(*pfList_.begin())->reorderAccordingGrounds (query);
|
||||
@ -707,7 +708,7 @@ LiftedVe::getBestOperation (const Grounds& query)
|
||||
{
|
||||
double bestCost = 0.0;
|
||||
LiftedOperator* bestOp = 0;
|
||||
vector<LiftedOperator*> validOps;
|
||||
std::vector<LiftedOperator*> validOps;
|
||||
validOps = LiftedOperator::getValidOps (pfList_, query);
|
||||
for (size_t i = 0; i < validOps.size(); i++) {
|
||||
double cost = validOps[i]->getLogCost();
|
||||
|
@ -1,6 +1,9 @@
|
||||
#ifndef PACKAGES_CLPBN_HORUS_LIFTEDVE_H
|
||||
#define PACKAGES_CLPBN_HORUS_LIFTEDVE_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "LiftedSolver.h"
|
||||
#include "ParfactorList.h"
|
||||
|
||||
@ -14,14 +17,14 @@ class LiftedOperator
|
||||
|
||||
virtual void apply (void) = 0;
|
||||
|
||||
virtual string toString (void) = 0;
|
||||
virtual std::string toString (void) = 0;
|
||||
|
||||
static vector<LiftedOperator*> getValidOps (
|
||||
static std::vector<LiftedOperator*> getValidOps (
|
||||
ParfactorList&, const Grounds&);
|
||||
|
||||
static void printValidOps (ParfactorList&, const Grounds&);
|
||||
|
||||
static vector<ParfactorList::iterator> getParfactorsWithGroup (
|
||||
static std::vector<ParfactorList::iterator> getParfactorsWithGroup (
|
||||
ParfactorList&, PrvGroup group);
|
||||
|
||||
private:
|
||||
@ -41,9 +44,9 @@ class ProductOperator : public LiftedOperator
|
||||
|
||||
void apply (void);
|
||||
|
||||
static vector<ProductOperator*> getValidOps (ParfactorList&);
|
||||
static std::vector<ProductOperator*> getValidOps (ParfactorList&);
|
||||
|
||||
string toString (void);
|
||||
std::string toString (void);
|
||||
|
||||
private:
|
||||
static bool validOp (Parfactor*, Parfactor*);
|
||||
@ -67,10 +70,10 @@ class SumOutOperator : public LiftedOperator
|
||||
|
||||
void apply (void);
|
||||
|
||||
static vector<SumOutOperator*> getValidOps (
|
||||
static std::vector<SumOutOperator*> getValidOps (
|
||||
ParfactorList&, const Grounds&);
|
||||
|
||||
string toString (void);
|
||||
std::string toString (void);
|
||||
|
||||
private:
|
||||
static bool validOp (PrvGroup, ParfactorList&, const Grounds&);
|
||||
@ -98,9 +101,9 @@ class CountingOperator : public LiftedOperator
|
||||
|
||||
void apply (void);
|
||||
|
||||
static vector<CountingOperator*> getValidOps (ParfactorList&);
|
||||
static std::vector<CountingOperator*> getValidOps (ParfactorList&);
|
||||
|
||||
string toString (void);
|
||||
std::string toString (void);
|
||||
|
||||
private:
|
||||
static bool validOp (Parfactor*, LogVar);
|
||||
@ -127,12 +130,12 @@ class GroundOperator : public LiftedOperator
|
||||
|
||||
void apply (void);
|
||||
|
||||
static vector<GroundOperator*> getValidOps (ParfactorList&);
|
||||
static std::vector<GroundOperator*> getValidOps (ParfactorList&);
|
||||
|
||||
string toString (void);
|
||||
std::string toString (void);
|
||||
|
||||
private:
|
||||
vector<pair<PrvGroup, unsigned>> getAffectedFormulas (void);
|
||||
std::vector<std::pair<PrvGroup, unsigned>> getAffectedFormulas (void);
|
||||
|
||||
PrvGroup group_;
|
||||
unsigned lvIndex_;
|
||||
|
@ -24,13 +24,13 @@ Literal::indexOfLogVar (LogVar X) const
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
Literal::toString (
|
||||
LogVarSet ipgLogVars,
|
||||
LogVarSet posCountedLvs,
|
||||
LogVarSet negCountedLvs) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
negated_ ? ss << "¬" : ss << "" ;
|
||||
ss << "λ" ;
|
||||
ss << lid_ ;
|
||||
@ -44,7 +44,7 @@ Literal::toString (
|
||||
ss << "-" << logVars_[i];
|
||||
} else if (ipgLogVars.contains (logVars_[i])) {
|
||||
LogVar X = logVars_[i];
|
||||
const string labels[] = {
|
||||
const std::string labels[] = {
|
||||
"a", "b", "c", "d", "e", "f",
|
||||
"g", "h", "i", "j", "k", "m" };
|
||||
(X >= 12) ? ss << "x_" << X : ss << labels[X];
|
||||
@ -60,7 +60,7 @@ Literal::toString (
|
||||
|
||||
|
||||
std::ostream&
|
||||
operator<< (ostream &os, const Literal& lit)
|
||||
operator<< (std::ostream &os, const Literal& lit)
|
||||
{
|
||||
os << lit.toString();
|
||||
return os;
|
||||
@ -320,7 +320,7 @@ void
|
||||
Clause::printClauses (const Clauses& clauses)
|
||||
{
|
||||
for (size_t i = 0; i < clauses.size(); i++) {
|
||||
cout << *clauses[i] << endl;
|
||||
std::cout << *clauses[i] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -337,7 +337,7 @@ Clause::deleteClauses (Clauses& clauses)
|
||||
|
||||
|
||||
std::ostream&
|
||||
operator<< (ostream &os, const Clause& clause)
|
||||
operator<< (std::ostream &os, const Clause& clause)
|
||||
{
|
||||
for (unsigned i = 0; i < clause.literals_.size(); i++) {
|
||||
if (i != 0) os << " v " ;
|
||||
@ -394,7 +394,7 @@ LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
|
||||
/*
|
||||
// INCLUSION-EXCLUSION TEST
|
||||
clauses_.clear();
|
||||
vector<vector<string>> names = {
|
||||
std::vector<std::vector<string>> names = {
|
||||
{"a1","b1"},{"a2","b2"}
|
||||
};
|
||||
Clause* c1 = new Clause (names);
|
||||
@ -406,7 +406,7 @@ LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
|
||||
/*
|
||||
// INDEPENDENT PARTIAL GROUND TEST
|
||||
clauses_.clear();
|
||||
vector<vector<string>> names = {
|
||||
std::vector<std::vector<string>> names = {
|
||||
{"a1","b1"},{"a2","b2"}
|
||||
};
|
||||
Clause* c1 = new Clause (names);
|
||||
@ -422,7 +422,7 @@ LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
|
||||
/*
|
||||
// ATOM-COUNTING TEST
|
||||
clauses_.clear();
|
||||
vector<vector<string>> names = {
|
||||
std::vector<std::vector<string>> names = {
|
||||
{"p1","p1"},{"p1","p2"},{"p1","p3"},
|
||||
{"p2","p1"},{"p2","p2"},{"p2","p3"},
|
||||
{"p3","p1"},{"p3","p2"},{"p3","p3"}
|
||||
@ -438,15 +438,15 @@ LiftedWCNF::LiftedWCNF (const ParfactorList& pfList)
|
||||
*/
|
||||
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << "FORMULA INDICATORS:" << endl;
|
||||
std::cout << "FORMULA INDICATORS:" << std::endl;
|
||||
printFormulaIndicators();
|
||||
cout << endl;
|
||||
cout << "WEIGHTED INDICATORS:" << endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "WEIGHTED INDICATORS:" << std::endl;
|
||||
printWeights();
|
||||
cout << endl;
|
||||
cout << "CLAUSES:" << endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "CLAUSES:" << std::endl;
|
||||
printClauses();
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -462,7 +462,7 @@ LiftedWCNF::~LiftedWCNF (void)
|
||||
void
|
||||
LiftedWCNF::addWeight (LiteralId lid, double posW, double negW)
|
||||
{
|
||||
weights_[lid] = make_pair (posW, negW);
|
||||
weights_[lid] = std::make_pair (posW, negW);
|
||||
}
|
||||
|
||||
|
||||
@ -470,8 +470,8 @@ LiftedWCNF::addWeight (LiteralId lid, double posW, double negW)
|
||||
double
|
||||
LiftedWCNF::posWeight (LiteralId lid) const
|
||||
{
|
||||
unordered_map<LiteralId, std::pair<double,double>>::const_iterator it;
|
||||
it = weights_.find (lid);
|
||||
std::unordered_map<LiteralId, std::pair<double,double>>::const_iterator it
|
||||
= weights_.find (lid);
|
||||
return it != weights_.end() ? it->second.first : LogAware::one();
|
||||
}
|
||||
|
||||
@ -480,14 +480,14 @@ LiftedWCNF::posWeight (LiteralId lid) const
|
||||
double
|
||||
LiftedWCNF::negWeight (LiteralId lid) const
|
||||
{
|
||||
unordered_map<LiteralId, std::pair<double,double>>::const_iterator it;
|
||||
it = weights_.find (lid);
|
||||
std::unordered_map<LiteralId, std::pair<double,double>>::const_iterator it
|
||||
= weights_.find (lid);
|
||||
return it != weights_.end() ? it->second.second : LogAware::one();
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector<LiteralId>
|
||||
std::vector<LiteralId>
|
||||
LiftedWCNF::prvGroupLiterals (PrvGroup prvGroup)
|
||||
{
|
||||
assert (Util::contains (map_, prvGroup));
|
||||
@ -536,7 +536,7 @@ LiftedWCNF::addIndicatorClauses (const ParfactorList& pfList)
|
||||
ConstraintTree tempConstr = (*it)->constr()->projectedCopy(
|
||||
formulas[i].logVars());
|
||||
Clause* clause = new Clause (tempConstr);
|
||||
vector<LiteralId> lids;
|
||||
std::vector<LiteralId> lids;
|
||||
for (size_t j = 0; j < formulas[i].range(); j++) {
|
||||
clause->addLiteral (Literal (freeLiteralId_, formulas[i].logVars()));
|
||||
lids.push_back (freeLiteralId_);
|
||||
@ -568,7 +568,7 @@ LiftedWCNF::addParameterClauses (const ParfactorList& pfList)
|
||||
ParfactorList::const_iterator it = pfList.begin();
|
||||
while (it != pfList.end()) {
|
||||
Indexer indexer ((*it)->ranges());
|
||||
vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||
std::vector<PrvGroup> groups = (*it)->getAllGroups();
|
||||
while (indexer.valid()) {
|
||||
LiteralId paramVarLid = freeLiteralId_;
|
||||
// λu1 ∧ ... ∧ λun ∧ λxi <=> θxi|u1,...,un
|
||||
@ -611,21 +611,21 @@ LiftedWCNF::printFormulaIndicators (void) const
|
||||
if (map_.empty()) {
|
||||
return;
|
||||
}
|
||||
set<PrvGroup> allGroups;
|
||||
std::set<PrvGroup> allGroups;
|
||||
ParfactorList::const_iterator it = pfList_.begin();
|
||||
while (it != pfList_.end()) {
|
||||
const ProbFormulas& formulas = (*it)->arguments();
|
||||
for (size_t i = 0; i < formulas.size(); i++) {
|
||||
if (Util::contains (allGroups, formulas[i].group()) == false) {
|
||||
allGroups.insert (formulas[i].group());
|
||||
cout << formulas[i] << " | " ;
|
||||
std::cout << formulas[i] << " | " ;
|
||||
ConstraintTree tempCt = (*it)->constr()->projectedCopy (
|
||||
formulas[i].logVars());
|
||||
cout << tempCt.tupleSet();
|
||||
cout << " indicators => " ;
|
||||
vector<LiteralId> indicators =
|
||||
std::cout << tempCt.tupleSet();
|
||||
std::cout << " indicators => " ;
|
||||
std::vector<LiteralId> indicators =
|
||||
(map_.find (formulas[i].group()))->second;
|
||||
cout << indicators << endl;
|
||||
std::cout << indicators << std::endl;
|
||||
}
|
||||
}
|
||||
++ it;
|
||||
@ -637,12 +637,12 @@ LiftedWCNF::printFormulaIndicators (void) const
|
||||
void
|
||||
LiftedWCNF::printWeights (void) const
|
||||
{
|
||||
unordered_map<LiteralId, std::pair<double,double>>::const_iterator it;
|
||||
it = weights_.begin();
|
||||
std::unordered_map<LiteralId, std::pair<double,double>>::const_iterator it
|
||||
= weights_.begin();
|
||||
while (it != weights_.end()) {
|
||||
cout << "λ" << it->first << " weights: " ;
|
||||
cout << it->second.first << " " << it->second.second;
|
||||
cout << endl;
|
||||
std::cout << "λ" << it->first << " weights: " ;
|
||||
std::cout << it->second.first << " " << it->second.second;
|
||||
std::cout << std::endl;
|
||||
++ it;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
|
||||
#include "ParfactorList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class ConstraintTree;
|
||||
|
||||
@ -16,8 +15,8 @@ enum LogVarType
|
||||
NEG_LV
|
||||
};
|
||||
|
||||
typedef long LiteralId;
|
||||
typedef vector<LogVarType> LogVarTypes;
|
||||
typedef long LiteralId;
|
||||
typedef std::vector<LogVarType> LogVarTypes;
|
||||
|
||||
|
||||
class Literal
|
||||
@ -47,7 +46,7 @@ class Literal
|
||||
|
||||
size_t indexOfLogVar (LogVar X) const;
|
||||
|
||||
string toString (LogVarSet ipgLogVars = LogVarSet(),
|
||||
std::string toString (LogVarSet ipgLogVars = LogVarSet(),
|
||||
LogVarSet posCountedLvs = LogVarSet(),
|
||||
LogVarSet negCountedLvs = LogVarSet()) const;
|
||||
|
||||
@ -59,7 +58,7 @@ class Literal
|
||||
friend std::ostream& operator<< (std::ostream &os, const Literal& lit);
|
||||
};
|
||||
|
||||
typedef vector<Literal> Literals;
|
||||
typedef std::vector<Literal> Literals;
|
||||
|
||||
|
||||
|
||||
@ -68,7 +67,8 @@ class Clause
|
||||
public:
|
||||
Clause (const ConstraintTree& ct = ConstraintTree({})) : constr_(ct) { }
|
||||
|
||||
Clause (vector<vector<string>> names) : constr_(ConstraintTree (names)) { }
|
||||
Clause (std::vector<std::vector<std::string>> names) :
|
||||
constr_(ConstraintTree (names)) { }
|
||||
|
||||
void addLiteral (const Literal& l) { literals_.push_back (l); }
|
||||
|
||||
@ -132,11 +132,12 @@ class Clause
|
||||
|
||||
static bool independentClauses (Clause& c1, Clause& c2);
|
||||
|
||||
static vector<Clause*> copyClauses (const vector<Clause*>& clauses);
|
||||
static std::vector<Clause*> copyClauses (
|
||||
const std::vector<Clause*>& clauses);
|
||||
|
||||
static void printClauses (const vector<Clause*>& clauses);
|
||||
static void printClauses (const std::vector<Clause*>& clauses);
|
||||
|
||||
static void deleteClauses (vector<Clause*>& clauses);
|
||||
static void deleteClauses (std::vector<Clause*>& clauses);
|
||||
|
||||
private:
|
||||
LogVarSet getLogVarSetExcluding (size_t idx) const;
|
||||
@ -147,12 +148,12 @@ class Clause
|
||||
LogVarSet negCountedLvs_;
|
||||
ConstraintTree constr_;
|
||||
|
||||
friend std::ostream& operator<< (ostream &os, const Clause& clause);
|
||||
friend std::ostream& operator<< (std::ostream &os, const Clause& clause);
|
||||
|
||||
DISALLOW_ASSIGN (Clause);
|
||||
};
|
||||
|
||||
typedef vector<Clause*> Clauses;
|
||||
typedef std::vector<Clause*> Clauses;
|
||||
|
||||
|
||||
|
||||
@ -211,7 +212,7 @@ class LiftedWCNF
|
||||
|
||||
double negWeight (LiteralId lid) const;
|
||||
|
||||
vector<LiteralId> prvGroupLiterals (PrvGroup prvGroup);
|
||||
std::vector<LiteralId> prvGroupLiterals (PrvGroup prvGroup);
|
||||
|
||||
Clause* createClause (LiteralId lid) const;
|
||||
|
||||
@ -228,11 +229,11 @@ class LiftedWCNF
|
||||
|
||||
void addParameterClauses (const ParfactorList& pfList);
|
||||
|
||||
Clauses clauses_;
|
||||
LiteralId freeLiteralId_;
|
||||
const ParfactorList& pfList_;
|
||||
unordered_map<PrvGroup, vector<LiteralId>> map_;
|
||||
unordered_map<LiteralId, std::pair<double,double>> weights_;
|
||||
Clauses clauses_;
|
||||
LiteralId freeLiteralId_;
|
||||
const ParfactorList& pfList_;
|
||||
std::unordered_map<PrvGroup, std::vector<LiteralId>> map_;
|
||||
std::unordered_map<LiteralId, std::pair<double,double>> weights_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN (LiftedWCNF);
|
||||
};
|
||||
|
@ -149,7 +149,7 @@ Parfactor::sumOutIndex (size_t fIdx)
|
||||
unsigned N = constr_->getConditionalCount (
|
||||
args_[fIdx].countedLogVar());
|
||||
unsigned R = args_[fIdx].range();
|
||||
vector<double> numAssigns = HistogramSet::getNumAssigns (N, R);
|
||||
std::vector<double> numAssigns = HistogramSet::getNumAssigns (N, R);
|
||||
Indexer indexer (ranges_, fIdx);
|
||||
while (indexer.valid()) {
|
||||
if (Globals::logDomain) {
|
||||
@ -224,10 +224,10 @@ Parfactor::countConvert (LogVar X)
|
||||
unsigned N = constr_->getConditionalCount (X);
|
||||
unsigned R = ranges_[fIdx];
|
||||
unsigned H = HistogramSet::nrHistograms (N, R);
|
||||
vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
||||
std::vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
||||
|
||||
Indexer indexer (ranges_);
|
||||
vector<Params> sumout (params_.size() / R);
|
||||
std::vector<Params> sumout (params_.size() / R);
|
||||
unsigned count = 0;
|
||||
while (indexer.valid()) {
|
||||
sumout[count].reserve (R);
|
||||
@ -279,11 +279,11 @@ Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
|
||||
unsigned H1 = HistogramSet::nrHistograms (N1, R);
|
||||
unsigned H2 = HistogramSet::nrHistograms (N2, R);
|
||||
|
||||
vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
||||
vector<Histogram> histograms1 = HistogramSet::getHistograms (N1, R);
|
||||
vector<Histogram> histograms2 = HistogramSet::getHistograms (N2, R);
|
||||
std::vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
||||
std::vector<Histogram> histograms1 = HistogramSet::getHistograms (N1, R);
|
||||
std::vector<Histogram> histograms2 = HistogramSet::getHistograms (N2, R);
|
||||
|
||||
vector<unsigned> sumIndexes;
|
||||
std::vector<unsigned> sumIndexes;
|
||||
sumIndexes.reserve (H1 * H2);
|
||||
for (unsigned i = 0; i < H1; i++) {
|
||||
for (unsigned j = 0; j < H2; j++) {
|
||||
@ -319,16 +319,16 @@ Parfactor::fullExpand (LogVar X)
|
||||
|
||||
unsigned N = constr_->getConditionalCount (X);
|
||||
unsigned R = args_[fIdx].range();
|
||||
vector<Histogram> originHists = HistogramSet::getHistograms (N, R);
|
||||
vector<Histogram> expandHists = HistogramSet::getHistograms (1, R);
|
||||
std::vector<Histogram> originHists = HistogramSet::getHistograms (N, R);
|
||||
std::vector<Histogram> expandHists = HistogramSet::getHistograms (1, R);
|
||||
assert (ranges_[fIdx] == originHists.size());
|
||||
vector<unsigned> sumIndexes;
|
||||
std::vector<unsigned> sumIndexes;
|
||||
sumIndexes.reserve (N * R);
|
||||
|
||||
Ranges expandRanges (N, R);
|
||||
Indexer indexer (expandRanges);
|
||||
while (indexer.valid()) {
|
||||
vector<unsigned> hist (R, 0);
|
||||
std::vector<unsigned> hist (R, 0);
|
||||
for (unsigned n = 0; n < N; n++) {
|
||||
hist += expandHists[indexer[n]];
|
||||
}
|
||||
@ -494,7 +494,7 @@ Parfactor::containsGroup (PrvGroup group) const
|
||||
|
||||
|
||||
bool
|
||||
Parfactor::containsGroups (vector<PrvGroup> groups) const
|
||||
Parfactor::containsGroups (std::vector<PrvGroup> groups) const
|
||||
{
|
||||
for (size_t i = 0; i < groups.size(); i++) {
|
||||
if (containsGroup (groups[i]) == false) {
|
||||
@ -565,10 +565,10 @@ Parfactor::nrFormulasWithGroup (PrvGroup group) const
|
||||
|
||||
|
||||
|
||||
vector<PrvGroup>
|
||||
std::vector<PrvGroup>
|
||||
Parfactor::getAllGroups (void) const
|
||||
{
|
||||
vector<PrvGroup> groups (args_.size());
|
||||
std::vector<PrvGroup> groups (args_.size());
|
||||
for (size_t i = 0; i < args_.size(); i++) {
|
||||
groups[i] = args_[i].group();
|
||||
}
|
||||
@ -577,10 +577,10 @@ Parfactor::getAllGroups (void) const
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
Parfactor::getLabel (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "phi(" ;
|
||||
for (size_t i = 0; i < args_.size(); i++) {
|
||||
if (i != 0) ss << "," ;
|
||||
@ -598,6 +598,8 @@ Parfactor::getLabel (void) const
|
||||
void
|
||||
Parfactor::print (bool printParams) const
|
||||
{
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
cout << "Formulas: " ;
|
||||
for (size_t i = 0; i < args_.size(); i++) {
|
||||
if (i != 0) cout << ", " ;
|
||||
@ -605,9 +607,9 @@ Parfactor::print (bool printParams) const
|
||||
}
|
||||
cout << endl;
|
||||
if (args_[0].group() != Util::maxUnsigned()) {
|
||||
vector<string> groups;
|
||||
std::vector<std::string> groups;
|
||||
for (size_t i = 0; i < args_.size(); i++) {
|
||||
groups.push_back (string ("g") + Util::toString (args_[i].group()));
|
||||
groups.push_back (std::string ("g") + Util::toString (args_[i].group()));
|
||||
}
|
||||
cout << "Groups: " << groups << endl;
|
||||
}
|
||||
@ -635,10 +637,10 @@ Parfactor::print (bool printParams) const
|
||||
void
|
||||
Parfactor::printParameters (void) const
|
||||
{
|
||||
vector<string> jointStrings;
|
||||
std::vector<std::string> jointStrings;
|
||||
Indexer indexer (ranges_);
|
||||
while (indexer.valid()) {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
for (size_t i = 0; i < args_.size(); i++) {
|
||||
if (i != 0) ss << ", " ;
|
||||
if (args_[i].isCounting()) {
|
||||
@ -659,8 +661,8 @@ Parfactor::printParameters (void) const
|
||||
++ indexer;
|
||||
}
|
||||
for (size_t i = 0; i < params_.size(); i++) {
|
||||
cout << "f(" << jointStrings[i] << ")" ;
|
||||
cout << " = " << params_[i] << endl;
|
||||
std::cout << "f(" << jointStrings[i] << ")" ;
|
||||
std::cout << " = " << params_[i] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -673,8 +675,8 @@ Parfactor::printProjections (void) const
|
||||
|
||||
LogVarSet Xs = copy.logVarSet();
|
||||
for (size_t i = 0; i < Xs.size(); i++) {
|
||||
cout << "-> projection of " << Xs[i] << ": " ;
|
||||
cout << copy.tupleSet ({Xs[i]}) << endl;
|
||||
std::cout << "-> projection of " << Xs[i] << ": " ;
|
||||
std::cout << copy.tupleSet ({Xs[i]}) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -684,12 +686,12 @@ void
|
||||
Parfactor::expandPotential (
|
||||
size_t fIdx,
|
||||
unsigned newRange,
|
||||
const vector<unsigned>& sumIndexes)
|
||||
const std::vector<unsigned>& sumIndexes)
|
||||
{
|
||||
ullong newSize = (params_.size() / ranges_[fIdx]) * newRange;
|
||||
if (newSize > params_.max_size()) {
|
||||
cerr << "Error: an overflow occurred when performing expansion." ;
|
||||
cerr << endl;
|
||||
std::cerr << "Error: an overflow occurred when performing expansion." ;
|
||||
std::cerr << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@ -698,7 +700,7 @@ Parfactor::expandPotential (
|
||||
params_.reserve (newSize);
|
||||
|
||||
size_t prod = 1;
|
||||
vector<size_t> offsets (ranges_.size());
|
||||
std::vector<size_t> offsets (ranges_.size());
|
||||
for (size_t i = ranges_.size(); i-- > 0; ) {
|
||||
offsets[i] = prod;
|
||||
prod *= ranges_[i];
|
||||
@ -706,7 +708,7 @@ Parfactor::expandPotential (
|
||||
|
||||
size_t index = 0;
|
||||
ranges_[fIdx] = newRange;
|
||||
vector<unsigned> indices (ranges_.size(), 0);
|
||||
std::vector<unsigned> indices (ranges_.size(), 0);
|
||||
for (size_t k = 0; k < newSize; k++) {
|
||||
assert (index < backup.size());
|
||||
params_.push_back (backup[index]);
|
||||
@ -872,12 +874,12 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
|
||||
std::pair<LogVars, LogVars> res = getAlignLogVars (g1, g2);
|
||||
const LogVars& alignLvs1 = res.first;
|
||||
const LogVars& alignLvs2 = res.second;
|
||||
// cout << "ALIGNING :::::::::::::::::" << endl;
|
||||
// std::cout << "ALIGNING :::::::::::::::::" << std::endl;
|
||||
// g1->print();
|
||||
// cout << "AND" << endl;
|
||||
// g2->print();
|
||||
// cout << "-> align lvs1 = " << alignLvs1 << endl;
|
||||
// cout << "-> align lvs2 = " << alignLvs2 << endl;
|
||||
// std::cout << "-> align lvs1 = " << alignLvs1 << std::endl;
|
||||
// std::cout << "-> align lvs2 = " << alignLvs2 << std::endl;
|
||||
LogVar freeLogVar (0);
|
||||
Substitution theta1, theta2;
|
||||
for (size_t i = 0; i < alignLvs1.size(); i++) {
|
||||
@ -933,8 +935,8 @@ Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
|
||||
}
|
||||
}
|
||||
|
||||
// cout << "theta1: " << theta1 << endl;
|
||||
// cout << "theta2: " << theta2 << endl;
|
||||
// std::cout << "theta1: " << theta1 << std::endl;
|
||||
// std::cout << "theta2: " << theta2 << std::endl;
|
||||
g1->applySubstitution (theta1);
|
||||
g2->applySubstitution (theta2);
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ class Parfactor : public TFactor<ProbFormula>
|
||||
|
||||
bool containsGroup (PrvGroup) const;
|
||||
|
||||
bool containsGroups (vector<PrvGroup>) const;
|
||||
bool containsGroups (std::vector<PrvGroup>) const;
|
||||
|
||||
unsigned nrFormulas (LogVar) const;
|
||||
|
||||
@ -81,7 +81,7 @@ class Parfactor : public TFactor<ProbFormula>
|
||||
|
||||
unsigned nrFormulasWithGroup (PrvGroup) const;
|
||||
|
||||
vector<PrvGroup> getAllGroups (void) const;
|
||||
std::vector<PrvGroup> getAllGroups (void) const;
|
||||
|
||||
void print (bool = false) const;
|
||||
|
||||
@ -89,7 +89,7 @@ class Parfactor : public TFactor<ProbFormula>
|
||||
|
||||
void printProjections (void) const;
|
||||
|
||||
string getLabel (void) const;
|
||||
std::string getLabel (void) const;
|
||||
|
||||
void simplifyGrounds (void);
|
||||
|
||||
@ -104,7 +104,7 @@ class Parfactor : public TFactor<ProbFormula>
|
||||
Parfactor* g1, Parfactor* g2);
|
||||
|
||||
void expandPotential (size_t fIdx, unsigned newRange,
|
||||
const vector<unsigned>& sumIndexes);
|
||||
const std::vector<unsigned>& sumIndexes);
|
||||
|
||||
static void alignAndExponentiate (Parfactor*, Parfactor*);
|
||||
|
||||
@ -115,7 +115,7 @@ class Parfactor : public TFactor<ProbFormula>
|
||||
DISALLOW_ASSIGN (Parfactor);
|
||||
};
|
||||
|
||||
typedef vector<Parfactor*> Parfactors;
|
||||
typedef std::vector<Parfactor*> Parfactors;
|
||||
|
||||
#endif // PACKAGES_CLPBN_HORUS_PARFACTOR_H
|
||||
|
||||
|
@ -64,9 +64,9 @@ ParfactorList::addShattered (Parfactor* pf)
|
||||
|
||||
|
||||
|
||||
list<Parfactor*>::iterator
|
||||
std::list<Parfactor*>::iterator
|
||||
ParfactorList::insertShattered (
|
||||
list<Parfactor*>::iterator it,
|
||||
std::list<Parfactor*>::iterator it,
|
||||
Parfactor* pf)
|
||||
{
|
||||
return pfList_.insert (it, pf);
|
||||
@ -75,16 +75,16 @@ ParfactorList::insertShattered (
|
||||
|
||||
|
||||
|
||||
list<Parfactor*>::iterator
|
||||
ParfactorList::remove (list<Parfactor*>::iterator it)
|
||||
std::list<Parfactor*>::iterator
|
||||
ParfactorList::remove (std::list<Parfactor*>::iterator it)
|
||||
{
|
||||
return pfList_.erase (it);
|
||||
}
|
||||
|
||||
|
||||
|
||||
list<Parfactor*>::iterator
|
||||
ParfactorList::removeAndDelete (list<Parfactor*>::iterator it)
|
||||
std::list<Parfactor*>::iterator
|
||||
ParfactorList::removeAndDelete (std::list<Parfactor*>::iterator it)
|
||||
{
|
||||
delete *it;
|
||||
return pfList_.erase (it);
|
||||
@ -98,7 +98,7 @@ ParfactorList::isAllShattered (void) const
|
||||
if (pfList_.size() <= 1) {
|
||||
return true;
|
||||
}
|
||||
vector<Parfactor*> pfs (pfList_.begin(), pfList_.end());
|
||||
Parfactors pfs (pfList_.begin(), pfList_.end());
|
||||
for (size_t i = 0; i < pfs.size(); i++) {
|
||||
assert (isShattered (pfs[i]));
|
||||
}
|
||||
@ -121,7 +121,7 @@ ParfactorList::print (void) const
|
||||
std::sort (pfVec.begin(), pfVec.end(), sortByParams());
|
||||
for (size_t i = 0; i < pfVec.size(); i++) {
|
||||
pfVec[i]->print();
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,8 +163,8 @@ ParfactorList::isShattered (const Parfactor* g) const
|
||||
formulas[i], *(g->constr()),
|
||||
formulas[j], *(g->constr())) == false) {
|
||||
g->print();
|
||||
cout << "-> not identical on positions " ;
|
||||
cout << i << " and " << j << endl;
|
||||
std::cout << "-> not identical on positions " ;
|
||||
std::cout << i << " and " << j << std::endl;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@ -172,8 +172,8 @@ ParfactorList::isShattered (const Parfactor* g) const
|
||||
formulas[i], *(g->constr()),
|
||||
formulas[j], *(g->constr())) == false) {
|
||||
g->print();
|
||||
cout << "-> not disjoint on positions " ;
|
||||
cout << i << " and " << j << endl;
|
||||
std::cout << "-> not disjoint on positions " ;
|
||||
std::cout << i << " and " << j << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -200,9 +200,10 @@ ParfactorList::isShattered (
|
||||
fms1[i], *(g1->constr()),
|
||||
fms2[j], *(g2->constr())) == false) {
|
||||
g1->print();
|
||||
cout << "^" << endl;
|
||||
std::cout << "^" << std::endl;
|
||||
g2->print();
|
||||
cout << "-> not identical on group " << fms1[i].group() << endl;
|
||||
std::cout << "-> not identical on group " ;
|
||||
std::cout << fms1[i].group() << std::endl;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@ -210,10 +211,10 @@ ParfactorList::isShattered (
|
||||
fms1[i], *(g1->constr()),
|
||||
fms2[j], *(g2->constr())) == false) {
|
||||
g1->print();
|
||||
cout << "^" << endl;
|
||||
std::cout << "^" << std::endl;
|
||||
g2->print();
|
||||
cout << "-> not disjoint on groups " << fms1[i].group();
|
||||
cout << " and " << fms2[j].group() << endl;
|
||||
std::cout << "-> not disjoint on groups " << fms1[i].group();
|
||||
std::cout << " and " << fms2[j].group() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -227,12 +228,12 @@ ParfactorList::isShattered (
|
||||
void
|
||||
ParfactorList::addToShatteredList (Parfactor* g)
|
||||
{
|
||||
queue<Parfactor*> residuals;
|
||||
std::queue<Parfactor*> residuals;
|
||||
residuals.push (g);
|
||||
while (residuals.empty() == false) {
|
||||
Parfactor* pf = residuals.front();
|
||||
bool pfSplitted = false;
|
||||
list<Parfactor*>::iterator pfIter;
|
||||
std::list<Parfactor*>::iterator pfIter;
|
||||
pfIter = pfList_.begin();
|
||||
while (pfIter != pfList_.end()) {
|
||||
std::pair<Parfactors, Parfactors> shattRes;
|
||||
@ -269,7 +270,7 @@ Parfactors
|
||||
ParfactorList::shatterAgainstMySelf (Parfactor* g)
|
||||
{
|
||||
Parfactors pfs;
|
||||
queue<Parfactor*> residuals;
|
||||
std::queue<Parfactor*> residuals;
|
||||
residuals.push (g);
|
||||
bool shattered = true;
|
||||
while (residuals.empty() == false) {
|
||||
@ -325,19 +326,22 @@ ParfactorList::shatterAgainstMySelf (
|
||||
{
|
||||
/*
|
||||
Util::printDashedLine();
|
||||
cout << "-> SHATTERING" << endl;
|
||||
std::cout << "-> SHATTERING" << std::endl;
|
||||
g->print();
|
||||
cout << "-> ON: " << g->argument (fIdx1) << "|" ;
|
||||
cout << g->constr()->tupleSet (g->argument (fIdx1).logVars()) << endl;
|
||||
cout << "-> ON: " << g->argument (fIdx2) << "|" ;
|
||||
cout << g->constr()->tupleSet (g->argument (fIdx2).logVars()) << endl;
|
||||
std::cout << "-> ON: " << g->argument (fIdx1) << "|" ;
|
||||
std::cout << g->constr()->tupleSet (g->argument (fIdx1).logVars());
|
||||
std::cout << std::endl;
|
||||
std::cout << "-> ON: " << g->argument (fIdx2) << "|" ;
|
||||
std::cout << g->constr()->tupleSet (g->argument (fIdx2).logVars())
|
||||
std::cout << std::endl;
|
||||
Util::printDashedLine();
|
||||
*/
|
||||
ProbFormula& f1 = g->argument (fIdx1);
|
||||
ProbFormula& f2 = g->argument (fIdx2);
|
||||
if (f1.isAtom()) {
|
||||
cerr << "Error: a ground occurs twice in the same parfactor." << endl;
|
||||
cerr << endl;
|
||||
std::cerr << "Error: a ground occurs twice in the same parfactor." ;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
assert (g->constr()->empty() == false);
|
||||
@ -441,14 +445,14 @@ ParfactorList::shatter (
|
||||
ProbFormula& f2 = g2->argument (fIdx2);
|
||||
/*
|
||||
Util::printDashedLine();
|
||||
cout << "-> SHATTERING" << endl;
|
||||
std::cout << "-> SHATTERING" << std::endl;
|
||||
g1->print();
|
||||
cout << "-> WITH" << endl;
|
||||
std::cout << "-> WITH" << std::endl;
|
||||
g2->print();
|
||||
cout << "-> ON: " << f1 << "|" ;
|
||||
cout << g1->constr()->tupleSet (f1.logVars()) << endl;
|
||||
cout << "-> ON: " << f2 << "|" ;
|
||||
cout << g2->constr()->tupleSet (f2.logVars()) << endl;
|
||||
std::cout << "-> ON: " << f1 << "|" ;
|
||||
std::cout << g1->constr()->tupleSet (f1.logVars()) << std::endl;
|
||||
std::cout << "-> ON: " << f2 << "|" ;
|
||||
std::cout << g2->constr()->tupleSet (f2.logVars()) << std::endl;
|
||||
Util::printDashedLine();
|
||||
*/
|
||||
if (f1.isAtom()) {
|
||||
@ -486,12 +490,12 @@ ParfactorList::shatter (
|
||||
assert (commCt1->tupleSet (f1.logVars()) ==
|
||||
commCt2->tupleSet (f2.logVars()));
|
||||
|
||||
// stringstream ss1; ss1 << "" << count << "_A.dot" ;
|
||||
// stringstream ss2; ss2 << "" << count << "_B.dot" ;
|
||||
// stringstream ss3; ss3 << "" << count << "_A_comm.dot" ;
|
||||
// stringstream ss4; ss4 << "" << count << "_A_excl.dot" ;
|
||||
// stringstream ss5; ss5 << "" << count << "_B_comm.dot" ;
|
||||
// stringstream ss6; ss6 << "" << count << "_B_excl.dot" ;
|
||||
// std::stringstream ss1; ss1 << "" << count << "_A.dot" ;
|
||||
// std::stringstream ss2; ss2 << "" << count << "_B.dot" ;
|
||||
// std::stringstream ss3; ss3 << "" << count << "_A_comm.dot" ;
|
||||
// std::stringstream ss4; ss4 << "" << count << "_A_excl.dot" ;
|
||||
// std::stringstream ss5; ss5 << "" << count << "_B_comm.dot" ;
|
||||
// std::stringstream ss6; ss6 << "" << count << "_B_excl.dot" ;
|
||||
// g1->constr()->exportToGraphViz (ss1.str().c_str(), true);
|
||||
// g2->constr()->exportToGraphViz (ss2.str().c_str(), true);
|
||||
// commCt1->exportToGraphViz (ss3.str().c_str(), true);
|
||||
|
@ -6,12 +6,9 @@
|
||||
#include "Parfactor.h"
|
||||
#include "ProbFormula.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class Parfactor;
|
||||
|
||||
|
||||
class ParfactorList
|
||||
{
|
||||
public:
|
||||
@ -23,7 +20,7 @@ class ParfactorList
|
||||
|
||||
~ParfactorList (void);
|
||||
|
||||
const list<Parfactor*>& parfactors (void) const { return pfList_; }
|
||||
const std::list<Parfactor*>& parfactors (void) const { return pfList_; }
|
||||
|
||||
void clear (void) { pfList_.clear(); }
|
||||
|
||||
@ -47,12 +44,14 @@ class ParfactorList
|
||||
|
||||
void addShattered (Parfactor* pf);
|
||||
|
||||
list<Parfactor*>::iterator insertShattered (
|
||||
list<Parfactor*>::iterator, Parfactor*);
|
||||
std::list<Parfactor*>::iterator insertShattered (
|
||||
std::list<Parfactor*>::iterator, Parfactor*);
|
||||
|
||||
list<Parfactor*>::iterator remove (list<Parfactor*>::iterator);
|
||||
std::list<Parfactor*>::iterator remove (
|
||||
std::list<Parfactor*>::iterator);
|
||||
|
||||
list<Parfactor*>::iterator removeAndDelete (list<Parfactor*>::iterator);
|
||||
std::list<Parfactor*>::iterator removeAndDelete (
|
||||
std::list<Parfactor*>::iterator);
|
||||
|
||||
bool isAllShattered (void) const;
|
||||
|
||||
@ -115,7 +114,7 @@ class ParfactorList
|
||||
}
|
||||
};
|
||||
|
||||
list<Parfactor*> pfList_;
|
||||
std::list<Parfactor*> pfList_;
|
||||
};
|
||||
|
||||
#endif // PACKAGES_CLPBN_HORUS_PARFACTORLIST_H
|
||||
|
@ -101,7 +101,7 @@ bool operator== (const ProbFormula& f1, const ProbFormula& f2)
|
||||
|
||||
|
||||
|
||||
std::ostream& operator<< (ostream &os, const ProbFormula& f)
|
||||
std::ostream& operator<< (std::ostream &os, const ProbFormula& f)
|
||||
{
|
||||
os << f.functor_;
|
||||
if (f.isAtom() == false) {
|
||||
@ -147,7 +147,7 @@ ObservedFormula::ObservedFormula (Symbol f, unsigned ev, const Tuple& tuple)
|
||||
|
||||
|
||||
|
||||
ostream& operator<< (ostream &os, const ObservedFormula& of)
|
||||
std::ostream& operator<< (std::ostream &os, const ObservedFormula& of)
|
||||
{
|
||||
os << of.functor_ << "/" << of.arity_;
|
||||
os << "|" << of.constr_.tupleSet();
|
||||
|
@ -58,9 +58,11 @@ class ProbFormula
|
||||
|
||||
static PrvGroup getNewGroup (void);
|
||||
|
||||
friend bool operator== (const ProbFormula& f1, const ProbFormula& f2);
|
||||
friend bool operator== (
|
||||
const ProbFormula& f1, const ProbFormula& f2);
|
||||
|
||||
friend std::ostream& operator<< (ostream &os, const ProbFormula& f);
|
||||
friend std::ostream& operator<< (
|
||||
std::ostream &os, const ProbFormula& f);
|
||||
|
||||
private:
|
||||
Symbol functor_;
|
||||
@ -71,7 +73,7 @@ class ProbFormula
|
||||
static PrvGroup freeGroup_;
|
||||
};
|
||||
|
||||
typedef vector<ProbFormula> ProbFormulas;
|
||||
typedef std::vector<ProbFormula> ProbFormulas;
|
||||
|
||||
|
||||
class ObservedFormula
|
||||
@ -101,10 +103,11 @@ class ObservedFormula
|
||||
unsigned evidence_;
|
||||
ConstraintTree constr_;
|
||||
|
||||
friend ostream& operator<< (ostream &os, const ObservedFormula& of);
|
||||
friend std::ostream& operator<< (
|
||||
std::ostream &os, const ObservedFormula& of);
|
||||
};
|
||||
|
||||
typedef vector<ObservedFormula> ObservedFormulas;
|
||||
typedef std::vector<ObservedFormula> ObservedFormulas;
|
||||
|
||||
#endif // PACKAGES_CLPBN_HORUS_PROBFORMULA_H
|
||||
|
||||
|
@ -2,19 +2,16 @@
|
||||
#define PACKAGES_CLPBN_HORUS_TINYSET_H
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
template <typename T, typename Compare = std::less<T>>
|
||||
class TinySet
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename vector<T>::iterator iterator;
|
||||
typedef typename vector<T>::const_iterator const_iterator;
|
||||
typedef typename std::vector<T>::iterator iterator;
|
||||
typedef typename std::vector<T>::const_iterator const_iterator;
|
||||
|
||||
TinySet (const TinySet& s)
|
||||
: vec_(s.vec_), cmp_(s.cmp_) { }
|
||||
@ -25,7 +22,7 @@ class TinySet
|
||||
TinySet (const T& t, const Compare& cmp = Compare())
|
||||
: vec_(1, t), cmp_(cmp) { }
|
||||
|
||||
TinySet (const vector<T>& elements, const Compare& cmp = Compare());
|
||||
TinySet (const std::vector<T>& elements, const Compare& cmp = Compare());
|
||||
|
||||
iterator insert (const T& t);
|
||||
|
||||
@ -60,9 +57,9 @@ class TinySet
|
||||
|
||||
bool intersects (const TinySet& s) const;
|
||||
|
||||
const T& operator[] (typename vector<T>::size_type i) const;
|
||||
const T& operator[] (typename std::vector<T>::size_type i) const;
|
||||
|
||||
T& operator[] (typename vector<T>::size_type i);
|
||||
T& operator[] (typename std::vector<T>::size_type i);
|
||||
|
||||
T front (void) const;
|
||||
|
||||
@ -72,15 +69,15 @@ class TinySet
|
||||
|
||||
T& back (void);
|
||||
|
||||
const vector<T>& elements (void) const;
|
||||
const std::vector<T>& elements (void) const;
|
||||
|
||||
bool empty (void) const;
|
||||
|
||||
typename vector<T>::size_type size (void) const;
|
||||
typename std::vector<T>::size_type size (void) const;
|
||||
|
||||
void clear (void);
|
||||
|
||||
void reserve (typename vector<T>::size_type size);
|
||||
void reserve (typename std::vector<T>::size_type size);
|
||||
|
||||
iterator begin (void) { return vec_.begin(); }
|
||||
iterator end (void) { return vec_.end(); }
|
||||
@ -92,8 +89,8 @@ class TinySet
|
||||
|
||||
bool consistent (void) const;
|
||||
|
||||
vector<T> vec_;
|
||||
Compare cmp_;
|
||||
std::vector<T> vec_;
|
||||
Compare cmp_;
|
||||
|
||||
friend bool operator== (const TinySet& s1, const TinySet& s2)
|
||||
{
|
||||
@ -108,7 +105,7 @@ class TinySet
|
||||
friend std::ostream& operator<< (std::ostream& out, const TinySet& s)
|
||||
{
|
||||
out << "{" ;
|
||||
typename vector<T>::size_type i;
|
||||
typename std::vector<T>::size_type i;
|
||||
for (i = 0; i < s.size(); i++) {
|
||||
out << ((i != 0) ? "," : "") << s.vec_[i];
|
||||
}
|
||||
@ -121,7 +118,7 @@ class TinySet
|
||||
|
||||
|
||||
template <typename T, typename C> inline
|
||||
TinySet<T,C>::TinySet (const vector<T>& elements, const C& cmp)
|
||||
TinySet<T,C>::TinySet (const std::vector<T>& elements, const C& cmp)
|
||||
: vec_(elements), cmp_(cmp)
|
||||
{
|
||||
std::sort (begin(), end(), cmp_);
|
||||
@ -290,7 +287,7 @@ TinySet<T,C>::intersects (const TinySet& s) const
|
||||
|
||||
|
||||
template <typename T, typename C> inline const T&
|
||||
TinySet<T,C>::operator[] (typename vector<T>::size_type i) const
|
||||
TinySet<T,C>::operator[] (typename std::vector<T>::size_type i) const
|
||||
{
|
||||
return vec_[i];
|
||||
}
|
||||
@ -298,7 +295,7 @@ TinySet<T,C>::operator[] (typename vector<T>::size_type i) const
|
||||
|
||||
|
||||
template <typename T, typename C> inline T&
|
||||
TinySet<T,C>::operator[] (typename vector<T>::size_type i)
|
||||
TinySet<T,C>::operator[] (typename std::vector<T>::size_type i)
|
||||
{
|
||||
return vec_[i];
|
||||
}
|
||||
@ -337,7 +334,7 @@ TinySet<T,C>::back (void)
|
||||
|
||||
|
||||
|
||||
template <typename T, typename C> inline const vector<T>&
|
||||
template <typename T, typename C> inline const std::vector<T>&
|
||||
TinySet<T,C>::elements (void) const
|
||||
{
|
||||
return vec_;
|
||||
@ -353,7 +350,7 @@ TinySet<T,C>::empty (void) const
|
||||
|
||||
|
||||
|
||||
template <typename T, typename C> inline typename vector<T>::size_type
|
||||
template <typename T, typename C> inline typename std::vector<T>::size_type
|
||||
TinySet<T,C>::size (void) const
|
||||
{
|
||||
return vec_.size();
|
||||
@ -370,7 +367,7 @@ TinySet<T,C>::clear (void)
|
||||
|
||||
|
||||
template <typename T, typename C> inline void
|
||||
TinySet<T,C>::reserve (typename vector<T>::size_type size)
|
||||
TinySet<T,C>::reserve (typename std::vector<T>::size_type size)
|
||||
{
|
||||
vec_.reserve (size);
|
||||
}
|
||||
@ -397,7 +394,7 @@ TinySet<T,C>::unique_cmp (iterator first, iterator last)
|
||||
template <typename T, typename C> inline bool
|
||||
TinySet<T,C>::consistent (void) const
|
||||
{
|
||||
typename vector<T>::size_type i;
|
||||
typename std::vector<T>::size_type i;
|
||||
for (i = 0; i < vec_.size() - 1; i++) {
|
||||
if ( ! cmp_(vec_[i], vec_[i + 1])) {
|
||||
return false;
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
|
||||
namespace Globals {
|
||||
|
||||
bool logDomain = false;
|
||||
|
||||
unsigned verbosity = 0;
|
||||
@ -33,14 +34,14 @@ toString (const bool& b)
|
||||
|
||||
|
||||
unsigned
|
||||
stringToUnsigned (string str)
|
||||
stringToUnsigned (std::string str)
|
||||
{
|
||||
int val;
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << str;
|
||||
ss >> val;
|
||||
if (val < 0) {
|
||||
cerr << "Error: the number readed is negative." << endl;
|
||||
std::cerr << "Error: the number readed is negative." << std::endl;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
return static_cast<unsigned> (val);
|
||||
@ -49,10 +50,10 @@ stringToUnsigned (string str)
|
||||
|
||||
|
||||
double
|
||||
stringToDouble (string str)
|
||||
stringToDouble (std::string str)
|
||||
{
|
||||
double val;
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << str;
|
||||
ss >> val;
|
||||
return val;
|
||||
@ -117,7 +118,7 @@ size_t
|
||||
sizeExpected (const Ranges& ranges)
|
||||
{
|
||||
return std::accumulate (ranges.begin(),
|
||||
ranges.end(), 1, multiplies<unsigned>());
|
||||
ranges.end(), 1, std::multiplies<unsigned>());
|
||||
}
|
||||
|
||||
|
||||
@ -136,10 +137,10 @@ nrDigits (int num)
|
||||
|
||||
|
||||
bool
|
||||
isInteger (const string& s)
|
||||
isInteger (const std::string& s)
|
||||
{
|
||||
stringstream ss1 (s);
|
||||
stringstream ss2;
|
||||
std::stringstream ss1 (s);
|
||||
std::stringstream ss2;
|
||||
int integer;
|
||||
ss1 >> integer;
|
||||
ss2 << integer;
|
||||
@ -148,10 +149,10 @@ isInteger (const string& s)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
parametersToString (const Params& v, unsigned precision)
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss.precision (precision);
|
||||
ss << "[" ;
|
||||
for (size_t i = 0; i < v.size(); i++) {
|
||||
@ -164,7 +165,7 @@ parametersToString (const Params& v, unsigned precision)
|
||||
|
||||
|
||||
|
||||
vector<string>
|
||||
std::vector<std::string>
|
||||
getStateLines (const Vars& vars)
|
||||
{
|
||||
Ranges ranges;
|
||||
@ -172,9 +173,9 @@ getStateLines (const Vars& vars)
|
||||
ranges.push_back (vars[i]->range());
|
||||
}
|
||||
Indexer indexer (ranges);
|
||||
vector<string> jointStrings;
|
||||
std::vector<std::string> jointStrings;
|
||||
while (indexer.valid()) {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
for (size_t i = 0; i < vars.size(); i++) {
|
||||
if (i != 0) ss << ", " ;
|
||||
ss << vars[i]->label() << "=" ;
|
||||
@ -188,18 +189,18 @@ getStateLines (const Vars& vars)
|
||||
|
||||
|
||||
|
||||
bool invalidValue (string option, string value)
|
||||
bool invalidValue (std::string option, std::string value)
|
||||
{
|
||||
cerr << "Warning: invalid value `" << value << "' " ;
|
||||
cerr << "for `" << option << "'." ;
|
||||
cerr << endl;
|
||||
std::cerr << "Warning: invalid value `" << value << "' " ;
|
||||
std::cerr << "for `" << option << "'." ;
|
||||
std::cerr << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
setHorusFlag (string option, string value)
|
||||
setHorusFlag (std::string option, std::string value)
|
||||
{
|
||||
bool returnVal = true;
|
||||
if (option == "lifted_solver") {
|
||||
@ -215,7 +216,7 @@ setHorusFlag (string option, string value)
|
||||
else returnVal = invalidValue (option, value);
|
||||
|
||||
} else if (option == "verbosity") {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << value;
|
||||
ss >> Globals::verbosity;
|
||||
|
||||
@ -251,14 +252,14 @@ setHorusFlag (string option, string value)
|
||||
returnVal = invalidValue (option, value);
|
||||
|
||||
} else if (option == "bp_accuracy") {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
double acc;
|
||||
ss << value;
|
||||
ss >> acc;
|
||||
BeliefProp::setAccuracy (acc);
|
||||
|
||||
} else if (option == "bp_max_iter") {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
unsigned mi;
|
||||
ss << value;
|
||||
ss >> mi;
|
||||
@ -285,7 +286,7 @@ setHorusFlag (string option, string value)
|
||||
else returnVal = invalidValue (option, value);
|
||||
|
||||
} else {
|
||||
cerr << "Warning: invalid option `" << option << "'" << endl;
|
||||
std::cerr << "Warning: invalid option `" << option << "'" << std::endl;
|
||||
returnVal = false;
|
||||
}
|
||||
return returnVal;
|
||||
@ -294,20 +295,20 @@ setHorusFlag (string option, string value)
|
||||
|
||||
|
||||
void
|
||||
printHeader (string header, std::ostream& os)
|
||||
printHeader (std::string header, std::ostream& os)
|
||||
{
|
||||
printAsteriskLine (os);
|
||||
os << header << endl;
|
||||
os << header << std::endl;
|
||||
printAsteriskLine (os);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
printSubHeader (string header, std::ostream& os)
|
||||
printSubHeader (std::string header, std::ostream& os)
|
||||
{
|
||||
printDashedLine (os);
|
||||
os << header << endl;
|
||||
os << header << std::endl;
|
||||
printDashedLine (os);
|
||||
}
|
||||
|
||||
@ -318,7 +319,7 @@ printAsteriskLine (std::ostream& os)
|
||||
{
|
||||
os << "********************************" ;
|
||||
os << "********************************" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -328,7 +329,7 @@ printDashedLine (std::ostream& os)
|
||||
{
|
||||
os << "--------------------------------" ;
|
||||
os << "--------------------------------" ;
|
||||
os << endl;
|
||||
os << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
@ -17,8 +17,6 @@
|
||||
|
||||
#include "Horus.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
@ -29,43 +27,52 @@ const double NEG_INF = -std::numeric_limits<double>::infinity();
|
||||
|
||||
namespace Util {
|
||||
|
||||
template <typename T> void addToVector (vector<T>&, const vector<T>&);
|
||||
template <typename T> void
|
||||
addToVector (std::vector<T>&, const std::vector<T>&);
|
||||
|
||||
template <typename T> void addToSet (set<T>&, const vector<T>&);
|
||||
template <typename T> void
|
||||
addToSet (std::set<T>&, const std::vector<T>&);
|
||||
|
||||
template <typename T> void addToQueue (queue<T>&, const vector<T>&);
|
||||
template <typename T> void
|
||||
addToQueue (std::queue<T>&, const std::vector<T>&);
|
||||
|
||||
template <typename T> bool contains (const vector<T>&, const T&);
|
||||
template <typename T> bool
|
||||
contains (const std::vector<T>&, const T&);
|
||||
|
||||
template <typename T> bool contains (const set<T>&, const T&);
|
||||
template <typename T> bool contains
|
||||
(const std::set<T>&, const T&);
|
||||
|
||||
template <typename K, typename V> bool contains (
|
||||
const unordered_map<K, V>&, const K&);
|
||||
template <typename K, typename V> bool
|
||||
contains (const std::unordered_map<K, V>&, const K&);
|
||||
|
||||
template <typename T> size_t indexOf (const vector<T>&, const T&);
|
||||
template <typename T> size_t
|
||||
indexOf (const std::vector<T>&, const T&);
|
||||
|
||||
template <class Operation>
|
||||
void apply_n_times (Params& v1, const Params& v2,
|
||||
unsigned repetitions, Operation);
|
||||
template <class Operation> void
|
||||
apply_n_times (Params& v1, const Params& v2, unsigned reps, Operation);
|
||||
|
||||
template <typename T> void log (vector<T>&);
|
||||
template <typename T> void
|
||||
log (std::vector<T>&);
|
||||
|
||||
template <typename T> void exp (vector<T>&);
|
||||
template <typename T> void
|
||||
exp (std::vector<T>&);
|
||||
|
||||
template <typename T> string elementsToString (
|
||||
const vector<T>& v, string sep = " ");
|
||||
template <typename T> std::string
|
||||
elementsToString (const std::vector<T>& v, std::string sep = " ");
|
||||
|
||||
template <typename T> std::string toString (const T&);
|
||||
template <typename T> std::string
|
||||
toString (const T&);
|
||||
|
||||
template <> std::string toString (const bool&);
|
||||
template <> std::string
|
||||
toString (const bool&);
|
||||
|
||||
double logSum (double, double);
|
||||
|
||||
unsigned maxUnsigned (void);
|
||||
|
||||
unsigned stringToUnsigned (string);
|
||||
unsigned stringToUnsigned (std::string);
|
||||
|
||||
double stringToDouble (string);
|
||||
double stringToDouble (std::string);
|
||||
|
||||
double factorial (unsigned);
|
||||
|
||||
@ -77,17 +84,18 @@ size_t sizeExpected (const Ranges&);
|
||||
|
||||
unsigned nrDigits (int);
|
||||
|
||||
bool isInteger (const string&);
|
||||
bool isInteger (const std::string&);
|
||||
|
||||
string parametersToString (const Params&, unsigned = Constants::PRECISION);
|
||||
std::string parametersToString (
|
||||
const Params&, unsigned = Constants::PRECISION);
|
||||
|
||||
vector<string> getStateLines (const Vars&);
|
||||
std::vector<std::string> getStateLines (const Vars&);
|
||||
|
||||
bool setHorusFlag (string option, string value);
|
||||
bool setHorusFlag (std::string option, std::string value);
|
||||
|
||||
void printHeader (string, std::ostream& os = std::cout);
|
||||
void printHeader (std::string, std::ostream& os = std::cout);
|
||||
|
||||
void printSubHeader (string, std::ostream& os = std::cout);
|
||||
void printSubHeader (std::string, std::ostream& os = std::cout);
|
||||
|
||||
void printAsteriskLine (std::ostream& os = std::cout);
|
||||
|
||||
@ -98,7 +106,7 @@ void printDashedLine (std::ostream& os = std::cout);
|
||||
|
||||
|
||||
template <typename T> void
|
||||
Util::addToVector (vector<T>& v, const vector<T>& elements)
|
||||
Util::addToVector (std::vector<T>& v, const std::vector<T>& elements)
|
||||
{
|
||||
v.insert (v.end(), elements.begin(), elements.end());
|
||||
}
|
||||
@ -106,7 +114,7 @@ Util::addToVector (vector<T>& v, const vector<T>& elements)
|
||||
|
||||
|
||||
template <typename T> void
|
||||
Util::addToSet (set<T>& s, const vector<T>& elements)
|
||||
Util::addToSet (std::set<T>& s, const std::vector<T>& elements)
|
||||
{
|
||||
s.insert (elements.begin(), elements.end());
|
||||
}
|
||||
@ -114,7 +122,7 @@ Util::addToSet (set<T>& s, const vector<T>& elements)
|
||||
|
||||
|
||||
template <typename T> void
|
||||
Util::addToQueue (queue<T>& q, const vector<T>& elements)
|
||||
Util::addToQueue (std::queue<T>& q, const std::vector<T>& elements)
|
||||
{
|
||||
for (size_t i = 0; i < elements.size(); i++) {
|
||||
q.push (elements[i]);
|
||||
@ -124,7 +132,7 @@ Util::addToQueue (queue<T>& q, const vector<T>& elements)
|
||||
|
||||
|
||||
template <typename T> bool
|
||||
Util::contains (const vector<T>& v, const T& e)
|
||||
Util::contains (const std::vector<T>& v, const T& e)
|
||||
{
|
||||
return std::find (v.begin(), v.end(), e) != v.end();
|
||||
}
|
||||
@ -132,7 +140,7 @@ Util::contains (const vector<T>& v, const T& e)
|
||||
|
||||
|
||||
template <typename T> bool
|
||||
Util::contains (const set<T>& s, const T& e)
|
||||
Util::contains (const std::set<T>& s, const T& e)
|
||||
{
|
||||
return s.find (e) != s.end();
|
||||
}
|
||||
@ -140,7 +148,7 @@ Util::contains (const set<T>& s, const T& e)
|
||||
|
||||
|
||||
template <typename K, typename V> bool
|
||||
Util::contains (const unordered_map<K, V>& m, const K& k)
|
||||
Util::contains (const std::unordered_map<K, V>& m, const K& k)
|
||||
{
|
||||
return m.find (k) != m.end();
|
||||
}
|
||||
@ -148,7 +156,7 @@ Util::contains (const unordered_map<K, V>& m, const K& k)
|
||||
|
||||
|
||||
template <typename T> size_t
|
||||
Util::indexOf (const vector<T>& v, const T& e)
|
||||
Util::indexOf (const std::vector<T>& v, const T& e)
|
||||
{
|
||||
return std::distance (v.begin(),
|
||||
std::find (v.begin(), v.end(), e));
|
||||
@ -157,7 +165,10 @@ Util::indexOf (const vector<T>& v, const T& e)
|
||||
|
||||
|
||||
template <class Operation> void
|
||||
Util::apply_n_times (Params& v1, const Params& v2, unsigned repetitions,
|
||||
Util::apply_n_times (
|
||||
Params& v1,
|
||||
const Params& v2,
|
||||
unsigned repetitions,
|
||||
Operation unary_op)
|
||||
{
|
||||
Params::iterator first = v1.begin();
|
||||
@ -176,7 +187,7 @@ Util::apply_n_times (Params& v1, const Params& v2, unsigned repetitions,
|
||||
|
||||
|
||||
template <typename T> void
|
||||
Util::log (vector<T>& v)
|
||||
Util::log (std::vector<T>& v)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(), ::log);
|
||||
}
|
||||
@ -184,17 +195,17 @@ Util::log (vector<T>& v)
|
||||
|
||||
|
||||
template <typename T> void
|
||||
Util::exp (vector<T>& v)
|
||||
Util::exp (std::vector<T>& v)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(), ::exp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T> string
|
||||
Util::elementsToString (const vector<T>& v, string sep)
|
||||
template <typename T> std::string
|
||||
Util::elementsToString (const std::vector<T>& v, std::string sep)
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
for (size_t i = 0; i < v.size(); i++) {
|
||||
ss << ((i != 0) ? sep : "") << v[i];
|
||||
}
|
||||
@ -287,7 +298,7 @@ template <typename T>
|
||||
void operator+=(std::vector<T>& v, double val)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(),
|
||||
std::bind2nd (plus<double>(), val));
|
||||
std::bind2nd (std::plus<double>(), val));
|
||||
}
|
||||
|
||||
|
||||
@ -296,7 +307,7 @@ template <typename T>
|
||||
void operator-=(std::vector<T>& v, double val)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(),
|
||||
std::bind2nd (minus<double>(), val));
|
||||
std::bind2nd (std::minus<double>(), val));
|
||||
}
|
||||
|
||||
|
||||
@ -305,7 +316,7 @@ template <typename T>
|
||||
void operator*=(std::vector<T>& v, double val)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(),
|
||||
std::bind2nd (multiplies<double>(), val));
|
||||
std::bind2nd (std::multiplies<double>(), val));
|
||||
}
|
||||
|
||||
|
||||
@ -314,7 +325,7 @@ template <typename T>
|
||||
void operator/=(std::vector<T>& v, double val)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(),
|
||||
std::bind2nd (divides<double>(), val));
|
||||
std::bind2nd (std::divides<double>(), val));
|
||||
}
|
||||
|
||||
|
||||
@ -324,7 +335,7 @@ void operator+=(std::vector<T>& a, const std::vector<T>& b)
|
||||
{
|
||||
assert (a.size() == b.size());
|
||||
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||
plus<double>());
|
||||
std::plus<double>());
|
||||
}
|
||||
|
||||
|
||||
@ -334,7 +345,7 @@ void operator-=(std::vector<T>& a, const std::vector<T>& b)
|
||||
{
|
||||
assert (a.size() == b.size());
|
||||
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||
minus<double>());
|
||||
std::minus<double>());
|
||||
}
|
||||
|
||||
|
||||
@ -344,7 +355,7 @@ void operator*=(std::vector<T>& a, const std::vector<T>& b)
|
||||
{
|
||||
assert (a.size() == b.size());
|
||||
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||
multiplies<double>());
|
||||
std::multiplies<double>());
|
||||
}
|
||||
|
||||
|
||||
@ -354,7 +365,7 @@ void operator/=(std::vector<T>& a, const std::vector<T>& b)
|
||||
{
|
||||
assert (a.size() == b.size());
|
||||
std::transform (a.begin(), a.end(), b.begin(), a.begin(),
|
||||
divides<double>());
|
||||
std::divides<double>());
|
||||
}
|
||||
|
||||
|
||||
@ -363,7 +374,7 @@ template <typename T>
|
||||
void operator^=(std::vector<T>& v, double exp)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(),
|
||||
std::bind2nd (ptr_fun<double, double, double> (std::pow), exp));
|
||||
std::bind2nd (std::ptr_fun<double, double, double> (std::pow), exp));
|
||||
}
|
||||
|
||||
|
||||
@ -372,13 +383,13 @@ template <typename T>
|
||||
void operator^=(std::vector<T>& v, int iexp)
|
||||
{
|
||||
std::transform (v.begin(), v.end(), v.begin(),
|
||||
std::bind2nd (ptr_fun<double, int, double> (std::pow), iexp));
|
||||
std::bind2nd (std::ptr_fun<double, int, double> (std::pow), iexp));
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<< (std::ostream& os, const vector<T>& v)
|
||||
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v)
|
||||
{
|
||||
os << "[" ;
|
||||
os << Util::elementsToString (v, ", ");
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "Var.h"
|
||||
|
||||
|
||||
unordered_map<VarId, VarInfo> Var::varsInfo_;
|
||||
std::unordered_map<VarId, VarInfo> Var::varsInfo_;
|
||||
|
||||
|
||||
Var::Var (const Var* v)
|
||||
@ -45,13 +45,13 @@ Var::setEvidence (int evidence)
|
||||
|
||||
|
||||
|
||||
string
|
||||
std::string
|
||||
Var::label (void) const
|
||||
{
|
||||
if (Var::varsHaveInfo()) {
|
||||
return Var::getVarInfo (varId_).label;
|
||||
}
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "x" << varId_;
|
||||
return ss.str();
|
||||
}
|
||||
@ -66,7 +66,7 @@ Var::states (void) const
|
||||
}
|
||||
States states;
|
||||
for (unsigned i = 0; i < range_; i++) {
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << i ;
|
||||
states.push_back (ss.str());
|
||||
}
|
||||
@ -77,10 +77,10 @@ Var::states (void) const
|
||||
|
||||
inline void
|
||||
Var::addVarInfo (
|
||||
VarId vid, string label, const States& states)
|
||||
VarId vid, std::string label, const States& states)
|
||||
{
|
||||
assert (Util::contains (varsInfo_, vid) == false);
|
||||
varsInfo_.insert (make_pair (vid, VarInfo (label, states)));
|
||||
varsInfo_.insert (std::make_pair (vid, VarInfo (label, states)));
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,19 +7,15 @@
|
||||
#include "Horus.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct VarInfo
|
||||
{
|
||||
VarInfo (string l, const States& sts)
|
||||
VarInfo (std::string l, const States& sts)
|
||||
: label(l), states(sts) { }
|
||||
string label;
|
||||
std::string label;
|
||||
States states;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class Var
|
||||
{
|
||||
public:
|
||||
@ -51,12 +47,12 @@ class Var
|
||||
|
||||
void setEvidence (int);
|
||||
|
||||
string label (void) const;
|
||||
std::string label (void) const;
|
||||
|
||||
States states (void) const;
|
||||
|
||||
static void addVarInfo (
|
||||
VarId vid, string label, const States& states);
|
||||
VarId vid, std::string label, const States& states);
|
||||
|
||||
static VarInfo getVarInfo (VarId vid);
|
||||
|
||||
@ -70,7 +66,7 @@ class Var
|
||||
int evidence_;
|
||||
size_t index_;
|
||||
|
||||
static unordered_map<VarId, VarInfo> varsInfo_;
|
||||
static std::unordered_map<VarId, VarInfo> varsInfo_;
|
||||
};
|
||||
|
||||
|
||||
|
@ -10,12 +10,12 @@ Params
|
||||
VarElim::solveQuery (VarIds queryVids)
|
||||
{
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << "Solving query on " ;
|
||||
std::cout << "Solving query on " ;
|
||||
for (size_t i = 0; i < queryVids.size(); i++) {
|
||||
if (i != 0) cout << ", " ;
|
||||
cout << fg.getVarNode (queryVids[i])->label();
|
||||
if (i != 0) std::cout << ", " ;
|
||||
std::cout << fg.getVarNode (queryVids[i])->label();
|
||||
}
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
totalFactorSize_ = 0;
|
||||
largestFactorSize_ = 0;
|
||||
@ -35,7 +35,7 @@ VarElim::solveQuery (VarIds queryVids)
|
||||
void
|
||||
VarElim::printSolverFlags (void) const
|
||||
{
|
||||
stringstream ss;
|
||||
std::stringstream ss;
|
||||
ss << "variable elimination [" ;
|
||||
ss << "elim_heuristic=" ;
|
||||
switch (ElimGraph::elimHeuristic()) {
|
||||
@ -47,7 +47,7 @@ VarElim::printSolverFlags (void) const
|
||||
}
|
||||
ss << ",log_domain=" << Util::toString (Globals::logDomain);
|
||||
ss << "]" ;
|
||||
cout << ss.str() << endl;
|
||||
std::cout << ss.str() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -61,7 +61,7 @@ VarElim::createFactorList (void)
|
||||
factorList_.push_back (new Factor (facNodes[i]->factor()));
|
||||
const VarIds& args = facNodes[i]->factor().arguments();
|
||||
for (size_t j = 0; j < args.size(); j++) {
|
||||
unordered_map<VarId, vector<size_t>>::iterator it;
|
||||
std::unordered_map<VarId, std::vector<size_t>>::iterator it;
|
||||
it = varMap_.find (args[j]);
|
||||
if (it != varMap_.end()) {
|
||||
it->second.push_back (i);
|
||||
@ -79,18 +79,18 @@ VarElim::absorveEvidence (void)
|
||||
{
|
||||
if (Globals::verbosity > 2) {
|
||||
Util::printDashedLine();
|
||||
cout << "(initial factor list)" << endl;
|
||||
std::cout << "(initial factor list)" << std::endl;
|
||||
printActiveFactors();
|
||||
}
|
||||
const VarNodes& varNodes = fg.varNodes();
|
||||
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;
|
||||
std::cout << "-> aborving evidence on ";
|
||||
std::cout << varNodes[i]->label() << " = " ;
|
||||
std::cout << varNodes[i]->getEvidence() << std::endl;
|
||||
}
|
||||
const vector<size_t>& indices = varMap_[varNodes[i]->varId()];
|
||||
const std::vector<size_t>& indices = varMap_[varNodes[i]->varId()];
|
||||
for (size_t j = 0; j < indices.size(); j++) {
|
||||
size_t idx = indices[j];
|
||||
if (factorList_[idx]->nrArguments() > 1) {
|
||||
@ -118,8 +118,8 @@ VarElim::processFactorList (const VarIds& queryVids)
|
||||
Util::printDashedLine();
|
||||
printActiveFactors();
|
||||
}
|
||||
cout << "-> summing out " ;
|
||||
cout << fg.getVarNode (elimOrder[i])->label() << endl;
|
||||
std::cout << "-> summing out " ;
|
||||
std::cout << fg.getVarNode (elimOrder[i])->label() << std::endl;
|
||||
}
|
||||
eliminate (elimOrder[i]);
|
||||
}
|
||||
@ -143,9 +143,9 @@ VarElim::processFactorList (const VarIds& queryVids)
|
||||
result.reorderArguments (unobservedVids);
|
||||
result.normalize();
|
||||
if (Globals::verbosity > 0) {
|
||||
cout << "total factor size: " << totalFactorSize_ << endl;
|
||||
cout << "largest factor size: " << largestFactorSize_ << endl;
|
||||
cout << endl;
|
||||
std::cout << "total factor size: " << totalFactorSize_ << std::endl;
|
||||
std::cout << "largest factor size: " << largestFactorSize_ << std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
return result.params();
|
||||
}
|
||||
@ -156,7 +156,7 @@ void
|
||||
VarElim::eliminate (VarId vid)
|
||||
{
|
||||
Factor* result = new Factor();
|
||||
const vector<size_t>& indices = varMap_[vid];
|
||||
const std::vector<size_t>& indices = varMap_[vid];
|
||||
for (size_t i = 0; i < indices.size(); i++) {
|
||||
size_t idx = indices[i];
|
||||
if (factorList_[idx]) {
|
||||
@ -173,7 +173,7 @@ VarElim::eliminate (VarId vid)
|
||||
result->sumOut (vid);
|
||||
const VarIds& args = result->arguments();
|
||||
for (size_t i = 0; i < args.size(); i++) {
|
||||
vector<size_t>& indices2 = varMap_[args[i]];
|
||||
std::vector<size_t>& indices2 = varMap_[args[i]];
|
||||
indices2.push_back (factorList_.size());
|
||||
}
|
||||
factorList_.push_back (result);
|
||||
@ -189,9 +189,9 @@ VarElim::printActiveFactors (void)
|
||||
{
|
||||
for (size_t i = 0; i < factorList_.size(); i++) {
|
||||
if (factorList_[i]) {
|
||||
cout << factorList_[i]->getLabel() << " " ;
|
||||
cout << factorList_[i]->params();
|
||||
cout << endl;
|
||||
std::cout << factorList_[i]->getLabel() << " " ;
|
||||
std::cout << factorList_[i]->params();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,6 @@
|
||||
#include "Horus.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class VarElim : public GroundSolver
|
||||
{
|
||||
public:
|
||||
@ -36,7 +33,7 @@ class VarElim : public GroundSolver
|
||||
Factors factorList_;
|
||||
unsigned largestFactorSize_;
|
||||
unsigned totalFactorSize_;
|
||||
unordered_map<VarId, vector<size_t>> varMap_;
|
||||
std::unordered_map<VarId, std::vector<size_t>> varMap_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN (VarElim);
|
||||
};
|
||||
|
@ -49,6 +49,8 @@ WeightedBp::getPosterioriOf (VarId vid)
|
||||
void
|
||||
WeightedBp::createLinks (void)
|
||||
{
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
if (Globals::verbosity > 0) {
|
||||
cout << "compressed factor graph contains " ;
|
||||
cout << fg.nrVarNodes() << " variables and " ;
|
||||
@ -86,7 +88,7 @@ WeightedBp::maxResidualSchedule (void)
|
||||
SortedOrder::iterator it = sortedOrder_.insert (links_[i]);
|
||||
linkMap_.insert (make_pair (links_[i], it));
|
||||
if (Globals::verbosity >= 1) {
|
||||
cout << "calculating " << links_[i]->toString() << endl;
|
||||
std::cout << "calculating " << links_[i]->toString() << std::endl;
|
||||
}
|
||||
}
|
||||
return;
|
||||
@ -94,18 +96,20 @@ WeightedBp::maxResidualSchedule (void)
|
||||
|
||||
for (size_t c = 0; c < links_.size(); c++) {
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << endl << "current residuals:" << endl;
|
||||
std::cout << std::endl << "current residuals:" << std::endl;
|
||||
for (SortedOrder::iterator it = sortedOrder_.begin();
|
||||
it != sortedOrder_.end(); ++it) {
|
||||
cout << " " << setw (30) << left << (*it)->toString();
|
||||
cout << "residual = " << (*it)->residual() << endl;
|
||||
std::cout << " " << std::setw (30) << std::left;
|
||||
std::cout << (*it)->toString();
|
||||
std::cout << "residual = " << (*it)->residual() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
SortedOrder::iterator it = sortedOrder_.begin();
|
||||
BpLink* link = *it;
|
||||
if (Globals::verbosity >= 1) {
|
||||
cout << "updating " << (*sortedOrder_.begin())->toString() << endl;
|
||||
std::cout << "updating " << (*sortedOrder_.begin())->toString();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (link->residual() < accuracy_) {
|
||||
return;
|
||||
@ -122,7 +126,8 @@ WeightedBp::maxResidualSchedule (void)
|
||||
for (size_t j = 0; j < links.size(); j++) {
|
||||
if (links[j]->varNode() != link->varNode()) {
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << " calculating " << links[j]->toString() << endl;
|
||||
std::cout << " calculating " << links[j]->toString();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
calculateMessage (links[j]);
|
||||
BpLinkMap::iterator iter = linkMap_.find (links[j]);
|
||||
@ -137,7 +142,8 @@ WeightedBp::maxResidualSchedule (void)
|
||||
for (size_t i = 0; i < links.size(); i++) {
|
||||
if (links[i]->varNode() != link->varNode()) {
|
||||
if (Globals::verbosity > 1) {
|
||||
cout << " calculating " << links[i]->toString() << endl;
|
||||
std::cout << " calculating " << links[i]->toString();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
calculateMessage (links[i]);
|
||||
BpLinkMap::iterator iter = linkMap_.find (links[i]);
|
||||
@ -167,13 +173,13 @@ WeightedBp::calcFactorToVarMsg (BpLink* _link)
|
||||
const WeightedLink* l = static_cast<const WeightedLink*> (links[i]);
|
||||
if ( ! (l->varNode() == dst && l->index() == link->index())) {
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " message from " << links[i]->varNode()->label();
|
||||
cout << ": " ;
|
||||
std::cout << " message from " << links[i]->varNode()->label();
|
||||
std::cout << ": " ;
|
||||
}
|
||||
Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
|
||||
reps, std::plus<double>());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
reps *= links[i]->varNode()->range();
|
||||
@ -183,13 +189,13 @@ WeightedBp::calcFactorToVarMsg (BpLink* _link)
|
||||
const WeightedLink* l = static_cast<const WeightedLink*> (links[i]);
|
||||
if ( ! (l->varNode() == dst && l->index() == link->index())) {
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " message from " << links[i]->varNode()->label();
|
||||
cout << ": " ;
|
||||
std::cout << " message from " << links[i]->varNode()->label();
|
||||
std::cout << ": " ;
|
||||
}
|
||||
Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
|
||||
reps, std::multiplies<double>());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
reps *= links[i]->varNode()->range();
|
||||
@ -204,19 +210,25 @@ WeightedBp::calcFactorToVarMsg (BpLink* _link)
|
||||
result.params() *= src->factor().params();
|
||||
}
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " message product: " << msgProduct << endl;
|
||||
cout << " original factor: " << src->factor().params() << endl;
|
||||
cout << " factor product: " << result.params() << endl;
|
||||
std::cout << " message product: " ;
|
||||
std::cout << msgProduct << std::endl;
|
||||
std::cout << " original factor: " ;
|
||||
std::cout << src->factor().params() << std::endl;
|
||||
std::cout << " factor product: " ;
|
||||
std::cout << result.params() << std::endl;
|
||||
}
|
||||
result.sumOutAllExceptIndex (link->index());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " marginalized: " << result.params() << endl;
|
||||
std::cout << " marginalized: " ;
|
||||
std::cout << result.params() << std::endl;
|
||||
}
|
||||
link->nextMessage() = result.params();
|
||||
LogAware::normalize (link->nextMessage());
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " curr msg: " << link->message() << endl;
|
||||
cout << " next msg: " << link->nextMessage() << endl;
|
||||
std::cout << " curr msg: " ;
|
||||
std::cout << link->message() << std::endl;
|
||||
std::cout << " next msg: " ;
|
||||
std::cout << link->nextMessage() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -234,13 +246,13 @@ WeightedBp::getVarToFactorMsg (const BpLink* _link) const
|
||||
double value = link->message()[src->getEvidence()];
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
msg[src->getEvidence()] = value;
|
||||
cout << msg << "^" << link->weight() << "-1" ;
|
||||
std::cout << msg << "^" << link->weight() << "-1" ;
|
||||
}
|
||||
msg[src->getEvidence()] = LogAware::pow (value, link->weight() - 1);
|
||||
} else {
|
||||
msg = link->message();
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << msg << "^" << link->weight() << "-1" ;
|
||||
std::cout << msg << "^" << link->weight() << "-1" ;
|
||||
}
|
||||
LogAware::pow (msg, link->weight() - 1);
|
||||
}
|
||||
@ -258,13 +270,13 @@ WeightedBp::getVarToFactorMsg (const BpLink* _link) const
|
||||
if ( ! (l->facNode() == dst && l->index() == link->index())) {
|
||||
msg *= l->powMessage();
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " x " << l->nextMessage() << "^" << link->weight();
|
||||
std::cout << " x " << l->nextMessage() << "^" << link->weight();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Constants::SHOW_BP_CALCS) {
|
||||
cout << " = " << msg;
|
||||
std::cout << " = " << msg;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
@ -274,6 +286,8 @@ WeightedBp::getVarToFactorMsg (const BpLink* _link) const
|
||||
void
|
||||
WeightedBp::printLinkInformation (void) const
|
||||
{
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
for (size_t i = 0; i < links_.size(); i++) {
|
||||
WeightedLink* l = static_cast<WeightedLink*> (links_[i]);
|
||||
cout << l->toString() << ":" << endl;
|
||||
|
@ -43,7 +43,7 @@ class WeightedBp : public BeliefProp
|
||||
{
|
||||
public:
|
||||
WeightedBp (const FactorGraph& fg,
|
||||
const vector<vector<unsigned>>& weights)
|
||||
const std::vector<std::vector<unsigned>>& weights)
|
||||
: BeliefProp (fg), weights_(weights) { }
|
||||
|
||||
~WeightedBp (void);
|
||||
@ -61,7 +61,7 @@ class WeightedBp : public BeliefProp
|
||||
|
||||
void printLinkInformation (void) const;
|
||||
|
||||
vector<vector<unsigned>> weights_;
|
||||
std::vector<std::vector<unsigned>> weights_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN (WeightedBp);
|
||||
};
|
||||
|
Reference in New Issue
Block a user