Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[sumo-user] Loading simulation state resets flow traffic generation

Hi everyone

At every step, I save the simulation state, load it separately with another traci connection, and run 20 steps of the later

I'm using flow to generate traffic and the problem is that the newly created connection repeats the traffic generation from the beginning and not from where it was loaded

Based on what I saw in the documentation
https://sumo.dlr.de/docs/Simulation/SaveAndLoad.html
and these two posts
https://www.eclipse.org/lists/sumo-user/msg01162.html
https://www.eclipse.org/lists/sumo-user/msg03476.html
I understand that the flow should start from the same point it was left

See script attached

SUMO 1.7.0

Sincerely,

Marcelo d'Almeida

Attachment: temp.zip
Description: Zip archive

import os
import 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'")

import traci
import traci.constants as tc
from sumolib import checkBinary
from os.path import expanduser


home = expanduser("~")

def get_sumo_binary(gui=False):

    if gui:
        sumo_binary = checkBinary('sumo-gui')
    else:
        sumo_binary = checkBinary('sumo')

    return sumo_binary

class Example:

    def __init__(self, net_file, route_file):

        self.net_file = net_file
        self.route_file = route_file

    def start_traci(self, gui=False):
        traci.start([
            get_sumo_binary(gui),
            '-n', self.net_file,
            '-r', self.route_file
        ])

    def run_inner_model(self, initial_step):
        print('starting')
        gui = True

        traci.start([
            get_sumo_binary(gui),
            '-n', self.net_file,
            '-r', self.route_file,
            '--start', str(True),
            '--begin', str(initial_step),
            '--load-state', home + '/temp/simulation_state' 
        ], label="1")

        traci_connection = traci.getConnection("1")

        i = 20
        while traci_connection.simulation.getMinExpectedNumber() > 0:
            traci_connection.simulationStep()
            print("running inner_model")

            if i == 0:
                break

            i -= 1

        traci.close()
        traci.switch('default')

    def run_simulation(self):

        while traci.simulation.getMinExpectedNumber() > 0:

            traci.simulation.saveState(home + '/temp/simulation_state')

            initial_step = traci.simulation.getTime()

            self.run_inner_model(initial_step)

            traci.simulationStep()


net_file = home + '/temp/regular-intersection__right_on_red.net.xml'
route_file = home + '/temp/regular-intersection.rou.xml'

example = Example(net_file, route_file)
example.start_traci()
example.run_simulation()
traci.close()



Back to the top