-
- import numpy as np
- import random
- import matplotlib.pyplot as plt
- import logging
-
- import csv
- import itertools
-
- from ElectricCars import all_ev
- from district_connections import main_districts, hotspot_districts, working_districts
- from collections import defaultdict
-
- # 30 november kl 15:33
- from Distributions import random_distribution_to_hotspot, cars_per_hour_to_hotspot_districts
- from Distributions import random_distribution_to_main, cars_per_hour_to_main_districts
- from Distributions import random_distribution_to_work, cars_per_hour_to_work_districts
-
-
- logger = logging.getLogger("my_logger")
- logger.setLevel(logging.DEBUG)
- fh = logging.FileHandler("main_log.log")
- #fh.setLevel(logging.DEBUG)
- fh.setLevel(logging.INFO) # hvis du kun vil ha denne i filen, så kommenter ut denne!
- sh = logging.StreamHandler()
- #sh.setLevel(logging.DEBUG)
- sh.setLevel(logging.INFO) # hvis du kun vil ha denne i filen, så kommenter ut denne!
- class CustomFormatter(logging.Formatter):
- def __init__(self, msg):
- logging.Formatter.__init__(self, msg)
-
- def format(self, record):
- try:
- msg = record.msg.split(':', 1)
- if len(msg) == 2:
- record.msg = '[%s]%s' % (msg[0], msg[1])
- except:
- pass
- return logging.Formatter.format(self, record)
- formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
- fh.setFormatter(CustomFormatter('[%(levelname)s] %(message)s'))
- sh.setFormatter(CustomFormatter('[%(levelname)s] %(message)s'))
- #sh.setFormatter(formatter)
- logger.addHandler(sh)
- logger.addHandler(fh)
-
-
- "------------------------------------------"
-
- plot_top5_stations_with_highest_average_queue_length = []
- plot_top5_stations_with_highest_rejected_cars = []
- plot_top5_stations_with_highest_stated_cars = []
-
-
- "-------------------------"
-
-
-
-
- "--------------------Function to create a CSV file----------------------"
-
- #with open('results_from_simulation.csv', 'w') as csvfile:
- # filewriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)
- # filewriter.writerow(['Hour','Station-name', 'Visited EV`s', 'Entered EV`s','Chargers-used', 'Queue-length', 'Rejected-cars', 'Stated-cars'])
- #filewriter.writerow(['1', '2', '3','4','5','6','7'])
-
-
- #quotechar='|',
- "-----------------------------------------------------------------------"
-
- print("-----------------------------------------------")
- print(f"Distribution: {random_distribution_to_hotspot}")
- amount_of_hotspots = len(hotspot_districts)
-
- print("-----------------------------------------------")
- print(f"Distribution: {random_distribution_to_main}")
- amount_of_main_districts = len(main_districts)
-
- print("-----------------------------------------------")
- print(f"Distribution: {random_distribution_to_work}")
- amount_of_work_districts = len(working_districts)
-
-
- #hotspot_stations = {"hour" : [],"name":[], "queue length":[], "Rejected cars:":[], "Balked cars": []}
- #hotspot_names = []
- #for names in hotspot_districts:
- # hotspot_names.append(names.station_name)
-
- #hotspot_stations.update(["name"].append(hotspot_names))
-
-
- #stats[0] = {"station": "", "queue": A, "rej_cars": 3, "bailed_cars":4}
- "----------------------------------------"
- hotspot_stats_per_hours = []
- work_districts_stats_per_hours = []
- main_districts_stats_per_hours = []
- "----------------------------------------"
-
-
-
- """---------------------------------------The main simulation loop-------------------------------------"""
-
- try:
- #for days in range(0,5):
-
- #day_iterator = 0
-
- for hour in range(0, 25):
-
-
- hotspot_iterator = 0
- for hotspot in hotspot_districts:
-
-
- #logger.debug("Neighbours: {}".format(len(hotspot.my_neighbours)))
- logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_hotspot_districts[hotspot_iterator][hour]}:")
- for cars in range(0, cars_per_hour_to_hotspot_districts[hotspot_iterator][hour]):
- hotspot.add_car(random.choice(all_ev), 2)
-
-
- hotspot_iterator += 1
- logger.info(f"Report: for Hotspot district: {hotspot}")
- logger.info("LIVE STATUS: \n{}".format(hotspot.print_status_of_station()))
- #hotspot_info.update(hotspot.print_status_of_station())
-
- hotspot_stats_per_hours.append({
- "hour": hour,
- "name": hotspot.station_name,
- "cars_in_queue": (len(hotspot.cars_waiting_in_queue)),
- "rejected_cars:": hotspot.rejected_cars,
- "bailed_cars": hotspot.bailed_cars,
- })
-
-
-
-
- for minut in range(0, 60):
- cars_currently_charging_at_type1 = 0
- cars_currently_charging_at_type2 = 0
- cars_currently_charging_at_type3 = 0
-
-
- for charger1 in hotspot.type_1_all_chargers:
- if charger1.car:
- cars_currently_charging_at_type1 += 1
- charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
- for charger2 in hotspot.type_2_all_chargers:
- if charger2.car:
- cars_currently_charging_at_type2 += 1
- charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
- for charger3 in hotspot.type_3_all_chargers:
- if charger3.car:
- cars_currently_charging_at_type3 += 1
- charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
- total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
- for charger1 in hotspot.type_1_all_chargers:
- charger1.update_charger(float(total_charging_rate))
- for charger2 in hotspot.type_2_all_chargers:
- charger2.update_charger(float(total_charging_rate))
- for charger3 in hotspot.type_3_all_chargers:
- charger3.update_charger(float(total_charging_rate))
-
-
- #print("--------------------------------------")
- #print(f"amount of cars at type 1: {cars_currently_charging_at_type1}")
- #print(f"amount of cars at type 2: {cars_currently_charging_at_type2}")
- #print(f"amount of cars at type 3: {cars_currently_charging_at_type3}")
- #print(f"Current charging rate type1: {charging_rate_type1}")
- #print(f"Current charging rate type2: {charging_rate_type2}")
- #print(f"Current charging rate type3: {charging_rate_type3}")
- #print(f"So total charging rate should be: {total_charging_rate}")
-
-
-
- hotspot.update_patience_for_cars_in_queue(all_ev)
- hotspot.give_me_charging_rate_of_station()
- hotspot.grab_car_from_waiting_queue_and_place_to_charger()
-
- #writer.writerow(hotspot.amount_of_cars_that_entered_the_station)
-
-
-
- work_district_iterator = 0
- for work in working_districts:
- #logger.debug("Neighbours: {}".format(len(work.my_neighbours)))
-
- logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_work_districts[work_district_iterator][hour]}:")
- for cars in range(0, cars_per_hour_to_work_districts[work_district_iterator][hour]):
- work.add_car(random.choice(all_ev), 2)
-
- work_district_iterator += 1
- logger.info(f"Report for Work district: {work}")
- logger.info("LIVE STATUS: \n{}".format(work.print_status_of_station()))
-
- work_districts_stats_per_hours.append({
- "hour" : hour,
- "name": work.station_name,
- "cars_in_queue" : (len(work.cars_waiting_in_queue)),
- "rejected_cars:": work.rejected_cars,
- "bailed_cars" : work.bailed_cars,
- })
-
-
- for minut in range(0, 60):
- cars_currently_charging_at_type1 = 0
- cars_currently_charging_at_type2 = 0
- cars_currently_charging_at_type3 = 0
-
- for charger1 in work.type_1_all_chargers:
- if charger1.car:
- cars_currently_charging_at_type1 += 1
- charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
- for charger2 in work.type_2_all_chargers:
- if charger2.car:
- cars_currently_charging_at_type2 += 1
- charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
- for charger3 in work.type_3_all_chargers:
- if charger3.car:
- cars_currently_charging_at_type3 += 1
- charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
- total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
- for charger1 in work.type_1_all_chargers:
- charger1.update_charger(float(total_charging_rate))
- for charger2 in work.type_2_all_chargers:
- charger2.update_charger(float(total_charging_rate))
- for charger3 in work.type_3_all_chargers:
- charger3.update_charger(float(total_charging_rate))
-
-
- #print("--------------------------------------")
- #print(f"amount of cars at type 1: {cars_currently_charging_at_type1}")
- #print(f"amount of cars at type 2: {cars_currently_charging_at_type2}")
- #print(f"amount of cars at type 3: {cars_currently_charging_at_type3}")
- #print(f"Current charging rate type1: {charging_rate_type1}")
- #print(f"Current charging rate type2: {charging_rate_type2}")
- #print(f"Current charging rate type3: {charging_rate_type3}")
- #print(f"So total charging rate should be: {total_charging_rate}")
- work.update_patience_for_cars_in_queue(all_ev)
- work.give_me_charging_rate_of_station()
- work.grab_car_from_waiting_queue_and_place_to_charger()
-
-
- main_district_iterator = 0
- for main in main_districts:
-
- #logger.debug("Neighbours: {}".format(len(main.my_neighbours)))
-
- logger.info(f"hour {hour}: cars at this hour: {cars_per_hour_to_main_districts[main_district_iterator][hour]}")
- for cars in range(0, cars_per_hour_to_main_districts[main_district_iterator][hour]):
- main.add_car(random.choice(all_ev), 2)
-
- main_district_iterator += 1
- logger.info(f"Report for Main district: {main}")
- logger.info("LIVE STATUS: \n{}".format(main.print_status_of_station()))
-
- main_districts_stats_per_hours.append({
- "hour" : hour,
- "name": main.station_name,
- "cars_in_queue" : (len(main.cars_waiting_in_queue)),
- "rejected_cars:": main.rejected_cars,
- "bailed cars" : main.bailed_cars,
- })
-
- for minut in range(0, 60):
- cars_currently_charging_at_type1 = 0
- cars_currently_charging_at_type2 = 0
- cars_currently_charging_at_type3 = 0
-
- for charger1 in main.type_1_all_chargers:
- if charger1.car:
- cars_currently_charging_at_type1 += 1
- charging_rate_type1 = cars_currently_charging_at_type1 * 0.02
- for charger2 in main.type_2_all_chargers:
- if charger2.car:
- cars_currently_charging_at_type2 += 1
- charging_rate_type2 = cars_currently_charging_at_type2 * 0.02
- for charger3 in main.type_3_all_chargers:
- if charger3.car:
- cars_currently_charging_at_type3 += 1
- charging_rate_type3 = cars_currently_charging_at_type3 * 0.02
- total_charging_rate = (charging_rate_type1 + charging_rate_type2 + charging_rate_type3)
- for charger1 in main.type_1_all_chargers:
- charger1.update_charger(float(total_charging_rate))
- for charger2 in main.type_2_all_chargers:
- charger2.update_charger(float(total_charging_rate))
- for charger3 in main.type_3_all_chargers:
- charger3.update_charger(float(total_charging_rate))
-
- #print("--------------------------------------")
- #print(f"amount of cars at type 1: {cars_currently_charging_at_type1}")
- #print(f"amount of cars at type 2: {cars_currently_charging_at_type2}")
- #print(f"amount of cars at type 3: {cars_currently_charging_at_type3}")
- #print(f"Current charging rate type1: {charging_rate_type1}")
- #print(f"Current charging rate type2: {charging_rate_type2}")
- #print(f"Current charging rate type3: {charging_rate_type3}")
- #print(f"So total charging rate should be: {total_charging_rate}")
-
- main.update_patience_for_cars_in_queue(all_ev)
- main.give_me_charging_rate_of_station()
- main.grab_car_from_waiting_queue_and_place_to_charger()
-
- #logger.info(f"days {days}: hhh: {cars_per_hour_to_main_districts[day_iterator][days]}")
- #day_iterator += 1
-
- except Exception as e:
- logger.exception(e)
-
- hotspot_average_queue_length = {}
-
-
- for print_stats_vertically in hotspot_stats_per_hours:
- print("----------------------")
- print(print_stats_vertically)
- print("----------------------")
-
- #HERE I WANT TO COUNT ALL THE QUEUES PER HOUR
-
- #sum(houer['cars_in_queue':] for houer in houers) if this is a list of dicts
-