Simulation
This page will go into details you the classes used to build and run a python simulation.
Building the simulation
The preferred method of creating a python simulation is making use of the SimulationBuilder class that provides an API that helps you build your simulation scenario and properly instantiates the Simulator class. A SimulationConfiguration is passed to the builder during initialization for simulation-level configuration.
To help you with positioning your nodes some utility methods are also provided.
gradysim.simulator.simulation.SimulationConfiguration
dataclass
Simulation-level configurations. These will change how the simulation will be run.
Source code in gradysim/simulator/simulation.py
debug: bool = False
class-attribute
instance-attribute
Setting this flag to true will enable additional logging. Helpful if you are having issues with the simulation.
duration: Optional[float] = None
class-attribute
instance-attribute
Maximum duration of the simulation in seconds. The simulation will end when no more events scheduled before
duration
are left. If None
, no limit is set.
execution_logging: bool = True
class-attribute
instance-attribute
Setting this flag to true will enable logging of the simulation execution. Even if disabled logging will still happen at the end of the simulation. Disabling this can improve performance.
log_file: Optional[Path] = None
class-attribute
instance-attribute
Simulation logs will be saved in this path.
max_iterations: Optional[int] = None
class-attribute
instance-attribute
Maximum number of simulation iterations. An iteration is counted every time an event is popped from the event-loop.
If None
, no limit is set.
profile: bool = False
class-attribute
instance-attribute
Setting this flag to true will enable profiling of the simulation. This will output to the logs profiling information about the simulation execution. This can be useful to identify bottlenecks in the simulation.
real_time: Union[bool, float] = False
class-attribute
instance-attribute
Setting this to true will put the simulation in real-time mode. This means that the simulation will run synchronized with real-world time. One simulation second will approximately equal to one real-world second. If set to a float will run at that many times real-time. For example, setting this to 2 will make the simulation run twice as fast as real-time. The float value must be greater than 0.
gradysim.simulator.simulation.PositionScheme
Collection of helpers for positioning your nodes within the simulation.
Source code in gradysim/simulator/simulation.py
random(x_range=(-10, 10), y_range=(-10, 10), z_range=(0, 10))
staticmethod
Generates a random position Args: x_range: Range of possible positions in the x axis y_range: Range of possible positions in the y axis z_range: Range of possible positions in the z axis
Returns:
Type | Description |
---|---|
Position
|
A random position within the specified ranges |
Source code in gradysim/simulator/simulation.py
gradysim.simulator.simulation.SimulationBuilder
Helper class to build python simulations. Use the add_handler
and add_node
methods to build your simulation
scenario them call build()
to get a simulation instance. Use this class instead of directly trying to instantiate
a Simulator
instance.
A simulation is build through a fluent interface. This means that you after instantiating this builder class you will set up your simulation by calling methods on that instance gradually building up your simulation.
All methods return the SimulationBuilder instance to help you with method chaining.
Source code in gradysim/simulator/simulation.py
__init__(configuration=SimulationConfiguration())
Initializes the simulation builder
Parameters:
Name | Type | Description | Default |
---|---|---|---|
configuration |
SimulationConfiguration
|
Configuration used for the simulation. The default values uses all default values from the |
SimulationConfiguration()
|
Source code in gradysim/simulator/simulation.py
add_handler(handler)
Adds a new handler to the simulation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
INodeHandler
|
A handler instance |
required |
Returns:
Type | Description |
---|---|
SimulationBuilder
|
The simulator builder instance. This is useful for method chaining |
Source code in gradysim/simulator/simulation.py
add_node(protocol, position)
Adds a new node to the simulation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
Type[IProtocol]
|
Type of protocol this node will run |
required |
position |
Position
|
Position of the node inside the simulation |
required |
Returns:
Type | Description |
---|---|
int
|
The id of the node created |
Source code in gradysim/simulator/simulation.py
build()
Builds the simulation. Should only be called after you have already added all nodes and handlers. Nodes and handlers added after this call will not affect the instance returned by this method.
Returns:
Type | Description |
---|---|
Simulator
|
Simulator instance configured using the previously called methods |
Source code in gradysim/simulator/simulation.py
Running the simulation
After calling the SimulationBuilder.build() method you will get a Simulator instance. This instance has already been pre-baked with all the nodes and handlers you configured using your builder. This class will manage your simulation which can be started by calling the start_simulation() method. That's the only Simulator method a user has to interact with.
The python simulation has the following overall architecture (open in a new tab if you want to take a closer look):
The simulation will run until either no more events exist or one of the termination conditions set in SimulationConfiguration are fired. To better understand the simulation you can check how the EventLoop works.
gradysim.simulator.simulation.Simulator
Executes the python simulation by managing the event loop. This class is responsible for making sure handlers' get the event loop instance they need to function, implementing simulation-level configurations like termination conditions and configuring logging.
You shouldn't instantiate this class directly, prefer to build it through SimulationBuilder.
Source code in gradysim/simulator/simulation.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
__init__(handlers, configuration)
Instantiates the simulation class. This constructor should not be called directly, prefer to use the SimulationBuilder API to get a simulator instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handlers |
Dict[str, INodeHandler]
|
Dictionary of handlers indexed by their labels |
required |
configuration |
SimulationConfiguration
|
Simulation configuration |
required |
Source code in gradysim/simulator/simulation.py
create_node(position, protocol, identifier)
Creates a new simulation node, encapsulating it. You shouldn't call this method directly, prefer to use the SimulationBuilder API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
position |
Position
|
Position where the node should be placed |
required |
protocol |
Type[IProtocol]
|
Type of protocol this node will run |
required |
identifier |
int
|
Identifier of the node |
required |
Returns:
Type | Description |
---|---|
Node
|
The encapsulated node |
Source code in gradysim/simulator/simulation.py
get_node(identifier)
Gets a node by its identifier
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identifier |
int
|
Identifier of the node |
required |
Returns:
Type | Description |
---|---|
Node
|
The encapsulated node |
is_simulation_done()
Checks if the simulation is done. The simulation is done if any of the termination conditions are met or if there are no mode events
Returns:
Type | Description |
---|---|
bool
|
True if the simulation is done, False otherwise |
Source code in gradysim/simulator/simulation.py
scope_event(iteration, timestamp, context)
Call this method to update the formatter's annotation with current information. This module is called by the Simulator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iteration |
int
|
Current iteration the simulation is at |
required |
timestamp |
float
|
Simulation timestamp in seconds |
required |
context |
str
|
Context of what's being currently executed in the simulation |
required |
Returns:
Source code in gradysim/simulator/simulation.py
start_simulation()
Call this method to start the simulation. It is a blocking call and runs until either no event is left in the event loop or a termination condition is met. If not termination condition is set and events are generated infinitely this simulation will run forever.
Source code in gradysim/simulator/simulation.py
step_simulation()
Performs a single step in the simulation. This method is useful if you want to run the simulation in a non-blocking way. This method will run a single event from the event loop and then return, updating the internal simulation state.
Returns:
Type | Description |
---|---|
bool
|
False if the simulation is done, True otherwise |