首页 > 解决方案 > 尽管相同参数的数量不同,但结果相同

问题描述

我想通过 2 次迭代检查更改特定参数(例如 tau、accel、decel、sigma)的量对流动特性的影响。因此,我使用以下 python 脚本和 TraCI 工具来观察 tau 变化:

import traci

tau = [3, 4]
n = 2
for i in range(n):
    def run():
        traci.start(sumoCmd)
        traci.vehicle.add('vehicle_2', 'route_1', 'emergency', '45')
        for j in tau:
            traci.vehicle.setTau('vehicle_2', j)                    
        N = 200
        step = 0
        for step in range(N):     
            traci.simulationStep()               
            step += 1
        traci.close()
        sys.stdout.flush()
          
     run()

在模拟结束时,结果并没有什么不同,这让我觉得我的代码有问题。如果你能帮助我,我将不胜感激。最好的问候,阿里

标签: pythonsumo

解决方案


您的主循环应该结束tau,而不是内部循环。

您还应该只定义一次函数,然后j作为参数传递。

import traci

tau = [3, 4]

def run(j):
    traci.start(sumoCmd)
    traci.vehicle.add('vehicle_2', 'route_1', 'emergency', '45')
    traci.vehicle.setTau('vehicle_2', j)                    
    N = 200
    step = 0
    for step in range(N):     
        traci.simulationStep()               
    traci.close()
    sys.stdout.flush()

for j in tau:
    

没有必要step += 1for循环自动增加它。


推荐阅读