API Documentation¶
Core Components¶
Base Simulation¶
sim_lab.core.BaseSimulation
¶
Bases: ABC
Base class for all SimLab simulations.
This abstract class defines the common interface and utility methods for all simulation types in the SimLab package.
Attributes:
Name | Type | Description |
---|---|---|
random_seed |
Optional[int]
|
Seed for random number generation to ensure reproducible results |
Source code in src/sim_lab/core/base_simulation.py
__init__(days, random_seed=None, **kwargs)
¶
Initialize the base simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
days
|
int
|
The duration of the simulation in days/steps. |
required |
random_seed
|
Optional[int]
|
Seed for random number generation. If None, random results will vary. |
None
|
**kwargs
|
Additional parameters for specific simulation types. |
{}
|
Source code in src/sim_lab/core/base_simulation.py
get_parameters_info()
classmethod
¶
Get information about the parameters required by this simulation.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
A dictionary mapping parameter names to their metadata (type, description, default, etc.) |
Source code in src/sim_lab/core/base_simulation.py
reset()
¶
Reset the simulation to its initial state.
This allows a simulation instance to be re-run with the same parameters.
run_simulation()
abstractmethod
¶
Run the simulation and return results.
This method must be implemented by all simulation subclasses.
Returns:
Type | Description |
---|---|
List[Union[float, int]]
|
A list of values representing the simulation results over time. |
Source code in src/sim_lab/core/base_simulation.py
Simulator Registry¶
sim_lab.core.SimulatorRegistry
¶
Registry for simulation models.
This class maintains a registry of all available simulation models, allowing for dynamic discovery and instantiation of simulations.
Source code in src/sim_lab/core/registry.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 |
|
create(name, **kwargs)
classmethod
¶
Create an instance of a simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the simulation to create. |
required |
**kwargs
|
Any
|
Parameters to pass to the simulation constructor. |
{}
|
Returns:
Type | Description |
---|---|
BaseSimulation
|
A new instance of the requested simulation. |
Raises:
Type | Description |
---|---|
KeyError
|
If the simulation is not registered. |
Source code in src/sim_lab/core/registry.py
get(name)
classmethod
¶
Get a simulation class by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the simulation to get. |
required |
Returns:
Type | Description |
---|---|
Type[BaseSimulation]
|
The simulation class. |
Raises:
Type | Description |
---|---|
KeyError
|
If the simulation is not registered. |
Source code in src/sim_lab/core/registry.py
list_simulators()
classmethod
¶
List all registered simulations.
Returns:
Type | Description |
---|---|
List[str]
|
A list of simulation names. |
load_simulator_from_path(module_path, class_name, register_as=None)
classmethod
¶
Load a simulator class from a module path and register it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_path
|
str
|
The dotted path to the module (e.g. 'sim_lab.custom.my_simulation'). |
required |
class_name
|
str
|
The name of the class to load. |
required |
register_as
|
Optional[str]
|
The name to register the simulation under. If None, the class name will be used. |
None
|
Returns:
Type | Description |
---|---|
Type[BaseSimulation]
|
The loaded simulation class. |
Raises:
Type | Description |
---|---|
ImportError
|
If the module or class cannot be loaded. |
TypeError
|
If the class is not a subclass of BaseSimulation. |
Source code in src/sim_lab/core/registry.py
register(name=None)
classmethod
¶
Decorator to register a simulation class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
Optional[str]
|
The name to register the simulation under. If None, the class name will be used. |
None
|
Returns:
Type | Description |
---|---|
callable
|
A decorator function that registers the class. |
Source code in src/sim_lab/core/registry.py
unregister(name)
classmethod
¶
Remove a simulation from the registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the simulation to remove. |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If the simulation is not registered. |
Source code in src/sim_lab/core/registry.py
Basic Simulations¶
Product Popularity Simulation Class¶
sim_lab.core.ProductPopularitySimulation
¶
Bases: BaseSimulation
A simulation class to model the dynamics of product popularity over time, incorporating factors like natural growth, marketing impact, and promotional campaigns.
Attributes:
Name | Type | Description |
---|---|---|
start_demand |
int
|
Initial demand for the product. |
days |
int
|
Duration of the simulation in days. |
growth_rate |
float
|
Natural growth rate of product demand. |
marketing_impact |
float
|
Impact of ongoing marketing efforts on demand. |
promotion_day |
Optional[int]
|
Day on which a major marketing campaign starts (default is None). |
promotion_effectiveness |
float
|
Effectiveness of the marketing campaign. |
random_seed |
Optional[int]
|
The seed for the random number generator to ensure reproducibility (default is None). |
Methods:
Name | Description |
---|---|
run_simulation |
Runs the simulation and returns a list of demand values over time. |
Source code in src/sim_lab/core/product_popularity_simulation.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 |
|
__init__(start_demand, days, growth_rate, marketing_impact, promotion_day=None, promotion_effectiveness=0, random_seed=None)
¶
Initializes the ProductPopularitySimulation with all necessary parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_demand
|
int
|
The initial level of demand for the product. |
required |
days
|
int
|
The total number of days to simulate. |
required |
growth_rate
|
float
|
The natural daily growth rate of demand, as a decimal. |
required |
marketing_impact
|
float
|
Daily impact of marketing on demand, as a decimal. |
required |
promotion_day
|
Optional[int]
|
The specific day on which a promotional event occurs (defaults to None). |
None
|
promotion_effectiveness
|
float
|
Multiplicative impact of the promotion on demand. |
0
|
random_seed
|
Optional[int]
|
Seed for the random number generator to ensure reproducible results (defaults to None). |
None
|
Source code in src/sim_lab/core/product_popularity_simulation.py
get_parameters_info()
classmethod
¶
Get information about the parameters required by this simulation.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
A dictionary mapping parameter names to their metadata (type, description, default, etc.) |
Source code in src/sim_lab/core/product_popularity_simulation.py
run_simulation()
¶
Simulates the demand for a product over a specified number of days based on the initial settings.
Returns:
Type | Description |
---|---|
List[float]
|
List[int]: A list containing the demand for the product for each day of the simulation. |
Source code in src/sim_lab/core/product_popularity_simulation.py
Resource Fluctuation Simulation Class¶
sim_lab.core.ResourceFluctuationsSimulation
¶
Bases: BaseSimulation
A simulation class to model the fluctuations of resource prices over time, considering factors like volatility, market trends (drift), and supply disruptions.
Attributes:
Name | Type | Description |
---|---|---|
start_price |
float
|
The initial price of the resource. |
days |
int
|
The duration of the simulation in days. |
volatility |
float
|
The volatility of price changes, representing day-to-day variability. |
drift |
float
|
The average daily price change, indicating the trend over time. |
supply_disruption_day |
Optional[int]
|
The specific day a supply disruption occurs (default is None). |
disruption_severity |
float
|
The magnitude of the disruption's impact on price (default is 0). |
random_seed |
Optional[int]
|
The seed for the random number generator to ensure reproducibility (default is None). |
Source code in src/sim_lab/core/resource_fluctuations_simulation.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 |
|
__init__(start_price, days, volatility, drift, supply_disruption_day=None, disruption_severity=0, random_seed=None)
¶
Initializes the ResourceSimulation with all necessary parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_price
|
float
|
The initial price of the resource. |
required |
days
|
int
|
The total number of days to simulate. |
required |
volatility
|
float
|
The volatility of the resource price, representing the randomness of day-to-day price changes. |
required |
drift
|
float
|
The expected daily percentage change in price, which can be positive or negative. |
required |
supply_disruption_day
|
Optional[int]
|
Day on which a supply disruption occurs (defaults to None). |
None
|
disruption_severity
|
float
|
The severity of the supply disruption, affecting prices multiplicatively. |
0
|
random_seed
|
Optional[int]
|
Seed for the random number generator to ensure reproducible results (defaults to None). |
None
|
Source code in src/sim_lab/core/resource_fluctuations_simulation.py
get_parameters_info()
classmethod
¶
Get information about the parameters required by this simulation.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
A dictionary mapping parameter names to their metadata (type, description, default, etc.) |
Source code in src/sim_lab/core/resource_fluctuations_simulation.py
run_simulation()
¶
Simulates the price of the resource over a specified number of days based on the initial settings.
Returns:
Type | Description |
---|---|
List[float]
|
List[float]: A list containing the price of the resource for each day of the simulation. |
Source code in src/sim_lab/core/resource_fluctuations_simulation.py
Stock Market Simulation Class¶
sim_lab.core.StockMarketSimulation
¶
Bases: BaseSimulation
A simulation class to model the fluctuations of stock prices over time, accounting for volatility, general market trends (drift), and specific market events.
Attributes:
Name | Type | Description |
---|---|---|
start_price |
float
|
The initial price of the stock. |
days |
int
|
The duration of the simulation in days. |
volatility |
float
|
The volatility of stock price changes, representing day-to-day variability. |
drift |
float
|
The average daily price change, indicating the trend over time. |
event_day |
Optional[int]
|
The specific day a major market event occurs (default is None). |
event_impact |
float
|
The magnitude of the event's impact on stock prices (default is 0). |
random_seed |
Optional[int]
|
The seed for the random number generator to ensure reproducibility (default is None). |
Methods:
Name | Description |
---|---|
run_simulation |
Runs the simulation and returns a list of stock prices over the simulation period. |
Source code in src/sim_lab/core/stock_market_simulation.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 |
|
__init__(start_price, days, volatility, drift, event_day=None, event_impact=0, random_seed=None)
¶
Initializes the StockMarketSimulation with all necessary parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_price
|
float
|
The initial stock price. |
required |
days
|
int
|
The total number of days to simulate. |
required |
volatility
|
float
|
The volatility of the stock price, representing the randomness of day-to-day price changes. |
required |
drift
|
float
|
The expected daily percentage change in price, which can be positive or negative. |
required |
event_day
|
Optional[int]
|
Day on which a major market event occurs (defaults to None). |
None
|
event_impact
|
float
|
The severity of the market event, affecting prices multiplicatively. |
0
|
random_seed
|
Optional[int]
|
Seed for the random number generator to ensure reproducible results (defaults to None). |
None
|
Source code in src/sim_lab/core/stock_market_simulation.py
get_parameters_info()
classmethod
¶
Get information about the parameters required by this simulation.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
A dictionary mapping parameter names to their metadata (type, description, default, etc.) |
Source code in src/sim_lab/core/stock_market_simulation.py
run_simulation()
¶
Simulates the stock price over a specified number of days based on the initial settings.
Returns:
Type | Description |
---|---|
List[float]
|
List[float]: A list containing the stock prices for each day of the simulation. |
Source code in src/sim_lab/core/stock_market_simulation.py
Discrete Event Simulations¶
Discrete Event Simulation¶
sim_lab.core.DiscreteEventSimulation
¶
Bases: BaseSimulation
A simulation class for discrete event simulations.
This simulation processes events in chronological order, with each event potentially generating new events. The simulation runs until a specified end time or until there are no more events to process.
Attributes:
Name | Type | Description |
---|---|---|
max_time |
float
|
The maximum simulation time. |
days |
int
|
Used for compatibility with other simulations (days = max_time). |
current_time |
float
|
The current simulation time. |
event_queue |
List[Event]
|
The priority queue of pending events. |
state |
Dict[str, Any]
|
The current state of the simulation. |
results |
List[float]
|
The results of the simulation at each time step. |
random_seed |
Optional[int]
|
Seed for random number generation. |
Source code in src/sim_lab/core/discrete_event_simulation.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 |
|
__init__(max_time, initial_events=None, time_step=1.0, random_seed=None)
¶
Initialize the discrete event simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_time
|
float
|
The maximum simulation time. |
required |
initial_events
|
List[Tuple[float, Callable, Any]]
|
List of (time, action, data) tuples to initialize the event queue. |
None
|
time_step
|
float
|
The time step for recording results (default: 1.0). |
1.0
|
random_seed
|
Optional[int]
|
Seed for random number generation. |
None
|
Source code in src/sim_lab/core/discrete_event_simulation.py
get_parameters_info()
classmethod
¶
Get information about the parameters required by this simulation.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
A dictionary mapping parameter names to their metadata. |
Source code in src/sim_lab/core/discrete_event_simulation.py
reset()
¶
run_simulation()
¶
Run the simulation until max_time or until there are no more events.
The simulation processes events in chronological order, with each event potentially generating new events by calling schedule_event().
Returns:
Type | Description |
---|---|
List[float]
|
A list of values representing the simulation state at regular intervals. |
Source code in src/sim_lab/core/discrete_event_simulation.py
schedule_event(time, action, priority=0, data=None)
¶
Schedule a new event to occur at the specified time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time
|
float
|
The absolute time at which the event should occur. |
required |
action
|
Callable
|
The function to execute when the event occurs. |
required |
priority
|
int
|
The priority of the event (lower is higher priority). |
0
|
data
|
Any
|
Additional data associated with the event. |
None
|
Source code in src/sim_lab/core/discrete_event_simulation.py
Advanced Simulation Types¶
Agent-Based Simulation¶
sim_lab.core.AgentBasedSimulation
¶
Bases: BaseSimulation
A simulation class for agent-based modeling.
This simulation models complex systems by simulating the actions and interactions of autonomous agents, allowing for emergent behavior to be observed.
Attributes:
Name | Type | Description |
---|---|---|
agents |
List[Agent]
|
List of agents in the simulation. |
environment |
Environment
|
The environment in which agents operate. |
days |
int
|
Number of steps to simulate. |
neighborhood_radius |
float
|
Radius for determining agent neighbors. |
random_seed |
Optional[int]
|
Seed for random number generation. |
Source code in src/sim_lab/core/agent_based_simulation.py
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
__init__(agent_factory, num_agents, environment=None, days=100, neighborhood_radius=10.0, save_history=False, random_seed=None)
¶
Initialize the agent-based simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent_factory
|
Callable[[int], Agent]
|
Function that creates new agents with given IDs. |
required |
num_agents
|
int
|
Number of agents to create. |
required |
environment
|
Optional[Environment]
|
The environment in which agents operate. If None, a default environment is created. |
None
|
days
|
int
|
Number of steps to simulate. |
100
|
neighborhood_radius
|
float
|
Radius for determining agent neighbors. |
10.0
|
save_history
|
bool
|
Whether to save agent and environment history. |
False
|
random_seed
|
Optional[int]
|
Seed for random number generation. |
None
|
Source code in src/sim_lab/core/agent_based_simulation.py
calculate_metrics()
¶
Calculate metrics for the current simulation state.
Override this method to define specific metrics for your simulation.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary of metrics derived from agent and environment states. |
Source code in src/sim_lab/core/agent_based_simulation.py
get_agent_history(agent_id)
¶
Get the state history for a specific agent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent_id
|
int
|
The ID of the agent. |
required |
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
List of state dictionaries representing the agent's history. |
Source code in src/sim_lab/core/agent_based_simulation.py
get_agent_neighbors(agent)
¶
Get the neighbors of an agent based on proximity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent
|
The agent whose neighbors to find. |
required |
Returns:
Type | Description |
---|---|
List[Agent]
|
List of neighboring agents within the neighborhood radius. |
Source code in src/sim_lab/core/agent_based_simulation.py
get_environment_history()
¶
Get the environment state history.
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
List of state dictionaries representing the environment's history. |
Source code in src/sim_lab/core/agent_based_simulation.py
get_metric_history(metric_name)
¶
Get the history of a specific metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric_name
|
str
|
The name of the metric to retrieve. |
required |
Returns:
Type | Description |
---|---|
List[Any]
|
List of values for the specified metric over time. |
Source code in src/sim_lab/core/agent_based_simulation.py
get_parameters_info()
classmethod
¶
Get information about the parameters required by this simulation.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
A dictionary mapping parameter names to their metadata. |
Source code in src/sim_lab/core/agent_based_simulation.py
reset()
¶
run_simulation()
¶
Run the agent-based simulation.
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
A list of metrics dictionaries for each time step. |
Source code in src/sim_lab/core/agent_based_simulation.py
Network Simulation¶
sim_lab.core.NetworkSimulation
¶
Bases: BaseSimulation
A simulation class for network/graph dynamics.
This simulation models the evolution of a network over time, allowing for changes in node and edge attributes, as well as network structure.
Attributes:
Name | Type | Description |
---|---|---|
nodes |
Dict[Any, Node]
|
Dictionary of nodes in the network. |
edges |
List[Edge]
|
List of edges in the network. |
days |
int
|
Number of steps to simulate. |
update_function |
Callable
|
Function to update the network at each time step. |
save_history |
bool
|
Whether to save node and edge history. |
random_seed |
Optional[int]
|
Seed for random number generation. |
Source code in src/sim_lab/core/network_simulation.py
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
|
__init__(initial_nodes=None, initial_edges=None, update_function=None, directed=False, days=100, save_history=False, random_seed=None)
¶
Initialize the network simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_nodes
|
Optional[Dict[Any, Dict[str, Any]]]
|
Dictionary mapping node IDs to attribute dictionaries. |
None
|
initial_edges
|
Optional[List[Tuple[Any, Any, Dict[str, Any]]]]
|
List of (source, target, attributes) tuples. |
None
|
update_function
|
Optional[Callable]
|
Function that updates the network at each time step. |
None
|
directed
|
bool
|
Whether the network is directed. |
False
|
days
|
int
|
Number of steps to simulate. |
100
|
save_history
|
bool
|
Whether to save node and edge history. |
False
|
random_seed
|
Optional[int]
|
Seed for random number generation. |
None
|
Source code in src/sim_lab/core/network_simulation.py
add_edge(source, target, directed=None, weight=1.0, attributes=None)
¶
Add an edge to the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Any
|
Source node ID. |
required |
target
|
Any
|
Target node ID. |
required |
directed
|
Optional[bool]
|
Whether the edge is directed (defaults to network's directed attribute). |
None
|
weight
|
float
|
Edge weight. |
1.0
|
attributes
|
Optional[Dict[str, Any]]
|
Dictionary of edge attributes. |
None
|
Returns:
Type | Description |
---|---|
Edge
|
The created edge. |
Raises:
Type | Description |
---|---|
ValueError
|
If the source or target node doesn't exist. |
Source code in src/sim_lab/core/network_simulation.py
add_node(node_id, attributes=None)
¶
Add a node to the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id
|
Any
|
Unique identifier for the node. |
required |
attributes
|
Optional[Dict[str, Any]]
|
Dictionary of node attributes. |
None
|
Returns:
Type | Description |
---|---|
Node
|
The created node. |
Raises:
Type | Description |
---|---|
ValueError
|
If a node with the given ID already exists. |
Source code in src/sim_lab/core/network_simulation.py
calculate_metrics()
¶
Calculate metrics for the current network state.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
A dictionary of network metrics. |
Source code in src/sim_lab/core/network_simulation.py
get_adjacency_matrix()
¶
Get the adjacency matrix of the network.
Returns:
Type | Description |
---|---|
ndarray
|
A NumPy array representing the adjacency matrix, with weights if applicable. |
Source code in src/sim_lab/core/network_simulation.py
get_degree_distribution()
¶
Get the degree distribution of the network.
Returns:
Type | Description |
---|---|
Dict[int, int]
|
A dictionary mapping degrees to the number of nodes with that degree. |
Source code in src/sim_lab/core/network_simulation.py
get_edge_attribute_history(source, target, attribute)
¶
Get the history of a specific edge attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Any
|
Source node ID. |
required |
target
|
Any
|
Target node ID. |
required |
attribute
|
str
|
The name of the attribute. |
required |
Returns:
Type | Description |
---|---|
List[Any]
|
List of values for the attribute over time. |
Raises:
Type | Description |
---|---|
ValueError
|
If the edge doesn't exist or history wasn't saved. |
Source code in src/sim_lab/core/network_simulation.py
get_node_attribute_history(node_id, attribute)
¶
Get the history of a specific node attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id
|
Any
|
The ID of the node. |
required |
attribute
|
str
|
The name of the attribute. |
required |
Returns:
Type | Description |
---|---|
List[Any]
|
List of values for the attribute over time. |
Raises:
Type | Description |
---|---|
ValueError
|
If the node doesn't exist or history wasn't saved. |
Source code in src/sim_lab/core/network_simulation.py
get_parameters_info()
classmethod
¶
Get information about the parameters required by this simulation.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
A dictionary mapping parameter names to their metadata. |
Source code in src/sim_lab/core/network_simulation.py
remove_edge(source, target)
¶
Remove an edge from the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Any
|
Source node ID. |
required |
target
|
Any
|
Target node ID. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the edge doesn't exist. |
Source code in src/sim_lab/core/network_simulation.py
remove_node(node_id)
¶
Remove a node from the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id
|
Any
|
The ID of the node to remove. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the node doesn't exist. |
Source code in src/sim_lab/core/network_simulation.py
reset()
¶
Reset the simulation to its initial state.
Source code in src/sim_lab/core/network_simulation.py
run_simulation()
¶
Run the network simulation.
In each step, the network is updated according to the update function.
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
A list of dictionaries containing network metrics for each time step. |