SHARE
TWEET

Untitled

a guest Dec 14th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from scipy.spatial import distance
  2. from PIL import Image ,ImageDraw
  3. import tkinter as tk
  4.  
  5. class Nodo(object):
  6.     def __init__(self, nombre, pos, adyNodes, ID, f, g, h, father):
  7.         self.nombre = nombre
  8.         self.adyNodes = adyNodes
  9.         self.pos = pos
  10.         self.ID = ID
  11.         self.f = f
  12.         self.g = g
  13.         self.h = h
  14.         self.father = father
  15.  
  16.     def name(self):
  17.         print(self.nombre)
  18.  
  19.     def equ(self, nodo):
  20.         if (self.nombre == nodo.nombre):
  21.             return True
  22.         return False
  23.  
  24.  
  25. infinity = float('inf')
  26.  
  27. shinagawa = Nodo("Shinagawa", (340, 480), [], "JY25", infinity, infinity, 0, Nodo)
  28. osaki = Nodo("osaki", (290, 480), [], "JY24", infinity, infinity, 0, Nodo)
  29. gotanda = Nodo("gotanda", (250, 480), [], "JY23", infinity, infinity, 0, Nodo)
  30. meguro = Nodo("meguro", (220, 480), [], "JY22", infinity, infinity, 0, Nodo)
  31. ebisu = Nodo("ebisu", (180, 450), [], "JY21", infinity, infinity, 0, Nodo)
  32. shibuya = Nodo("shibuya", (180, 410), [], "JY20", infinity, infinity, 0, Nodo)
  33. harajuku = Nodo("harajuku", (180, 370), [], "JY19", infinity, infinity, 0, Nodo)
  34. yoyogi = Nodo("yoyogi", (180, 300), [], "JY18-JB11", infinity, infinity, 0, Nodo)
  35. shinjuku = Nodo("shinjuku", (180, 270), [], "JY17-JC05-JB10", infinity, infinity, 0, Nodo)
  36. shinokubo = Nodo("shin-okubo", (180, 200), [], "JY16", infinity, infinity, 0, Nodo)
  37. takadanobaba = Nodo("takadanobaba", (180, 160), [], "JY15", infinity, infinity, 0, Nodo)
  38. mejiro = Nodo("mejiro", (215, 130), [], "JY14", infinity, infinity, 0, Nodo)
  39. ikebukuro = Nodo("ikebukuro", (270, 90), [], "JY13", infinity, infinity, 0, Nodo)
  40. otsuka = Nodo("otsuka", (320, 90), [], "JY12", infinity, infinity, 0, Nodo)
  41. sugamo = Nodo("sugamo", (360, 90), [], "JY11", infinity, infinity, 0, Nodo)
  42. komagome = Nodo("komagome", (390, 90), [], "JY10", infinity, infinity, 0, Nodo)
  43. tabata = Nodo("tabata", (430, 110), [], "JY09", infinity, infinity, 0, Nodo)
  44. nishinippori = Nodo("nishinippori", (450, 130), [], "JY08", infinity, infinity, 0, Nodo)
  45. nippori = Nodo("nippori", (450, 160), [], "JY07", infinity, infinity, 0, Nodo)
  46. Uguisudani = Nodo("Uguisudani", (450, 180), [], "JY06", infinity, infinity, 0, Nodo)
  47. ueno = Nodo("ueno", (450, 205), [], "JY05", infinity, infinity, 0, Nodo)
  48. okachimachi = Nodo("okachimachi", (450, 230), [], "JY04", infinity, infinity, 0, Nodo)
  49. akihabara = Nodo("akihabara", (450, 260), [], "JY03-JB19", infinity, infinity, 0, Nodo)
  50. kanda = Nodo("kanda", (450, 300), [], "JY02", infinity, infinity, 0, Nodo)
  51. tokio = Nodo("tokio", (450, 330), [], "JY01-JC01", infinity, infinity, 0, Nodo)
  52. yurakucho = Nodo("yurakucho", (450, 360), [], "JY30", infinity, infinity, 0, Nodo)
  53. shimbashi = Nodo("shimbashi", (430, 390), [], "JY29", infinity, infinity, 0, Nodo)
  54. hamamatsucho = Nodo("hamamatsucho", (400, 410), [], "JY28", infinity, infinity, 0, Nodo)
  55. tamachi = Nodo("tamachi", (370, 440), [], "JY27", infinity, infinity, 0, Nodo)
  56. ochanomizu = Nodo("ochanomizu", (350, 235), [], "JC03-JB18", infinity, infinity, 0, Nodo)
  57. suidobashi = Nodo("suidobashi", (305, 275), [], "JB17", infinity, infinity, 0, Nodo)
  58. lidabashi = Nodo("lidabashi", (290, 290), [], "JB16", infinity, infinity, 0, Nodo)
  59. lichigaya = Nodo("lichigaya", (275, 305), [], "JB15", infinity, infinity, 0, Nodo)
  60. yotsuya = Nodo("yotsuya", (260, 325), [], "JB14", infinity, infinity, 0, Nodo)
  61. shinanomachi = Nodo("shinanomachi", (235, 340), [], "JB13", infinity, infinity, 0, Nodo)
  62. sendagaya = Nodo("sendagaya", (195, 340), [], "JB11", infinity, infinity, 0, Nodo)
  63.  
  64. shinagawa.adyNodes = [(tamachi, 2.2), (osaki, 2)]
  65. osaki.adyNodes = [(shinagawa, 2), (gotanda, 0.9)]
  66. gotanda.adyNodes = [(osaki, 0.9), (meguro, 1.2)]
  67. meguro.adyNodes = [(gotanda, 1.2), (ebisu, 1.5)]
  68. ebisu.adyNodes = [(meguro, 1.5), (shibuya, 1.6)]
  69. shibuya.adyNodes = [(ebisu, 1.6), (harajuku, 1.2)]
  70. harajuku.adyNodes = [(shibuya, 1.2), (yoyogi, 1.5)]
  71. yoyogi.adyNodes = [(harajuku, 1.5), (shinjuku, 0.7), (sendagaya, 1.0), (shinjuku, 0.7)]
  72. shinjuku.adyNodes = [(yoyogi, 0.7), (shinokubo, 1.3), (ochanomizu, 1.5), (yoyogi, 0.7)]
  73. shinokubo.adyNodes = [(shinjuku, 1.3), (takadanobaba, 1.4)]
  74. takadanobaba.adyNodes = [(shinokubo, 1.4), (mejiro, 0.9)]
  75. mejiro.adyNodes = [(takadanobaba, 0.9), (ikebukuro, 1.2)]
  76. ikebukuro.adyNodes = [(mejiro, 0.9), (otsuka, 1.8)]
  77. otsuka.adyNodes = [(ikebukuro, 1.8), (sugamo, 1.1)]
  78. sugamo.adyNodes = [(otsuka, 1.1), (komagome, 0.7)]
  79. komagome.adyNodes = [(sugamo, 0.7), (tabata, 1.6)]
  80. tabata.adyNodes = [(komagome, 162), (nishinippori, 0.8)]
  81. nishinippori.adyNodes = [(tabata, 0.8), (nippori, 0.5)]
  82. nippori.adyNodes = [(nishinippori, 0.5), (Uguisudani, 1.1)]
  83. Uguisudani.adyNodes = [(nippori, 1.1), (ueno, 1.1)]
  84. ueno.adyNodes = [(Uguisudani, 1.1), (okachimachi, 0.6)]
  85. okachimachi.adyNodes = [(ueno, 0.6), (akihabara, 1)]
  86. akihabara.adyNodes = [(okachimachi, 1), (kanda, 0.7), (ochanomizu, 0.9)]
  87. kanda.adyNodes = [(akihabara, 0.7), (tokio, 1.3)]
  88. tokio.adyNodes = [(kanda, 1.3), (yurakucho, 0.8), (ochanomizu, 2.6)]
  89. yurakucho.adyNodes = [(tokio, 0.8), (shimbashi, 1.1)]
  90. shimbashi.adyNodes = [(yurakucho, 1.1), (hamamatsucho, 1.2)]
  91. hamamatsucho.adyNodes = [(shimbashi, 1.2), (tamachi, 1.5)]
  92. tamachi.adyNodes = [(hamamatsucho, 1.5), (shinagawa, 2.2)]
  93. ochanomizu.adyNodes = [(tokio, 2.6), (shinjuku, 1.5), (akihabara, 0.9), (suidobashi, 0.8)]
  94. suidobashi.adyNodes = [(ochanomizu, 0.8), (lidabashi, 0.9)]
  95. lidabashi.adyNodes = [(suidobashi, 0.9), (lichigaya, 1.5)]
  96. lichigaya.adyNodes = [(lidabashi, 1.5), (yotsuya, 0.8)]
  97. yotsuya.adyNodes = [(lichigaya, 0.8), (shinanomachi, 1, 3)]
  98. shinanomachi.adyNodes = [(yotsuya, 1.3), (sendagaya, 0.7)]
  99. sendagaya.adyNodes = [(shinanomachi, 0.7), (yoyogi, 1)]
  100.  
  101.  
  102. class Enviroment:
  103.     def __init__(self, nodos):
  104.         self.nodos = nodos
  105.  
  106.     def display(self):
  107.         for nodo in self.nodos:
  108.             nodo.display()
  109.  
  110.  
  111. env = Enviroment(
  112.     [shinagawa, osaki, gotanda, meguro, ebisu, shibuya, harajuku, yoyogi, shinjuku, shinokubo, takadanobaba, mejiro,
  113.      ikebukuro, otsuka, sugamo, komagome, tabata, nishinippori, nippori, Uguisudani, ueno, okachimachi, akihabara,
  114.      kanda, tokio, yurakucho,
  115.      shimbashi, hamamatsucho, tamachi, ochanomizu, suidobashi, lidabashi, lichigaya, yotsuya, shinanomachi, sendagaya])
  116.  
  117.  
  118. def reconstructPath(closedSet, current, totalPath):
  119.     if (current.father in closedSet):
  120.         if (current.father not in totalPath):
  121.             totalPath.append(current.father)
  122.             current = current.father
  123.     else:
  124.         return totalPath
  125.     return reconstructPath(closedSet, current, totalPath)
  126.  
  127.  
  128. def aStar(entrada, salida):
  129.     global env
  130.     for nodo in env.nodos:
  131.         nodo.h = distance.euclidean(nodo.pos, salida.pos)/ 100
  132.  
  133.     openSet = [entrada]
  134.     closedSet = [entrada]
  135.     pathValue = 0
  136.     entrada.g = 0
  137.     entrada.f = entrada.g + entrada.h
  138.  
  139.     while len(openSet) != 0:
  140.         current = openSet[0]
  141.         for nodo in openSet:
  142.             if nodo.f < current.f:
  143.                 current = nodo
  144.  
  145.         if current.equ(salida):
  146.             inverso = closedSet[::-1]
  147.             totalPath = [current]
  148.             return reconstructPath(inverso, current, totalPath)
  149.  
  150.         openSet.remove(current)
  151.  
  152.         for pair in current.adyNodes:
  153.             sum = pathValue + pair[1]
  154.             if (sum < pair[0].g and pair[0] not in closedSet):  # infinito al principio
  155.                 pair[0].father = current
  156.                 closedSet.append(pair[0])
  157.                 pair[0].g = sum
  158.                 pair[0].f = sum + pair[0].h
  159.                 if (pair[0] not in openSet):
  160.                     openSet.append(pair[0])
  161.  
  162.  
  163. path = []
  164. def setup():
  165.     path = aStar(tamachi, yoyogi)
  166.     anterior = path[0]
  167.     img = Image.open('yamanoteLine.jpg')
  168.     draw = ImageDraw.Draw(img)
  169.     for nodo in path:
  170.         draw.line((anterior.pos, nodo.pos), fill=(255, 255, 0), width=5)
  171.         print (nodo.nombre)
  172.         anterior = nodo
  173.     img.show()
  174.  
  175.  
  176.  
  177. setup()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top