spacepaste

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