15,795,861 members
Articles / Desktop Programming / WPF
Article

# Fuzzy Logic - Obstacle Avoidance - WPF

Rate me:
13 Jul 2008CDDL6 min read 66.3K   2.5K   58   8
An fuzzy logic control system for a moving object to avoid obstacles.

## Introduction

Yay, my second article for code project. Like my first article this one was also one of my assignments for AI at university and I thought I'd try this one out in WPF using C# as well.

Fuzzy logic - a dictionary like description:
"Fuzzy logic is a mathematical approach to problem solving. It excels in producing exact results from imprecise data, and is especially useful in computers and electronic applications.

Fuzzy logic differs from classical logic in that statements are no longer black or white, true or false, on or off. In traditional logic an object takes on a value of either zero or one; in fuzzy logic, a statement can assume any real value between 0 and 1, representing the degree to which an element belongs to a given set.

The human brain can reason with uncertainties, vagueness, and judgments. Computers can only manipulate precise valuations. Fuzzy logic is an attempt to combine the two techniques." - Paul Wang, Professor of Electrical Engineering

That about sums it up quite nicely. So fuzzy logic is just logic that takes imprecise inputs and ouptus a precise output - a precise problem solving methodology. So lets go about creating a fuzzy logic system for an obstacle avoidance system.

## Fuzzy Logic System

Inputs to a fuzzy system could say the temperature in a room or the color of a particular pixel on screen. The fuzzy logic system works by applying certain rules to the input values to find out the degree that the particular input matches a set of values defined by a rule.

http://blog.peltarion.com/2006/10/25/fuzzy-math-part-1-the-theory/

By looking at the input fuzzy sets above, a person who is 165cm according to the boolean representation, the person would by short. But the fuzzy logic would output that the person is partly short and partly average. The input value 165 is a member of both the short and average domain. So a Fuzzy Logic recognizes not only clear-cut, black-and-white alternatives but also the infinite gradations in between. So in Fuzzy terms the person would classified within a range of [0, 1] as small to a degree of 0.4, and average to a degree of 0.6.

## Designing a Fuzzy Inference System

So lets go about designing a fuzzy system for obstacle avoidance of the car in my assignment.

Fuzzy Inference System

#### First we need to define the input and output variables of the fuzzy system.

Inputs:
1. Angle of the obstacle relative to the direction my car is travelling in
2. Distance to between the obstance and my car
Ouputs:
1. Adjustment Angle of the heading of the car to avoid possible collisions with obstacles

#### Now for the actual parameters of the fuzzy system in 3 simple steps:

1. Specify the fuzzy sets to be associated with each variable
2. Decide on what the fuzzy rules are going to be (can be described linguistically)
3. Specify the shape of the membership functions

#### 1. Fuzzy set

For our input variables - angle could be described as: {small, medium, large} and distance could be described as: {near, far, very far}. Refer to the diagram below, it should make more sense.

#### 2. Rule Evaluation

A rule can be written linguistically like so:

##### if (obstacle angle small) and (obstacle distance near) then (turn sharply).
We can represent all possible combinations of our input variables in a fuzzy associative memory matrix.

Preset Values (determined by you):

##### x1 = 5, x2 = 4, x3 = 3, x4 = 4, x5 = 3, x6 = 2, x7 = 3, x8 = 2, x9 = 1
There is pattern here which you should have picked up by looking at the shades and how they form a diagnol pattern. Usually X2 and X4 will have matching values so you could combine that into a variable say MF as you might usually want to turn the output the same value if the angle is medium and the distance is small or if the distance is far and the angle is small. These table will make sense in the next little section.

Previously I described input variables being members of our fuzzy set to varying degrees of membership. Now if you combine the membership values (between 0 and 1) of all possible combinations that this matrix represents and multiply by a preset constant from X1 to X9 you should receive an output.

#### 3. Defuzzification

So lets say our obstacle is very close in distance and the angle is small so only the the both our input variables will will have a membership of 1 with both angle {small} and distance {large}. All the other possible set will yeild a membership value of 0. So to make it really simple you basically multiply all the membership values and then add all of them and divide by the sum of all the preset values from you FAMM. So for our example you would get X1*(1 x 1) + X2(0 x 0)... / X1 + ... + X9. And this ladies and gentlemen outputs a crisp value of the angle to adjust. Simple as that.

## How I implemented this solution using WPF

Well the best way to find that out is just to dig in through the code. But if you've seen my previous article ( 8 Puzzle ) it uses the same animation technique and uses the animation complete event handler to calculate the adjustment angle between each iteration. Maybe a good extension to this would be to have the Fuzzy Logic running on a separate thread instead of making incrementally doing it.

As far as instructions are concerned, you just click to place the target somewhere and the car will seek the target whilst avoiding the little monsters. There is glitch if you set a new target before the car stops moving.

I will modify this article soon to show a bit more of a detailed walkthrough of the code but it is really well commented and checking out the code and then reading my explanation might be a better way to understand it (or not, i'm just guessing here).

It has been a while since I've done this paper so there will be some minor corrections made once I have the time to properly proof read and make sure I've explained things properly.

## Credits

My lecturer Napoleon Reyes. I'm referring to his lectures as I'm writing this article so thanks Napoloen.

## History

Nothing as of yet...

##### Any feedback/comments that you have would be very much appreciated not to mention motivating =) So please feel free to leave a comment or two, even just to say sup!

Written By
New Zealand
Hey, i'm a student doing my final few papers at Massey Univeristy, Auckalnd, New Zealand =). My degree is a Bachelor of Engineering, majoring as a Software Engineer. I'll be the last B.E. Software Engineer to graduate from Massey, Albany campus... so u know, i'm pretty exclusive kinda =P

I pretty into programming. I'm woking full-time while I'm finishing of the last of my degree.

That's about all I want to share for now I think!

 First Prev Next
 My vote of 5 Kanasz Robert6-Nov-12 1:01 Kanasz Robert 6-Nov-12 1:01
 Very cool but would be nice to see some code actually in the article too Sacha Barber20-May-11 21:48 Sacha Barber 20-May-11 21:48
 hi james aaron fanlo25-Jan-11 7:35 james aaron fanlo 25-Jan-11 7:35
 hey I've read your article it was quite amazing. Would u mind helping us out on our project? that's if your're not busy. Our project is somewhat similar to the one that you have submitted here in codeproject. Our project is about simulating a robot ball gatherer. The robot will gather the ball depending on the color and place it back inside a box that corresponds to the color of the ball. Obstacle avoidance is also present. And the robot's speed will gradually slow don until it reaches the ball. I would really appreciate if you will help me on this. I have been coding for 2 weeks already, and I can't even start the robot move. tsk. It's kinda hard especially for me as a second year student. Thank you very much in advance and hope I can hear from you soon. thanks a lot.
 Bookmarked for later reading DaveAuld3-Jan-11 13:01 DaveAuld 3-Jan-11 13:01
 errors ... [modified] mBenkirane15-Nov-09 21:28 mBenkirane 15-Nov-09 21:28
 Running the project Member 447464818-May-09 21:09 Member 4474648 18-May-09 21:09
 Fuzzy Logic Library for Microsoft .Net Dmitrie21-Mar-09 9:07 Dmitrie 21-Mar-09 9:07
 Re: Fuzzy Logic Library for Microsoft .Net Member 1019550511-Dec-20 9:36 Member 10195505 11-Dec-20 9:36
 Last Visit: 31-Dec-99 19:00     Last Update: 6-Dec-23 3:45 Refresh 1