+* Replaced the standard assert() macro by DAI_ASSERT, which throws a
+ dai::Exception and is even active if NDEBUG is defined
* Added a constructor TProb<T>::TProb<T>( const std::vector<S> &v )
* Introduced CCNODEBUGFLAGS in Makefile
* Updated copyright headers
// Iterate over all neighbors n2 of n1
foreach( const BipartiteGraph::Neighbor &n2, G.nb1(n1) ) {
// The n2.iter'th neighbor of n1 is n2:
- assert( G.nb1(n1)[n2.iter] == n2 );
+ DAI_ASSERT( G.nb1(n1)[n2.iter] == n2 );
// The n2.dual'th neighbor of n2 is n1:
- assert( G.nb2(n2)[n2.dual] == n1 );
+ DAI_ASSERT( G.nb2(n2)[n2.dual] == n1 );
// n2 can be used as an abbreviation of n2.node:
- assert( static_cast<size_t>(n2) == n2.node );
+ DAI_ASSERT( static_cast<size_t>(n2) == n2.node );
cout << " the " << n2.iter << "'th neighbor is node " << n2 << " of type 2" << endl;
}
cout << " " << s0 << " " << s1 << " " << X.calcState(states) << endl;
// VarSet::calcStates is the inverse of VarSet::calcState
- assert( X.calcStates(X.calcState(states)) == states );
+ DAI_ASSERT( X.calcStates(X.calcState(states)) == states );
}
cout << endl << "And vice versa:" << endl;
cout << " " << states[x0] << " " << states[x1] << " " << S << endl;
// VarSet::calcState is the inverse of VarSet::calcStates
- assert( X.calcState(X.calcStates(S)) == S );
+ DAI_ASSERT( X.calcState(X.calcStates(S)) == S );
}
return 0;
#include <ostream>
#include <vector>
-#include <cassert>
#include <algorithm>
#include <dai/util.h>
+#include <dai/exceptions.h>
namespace dai {
nbs1new.reserve( sizeHint );
size_t iter = 0;
for( NodeInputIterator it = begin; it != end; ++it ) {
- assert( *it < nr2() );
+ DAI_ASSERT( *it < nr2() );
Neighbor nb1new( iter, *it, nb2(*it).size() );
Neighbor nb2new( nb2(*it).size(), nr1(), iter++ );
nbs1new.push_back( nb1new );
nbs2new.reserve( sizeHint );
size_t iter = 0;
for( NodeInputIterator it = begin; it != end; ++it ) {
- assert( *it < nr1() );
+ DAI_ASSERT( *it < nr1() );
Neighbor nb2new( iter, *it, nb1(*it).size() );
Neighbor nb1new( nb1(*it).size(), nr2(), iter++ );
nbs2new.push_back( nb2new );
/// Removes edge between node n1 of type 1 and node n2 of type 2.
void eraseEdge( size_t n1, size_t n2 ) {
- assert( n1 < nr1() );
- assert( n2 < nr2() );
+ DAI_ASSERT( n1 < nr1() );
+ DAI_ASSERT( n2 < nr2() );
for( Neighbors::iterator i1 = _nb1[n1].begin(); i1 != _nb1[n1].end(); i1++ )
if( i1->node == n2 ) {
_nb1[n1].erase( i1 );
/** If check == true, only adds the edge if it does not exist already.
*/
void addEdge( size_t n1, size_t n2, bool check = true ) {
- assert( n1 < nr1() );
- assert( n2 < nr2() );
+ DAI_ASSERT( n1 < nr1() );
+ DAI_ASSERT( n2 < nr2() );
bool exists = false;
if( check ) {
// Check whether the edge already exists
}
const Edge& edge(size_t e) const {
- assert(_edge_indexed);
+ DAI_ASSERT(_edge_indexed);
return _edges[e];
}
}
size_t VV2E(size_t n1, size_t n2) const {
- assert(_edge_indexed);
+ DAI_ASSERT(_edge_indexed);
Edge e(n1,n2);
hash_map<Edge,size_t>::const_iterator i = _vv2e.find(e);
- assert(i != _vv2e.end());
+ DAI_ASSERT(i != _vv2e.end());
return i->second;
}
size_t nr_edges() const {
- assert(_edge_indexed);
+ DAI_ASSERT(_edge_indexed);
return _edges.size();
}
//}@
*/
#define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
+/// Assertion mechanism, similar to the standard assert() macro, but is always active, even if NDEBUG is defined
+#define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, #condition))
+
+// Assertion only if DAI_DEBUG is defined
+#ifdef DAI_DEBUG
+/// Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined
+#define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
+#else
+#define DAI_DEBASSERT(X) do {} while(0)
+#endif
+
namespace dai {
class Exception : public std::runtime_error {
public:
/// Enumeration of exceptions used in libDAI
- enum Code {NOT_IMPLEMENTED,
+ enum Code {ASSERTION_FAILED,
+ NOT_IMPLEMENTED,
UNKNOWN_DAI_ALGORITHM,
UNKNOWN_PROPERTY_TYPE,
MALFORMED_PROPERTY,
* defined by \f$g(\{x_l\}_{l\in L \setminus M}) = f(\{x_l\}_{l\in L \setminus M}, \{s(x_m)\}_{m\in M})\f$.
*/
TFactor<T> slice( const VarSet& ns, size_t nsState ) const {
- assert( ns << _vs );
+ DAI_ASSERT( ns << _vs );
VarSet nsrem = _vs / ns;
TFactor<T> result( nsrem, T(0) );
* the embedded factor corresponds with \f$g : \prod_{m\in M} X_m \to [0,\infty) : x \mapsto f(x_L)\f$.
*/
TFactor<T> embed(const VarSet & ns) const {
- assert( ns >> _vs );
+ DAI_ASSERT( ns >> _vs );
if( _vs == ns )
return *this;
else
* \pre f.vars() == g.vars()
*/
template<typename T> TFactor<T> max( const TFactor<T> &f, const TFactor<T> &g ) {
- assert( f._vs == g._vs );
+ DAI_ASSERT( f._vs == g._vs );
return TFactor<T>( f._vs, min( f.p(), g.p() ) );
}
* \pre f.vars() == g.vars()
*/
template<typename T> TFactor<T> min( const TFactor<T> &f, const TFactor<T> &g ) {
- assert( f._vs == g._vs );
+ DAI_ASSERT( f._vs == g._vs );
return TFactor<T>( f._vs, max( f.p(), g.p() ) );
}
* \pre f.vars().size() == 2
*/
template<typename T> Real MutualInfo(const TFactor<T> &f) {
- assert( f.vars().size() == 2 );
+ DAI_ASSERT( f.vars().size() == 2 );
VarSet::const_iterator it = f.vars().begin();
Var i = *it; it++; Var j = *it;
TFactor<T> projection = f.marginal(i) * f.marginal(j);
/// Set the content of the I'th factor and make a backup of its old content if backup == true
virtual void setFactor( size_t I, const Factor &newFactor, bool backup = false ) {
- assert( newFactor.vars() == factor(I).vars() );
+ DAI_ASSERT( newFactor.vars() == factor(I).vars() );
if( backup )
backupFactor( I );
_factors[I] = newFactor;
#include <vector>
#include <algorithm>
#include <map>
-#include <cassert>
#include <dai/varset.h>
/// Return linear state
operator size_t() const {
- assert( valid() );
+ DAI_ASSERT( valid() );
return( _state );
}
/// Return k'th index
size_t operator[]( size_t k ) const {
- assert( valid() );
- assert( k < _states.size() );
+ DAI_ASSERT( valid() );
+ DAI_ASSERT( k < _states.size() );
return _states[k];
}
/// Construct from vector of index dimensions and permutation sigma
Permute( const std::vector<size_t> &d, const std::vector<size_t> &sigma ) : _dims(d), _sigma(sigma) {
- assert( _dims.size() == _sigma.size() );
+ DAI_ASSERT( _dims.size() == _sigma.size() );
}
/// Construct from vector of variables
/// Return linear state
operator size_t() const {
- assert( valid() );
+ DAI_ASSERT( valid() );
return( state );
}
/// Return state of variable n, or zero if n is not in this State
size_t operator() ( const Var &n ) const {
- assert( valid() );
+ DAI_ASSERT( valid() );
states_type::const_iterator entry = states.find( n );
if( entry == states.end() )
return 0;
/// Return linear state of variables in varset, setting them to zero if they are not in this State
size_t operator() ( const VarSet &vs ) const {
- assert( valid() );
+ DAI_ASSERT( valid() );
size_t vs_state = 0;
size_t prod = 1;
for( VarSet::const_iterator v = vs.begin(); v != vs.end(); v++ ) {
#include <cmath>
#include <vector>
#include <ostream>
-#include <cassert>
#include <algorithm>
#include <numeric>
#include <functional>
/** \relates TProb
*/
template<typename T> TProb<T> min( const TProb<T> &a, const TProb<T> &b ) {
- assert( a.size() == b.size() );
+ DAI_ASSERT( a.size() == b.size() );
TProb<T> result( a.size() );
for( size_t i = 0; i < a.size(); i++ )
if( a[i] < b[i] )
/** \relates TProb
*/
template<typename T> TProb<T> max( const TProb<T> &a, const TProb<T> &b ) {
- assert( a.size() == b.size() );
+ DAI_ASSERT( a.size() == b.size() );
TProb<T> result( a.size() );
for( size_t i = 0; i < a.size(); i++ )
if( a[i] > b[i] )
#include <boost/any.hpp>
#include <map>
#include <vector>
-#include <cassert>
#include <typeinfo>
#include <dai/exceptions.h>
#include <dai/util.h>
void ConvertTo(const PropertyKey &key) {
PropertyValue val = Get(key);
if( val.type() != typeid(ValueType) ) {
- assert( val.type() == typeid(std::string) );
+ DAI_ASSERT( val.type() == typeid(std::string) );
try {
Set(key, boost::lexical_cast<ValueType>(GetAs<std::string>(key)));
} catch(boost::bad_lexical_cast &) {
#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)
-/// Assertion if DAI_DEBUG is defined
-#define DAI_DEBASSERT(x) do {assert(x);} while(0)
#else
#define DAI_PV(x) do {} while(0)
#define DAI_DMSG(str) do {} while(0)
-#define DAI_DEBASSERT(X) do {} while(0)
#endif
/// Produces accessor and mutator methods according to the common pattern.
#include <iostream>
-#include <cassert>
+#include <dai/exceptions.h>
namespace dai {
bool operator <= ( const Var& n ) const {
#ifdef DAI_DEBUG
if( _label == n._label )
- assert( _states == n._states );
+ DAI_ASSERT( _states == n._states );
#endif
return( _label <= n._label );
}
bool operator >= ( const Var& n ) const {
#ifdef DAI_DEBUG
if( _label == n._label )
- assert( _states == n._states );
+ DAI_ASSERT( _states == n._states );
#endif
return( _label >= n._label );
}
bool operator != ( const Var& n ) const {
#ifdef DAI_DEBUG
if( _label == n._label )
- assert( _states == n._states );
+ DAI_ASSERT( _states == n._states );
#endif
return( _label != n._label );
}
bool operator == ( const Var& n ) const {
#ifdef DAI_DEBUG
if( _label == n._label )
- assert( _states == n._states );
+ DAI_ASSERT( _states == n._states );
#endif
return( _label == n._label );
}
#include <vector>
#include <map>
-#include <cassert>
#include <ostream>
#include <dai/var.h>
#include <dai/util.h>
states[*n] = linearState % n->states();
linearState /= n->states();
}
- assert( linearState == 0 );
+ DAI_ASSERT( linearState == 0 );
return states;
}
#include <map>
#include <iostream>
#include <set>
-#include <cassert>
#include <limits>
#include <climits> // Work-around for bug in boost graph library
Prob unnormAdjoint( const Prob &w, Real Z_w, const Prob &adj_w ) {
- assert( w.size() == adj_w.size() );
+ DAI_ASSERT( w.size() == adj_w.size() );
Prob adj_w_unnorm( w.size(), 0.0 );
Real s = 0.0;
for( size_t i = 0; i < w.size(); i++ )
_adj_psi_V.reserve( _fg->nrVars() );
for( size_t i = 0; i < _fg->nrVars(); i++ ) {
Prob p( _adj_b_V_unnorm[i] );
- assert( p.size() == _fg->var(i).states() );
+ DAI_ASSERT( p.size() == _fg->var(i).states() );
foreach( const Neighbor &I, _fg->nbV(i) )
p *= _bp_dual.msgM( i, I.iter );
p += _init_adj_psi_V[i];
_adj_psi_F.reserve( _fg->nrFactors() );
for( size_t I = 0; I < _fg->nrFactors(); I++ ) {
Prob p( _adj_b_F_unnorm[I] );
- assert( p.size() == _fg->factor(I).states() );
+ DAI_ASSERT( p.size() == _fg->factor(I).states() );
foreach( const Neighbor &i, _fg->nbF(I) ) {
Prob n_iI( _bp_dual.msgN( i, i.dual ) );
const _ind_t& ind = _index( i, i.dual );
{ // calculate adj_m
Prob prod( _adj_b_V_unnorm[i] );
- assert( prod.size() == _fg->var(i).states() );
+ DAI_ASSERT( prod.size() == _fg->var(i).states() );
foreach( const Neighbor &J, _fg->nbV(i) )
if( J.node != I.node )
prod *= _bp_dual.msgM(i,J.iter);
foreach( const Neighbor &I, _fg->nbV(i) ) {
// calculate adj_m
Prob prod( _adj_b_V_unnorm[i] );
- assert( prod.size() == _fg->var(i).states() );
+ DAI_ASSERT( prod.size() == _fg->var(i).states() );
foreach( const Neighbor &J, _fg->nbV(i) )
if( J.node != I.node )
prod *= _bp_dual.msgM( i, J.iter );
void BBP::sendSeqMsgN( size_t i, size_t _I, const Prob &f ) {
Prob f_unnorm = unnormAdjoint( _bp_dual.msgN(i,_I), _bp_dual.zN(i,_I), f );
const Neighbor &I = _fg->nbV(i)[_I];
- assert( I.iter == _I );
+ DAI_ASSERT( I.iter == _I );
_adj_psi_V[i] += f_unnorm * T( i, _I );
#if 0
if(f_unnorm.sumAbs() > pv_thresh) {
DAI_PV(_fg->nbV(i).size());
}
#endif
- assert( _fg->nbV(i)[i.dual].node == I );
+ DAI_ASSERT( _fg->nbV(i)[i.dual].node == I );
sendSeqMsgN( i, i.dual, msg );
}
}
// argmax_var_state = argmax_state.first;
// }
// }
-// assert(/*0 <= argmax_var_state &&*/
+// DAI_ASSERT(/*0 <= argmax_var_state &&*/
// argmax_var_state < _fg->var(argmax_var).states());
// return tuple<size_t,size_t,Real>(argmax_var,argmax_var_state,max_var);
// }
out__I = I.iter;
}
}
- assert( found );
+ DAI_ASSERT( found );
}
state = getGibbsState( ia, 2*ia.Iterations() );
else
state = *stateP;
- assert( state.size() == fg.nrVars() );
+ DAI_ASSERT( state.size() == fg.nrVars() );
vector<Prob> b1_adj;
b1_adj.reserve(fg.nrVars());
for( size_t i = 0; i < state.size(); i++ ) {
size_t n = fg.var(i).states();
Prob delta( n, 0.0 );
- assert(/*0<=state[i] &&*/ state[i] < n);
+ DAI_ASSERT(/*0<=state[i] &&*/ state[i] < n);
double b = ia.beliefV(i)[state[i]];
switch( (size_t)cfn_type ) {
case bbp_cfn_t::CFN_GIBBS_B:
state = getGibbsState( ia, 2*ia.Iterations() );
else
state = *stateP;
- assert( state.size() == fg.nrVars() );
+ DAI_ASSERT( state.size() == fg.nrVars() );
vector<Prob> b2_adj;
b2_adj.reserve( fg.nrVars() );
Prob delta( n, 0.0 );
size_t x_I = getFactorEntryForState( fg, I, state );
- assert(/*0<=x_I &&*/ x_I < n);
+ DAI_ASSERT(/*0<=x_I &&*/ x_I < n);
double b = ia.beliefF(I)[x_I];
switch( (size_t)cfn_type ) {
case bbp_cfn_t::CFN_GIBBS_B:
case bbp_cfn_t::CFN_GIBBS_B2:
case bbp_cfn_t::CFN_GIBBS_EXP: {
- assert( stateP != NULL );
+ DAI_ASSERT( stateP != NULL );
vector<size_t> state = *stateP;
- assert( state.size() == fg.nrVars() );
+ DAI_ASSERT( state.size() == fg.nrVars() );
for( size_t i = 0; i < fg.nrVars(); i++ ) {
double b = ia.beliefV(i)[state[i]];
switch( (size_t)cfn_type ) {
} case bbp_cfn_t::CFN_GIBBS_B_FACTOR:
case bbp_cfn_t::CFN_GIBBS_B2_FACTOR:
case bbp_cfn_t::CFN_GIBBS_EXP_FACTOR: {
- assert( stateP != NULL );
+ DAI_ASSERT( stateP != NULL );
vector<size_t> state = *stateP;
- assert( state.size() == fg.nrVars() );
+ DAI_ASSERT( state.size() == fg.nrVars() );
for( size_t I = 0; I < fg.nrFactors(); I++ ) {
size_t x_I = getFactorEntryForState( fg, I, state );
double b = ia.beliefF(I)[x_I];
void BipartiteGraph::erase1( size_t n1 ) {
- assert( n1 < nr1() );
+ DAI_ASSERT( n1 < nr1() );
// Erase neighbor entry of node n1
_nb1.erase( _nb1.begin() + n1 );
// Adjust neighbor entries of nodes of type 2
void BipartiteGraph::erase2( size_t n2 ) {
- assert( n2 < nr2() );
+ DAI_ASSERT( n2 < nr2() );
// Erase neighbor entry of node n2
_nb2.erase( _nb2.begin() + n2 );
// Adjust neighbor entries of nodes of type 1
for( size_t n1 = 0; n1 < N1; n1++ ) {
size_t iter = 0;
foreach( const Neighbor &n2, nb1(n1) ) {
- assert( n2.iter == iter );
- assert( n2.node < N2 );
- assert( n2.dual < nb2(n2).size() );
- assert( nb2(n2, n2.dual) == n1 );
+ DAI_ASSERT( n2.iter == iter );
+ DAI_ASSERT( n2.node < N2 );
+ DAI_ASSERT( n2.dual < nb2(n2).size() );
+ DAI_ASSERT( nb2(n2, n2.dual) == n1 );
iter++;
}
}
for( size_t n2 = 0; n2 < N2; n2++ ) {
size_t iter = 0;
foreach( const Neighbor &n1, nb2(n2) ) {
- assert( n1.iter == iter );
- assert( n1.node < N1 );
- assert( n1.dual < nb1(n1).size() );
- assert( nb1(n1, n1.dual) == n2 );
+ DAI_ASSERT( n1.iter == iter );
+ DAI_ASSERT( n1.node < N1 );
+ DAI_ASSERT( n1.dual < nb1(n1).size() );
+ DAI_ASSERT( nb1(n1, n1.dual) == n2 );
iter++;
}
}
void BP::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("tol") );
- assert( opts.hasKey("maxiter") );
- assert( opts.hasKey("logdomain") );
- assert( opts.hasKey("updates") );
+ DAI_ASSERT( opts.hasKey("tol") );
+ DAI_ASSERT( opts.hasKey("maxiter") );
+ DAI_ASSERT( opts.hasKey("logdomain") );
+ DAI_ASSERT( opts.hasKey("updates") );
props.tol = opts.getStringAs<double>("tol");
props.maxiter = opts.getStringAs<size_t>("maxiter");
void BP::findMaxResidual( size_t &i, size_t &_I ) {
- assert( !_lut.empty() );
+ DAI_ASSERT( !_lut.empty() );
LutType::const_iterator largestEl = _lut.end();
--largestEl;
i = largestEl->second.first;
for( I = 0; I < nrFactors(); I++ )
if( factor(I).vars() >> ns )
break;
- assert( I != nrFactors() );
+ DAI_ASSERT( I != nrFactors() );
return beliefF(I).marginal(ns);
}
}
/// Calculates a vector of mixtures p * b + (1-p) * c
static vector<Factor> mixBeliefs( Real p, const vector<Factor> &b, const vector<Factor> &c ) {
vector<Factor> out;
- assert( b.size() == c.size() );
+ DAI_ASSERT( b.size() == c.size() );
out.reserve( b.size() );
Real pc = 1 - p;
for( size_t i = 0; i < b.size(); i++ )
if( clampingVar )
foreach( size_t xi, xis )
- assert(/*0<=xi &&*/ xi < var(i).states() );
+ DAI_ASSERT(/*0<=xi &&*/ xi < var(i).states() );
else
foreach( size_t xI, xis )
- assert(/*0<=xI &&*/ xI < factor(i).states() );
+ DAI_ASSERT(/*0<=xI &&*/ xI < factor(i).states() );
// - otherwise, clamp and recurse, saving margin estimates for each
// clamp setting. afterwards, combine estimates.
xi = rnd( var(i).states() );
t++;
} while( bp->beliefV(i).p()[xi] < tiny && t < t1 );
- assert( t < t1 );
+ DAI_ASSERT( t < t1 );
xis.resize( 1, xi );
- // assert(!_clamped_vars.count(i)); // not true for >2-ary variables
+ // DAI_ASSERT(!_clamped_vars.count(i)); // not true for >2-ary variables
DAI_IFVERB(2, "CHOOSE_RANDOM at level " << clamped_vars_list.size() << " chose variable " << i << " state " << xis[0] << endl);
} else {
int t = 0, t1 = 100;
xi = rnd( factor(i).states() );
t++;
} while( bp->beliefF(i).p()[xi] < tiny && t < t1 );
- assert( t < t1 );
+ DAI_ASSERT( t < t1 );
xis.resize( 1, xi );
- // assert(!_clamped_vars.count(i)); // not true for >2-ary variables
+ // DAI_ASSERT(!_clamped_vars.count(i)); // not true for >2-ary variables
DAI_IFVERB(2, endl<<"CHOOSE_RANDOM chose factor "<<i<<" state "<<xis[0]<<endl);
}
} else if( ChooseMethod() == Properties::ChooseMethodType::CHOOSE_MAXENT ) {
win_xk = bp->beliefV(k).p().argmax().first;
}
}
- assert( win_k >= 0 );
- assert( win_xk >= 0 );
+ DAI_ASSERT( win_k >= 0 );
+ DAI_ASSERT( win_xk >= 0 );
i = win_k;
xis.resize( 1, win_xk );
DAI_IFVERB(2, endl<<"CHOOSE_MAXENT chose variable "<<i<<" state "<<xis[0]<<endl);
win_xk = bp->beliefF(k).p().argmax().first;
}
}
- assert( win_k >= 0 );
- assert( win_xk >= 0 );
+ DAI_ASSERT( win_k >= 0 );
+ DAI_ASSERT( win_xk >= 0 );
i = win_k;
xis.resize( 1, win_xk );
DAI_IFVERB(2, endl<<"CHOOSE_MAXENT chose factor "<<i<<" state "<<xis[0]<<endl);
if( !doL1 && needGibbsState( BBP_cost_function() ) )
state = getGibbsState( *bp, 2*bp->Iterations() );
// try clamping each variable manually
- assert( Clamping() == Properties::ClampType::CLAMP_VAR );
+ DAI_ASSERT( Clamping() == Properties::ClampType::CLAMP_VAR );
Real max_cost = 0.0;
int win_k = -1, win_xk = -1;
for( size_t k = 0; k < nrVars(); k++ ) {
delete bp1;
}
}
- assert( win_k >= 0 );
- assert( win_xk >= 0 );
+ DAI_ASSERT( win_k >= 0 );
+ DAI_ASSERT( win_xk >= 0 );
i = win_k;
xis.resize( 1, win_xk );
} else if( ChooseMethod() == Properties::ChooseMethodType::CHOOSE_BBP ) {
xis.resize( 1, xi );
if( clampingVar )
- assert(/*0<=xi &&*/ xi < var(i).states() );
+ DAI_ASSERT(/*0<=xi &&*/ xi < var(i).states() );
else
- assert(/*0<=xi &&*/ xi < factor(i).states() );
+ DAI_ASSERT(/*0<=xi &&*/ xi < factor(i).states() );
DAI_IFVERB(2, "CHOOSE_BBP (num clamped = " << clamped_vars_list.size() << ") chose " << i << " state " << xi << endl);
} else
DAI_THROW(UNKNOWN_ENUM_VALUE);
argmax_var_state = argmax_state.first;
}
}
- assert(/*0 <= argmax_var_state &&*/
+ DAI_ASSERT(/*0 <= argmax_var_state &&*/
argmax_var_state < in_bp.fg().var(argmax_var).states() );
} else {
for( size_t I = 0; I < in_bp.fg().nrFactors(); I++ ) {
argmax_var_state = argmax_state.first;
}
}
- assert(/*0 <= argmax_var_state &&*/
+ DAI_ASSERT(/*0 <= argmax_var_state &&*/
argmax_var_state < in_bp.fg().factor(argmax_var).states() );
}
if( maxVarOut )
if( j >= xis.size() || xis[j] > xi )
cmp_xis.push_back(xi);
}
- assert( xis.size()+cmp_xis.size() == n_states );
+ DAI_ASSERT( xis.size()+cmp_xis.size() == n_states );
return cmp_xis;
}
// Do variable elimination
for( vector<Var>::const_iterator n = ElimSeq.begin(); n != ElimSeq.end(); n++ ) {
size_t i = cl.findVar( *n );
- assert( i != cl.vars.size() );
+ DAI_ASSERT( i != cl.vars.size() );
result.insert( cl.Delta(i) );
delete clamped;
- assert( result.size() == (ns.size() * (ns.size() - 1) / 2) );
+ DAI_ASSERT( result.size() == (ns.size() * (ns.size() - 1) / 2) );
return result;
}
CondProbEstimation::CondProbEstimation( size_t target_dimension, const Prob &pseudocounts )
: _target_dim(target_dimension), _stats(pseudocounts), _initial_stats(pseudocounts)
{
- assert( !(_stats.size() % _target_dim) );
+ DAI_ASSERT( !(_stats.size() % _target_dim) );
}
void SharedParameters::setPermsAndVarSetsFromVarOrders() {
if( _varorders.size() == 0 )
return;
- assert( _estimation != NULL );
+ DAI_ASSERT( _estimation != NULL );
// Construct the permutation objects and the varsets
for( FactorOrientations::const_iterator foi = _varorders.begin(); foi != _varorders.end(); ++foi ) {
VarSet vs;
_perms[foi->first] = calculatePermutation( foi->second, vs );
_varsets[foi->first] = vs;
- assert( _estimation->probSize() == vs.nrStates() );
+ DAI_ASSERT( _estimation->probSize() == vs.nrStates() );
}
}
outVarOrder.push_back( *var_it );
const Factor &f = fg.factor(I);
- assert( f.vars() == _varsets[I] );
+ DAI_ASSERT( f.vars() == _varsets[I] );
const Permute &perm = _perms[I];
for( size_t val_index = 0; val_index < f.states(); ++val_index )
outVals.push_back( f[perm.convert_linear_index(val_index)] );
void ExactInf::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("verbose") );
+ DAI_ASSERT( opts.hasKey("verbose") );
props.verbose = opts.getStringAs<size_t>("verbose");
}
for( I = 0; I < nrFactors(); I++ )
if( factor(I).vars() >> ns )
break;
- assert( I != nrFactors() );
+ DAI_ASSERT( I != nrFactors() );
return beliefF(I).marginal(ns);
}
}
std::string Exception::ErrorStrings[NUM_ERRORS] = {
- "This feature is not implemented",
+ "Assertion failed",
+ "Feature not implemented",
"Unknown DAI algorithm",
"Unknown Property type",
"Malformed Property",
void FactorGraph::clamp( size_t i, size_t x, bool backup ) {
- assert( x <= var(i).states() );
+ DAI_ASSERT( x <= var(i).states() );
Factor mask( var(i), 0.0 );
mask[x] = 1.0;
Factor mask_n( n, 0.0 );
foreach( size_t i, is ) {
- assert( i <= n.states() );
+ DAI_ASSERT( i <= n.states() );
mask_n[i] = 1.0;
}
Factor newF( factor(I).vars(), 0.0 );
foreach( size_t i, is ) {
- assert( i <= st );
+ DAI_ASSERT( i <= st );
newF[i] = factor(I)[i];
}
void Gibbs::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("iters") );
+ DAI_ASSERT( opts.hasKey("iters") );
props.iters = opts.getStringAs<size_t>("iters");
if( opts.hasKey("verbose") )
Prob Gibbs::getVarDist( size_t i ) {
- assert( i < nrVars() );
+ DAI_ASSERT( i < nrVars() );
size_t i_states = var(i).states();
Prob i_given_MB( i_states, 1.0 );
for( I = 0; I < nrFactors(); I++ )
if( factor(I).vars() >> ns )
break;
- assert( I != nrFactors() );
+ DAI_ASSERT( I != nrFactors() );
return beliefF(I).marginal(ns);
}
}
void HAK::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("tol") );
- assert( opts.hasKey("maxiter") );
- assert( opts.hasKey("verbose") );
- assert( opts.hasKey("doubleloop") );
- assert( opts.hasKey("clusters") );
+ DAI_ASSERT( opts.hasKey("tol") );
+ DAI_ASSERT( opts.hasKey("maxiter") );
+ DAI_ASSERT( opts.hasKey("verbose") );
+ DAI_ASSERT( opts.hasKey("doubleloop") );
+ DAI_ASSERT( opts.hasKey("clusters") );
props.tol = opts.getStringAs<double>("tol");
props.maxiter = opts.getStringAs<size_t>("maxiter");
if( opts.hasKey("loopdepth") )
props.loopdepth = opts.getStringAs<size_t>("loopdepth");
else
- assert( props.clusters != Properties::ClustersType::LOOP );
+ DAI_ASSERT( props.clusters != Properties::ClustersType::LOOP );
if( opts.hasKey("damping") )
props.damping = opts.getStringAs<double>("damping");
else
// Check whether counting numbers won't lead to problems
for( size_t beta = 0; beta < nrIRs(); beta++ )
- assert( nbIR(beta).size() + IR(beta).c() != 0.0 );
+ DAI_ASSERT( nbIR(beta).size() + IR(beta).c() != 0.0 );
// Keep old beliefs to check convergence
vector<Factor> old_beliefs;
for( alpha = _Qa.begin(); alpha != _Qa.end(); alpha++ )
if( alpha->vars() >> ns )
break;
- assert( alpha != _Qa.end() );
+ DAI_ASSERT( alpha != _Qa.end() );
return( alpha->marginal(ns) );
}
}
void JTree::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("verbose") );
- assert( opts.hasKey("updates") );
+ DAI_ASSERT( opts.hasKey("verbose") );
+ DAI_ASSERT( opts.hasKey("updates") );
props.verbose = opts.getStringAs<size_t>("verbose");
props.updates = opts.getStringAs<Properties::UpdateType>("updates");
fac2OR.push_back( alpha );
break;
}
- assert( alpha != nrORs() );
+ DAI_ASSERT( alpha != nrORs() );
}
RecomputeORs();
for( alpha = Qa.begin(); alpha != Qa.end(); alpha++ )
if( alpha->vars() >> ns )
break;
- assert( alpha != Qa.end() );
+ DAI_ASSERT( alpha != Qa.end() );
return( alpha->marginal(ns) );
}
}
if( OR(newTree[e].n2).vars().contains( *n ) )
break;
}
- assert( e != newTree.size() );
+ DAI_ASSERT( e != newTree.size() );
// track-back path to root and add edges to subTree
subTree.insert( newTree[e] );
if( newTree[e].n2 == PreviousRoot )
break;
}
- assert( e != newTree.size() );
+ DAI_ASSERT( e != newTree.size() );
// track-back path to root and add edges to subTree
subTree.insert( newTree[e] );
for( beta = 0; beta < nrIRs(); beta++ )
if( UEdge( RTree[beta].n1, RTree[beta].n2 ) == UEdge( alpha1, alpha2 ) )
break;
- assert( beta != nrIRs() );
+ DAI_ASSERT( beta != nrIRs() );
b[i] = beta;
if( !Qa_old.count( alpha1 ) )
void LC::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("tol") );
- assert( opts.hasKey("maxiter") );
- assert( opts.hasKey("verbose") );
- assert( opts.hasKey("cavity") );
- assert( opts.hasKey("updates") );
+ DAI_ASSERT( opts.hasKey("tol") );
+ DAI_ASSERT( opts.hasKey("maxiter") );
+ DAI_ASSERT( opts.hasKey("verbose") );
+ DAI_ASSERT( opts.hasKey("cavity") );
+ DAI_ASSERT( opts.hasKey("updates") );
props.tol = opts.getStringAs<double>("tol");
props.maxiter = opts.getStringAs<size_t>("maxiter");
i = *n;
break;
}
- assert( i.label() == ilabel );
+ DAI_ASSERT( i.label() == ilabel );
// Find variable in psi with label jlabel
Var j;
j = *n;
break;
}
- assert( j.label() == jlabel );
+ DAI_ASSERT( j.label() == jlabel );
// Calculate N(psi,i,j);
double N = psi.strength( i, j );
void MF::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("tol") );
- assert( opts.hasKey("maxiter") );
+ DAI_ASSERT( opts.hasKey("tol") );
+ DAI_ASSERT( opts.hasKey("maxiter") );
props.tol = opts.getStringAs<double>("tol");
props.maxiter = opts.getStringAs<size_t>("maxiter");
if( ns.size() == 1 )
return belief( *(ns.begin()) );
else {
- assert( ns.size() == 1 );
+ DAI_ASSERT( ns.size() == 1 );
return Factor();
}
}
void MR::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("tol") );
- assert( opts.hasKey("verbose") );
- assert( opts.hasKey("updates") );
- assert( opts.hasKey("inits") );
+ DAI_ASSERT( opts.hasKey("tol") );
+ DAI_ASSERT( opts.hasKey("verbose") );
+ DAI_ASSERT( opts.hasKey("updates") );
+ DAI_ASSERT( opts.hasKey("inits") );
props.tol = opts.getStringAs<double>("tol");
props.verbose = opts.getStringAs<size_t>("verbose");
for(size_t _j=0; _j<con[i]; _j++){ // for all j in N_i
size_t _i = kindex[i][_j];
size_t j = nb[i][_j];
- assert( nb[j][_i] == i );
+ DAI_ASSERT( nb[j][_i] == i );
double newM = 0.0;
if( props.updates == Properties::UpdateType::FULL ) {
fac2OR.push_back( alpha );
break;
}
- assert( alpha != nrORs() );
+ DAI_ASSERT( alpha != nrORs() );
}
RecomputeORs();
fac2OR.push_back( alpha );
break;
}
- assert( alpha != nrORs() );
+ DAI_ASSERT( alpha != nrORs() );
}
RecomputeORs();
void RegionGraph::RecomputeOR( size_t I ) {
- assert( I < nrFactors() );
+ DAI_ASSERT( I < nrFactors() );
if( fac2OR[I] != -1U ) {
size_t alpha = fac2OR[I];
OR(alpha).fill( 1.0 );
void TreeEP::setProperties( const PropertySet &opts ) {
- assert( opts.hasKey("tol") );
- assert( opts.hasKey("maxiter") );
- assert( opts.hasKey("verbose") );
- assert( opts.hasKey("type") );
+ DAI_ASSERT( opts.hasKey("tol") );
+ DAI_ASSERT( opts.hasKey("maxiter") );
+ DAI_ASSERT( opts.hasKey("verbose") );
+ DAI_ASSERT( opts.hasKey("type") );
props.tol = opts.getStringAs<double>("tol");
props.maxiter = opts.getStringAs<size_t>("maxiter");
for( beta = 0; beta < jt_RTree.size(); beta++ )
if( UEdge( jt_RTree[beta].n1, jt_RTree[beta].n2 ) == UEdge( alpha1, alpha2 ) )
break;
- assert( beta != jt_RTree.size() );
+ DAI_ASSERT( beta != jt_RTree.size() );
size_t newalpha1 = find(_a.begin(), _a.end(), alpha1) - _a.begin();
if( newalpha1 == _a.size() ) {
TreeEP::TreeEP( const FactorGraph &fg, const PropertySet &opts ) : JTree(fg, opts("updates",string("HUGIN")), false), _maxdiff(0.0), _iters(0), props(), _Q() {
setProperties( opts );
- assert( fg.isConnected() );
+ DAI_ASSERT( fg.isConnected() );
if( opts.hasKey("tree") ) {
ConstructRG( opts.GetAs<DEdgeVec>("tree") );
fac2OR[I] = alpha;
break;
}
- // DIFF WITH JTree::GenerateJT: assert
+ // DIFF WITH JTree::GenerateJT: assert
}
RecomputeORs();
#include <algorithm>
-#include <cassert>
#include <dai/weightedgraph.h>
#include <dai/util.h>
+#include <dai/exceptions.h>
namespace dai {
for( Graph::iterator e = Gr.begin(); e != Gr.end(); ) {
bool e1_in_treeV = treeV.count( e->n1 );
bool e2_in_treeV = treeV.count( e->n2 );
- assert( !(e1_in_treeV && e2_in_treeV) );
+ DAI_ASSERT( !(e1_in_treeV && e2_in_treeV) );
if( e1_in_treeV ) {
// Add directed edge, pointing away from the root
result.push_back( DEdge( e->n1, e->n2 ) );
// (which becomes uniform in the limit that d is small and N goes
// to infinity).
- assert( (N * d) % 2 == 0 );
+ DAI_ASSERT( (N * d) % 2 == 0 );
bool ready = false;
UEdgeVec G;
Factor BinaryFactor( const Var &n, double field ) {
- assert( n.states() == 2 );
+ DAI_ASSERT( n.states() == 2 );
double buf[2];
buf[0] = exp(-field);
buf[1] = exp(field);
Factor BinaryFactor( const Var &n1, const Var &n2, double coupling ) {
- assert( n1.states() == 2 );
- assert( n2.states() == 2 );
- assert( n1 != n2 );
+ DAI_ASSERT( n1.states() == 2 );
+ DAI_ASSERT( n2.states() == 2 );
+ DAI_ASSERT( n1 != n2 );
double buf[4];
buf[0] = (buf[3] = exp(coupling));
buf[1] = (buf[2] = exp(-coupling));
Factor PottsFactor( const Var &n1, const Var &n2, double beta ) {
Factor fac( VarSet( n1, n2 ), 1.0 );
- assert( n1.states() == n2.states() );
+ DAI_ASSERT( n1.states() == n2.states() );
for( size_t s = 0; s < n1.states(); s++ )
fac[ s * (n1.states() + 1) ] = exp(beta);
return fac;
vector<Factor> factors;
size_t N = th.size();
- assert( (w.size1() == N) && (w.size2() == N) );
+ DAI_ASSERT( (w.size1() == N) && (w.size2() == N) );
vars.reserve(N);
for( size_t i = 0; i < N; i++ )
BipartiteGraph CreateRandomBipartiteGraph( size_t N, size_t K, size_t n, size_t k ) {
BipartiteGraph G;
- assert( N * n == K * k );
+ DAI_ASSERT( N * n == K * k );
// build lists of degree-repeated vertex numbers
std::vector<size_t> stubs1(N*n,0);
// Returns order of x in GF(p) with p prime
size_t order (int x, int p) {
x = x % p;
- assert( x != 0 );
+ DAI_ASSERT( x != 0 );
size_t n = 0;
size_t prod = 1;
do {
for( a = 2; a < p; a++ )
if( order(a,p) == k )
break;
- assert( a != p );
+ DAI_ASSERT( a != p );
for( b = 2; b < p; b++ )
if( order(b,p) == j )
break;
- assert( b != p );
+ DAI_ASSERT( b != p );
// cout << "# order(a=" << a << ") = " << order(a,p) << endl;
// cout << "# order(b=" << b << ") = " << order(b,p) << endl;
- assert( N * n == K * k );
+ DAI_ASSERT( N * n == K * k );
typedef BipartiteGraph::Edge Edge;
vector<Edge> edges;