Added init parameter to HAK/GBP to allow for random initialization
authorJoris Mooij <joris.mooij@tuebingen.mpg.de>
Fri, 9 Oct 2009 15:56:05 +0000 (17:56 +0200)
committerJoris Mooij <joris.mooij@tuebingen.mpg.de>
Fri, 9 Oct 2009 15:56:05 +0000 (17:56 +0200)
ChangeLog
include/dai/hak.h
src/hak.cpp
tests/aliases.conf

index 34774e3..a53c029 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* Added init parameter to HAK/GBP to allow for random initialization
 * 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 )
index e62b7f6..ccca19b 100644 (file)
@@ -48,6 +48,9 @@ class HAK : public DAIAlgRG {
             /// Enumeration of possible cluster choices
             DAI_ENUM(ClustersType,MIN,DELTA,LOOP)
 
+            /// Enumeration of possible message initializations
+            DAI_ENUM(InitType,UNIFORM,RANDOM)
+
             /// Verbosity
             size_t verbose;
 
@@ -63,6 +66,9 @@ class HAK : public DAIAlgRG {
             /// How to choose the clusters
             ClustersType clusters;
 
+            /// How to initialize the messages
+            InitType init;
+
             /// Use single-loop (GBP) or double-loop (HAK)
             bool doubleloop;
 
index 43a368d..7232615 100644 (file)
@@ -45,6 +45,10 @@ void HAK::setProperties( const PropertySet &opts ) {
         props.damping = opts.getStringAs<double>("damping");
     else
         props.damping = 0.0;
+    if( opts.hasKey("init") )
+        props.init = opts.getStringAs<Properties::InitType>("init");
+    else
+        props.init = Properties::InitType::UNIFORM;
 }
 
 
@@ -55,6 +59,7 @@ PropertySet HAK::getProperties() const {
     opts.Set( "verbose", props.verbose );
     opts.Set( "doubleloop", props.doubleloop );
     opts.Set( "clusters", props.clusters );
+    opts.Set( "init", props.init );
     opts.Set( "loopdepth", props.loopdepth );
     opts.Set( "damping", props.damping );
     return opts;
@@ -69,6 +74,7 @@ string HAK::printProperties() const {
     s << "verbose=" << props.verbose << ",";
     s << "doubleloop=" << props.doubleloop << ",";
     s << "clusters=" << props.clusters << ",";
+    s << "init=" << props.init << ",";
     s << "loopdepth=" << props.loopdepth << ",";
     s << "damping=" << props.damping << "]";
     return s.str();
@@ -168,16 +174,28 @@ string HAK::identify() const {
 
 void HAK::init( const VarSet &ns ) {
     for( vector<Factor>::iterator alpha = _Qa.begin(); alpha != _Qa.end(); alpha++ )
-        if( alpha->vars().intersects( ns ) )
-            alpha->fill( 1.0 / alpha->states() );
+        if( alpha->vars().intersects( ns ) ) {
+            if( props.init == Properties::InitType::UNIFORM )
+                alpha->fill( 1.0 / alpha->states() );
+            else
+                alpha->randomize();
+        }
 
     for( size_t beta = 0; beta < nrIRs(); beta++ )
         if( IR(beta).intersects( ns ) ) {
-            _Qb[beta].fill( 1.0 );
+            if( props.init == Properties::InitType::UNIFORM )
+                _Qb[beta].fill( 1.0 );
+            else
+                _Qb[beta].randomize();
             foreach( const Neighbor &alpha, nbIR(beta) ) {
                 size_t _beta = alpha.dual;
-                muab( alpha, _beta ).fill( 1.0 );
-                muba( alpha, _beta ).fill( 1.0 );
+                if( props.init == Properties::InitType::UNIFORM ) {
+                    muab( alpha, _beta ).fill( 1.0 );
+                    muba( alpha, _beta ).fill( 1.0 );
+                } else {
+                    muab( alpha, _beta ).randomize();
+                    muba( alpha, _beta ).randomize();
+                }
             }
         }
 }
@@ -185,16 +203,27 @@ void HAK::init( const VarSet &ns ) {
 
 void HAK::init() {
     for( vector<Factor>::iterator alpha = _Qa.begin(); alpha != _Qa.end(); alpha++ )
-        alpha->fill( 1.0 / alpha->states() );
+        if( props.init == Properties::InitType::UNIFORM )
+            alpha->fill( 1.0 / alpha->states() );
+        else
+            alpha->randomize();
 
     for( vector<Factor>::iterator beta = _Qb.begin(); beta != _Qb.end(); beta++ )
-        beta->fill( 1.0 / beta->states() );
+        if( props.init == Properties::InitType::UNIFORM )
+            beta->fill( 1.0 / beta->states() );
+        else
+            beta->randomize();
 
     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 ).states() );
-            muba( alpha, _beta ).fill( 1.0 / muab( alpha, _beta ).states() );
+            if( props.init == Properties::InitType::UNIFORM ) {
+                muab( alpha, _beta ).fill( 1.0 / muab( alpha, _beta ).states() );
+                muba( alpha, _beta ).fill( 1.0 / muab( alpha, _beta ).states() );
+            } else {
+                muab( alpha, _beta ).randomize();
+                muba( alpha, _beta ).randomize();
+            }
         }
 }
 
index 81eee6a..3d2f9d4 100644 (file)
@@ -46,23 +46,23 @@ MR_EXACT_LINEAR:                MR[updates=LINEAR,inits=EXACT,verbose=0,tol=1e-9
 
 # --- HAK ---------------------
 
-GBP_MIN:                        HAK[doubleloop=0,clusters=MIN,tol=1e-9,maxiter=10000,verbose=0]
-GBP_DELTA:                      HAK[doubleloop=0,clusters=DELTA,tol=1e-9,maxiter=10000,verbose=0]
-GBP_LOOP3:                      HAK[doubleloop=0,clusters=LOOP,loopdepth=3,tol=1e-9,maxiter=10000,verbose=0]
-GBP_LOOP4:                      HAK[doubleloop=0,clusters=LOOP,loopdepth=4,tol=1e-9,maxiter=10000,verbose=0]
-GBP_LOOP5:                      HAK[doubleloop=0,clusters=LOOP,loopdepth=5,tol=1e-9,maxiter=10000,verbose=0]
-GBP_LOOP6:                      HAK[doubleloop=0,clusters=LOOP,loopdepth=6,tol=1e-9,maxiter=10000,verbose=0]
-GBP_LOOP7:                      HAK[doubleloop=0,clusters=LOOP,loopdepth=7,tol=1e-9,maxiter=10000,verbose=0]
-GBP_LOOP8:                      HAK[doubleloop=0,clusters=LOOP,loopdepth=8,tol=1e-9,maxiter=10000,verbose=0]
-
-HAK_MIN:                        HAK[doubleloop=1,clusters=MIN,tol=1e-9,maxiter=10000,verbose=0]
-HAK_DELTA:                      HAK[doubleloop=1,clusters=DELTA,tol=1e-9,maxiter=10000,verbose=0]
-HAK_LOOP3:                      HAK[doubleloop=1,clusters=LOOP,loopdepth=3,tol=1e-9,maxiter=10000,verbose=0]
-HAK_LOOP4:                      HAK[doubleloop=1,clusters=LOOP,loopdepth=4,tol=1e-9,maxiter=10000,verbose=0]
-HAK_LOOP5:                      HAK[doubleloop=1,clusters=LOOP,loopdepth=5,tol=1e-9,maxiter=10000,verbose=0]
-HAK_LOOP6:                      HAK[doubleloop=1,clusters=LOOP,loopdepth=6,tol=1e-9,maxiter=10000,verbose=0]
-HAK_LOOP7:                      HAK[doubleloop=1,clusters=LOOP,loopdepth=7,tol=1e-9,maxiter=10000,verbose=0]
-HAK_LOOP8:                      HAK[doubleloop=1,clusters=LOOP,loopdepth=8,tol=1e-9,maxiter=10000,verbose=0]
+GBP_MIN:                        HAK[doubleloop=0,clusters=MIN,init=UNIFORM,tol=1e-9,maxiter=10000,verbose=0]
+GBP_DELTA:                      HAK[doubleloop=0,clusters=DELTA,init=UNIFORM,tol=1e-9,maxiter=10000,verbose=0]
+GBP_LOOP3:                      HAK[doubleloop=0,clusters=LOOP,init=UNIFORM,loopdepth=3,tol=1e-9,maxiter=10000,verbose=0]
+GBP_LOOP4:                      HAK[doubleloop=0,clusters=LOOP,init=UNIFORM,loopdepth=4,tol=1e-9,maxiter=10000,verbose=0]
+GBP_LOOP5:                      HAK[doubleloop=0,clusters=LOOP,init=UNIFORM,loopdepth=5,tol=1e-9,maxiter=10000,verbose=0]
+GBP_LOOP6:                      HAK[doubleloop=0,clusters=LOOP,init=UNIFORM,loopdepth=6,tol=1e-9,maxiter=10000,verbose=0]
+GBP_LOOP7:                      HAK[doubleloop=0,clusters=LOOP,init=UNIFORM,loopdepth=7,tol=1e-9,maxiter=10000,verbose=0]
+GBP_LOOP8:                      HAK[doubleloop=0,clusters=LOOP,init=UNIFORM,loopdepth=8,tol=1e-9,maxiter=10000,verbose=0]
+
+HAK_MIN:                        HAK[doubleloop=1,clusters=MIN,init=UNIFORM,tol=1e-9,maxiter=10000,verbose=0]
+HAK_DELTA:                      HAK[doubleloop=1,clusters=DELTA,init=UNIFORM,tol=1e-9,maxiter=10000,verbose=0]
+HAK_LOOP3:                      HAK[doubleloop=1,clusters=LOOP,init=UNIFORM,loopdepth=3,tol=1e-9,maxiter=10000,verbose=0]
+HAK_LOOP4:                      HAK[doubleloop=1,clusters=LOOP,init=UNIFORM,loopdepth=4,tol=1e-9,maxiter=10000,verbose=0]
+HAK_LOOP5:                      HAK[doubleloop=1,clusters=LOOP,init=UNIFORM,loopdepth=5,tol=1e-9,maxiter=10000,verbose=0]
+HAK_LOOP6:                      HAK[doubleloop=1,clusters=LOOP,init=UNIFORM,loopdepth=6,tol=1e-9,maxiter=10000,verbose=0]
+HAK_LOOP7:                      HAK[doubleloop=1,clusters=LOOP,init=UNIFORM,loopdepth=7,tol=1e-9,maxiter=10000,verbose=0]
+HAK_LOOP8:                      HAK[doubleloop=1,clusters=LOOP,init=UNIFORM,loopdepth=8,tol=1e-9,maxiter=10000,verbose=0]
 
 # --- LC ----------------------