Tuesday, December 20, 2016

Cron Based Discrete Event Simulator

In my last post I showed how to use the croniter package to find all the cron events that occur between two dates.  I wanted to see if it was suitable for integration into my discrete event simulator, and after a overhaul of the original simulator that's exactly what I've done.  To demonstrate the functionality it's helpful to have a real world scenario to model.

Scenario

A programmer is addicted to energy drinks and every night at 8 pm they put all the empty cans on their desk in the bin.  Some days are more stressful than others so the number of cans taken out is a random number between 0 and 10 included.  The rubbish bin is emptied every Monday morning at 8 am.  The programmer only works from home January through April but the number of cans in the bin needs to be determined between December to May.  The amount of rubbish in the bin needs to be monitored every 30 minutes.

The first step is to define tasks that need to be performed.  They are simply functions that accept a variable describing the simulation environment.

Define Events

The parameters to initialise the simulator include a start time, an end time, and a default location.  The times are made up of a year, month, day of the month, hour, minute, second and an optional location that is a valid entry for the pytz package.  If no location is supplied for the times, the default location is used.  Other parameters specific to the state of the simulation are included as parameters.  Locations are needed as the underlying simulator uses unix timestamps as the timebase.  This prevents ambiguous time due to daylight saving and different time zones.  Essentially a time and date is useless information without a location.

Initialise the simulator

The tasks are scheduled to run via a cron string and a location for the event.  If a location isn't supplied the default simulation location is used.  You can also set a range for the cron events to run.  In the example below the log_data and the empty_rubbish tasks don't have start and end times so they run for the entire simulation.  The take_out_trash task runs for a shorter period of time and has defined start and end times.  The simulation is then run with the results logged to file.

Schedule Events

The results show that on one occasion there are at more than 50 cans in the bin.  This could be used to determine the size of a bin to buy if the current one is too small.  Let's say you have a bin that takes only 40 cans, it wouldn't be too much trouble to calculate the percentage of the time that there are more than 40 cans to be placed in it.  You may decide that it's acceptable for the bin to be overfull for a certain percentage of the time.  It's all about quantifying data from the simulation and using it to make informed decisions instead of just going by your gut.

Simulation Results

This is a work in progress and there are parts I want to improve, and although this scenario is simple I hope I've demonstrated the value of simulating a situation.
Get the Code!


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.