This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/clpbn/bp/LiftedUtils.h

156 lines
2.9 KiB
C
Raw Normal View History

2012-03-22 11:33:24 +00:00
#ifndef HORUS_LIFTEDUTILS_H
#define HORUS_LIFTEDUTILS_H
#include <limits>
#include <string>
#include <vector>
#include <unordered_map>
#include "TinySet.h"
#include "Util.h"
using namespace std;
class Symbol
{
public:
Symbol (void) : id_(numeric_limits<unsigned>::max()) { }
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
Symbol (unsigned id) : id_(id) { }
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
operator unsigned (void) const { return id_; }
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
bool valid (void) const { return id_ != numeric_limits<unsigned>::max(); }
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
static Symbol invalid (void) { return Symbol(); }
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
friend ostream& operator<< (ostream &os, const Symbol& s);
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
private:
unsigned id_;
};
class LogVar
{
public:
LogVar (void) : id_(numeric_limits<unsigned>::max()) { }
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
LogVar (unsigned id) : id_(id) { }
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
operator unsigned (void) const { return id_; }
LogVar& operator++ (void)
{
assert (valid());
id_ ++;
return *this;
}
bool valid (void) const
{
return id_ != numeric_limits<unsigned>::max();
}
friend ostream& operator<< (ostream &os, const LogVar& X);
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
private:
unsigned id_;
};
namespace std {
template <> struct hash<Symbol> {
size_t operator() (const Symbol& s) const {
return std::hash<unsigned>() (s);
}};
template <> struct hash<LogVar> {
size_t operator() (const LogVar& X) const {
return std::hash<unsigned>() (X);
}};
};
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;
ostream& operator<< (ostream &os, const Tuple& t);
namespace LiftedUtils {
2012-03-31 23:27:37 +01:00
Symbol getSymbol (const string&);
void printSymbolDictionary (void);
2012-03-22 11:33:24 +00:00
}
class Ground
{
public:
Ground (Symbol f) : functor_(f) { }
2012-03-31 23:27:37 +01:00
Ground (Symbol f, const Symbols& args) : functor_(f), args_(args) { }
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
Symbol functor (void) const { return functor_; }
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
Symbols args (void) const { return args_; }
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
unsigned arity (void) const { return args_.size(); }
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
bool isAtom (void) const { return args_.size() == 0; }
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
friend ostream& operator<< (ostream &os, const Ground& gr);
2012-03-22 11:33:24 +00:00
private:
2012-03-31 23:27:37 +01:00
Symbol functor_;
Symbols args_;
2012-03-22 11:33:24 +00:00
};
2012-03-31 23:27:37 +01:00
typedef vector<Ground> Grounds;
2012-03-22 11:33:24 +00:00
class Substitution
{
public:
void add (LogVar X_old, LogVar X_new)
{
2012-03-31 23:27:37 +01:00
assert (Util::contains (subs_, X_old) == false);
2012-03-22 11:33:24 +00:00
subs_.insert (make_pair (X_old, X_new));
}
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
void rename (LogVar X_old, LogVar X_new)
{
2012-03-31 23:27:37 +01:00
assert (Util::contains (subs_, X_old));
2012-03-22 11:33:24 +00:00
subs_.find (X_old)->second = X_new;
}
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
LogVar newNameFor (LogVar X) const
{
2012-03-31 23:27:37 +01:00
assert (Util::contains (subs_, X));
2012-03-22 11:33:24 +00:00
return subs_.find (X)->second;
}
2012-03-31 23:27:37 +01:00
2012-04-03 11:58:21 +01:00
LogVars getDiscardedLogVars (void) const;
2012-03-31 23:27:37 +01:00
friend ostream& operator<< (ostream &os, const Substitution& theta);
2012-03-22 11:33:24 +00:00
private:
unordered_map<LogVar, LogVar> subs_;
2012-04-03 11:58:21 +01:00
2012-03-22 11:33:24 +00:00
};
#endif // HORUS_LIFTEDUTILS_H