virtual InfAlg* clone() const = 0;
/// Returns a pointer to a newly constructed object *this (i.e., virtual default constructor)
- virtual InfAlg* create() const = 0;
+ virtual InfAlg* create() const { DAI_THROW(NOT_IMPLEMENTED); }
/// Identifies itself for logging purposes
virtual std::string identify() const = 0;
/// Returns the "belief" (i.e., approximate marginal probability distribution) of a set of variables
virtual Factor belief( const VarSet &n ) const = 0;
+ /// Returns marginal for a variable.
+ /** Sometimes preferred to belief() for performance reasons.
+ * Faster implementations exist in e.g. BP.
+ */
+ virtual Factor beliefV( size_t i ) const { return belief( fg().var(i) ); }
+
+ /// Returns marginal for a factor.
+ /** Sometimes preferred to belief() for performance reasons.
+ * Faster implementations exist in e.g. BP.
+ */
+ virtual Factor beliefF( size_t I ) const { return belief( fg().factor(I).vars() ); }
+
/// Returns all "beliefs" (i.e., approximate marginal probability distribution) calculated by the algorithm
virtual std::vector<Factor> beliefs() const = 0;
/// Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue)
class PropertySet : private std::map<PropertyKey, PropertyValue> {
public:
+ /// Default constructor
+ PropertySet() {}
+
+ /// Construct PropertySet from a string
+ PropertySet( const std::string &s ) {
+ std::stringstream ss;
+ ss << s;
+ ss >> *this;
+ }
+
/// Gets a property
const PropertyValue & Get(const PropertyKey &key) const {
PropertySet::const_iterator x = find(key);
template<typename ValueType>
ValueType getStringAs(const PropertyKey &key) const {
PropertyValue val = Get(key);
- if( val.type() == typeid(std::string) ) {
+ if( val.type() == typeid(ValueType) ) {
+ return boost::any_cast<ValueType>(val);
+ } else if( val.type() == typeid(std::string) ) {
std::stringstream ss;
ss << GetAs<std::string>(key);
ValueType result;
ss >> result;
return result;
- } else if( val.type() == typeid(ValueType) ) {
- return boost::any_cast<ValueType>(val);
} else {
DAI_THROW(IMPOSSIBLE_TYPECAST);
return ValueType();
/// Check if a property with the given key exists
bool hasKey(const PropertyKey &key) const { PropertySet::const_iterator x = find(key); return (x != this->end()); }
+ /// Returns a set containing all keys
+ std::set<PropertyKey> allKeys() const {
+ std::set<PropertyKey> res;
+ const_iterator i;
+ for( i = begin(); i != end(); i++ )
+ res.insert( i->first );
+ return res;
+ }
+
// Friends
friend std::ostream& operator<< (std::ostream & os, const PropertySet & ps);
friend std::istream& operator>> (std::istream& is, PropertySet & ps);
#define foreach BOOST_FOREACH
#ifdef DAI_DEBUG
-#define DAI_PV(x) do {std::cerr << #x "= " << x << std::endl;} while(0)
+/// \brief "Print variable". Prints the text of an expression, followed by its value (only if DAI_DEBUG is defined)
+/**
+ * Useful debugging macro to see what your code is doing.
+ * Example: \code DAI_PV(3+4) \endcode
+ */ Output: \code 3+4= 7 \endcode
+#define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
+/// "Debugging message": Prints a message (only if DAI_DEBUG is defined)
#define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
#else
#define DAI_PV(x) do {} while(0)
#define DAI_DMSG(str) do {} while(0)
#endif
+/// Produces accessor and mutator methods according to the common pattern.
+/** Example:
+ * \code DAI_ACCMUT(size_t& maxIter(), { return props.maxiter; }); \endcode
+ * \todo At the moment, only the mutator appears in doxygen documentation.
#define DAI_ACCMUT(x,y) \
x y; \
const x const y;
+/// Macro to give error message \a stmt if props.verbose>=\a n
#define DAI_IFVERB(n, stmt) if(props.verbose>=n) { cerr << stmt; }
return os;
}
+/// Concatenate two vectors
+template<class T>
+std::vector<T> concata (const std::vector<T>& u, const std::vector<T>& v ) {
+ std::vector<T> w;
+ w.reserve( u.size() + v.size() );
+ for( size_t i = 0; i < u.size(); i++ )
+ w.push_back( u[i] );
+ for( size_t i = 0; i < v.size(); i++ )
+ w.push_back( v[i] );
+ return w;
+}
/// Used to keep track of the progress made by iterative algorithms
class Diffs : public std::vector<double> {