DESRu (pronounced "dez-ruh") is a discrete event simulation package for Rust.
This Rust crate provides a flexible framework for simulating Discrete Event Systems (DES). It allows users to schedule, manage, and execute events over time, making it suitable for simulating systems such as queueing networks, resource allocation systems, and other event-driven models.
- Event Scheduling: Schedule events to occur at specific times or after delays.
- Event Logging: Keep a log of all executed events and their outcomes for post-simulation analysis.
- Flexible Execution: Run simulations for a specific duration or until a custom stopping condition is met.
- Contextual Information: Attach metadata to events for richer simulation context and behavior customization.
$ cargo add desru
use desru::{Event, EventScheduler};
fn main() {
let mut scheduler = EventScheduler::new();
let event = Event::new(
0.0,
Some(Box::new(|scheduler| Some("Executed".to_string()))),
None
);
scheduler.schedule(event);
scheduler.run_until_max_time(10.0);
}
Inspired by the simple clock example in the SimPy documentation.
use desru::{Event, EventScheduler};
fn clock(scheduler: &mut EventScheduler, name: String, tick: f64) {
// Function to handle the clock's actions and schedule the next tick
fn action(scheduler: &mut EventScheduler, name: String, tick: f64) {
// Print the name of the clock and the current simulation time
println!("{}: {}", name, scheduler.current_time);
// Schedule the next tick of the clock
let next_time = scheduler.current_time + tick;
let event = Event::new(
next_time,
Some(Box::new(move |scheduler: &mut EventScheduler| {
action(scheduler, name.clone(), tick);
None
})),
None,
);
scheduler.schedule(event);
}
// Schedule the first event for the clock at time 0
scheduler.schedule(Event::new(
0.0,
Some(Box::new(move |scheduler: &mut EventScheduler| {
action(scheduler, name.clone(), tick);
None
})),
None,
));
}
fn main() {
// Initialize the event scheduler
let mut scheduler = EventScheduler::new();
// Schedule initial clock processes
clock(&mut scheduler, "fast".to_string(), 0.5);
clock(&mut scheduler, "slow".to_string(), 1.0);
// Run the scheduler until the maximum simulation time
scheduler.run_until_max_time(2.0);
}
This example replicates the classic SimPy Car simulation, where a car alternates between parking and driving.
use desru::{Event, EventScheduler};
const PARK_DURATION: f64 = 5.0;
const DRIVE_DURATION: f64 = 2.0;
fn car(scheduler: &mut EventScheduler) {
park(scheduler);
}
fn park(scheduler: &mut EventScheduler) {
println!("Start parking at {}", scheduler.current_time);
scheduler.schedule(Event::new(
scheduler.current_time + PARK_DURATION,
Some(Box::new(move |scheduler: &mut EventScheduler| {
drive(scheduler);
None
})),
None,
));
}
fn drive(scheduler: &mut EventScheduler) {
println!("Start driving at {}", scheduler.current_time);
scheduler.schedule(Event::new(
scheduler.current_time + DRIVE_DURATION,
Some(Box::new(move |scheduler: &mut EventScheduler| {
park(scheduler);
None
})),
None,
));
}
fn main() {
let mut scheduler = EventScheduler::new();
car(&mut scheduler);
scheduler.run_until_max_time(15.0);
}
This example uses a more object-oriented approach to simulate a car alternating between charging and driving.
use desru::{Event, EventScheduler};
const CHARGE_DURATION: f64 = 5.0;
const TRIP_DURATION: f64 = 2.0;
struct Car<'a> {
scheduler: &'a mut EventScheduler,
}
impl<'a> Car<'a> {
fn new(scheduler: &'a mut EventScheduler) -> Self {
let mut car = Car { scheduler };
car.charge();
car
}
fn charge(&mut self) {
println!("Start charging at {}", self.scheduler.current_time);
self.scheduler.schedule(Event::new(
self.scheduler.current_time + CHARGE_DURATION,
Some(Box::new(move |scheduler: &mut EventScheduler| {
let mut car_instance = Car { scheduler };
car_instance.drive();
None
})),
None,
));
}
fn drive(&mut self) {
println!("Start driving at {}", self.scheduler.current_time);
self.scheduler.schedule(Event::new(
self.scheduler.current_time + TRIP_DURATION,
Some(Box::new(move |scheduler: &mut EventScheduler| {
let mut car_instance = Car { scheduler };
car_instance.charge();
None
})),
None,
));
}
}
fn main() {
let mut scheduler = EventScheduler::new();
let _car = Car::new(&mut scheduler);
scheduler.run_until_max_time(15.0);
}
The Event
struct represents a discrete event in the simulation. Each event has:
- A scheduled time.
- A closure (the action) to be executed when the event is triggered.
- Contextual metadata for storing key-value pairs.
The EventScheduler
manages the execution of events. It processes events in order of their scheduled times, executing them and then advancing the simulation time.
- Keep it simple.
- Keep it performant.
- Keep it to the fundamentals.