Click here to Skip to main content
15,662,893 members
Articles / Desktop Programming / Win32
Posted 2 Feb 2008

Tagged as


40 bookmarked

Introduction to C# and Fuzzy Logic

Rate me:
Please Sign up or sign in to vote.
3.00/5 (18 votes)
2 Feb 2008CPOL3 min read
This article gives a brief introduction to the basic technology of rule based fuzzy logic systems using a container crane control example
Image 1


The container (yellow) is already picked up from the ship (red). It must be positioned over the truck (green). The values of Angle and Distance are computed by the process simulation, while Power is the control variable either set manually or by the fuzzy logic controller. The fuzzy logic controller using the human operator's experience. A human operator is capable of controlling a crane without differential equations.

Implementing a Linguistic

Control strategy sensors for the crane head position Distance and the angle of the container sway Angle are employed to automate the control of this crane. Using these inputs to describe the current condition of the crane, for example :

IF Distance = medium AND Angle = neg_small THEN Power = pos_high 

The figure below shows the complete structure of a fuzzy logic controller:

Image 2


Linguistic variables have to be defined for all variables used in the if-then rules. For the crane controller, the terms are:

  1. Distance: {neg_close, zero, close, medium, far}
  2. Angle: {neg_big, neg_small, zero, pos_small, pos_big}
  3. Power: {neg_high, neg_medium, zero, pos_medium, pos_high}.

For every linguistic variable, each term is defined by its membership function.

Image 3

Image 4

Consider a possible situation for the crane where the Distance of the crane head to the target position is 12 yards and the Angle of the container is -30 degrees.

A Distance of 12 yards is a member of the fuzzy sets for the terms :

  • medium to the degree of 0.83
  • far to the degree of 0.17
  • and other term's degrees are 0.00

An Angle of -30 degrees is member of the fuzzy sets for the terms :

  • neg_big to the degree of 0.4
  • neg_small to the degree of 0.6
  • and other term's degrees are 0.00


Using If-Then rules now that all input variables have been converted to linguistic variable values, the fuzzy inference step can identify the rules that apply to the current situation and compute the value of the output linguistic variable. Consider the above example:

The IF part combines the two conditions Distance = medium and Angle= neg_small. The IF part defines whether the rule is valid in the current situation or not. In conventional logic, the combination of the two conditions can be computed by the Boolean AND as shown in the following figure:

Image 5

In the case of fuzzy logic, the Boolean AND cannot be used as it cannot cope with conditions that are more-or-less true. Hence, new operators had to be defined for fuzzy logic to represent logical connectives such as AND, OR, and NOT.

Image 6

The IF part of the above example can be computed as shown:

min{ 0,83; 0.6} = 0.6 


At the end of the fuzzy inference, the result for Power is given as the value of a linguistic variable. In order to use it to set the motor power, it has to be translated into a real value. The relation between linguistic values and corresponding real values is always given by the membership function definitions. The figure below plots the membership functions for the linguistic variable Power.

Image 7

I use a method of defuzzification is called Center-of-Maximum and is identical to the Center-of-Gravity method using singleton membership functions. These defuzzification methods are used in most fuzzy logic implementations.

Code Sample

The application consists of many functions and objects, but my favorite subject in this application is that of Fuzzy Logic. I wrote the fuzzy function by the above membership functions and concepts of fuzzy logic. Here is the fuzzy function :

//Fuzzy function to produce optimized power
public double fuzzy()
//Fuzzification of variables, distance and angle

else if((angle<-10)&&(angle>=-60))
else if((angle<0)&&(angle>=-10))
else if((angle<=90)&&(angle>=0))
else if((angle>=-60)&&(angle<-10))
else if((angle>=-10)&&(angle<=90))
else if((distance<10)&&(distance>=5))
else if((distance<22)&&(distance>=10))
else if((distance<=30)&&(distance>=22))
else if((distance<22)&&(distance>=10))
else if((distance<=30)&&(distance>=22))
//... other if_ then_else clauses for other terms of  distance and angle to be
// continued .

//Defuzzification of variable, power

In Conclusion

Please, notice that my simulation program isn't a scientific simulation. My application is not pretty because I'm a beginner, but I think it is almost working properly. What do you think?

Have a good time.


  • 2nd February, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionMembership functions? Pin
Member 101955057-Dec-20 2:59
professionalMember 101955057-Dec-20 2:59 
GeneralLOL Pin
Carter.DF8-Feb-11 11:01
Carter.DF8-Feb-11 11:01 
GeneralMy vote of 5 Pin
Akkibawankar29-Sep-10 17:08
Akkibawankar29-Sep-10 17:08 
GeneralFuzzy Logic Library Pin
Dmitrie7-Apr-09 0:53
Dmitrie7-Apr-09 0:53 
This library makes fuzzy logic programming for .Net easier - Fuzzy Logic Library for Microsoft .Net
GeneralNo Rules for the code Pin
Haipro18-Mar-09 19:04
Haipro18-Mar-09 19:04 
GeneralFuzzy Logic code and sample program Pin
ahyeek20-Sep-08 14:46
ahyeek20-Sep-08 14:46 
GeneralRe: Fuzzy Logic code and sample program Pin
QMasters10-May-22 12:19
professionalQMasters10-May-22 12:19 
GeneralProblem Pin
Nasif M.6-Jun-08 8:56
Nasif M.6-Jun-08 8:56 
GeneralRe: Problem Pin
kelary15-Jun-08 16:26
kelary15-Jun-08 16:26 
QuestionWhere's the code? Pin
Stephan Poirier2-Feb-08 10:05
Stephan Poirier2-Feb-08 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.