-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
347 lines (292 loc) · 13 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import sys
import csv
from itertools import product
from functools import reduce
from random import random
from collections import Counter
class Node(object):
def __init__(self, x, y):
self.__x = x
self.__y = y
self.__items = []
self.__accessible_items = []
self.__distance = []
self.__path = []
def add_item(self, item_id):
self.__items.append(item_id)
def get_items(self):
return self.__items
def is_occupied(self):
return len(self.__items) != 0
def add_accessible_items(self, ids):
self.__accessible_items += ids
def get_accessible_items(self):
return self.__accessible_items
def set_shortest_paths(self, distance, path):
self.__distance = distance
self.__path = path
def has_distance(self):
return self.__distance
def get_distance(self, x, y):
# print(x, y)
return self.__distance[x][y]
def has_path(self):
return self.__path
def get_path(self, x, y):
if self.__path[x][y] == (-1, -1):
return []
cur = (x, y)
Str = lambda x: str((float(x[0]) / accuracy, float(x[1]) / accuracy))
cur_path = Str(self.__path[x][y])
while cur != (self.__x, self.__y):
cur = self.__path[cur[0]][cur[1]]
cur_path = Str(cur) + cur_path
return cur_path
class Grid(object):
def __init__(self, csv_file_name, accuracy, xLen="0", yLen="0"):
self.__accuracy = accuracy
# read csv file
(self.__ids, self.__xs, self.__ys) = self.__read_csv(csv_file_name)
self.__xLen = max(max(self.__xs), self.f2i(xLen)) + 1
self.__yLen = max(max(self.__ys), self.f2i(yLen)) + 1
self.__nodes = [[Node(i, j) for j in range(self.__yLen)] for i in range(self.__xLen)]
# initiate
self.__initiate()
self.__start_x = 0
self.__start_y = 0
self.__end_x = 0
self.__end_y = 0
# convert float string to int
def f2i(self, s):
return int(round(float(s) * self.__accuracy))
def set_start(self, x, y):
(self.__start_x, self.__start_y) = (x, y)
def get_start(self):
return self.__start_x, self.__start_y
def set_end(self, x, y):
(self.__end_x, self.__end_y) = (x, y)
def get_end(self):
return self.__end_x, self.__end_y
def get_location(self, id):
index = self.__ids.index(id)
return self.__xs[index], self.__ys[index]
def find_item(self, id):
return self.find_item_from(id, self.__start_x, self.__start_y)
def find_item_from(self, id, from_x, from_y):
if not self.__nodes[from_x][from_y].has_path():
self.__find_path(from_x, from_y)
if not self.__nodes[from_x][from_y].has_path():
return "Non accessible."
index = self.__ids.index(id)
neighbor_x = [self.__xs[index] - 1, self.__xs[index] + 1]
distances = list(map(lambda x: self.__nodes[from_x][from_y].get_distance(x, self.__ys[index]), neighbor_x))
index = distances.index(min(distances))
return self.__nodes[from_x][from_y].get_path(neighbor_x[index], self.__ys[index])
def path_between(self, from_x, from_y, to_x, to_y):
if not self.__nodes[from_x][from_y].has_path():
self.__find_path(from_x, from_y)
if not self.__nodes[from_x][from_y].has_path():
return "Non accessible."
return self.__nodes[from_x][from_y].get_path(to_x, to_y)
def distance_between(self, from_coord, to_coord):
(from_x, from_y) = from_coord
(to_x, to_y) = to_coord
if not self.__nodes[from_x][from_y].has_distance():
self.__find_path(from_x, from_y)
if not self.__nodes[from_x][from_y].has_distance():
return "Non accessible."
# print(from_coord, " in distance_between")
return self.__nodes[from_x][from_y].get_distance(to_x, to_y)
def is_occupied(self, x, y):
return self.__nodes[x][y].is_occupied();
def __read_csv(self, csv_file_name):
item_table = zip(*csv.reader(open(csv_file_name)))
return map(lambda x, y: list(map(y, x)), item_table, [lambda x: x, self.f2i, self.f2i])
def __initiate(self):
self.__locate_items()
self.__find_item_access()
def __locate_items(self):
items = zip(*(self.__ids, self.__xs, self.__ys))
for i in items:
self.__nodes[i[1]][i[2]].add_item(i[0])
def __find_item_access(self):
for i in range(self.__xLen):
for j in range(self.__yLen):
accessible_neighbor = self.__get_accessible_neighbor(i, j)
accessible_items = map(lambda x: self.__nodes[x[0]][x[1]].get_items(), accessible_neighbor)
self.__nodes[i][j].add_accessible_items(reduce(lambda x, y: x + y, accessible_items))
def __get_neighbor(self, x, y):
neighbor = [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
return filter(lambda x: self.__is_inside_grid(x[0], x[1]), neighbor)
def __get_accessible_neighbor(self, x, y):
neighbor = [(x - 1, y), (x + 1, y)]
return filter(lambda x: self.__is_inside_grid(x[0], x[1]), neighbor)
def __is_inside_grid(self, x, y):
return 0 <= x < self.__xLen and 0 <= y < self.__yLen
def __find_path(self, x, y):
if self.__nodes[x][y].is_occupied():
return None
distance = [[-1 for j in range(self.__yLen)] for i in range(self.__xLen)]
path = [[(-1, -1) for j in range(self.__yLen)] for i in range(self.__xLen)]
distance[x][y] = 0
changed = 1
i_range = max(map(lambda k: k[0] + k[1], product([x, self.__xLen - x], [y, self.__yLen - y])))
# calculate the distance from the starting point to the four accessible neighbors of (_x,_y)
def neighbor_distance(x, y):
# unoccupied and inside the map
if not self.__is_inside_grid(x, y) or self.__nodes[x][y].is_occupied() or distance[x][y] < 0:
return 0
_changed = 0
# four neighbor squares of (_x,_y)
neighbors = self.__get_neighbor(x, y)
# neighbors unoccupied and inside the grid
valid = lambda x, y: self.__is_inside_grid(x, y) and not self.__nodes[x][y].is_occupied()
neighbors = filter(lambda k: valid(k[0], k[1]), neighbors)
# calculating distance
for neighbor in neighbors:
if distance[x][y] + 1 < distance[neighbor[0]][neighbor[1]] or distance[neighbor[0]][neighbor[1]] < 0:
_changed = 1
distance[neighbor[0]][neighbor[1]] = distance[x][y] + 1
path[neighbor[0]][neighbor[1]] = (x, y)
return _changed
while changed != 0:
changed = 0
for i in range(i_range):
for j in range(max(i - self.__xLen, 0), min(i + 1, self.__yLen)):
cur = product([x + (i - j), x - (i - j)], [y + j, y - j])
changed += sum(map(lambda k: neighbor_distance(k[0], k[1]), cur))
self.__nodes[x][y].set_shortest_paths(distance, path)
class Task(object):
grid = None
def __init__(self, task):
# print(task)
self.task = task
self.original = range(1, len(task) + 1)
self.optimal = range(1, len(task) + 1)
def get_side_location(i):
loc = self.grid.get_location(i)
new_loc = (loc[0] - 1, loc[1])
if self.grid.is_occupied(new_loc[0], new_loc[1]):
return (loc[0] + 1, loc[1])
return new_loc
self.location = [self.grid.get_start()] + list(map(get_side_location, task)) + [self.grid.get_end()]
self.size = len(task) + 2
self.distance = [[-1 for i in range(self.size)] for j in range(self.size)]
self.__get_distance()
self.original_len = float(self.__get_path_length(self.original)) / accuracy
self.optimal_len = float(self.__get_path_length(self.optimal)) / accuracy
def optimize(self):
all = set(self.optimal)
pre = self.optimal
pre_len = self.distance
pop_size = 50
prt = 0.8
t = 0.9
cur = [[] for i in range(pop_size)]
intcur = [[] for i in range(pop_size)]
for i in range(pop_size):
for j in range(self.size - 2):
# print(i,"curlen",len(cur))
cur[i].append(random() * (self.size - 2) + 1)
# print(len(intcur[i]),len(cur[i]))
intcur[i] = list(map(lambda x: int(x), cur[i]))
count = Counter(intcur[i])
perm = [k for k in all if k not in intcur[i]]
for j in range(self.size - 2):
if len(perm) == 0:
break
if count[intcur[i][j]] > 1:
rand = int(random() * len(perm))
cur[i][j] = float(perm[rand])
perm.remove(perm[rand])
count[intcur[i][j]] -= 1
intcur[i] = list(map(lambda x: int(x), cur[i]))
cur.append(list(map(lambda x: float(x), pre)))
intcur.append(pre)
pop_size += 1
distance = list(map(self.__get_path_length, intcur))
self.optimal = intcur[distance.index(min(distance))]
self.optimal_len = distance[distance.index(min(distance))]
while True:
pre = self.optimal
pre_len = self.optimal_len
for i in range(pop_size):
for j in range(self.size - 2):
# print(i, j, len(cur), len(self.optimal))
Prt = int(random() * prt * 2)
cur[i][j] = cur[i][j] + (self.optimal[j] - cur[i][j]) * Prt * t
# print(len(intcur[i]),len(cur[i]))
intcur[i] = list(map(lambda x: int(x), cur[i]))
count = Counter(intcur[i])
perm = [k for k in all if k not in intcur[i]]
for j in range(self.size - 2):
if len(perm) == 0:
break
if count[intcur[i][j]] > 1:
rand = int(random() * len(perm))
cur[i][j] = perm[rand]
perm.remove(perm[rand])
count[intcur[i][j]] -= 1
intcur[i] = list(map(lambda x: int(x), cur[i]))
distance = list(map(self.__get_path_length, intcur))
self.optimal = intcur[distance.index(min(distance))]
self.optimal_len = distance[distance.index(min(distance))]
if self.optimal_len - pre_len < 1 / (10 * accuracy):
self.optimal_len = float(self.optimal_len) / accuracy
break
def output(self):
Str = lambda x: str((float(x[0]) / accuracy, float(x[1]) / accuracy))
s = "##Worker Start Location##\n" + Str(self.grid.get_start())
s += "\n## Worker End Location##\n" + Str(self.grid.get_end())
s += "\n##Original Parts Order##\n" + str(list(map(lambda x: self.task[x - 1], self.original)))
s += "\n##Optimized Parts Order##\n" + str(list(map(lambda x: self.task[x - 1], self.optimal)))
s += "\n##Original Parts Total Distance##\n" + str(self.original_len)
s += "\n##Optimized Parts Total Distance##\n" + str(self.optimal_len)
return s
def __get_distance(self):
for i in range(self.size):
for j in range(self.size):
# print(self.location[i], self.location[j], "in __get_distance")
self.distance[i][j] = self.grid.distance_between(self.location[i], self.location[j])
def __get_path_length(self, path):
total = 0
tmp = [0] + list(path) + [self.size - 1]
for i in range(self.size - 1):
total += self.distance[tmp[i]][tmp[i + 1]]
return total
# accuracy of map grid
if len(sys.argv) >= 2:
accuracy = int(sys.argv[1])
else:
accuracy = 50
while True:
start_x = input("Hello User, where is your worker?\nx:")
start_y = input("y:")
end_x = input("What is your worker's end location?\nx:")
end_y = input("y:")
if Task.grid is None:
Task.grid = Grid('warehouse-grid.csv', accuracy, max(start_x, end_x), max(start_y, end_y))
start_x = Task.grid.f2i(start_x)
start_y = Task.grid.f2i(start_y)
end_x = Task.grid.f2i(end_x)
end_y = Task.grid.f2i(end_y)
Task.grid.set_start(start_x, start_y)
Task.grid.set_end(end_x, end_y)
alter = input("File input or manual input? (f/m): ").lower()
if alter == "m":
task = Task((input("Hello User, what items would you like to pick? (split with spaces)\n")).split())
task.optimize()
print(task.output())
if alter == "f":
i_file = input("Please list file of orders to be processed: ")
o_file = input("Please list output file: ")
tasks = map(Task, csv.reader(open(i_file)))
s = ""
for i in tasks:
i.optimize()
s += i.output() + "\n"
f = open(o_file)
print(s, f)
foo = input("Continue? (Y/N): ").lower()
if (foo=="n"):
break