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 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) print("-----------------------------------------------") print(f"Distribution: {random_distribution_to_hotspot}") amount_of_hotspots = len(hotspot_districts) hotspot_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)) 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() 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)) 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)) except Exception as e: logger.exception(e) print("HOTSPOT DISTRICTS:") #QUESTION 1: HOW CAN I PRINT INFO ABOUT ONLY WHAT HAPPENED IN THE 24`HOUR? for hour24 in hotspot_stats_per_hours: #QUESTION 2: LETS SAY I WANNA PRINT THE AVERAGE QUEUE LENGTH, HOW CAN I DO THAT? if ["hour"] == 24: print(hour24[24])