I am trying to simulate the charging of electric vehicles by a mobile charger, based on the charging urgency of the vehicles (EDF and Baseline are the two algorithms which determine the urgency of the charge requester vehicles). I am unable to integrate the vehicles from SUMO into my Python code. I am not understanding how to do it. The code so far looks like this:
'''
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import optparse
import random
import xml.etree.ElementTree as ET
import operator # get max value of a dict
import math # sqrt()
from datetime import datetime
from sumolib import checkBinary
import traci
import random
import sys
from sumolib import net # get junction's nearby edge
from helper.prior import *
from helper.helperFuncs import *
# Log System:
# Choose keywords from following to customize the log printing.
# {"core"}
# comments = {}
comments = {"core"}
# we need to import python modules from the $SUMO_HOME/tools directory
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
dayuanSUMOpath = os.path.join("/usr", "local", "Cellar", "sumo", "1.14.1", "share", "sumo", "tools")
sys.path.append(dayuanSUMOpath)
print("PATH:", sys.path)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
netfile = 'netfiles/jawad.grid.net.xml'
net_xml = net.readNet(netfile)
def run():
if "core" in comments:
print("\nexecute the TraCI control loop.\n")
step = 0
# Prepare variables used for all cycles
# get all TLs
# allTLIds = traci.trafficlight.getIDList() # all traffic lights id
allTLIds = AllTLIDs # AllTLIDs from prior
# AllEdgeIDs from prior
if "core" in comments:
print("\n\nProgram starts. \n\nThere are", len(allTLIds), "traffic lights totally.\n") # =12
# while traci.simulation.getTime() < 200: # unit is second. Set a short time for testing
while traci.simulation.getMinExpectedNumber() > 0: # run until all v have arrived
traci.simulationStep() # forward one step
if "core" in comments:
print("\n\nCurrent step: ", step, "\n\n")
allVehIDs = []
# iterate each edge
for edge_id in AllEdgeIDs:
allVehIDs_inThisEdge = traci.edge.getLastStepVehicleIDs(edge_id)
print(edge_id, allVehIDs_inThisEdge)
allVehIDs.extend(allVehIDs_inThisEdge)
print("All veh id in current SUMO step: ", allVehIDs)
# Create/update Vehicle objects using SUMO API calls
vehicles = [Vehicle(veh_id) for veh_id in allVehIDs]
for vehicle in vehicles:
vehicle.update_parameters()
print("veh id: ", vehicle.veh_id, " Speed: ", vehicle.speed)
print("veh id: ", vehicle.veh_id, " Neighbors: ", vehicle.neighbors)
print("veh id: ", vehicle.veh_id, " ElectricityConsumption: ", vehicle.electricity_consumption)
print("veh id: ", vehicle.veh_id, " Distance from starting point: ", vehicle.distance_from_start)
print("veh id: ", vehicle.veh_id, " Position: ", vehicle.position)
print("veh id: ", vehicle.veh_id, " RoadID: ", vehicle.road_id)
print("veh id: ", vehicle.veh_id, " Route: ", vehicle.route)
# Class to represent a Vehicle
class Vehicle:
def __init__(self, veh_id):
self.veh_id = veh_id
self.speed = 0
self.neighbors = []
self.electricity_consumption = 0
self.distance_from_start = 0
self.position = (0, 0)
self.road_id = ""
self.route = []
def update_parameters(self):
self.speed = traci.vehicle.getSpeed(self.veh_id)
self.neighbors = traci.vehicle.getNeighbors(self.veh_id, 10)
self.electricity_consumption = traci.vehicle.getElectricityConsumption(self.veh_id)
self.distance_from_start = traci.vehicle.getDistance(self.veh_id)
self.position = traci.vehicle.getPosition(self.veh_id)
self.road_id = traci.vehicle.getRoadID(self.veh_id)
self.route = traci.vehicle.getRoute(self.veh_id)
# Class to represent a Drone
class Drone:
def __init__(self, lat, lon, speed, soc):
self.lat = lat
self.lon = lon
self.speed = speed
self.soc = soc
def calculate_distance(self, lat1, lon1, lat2, lon2):
# Radius of the Earth in kilometers
earth_radius = 6371
# Convert coordinates to radians
lat1_rad = math.radians(lat1)
lon1_rad = math.radians(lon1)
lat2_rad = math.radians(lat2)
lon2_rad = math.radians(lon2)
# Haversine formula
dlat = lat2_rad - lat1_rad
dlon = lon2_rad - lon1_rad
a = math.sin(dlat / 2) ** 2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2) ** 2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
# Distance in kilometers
distance = earth_radius * c
return distance
def calculate_rdv_loc(self, vehicle, charging_range):
# Calculate the speed of the vehicle based on its state of charge
vehicle_speed = vehicle.speed / 10
# Calculate the distance between the vehicle and the drone
distance_vehicle_drone = self.calculate_distance(vehicle.position[0], vehicle.position[1], self.lat, self.lon)
# Calculate the time for the drone to reach the vehicle
time_to_reach = distance_vehicle_drone / self.speed
# Calculate the distance the vehicle will travel during the drone's travel time
distance_vehicle_travel = time_to_reach * vehicle_speed
# Calculate the RDV-LOC based on the selected vehicle and charging range
rdvloc_lat = vehicle.position[0] + (distance_vehicle_travel / 111) # 1 degree of latitude is approximately 111 kilometers
rdvloc_lon = vehicle.position[1]
return rdvloc_lat, rdvloc_lon
def charge(self, vehicle_index, rdv_loc):
# Perform charging logic here
pass
# Class to represent the Baseline algorithm
class Baseline:
def __init__(self, drone, charging_range, charging_stations):
self.drone = drone
self.charging_range = charging_range
self.charging_stations = charging_stations
def charge(self, vehicles):
# Logic for the Baseline algorithm
pass
# Class to represent the EDF algorithm
class EDF:
def __init__(self, drone, charging_range):
self.drone = drone
self.charging_range = charging_range
def charge(self, vehicles):
# Logic for the EDF algorithm
pass
# Connect to SUMO and start the simulation
traci.close()
sumoBinary = checkBinary('sumo')
traci.start([sumoBinary, "-c",
"C:/Users/Sadi M Jawad Ahsan/OneDrive - UMBC/UMBC Studies/Spring_2024/EV_charging_project/sookyoung_paper/code/jawad.grid.sumocfg"])
traci.simulationStep()
# Retrieve the IDs of the vehicles generated by SUMO
all_vehicle_ids = traci.vehicle.getIDList()
# Create/update Vehicle objects using SUMO API calls
vehicles = [Vehicle(vehicle_id) for vehicle_id in all_vehicle_ids]
# Run the simulation loop
run()
# Disconnect from SUMO
traci.close()
sys.stdout.flush()
'''