Click here to Skip to main content
13,195,163 members (69,999 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 2 Feb 2008

Introduction to C# and Fuzzy Logic

, 2 Feb 2008
Rate this:
Please Sign up or sign in to vote.
This article gives a brief introduction to the basic technology of rule based fuzzy logic systems using a container crane control example


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:


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.

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:

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.

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.

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)


About the Author

Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralLOL Pin
Carter.DF8-Feb-11 11:01
memberCarter.DF8-Feb-11 11:01 
GeneralMy vote of 5 Pin
Akkibawankar29-Sep-10 17:08
memberAkkibawankar29-Sep-10 17:08 
GeneralFuzzy Logic Library Pin
Dmitrie7-Apr-09 0:53
memberDmitrie7-Apr-09 0:53 
GeneralNo Rules for the code Pin
Haipro18-Mar-09 19:04
memberHaipro18-Mar-09 19:04 
Hi kelary,
This is the great code and it runs smoothly but I have some doubts on rules.
I did not see any rule in your code,but ony defuzzification and very short code without explanation.
I went through some papers on some universities, they also used "centroid" like you but the defuzzification is much more complicated than you.

It must go for these steps:
0> Error input signal
1> Calculate from each input
2> Calculate for its output and then
3> Make aggregation graph
4> Calculate Center-of-Gravity value to output.

Could you explain more detail about defuzzification and your rule in your code?
Many thanks,
Hai Mai
GeneralFuzzy Logic code and sample program Pin
ahyeek20-Sep-08 14:46
memberahyeek20-Sep-08 14:46 
GeneralProblem Pin
Nasif M.6-Jun-08 8:56
memberNasif M.6-Jun-08 8:56 
GeneralRe: Problem Pin
kelary15-Jun-08 16:26
memberkelary15-Jun-08 16:26 
QuestionWhere's the code? Pin
Stephan Poirier2-Feb-08 10:05
memberStephan 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171019.1 | Last Updated 2 Feb 2008
Article Copyright 2008 by kelary
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid