sumo icon indicating copy to clipboard operation
sumo copied to clipboard

How to set the electric vehicle random initial charge

Open just-wynot opened this issue 3 years ago • 8 comments

I know that I can set the initial power through the following parameters,

but I want to generate different random initial power for different electric vehicles. For example, how should I write it in traci

**SUMO-version:**1.16

just-wynot avatar Jul 12 '23 05:07 just-wynot

The current battery capacity of the vehicle can be overwritten by TraCI. You can read all device parameters in TraCI using traci.vehicle.getParameter and write some by traci.vehicle.setParameter (see docs). Essentially you have to write:

traci.vehicle.setParameter(vehicleID, "device.battery.actualBatteryCapacity", value)

m-kro avatar Jul 12 '23 06:07 m-kro

i firstly write param key="device.battery.actualBatteryCapacity" value="1000" In the definition of vehicle type in rou.xml,then i write traci.vehicle.setParameter(vehicle_id, "device.battery.actualBatteryCapacity",str(initial_soc)) in the python,but i found it doesn't work.

just-wynot avatar Jul 12 '23 06:07 just-wynot

"it doesn't work" isn't enough to provide more help - please be more specific. Do you encounter error messages?

m-kro avatar Jul 12 '23 06:07 m-kro

"it doesn't work" isn't enough to provide more help - please be more specific. Do you encounter error messages?

import traci
import numpy as np

# Set the mean and standard deviation of the normal distribution
mean = 1000  # Assuming mean to be 1000
std_dev = 100  # Assuming standard deviation to be 100

# Start SUMO with the GUI and connect to it
traci.start(["sumo-gui", "-c", "test.sumocfg"])

# Get the list of vehicle IDs
vehicle_ids = traci.vehicle.getIDList()

# Generate a normally distributed initial state of charge (SOC) for each vehicle and set it
# Ensure that the initial SOC is within the valid range of [0, maximum_battery_capacity]
for vehicle_id in vehicle_ids:
   maximum_battery_capacity = traci.vehicle.getTypeParameter(vehicle_id, "maximumBatteryCapacity")
   initial_soc = np.random.normal(mean, std_dev)  # Generate a random number from a normal distribution
   initial_soc = max(initial_soc, 0)  # Ensure initial SOC is not less than 0
   initial_soc = min(initial_soc, maximum_battery_capacity)  # Ensure initial SOC does not exceed maximum capacity
   traci.vehicle.setTypeParameter(vehicle_id, "maximumBatteryCapacity", str(maximum_battery_capacity))
   traci.vehicle.setParameter(vehicle_id, "device.battery.actualBatteryCapacity", str(initial_soc))

# Start the simulation
for step in range(100):
   traci.simulationStep()

# Close the connection to SUMO
traci.close()

this is my python code,I found out from the output meter that the initial charge was still not random

just-wynot avatar Jul 12 '23 07:07 just-wynot

Well you can only update the vehicles' batteries once the vehicles have departed. Probably your vehicle_ids list is empty...! You could run a first simulation step and then patch the vehicles which departed at t=0.

m-kro avatar Jul 12 '23 08:07 m-kro

Well you can only update the vehicles' batteries once the vehicles have departed. Probably your list is empty...! You could run a first simulation step and then patch the vehicles which departed at t=0.vehicle_ids

thanks very much,i will try again!

just-wynot avatar Jul 12 '23 09:07 just-wynot

好吧,您只能在车辆离开后更新车辆的电池。可能你的列表是空的...!您可以运行第一个模拟步骤,然后修补在 t=0 时离开的车辆。vehicle_ids

import traci import numpy as np

Set the mean and standard deviation of the normal distribution

mean = 1000 # Assuming mean to be 1000 std_dev = 100 # Assuming standard deviation to be 100

Start SUMO with the GUI and connect to it

traci.start(["sumo-gui", "-c", "77.sumocfg"])

Get the list of vehicle IDs

vehicle_ids = traci.vehicle.getIDList()

Generate a normally distributed initial state of charge (SOC) for each vehicle and set it

Ensure that the initial SOC is within the valid range of [0, maximum_battery_capacity]

for vehicle_id in vehicle_ids: maximum_battery_capacity = traci.vehicle.getTypeParameter(vehicle_id, "device.battery.maximumBatteryCapacity") initial_soc = np.random.normal(mean, std_dev) # Generate a random number from a normal distribution initial_soc = max(initial_soc, 0) # Ensure initial SOC is not less than 0 initial_soc = min(initial_soc, maximum_battery_capacity) # Ensure initial SOC does not exceed maximum capacity traci.vehicle.setTypeParameter(vehicle_id, "device.battery.actualBatteryCapacity", str(initial_soc))

Start the simulation

for step in range(100): traci.simulationStep() vehicle_ids = traci.vehicle.getIDList() # Update the list of vehicle IDs print("step:", step) for vehicle_id in vehicle_ids: # Get the actual SOC of the vehicle actual_soc = traci.vehicle.getParameter(vehicle_id, "device.battery.actualBatteryCapacity") print("vehicle id:", vehicle_id, "actual SOC:", actual_soc)

Close the connection to SUMO

traci.close() Running result: step: 0 vehicle id: 0 actual SOC: 1000.00 vehicle id: 1 actual SOC: 1000.00 step: 1 vehicle id: 0 actual SOC: 999.60 vehicle id: 1 actual SOC: 999.60 step: 2 vehicle id: 0 actual SOC: 998.52 vehicle id: 1 actual SOC: 998.52 step: 3 vehicle id: 0 actual SOC: 996.75 vehicle id: 1 actual SOC: 996.75 step: 4 vehicle id: 0 actual SOC: 994.30 vehicle id: 1 actual SOC: 994.29 vehicle id: 2 actual SOC: 1000.00 vehicle id: 3 actual SOC: 1000.00 step: 5 vehicle id: 0 actual SOC: 991.19 vehicle id: 1 actual SOC: 991.15 vehicle id: 2 actual SOC: 999.75 vehicle id: 3 actual SOC: 999.75 ........ After writing this, you can indeed get the vehicle id, but the initial power still cannot be modified

just-wynot avatar Jul 13 '23 01:07 just-wynot

@wy12184163 hello can we connect as i am working on a project related to EV using sumo and i think you have past experience within it . connect me on mail [email protected]

technomancerAdarsh avatar Mar 08 '25 06:03 technomancerAdarsh