Advertisement
Guest User

Untitled

a guest
Nov 4th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. import math
  2. import random
  3. import copy
  4. import time
  5.  
  6.  
  7. def dist(first_point, second_point):
  8.  
  9.     return math.sqrt((second_point[0] - first_point[0]) ** 2 + (second_point[1] - first_point[1]) ** 2)
  10.  
  11.  
  12. def readFile():
  13.     lst1 = []
  14.     with open("cityData.txt") as file:
  15.         for line in file:
  16.             firstMatrix = []
  17.             x1, y1, z1 = line.split()
  18.             firstMatrix.append(float(x1))
  19.             firstMatrix.append(float(y1))
  20.             firstMatrix.append(z1)
  21.             lst1.append(firstMatrix)
  22.     return lst1
  23.  
  24.  
  25. def get_distance(aList):
  26.  
  27.     result = sum([dist(x[:-1], y[:-1]) for x, y in zip(aList, aList[1:])])
  28.     result += dist(aList[-1][:-1],aList[0][:-1])
  29.     return result
  30.  
  31.  
  32. def acceptanceProbability(energy, newEnergy, temperature):
  33.     if newEnergy < energy:
  34.         return 1.0
  35.     else:
  36.         return math.exp((energy-newEnergy)/temperature)
  37.  
  38.  
  39. def neighbor_generator(alist, index1, index2):
  40.             copy_list = copy.deepcopy(alist)
  41.             copy_list[index1],copy_list[index2]= copy_list[index2],copy_list[index1]
  42.             return copy_list
  43.  
  44.  
  45. def simmulated_annealing(cityPaths,startTemp, cooling_rate):
  46.     old_path = random.sample(cityPaths,len(cityPaths))
  47.  
  48.  
  49.     old_cost = get_distance(old_path)
  50.     print("starting path is"+str(old_path))
  51.     print("starting cost is "+str(old_cost))
  52.     best = []
  53.  
  54.  
  55.     counter = 0
  56.     while startTemp>1:
  57.         # i = 1
  58.         # #print(startTemp)
  59.         # while i<=100:
  60.  
  61.         rando_list = random.sample(range(0,14), 2)
  62.         new_path = neighbor_generator(old_path,rando_list[0],rando_list[1])
  63.         new_cost = get_distance(new_path)
  64.         probs = acceptanceProbability(old_cost, new_cost, startTemp)
  65.         if new_cost < old_cost:
  66.             old_cost = new_cost
  67.             old_path = new_path
  68.             if old_cost not in best and (len(best) < 50):
  69.                 best.append(old_cost)
  70.         else:
  71.             if probs > random.random():
  72.                 old_cost = new_cost
  73.                 old_path = new_path
  74.  
  75.  
  76.  
  77.         startTemp *= 1-cooling_rate
  78.  
  79.  
  80.  
  81.     print("the best path is "+ str(old_path))
  82.     print("the trip cost is "+str(old_cost))
  83.  
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     cityPaths = readFile()
  88.     simmulated_annealing(cityPaths,10000,0.003)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement