Skip to content

Commit

Permalink
Running multiple aimsun instances while training (#766)
Browse files Browse the repository at this point in the history
* Use port to generate unique auxiliary files

* open multiple copies of ang file to avoid conflict

* flake
  • Loading branch information
temetski authored and AboudyKreidieh committed Nov 5, 2019
1 parent 4ff791b commit 2b9e7a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
21 changes: 12 additions & 9 deletions flow/core/kernel/network/aimsun.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def generate_network(self, network):
cur_dir = os.path.join(config.PROJECT_PATH,
'flow/core/kernel/network')
# TODO: add current time
with open(os.path.join(cur_dir, 'data.json'), 'w') as outfile:
with open(os.path.join(cur_dir, 'data_%s.json' % self.sim_params.port), 'w') as outfile:
json.dump(output, outfile, sort_keys=True, indent=4)

# path to the Aimsun_Next binary
Expand All @@ -94,11 +94,11 @@ def generate_network(self, network):

# remove network data file if if still exists from
# the previous simulation
data_file = 'flow/core/kernel/network/network_data.json'
data_file = 'flow/core/kernel/network/network_data_%s.json' % self.sim_params.port
data_file_path = os.path.join(config.PROJECT_PATH, data_file)
if os.path.exists(data_file_path):
os.remove(data_file_path)
check_file = 'flow/core/kernel/network/network_data_check'
check_file = 'flow/core/kernel/network/network_data_check%s' % self.sim_params.port
check_file_path = os.path.join(config.PROJECT_PATH, check_file)
if os.path.exists(check_file_path):
os.remove(check_file_path)
Expand All @@ -115,9 +115,11 @@ def generate_network(self, network):
script_path = osp.join(config.PROJECT_PATH,
'flow/utils/aimsun/load.py')
file_path = osp.join(config.PROJECT_PATH,
'flow/utils/aimsun/aimsun_template_path')
'flow/utils/aimsun/aimsun_template_path_%s' % self.sim_params.port)
with open(file_path, 'w') as f:
f.write(template_path)
f.write("%s_%s" % (template_path, self.sim_params.port))
# instances must have unique template paths to avoid crashing?
os.popen('cp %s %s_%s' % (template_path, template_path, self.sim_params.port))

# start the aimsun process
aimsun_call = [aimsun_path, "-script", script_path, str(self.sim_params.port)]
Expand Down Expand Up @@ -152,10 +154,10 @@ def generate_network(self, network):

else:
# load network from template
scenar_file = "flow/core/kernel/network/network_data.json"
scenar_file = "flow/core/kernel/network/network_data_%s.json" % self.sim_params.port
scenar_path = os.path.join(config.PROJECT_PATH, scenar_file)

check_file = "flow/core/kernel/network/network_data_check"
check_file = "flow/core/kernel/network/network_data_check_%s" % self.sim_params.port
check_path = os.path.join(config.PROJECT_PATH, check_file)

# a check file is created when all the network data
Expand All @@ -176,7 +178,7 @@ def generate_network(self, network):
# TODO load everything that is in content into the network

else:
data_file = 'flow/utils/aimsun/osm_edges.json'
data_file = 'flow/utils/aimsun/osm_edges_%s.json' % self.sim_params.port
filepath = os.path.join(config.PROJECT_PATH, data_file)

while not os.path.exists(filepath):
Expand Down Expand Up @@ -259,7 +261,8 @@ def close(self):
# 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.json'))
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))

###########################################################################
# State acquisition methods #
Expand Down
4 changes: 2 additions & 2 deletions flow/utils/aimsun/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def set_sim_step(experiment, sim_step):


# collect the network-specific data
data_file = 'flow/core/kernel/network/data.json'
data_file = 'flow/core/kernel/network/data_%s.json'%port_string
with open(os.path.join(config.PROJECT_PATH, data_file)) as f:
data = json.load(f)

Expand Down Expand Up @@ -834,7 +834,7 @@ def set_sim_step(experiment, sim_step):
"length": length,
"numLanes": num_lanes}
with open(os.path.join(config.PROJECT_PATH,
'flow/utils/aimsun/osm_edges.json'), 'w') \
'flow/utils/aimsun/osm_edges_%s.json' % port_string), 'w') \
as outfile:
json.dump(edge_osm, outfile, sort_keys=True, indent=4)

Expand Down
10 changes: 5 additions & 5 deletions flow/utils/aimsun/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ def get_dict_from_objects(sections, nodes, turnings, cen_connections):

return scenario_data

port_string = sys.argv[1]

# collect template path
file_path = os.path.join(config.PROJECT_PATH,
'flow/utils/aimsun/aimsun_template_path')
'flow/utils/aimsun/aimsun_template_path_%s'%port_string)
with open(file_path, 'r') as f:
template_path = f.readline()
os.remove(file_path)
Expand All @@ -136,11 +137,10 @@ def get_dict_from_objects(sections, nodes, turnings, cen_connections):
model.load(template_path)

# HACK: Store port in author
port_string = sys.argv[1]
model.setAuthor(port_string)

# collect the simulation parameters
params_file = 'flow/core/kernel/network/data.json'
params_file = 'flow/core/kernel/network/data_%s.json' % port_string
params_path = os.path.join(config.PROJECT_PATH, params_file)
with open(params_path) as f:
data = json.load(f)
Expand Down Expand Up @@ -176,7 +176,7 @@ def get_dict_from_objects(sections, nodes, turnings, cen_connections):
scenario_data = load_network()

# save template's scenario into a file to be loaded into Flow's scenario
scenario_data_file = 'flow/core/kernel/network/network_data.json'
scenario_data_file = 'flow/core/kernel/network/network_data_%s.json'%port_string
scenario_data_path = os.path.join(config.PROJECT_PATH, scenario_data_file)
with open(scenario_data_path, 'w') as f:
json.dump(scenario_data, f, sort_keys=True, indent=4)
Expand All @@ -185,7 +185,7 @@ def get_dict_from_objects(sections, nodes, turnings, cen_connections):

# create a check file to announce that we are done
# writing all the network data into the .json file
check_file = 'flow/core/kernel/network/network_data_check'
check_file = 'flow/core/kernel/network/network_data_check_%s'%port_string
check_file_path = os.path.join(config.PROJECT_PATH, check_file)
open(check_file_path, 'a').close()

Expand Down

0 comments on commit 2b9e7a2

Please sign in to comment.