I finally managed to pick a weekend coding project that was actually weekend sized: I wrote a little program in Ruby to simulate bus passenger loads during rush hour. You can grab the code for it on my website. It’s just a rough draft, but it’s kind of fun to run a couple of times, changing the rates of people getting on and busses being added to the route.
I started this because I was thinking about how annoying it is that people living closer to downtown (like me) often have to watch a bus or two go by in the morning, before one with enough room finally stops. It seems like you could avoid this by either increasing the frequency of buses (though with the other traffic, anything more than the current ~7 minute spacing is likely to result in clumps of buses rather than a steady flow) or by adding an express bus every so often to pick up passengers from the more active stops. My model is still too simple to show any of this accurately, but if you add more than one person per stop per cycle, the people closest to the end of the route rarely find room on a bus.
Changes that would improve the simulation:
- Currently each bus moves down the route every cycle, and passengers are also added to stops at the same time. It would work in a more realistic manner if I made the cycles more like minutes, and used that to provide more flexibility to move each bus and add waiting passengers at different rates.
- The output is just four lines of text, representing the stops, people waiting, bus locations, and bus passengers. I could create a better graphical output for this.
- The only way to change any settings for the simulation is to alter the code. It would be pretty simple to ask the user for input before starting each time, or to read in parameters from a file.
- No one is getting off the bus. The rate of people leaving the bus in the last few miles to downtown is small, in my experience, but there are at least a few people getting off earlier most mornings, and I should include that.