Advertisement
Guest User

cursesTSP.py

a guest
Dec 14th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. ##Brendan Benshoof
  2. ##12/14/2011
  3. ##Copyleft 2011
  4.  
  5. import random, pygame, sys, math
  6. import pygcurse as pygcurse
  7. from pygame.locals import *
  8.  
  9. ##window setup
  10. win = pygcurse.PygcurseWindow(40, 40)
  11.  
  12. #genetic algorithim speedcode TSP 38 minutes long
  13.  
  14. #assumption 1 input graph is a clique
  15.  
  16. #cities x,y coords
  17. cities = []
  18. mapsize = 40 #sidelength of a square map
  19. num_of_cities = 15
  20.  
  21. gen_size = 50 #divisable by parents
  22. parentsl = 10 #number of parents for each generation
  23. game_length = 100 # number of generations
  24.  
  25. def get_dist(a,b):
  26.     return math.sqrt((a[0]-b[0])**2.0+(a[1]-b[1])**2.0)
  27.  
  28. #now the fun really starts
  29. def Grade(order):
  30.     total = 0.0
  31.     for i in range(0,num_of_cities-1):
  32.         total += get_dist(order[i],order[i+1])
  33.     return total
  34.  
  35. def Mutate(order):
  36.     copy = order[:]
  37.     a = random.randint(0,num_of_cities-1)
  38.     b = random.randint(0,num_of_cities-1)
  39.     aa = copy[a]
  40.     bb = copy[b]
  41.     copy[a]=bb
  42.     copy[b]=aa
  43.     return copy
  44.  
  45.  
  46.  
  47.  
  48. #make a list of 20 random cities
  49. for i in range(0,num_of_cities):
  50.     x = random.random()*mapsize
  51.     y = random.random()*mapsize
  52.     cities.append((x,y))
  53. #print(cities)
  54.  
  55. #populate
  56. generation = []
  57. for i in range(0,gen_size):
  58.     copy = cities[:]
  59.     random.shuffle(copy)
  60.     generation.append(copy)
  61.  
  62. def render(order):
  63.     global win
  64.     pi = math.pi
  65.     for i in range(0,num_of_cities):
  66.        
  67.         #walk to the next city
  68.         char = "|"
  69.             ##up,down,left,right
  70.         resolution = 100 #more then high enough
  71.         slope = math.fabs((order[i][1]*1.0-order[(i+1)%num_of_cities][1]*1.0)/(order[i][0]*1.0-order[(i+1)%num_of_cities][0]*1.0))
  72.         if(slope<0.4):
  73.             char = "-"
  74.         elif(slope<100):
  75.             char = "/"
  76.         for j in range(0,resolution):
  77.             stepx = int((order[(i+1)%num_of_cities][0]-order[i][0]*1.0)*1.0*j/(resolution*1.0)+order[i][0])
  78.             stepy = int((order[(i+1)%num_of_cities][1]-order[i][1]*1.0)*1.0*j/(resolution*1.0)+order[i][1])
  79.             if(stepx>=0 and stepy>=0):
  80.                 win.write(char,x=stepx,y=stepy)
  81.        
  82.     for i in range(0,num_of_cities):
  83.         win.write("#",x=order[i][0],y=order[i][1])
  84.    
  85.     pygcurse.waitforkeypress()
  86. #mainloop
  87. for k in range(0,game_length):
  88.     parents = []
  89.     for i in range(0,parentsl): #this is horrible I know, no time to think
  90.         bestscore = 999999999.9
  91.         bestid = -1
  92.         for j in range(0,len(generation)):
  93.             s = Grade(generation[j])
  94.             if(s<bestscore):
  95.                 bestscore = s
  96.                 bestid = j
  97.         parents.append(generation[bestid])
  98.         generation.pop(bestid)
  99.  
  100.    
  101.     #make more kids
  102.     generation = []
  103.     for i in range(0,parentsl): #this is horrible I know, no time to think
  104.         kids = gen_size/parentsl-1
  105.         generation.append(parents[i])
  106.         for j in range(0,kids):
  107.             generation.append(Mutate(parents[i]))
  108.     #repeat while time remains
  109. render(generation[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement