Skip to content
1 change: 0 additions & 1 deletion examples/exp_configs/non_rl/aimsun_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
sim=AimsunParams(
sim_step=0.1,
render=True,
emission_path='data',
replication_name="Replication 930",
centroid_config_name="Centroid Configuration 910"
),
Expand Down
4 changes: 3 additions & 1 deletion examples/exp_configs/non_rl/figure_eight.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
vehicles = VehicleParams()
vehicles.add(
veh_id="idm",
acceleration_controller=(IDMController, {}),
acceleration_controller=(IDMController, {
"noise": 0.1,
}),
lane_change_controller=(StaticLaneChanger, {}),
routing_controller=(ContinuousRouter, {}),
car_following_params=SumoCarFollowingParams(
Expand Down
55 changes: 55 additions & 0 deletions examples/exp_configs/non_rl/i210_subnetwork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Runs a simulation of the I-210 subnetwork."""
from flow.core.params import AimsunParams, EnvParams, NetParams
from flow.core.params import VehicleParams
from flow.core.params import InFlows
import flow.config as config
from flow.envs import TestEnv
from flow.networks import Network
import os


# no vehicles in the network
vehicles = VehicleParams()

# path to the imported Aimsun template
template_path = os.path.join(
config.PROJECT_PATH, "examples/prebuilt/aimsun/i_210_sub_merge_020620.ang")


flow_params = dict(
# name of the experiment
exp_tag='I-210_subnetwork',

# name of the flow environment the experiment is running on
env_name=TestEnv,

# name of the network class the experiment is running on
network=Network,

# simulator that is used by the experiment
simulator='aimsun',

# Aimsun-related parameters
sim=AimsunParams(
sim_step=0.1, # FIXME: 0.8?
render=True,
replication_name="Replication 930", # FIXME
centroid_config_name="Centroid Configuration 910" # FIXME
),

# environment related parameters (see flow.core.params.EnvParams)
env=EnvParams(
horizon=3000, # FIXME
),

# network-related parameters (see flow.core.params.NetParams and the
# network's documentation or ADDITIONAL_NET_PARAMS component)
net=NetParams(
inflows=InFlows(),
template=template_path
),

# vehicles to be placed in the network at the start of a rollout (see
# flow.core.params.VehicleParams)
veh=vehicles,
)
1 change: 0 additions & 1 deletion examples/exp_configs/non_rl/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
# sumo-related parameters (see flow.core.params.SumoParams)
sim=SumoParams(
render=True,
emission_path="./data/",
sim_step=0.2,
restart_instance=False,
),
Expand Down
4 changes: 4 additions & 0 deletions examples/exp_configs/non_rl/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flow.controllers import IDMController, ContinuousRouter
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams
from flow.core.params import VehicleParams
from flow.core.params import SumoCarFollowingParams
from flow.envs.ring.accel import AccelEnv, ADDITIONAL_ENV_PARAMS
from flow.networks.ring import RingNetwork, ADDITIONAL_NET_PARAMS

Expand All @@ -15,6 +16,9 @@
veh_id="idm",
acceleration_controller=(IDMController, {}),
routing_controller=(ContinuousRouter, {}),
car_following_params=SumoCarFollowingParams(
min_gap=0,
),
num_vehicles=22)


Expand Down
4 changes: 4 additions & 0 deletions examples/exp_configs/rl/multiagent/multiagent_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from flow.core.params import NetParams
from flow.core.params import SumoParams
from flow.core.params import VehicleParams
from flow.core.params import SumoCarFollowingParams
from flow.envs.multiagent import MultiWaveAttenuationPOEnv
from flow.networks import MultiRingNetwork
from flow.utils.registry import make_create_env
Expand All @@ -35,6 +36,9 @@
acceleration_controller=(IDMController, {
'noise': 0.2
}),
car_following_params=SumoCarFollowingParams(
min_gap=0
),
routing_controller=(ContinuousRouter, {}),
num_vehicles=21)
vehicles.add(
Expand Down
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import argparse
import sys
from flow.core.experiment import Experiment
from flow.core.params import AimsunParams


def parse_args(args):
Expand Down Expand Up @@ -60,6 +61,12 @@ def parse_args(args):
flow_params['sim'].render = not flags.no_render
flow_params['simulator'] = 'aimsun' if flags.aimsun else 'traci'

# If Aimsun is being called, replace SumoParams with AimsunParams.
if flags.aimsun:
sim_params = AimsunParams()
sim_params.__dict__.update(flow_params['sim'].__dict__)
flow_params['sim'] = sim_params

# specify an emission path if they are meant to be generated
if flags.gen_emission:
flow_params['sim'].emission_path = "./data"
Expand Down
12 changes: 7 additions & 5 deletions flow/core/kernel/network/aimsun.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,13 @@ def update(self, reset):

def close(self):
"""See parent class."""
# delete the json file that was used to read the network data
cur_dir = os.path.join(config.PROJECT_PATH,
'flow/core/kernel/network')
os.remove(os.path.join(cur_dir, 'data_%s.json' % self.sim_params.port))
os.remove('%s_%s' % (self.network.net_params.template, self.sim_params.port))
try:
# delete the json file that was used to read the network data
cur_dir = os.path.join(config.PROJECT_PATH, 'flow/core/kernel/network')
os.remove(os.path.join(cur_dir, 'data_%s.json' % self.sim_params.port))
os.remove('%s_%s' % (self.network.net_params.template, self.sim_params.port))
except FileNotFoundError:
pass

###########################################################################
# State acquisition methods #
Expand Down
11 changes: 9 additions & 2 deletions flow/core/kernel/simulation/aimsun.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Script containing the base simulation kernel class."""
from flow.core.kernel.simulation.base import KernelSimulation
from flow.utils.aimsun.api import FlowAimsunAPI
import os
import signal
import os.path as osp
import csv

from flow.core.kernel.simulation.base import KernelSimulation
from flow.utils.aimsun.api import FlowAimsunAPI
from flow.core.util import ensure_dir


Expand Down Expand Up @@ -126,6 +129,10 @@ def close(self):
try:
self.kernel_api.stop_simulation()
self.master_kernel.network.aimsun_proc.kill()

# Weird bug that I don't understand. This closes Aimsun's window.
pid = self.master_kernel.network.aimsun_proc.pid + 1
os.kill(pid, signal.SIGTERM)
except OSError:
# in case no simulation originally existed (used by the visualizer)
pass
28 changes: 14 additions & 14 deletions flow/core/kernel/vehicle/aimsun.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ def __init__(self,
# Ordered dictionary used to keep neural net inputs in order
self.__vehicles = collections.OrderedDict()

# total number of vehicles in the network
self.num_vehicles = 0
# number of rl vehicles in the network
self.num_rl_vehicles = 0

# contains the parameters associated with each type of vehicle
self.type_parameters = {}

Expand Down Expand Up @@ -110,8 +105,6 @@ def initialize(self, vehicles):
individual vehicles and their initial speeds
"""
self.type_parameters = vehicles.type_parameters
self.num_vehicles = 0
self.num_rl_vehicles = 0

self.__vehicles.clear()
for typ in vehicles.initial:
Expand All @@ -121,9 +114,6 @@ def initialize(self, vehicles):
self.__vehicles[veh_id]['type'] = typ['veh_id']
self.__vehicles[veh_id]['type_name'] = typ['veh_id'] # FIXME
self.__vehicles[veh_id]['initial_speed'] = typ['initial_speed']
self.num_vehicles += 1
if typ['acceleration_controller'][0] == RLController:
self.num_rl_vehicles += 1

# for tracked_type in self.tracked_vehicle_types:
# self.num_type[tracked_type] = 0
Expand Down Expand Up @@ -174,6 +164,12 @@ def update(self, reset):
added_vehicles = self.kernel_api.get_entered_ids()
exited_vehicles = self.kernel_api.get_exited_ids()

# updated the list of departed and arrived vehicles
self._num_departed.append(len(added_vehicles))
self._num_arrived.append(len(exited_vehicles))
self._departed_ids.append(added_vehicles)
self._arrived_ids.append(exited_vehicles)

# add the new vehicles if they should be tracked
for aimsun_id in added_vehicles:
veh_type = self.kernel_api.get_vehicle_type_name(aimsun_id)
Expand Down Expand Up @@ -351,7 +347,6 @@ def _add_departed(self, aimsun_id):
# add the vehicle's id to the list of vehicle ids
if accel_controller[0] == RLController:
self.__rl_ids.append(veh_id)
self.num_rl_vehicles += 1
else:
self.__human_ids.append(veh_id)
if accel_controller[0] != SimCarFollowingController:
Expand All @@ -369,7 +364,6 @@ def _add_departed(self, aimsun_id):

def add(self, veh_id, type_id, edge, pos, lane, speed):
"""See parent class."""
self.num_vehicles += 1
self.__ids.append(veh_id)
self.__vehicles[veh_id] = {}
self.__vehicles[veh_id]["type_name"] = type_id
Expand Down Expand Up @@ -414,7 +408,6 @@ def remove(self, aimsun_id):
del self._id_aimsun2flow[aimsun_id]
del self._id_flow2aimsun[veh_id]
self.__ids.remove(veh_id)
self.num_vehicles -= 1

# remove it from all other ids (if it is there)
if veh_id in self.__human_ids:
Expand All @@ -426,7 +419,6 @@ def remove(self, aimsun_id):
elif veh_id in self.__rl_ids:
# FIXME should be else
self.__rl_ids.remove(veh_id)
self.num_rl_vehicles -= 1

# make sure that the rl ids remain sorted
self.__rl_ids.sort()
Expand Down Expand Up @@ -802,3 +794,11 @@ def get_lane_followers_speed(self, veh_id, error=None):
def get_lane_leaders_speed(self, veh_id, error=None):
"""See parent class."""
raise NotImplementedError

@property
def num_vehicles(self):
return len(self.__ids)

@property
def num_rl_vehicles(self):
return len(self.__rl_ids)
9 changes: 5 additions & 4 deletions flow/envs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,10 @@ def step(self, rl_actions):

# test if the environment should terminate due to a collision or the
# time horizon being met
done = (self.time_counter >= self.env_params.warmup_steps +
self.env_params.horizon) # or crash
current_time = self.time_counter / self.env_params.sims_per_step
max_time = self.env_params.warmup_steps + self.env_params.horizon

done = (current_time >= max_time) # or crash

# compute the info for each agent
infos = {}
Expand Down Expand Up @@ -478,8 +480,7 @@ def reset(self):

# reintroduce the initial vehicles to the network
for veh_id in self.initial_ids:
type_id, edge, lane_index, pos, speed = \
self.initial_state[veh_id]
type_id, edge, lane_index, pos, speed = self.initial_state[veh_id]

try:
self.k.vehicle.add(
Expand Down
2 changes: 1 addition & 1 deletion flow/networks/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RingNetwork(Network):
>>> from flow.core.params import NetParams
>>> from flow.core.params import VehicleParams
>>> from flow.core.params import InitialConfig
>>> from flow.scenarios import RingNetwork
>>> from flow.networks import RingNetwork
>>>
>>> network = RingNetwork(
>>> name='ring_road',
Expand Down
1 change: 1 addition & 0 deletions flow/utils/aimsun/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
port_string = sys.argv[1]
model.setAuthor(port_string)


def generate_net(nodes,
edges,
connections,
Expand Down