Driver Behavior

Read time: 28 minutes (7193 words)

How do drivers behave behind the wheel, especially on I-35 in “rush hour”?

LIKE IDIOTS! (Excuse the outburst, I must have just exited that highway!)

Drivers react to the surroundings, and we need to simulate that. But Exactly how do we do that>

Pool Ball Simulation

In our Pool Ball simulation, we gave the ball very little “behavior”. It could move, and that was about all it could do!

We gave the real power to the table where the ball was moving. That table object could look at the balls, and adjust their velocity values as it saw fit. It watched for collisions with the walls, and it watched for collisions with other balls. That table was the master of the simulation. The result looked pretty good, so that method worked fine.

In this case, the vehicle has a lot more issues to deal with before it can move. Allowing a supervising object to control everything will not work very well, because each vehicle owns a bunch of behavior that the supervising object knows nothing about.

So, it makes sense for the highway object in our simulation to simply notify each vehicle of some basic facts about the world that vehicle is living in. The vehicle will decide what to do with all of those facts as it wishes. This sounds like a real world approach to solving this problem, and that is what object-oriented design is all about!

Highway Observer

We can imagine that the highway object is a camera sitting high above the road, with a clear view of every vehicle tied up in the mess below. It can see where all of the vehicles are, but maybe it cannot detect their speed.

Note

In the old days, there used to be spotters flying over roads measuring the speed using lines painted on the road They would radio the police to caththose speeders! Today, rdar on the ground does all of that!

What it obviously needs to do is notify each vehicle of some basic information concerning the world around that particular vehicle:

  • distance to the car directly in front of them

  • distance to the closest car to the left or right ahead

  • distance to the car directly behind them

  • distance to the cars approaching from behind.

The speeds of all of these cars surrounding our particular car might be a concern as well, so maybe we provide that information as well.

Evaluating the next move

The vehicle is now armed with a lot of information (just as a real driver is when they look around using mirrors. The actions are simple for starters:

  • continue in the same lane

  • try to move to another lane

  • move to an exit ramp.

Wait, we need to know if each car wants to exit, and the distance to that exit. Our random number generator might need to help decide were the car wants to exit (ramp of end of our road).

The vehicle will need to process the information to decide what to do.

Continue in the Current Lane

If the distance to the vehicle in front is too close, the vehicle needs to slow down. If the distance gets large enough, the vehicle might need to speed up. How fast they can do this depends on the driver reaction time, the speed of the vehicle, and how fast that vehicle can speed up or slow down once told to by the driver. Sheesh, this is a lot of calculations.

Once we decide what our new speed is, we move forward, just like our pool ball did.

Changing Lanes

If a decision is made to change lanes, we need to make sure there is room. If there is another vehicle in that lane, we cannot do that, but the desire to change is still there. We need to wait for a hole in the traffic in the next lane opens up big enough to suit this driver. Then they start to change lanes.

How long it takes them to change lanes is another issue. Some zoom from lane to lane, other take FOREVER to get the job done.

Some drivers seem to prefer a certain lane, and if they pass, they will return to their preferred lane.

Exiting the Road

When we move to an off ramp, or reach the end of the road, we need to remove the vehicle from the simulation, and add that object store to the inactive list. When we need a new vehicle, we will check the inactive list to see if one is available, if so we use that one, otherwise, we create a new vehicle object and add it to the active list.

Crashes

Sad to say, sometimes things go bump! Since we are not going to instantaneously change the speed of a vehicle, there will be times when a vehicle cannot slow down enough to avoid a collision. What happens them is an issue we need to address.

Perhaps the vehicles just stop in place and do not move again. That surely happens. However, it might be more interesting to simulate what often happens. Two cars collide and go spinning off in random directions, slowing down as they move until they hit another vehicle (sounds like pool balls again) or they hit a wall and just stop there. This will cause a mess, and obviously traffic will have to adapt to all of this.

Passing a Crash

Drivers are silly. If they see a crash, they slow down to gawk. Some times drivers on the opposite side of the highway do exactly that, for no other reason that to look things over. The result is a slow-down where none is really called for!

The same thing happens if there is a police vehicle on the side of the road.

Police

Maybe we need to simulate police as well. If we position a police car along the highway, and change it with watching for speeders, it can jump into the lane (in an appropriate hole, or course), and speed off trying to catch the offending speeder. Then both of them should head to the nearest side of the road, or an off ramp It would be fun to show the police car with a flashing red light, something we can definitely do!

This Sounds Hard

Remember Baby Steps! DO not try to tackle this all at once, we start off slowly, and add behaviors as we think it up.

For starters, we move a simple vehicle along the highway at a fixed speed, just to make sure it will move along all the segments of our highway. Next we add more vehicles, using our linked list and random number generator to inject them into the simulation. Those vehicles will all move along smoothly as well.

We probably need to make sure we do not collide with other cars in the same lane. The distance to the car in front is the only data item needed for this change. A simple start on this is to set some minimum distance between cars, and if the car gets to close, change that car’s speed to the speed of the vehicle in fromt of them.

Note

Sitting here writing all of this, it occcurred to me that maybe providing each vehicle with a list if the cars “close” to them would work, then the vehicle class could check each cloe vehicle to determine the data needed. The highway would need ot jelp build that list.

Next, it makes sense to try changing lanes. This is the first time you really need to add some hard “thinking” to the vehicle behavior. We need to process data and decide when to switch lanes. For my initial simulation, I simply changed the lane number and instantly drew the vehicle in the new lane. Not realistic, but a decent start.

MOre to Go

Obviously, this simulation could grow into a monster, as we see new things to add. The real value in working on a project like this is that it forces you to explore how to provide access to needed data, and control what happens. As the project grows, the amount of detail you need o think through grows as well.

Welcome to real world development! It is hard work, but rewarding as well, especially when you see the results running for real!