sim.simulation module

Module simulation is the main simulation module. It contains World class that carries out the event driven simulation and simulation animation.

Note

  • Created by: CH Foh (2021)

class sim.simulation.World

Bases: object

This is the simulation world.

There should only be one simulation world in a simulation. It includes an event driven simulation engine running in the background.

The simulation world can be setup and run in two simple steps:

sim = World()              # create a World object
sim.config(sim_stop=500,   # configure the object
           sim_step=0.1, 
           sim_speed=1.0, 
           display_option=True, 
           scenario=MyScenario(sim))
sim.run()                  # run it, until stopping condition is met
print("Simulation done")   # return from run() when done

Methods

banner()

It returns an ASC banner text for PyMoSim. The ASC banner it returns is::.

config([sim_stop, sim_step, sim_speed, …])

The function to configure the world object.

get_scenario()

Use this method to get the associated scenario object of the simulation.

is_animation_shown()

Use this method to check if animation is turned on for this simulation.

run()

The function to run the simulation.

sim__exit(restart_flag)

This method will open a dialog to confirm closing of the simulation and trigger termination or restart process based on the user action.

sim__set_scenario_name(name)

Use this method to give a name to the scenario.

sim__set_stop_time(stop_time)

Set a new simulation stop time.

sim__set_timer(timer[, delay])

The function to set a timer in the simulation.

sim__set_zoom_scale(zoom)

Use this method to adjust the zoom scale of the map on the screen.

sim__user_event(event)

This is an event listener published for the frame object to call when the user acts on the frame, and this method should implement the corresponding responses based on the user action such as button click, menu item selection, etc.

version()

It returns the version of the simulation in (version,minor,build) format where version is the major version, minor is the minor version, and build is the build version.

__init__()

The constructor for World class.

config(sim_stop=None, sim_step=None, sim_speed=None, display_option=None, scenario=None)

The function to configure the world object.

In the simulation, user simulation is called every sim_step interval in real-time. If sim_step=0.1, user simulation will be called 10 times every second. For each call, the animation engine will render the outcome of the world and show on the screen. In other words, the frame rate of the animation in this case is 10 frames per second, calculated by sim_speed/sim_step.

If sim_speed is set higher than 1, say sim_speed=3, the simulation engine will now attempt to call user simulation 30 times every second, and animation engine will attempt to render the outcome to produce 30 frames per second. Whether 30 frames per second can be produced depends on the speed of the CPU. The animation may lag behind the real-time if the CPU cannot catch up with the real-time. Reducing the frame rate can help CPU to catch up with the real-time. This can be done by reducing sim_step. For this example we can set sim_step=0.3 so that sim_speed/sim_step=10 frames per second.

Parameters
sim_stopfloat

The time that the simulation stops (in seconds).

sim_stepfloat

The time step (in seconds) that the simulator updates the movement of vehicles.

sim_speedfloat

How many times faster the simulator should run. Specifying 1.0 for simulation to run in real time, >1.0 to run faster than real time, <1.0 to run slower than real time.

display_optionbool

Whether to enable the display to see the animation. Setting False to cause the simulator to run without display and animation.

scenariosim.simulation.Scenario subclass

The scenario object to simulate.

sim__set_stop_time(stop_time)

Set a new simulation stop time.

Note

User simulation should not use this method directly. Instead, user simulation should set the stop time using scenario class.

sim__set_timer(timer, delay=0)

The function to set a timer in the simulation.

Note

User simulation should not use this method directly. Instead, user simulation should use node.timer.NodeTimer to create a timer.

Parameters
timernode.timer.NodeTimer

The timer instance to set.

delayfloat, optional, default=0

Specify a delay start to this time. The default is zero, which means that the timer will be triggered to run immediately.

get_scenario()

Use this method to get the associated scenario object of the simulation.

Returns
sim.scenario.BaseScenario subclass

It returns the instance of the scenario.

sim__set_scenario_name(name)

Use this method to give a name to the scenario. The name will show on the window in the GUI mode.

Note

The user simulation should not use this method to set scenario name. Instead, the user simulation should set the name using the corresponding method provided by sim.scenario.BaseScenario.

sim__set_zoom_scale(zoom)

Use this method to adjust the zoom scale of the map on the screen.

Note

The user simulation should not use this method to adjust the zoom scale setting. Instead, the user simulation should adjust the setting by using the corresponding method provided by sim.scenario.BaseScenario.

is_animation_shown()

Use this method to check if animation is turned on for this simulation.

Returns
bool

It returns True if the animation is turned on, False otherwise.

sim__user_event(event)

This is an event listener published for the frame object to call when the user acts on the frame, and this method should implement the corresponding responses based on the user action such as button click, menu item selection, etc.

Note

The user simulation should not use this method. It is a callback designed to be triggered by a user action.

sim__exit(restart_flag)

This method will open a dialog to confirm closing of the simulation and trigger termination or restart process based on the user action.

Note

The user simulation should not use this method.

run() → bool

The function to run the simulation.

The control will remain in this function until the simulation has completed.

Returns
bool

Whether a re-run is requested. Return True if the user has requested for a re-run, False otherwise.

version()

It returns the version of the simulation in (version,minor,build) format where version is the major version, minor is the minor version, and build is the build version.

Returns
Tuple (version:int,minor:int,build:int)

The version of this build, e.g. version 0.8.1 is major version 0, minor version 8, build number 1.

banner()

It returns an ASC banner text for PyMoSim. The ASC banner it returns is:

~  ___    _  _   __  __            ___      _            
~ | _ \  | || | |  \/  |   ___    / __|    (_)    _ __   
~ |  _/   \_, | | |\/| |  / _ \   \__ \    | |   | '  \  
._|_|_   _|__/  |_|__|_|  \___/   |___/   _|_|_  |_|_|_| 
| """ | | """"| |""""""| |"""""| |"""""| |"""""| |"""""| 
`-0-0-' `-0-0-' `-0--0-' `-0-0-' `-0-0-' `-0-0-' `-0-0-'
Returns
str

The ASC banner string.