# EXAMPLE
##########
-example : $(SRC)/example.cpp $(HEADERS) $(LIB)/libdai.a
- $(CC) $(CCFLAGS) -o example $(SRC)/example.cpp -ldai
+example : example.cpp $(HEADERS) $(LIB)/libdai.a
+ $(CC) $(CCFLAGS) -o example example.cpp -ldai
# TESTS
########
// construct BP object from FactorGraph
BP(const FactorGraph & fg, const Properties &opts) : DAIAlgFG(fg, opts) {
assert( checkProperties() );
- Regenerate();
+ create();
}
// assignment operator
BP & operator=(const BP & x) {
void findMaxResidual( size_t &i, size_t &_I );
std::string identify() const;
- void Regenerate();
+ void create();
void init();
void calcNewMessage( size_t i, size_t _I );
double run();
if( size() < _maxsize ) {
push_back(x);
_pos = end();
- _maxpos = max_element(begin(),end());
+ if( size() > 1 ) {
+ if( *_maxpos < back() ) {
+ _maxpos = end();
+ _maxpos--;
+ }
+ } else {
+ _maxpos = begin();
+ }
}
else {
if( _pos == end() )
}
}
}
+ size_t max_size() { return _maxsize; }
};
TFactor () : _vs(), _p(1,1.0) {}
// Construct Factor from VarSet
- TFactor( const VarSet& ns ) : _vs(ns), _p(_vs.stateSpace()) {}
+ TFactor( const VarSet& ns ) : _vs(ns), _p(_vs.states()) {}
// Construct Factor from VarSet and initial value
- TFactor( const VarSet& ns, Real p ) : _vs(ns), _p(_vs.stateSpace(),p) {}
+ TFactor( const VarSet& ns, Real p ) : _vs(ns), _p(_vs.states(),p) {}
// Construct Factor from VarSet and initial array
- TFactor( const VarSet& ns, const Real* p ) : _vs(ns), _p(_vs.stateSpace(),p) {}
+ TFactor( const VarSet& ns, const Real* p ) : _vs(ns), _p(_vs.states(),p) {}
// Construct Factor from VarSet and TProb<T>
TFactor( const VarSet& ns, const TProb<T> p ) : _vs(ns), _p(p) {
#ifdef DAI_DEBUG
- assert( _vs.stateSpace() == _p.size() );
+ assert( _vs.states() == _p.size() );
#endif
}
const TProb<T> & p() const { return _p; }
TProb<T> & p() { return _p; }
const VarSet & vars() const { return _vs; }
- size_t stateSpace() const {
+ size_t states() const {
#ifdef DAI_DEBUG
- assert( _vs.stateSpace() == _p.size() );
+ assert( _vs.states() == _p.size() );
#endif
return _p.size();
}
// OPTIMIZE ME
Index i_ns (ns, _vs);
Index i_nsrem (nsrem, _vs);
- for( size_t i = 0; i < stateSpace(); i++, ++i_ns, ++i_nsrem )
+ for( size_t i = 0; i < states(); i++, ++i_ns, ++i_nsrem )
if( (size_t)i_ns == ns_state )
result._p[i_nsrem] = _p[i];
// construct MF object from FactorGraph
MF(const FactorGraph & fg, const Properties &opts) : DAIAlgFG(fg, opts) {
assert( checkProperties() );
- Regenerate();
+ create();
}
// assignment operator
MF & operator=(const MF & x) {
static const char *Name;
std::string identify() const;
- void Regenerate();
+ void create();
void init();
double run();
Factor beliefV (size_t i) const;
/// Return statespace, i.e. the product of the number of states of each variable
- size_t stateSpace() const {
+ size_t states() const {
#ifdef DAI_DEBUG
size_t x = 1;
for( const_iterator i = begin(); i != end(); ++i )
dims.push_back( j->states() );
}
- // mxArray *BiP = mxCreateDoubleMatrix(I->stateSpace(),1,mxREAL);
+ // mxArray *BiP = mxCreateDoubleMatrix(I->states(),1,mxREAL);
mxArray *BiP = mxCreateNumericArray(I->vars().size(), &(*(dims.begin())), mxDOUBLE_CLASS, mxREAL);
double *BiP_data = mxGetPr(BiP);
- for( size_t j = 0; j < I->stateSpace(); j++ )
+ for( size_t j = 0; j < I->states(); j++ )
BiP_data[j] = (*I)[j];
mxSetField(Bi,0,"Member",BiMember);
}
-void BP::Regenerate() {
+void BP::create() {
// create edge properties
edges.clear();
edges.reserve( nrVars() );
newEP.message = Prob( var(i).states() );
newEP.newMessage = Prob( var(i).states() );
- newEP.index.reserve( factor(I).stateSpace() );
+ newEP.index.reserve( factor(I).states() );
for( Index k( var(i), factor(I).vars() ); k >= 0; ++k )
newEP.index.push_back( k );
os << i->states() << " ";
os << endl;
size_t nr_nonzeros = 0;
- for( size_t k = 0; k < fg.factor(I).stateSpace(); k++ )
+ for( size_t k = 0; k < fg.factor(I).states(); k++ )
if( fg.factor(I)[k] != 0.0 )
nr_nonzeros++;
os << nr_nonzeros << endl;
- for( size_t k = 0; k < fg.factor(I).stateSpace(); k++ )
+ for( size_t k = 0; k < fg.factor(I).states(); k++ )
if( fg.factor(I)[k] != 0.0 ) {
char buf[20];
sprintf(buf,"%18.14g", fg.factor(I)[k]);
void FactorGraph::clamp( const Var & n, size_t i ) {
assert( i <= n.states() );
-/* if( do_surgery ) {
- size_t ni = find( vars().begin(), vars().end(), n) - vars().begin();
-
- if( ni != nrVars() ) {
- for( _nb_cit I = nb1(ni).begin(); I != nb1(ni).end(); I++ ) {
- if( factor(*I).size() == 1 )
- // Remove this single-variable factor
- // I = (_V2.erase(I))--;
- _E12.erase( _E12.begin() + VV2E(ni, *I) );
- else {
- // Replace it by the slice
- Index ind_I_min_n( factor(*I), factor(*I) / n );
- Index ind_n( factor(*I), n );
- Factor slice_I( factor(*I) / n );
- for( size_t ind_I = 0; ind_I < factor(*I).stateSpace(); ++ind_I, ++ind_I_min_n, ++ind_n )
- if( ind_n == i )
- slice_I[ind_I_min_n] = factor(*I)[ind_I];
- factor(*I) = slice_I;
-
- // Remove the edge between n and I
- _E12.erase( _E12.begin() + VV2E(ni, *I) );
- }
- }
-
- Regenerate();
-
- // remove all unconnected factors
- for( size_t I = 0; I < nrFactors(); I++ )
- if( nb2(I).size() == 0 )
- DeleteFactor(I--);
-
- DeleteVar( ni );
-
- // FIXME
- }
- } */
-
- // The cheap solution (at least in terms of coding time) is to multiply every factor
- // that contains the variable with a delta function
+ // Multiply each factor that contains the variable with a delta function
Factor delta_n_i(n,0.0);
delta_n_i[i] = 1.0;
void HAK::init( const VarSet &ns ) {
for( vector<Factor>::iterator alpha = _Qa.begin(); alpha != _Qa.end(); alpha++ )
if( alpha->vars() && ns )
- alpha->fill( 1.0 / alpha->stateSpace() );
+ alpha->fill( 1.0 / alpha->states() );
for( size_t beta = 0; beta < nrIRs(); beta++ )
if( IR(beta) && ns ) {
- _Qb[beta].fill( 1.0 / IR(beta).stateSpace() );
+ _Qb[beta].fill( 1.0 );
foreach( const Neighbor &alpha, nbIR(beta) ) {
size_t _beta = alpha.dual;
- muab( alpha, _beta ).fill( 1.0 / IR(beta).stateSpace() );
- muba( alpha, _beta ).fill( 1.0 / IR(beta).stateSpace() );
+ muab( alpha, _beta ).fill( 1.0 / IR(beta).states() );
+ muba( alpha, _beta ).fill( 1.0 / IR(beta).states() );
}
}
}
assert( checkProperties() );
for( vector<Factor>::iterator alpha = _Qa.begin(); alpha != _Qa.end(); alpha++ )
- alpha->fill( 1.0 / alpha->stateSpace() );
+ alpha->fill( 1.0 / alpha->states() );
for( vector<Factor>::iterator beta = _Qb.begin(); beta != _Qb.end(); beta++ )
- beta->fill( 1.0 / beta->stateSpace() );
+ beta->fill( 1.0 / beta->states() );
for( size_t alpha = 0; alpha < nrORs(); alpha++ )
foreach( const Neighbor &beta, nbOR(alpha) ) {
size_t _beta = beta.iter;
- muab( alpha, _beta ).fill( 1.0 / muab( alpha, _beta ).stateSpace() );
- muba( alpha, _beta ).fill( 1.0 / muab( alpha, _beta ).stateSpace() );
+ muab( alpha, _beta ).fill( 1.0 / muab( alpha, _beta ).states() );
+ muba( alpha, _beta ).fill( 1.0 / muab( alpha, _beta ).states() );
}
}
// find new root clique (the one with maximal statespace overlap with ns)
size_t maxval = 0, maxalpha = 0;
for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
- size_t val = (ns & OR(alpha).vars()).stateSpace();
+ size_t val = (ns & OR(alpha).vars()).states();
if( val > maxval ) {
maxval = val;
maxalpha = alpha;
double maxdiff = 0;
if( Verbose() >= 2 )
- cout << "Initing cavity " << var(i) << "(" << delta(i).size() << " vars, " << delta(i).stateSpace() << " states)" << endl;
+ cout << "Initing cavity " << var(i) << "(" << delta(i).size() << " vars, " << delta(i).states() << " states)" << endl;
if( Cavity() == CavityType::UNIFORM )
Bi = Factor(delta(i));
}
-void MF::Regenerate() {
-// DAIAlgFG::Regenerate();
-
+void MF::create() {
// clear beliefs
_beliefs.clear();
_beliefs.reserve( nrVars() );
} while( 1 );
}
Factor newfac(vars);
- for( size_t t = 0; t < newfac.stateSpace(); t++ )
+ for( size_t t = 0; t < newfac.states(); t++ )
newfac[t] = exp(rnd_stdnormal() * sigma);
factors.push_back(newfac);
}
size_t max_Delta_size = 0;
for( size_t i = 0; i < fg.nrVars(); i++ ) {
VarSet di = fg.delta(i);
- size_t Ds = fg.Delta(i).stateSpace();
+ size_t Ds = fg.Delta(i).states();
if( Ds > max_Delta_size )
max_Delta_size = Ds;
- cavsum_lcbp += di.stateSpace();
+ cavsum_lcbp += di.states();
for( VarSet::const_iterator j = di.begin(); j != di.end(); j++ )
cavsum_lcbp2 += j->states();
}