Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #-*-coding:utf-8-*-
  2. __author__ = 'MarkShawn'
  3.  
  4.  
  5. from core import *
  6. from utils import *
  7.  
  8.  
  9. """
  10. ==================================================================
  11. 装饰器 log_each_step 用于打印某个调度过程中发生的一些数据改变量
  12. 装饰器 log_each_round 用于打印某个时间片结束时的一些数据标量
  13. ==================================================================
  14. """
  15.  
  16.  
  17. @log_each_step
  18. def schedule_step_1(time_round):
  19. """
  20. 第一步调度,标定道路上能移动或需要等待的小车
  21. 该步骤结束时,将全图发优先车
  22. """
  23. if np.mod(time_round, REFRESH_FREQUENCY) == 1 : # and Data.total_finished_ratio < 0.9
  24. c.update_roads() # 更新路权
  25.  
  26. for road in c.road_dict.values():
  27. for lane_seq in range(road.car_arr.shape[0]):
  28. res = road.mark_cars_on_this_lane_in_step_1(lane_seq)
  29. if res and road not in Data.roads_to_schedule_in_step_2:
  30. Data.roads_to_schedule_in_step_2.append(road)
  31.  
  32. c.load_cars(time_round=time_round, priority=True)
  33. c.depart_cars(time_round=time_round, priority=True)
  34.  
  35.  
  36. @dead_lock_check # 死锁检测装饰器,服务器运行时自动休眠
  37. def period_schedule_in_step_2(time_round):
  38. for road in list(Data.roads_to_schedule_in_step_2):
  39. while True:
  40. first_car = road.get_first_waiting_car()
  41.  
  42. if not first_car:
  43. Data.roads_to_schedule_in_step_2.remove(road)
  44. break
  45.  
  46. if c.car_is_conflicted(first_car) or not first_car.can_move_to_next_road:
  47. break
  48.  
  49. lane = first_car.lane
  50. first_car.move_to_next_road()
  51. Data.to_schedule -= 1
  52. road.move_cars_on_this_lane_in_step_2(lane)
  53. c.depart_cars(time_round=time_round, priority=True, road=road)
  54.  
  55.  
  56. @log_each_step
  57. def schedule_step_2(time_round):
  58. """
  59. 第二步调度,循环调度以使道路上所有的车移动至结束状态
  60. 该步骤中,只要有小车通过路口,将继续对该小车原所在路做一次优先车发车
  61. 该步骤结束后,将依次先全图发优先车,再全图发非优先车
  62. """
  63.  
  64. while Data.roads_to_schedule_in_step_2:
  65. try:
  66. period_schedule_in_step_2(time_round=time_round)
  67. except:
  68. raise evaluate_results(c, succeed=False)
  69.  
  70. c.depart_cars(time_round=time_round, priority=True)
  71. c.load_cars(time_round=time_round, priority=False)
  72. c.depart_cars(time_round=time_round, priority=False)
  73.  
  74.  
  75. @log_each_round
  76. def each_round(time_round):
  77. schedule_step_1(time_round)
  78. schedule_step_2(time_round)
  79.  
  80. scan_and_log_cars_each_time_round(c, logger)
  81.  
  82.  
  83. def main():
  84. while Data.total_finished != Data.CARS_CNT:
  85. Data.time_round += 1
  86. each_round(Data.time_round)
  87.  
  88. if ENABLE_CAR_ARRIVAL_TIME_FILE_WRITE:
  89. write_arrival_times(c, 'arrival_core.txt')
  90.  
  91. if ENABLE_EVALUATING_RESULTS_LOG:
  92. evaluate_results(c)
  93.  
  94.  
  95. if __name__ == '__main__':
  96. c = Core()
  97. c.init_data()
  98. c.pre_process_data()
  99. main()
  100. write_answer(c, ANSWER_PATH, including_preset=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement