如何通过TraCI结合Pandas DataFrame在SUMO中设置车辆车速?
Hey there! Let's walk through how to control specific vehicles in your SUMO simulation using predefined speed vectors generated with Python. Since you've got 3 total vehicles and need to set custom speeds for 2 of them, here's a tailored step-by-step guide:
First, create a Python script to generate your desired speed vectors. You can define speeds by simulation timesteps, real-world time, or even based on road segments. For example:
# speed_generator.py def generate_speed_profiles(): # Key: vehicle ID, Value: list of speeds (one per simulation step) speed_data = { "target_veh_1": [11.1, 13.9, 15.6, 15.6, 13.9], # ~25, 31, 35, 35, 31 mph "target_veh_2": [8.9, 10.0, 11.1, 11.1, 10.0] # ~20, 22, 25, 25, 22 mph } return speed_data # Save to a JSON file if you want to reuse it across simulations import json with open("speed_profiles.json", "w") as f: json.dump(generate_speed_profiles(), f)
Update your existing Script.py to load the speed profiles and enforce them on your target vehicles. The key here is using traci.vehicle.setSpeed() and disabling SUMO's default car-following behavior with setSpeedMode(0) so vehicles strictly follow your predefined speeds:
# Script.py import traci import sumolib import json # Load your pre-generated speed profiles with open("speed_profiles.json", "r") as f: speed_profiles = json.load(f) # Initialize SUMO connection sumo_binary = sumolib.checkBinary("sumo-gui") # Use "sumo" for headless mode traci.start([sumo_binary, "-c", "your_config.sumocfg"]) current_step = 0 max_steps = max(len(speeds) for speeds in speed_profiles.values()) while current_step < max_steps: traci.simulationStep() # Apply speed to each target vehicle for veh_id, speeds in speed_profiles.items(): # Make sure the vehicle is already in the simulation if veh_id in traci.vehicle.getIDList(): # Disable default speed control (0 = ignore all safety/flow rules) traci.vehicle.setSpeedMode(veh_id, 0) # Set the speed for the current timestep if current_step < len(speeds): traci.vehicle.setSpeed(veh_id, speeds[current_step]) current_step += 1 traci.close()
Double-check that the vehicle IDs you're targeting in your Python script exactly match the IDs defined in your route file (*.rou.xml). For example, in your route file:
<routes> <vType id="default_car" accel="2.6" decel="4.5" sigma="0.5" length="5" maxSpeed="70"/> <vehicle id="target_veh_1" type="default_car" route="route_1" depart="0"/> <vehicle id="target_veh_2" type="default_car" route="route_2" depart="0"/> <vehicle id="regular_veh_3" type="default_car" route="route_3" depart="1"/> </routes>
- If vehicles aren't following your speeds: Make sure you've set
setSpeedMode(0)—without this, SUMO's car-following model will override your settings. - If you get "vehicle not found" errors: Add a small delay or check if the vehicle has departed before trying to set its speed (use
traci.vehicle.getDepartureTime(veh_id)to verify). - For continuous speed changes: Instead of fixed steps, use
traci.simulation.getTime()to map speeds to real simulation time and interpolate values if needed.
内容的提问来源于stack exchange,提问作者umair durrani

