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. from Distributions import random_distribution_to_hotspot, cars_per_hour_to_hotspot_districts
  12. from Distributions import random_distribution_to_main, cars_per_hour_to_main_districts
  13. from Distributions import random_distribution_to_work, cars_per_hour_to_work_districts
  14. logger = logging.getLogger("my_logger")
  15. logger.setLevel(logging.DEBUG)
  16. fh = logging.FileHandler("main_log.log")
  17. #fh.setLevel(logging.DEBUG)
  18. fh.setLevel(logging.INFO) # hvis du kun vil ha denne i filen, så kommenter ut denne!
  19. sh = logging.StreamHandler()
  20. #sh.setLevel(logging.DEBUG)
  21. sh.setLevel(logging.INFO) # hvis du kun vil ha denne i filen, så kommenter ut denne!
  22. class CustomFormatter(logging.Formatter):
  23. def __init__(self, msg):
  24. logging.Formatter.__init__(self, msg)
  25. def format(self, record):
  26. try:
  27. msg = record.msg.split(':', 1)
  28. if len(msg) == 2:
  29. record.msg = '[%s]%s' % (msg[0], msg[1])
  30. except:
  31. pass
  32. return logging.Formatter.format(self, record)
  33. formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  34. fh.setFormatter(CustomFormatter('[%(levelname)s] %(message)s'))
  35. sh.setFormatter(CustomFormatter('[%(levelname)s] %(message)s'))
  36. #sh.setFormatter(formatter)
  37. logger.addHandler(sh)
  38. logger.addHandler(fh)
  39. print("-----------------------------------------------")
  40. print(f"Distribution: {random_distribution_to_hotspot}")
  41. amount_of_hotspots = len(hotspot_districts)
  42. hotspot_stats_per_hours = []
  43. """---------------------------------------The main simulation loop-------------------------------------"""
  44. try:
  45. #for days in range(0,5):
  46. #day_iterator = 0
  47. for hour in range(0, 25):
  48. hotspot_iterator = 0
  49. for hotspot in hotspot_districts:
  50. #logger.debug("Neighbours: {}".format(len(hotspot.my_neighbours)))
  51. logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_hotspot_districts[hotspot_iterator][hour]}:")
  52. for cars in range(0, cars_per_hour_to_hotspot_districts[hotspot_iterator][hour]):
  53. hotspot.add_car(random.choice(all_ev), 2)
  54. hotspot_iterator += 1
  55. logger.info(f"Report: for Hotspot district: {hotspot}")
  56. logger.info("LIVE STATUS: \n{}".format(hotspot.print_status_of_station()))
  57. #hotspot_info.update(hotspot.print_status_of_station())
  58. hotspot_stats_per_hours.append({
  59. "hour" : hour,
  60. "name": hotspot.station_name,
  61. "Cars in queue" : (len(hotspot.cars_waiting_in_queue)),
  62. "Rejected cars:": hotspot.rejected_cars,
  63. "Bailed cars" : hotspot.bailed_cars,
  64. })
  65. for minut in range(0, 60):
  66. cars_currently_charging_at_type1 = 0
  67. cars_currently_charging_at_type2 = 0
  68. cars_currently_charging_at_type3 = 0
  69. for charger1 in hotspot.type_1_all_chargers:
  70. if charger1.car:
  71. cars_currently_charging_at_type1 += 1
  72. charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
  73. for charger2 in hotspot.type_2_all_chargers:
  74. if charger2.car:
  75. cars_currently_charging_at_type2 += 1
  76. charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
  77. for charger3 in hotspot.type_3_all_chargers:
  78. if charger3.car:
  79. cars_currently_charging_at_type3 += 1
  80. charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
  81. total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
  82. for charger1 in hotspot.type_1_all_chargers:
  83. charger1.update_charger(float(total_charging_rate))
  84. for charger2 in hotspot.type_2_all_chargers:
  85. charger2.update_charger(float(total_charging_rate))
  86. for charger3 in hotspot.type_3_all_chargers:
  87. charger3.update_charger(float(total_charging_rate))
  88. hotspot.update_patience_for_cars_in_queue(all_ev)
  89. hotspot.give_me_charging_rate_of_station()
  90. hotspot.grab_car_from_waiting_queue_and_place_to_charger()
  91. work_district_iterator = 0
  92. for work in working_districts:
  93. #logger.debug("Neighbours: {}".format(len(work.my_neighbours)))
  94. logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_work_districts[work_district_iterator][hour]}:")
  95. for cars in range(0, cars_per_hour_to_work_districts[work_district_iterator][hour]):
  96. work.add_car(random.choice(all_ev), 2)
  97. work_district_iterator += 1
  98. logger.info(f"Report for Work district: {work}")
  99. logger.info("LIVE STATUS: \n{}".format(work.print_status_of_station()))
  100. work_districts_stats_per_hours.append({
  101. "hour" : hour,
  102. "name": work.station_name,
  103. "Cars in queue" : (len(work.cars_waiting_in_queue)),
  104. "Rejected cars:": work.rejected_cars,
  105. "Bailed cars" : work.bailed_cars,
  106. })
  107. for minut in range(0, 60):
  108. cars_currently_charging_at_type1 = 0
  109. cars_currently_charging_at_type2 = 0
  110. cars_currently_charging_at_type3 = 0
  111. for charger1 in work.type_1_all_chargers:
  112. if charger1.car:
  113. cars_currently_charging_at_type1 += 1
  114. charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
  115. for charger2 in work.type_2_all_chargers:
  116. if charger2.car:
  117. cars_currently_charging_at_type2 += 1
  118. charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
  119. for charger3 in work.type_3_all_chargers:
  120. if charger3.car:
  121. cars_currently_charging_at_type3 += 1
  122. charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
  123. total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
  124. for charger1 in work.type_1_all_chargers:
  125. charger1.update_charger(float(total_charging_rate))
  126. for charger2 in work.type_2_all_chargers:
  127. charger2.update_charger(float(total_charging_rate))
  128. for charger3 in work.type_3_all_chargers:
  129. charger3.update_charger(float(total_charging_rate))
  130. work.update_patience_for_cars_in_queue(all_ev)
  131. work.give_me_charging_rate_of_station()
  132. work.grab_car_from_waiting_queue_and_place_to_charger()
  133. main_district_iterator = 0
  134. for main in main_districts:
  135. #logger.debug("Neighbours: {}".format(len(main.my_neighbours)))
  136. logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_main_districts[main_district_iterator][hour]}")
  137. for cars in range(0, cars_per_hour_to_main_districts[main_district_iterator][hour]):
  138. main.add_car(random.choice(all_ev), 2)
  139. main_district_iterator += 1
  140. logger.info(f"Report for Main district: {main}")
  141. logger.info("LIVE STATUS: \n{}".format(main.print_status_of_station()))
  142. main_districts_stats_per_hours.append({
  143. "hour" : hour,
  144. "name": main.station_name,
  145. "Cars in queue" : (len(main.cars_waiting_in_queue)),
  146. "Rejected cars:": main.rejected_cars,
  147. "Bailed cars" : main.bailed_cars,
  148. })
  149. for minut in range(0, 60):
  150. cars_currently_charging_at_type1 = 0
  151. cars_currently_charging_at_type2 = 0
  152. cars_currently_charging_at_type3 = 0
  153. for charger1 in main.type_1_all_chargers:
  154. if charger1.car:
  155. cars_currently_charging_at_type1 += 1
  156. charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
  157. for charger2 in main.type_2_all_chargers:
  158. if charger2.car:
  159. cars_currently_charging_at_type2 += 1
  160. charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
  161. for charger3 in main.type_3_all_chargers:
  162. if charger3.car:
  163. cars_currently_charging_at_type3 += 1
  164. charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
  165. total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
  166. for charger1 in main.type_1_all_chargers:
  167. charger1.update_charger(float(total_charging_rate))
  168. for charger2 in main.type_2_all_chargers:
  169. charger2.update_charger(float(total_charging_rate))
  170. for charger3 in main.type_3_all_chargers:
  171. charger3.update_charger(float(total_charging_rate))
  172. except Exception as e:
  173. logger.exception(e)
  174. print("HOTSPOT DISTRICTS:") #QUESTION 1: HOW CAN I PRINT INFO ABOUT ONLY WHAT HAPPENED IN THE 24`HOUR?
  175. for hour24 in hotspot_stats_per_hours: #QUESTION 2: LETS SAY I WANNA PRINT THE AVERAGE QUEUE LENGTH, HOW CAN I DO THAT?
  176. if ["hour"] == 24:
  177. print(hour24[24])
  178.