Commits

Luke Plant committed fa7065c

Made algorithm cope with mutable data structures in state variables

  • Participants
  • Parent commits 8a4492c

Comments (0)

Files changed (2)

         # Note initial state
         T = Tmax
         E = self.energy(state)
-        #prevState = copy.deepcopy(state)
-        prevState = state[:]
+        prevState = copy.deepcopy(state)
         prevEnergy = E
-        #bestState = copy.deepcopy(state)
-        bestState = state[:]
+        bestState = copy.deepcopy(state)
         bestEnergy = E
         trials, accepts, improves = 0, 0, 0
         if updates > 0:
             trials += 1
             if dE > 0.0 and math.exp(-dE/T) < random.random():
                 # Restore previous state
-                #state = copy.deepcopy(prevState)
-                state = prevState[:]
+                state = copy.deepcopy(prevState)
                 E = prevEnergy
             else:
                 # Accept new state and compare to best state
                 accepts += 1
                 if dE < 0.0:
                     improves += 1
-                #prevState = copy.deepcopy(state)
-                prevState = state[:]
+                prevState = copy.deepcopy(state)
                 prevEnergy = E
                 if E < bestEnergy:
-                    #bestState = copy.deepcopy(state)
-                    bestState = state[:]
+                    bestState = copy.deepcopy(state)
                     bestEnergy = E
             if updates > 1:
                 if step // updateWavelength > (step-1) // updateWavelength:
             """Anneals a system at constant temperature and returns the state,
             energy, rate of acceptance, and rate of improvement."""
             E = self.energy(state)
-            #prevState = copy.deepcopy(state)
-            prevState = state[:]
+            prevState = copy.deepcopy(state)
             prevEnergy = E
             accepts, improves = 0, 0
             for step in range(steps):
                 E = self.energy(state)
                 dE = E - prevEnergy
                 if dE > 0.0 and math.exp(-dE/T) < random.random():
-                    #state = copy.deepcopy(prevState)
-                    state = prevState[:]
+                    state = copy.deepcopy(prevState)
                     E = prevEnergy
                 else:
                     accepts += 1
                     if dE < 0.0:
                         improves += 1
-                    #prevState = copy.deepcopy(state)
-                    prevState = state[:]
+                    prevState = copy.deepcopy(state)
                     prevEnergy = E
             return state, E, float(accepts)/steps, float(improves)/steps
         

seating_planner.py

 #### Plans and Tables ####
 
 # A Plan is a list of Tables.
-# A Table is a tuple of integers representing people
+# A Table is a list of integers representing people
 
-# We use tuples so that they are immutable, so that copying a whole plan is just
-# shallow copying a list, without having to worry about substructures being
-# modified by other copies.
-
-Table = tuple
+Table = list
 Plan = list
 
 
     p1 = random.choice(t1)
     p2 = random.choice(t2)
 
-    t1 = list(t1)
     t1.remove(p1)
     t1.append(p2)
-    t1 = Table(t1)
 
-    t2 = list(t2)
     t2.remove(p2)
     t2.append(p1)
-    t2 = Table(t2)
 
     plan.append(t1)
     plan.append(t2)