Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. from math import sqrt, ceil
  2.  
  3. class Area:
  4. time = 0
  5. rows = 0
  6. columns = 0
  7. drones = []
  8. deadline = 0
  9. max_load = 0
  10. product_weights = []
  11. warehouses = []
  12. orders = []
  13. def __str__(self):
  14. return "Area:"\
  15. + "rows = " + str(self.rows) + "\n"\
  16. + "columns = " + str(self.columns) + "\n"\
  17. + "drones = " + str(self.drones) + "\n"\
  18. + "deadline = " + str(self.deadline) + "\n"\
  19. + "max_load = " + str(self.max_load) + "\n"\
  20. + "product_weights = " + str(self.product_weights) + "\n"\
  21. + "warehouses = " + str(self.warehouses) + "\n"\
  22. + "orders = " + str(self.orders) + "\n"
  23.  
  24. def timeAdvance(self):
  25. min_adv = self.deadline
  26. for drone in self.drones:
  27. if drone.timeready < min_adv:
  28. min_adv = drone.timeready
  29. self.time = min_adv
  30.  
  31.  
  32. class drone:
  33. id = 0
  34. def __init__(self, num):
  35. self.id = num
  36. items = [] #item ids, duplicates allowed
  37. coords = []
  38. timeready = 0
  39. def __str__(self):
  40. return "drone:\n"\
  41. + "id: " + str(self.id) + "\n"\
  42. + "coords: " + str(self.coords) + "\n"
  43.  
  44. class warehouse:
  45. id = 0
  46. def __init__(self, num):
  47. self.id = num
  48. coords = []
  49. products = []
  50. def __str__(self):
  51. return "warehouse:\n"\
  52. + "coords: " + str(self.coords) + "\n"\
  53. + "products: " + str(self.products) + "\n"
  54.  
  55. def __repr__(self):
  56. return self.__str__()
  57.  
  58. class order:
  59. def __init__(self, num):
  60. self.id = num
  61. coords = []
  62. items = []
  63. def __str__(self):
  64. return "order:\n"\
  65. + "coords: " + str(self.coords) + "\n"\
  66. + "items: " + str(self.items) + "\n"
  67.  
  68. def __repr__(self):
  69. return self.__str__()
  70.  
  71.  
  72.  
  73. def read_problem(filename):
  74. area = Area()
  75. f = open(filename, "r")
  76. f_str = f.read()
  77. lines = f_str.splitlines()
  78. for i in range(len(lines)):
  79. lines[i] = lines[i].split()
  80.  
  81. area.rows = int(lines[0][0])
  82. area.columns = int(lines[0][1])
  83. for i in range(int(lines[0][2])):
  84. area.drones.append(drone(i))
  85. area.deadline = int(lines[0][3])
  86. area.max_load = int(lines[0][4])
  87.  
  88. for i in range(int(lines[1][0])):
  89. area.product_weights.append(int(lines[2][i]))
  90.  
  91. for i in range(int(lines[3][0])):
  92. linenr = 4 + (i * 2)
  93. w = warehouse(i)
  94. coords = [0,0]
  95. coords[0] = int(lines[linenr][0])
  96. coords[1] = int(lines[linenr][1])
  97. w.coords = coords
  98.  
  99. prods = []
  100. for i in range(int(lines[1][0])):
  101. prods.append(int(lines[linenr + 1][i]))
  102. w.products = prods
  103.  
  104. area.warehouses.append(w)
  105.  
  106. base = 4 + (2 * int(lines[3][0]))
  107. for i in range(int(lines[base][0])):
  108. linenr = base + 1 + (i * 3)
  109. ord = order(i)
  110. coords = [int(lines[linenr][0]), int(lines[linenr][1])]
  111. ord.coords = coords
  112.  
  113. items = []
  114. for i in range(int(lines[1][0])):
  115. items.append(0)
  116. for i in range(int(lines[linenr + 1][0])):
  117. items[int(lines[linenr + 2][i])] += 1
  118. ord.items = items
  119.  
  120. area.orders.append(ord)
  121.  
  122. for _drone in area.drones:
  123. _drone.coords = area.warehouses[0].coords
  124. #log(area)
  125.  
  126. return area
  127.  
  128. def distance(coo1, coo2):
  129. return ceil(sqrt((coo1[0]-coo2[0])**2 + (coo1[1]-coo2[1])**2))
  130.  
  131.  
  132.  
  133. def calculateMinOrderLength(o, area):
  134. chosen = []
  135. total = 0
  136. for item in range(len(o.items)):
  137. dists = []
  138. for wh in area.warehouses:
  139. if wh.products[item] > 0:
  140. dists.append((distance(o.coords, wh.coords), wh.products[item], wh.id))
  141. dists.sort(reverse = True) #smallest last
  142. needed = o.items[item]
  143. while needed > 0:
  144. temp = dists.pop()
  145.  
  146. subtr = min(temp[1], needed)
  147. # append (dist, whsID, itemID, count
  148. chosen.append((temp[0], temp[2], item, subtr))
  149. needed -= subtr
  150. # count dist
  151. total += temp[0]
  152. return (total, chosen, o.id)
  153.  
  154.  
  155. def calculateShortestOrder(area):
  156. orderscpy = []
  157. for o in area.orders:
  158. if o:
  159. orderscpy.append(calculateMinOrderLength(o, area))
  160. log(orderscpy)
  161. return min(orderscpy)
  162.  
  163. def findClosestDrone(whs, area):
  164. maxdist = area.rows + area.columns
  165. bestdrone = False
  166. for drone in area.drones:
  167. dist = distance(drone.coords, area.warehouses[whs].coords)
  168. if dist < maxdist and drone.timeready <= area.time:
  169. maxdist = dist
  170. bestdrone = drone
  171. return bestdrone
  172.  
  173. def write_solution(filename, sol):
  174. f = open(filename, "w")
  175. f.write(str(len(sol)) + "\n")
  176. for i in sol:
  177. f.write(i + "\n")
  178. f.close()
  179.  
  180. def log(s):
  181. #print(s)
  182. pass
  183.  
  184. if __name__ == "__main__":
  185.  
  186. commands = []
  187. problem_name = "busy_day"
  188. #problem_name = "redundancy"
  189. #problem_name = "mother_of_all_warehouses"
  190. area = read_problem(problem_name + ".in")
  191.  
  192. #while len(area.orders) > 0:
  193. for jef in range(len(area.orders)):
  194. print(str(area.orders.count(False)) + "/" + str(len(area.orders)))
  195. # log(area.orders)
  196. sor = calculateShortestOrder(area)
  197. log("sor = " + str(sor))
  198. for (dist, whsid, itemid, count) in sor[1]:
  199. drone = findClosestDrone(whsid, area)
  200. if not drone:
  201. log("time advance")
  202. area.timeAdvance()
  203. drone = findClosestDrone(whsid, area)
  204.  
  205. log(drone)
  206. # 0L123 (Command to drone 0, load at warehouse 1 products of product type 2, three of them.
  207.  
  208. commands.append(str(drone.id) + " " +\
  209. "L " +\
  210. str(whsid) + " " +\
  211. str(itemid) + " " +\
  212. str(count)
  213. )
  214. # 0D123 (Command to drone 0, deliver for order 1 items of product type 2, three of them.
  215. commands.append(str(drone.id) + " " +\
  216. "D " +\
  217. str(sor[2]) + " " +\
  218. str(itemid) + " " +\
  219. str(count)
  220. )
  221.  
  222.  
  223. area.warehouses[whsid].products[itemid] -= 1
  224. if area.warehouses[whsid].products[itemid] < 0:
  225. log("wtf")
  226. drone.timeready += distance(drone.coords, area.warehouses[whsid].coords)\
  227. + distance(area.warehouses[whsid].coords, area.orders[sor[2]].coords) + 2
  228. drone.coords = area.orders[sor[2]].coords
  229. area.orders[sor[2]] = False
  230.  
  231.  
  232.  
  233. print(commands)
  234. write_solution(problem_name + ".out", commands)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement