13

I am going to be asking a lot of questions in the upcoming months. For my ninth grade science fair project I would like to create a traffic simulator in order to test whether or not interconnected communicating traffic lights can increase traffic flow. I have a couple of generic questions that I need help with...

  • How would I represent roads?
  • How would I make car follow a road?
  • How would I make a car switch lanes or roads?

I am not looking for specific code, just good pointers and resources to help me get started. Any help is appreciated, C.Ruhl.

PS I am only in high school so no advanced math notations please :)

Conner Ruhl
  • 1,693
  • 4
  • 20
  • 31
  • 2
    Is Javascript a requirement? Why don't you use a tool for generating Agent-based Models such as http://ccl.northwestern.edu/netlogo/. Plus is already has a traffic simulation in there ;), http://ccl.northwestern.edu/netlogo/models/TrafficGrid. – Nick May 15 '11 at 07:24
  • Netlogo is an excellent suggestion, actually. – Charlie Martin May 15 '11 at 15:49

5 Answers5

7

One possible approach which is taken quite often is to use a discrete model for roads and cars' positions.

Road representation

Each position on the road can either be occupied by a car (blue dot) or be empty. Cars move at discrete time steps by exactly one position (if the target position is empty) along the given arrows. Thus a car can even switch lanes if it would otherwise have to slow down or stop.

You can further improve it by using separate timesteps per car (simulating faster/slower cars) or in many other ways.

After you've defined your roads (i.e. the positions and their follow-up positions) by an appropriate data structure this model is relatively easy to simulate but already shows interesting effects.

Howard
  • 38,639
  • 9
  • 64
  • 83
  • This seems like a good way to represent the roads, but two questions... How do you make curved roads? How do you move the car smoothly (it seems like the steps between road segments is pretty large)? Lastly, how big (pixel-wise) would you suggest the road segments be? – Conner Ruhl May 15 '11 at 17:07
  • 1
    @Conner Ruhl The model is more a logical representation of a road/car system than a physical one. While I draw the roads in a straight manner it is also possible to bend it into curves for visual representation. Also the cars cannot run smoothly in such a model (thats what I meant by discrete). A model always lacks some properties of the real object. It is up to you which properties you think are important and which you can neglegt. A good model simplifies reality to a great degree but does not loose the characteristics of the object which you want to observe. – Howard May 15 '11 at 17:15
  • Alright, I will use this as a starting point, but I would like to make it a little more practical – Conner Ruhl May 15 '11 at 17:30
7
  1. Forget about the UI.
  2. Represent each object in its base form --only put object properties in it. Example, a car will have a size and ability to move. But it won't have the logic to make it move. Similarly a traffic light will have states such as green, amber and red. But it won't have the logic to switch between these states. Similar classes for roads, lanes etc.
  3. Build a different class for the driver. This class will contain all methods such as lane shifting, stopping, turning, moving forward etc. More technically, this will be your "actor" and will act on the veichle. A similar actor would be for traffic light control which will act on a network of traffic lights. Make it an interface and have two implementations to it --one that takes advantage of interconnectedness and other that operates on static times.
  4. Optional add a UI on top of this object model. Don't go fancy, have simple dots to begin with. Once you get all simple stuff working, adding more fancy features should be easy and impact free (relatively).

This will be a very challenging project.

But if your objective is a proof of concept, I have a simpler suggestion. You can go user generated here and get all the complexity of simulation out and all the accuracy in. Start with 15-20 remote controlled cars, a cardboard model of a fictional town, some bulbs to simulate traffic lights and some volunteers who know how to drive. Have a preprogrammed sequence of on and offs written on paper and assign some of the volunteers to control those lights. Have another set of volunteers control the cars. If you have hands on experience in basic electronics you can build a timer controlled circuit to control the lights.

All the very best!

Apoorv
  • 1,091
  • 6
  • 19
  • This year I participated in the Intel International Science and Engineering Fair at Los Angeles and after seeing the projects there I don't believe a physical model will make the cut :) I do like your answer though it offers some good advice on the programming bit. – Conner Ruhl May 15 '11 at 16:54
2

You could try the SIM.JS discrete event simulation library in Javascript. They have a very simple example for traffic at road intersection simulation here.

mvarshney
  • 364
  • 1
  • 8
1

Ooh, Conner, you've found an interesting question indeed -- and one which is the subject of research even today. Here's a suggestion: before you fret about how to do it in JavaScript, spend some time thinking just how to do it at all.

Here's a suggestion: think about the objects invovled first. You have Cars, and they travel along Roads. Start with a square grid of roads, so your cars go from intersection to intersection.

Pick a fixed speed for the cars, so it takes a constant time to travel from intersection to intersection.

Each intersection has a traffic light, which can be red or green. If it's red, of course cars can't go through; they have to wait.

Now, your basic program will look like

time = 0
while time < end-time:
    for each car:
        update the car's location
    add time consumed to time

when you update the cars location, what happens? (Hint: the car moves; can it go through an intersection or not?)

That will give you a start.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • But remember that you cannot use a while/for loop in javascript to update stuff while other things happen. You will need to release processor time by using setTimeout or setInterval – mplungjan May 15 '11 at 06:57
  • Yes, last year's science fair project involved an alpha-beta search algorithm for checkers. It was a nightmare to incorporate a setTimeout to that, but it did increase speed. – Conner Ruhl May 15 '11 at 16:57
1

For my Bachelor's degree exam I developed a traffic control web-app that tracked the vehicles in my town in real-time and I used google maps api.
I suggest you use a map service such as maps.google.com , yahoo.maps.com...
They have an api for everything... you can use markers to represent anything on the map (cars,street lights,even pedestrians :)) ) and you can use their api to calculate distances and paths.
It may seem a bit more complex then the average div-implementation, but, trust me, it's a big plus to use a service with a well-organised api.
+it would have a more professional look ;).

gion_13
  • 41,171
  • 10
  • 96
  • 108