Skip to main content

All is Fuzzy

This is an excellent starter for all fuzzy logic programmers.

By Seshi from New Zealand, read the original text here:
http://www.codeproject.com/KB/recipes/Seshi_FuzzyLogic.aspx

Introduction

Sample Image - maximum width is 600 pixels

Download source - 395 Kb

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 outputs 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
Fuzzy Infrence 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 obstacle 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 diagonal 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 yield 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.

Read more:  http://sourceforge.net/projects/fuzzynet

Comments

Popular posts from this blog

My love affair with ThinkPads

I had never used a ThinkPad in my life till 2018. To be honest, I did not really see the benefit of using them anyway. Consumer series laptops (with plastic bodies) were the ones that I had always used before. Then I was introduced to the rugged looks of the T60 and T61 original IBM ThinkPad designs that came out way back in 2004 or so. And I loved it. Finding a ThinkPad that old did not appeal to me. I was impressed with the smaller compact X series laptops and decided to have either an X230 or an X220. Both of these are fairly similar and were launched about 8 years back. But they still sport good processors, plenty of memory upgradability, provision to add a maximum of three hard disks and a decent battery backup to boot. My reasons to get this were simpler -  They are rugged, I like to use my laptops well Memory is not soldered. There are plenty of options to upgrade. The keyboards are probably the best in the world. Why not get the latest ThinkPad? Again, simple.  They come with a

How To: Change Fonts On The 5800, 5233 etc

The font that comes with Nokia is nice. But after six months you are bound to crave for a new look to your phone. Now changing fonts on a phone is usually not an easy task, I mean that was how you must have thought. But you know what you can use custom fonts, yes any font of your choice on your Nokia phone without any software. All you need to do is copy and paste a bunch of files. No replacing of system resources necessary! First to increase your greed, I will post some screenshots, Screenshot Gallery o   Font Style Download o Another Font Style Now let me tell you how I did it. Procedure to apply new Fonts Connect the phone in the ‘Mass Storage’ profile to the computer. Alternatively, you can use the memory card reader. Goto the Memory Card, it shows up as a Removable Disk. Navigate to the ‘resource’ folder and open it. Copy the ‘Fonts’ folder into in. Eject the device and restart. You should see the new fonts applied. Procedure to apply your Fonts Ma

Reaching the peaks of my own stupidity or not

 I have never considered myself to be particularly intelligent. Though I have not had myself clinically diagnosed, anecdotal evidence and day to day activities amply demonstrate how stupid I am. Now wait. This does not mean I am indulging in self pity.  No. Far from it. I have had successes in life. I continue to do well in things that I love to do. I have no complaints there. It is just that I do feel my intelligence lacking. My success is, therefore, despite my stupidity and I take some pride in it. Of course defending the statement - 'I am stupid' is difficult. Especially without a test that ascertains the fact for certain. I will make a more nuanced statement as a result. I have less intelligence than the average person around me. That is a more defensible stance and there are tests (even other than IQ) to confirm it. Aptitude tests are a good example. I do horrible in them.  Okay, there is an exception - I got into IITB. But that was me trying for a third time. In any case