matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
82 stars 180 forks source link

Question about finishing simulation when all vehicles leave area. #544

Closed RydAlex closed 3 years ago

RydAlex commented 3 years ago

Hi,

I transfer networks and routes from SUMO to MATsim. However, I noticed that SUMO finalise simulation just after all vehicles end their trips, and MATsim simulate everything until the whole simulation time is finished. Is there any settings in the scenario or config file that can change that? My goal is to reduce simulation time to the minimum and read specific execution time.

My config.xml:

<!DOCTYPE config SYSTEM "http://www.matsim.org/files/dtd/config_v2.dtd">
<config>

    <module name="global">
        <param name="randomSeed" value="4711" />
        <param name="coordinateSystem" value="Atlantis" />
    </module>

    <module name="network">
        <param name="inputNetworkFile" value="test.mat.net.xml" />
    </module>

    <module name="plans">
        <param name="inputPlansFile" value="test.mat.plan.xml" />
    </module>

    <module name="controler">
        <param name="outputDirectory" value="./test" />
        <param name="lastIteration" value="10" />
        <param name="mobsim" value="qsim" />
    </module>

    <module name="planCalcScore">
         <parameterset type="activityParams">
             <param name="activityType" value="home"/>
             <param name="typicalDuration" value="12:00:00"/>
         </parameterset >
    </module>

    <module name="qsim">
        <param name="startTime" value="00:00:00" />
        <param name="numberOfThreads" value="8" />
    </module>

    <module name="strategy">
        <param name="maxAgentPlanMemorySize" value="0" />

        <param name="ModuleProbability_1" value="1" />
        <param name="Module_1" value="BestScore" />

        <param name="ModuleProbability_2" value="0.1" />
        <param name="Module_2" value="ReRoute" />
    </module>
</config>

legHistogram.txt

time            time        departures_all    arrivals_all
00:00:00    0       300                   208            
00:05:00    300     300                       309   
00:10:00    600     300                   306   
00:15:00    900     100                   177   
00:20:00    1200    0                         0 
00:25:00    1500    0                         0 
00:30:00    1800    0                         0
... etc.
.... etc.
30:05:00    108300  0                             0

from Java I call scenario with this code:

Config config;
config = ConfigUtils.loadConfig(simConfigFileName);
config.controler().setOverwriteFileSetting( OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
Scenario scenario = ScenarioUtils.loadScenario(config);
Controler controler = new Controler(scenario);
controler.run();

And it seems the whole 30 hours is simulated even if nothing moves.

mrieser commented 3 years ago

MATSim should also end the simulation, once all agents have reached their final activity. But it can be that some agents got stuck (e.g. they missed their last bus, and now wait and wait at the bus stop...), or that they decided to walk very long distances which takes multiple hours etc.

There is a setting in the config.xml, similar to qsim > startTime, but named endTime. You could specify an earlier time there, e.g.:

<module name="qsim">
    <param name="endTime" value="24:00:00" />
</module>

The simulation will then end either at 24 o'clock, or when all agents have arrived at their last activity, whatever comes first.

RydAlex commented 3 years ago

Thanks for your answer, @mrieser.

EndTime, for me, seems like a bit dramatic way to kill the simulation process even if something is stuck, however, it is strange that simulation doesn't stop when everything is finished as I create 1000 persons with plans and all of them seems to be completed when I look on legHistogram.txt

time            time        departures_all    arrivals_all
00:00:00    0       300                   208            
00:05:00    300     300                       309   
00:10:00    600     300                   306   
00:15:00    900     100                   177   
------ at this moment there are1000 departure and arrivals ---- 
mrieser commented 3 years ago

In the log file, QSim should write out at every simulated hour how many vehicles/agents still are considered "active":

INFO QSim:549 SIMULATION (NEW QSim) AT 07:00:00 : #Veh=37544 lost=0 simT=10440.0s realT=3s; (s/r): 3480.0

In this case, 37544 agents are still considered to be active. You might want to observe this number in your log file to see if there are indeed all agents back at home, or not.

I don't know if you use public transport or not, but if you do: Note that every "transit route departure" also gets a synthetic driver assigned, which also counts as vehicle or agent in the simulation. So it might be that all regular agents have finished, but that there are still transit services running, which keep the simulation alive.

You can also check the events file towards the end and look what agents have created the last few events. Maybe this helps to figure out why the simulation keeps running until that point of time, and if the agents maybe got stuck somewhere.

RydAlex commented 3 years ago

Thanks for the tip, All agents finished their plans, and I do not have any transit route in my simulation, so it is strange.

I have a similar logline to you, but I have such short paths that all are finished already. Additionally, this logline is printed only once. Is that can be true that the simulation finish earlier, but in legHistogram.txt, I have logged up to 30 hours, even when the whole simulation ends in the first 10 minutes? Is there any file in the output directory with exact logs of each agent start/stop time inside simulation? Eventually, is there a possibility to increase this line log's frequency: INFO QSim:549 SIMULATION ...?

mrieser commented 3 years ago

is there a possibility to increase this line log's frequency: INFO QSim:549 SIMULATION ...?

Sadly, this is hard-coded to be at every full hour.

but in legHistogram.txt, I have logged up to 30 hours

Yes, the leg histogram always shows up to 30 hours. This helps comparing multiple runs, where one ends earlier than the other due to different traffic conditions. But I see that it can be confusing.

Is there any file in the output directory with exact logs of each agent start/stop time inside simulation?

That would be the events file. Look at the time step of the last few events to find the simulation end time.

Additionally, this logline is printed only once.

Great! This means that the simulation ends before reaching the next full hour.

RydAlex commented 3 years ago

Great! Thank you very much for your explanation, @mrieser - it solves all of my current problems :)