spacepaste

  1.  
  2. import numpy as np
  3. import random
  4. import matplotlib.pyplot as plt
  5. import logging
  6. import csv
  7. import itertools
  8. from ElectricCars import all_ev
  9. from district_connections import main_districts, hotspot_districts, working_districts
  10. from collections import defaultdict
  11. # 30 november kl 15:33
  12. from Distributions import random_distribution_to_hotspot, cars_per_hour_to_hotspot_districts
  13. from Distributions import random_distribution_to_main, cars_per_hour_to_main_districts
  14. from Distributions import random_distribution_to_work, cars_per_hour_to_work_districts
  15. logger = logging.getLogger("my_logger")
  16. logger.setLevel(logging.DEBUG)
  17. fh = logging.FileHandler("main_log.log")
  18. #fh.setLevel(logging.DEBUG)
  19. fh.setLevel(logging.INFO) # hvis du kun vil ha denne i filen, så kommenter ut denne!
  20. sh = logging.StreamHandler()
  21. #sh.setLevel(logging.DEBUG)
  22. sh.setLevel(logging.INFO) # hvis du kun vil ha denne i filen, så kommenter ut denne!
  23. class CustomFormatter(logging.Formatter):
  24. def __init__(self, msg):
  25. logging.Formatter.__init__(self, msg)
  26. def format(self, record):
  27. try:
  28. msg = record.msg.split(':', 1)
  29. if len(msg) == 2:
  30. record.msg = '[%s]%s' % (msg[0], msg[1])
  31. except:
  32. pass
  33. return logging.Formatter.format(self, record)
  34. formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  35. fh.setFormatter(CustomFormatter('[%(levelname)s] %(message)s'))
  36. sh.setFormatter(CustomFormatter('[%(levelname)s] %(message)s'))
  37. #sh.setFormatter(formatter)
  38. logger.addHandler(sh)
  39. logger.addHandler(fh)
  40. "------------------------------------------"
  41. plot_top5_stations_with_highest_average_queue_length = []
  42. plot_top5_stations_with_highest_rejected_cars = []
  43. plot_top5_stations_with_highest_stated_cars = []
  44. "-------------------------"
  45. "--------------------Function to create a CSV file----------------------"
  46. #with open('results_from_simulation.csv', 'w') as csvfile:
  47. # filewriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)
  48. # filewriter.writerow(['Hour','Station-name', 'Visited EV`s', 'Entered EV`s','Chargers-used', 'Queue-length', 'Rejected-cars', 'Stated-cars'])
  49. #filewriter.writerow(['1', '2', '3','4','5','6','7'])
  50. #quotechar='|',
  51. "-----------------------------------------------------------------------"
  52. print("-----------------------------------------------")
  53. print(f"Distribution: {random_distribution_to_hotspot}")
  54. amount_of_hotspots = len(hotspot_districts)
  55. print("-----------------------------------------------")
  56. print(f"Distribution: {random_distribution_to_main}")
  57. amount_of_main_districts = len(main_districts)
  58. print("-----------------------------------------------")
  59. print(f"Distribution: {random_distribution_to_work}")
  60. amount_of_work_districts = len(working_districts)
  61. #hotspot_stations = {"hour" : [],"name":[], "queue length":[], "Rejected cars:":[], "Balked cars": []}
  62. #hotspot_names = []
  63. #for names in hotspot_districts:
  64. # hotspot_names.append(names.station_name)
  65. #hotspot_stations.update(["name"].append(hotspot_names))
  66. #stats[0] = {"station": "", "queue": A, "rej_cars": 3, "bailed_cars":4}
  67. "----------------------------------------"
  68. hotspot_stats_per_hours = []
  69. work_districts_stats_per_hours = []
  70. main_districts_stats_per_hours = []
  71. "----------------------------------------"
  72. """---------------------------------------The main simulation loop-------------------------------------"""
  73. try:
  74. #for days in range(0,5):
  75. #day_iterator = 0
  76. for hour in range(0, 25):
  77. hotspot_iterator = 0
  78. for hotspot in hotspot_districts:
  79. #logger.debug("Neighbours: {}".format(len(hotspot.my_neighbours)))
  80. logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_hotspot_districts[hotspot_iterator][hour]}:")
  81. for cars in range(0, cars_per_hour_to_hotspot_districts[hotspot_iterator][hour]):
  82. hotspot.add_car(random.choice(all_ev), 2)
  83. hotspot_iterator += 1
  84. logger.info(f"Report: for Hotspot district: {hotspot}")
  85. logger.info("LIVE STATUS: \n{}".format(hotspot.print_status_of_station()))
  86. #hotspot_info.update(hotspot.print_status_of_station())
  87. hotspot_stats_per_hours.append({
  88. "hour": hour,
  89. "name": hotspot.station_name,
  90. "cars_in_queue": (len(hotspot.cars_waiting_in_queue)),
  91. "rejected_cars:": hotspot.rejected_cars,
  92. "bailed_cars": hotspot.bailed_cars,
  93. })
  94. for minut in range(0, 60):
  95. cars_currently_charging_at_type1 = 0
  96. cars_currently_charging_at_type2 = 0
  97. cars_currently_charging_at_type3 = 0
  98. for charger1 in hotspot.type_1_all_chargers:
  99. if charger1.car:
  100. cars_currently_charging_at_type1 += 1
  101. charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
  102. for charger2 in hotspot.type_2_all_chargers:
  103. if charger2.car:
  104. cars_currently_charging_at_type2 += 1
  105. charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
  106. for charger3 in hotspot.type_3_all_chargers:
  107. if charger3.car:
  108. cars_currently_charging_at_type3 += 1
  109. charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
  110. total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
  111. for charger1 in hotspot.type_1_all_chargers:
  112. charger1.update_charger(float(total_charging_rate))
  113. for charger2 in hotspot.type_2_all_chargers:
  114. charger2.update_charger(float(total_charging_rate))
  115. for charger3 in hotspot.type_3_all_chargers:
  116. charger3.update_charger(float(total_charging_rate))
  117. #print("--------------------------------------")
  118. #print(f"amount of cars at type 1: {cars_currently_charging_at_type1}")
  119. #print(f"amount of cars at type 2: {cars_currently_charging_at_type2}")
  120. #print(f"amount of cars at type 3: {cars_currently_charging_at_type3}")
  121. #print(f"Current charging rate type1: {charging_rate_type1}")
  122. #print(f"Current charging rate type2: {charging_rate_type2}")
  123. #print(f"Current charging rate type3: {charging_rate_type3}")
  124. #print(f"So total charging rate should be: {total_charging_rate}")
  125. hotspot.update_patience_for_cars_in_queue(all_ev)
  126. hotspot.give_me_charging_rate_of_station()
  127. hotspot.grab_car_from_waiting_queue_and_place_to_charger()
  128. #writer.writerow(hotspot.amount_of_cars_that_entered_the_station)
  129. work_district_iterator = 0
  130. for work in working_districts:
  131. #logger.debug("Neighbours: {}".format(len(work.my_neighbours)))
  132. logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_work_districts[work_district_iterator][hour]}:")
  133. for cars in range(0, cars_per_hour_to_work_districts[work_district_iterator][hour]):
  134. work.add_car(random.choice(all_ev), 2)
  135. work_district_iterator += 1
  136. logger.info(f"Report for Work district: {work}")
  137. logger.info("LIVE STATUS: \n{}".format(work.print_status_of_station()))
  138. work_districts_stats_per_hours.append({
  139. "hour" : hour,
  140. "name": work.station_name,
  141. "cars_in_queue" : (len(work.cars_waiting_in_queue)),
  142. "rejected_cars:": work.rejected_cars,
  143. "bailed_cars" : work.bailed_cars,
  144. })
  145. for minut in range(0, 60):
  146. cars_currently_charging_at_type1 = 0
  147. cars_currently_charging_at_type2 = 0
  148. cars_currently_charging_at_type3 = 0
  149. for charger1 in work.type_1_all_chargers:
  150. if charger1.car:
  151. cars_currently_charging_at_type1 += 1
  152. charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
  153. for charger2 in work.type_2_all_chargers:
  154. if charger2.car:
  155. cars_currently_charging_at_type2 += 1
  156. charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
  157. for charger3 in work.type_3_all_chargers:
  158. if charger3.car:
  159. cars_currently_charging_at_type3 += 1
  160. charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
  161. total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
  162. for charger1 in work.type_1_all_chargers:
  163. charger1.update_charger(float(total_charging_rate))
  164. for charger2 in work.type_2_all_chargers:
  165. charger2.update_charger(float(total_charging_rate))
  166. for charger3 in work.type_3_all_chargers:
  167. charger3.update_charger(float(total_charging_rate))
  168. #print("--------------------------------------")
  169. #print(f"amount of cars at type 1: {cars_currently_charging_at_type1}")
  170. #print(f"amount of cars at type 2: {cars_currently_charging_at_type2}")
  171. #print(f"amount of cars at type 3: {cars_currently_charging_at_type3}")
  172. #print(f"Current charging rate type1: {charging_rate_type1}")
  173. #print(f"Current charging rate type2: {charging_rate_type2}")
  174. #print(f"Current charging rate type3: {charging_rate_type3}")
  175. #print(f"So total charging rate should be: {total_charging_rate}")
  176. work.update_patience_for_cars_in_queue(all_ev)
  177. work.give_me_charging_rate_of_station()
  178. work.grab_car_from_waiting_queue_and_place_to_charger()
  179. main_district_iterator = 0
  180. for main in main_districts:
  181. #logger.debug("Neighbours: {}".format(len(main.my_neighbours)))
  182. logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_main_districts[main_district_iterator][hour]}")
  183. for cars in range(0, cars_per_hour_to_main_districts[main_district_iterator][hour]):
  184. main.add_car(random.choice(all_ev), 2)
  185. main_district_iterator += 1
  186. logger.info(f"Report for Main district: {main}")
  187. logger.info("LIVE STATUS: \n{}".format(main.print_status_of_station()))
  188. main_districts_stats_per_hours.append({
  189. "hour" : hour,
  190. "name": main.station_name,
  191. "cars_in_queue" : (len(main.cars_waiting_in_queue)),
  192. "rejected_cars:": main.rejected_cars,
  193. "bailed cars" : main.bailed_cars,
  194. })
  195. for minut in range(0, 60):
  196. cars_currently_charging_at_type1 = 0
  197. cars_currently_charging_at_type2 = 0
  198. cars_currently_charging_at_type3 = 0
  199. for charger1 in main.type_1_all_chargers:
  200. if charger1.car:
  201. cars_currently_charging_at_type1 += 1
  202. charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
  203. for charger2 in main.type_2_all_chargers:
  204. if charger2.car:
  205. cars_currently_charging_at_type2 += 1
  206. charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
  207. for charger3 in main.type_3_all_chargers:
  208. if charger3.car:
  209. cars_currently_charging_at_type3 += 1
  210. charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
  211. total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
  212. for charger1 in main.type_1_all_chargers:
  213. charger1.update_charger(float(total_charging_rate))
  214. for charger2 in main.type_2_all_chargers:
  215. charger2.update_charger(float(total_charging_rate))
  216. for charger3 in main.type_3_all_chargers:
  217. charger3.update_charger(float(total_charging_rate))
  218. #print("--------------------------------------")
  219. #print(f"amount of cars at type 1: {cars_currently_charging_at_type1}")
  220. #print(f"amount of cars at type 2: {cars_currently_charging_at_type2}")
  221. #print(f"amount of cars at type 3: {cars_currently_charging_at_type3}")
  222. #print(f"Current charging rate type1: {charging_rate_type1}")
  223. #print(f"Current charging rate type2: {charging_rate_type2}")
  224. #print(f"Current charging rate type3: {charging_rate_type3}")
  225. #print(f"So total charging rate should be: {total_charging_rate}")
  226. main.update_patience_for_cars_in_queue(all_ev)
  227. main.give_me_charging_rate_of_station()
  228. main.grab_car_from_waiting_queue_and_place_to_charger()
  229. #logger.info(f"days {days}: hhh: {cars_per_hour_to_main_districts[day_iterator][days]}")
  230. #day_iterator += 1
  231. except Exception as e:
  232. logger.exception(e)
  233. hotspot_average_queue_length = {}
  234. for print_stats_vertically in hotspot_stats_per_hours:
  235. print("----------------------")
  236. print(print_stats_vertically)
  237. print("----------------------")
  238. #HERE I WANT TO COUNT ALL THE QUEUES PER HOUR
  239. #sum(houer['cars_in_queue':] for houer in houers) if this is a list of dicts
  240.