Skip to content

Commit

Permalink
Fixes regression in #5
Browse files Browse the repository at this point in the history
  • Loading branch information
mschrader15 committed Jun 21, 2022
1 parent a8ffc1a commit c1cf08c
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 79 deletions.
16 changes: 8 additions & 8 deletions example/setting-files/ES_4_25.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"net_file": "sumo-xml/net.net.xml",
"route_file": "sumo-xml/routes/rou.route.xml",
"additional_files": [
"sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"sumo-xml/traffic-lights/dual_ring_ready.63082004.xml",
"sumo-xml/traffic-lights/tls.rl.add.xml",
"sumo-xml/vehType/vTypeDistributions.add.xml"
"{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082004.xml",
"{ROOT}/example/sumo-xml/traffic-lights/tls.rl.add.xml",
"{ROOT}/example/sumo-xml/vehType/vTypeDistributions.add.xml"
],
"tl_ids": [
"63082002",
Expand All @@ -34,9 +34,9 @@
"gui": "False",
"central_junction": "63082003",
"tl_file_dict": {
"63082002": "sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"63082003": "sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"63082004": "sumo-xml/traffic-lights/dual_ring_ready.63082004.xml"
"63082002": "{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"63082003": "{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"63082004": "{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082004.xml"
}
}
}
18 changes: 9 additions & 9 deletions example/setting-files/NO-RL_4_25.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"net_file": "sumo-xml/net.net.xml",
"route_file": "sumo-xml/routes/rou.route.xml",
"additional_files": [
"sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"sumo-xml/traffic-lights/dual_ring_ready.63082004.xml",
"sumo-xml/traffic-lights/tls.rl.add.xml",
"sumo-xml/vehType/vTypeDistributions.add.xml"
"{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082004.xml",
"{ROOT}/example/sumo-xml/traffic-lights/tls.rl.add.xml",
"{ROOT}/example/sumo-xml/vehType/vTypeDistributions.add.xml"
],
"tl_ids": [
"63082002",
Expand All @@ -31,12 +31,12 @@
"tl_file": "sumo-xml/traffic-lights/tls.rl.add.xml",
"sim_step": 0.5,
"start_time": "2020-02-24 06:30:00.000",
"gui": "True",
"gui": "False",
"central_junction": "63082003",
"tl_file_dict": {
"63082002": "sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"63082003": "sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"63082004": "sumo-xml/traffic-lights/dual_ring_ready.63082004.xml"
"63082002": "{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082002.xml",
"63082003": "{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082003.xml",
"63082004": "{ROOT}/example/sumo-xml/traffic-lights/dual_ring_ready.63082004.xml"
}
}
}
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
protobuf==3.19.4
click==8.0.3
ray
gym==0.22.0
numpy
Expand All @@ -10,7 +12,7 @@ tabulate
lz4
tensorboardX
lxml
click
pandas
tensorflow
xmltodict
json5
150 changes: 91 additions & 59 deletions rl_sumo/core/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,47 @@
import traci
import traci.constants as tc
import logging
import time
from copy import deepcopy
from sumolib import checkBinary
import subprocess

# should I try and use libsumo?
# Most of the libsumo code is take directly from https://github.com/LucasAlegre/sumo-rl/blob/master/sumo_rl/environment/env.py
LIBSUMO = 'LIBSUMO_AS_TRACI' in os.environ
# Most of the libsumo code is take directly from https://github.com/LucasAlegre/sumo-rl/blob/master/sumo_rl/environment/env.py
LIBSUMO = "LIBSUMO_AS_TRACI" in os.environ


def sumo_cmd_line(params, kernel):

cmd = ['-c', params['gui_config_file']] if params['gui_config_file'] else []

cmd.extend([
'-n',
params.net_file,
'-e',
str(params.sim_length),
'--step-length',
str(params.sim_step),
'--remote-port',
str(params.port),
'--seed',
str(kernel.seed),
"--time-to-teleport",
"-1",
"--collision.action",
"remove",
])
cmd = ["-c", params["gui_config_file"]] if params["gui_config_file"] else []

cmd.extend(
[
"-n",
params.net_file,
"-e",
str(params.sim_length),
"--step-length",
str(params.sim_step),
"--seed",
str(kernel.seed),
"--time-to-teleport",
"-1",
"--collision.action",
"remove",
]
)

additional_files = ", ".join(params.additional_files + [params.route_file])

if params['tls_record_file']:
additional_files = ", ".join([additional_files] + [params['tls_record_file']])
if params["tls_record_file"]:
additional_files = ", ".join([additional_files] + [params["tls_record_file"]])

cmd.extend(["-a", additional_files])

if params.gui:
cmd.extend(['--start'])
cmd.extend(["--start"])

if params['emissions']:
cmd.extend(['--emission-output', params['emissions']])
if params["emissions"]:
cmd.extend(["--emission-output", params["emissions"]])

return cmd

Expand All @@ -58,6 +56,7 @@ class Kernel(object):
"""
This class is the core for interfacing with the simulation
"""

CONNECTION_NUMBER = 0

def __init__(self, sim_params):
Expand All @@ -67,7 +66,9 @@ def __init__(self, sim_params):
self.parent_fns = []
self.sim_params = deepcopy(sim_params)
self.sim_step_size = self.sim_params.sim_step
self.state_file = os.path.join(sim_params.sim_state_dir, f"start_state_{sim_params.port}.xml")
self.state_file = os.path.join(
sim_params.sim_state_dir, f"start_state_{sim_params.port}.xml"
)
self.sim_time = 0
self.seed = 5
self.traci_calls = []
Expand All @@ -79,7 +80,6 @@ def __init__(self, sim_params):
# increment the connection
Kernel.CONNECTION_NUMBER += 1


def set_seed(self, seed):
self.seed = seed

Expand All @@ -91,23 +91,27 @@ def pass_traci_kernel(self, traci_c):
"""
self.traci_c = traci_c

def start_simulation(self, ):
def start_simulation(
self,
):
# find SUMO
sumo_binary = checkBinary('sumo-gui') if self.sim_params.gui else checkBinary('sumo')
sumo_binary = (
checkBinary("sumo-gui") if self.sim_params.gui else checkBinary("sumo")
)

# create the command line call
sumo_call = [sumo_binary] + sumo_cmd_line(self.sim_params, self)

# # start the process
# self.sumo_proc = subprocess.Popen(
# sumo_call,
# stdout=subprocess.DEVNULL,
# ) # stderr=subprocess.STDOUT)

# # sleep before trying to connect with TRACI
# time.sleep(1)
if LIBSUMO:
traci.start(sumo_call, )
sumo_call.extend(
[
"--remote-port",
str(self.sim_params.port),
]
)
traci.start(
sumo_call,
)
traci_c = traci
else:
traci.start(sumo_call, label=self._sumo_conn_label)
Expand All @@ -118,7 +122,7 @@ def start_simulation(self, ):

# set the traffic lights to the default behaviour and run for warm up period
for tl_id in self.sim_params.tl_ids:
traci_c.trafficlight.setProgram(tl_id, f'{tl_id}-1')
traci_c.trafficlight.setProgram(tl_id, f"{tl_id}-1")

# run for an hour to warm up the simulation
for _ in range(int(self.sim_params.warmup_time * 1 / self.sim_step_size)):
Expand All @@ -135,8 +139,8 @@ def start_simulation(self, ):
# set the traffic lights to the all green program
if not self.sim_params.no_actor:
for tl_id in self.sim_params.tl_ids:
traci_c.trafficlight.setProgram(tl_id, f'{tl_id}-2')
traci_c.trafficlight.setProgram(tl_id, f"{tl_id}-2")

# overwrite the default traffic light states to what they where
for tl_id in self.sim_params.tl_ids:
traci_c.trafficlight.setPhase(tl_id, 0)
Expand All @@ -146,21 +150,31 @@ def start_simulation(self, ):

traci_c.simulation.subscribe([tc.VAR_COLLIDING_VEHICLES_NUMBER])

self.add_traci_call([
[traci_c.lane.getAllSubscriptionResults, (), tc.VAR_COLLIDING_VEHICLES_NUMBER],
])
self.add_traci_call(
[
[
traci_c.lane.getAllSubscriptionResults,
(),
tc.VAR_COLLIDING_VEHICLES_NUMBER,
],
]
)

self.sim_time = 0

return traci_c

# @staticmethod
def _subscribe_to_vehicles(self, ):
def _subscribe_to_vehicles(
self,
):
# subscribe to all new vehicle positions and fuel consumption
for veh_id in self.traci_c.simulation.getDepartedIDList():
self.traci_c.vehicle.subscribe(veh_id, VEHICLE_SUBSCRIPTIONS)

def reset_simulation(self, ):
def reset_simulation(
self,
):

try:
self.sim_time = 0
Expand All @@ -173,15 +187,15 @@ def reset_simulation(self, ):
for veh_id in self.traci_c.vehicle.getIDList():
self.traci_c.vehicle.unsubscribe(veh_id)

logging.info('resetting the simulation')
logging.info("resetting the simulation")
self.traci_c.simulation.loadState(self.state_file)

# set the traffic lights to the correct program
# set the traffic lights to the all green program
if not self.sim_params.no_actor:

for tl_id in self.sim_params.tl_ids:
self.traci_c.trafficlight.setProgram(tl_id, f'{tl_id}-2')
self.traci_c.trafficlight.setProgram(tl_id, f"{tl_id}-2")
self.traci_c.trafficlight.setPhase(tl_id, 0)

# subscribe to all vehicles in the simulation at this point
Expand All @@ -197,34 +211,50 @@ def reset_simulation(self, ):
print("Something in TRACI failed")
raise e from e

def kill_simulation(self, ):
for fn, args in [[self._kill_sumo_proc, ()], [self._os_pg_killer, ()], [self._close_traci, ()]]:
def kill_simulation(
self,
):
for fn, args in [
[self._kill_sumo_proc, ()],
[self._os_pg_killer, ()],
[self._close_traci, ()],
]:
with contextlib.suppress(Exception):
fn(*args)

def _kill_sumo_proc(self, ):
def _kill_sumo_proc(
self,
):
if self.sumo_proc:
self.sumo_proc.kill()

def _close_traci(self, ):
def _close_traci(
self,
):
if self.traci_c:
self.traci_c.close()

def _os_pg_killer(self, ):
def _os_pg_killer(
self,
):
if self.sumo_proc:
os.killpg(self.sumo_proc.pid, signal.SIGTERM)

def _execute_traci_fns(self):
for fn in self.parent_fns:
fn(self.traci_c)

def close_simulation(self, ):
logging.info('closing the simulation')
def close_simulation(
self,
):
logging.info("closing the simulation")
# kill the simulation if using the gui.
self.kill_simulation()
self.traci_calls.clear()

def simulation_step(self, ):
def simulation_step(
self,
):
# step the simulation
try:
self.traci_c.simulationStep()
Expand All @@ -240,7 +270,9 @@ def simulation_step(self, ):

return self.sim_data

def get_traci_data(self, ):
def get_traci_data(
self,
):
return {key: fn(*args) for fn, args, key in self.traci_calls}

def add_traci_call(self, traci_module):
Expand Down
8 changes: 8 additions & 0 deletions rl_sumo/parameters/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import List
import pathlib

import json5

# Hacky
ROOT = pathlib.Path(__file__).parent.parent.parent.resolve()

Expand All @@ -27,6 +29,7 @@ def safe_getter(_dict: dict, param: str):
return None



def make_directory(path):
"""
make a directory if it doesn't exist
Expand Down Expand Up @@ -107,6 +110,11 @@ def __init__(self, env_params: EnvParams, settings_dict: dict):
except Exception:
root = params['file_root'].format(ROOT=ROOT)


# Add in the ROOT to the params. Ineffecient but it works.
# I should switch to OmegaConf
params = json5.loads(json5.dumps(params).replace("{ROOT}", str(ROOT)))

self.sim_state_dir: str = os.path.join(root, 'reinforcement-learning-sumo', 'tmp', 'sim_state')

# make the directory
Expand Down
Loading

0 comments on commit c1cf08c

Please sign in to comment.