与 Traci 的简单 python 连接

Simple python connection to Traci

我正在尝试在我打算与 SUMO 交通模拟器交互的 Conda 环境中连接到 Traci-Server(我的 Tensorflow 项目需要 Conda)

import os, sys

if 'SUMO_HOME' in os.environ:
 tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
 sys.path.append(tools)
else:
 sys.exit("please declare environment variable 'SUMO_HOME'")

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

import traci                    #TraCI functionality
from sumolib import checkBinary #checkBinary help locate the Sumo binaries
import time


traci.connect()
traci.start(['sumo-gui', '-c', 'osm.sumocfg', "--start", "--quit-on-end"])

traci.close()

但是,我收到连接错误

C:\Users\<UserName>\.conda\envs\tf\python.exe "C:/Users/<UserName>/PycharmProjects/Tensor Tutorial/TestTraffic.py"
 Retrying in 1 seconds
Could not connect to TraCI Server at localhost:8813 [WinError 10061] No connection could be made because the target machines actively refused it
 Retrying in 1 seconds
Could not connect to TraCI Server at localhost:8813 [WinError 10061] No connection could be made because the target machines actively refused it
 Retrying in 1 seconds
Could not connect to TraCI Server at localhost:8813 [WinError 10061] No connection could be made because the target machines actively refused it
 Retrying in 1 seconds
Traceback (most recent call last):
  File "C:\Program_Files_(x86)\Eclipse\Sumo\tools\traci\main.py", line 166, in connect
   conn = Connection(host, port, proc)
  File "C:\Program_Files_(x86)\Eclipse\Sumo\tools\traci\main.py", line 51, in __init__
   self._socket.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/<UserName>/PycharmProjects/Tensor Tutorial/TestTraffic.py", line 24, in <module>
   traci.connect()
  File "C:\Program_Files_(x86)\Eclipse\Sumo\tools\traci\main.py", line 127, in connect
   time.sleep(waitBetweenRetries)
KeyboardInterrupt

Process finished with exit code 1

只需省略 traci.connect。如果使用 traci.start.

则不需要