1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-23 20:16:29 +00:00

Add an option to seed sumo's PRNG

This commit is contained in:
Thibaud Gasser 2019-02-01 11:50:41 +01:00
parent 8d89f83d03
commit 5454b8805b
3 changed files with 12 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import os
import shutil
import subprocess
import tempfile
import time
from sys import argv
from types import SimpleNamespace
from xml.etree import ElementTree
@ -108,7 +109,7 @@ def load_polyconvert_template(osm_file, type_file, scenario_name):
return polyconfig
def load_sumoconfig_template(simulation_name, routefiles=(), generate_polygons=False):
def load_sumoconfig_template(simulation_name, routefiles=(), generate_polygons=False, seed=None):
routefiles = routefiles or (f'{simulation_name}.rou.xml',)
sumoconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
root = sumoconfig.getroot()
@ -120,6 +121,8 @@ def load_sumoconfig_template(simulation_name, routefiles=(), generate_polygons=F
else:
root.find('input').remove(additional)
root.find('report/log').set('value', f'{simulation_name}.log')
# Set the seed for the random number generator. By default, use the current time
root.find('random_number/seed').set('value', seed or str(time.time()))
return sumoconfig
@ -187,7 +190,7 @@ def generate_all(args):
logs_dir = os.path.join(simulation_dir, 'log')
generate_scenario(osm_file, simulation_dir, simulation_name, generate_polygons)
routefiles = generate_mobility(simulation_dir, simulation_name, args.vclasses)
generate_sumo_configuration(routefiles, simulation_dir, simulation_name, )
generate_sumo_configuration(routefiles, simulation_dir, simulation_name, generate_polygons, )
# Move all logs to logdir
move_logs(simulation_dir, logs_dir)
@ -214,6 +217,7 @@ def parse_command_line():
help='Generate this vclass with given density, in pair form vclass=density. The density is '
'given in vehicles per hour per kilometer. For now, the following vehicle classes are '
'available: passenger, truck, bus.')
parser.add_argument('--seed', help='Initializes the random number generator.')
options = parser.parse_args()
handle_args(options)

View File

@ -24,7 +24,7 @@
</report>
<random_number>
<seed value="42"/>
<seed value=""/>
</random_number>
</configuration>

View File

@ -41,6 +41,11 @@ class TemplateTests(unittest.TestCase):
self.assertEqual(tree.find('input/route-files').get('value'), ','.join(routefiles))
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log')
def test_load_sumoconfig_template_with_seed(self):
routefiles = (f'{self.sim_name}.bus.rou.xml', f'{self.sim_name}.passenger.rou.xml')
tree = configurator.load_sumoconfig_template(self.sim_name, routefiles, seed=42)
self.assertEqual(tree.find('random_number/seed').get('value'), 42)
def test_load_polyconvert_template(self):
tree = configurator.load_polyconvert_template(
osm_file=f'{self.sim_name}.osm',