-
Notifications
You must be signed in to change notification settings - Fork 0
/
TurtleGUI_TSP.py
235 lines (200 loc) · 7.21 KB
/
TurtleGUI_TSP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import turtle
from TSP import Distance as DistanceOfTwoPoints
from heuristicCalculation import heuristic_cal
# init = [0, 0]
# goal = [len(grid) - 1, len(grid[0]) - 1]
# goal = [4, 1]
delta = [[-1, 0], # up
[0, -1], # left
[1, 0], # down
[0, 1]] # right
delta_name = ['^', '<', 'v', '>']
# goal_init=[[0,0],[0,5],[4,3]]
# goal_init=[]
cost = 1
counter = 0
# screen1=turtle.Screen()
t = turtle.Pen()
dot_d = 20
new_list = []
RadiusOfWheel= 3.5
def draw(grid, goal_init, rows, columns, speed):
turtle.clear()
turtle.reset()
my_start = (goal_init[0][1] * dot_d, -goal_init[0][0] * dot_d)
turtle.penup()
turtle.setx(my_start[0])
turtle.sety(my_start[1])
turtle.pendown()
t.clear()
t.reset()
t.penup()
t.speed(speed)
print "dot " + str(turtle.position())
for i in range(rows):
for j in range(columns):
# for i in range(len(grid)):
# for j in range(len(grid[0])):
if grid[i][j] == 1:
t.dot(10, "blue")
t.forward(dot_d)
else:
t.dot(10, "black")
t.forward(dot_d)
t.back(dot_d * (j + 1))
t.right(90)
t.forward(dot_d)
t.left(90)
def set_initial(goal_init, turtle):
my_start = (goal_init[0][1] * dot_d, -goal_init[0][0] * dot_d)
turtle.penup()
turtle.setx(my_start[0])
turtle.sety(my_start[1])
turtle.pendown()
def search(init, goal, speed, grid, f_degree, b_degree, r_degree, l_degree, initialPoint):
global speedGlobal, gridGlobal, f_degreeGlobal, b_degreeGlobal, r_degreeGlobal, l_degreeGlobal, initalLocation
speedGlobal=speed
gridGlobal=grid
f_degreeGlobal=f_degree
b_degreeGlobal=b_degree
r_degreeGlobal=r_degree
l_degreeGlobal=l_degree
initalLocation=initialPoint
heuristic = heuristic_cal(grid)
turtle.speed(speed)
closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
closed[init[0]][init[1]] = 1
expand = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
action = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
policy = [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
x = init[0]
y = init[1]
g = 0
h = heuristic[x][y]
f = g + h
open = [[f, g, h, x, y]]
found = False # flag that is set when search complete
resign = False # flag set if we can't find expand
count = 0
# print 'initial open list:'
# for i in range(len(open)):
# print ' ',open[i]
# print '-----------------'
while found is False and resign is False:
# check if we still have elements on the open list
if len(open) == 0: # if open list is empty, nothing to expand
resign = True
print 'fail'
else: # if there are still elements
# remove node form list
open.sort() # sort elements in increasing order
# print 'open' +str(open)
open.reverse()
# print 'open' +str(open)
next = open.pop() # pop the elements with smallest g value
# print 'open' +str(open)
# print 'take list item'
# print next
x = next[3]
y = next[4]
g = next[1]
# print 'x '+str(next[1]) +',y '+str(next[2])+',g '+str(next[0])
expand[x][y] = count
count += 1
# check the goal
if x == goal[0] and y == goal[1]:
found = True
# print next
else:
# expand winning elements and add to new open list
for i in range(len(delta)):
x2 = x + delta[i][0]
y2 = y + delta[i][1]
if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid[0]):
if closed[x2][y2] == 0 and grid[x2][y2] == 0: # closed[][] x2 and y2 is not yet checked/// grid[][] if no obstacle in here
g2 = g + cost
h2 = heuristic[x2][y2]
f2 = g2 + h2
open.append([f2, g2, h2, x2, y2])
# print 'append list item'
# print [g2, x2, y2]
closed[x2][y2] = 1
action[x2][y2] = i
# for i in range(len(expand)):
# print expand[i]
# policy = [[' ' for row in range(len(grid))] for col in range(len(grid[0]))]
x = goal[0]
y = goal[1]
print "Goal", x, y
print "Init", init[0], init[1]
policy[x][y] = '*' # 4,5
list = []
currentLocationList = []
while x != init[0] or y != init[1]:
# print "Currrent x, y: "+str(x)+" "+str(y)
global currentLocation
currentLocation = [x, y] # edit 3
currentLocationList.append(currentLocation) # edit 3
# print delta[action[x][y]]
x2 = x - delta[action[x][y]][0]
y2 = y - delta[action[x][y]][1]
policy[x2][y2] = delta_name[action[x][y]]
list.append(policy[x2][y2])
x = x2
y = y2
for i in range(len(policy)):
print policy[i]
currentLocationList.append(init)# edit 3
currentLocationList.reverse()# edit 3
# print "currentLocation", currentLocationList # edit 3
list.reverse()
# print "Path List from", init, "to", goal, list
print list
new_list.append(list)
# print "-----------alllist---------"
# print new_list
# print new_list[0]
# ----------------------encoder tick-----------------------------
# -----------handle this---------------------
try:
if DistanceOfTwoPoints(init, goal) != [] or x != init[0] or y != init[1]:
print "Distance from", init, "to", goal, DistanceOfTwoPoints(init, goal)
from CompassControl import key_input
key_input(list, f_degree, b_degree, r_degree, l_degree, initialPoint, currentLocationList) # edit 4
except:
print ""
# for items, previous_item in zip(list, list[1:]):
for items in list:
turtle.setheading(90)
if items == "^":
turtle.setheading(90)
turtle.forward(20)
# print turtle.position()
elif items == ">":
turtle.setheading(90)
turtle.right(90)
turtle.forward(20)
# turtle.forward(20)
# print turtle.position()
elif items == "<":
turtle.setheading(90)
turtle.left(90)
turtle.forward(20)
# print turtle.position()
elif items == "v":
turtle.setheading(90)
turtle.left(90)
turtle.left(90)
turtle.forward(20)
# print turtle.position()
# print "-----------alllist---------"
# print new_list
# turtle.mainloop()
def stopAgent():
try:
print "currentLocationinTutleGUI_TSP:", currentLocation
print "FirstLocationinTutleGUI_TSP",initalLocation
from CompassControl import stop as stop
stop(currentLocation, speedGlobal, gridGlobal, f_degreeGlobal, b_degreeGlobal, r_degreeGlobal, l_degreeGlobal,initalLocation)
except:
print "Found Earlier..............."