Commits

Luke Plant committed 9e33f92

Avoid infinite loop for flat energy function

Comments (0)

Files changed (2)

         print 'Attempting automatic simulated anneal...'
         
         # Find an initial guess for temperature
+
+        # This can get in an infinite loop if the energy function always returns
+        # the same value, so we include a cut off
         T = 0.0
         E = self.energy(state)
-        while T == 0.0:
+        loops = 0
+        while T == 0.0 and loops < 10000:
             step += 1
+            loops += 1
             self.move(state)
             T = abs( self.energy(state) - E )
+        if T == 0.0:
+            raise Exception("Couldn't find initial temperature")
         
         print 'Exploring temperature landscape:'
         print ' Temperature        Energy    Accept   Improve     Elapsed'
         matrix.append(line)
 
     # Solve
-    planning_data, plan = solve(names, matrix, table_size, table_count,
-                                annealing_time=annealing_time,
-                                exploration_steps=exploration_steps,
-                                )
+    try:
+        planning_data, plan = solve(names, matrix, table_size, table_count,
+                                    annealing_time=annealing_time,
+                                    exploration_steps=exploration_steps,
+                                    )
+    except Exception as e:
+        return error("An error occurred trying to solve this matrix")
 
     return jsonify({'solution': normalise_plan(planning_data.plan_to_people(plan))})