Fixed bug in findMaximum (it only considered a single connected component of the...
authorJoris Mooij <j.mooij@cs.ru.nl>
Sat, 11 Feb 2012 19:19:19 +0000 (20:19 +0100)
committerJoris Mooij <j.mooij@cs.ru.nl>
Sat, 11 Feb 2012 19:19:19 +0000 (20:19 +0100)
ChangeLog
src/daialg.cpp

index 21c0ad4..a734860 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
 git master
 ----------
+* Fixed bug in findMaximum (it only considered a single connected component of the factor graph)
 * [Benjamin Piwowarski] Renamed "foreach" macro into "bforeach" to avoid conflicts with newer Boost library versions
 * Optimized ClusterGraph( const FactorGraph&, bool) constructor
 * Fixed "typename" bug in src/alldai.cpp which resulted in FTBFS for older g++ compilers
index 7ddb495..f34d972 100644 (file)
@@ -213,13 +213,24 @@ std::vector<size_t> findMaximum( const InfAlg& obj ) {
     vector<bool> visitedVars( obj.fg().nrVars(), false );
     vector<bool> visitedFactors( obj.fg().nrFactors(), false );
     stack<size_t> scheduledFactors;
-    scheduledFactors.push( 0 );
-    while( !scheduledFactors.empty() ) {
+    size_t nrVisitedFactors = 0;
+    size_t firstUnvisitedFactor = 0;
+    while( nrVisitedFactors < obj.fg().nrFactors() ) {
+        if( scheduledFactors.size() == 0 ) {
+            while( visitedFactors[firstUnvisitedFactor] ) {
+                firstUnvisitedFactor++;
+                if( firstUnvisitedFactor >= obj.fg().nrFactors() )
+                    DAI_THROWE(RUNTIME_ERROR,"Internal error in findMaximum()");
+            }
+            scheduledFactors.push( firstUnvisitedFactor );
+        }
+            
         size_t I = scheduledFactors.top();
         scheduledFactors.pop();
         if( visitedFactors[I] )
             continue;
         visitedFactors[I] = true;
+        nrVisitedFactors++;
 
         // Get marginal of factor I
         Prob probF = obj.beliefF(I).p();