12,623,724 members (34,023 online)
Add your own
alternative version

106.3K views
4.1K downloads
39 bookmarked
Posted

# Introduction to C# and Fuzzy Logic

, 2 Feb 2008 CPOL
 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

## Introduction

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:

## Fuzzification

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

## Fuzzy-Inference

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

## Defuzzification

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

if((angle<-60)&&(angle>=-90))
neg_small=0;
else if((angle<-10)&&(angle>=-60))
neg_small=(0.02*angle+1.2);
else if((angle<0)&&(angle>=-10))
neg_small=(-0.1*angle);
else if((angle<=90)&&(angle>=0))
neg_small=0;
if((angle<-60)&&(angle>=-90))
neg_big=1;
else if((angle>=-60)&&(angle<-10))
neg_big=(-0.02*angle-0.2);
else if((angle>=-10)&&(angle<=90))
neg_big=0;
if((distance<5)&&(distance>=-10))
medium=0;
else if((distance<10)&&(distance>=5))
medium=(0.2*distance-1);
else if((distance<22)&&(distance>=10))
medium=((-1/12)*distance+(11/6));
else if((distance<=30)&&(distance>=22))
medium=0;
if((distance<10)&&(distance>=-10))
far=0;
else if((distance<22)&&(distance>=10))
far=((1/12)*distance-(5/6));
else if((distance<=30)&&(distance>=22))
far=1;
//... other if_ then_else clauses for other terms of  distance and angle to be
// continued .

//Defuzzification of variable, power
return
System.Math.Round(System.Math.Max(System.Math.Min(pos_small,zerodis),
System.Math.Min(pos_small,close))*neg_medium_pow+System.Math.Max(
System.Math.Min(zero,zerodis),System.Math.Min(zero,close))*zero_pow+System.Math.Max(
System.Math.Max(System.Math.Min(neg_small,close),System.Math.Min(neg_big,medium)),
System.Math.Min(zero,far))*pos_medium_pow+System.Math.Max(System.Math.Min(neg_small,
medium),System.Math.Min(neg_small,far))*pos_high_pow,2);
}

## 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.

## History

• 2nd February, 2008: Initial post

## License

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

## About the Author

 Other Iran (Islamic Republic of)
No Biography provided

## Comments and Discussions

 View All Threads First Prev Next
 Where's the code? Stephan Poirier2-Feb-08 11:05 Stephan Poirier 2-Feb-08 11:05
 Last Visit: 31-Dec-99 19:00     Last Update: 4-Dec-16 6:28 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

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