Building a Traffic Simulation

Have you ever been on a highway going really slowly in heavy traffic and all of a sudden the traffic begins to speed up for no apparent reason at all? Nobody got off. You didn't pass an accident. Nothing seems to have changed except that now the cars are going faster. What happened?

In this lab, you will develop a model of what might affect traffic speed, and build a simulation in Snap! to explore that model.

The first four steps on this page ask you to think about the algorithm you'll use to build this program. You shouldn't be doing anything in Snap! during those steps.

To keep the model from being too complicated to program, here are some simplifying assumptions:

Each of these steps asks you to think about one part of the program. You'll definitely want to talk to your partner. You may even want to write some things down.

  1. Consider the overall setup. Each car is a sprite. All sprites will have essentially the same scripts. The only difference will be where the car starts in line and which car it is following. So, this is what car 5's scripts might look like. (But don't start building them now. Your programming starts in problem 5 below.)

    You will have to invent startpos and drive but avoid hitting sprite.
    Script for sprite 5---when green flag clicked, move to starting position. Script for sprite 5---When space key pressed, drive but avoid hitting sprite 6.

    Because our version of startpos is the same for all sprites, it put all the sprites in the same place. So we needed each sprite to move to space out the cars. Your program might be different.

    The drive but avoid hitting sprite block will repeat a set of actions that might be like this:

    • If too close to next car, change speed to lower speed; otherwise, change to higher speed.
    • If over the speed limit, change to lower speed.
    • If at the end of the stage, go back to the beginning.
    • Move at my own current speed.

  2. Analyze the state of one vehicle. Write Out Your Thoughts What variables will it need?
    • You might want to make variables for the rate of acceleration and the rate of braking so that you can experiment with them.
    • You must have a variable for the cars' speeds: Each car has its own speed, so that variable will be a list of speeds, one for each car.
    • You must set a speed limit, and you might want to be able to experiment with it.
    • Are there other variables you need to keep track of? (Maybe. Maybe not. Keep your model simple. You can always add features later on.)
  3. Analyze the behavior of one vehicle. If it sees a car in front of it, it should slow to the speed of that car. Otherwise, it should speed up. The distance to the car in front should probably matter.
  4. Talk with Your Partner

    Design an algorithm for the behavior. Exactly how will the car's speed change? When it is close enough to the car in front of it, will it just become the speed of that car? Or will it slow down more gradually? It must not crash or pass.

    Below is one possibility. Notice how it uses replace item to replace the old speed with the new speed. This version just replaces it with a speed slightly (and randomly) less than the car in front of it.
    script for slow-down-(mySpriteNumber):  Replace item (mySpriteNumber) of (spriteSpeeds) with (item(SpriteNumber in front of me(mySpriteNumber)) of (spriteSpeeds) minus (pick random 0.1 to 0.5)))

    When the car has lots of room in front of it, how will it speed up? It must not exceed the limit.

  5. NOW, program your algorithm in Snap! Give one sprite the ability to detect its distance to the (not-yet-existent) sprite ahead of it, and write the script that uses that to control its forward motion. Make sure that when a car goes off the screen it comes back in the other side.

    Here are two tools we found useful. You may or may not need them, depending on how you program your model. The 12 in SpriteNumber in front of me was the number of sprites in our model. You may need a different number there. You might also build this tool quite differently.
    NextSpriteName-(NextSpriteNumber): report (join (Sprite left.paren) (NextSpriteNumber) (right-paren) )
    SpriteNumber-in-front-of-me-(MyNumber): report (1 plus (MyNumber mod 12)

  6. Clone your sprite to create enough cars for your simulation.
  7. Make sure the highest-numbered car knows to follow the lowest-numbered car. Depending on how you've programmed, that one behavior might be slightly different from every other car's behavior.
  8. Initialize: Set your cars out in some starting position, and give each car whatever other information it needs to start off.

    One sprite will also need to set the variables that all sprites use. In our model, the script looked like this:
    When-green-flag-clicked-initializer: startpos, set speed-limit to 10, set spriteSpeeds to (empty list) set danger-distance to 20, set pen color to bright red

  9. Test and refine your model: Do the cars keep moving? Do they avoid crashes as they should? Does traffic flow smoothly? Do you see changes in speed? Adjust your model, if necessary, so that it works.