Merged regiongraph.* and daialg.* from SVN head,
[libdai.git] / src / daialg.cpp
index e199670..0fabbfb 100644 (file)
@@ -29,8 +29,6 @@ namespace dai {
 using namespace std;
 
 
-/// Calculate the marginal of obj on ns by clamping 
-/// all variables in ns and calculating logZ for each joined state
 Factor calcMarginal( const InfAlg & obj, const VarSet & ns, bool reInit ) {
     Factor Pns (ns);
     
@@ -41,7 +39,7 @@ Factor calcMarginal( const InfAlg & obj, const VarSet & ns, bool reInit ) {
     Real logZ0 = 0.0;
     for( State s(ns); s.valid(); s++ ) {
         // save unclamped factors connected to ns
-        clamped->saveProbs( ns );
+        clamped->backupFactors( ns );
 
         // set clamping Factors to delta functions
         for( VarSet::const_iterator n = ns.begin(); n != ns.end(); n++ )
@@ -50,6 +48,8 @@ Factor calcMarginal( const InfAlg & obj, const VarSet & ns, bool reInit ) {
         // run DAIAlg, calc logZ, store in Pns
         if( reInit )
             clamped->init();
+        else
+            clamped->init(ns);
         clamped->run();
 
         Real Z;
@@ -64,12 +64,12 @@ Factor calcMarginal( const InfAlg & obj, const VarSet & ns, bool reInit ) {
         Pns[s] = Z;
         
         // restore clamped factors
-        clamped->undoProbs( ns );
+        clamped->restoreFactors( ns );
     }
 
     delete clamped;
 
-    return( Pns.normalized(Prob::NORMPROB) );
+    return( Pns.normalized() );
 }
 
 
@@ -86,9 +86,9 @@ vector<Factor> calcPairBeliefs( const InfAlg & obj, const VarSet& ns, bool reIni
     for( size_t j = 0; j < N; j++ )
         for( size_t k = 0; k < N; k++ )
             if( j == k )
-                pairbeliefs.push_back(Factor());
+                pairbeliefs.push_back( Factor() );
             else
-                pairbeliefs.push_back(Factor(vns[j] | vns[k]));
+                pairbeliefs.push_back( Factor( vns[j] | vns[k] ) );
 
     InfAlg *clamped = obj.clone();
     if( !reInit )
@@ -98,12 +98,11 @@ vector<Factor> calcPairBeliefs( const InfAlg & obj, const VarSet& ns, bool reIni
     for( size_t j = 0; j < N; j++ ) {
         // clamp Var j to its possible values
         for( size_t j_val = 0; j_val < vns[j].states(); j_val++ ) {
-            // save unclamped factors connected to ns
-            clamped->saveProbs( ns );
-            
-            clamped->clamp( vns[j], j_val );
+            clamped->clamp( vns[j], j_val, true );
             if( reInit )
                 clamped->init();
+            else
+                clamped->init(ns);
             clamped->run();
 
             //if( j == 0 )
@@ -127,7 +126,7 @@ vector<Factor> calcPairBeliefs( const InfAlg & obj, const VarSet& ns, bool reIni
                 }
 
             // restore clamped factors
-            clamped->undoProbs( ns );
+            clamped->restoreFactors( ns );
         }
     }
     
@@ -155,7 +154,7 @@ Factor calcMarginal2ndO( const InfAlg & obj, const VarSet& ns, bool reInit ) {
     for( size_t ij = 0; ij < pairbeliefs.size(); ij++ )
         Pns *= pairbeliefs[ij];
     
-    return( Pns.normalized(Prob::NORMPROB) );
+    return( Pns.normalized() );
 }
 
 
@@ -178,12 +177,14 @@ vector<Factor> calcPairBeliefsNew( const InfAlg & obj, const VarSet& ns, bool re
             for( size_t j_val = 0; j_val < nj->states(); j_val++ ) 
                 for( size_t k_val = 0; k_val < nk->states(); k_val++ ) {
                     // save unclamped factors connected to ns
-                    clamped->saveProbs( ns );
+                    clamped->backupFactors( ns );
             
                     clamped->clamp( *nj, j_val );
                     clamped->clamp( *nk, k_val );
                     if( reInit )
                         clamped->init();
+                    else
+                        clamped->init(ns);
                     clamped->run();
 
                     double Z_xj = 1.0;
@@ -199,7 +200,7 @@ vector<Factor> calcPairBeliefsNew( const InfAlg & obj, const VarSet& ns, bool re
                     pairbelief[j_val + (k_val * nj->states())] = Z_xj;
 
                     // restore clamped factors
-                    clamped->undoProbs( ns );
+                    clamped->restoreFactors( ns );
                 }
         
             result.push_back( pairbelief );