Fuzzy Logic Controlled Race car
PROJECT TIMESCALE: february 2016 - march 2016
Below is the report I wrote on this project.
Introduction
Artificial intelligence is used in games to control non playable characters (NPCs) and entities. This can involve moving shapes using pathfinding algorithms such as A* or having entities learn complex behaviours with neural networks. A technique called fuzzy logic is utilised in games for NPC decision making.
Fuzzy logic is a multi value logic system that takes crisp numbers and “fuzzifies” them. This means that values can be interpreted in a manner similar to how humans interpret them, such as “small”, “medium” and “big” or “near” and “far”. These concepts allow an AI to be more flexible in the input it takes and the output it produces. Fuzzy logic is used in this project to control the steering of a race car.
The application simulates a car moving left and right to stay on a racing line. The car takes in two inputs; the distance from the car to the racing line and the car’s horizontal linear velocity. It then uses these inputs to calculate how much the car should turn left or right to move in the direction of the line. The program has a manual mode and an automatic mode. In the manual mode, the user can adjust the input values with the arrow keys and see the results in the console window. In the automatic mode, the program will use a red rectangle and a black rectangle to represent the car and racing line respectively. Every frame of a game loop, the inputs will be calculated and the red car will move by adding the output value to its current horizontal speed. The user will be able to move the racing line left and right with the A and D keys. Pressing the spacebar will toggle between the two modes of the application.
Methodology
Fuzzy logic, developed from the theory proposed by Lotfi Zadeh, is used to control electronic systems such as traffic lights or washing machines. Instead of binary like functions, which can only take the value of 0 or 1, membership functions (MFs) are used. These MFs can take any value between 0 and 1. Also, they are able to overlap each other which is where the “fuzzy” nature of the values comes from. MFs are created for both the inputs and outputs of a fuzzy inference system (FIS). After the MFs are defined, rules can be stated to calculated the output based off of the input. For instance, a controller for an air conditioner may take the current room temperature as an input and output the speed to set the fan too. MFs for the input could be “hot”, “warm” and “cold” whilst the output membership functions could be “fast”, “slow” and “off”. An example rule would be something like IF temperature IS “hot” THEN fan speed IS “fast”. Although these fuzzy values are easy for humans to understand; they are beyond what a computer can understand. This means that FISs need a way to defuzzify the values and turn them into crisp ones. Some common methods of defuzzification are finding the centroid, finding the bisector and finding the middle, smallest or largest of maximum.
Implementation
The FIS for this application was designed using Matlab’s Fuzzy Toolbox. The fuzzy sets for the two inputs and the output each have 5 MFs. The MFs of the first input, “DistanceFromRacingLine”, are “FarLeftOfLine”, “LeftOfLine”, “OnLine”, “RightOfLine” and “FarRightOfLine”. The “OnLine” MF uses a triangle shape since it is only fully true at a value of zero when there is no distance between the car and the racing line. The other four MFs all use a trapezoid shape, where a range of values can have full membership. The second input, “LinearVelocity”, has “MovingLeftFast”, “MovingLeft”, “NoMovement”, “MovingRight” and “MovingRightFast” as MFs. Like the first input, this set uses a triangle shape for one MF, “NoMovement”, and trapezoid shapes for the rest. The output set, “Steering”, MFs are “HardLeft”, “Left”, “NoSteering”, “Right” and “HardRight”. This fuzzy set only uses triangle shapes since trapezoid shapes would produce a smaller range of output values. The three fuzzy sets can be seen in the pictures below.
Introduction
Artificial intelligence is used in games to control non playable characters (NPCs) and entities. This can involve moving shapes using pathfinding algorithms such as A* or having entities learn complex behaviours with neural networks. A technique called fuzzy logic is utilised in games for NPC decision making.
Fuzzy logic is a multi value logic system that takes crisp numbers and “fuzzifies” them. This means that values can be interpreted in a manner similar to how humans interpret them, such as “small”, “medium” and “big” or “near” and “far”. These concepts allow an AI to be more flexible in the input it takes and the output it produces. Fuzzy logic is used in this project to control the steering of a race car.
The application simulates a car moving left and right to stay on a racing line. The car takes in two inputs; the distance from the car to the racing line and the car’s horizontal linear velocity. It then uses these inputs to calculate how much the car should turn left or right to move in the direction of the line. The program has a manual mode and an automatic mode. In the manual mode, the user can adjust the input values with the arrow keys and see the results in the console window. In the automatic mode, the program will use a red rectangle and a black rectangle to represent the car and racing line respectively. Every frame of a game loop, the inputs will be calculated and the red car will move by adding the output value to its current horizontal speed. The user will be able to move the racing line left and right with the A and D keys. Pressing the spacebar will toggle between the two modes of the application.
Methodology
Fuzzy logic, developed from the theory proposed by Lotfi Zadeh, is used to control electronic systems such as traffic lights or washing machines. Instead of binary like functions, which can only take the value of 0 or 1, membership functions (MFs) are used. These MFs can take any value between 0 and 1. Also, they are able to overlap each other which is where the “fuzzy” nature of the values comes from. MFs are created for both the inputs and outputs of a fuzzy inference system (FIS). After the MFs are defined, rules can be stated to calculated the output based off of the input. For instance, a controller for an air conditioner may take the current room temperature as an input and output the speed to set the fan too. MFs for the input could be “hot”, “warm” and “cold” whilst the output membership functions could be “fast”, “slow” and “off”. An example rule would be something like IF temperature IS “hot” THEN fan speed IS “fast”. Although these fuzzy values are easy for humans to understand; they are beyond what a computer can understand. This means that FISs need a way to defuzzify the values and turn them into crisp ones. Some common methods of defuzzification are finding the centroid, finding the bisector and finding the middle, smallest or largest of maximum.
Implementation
The FIS for this application was designed using Matlab’s Fuzzy Toolbox. The fuzzy sets for the two inputs and the output each have 5 MFs. The MFs of the first input, “DistanceFromRacingLine”, are “FarLeftOfLine”, “LeftOfLine”, “OnLine”, “RightOfLine” and “FarRightOfLine”. The “OnLine” MF uses a triangle shape since it is only fully true at a value of zero when there is no distance between the car and the racing line. The other four MFs all use a trapezoid shape, where a range of values can have full membership. The second input, “LinearVelocity”, has “MovingLeftFast”, “MovingLeft”, “NoMovement”, “MovingRight” and “MovingRightFast” as MFs. Like the first input, this set uses a triangle shape for one MF, “NoMovement”, and trapezoid shapes for the rest. The output set, “Steering”, MFs are “HardLeft”, “Left”, “NoSteering”, “Right” and “HardRight”. This fuzzy set only uses triangle shapes since trapezoid shapes would produce a smaller range of output values. The three fuzzy sets can be seen in the pictures below.
Rules were written based off of the fuzzy associative map seen below.
A rule was written for each permutation of the two inputs. The Min function is used to check if two statements are both true. This acts as the AND operation found in boolean logic. Most of the rules written for the FIS can be seen in the following picture.
The centroid method is used to defuzzify the values into crisp numbers since it produces more satisfactory results than the other methods described previously.
The application was written in C++ using an open source library called Fuzzylite. Fuzzylite was chosen since it allowed the FIS designed in Matlab to be easily translated into C++ code. Another open source library, SFML (Simple Fast Media Library), was used to program the graphical output.
Results
Below is an example of a set of inputs and their corresponding output from the FIS built in Matlab’s fuzzy toolbox.
The application was written in C++ using an open source library called Fuzzylite. Fuzzylite was chosen since it allowed the FIS designed in Matlab to be easily translated into C++ code. Another open source library, SFML (Simple Fast Media Library), was used to program the graphical output.
Results
Below is an example of a set of inputs and their corresponding output from the FIS built in Matlab’s fuzzy toolbox.
Here is a set of example inputs used within the C++ application and their corresponding output values.
Both the Matlab FIS and the application produce expected results. They produce a wide array of possible outputs which would require significantly more difficulty and complexity to achieve without using fuzzy logic. The following shows the output surface of the FIS; illustrating the full range of possible outputs.
Conclusions
This project showed the power in utilising fuzzy logic for a control system. Even though the application only has the race car moving left and right towards a line, it already has a lot of the tools needed to simulate an AI controlled car in a complete racing game. Using curves instead of a line would result in a car able to follow complex paths, such as a race track. Adding a physics engine could then create more realistic movement for the car.
In terms of the fuzzy inference system; more experimentation could be done with different defuzzification methods and membership functions to see the effects on the race car. Other inputs could be added, such as the distance from a player’s car, in order to add further complexity to the AI behaviour. More inputs would mean an exponential increase in the number of rules required though. To combat this, unwanted states can be removed.
In general, fuzzy logic is a smart way to describe numbers to a computer in a way that is more natural to humans. Fuzzy logic has the potential to make game entities seem more like believable characters to players.
This project showed the power in utilising fuzzy logic for a control system. Even though the application only has the race car moving left and right towards a line, it already has a lot of the tools needed to simulate an AI controlled car in a complete racing game. Using curves instead of a line would result in a car able to follow complex paths, such as a race track. Adding a physics engine could then create more realistic movement for the car.
In terms of the fuzzy inference system; more experimentation could be done with different defuzzification methods and membership functions to see the effects on the race car. Other inputs could be added, such as the distance from a player’s car, in order to add further complexity to the AI behaviour. More inputs would mean an exponential increase in the number of rules required though. To combat this, unwanted states can be removed.
In general, fuzzy logic is a smart way to describe numbers to a computer in a way that is more natural to humans. Fuzzy logic has the potential to make game entities seem more like believable characters to players.