SIGN UP MEMBER LOGIN:    
ARTICLE

C# Introduction to Fuzzy Computing: Part I

Posted by Matthew Cochran Articles | Visual C# November 15, 2006
Boolean logic often fails to adequately simulate real-world conditions because things are never simply just black and white (or zeroes and ones) but exist in millions of shades of grey. This article is a brief introduction to the subject of fuzzy computing through demonstration of a few of the core principles using a fuzzy controller.
Reader Level:
Download Files:
 

Fuzziness?

Just like a light switch the value is either "on" or "off", we all know the value for a Boolean is either a False (zero or "off") or a True (one or "on").  Fuzzy numbers fit in the infinite space between the Boolean zero and one and provide us with a "dimmer" switch through which to view the world. The applications of this "dimmer switch" approach are all around us in products such as anti-lock breaks, washing machines,  thermostats, auto-focus cameras, and more.

Here is a simple example of what we are talking about: If you have someone who is 6ft (72in) tall, would they be considered tall or short? It's not that simple, right.  They are kind of tall, or depending on the population they are being compared against, they could be considered not tall at all.

If you look at the graph below, you'll see a person of 72in is 0.63 tall, or has a 0.63 correlation to the "tall" concept.  This person is more tall than not tall, but not 100% (1.00) tall.  These are our "shades of grey" in respect to tall.  The membership function can be defined differently or modified through what are called hedges (but that's a subject for a later article). At a certain point the fuzzy logic lines up with regular Boolean logic where the correlation to the "tall" concept is either 0.00 (absolutely false) or 1.00 (absolutely true).  If we need to convert a fuzzy number (from 0.00 to 1.00) to a Boolean value, we just say it is true if the fuzzy value is greater than 0.50.

Fuzzy Controller

Usually, a fuzzy controller has multiple "area" definitions as in the graph below.  Traditionally, they are labeled: L1, L0, M, H0, and H1 (L standing for Low and H standing for High).

If we apply this method to the concept of tall well be using the following ranges: "short", "somewhat short", "medium", "somewhat tall", and "tall".

And now if we look at 72in, we have a fuzzy relation to the somewhat tall and tall groups (with values 0.75 and 0.25 respectively).  Now we can code against these fuzzy definitions and can think in terms of ranges of heights instead of specifics.

Just as a side note, defined fuzzy regions don't necessarily have to be exactly the same.  They can also be triangles and the slopes on each section can vary instead of the symmetric plateau-shaped polygons I am using in the examples.  It all depends on how you want to look a the data.

Boolean Operators

We can use basic Boolean operators on fuzzy numbers.  The table below shows how they would be evaluated.

Operator Boolean  Logic Fuzzy Implementation
|  (OR) Sort OR Somewhat Short MAX(Short, Somewhat short)
& (And) Short AND Somewhat Short MIN(Short, Somewhat short)
!  (NOT) NOT Short 1.0 - Short

Code Sample

The sample application consists of a bunch of points on a windows app.  Each point has a momentum which is modified depending on how far from the center of the screen they are located.  The momentum of each point is also modified by the relations with all other points.  This gives us a chaotic system which is kind of fun to watch.

Here is our fuzzy number class which is pretty straightforward.  Notice the overloaded Boolean operators:

public class FuzzyNumber

{

    public FuzzyNumber(double value)

    {

        m_value = value;

    }

    private double m_value;

 

    public double Value

    {

        get { return m_value; }

        set { m_value = value; }

    }

 

    public bool IsTrue()

    {

        return m_value > 0.5;

    }

 

    public static FuzzyNumber operator | (FuzzyNumber A, FuzzyNumber B)

    {

        return new FuzzyNumber(Math.Max(A.m_value, B.m_value));

    }

 

    public static FuzzyNumber operator & (FuzzyNumber A, FuzzyNumber B)

    {

        return new FuzzyNumber(Math.Min(A.m_value, B.m_value));

    }

 

    public static FuzzyNumber operator !(FuzzyNumber A)

    {

        return new FuzzyNumber(1.0 - A.Value);

    }

}

 
I have defined the distances and positions with enums as follows:

 

public enum HorizontalPosition : int

{

    FarLeft, Left, Center, Right, FarRight

}

 

public enum VerticalPosition : int

{

    ExtremeLow, Low, Middle, High, ExtremeHigh

}

 

public enum Distance : int

{

    VeryClose, Close, SlightlyClose, Normal, SlightlyFar, Far, VeryFar

}

And so can define how the velocities of each point will changed based on these enums:

m_horizAdjustments = new Dictionary<HorizontalPosition, float>();

m_horizAdjustments.Add(HorizontalPosition.FarLeft, -.075F);

m_horizAdjustments.Add(HorizontalPosition.Left, -0.25F);

m_horizAdjustments.Add(HorizontalPosition.Center, 0.0F);

m_horizAdjustments.Add(HorizontalPosition.Right, 0.25F);

m_horizAdjustments.Add(HorizontalPosition.FarRight, 0.75F);

 

m_vertAdjustment = new Dictionary<VerticalPosition, float>();

m_vertAdjustment.Add(VerticalPosition.ExtremeHigh, 0.75F);

m_vertAdjustment.Add(VerticalPosition.High, 0.25F);

m_vertAdjustment.Add(VerticalPosition.Middle, 0.0F);

m_vertAdjustment.Add(VerticalPosition.Low, -0.25F);

m_vertAdjustment.Add(VerticalPosition.ExtremeLow, -0.75F);

 

m_distAdjustment = new Dictionary<Distance, float>();

m_distAdjustment.Add(Distance.VeryFar, 100.0F);

m_distAdjustment.Add(Distance.Far, 10.0F);

m_distAdjustment.Add(Distance.SlightlyFar, 1.0F);

m_distAdjustment.Add(Distance.Normal, 1.0F);

m_distAdjustment.Add(Distance.SlightlyClose, 1.0F);

m_distAdjustment.Add(Distance.Close, 1.0F);
m_distAdjustment.Add(Distance.VeryClose, 1.0F);

In Conclusion

I'm not going to go through all the code for implementing the project because it would take too long, however it is pretty self-documenting and if you have any questions, let me know and I'll do my best to answer them. 

Download the source and try ajusting the settings (above) to see how the points interact differently.  If nothing else, it is a fun toy to play with.

In upcoming articles, I'll dig into other ways to implement fuzzy computing, but for now I hope you found this article interesting.

Until next time,

-Happy coding

Login to add your contents and source code to this article
share this article :
post comment
 

thank you for interesting work. But it would be better to upload simpler code to get the idea how fuzzy works especially for newbies. As being newbie in fuzzy logic its hard to follow idea even I understand the c# codes. Anyway, thanks. it really gives some idea

Posted by ruslan abdulla Dec 25, 2011

Is there any follow up articles or coding references for this interesting topic?

Posted by robert Dec 24, 2010

How you build membership functions (high, low)&

Posted by olga belova Nov 21, 2010

Thanx for this article.
It is so good.
but i have a question,How can i interface between c# and Fuzzy Logic, and please i want to learn more about fuzzy logic because i'll use it in my project (Intelligent Classroom).
If you want to contact with me,this my e-mail(aleemo_aleemo2007@yahoo.com).
With my best wishes

Posted by Ahmed Elsharkawy Oct 29, 2010

I am a beginner of Indonesian students, what methods you use from the article titled "Introduction to Fuzzy Computing: Part I"?  email me: arieffianto_nurhuda@yahoo.co.id

send me too fuzzy logic visual basic version, please... because i am a beginner

Posted by nurhuda arieffianto Sep 02, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor