Emania

Untitled

Mar 1st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. import heapq
  2. import sys
  3. from collections import namedtuple
  4. import random
  5.  
  6. import numpy as np
  7.  
  8. Job = namedtuple('Job', ['id', 'x1', 'y1', 'x2', 'y2', 's', 'f', 'rew', 'deadline_odjezd'])
  9.  
  10. BONUS = float('nan')
  11.  
  12. Kr = 1
  13. Kd = 100
  14. Krand = 100
  15.  
  16. # 15278973
  17. # 15557121
  18. # 15273737 # rand
  19.  
  20. # high bonus
  21. # 21478343
  22. # 11601343 - 1, 1, 1
  23. # 11601343 - 1, 1, 1
  24. # 7309176 10, -10, 0
  25.  
  26.  
  27.  
  28. # C
  29. # 15257904
  30. #
  31.  
  32.  
  33. class Car:
  34. def __init__(self, car_id):
  35. self.id = car_id
  36. self.time_free = 0
  37. self.x = 0
  38. self.y = 0
  39. self.tasks = []
  40. self.score = 0
  41.  
  42. def add_task(self, task_id):
  43. self.tasks.append(task_id)
  44.  
  45. def __lt__(self, other):
  46. return self.time_free < other.time_free
  47.  
  48. def __str__(self):
  49. return "CAR {}, time free: {}, x: {}, y: {}, score: {}, tasks: {}".format(self.id, self.time_free, self.x,
  50. self.y,
  51. self.score, str(self.tasks))
  52.  
  53.  
  54. def printSol(cars, fn):
  55. fn = fn.split(".")[-2] + ".out"
  56. with open(fn, 'w') as f:
  57. for c in cars:
  58. # Write ID of car
  59. f.write(str(len(c.tasks)))
  60. f.write(" ")
  61.  
  62. # Write out all tasks in order that they were appened
  63. for task in c.tasks:
  64. f.write(str(task.id))
  65. f.write(" ")
  66.  
  67. # Newline
  68. f.write("\n")
  69.  
  70.  
  71. def distance(x1, y1, x2, y2):
  72. return abs(x1 - x2) + abs(y1 - y2)
  73.  
  74.  
  75. def select_task(car, tasks):
  76. cx, cy = car.x, car.y
  77. time = car.time_free
  78.  
  79. best_cost = -float("inf")
  80. best_i = -1
  81. best_bonus = 0
  82. best_arrival_time = float("nan")
  83. best_task = None
  84. for i, task in enumerate(tasks):
  85. if task:
  86. if task.deadline_odjezd < time:
  87. tasks[i] = None
  88. else:
  89. dist = distance(cx, cy, task.x1, task.y1)
  90. arrival = time + dist
  91. bonus = BONUS if arrival <= task.s else 0
  92. waiting = max(task.s - arrival, 0)
  93. cost = Kr*task.rew - Kd*dist + bonus - waiting + random.random()*Krand
  94. # print task.rew, dist, bonus, waiting
  95. if cost > best_cost:
  96. best_task = task
  97. best_bonus = bonus
  98. best_cost = cost
  99. best_i = i
  100. best_arrival_time = time + dist + waiting + task.rew
  101.  
  102. if not best_task:
  103. return -1
  104. car.x, car.y = best_task.x2, best_task.y2
  105. car.time_free = best_arrival_time
  106. car.score += best_task.rew + best_bonus
  107. car.add_task(best_task)
  108. return best_i
  109.  
  110.  
  111. def print_cars(cars):
  112. print("\n".join(list(map(str, cars))))
  113.  
  114.  
  115. def solve(input_path):
  116. global BONUS
  117. f = open(input_path, "r")
  118. head = f.readline()
  119. R, C, F, N, B, T = map(int, head.split())
  120. # print("Row {}, col {}, cars {}, tasks {}, bonus {}, end time {}".format(R, C, F, N, B, T))
  121. BONUS = int(B)
  122.  
  123. cars = []
  124.  
  125. for i in range(F):
  126. cars.append(Car(i + 1))
  127.  
  128. tasks = []
  129. for i, job in enumerate(f):
  130. h = [i]
  131. h.extend(map(int, job.split()))
  132. hh = h.copy()
  133. h.append(0)
  134. h.append(0)
  135. j = Job(*h)
  136. rew = abs(j.x1 - j.x2) + abs(j.y1 - j.y2)
  137. hh.append(rew)
  138. hh.append(j.f - rew)
  139. j = Job(*hh)
  140. tasks.append(j)
  141.  
  142. # print(R, C, F, N, B, T)
  143. # print(tasks)
  144.  
  145. heapq.heapify(cars)
  146.  
  147. while True:
  148. # print("##next it##")
  149. # print_cars(cars)
  150. car = heapq.heappop(cars)
  151. # print("selected car", car.id)
  152.  
  153. task_pos = select_task(car, tasks)
  154. heapq.heappush(cars, car)
  155.  
  156. if task_pos < 0:
  157. break
  158. tasks[task_pos] = None
  159.  
  160. if car.time_free > T:
  161. break
  162.  
  163. cars = sorted(cars, key=lambda x: x.id)
  164.  
  165. # print_cars(cars)
  166.  
  167. return cars
  168.  
  169.  
  170. def eval(filename, N):
  171. scores = []
  172. best_score = -1
  173. best_carlist = []
  174.  
  175. for i in range(N):
  176. print("%d/ %d" % (i, N))
  177. # Get solution
  178. carlist = solve(filename)
  179.  
  180. # Evaluate solution
  181. score = 0
  182. for c in carlist:
  183. score += c.score
  184.  
  185. # Append to score list for statistics
  186. scores.append(score)
  187.  
  188. # Update best score and carlist
  189. if score > best_score:
  190. best_score = score
  191. best_carlist = carlist
  192.  
  193. # Print out statistics
  194. scores = np.array(scores)
  195. print("Results --- min: {}, max: {}, mean: {}, std: {}".format(np.min(scores), np.max(scores), np.mean(scores),
  196. np.std(scores)))
  197.  
  198. # Write solution to file for submission
  199. printSol(best_carlist, filename)
  200.  
  201.  
  202. if __name__ == "__main__":
  203. eval(sys.argv[1], 1)
Advertisement
Add Comment
Please, Sign In to add comment