Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
99 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

The output

The output needs to control the vehicle's speed and direction. That would be the acceleration, the brake and the steering wheel. So we need 2 outputs; one will be the acceleration/brake since the brake is just a negative acceleration, and the other will be the change in direction.

The output from the neural network is also between 0.0 and 1.0 for the same reason as the input. For the acceleration, 0.0 means "full brakes on"; 1.0 means "full gas" and 0.5 means no gas or acceleration. For the steering, 0.0 means full left, 1.0 means full right and 0.5 means strait ahead. So we need to translate the output into values that can be used.

We should note that a "negative acceleration" means braking if the vehicles is moving forward, but it also means going into reverse if the vehicle is at rest. Similarly, a "positive acceleration" means braking if the vehicle is moving in reverse.

Training

As I mentioned earlier, we need to train the neural network to approximate the function we need it to accomplish. We need to create a set of input-output that would be the main reaction we want the neural network to have.

Choosing the right input-output to train a neural network is probably the trickiest part of all. I had to train the network with a set of input-output, see how it reacted in an environment, and modify the entries as needed. Depending on how we train the network, the vehicle can hesitate in some situations and get immobilized.

We make a table (Table 1) with different arrangements of obstacle positions relative to the vehicle and the desired reaction from the AI.

Table 1
Input Neurons
Relative distance to obstacle
Output Neurons
Left Center Right Acceleration Turn
No obstacle No obstacle No obstacle Full Acceleration straight
Half way No obstacle No obstacle Accelerate a bit Little bit to the right
No obstacle No obstacle Half way Accelerate a bit Little bit to the left
No obstacle Half way No obstacle Slow down Little bit to the left
Half way No obstacle Half way Accelerate straight
Touching Object Touching Object Touching Object Reverse Left
Half way Half way Half way No Action Little bit to the left
Touching Object No obstacle No obstacle Slow down Full Right
No obstacle No obstacle Touching Object Slow down Full Left
No obstacle Touching Object No obstacle Reverse Left
Touching Object No obstacle Touching Object Full Acceleration straight
Touching Object Touching Object No obstacle Reverse Full Right
No obstacle Touching Object Touching Object Reverse Full Left
Object Close Object Close Object Very Close No Action Left
Object Very Close Object Close Object Close No Action Right
Touching Object Object Very Close Object Very Close Slow down Full Right
Object Very Close Object Very Close Touching Object Slow down Full Left
Touching Object Object Close Object Far No Action Right
Object Far Object Close Touching Object No Action Left
Object Very Close Object Close Object Closer than halfway No Action Full Right
Object Closer than halfway Object Close Object Very Close Slow down Full Left

And now, we translate this into numbers in Table 2.

Table 2
Input Neurons Output Neurons
Left Center Right Acceleration Turn
1.0 1.0 1.0 1.0 0.5
0.5 1.0 1.0 0.6 0.7
1.0 1.0 0.5 0.6 0.3
1.0 0.5 1.0 0.3 0.4
0.5 1.0 0.5 0.7 0.5
0.0 0.0 0.0 0.2 0.2
0.5 0.5 0.5 0.5 0.4
0.0 1.0 1.0 0.4 0.9
1.0 1.0 0.0 0.4 0.1
1.0 0.0 1.0 0.2 0.2
0.0 1.0 0.0 1.0 0.5
0.0 0.0 1.0 0.3 0.8
1.0 0.0 0.0 0.3 0.2
0.3 0.4 0.1 0.5 0.3
0.1 0.4 0.3 0.5 0.7
0.0 0.1 0.2 0.3 0.9
0.2 0.1 0.0 0.3 0.1
0.0 0.3 0.6 0.5 0.8
0.6 0.3 0.0 0.5 0.2
0.2 0.3 0.4 0.5 0.9
0.4 0.3 0.2 0.4 0.1

Input:
0.0 : Object almost touching vehicle.
1.0 : Object at maximum view distance or no object in view

Output:
Acceleration
0.0 : Maximum negative acceleration (brake or reverse)
1.0 : Maximum positive acceleration
Turn
0.0 : Full left turn
0.5 : Straight
1.0 : Full Right turn

Conclusion / Improvement

Using a neural network with back propagation works well for our purposes, but there are some problems once implemented and tested. Some improvement or modification can be done to either make the program more reliable or to adapt it to other situations. I will now address some of the problems you might be thinking about right now.


Figure 5

The vehicle does get "stuck" once in a while because it hesitates deciding whether it should go right or left. That should be expected: humans have the same problem sometimes. Fixing this problem is not so easy if we try to tweak the weights of the neural network. But we can easily add a line of code that says:

"If (vehicle doesn't move for 5 seconds) then (take over the controls and make it turn 90 degrees right)".

With this simple check, we ensure that the vehicle will never get in a place not knowing what to do.

The vehicle will not see a small gap between two buildings as shown in figure 5. Since we don't have a high level of precision in the vision (left, center, right), two buildings close together will seems like a wall for the AI. To have better vision of our AI, we need to have 5 or 7 levels of precision in the input for the neural network. Instead of having "right, center, left" we could have "far right, close right, center, close left, far left". With a careful training of the neural network, the AI will see the gap and realize that it can go through it.

This works in a "2D" world, but what if we have a vehicle capable of flying through a cave? With some modification of this technique, we can have the AI fly rather than drive. Similar to the last problem, we add precision to the vision. But instead of adding a "far right" and a "far left", we can do as shown in Table 3.

Table 3
Upper left Up Upper right
Left Center Right
Lower left Down Lower right

Now that our neural network can see in 3D we just need to modify our controls and the physics on how the vehicle reacts to the world.

All the vehicle does is "wander around" without purpose. It doesn't do anything other than avoiding obstacle. Depending on why we need an AI driven vehicle, we can "swap" brains as needed. We can have a set of different neural networks and use the right one according to the situation. For example, we might want the vehicle to follow a second vehicle if it is in its field of view. We just need to plug another neural network trained to follow another vehicle with the position of the second vehicle as input.

As we have just seen, this technique can be improved and used in many different areas. Even if it is not applied to any useful purpose, it is still interesting to see how an artificial intelligent system reacts. If we watch it long enough, we realize that in a complex environment, the vehicle will not always take the same path and because of the small difference in decision due to the nature of neural network, the vehicle will sometimes drive to the left of a building, and sometimes to the right of the same building.

References

Joey Rogers, Object-Oriented Neural Network in C++, Academic Press, San Diego, CA, 1997

M.T. Hagan, H.B. Demuth and M.H. Beale, Neural Network Design, PWS Publishing, Boston, MA, 1995




Contents
  Introduction
  The input
  The output

  Printable version
  Discuss this article