15,671,134 members
Articles / Desktop Programming / Win32
Article
Posted 2 Feb 2008

143.6K views
40 bookmarked

# Introduction to C# and Fuzzy Logic

Rate me:
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

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

C#
```//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

Written By
Other
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

 First Prev Next
 Membership functions? Member 101955057-Dec-20 2:59 Member 10195505 7-Dec-20 2:59
 LOL Carter.DF8-Feb-11 11:01 Carter.DF 8-Feb-11 11:01
 My vote of 5 Akkibawankar29-Sep-10 17:08 Akkibawankar 29-Sep-10 17:08
 Fuzzy Logic Library Dmitrie7-Apr-09 0:53 Dmitrie 7-Apr-09 0:53
 No Rules for the code Haipro18-Mar-09 19:04 Haipro 18-Mar-09 19:04
 Fuzzy Logic code and sample program ahyeek20-Sep-08 14:46 ahyeek 20-Sep-08 14:46
 Re: Fuzzy Logic code and sample program QMasters10-May-22 12:19 QMasters 10-May-22 12:19
 Problem Nasif M.6-Jun-08 8:56 Nasif M. 6-Jun-08 8:56
 Re: Problem kelary15-Jun-08 16:26 kelary 15-Jun-08 16:26
 Where's the code? Stephan Poirier2-Feb-08 10:05 Stephan Poirier 2-Feb-08 10:05
 Last Visit: 31-Dec-99 18:00     Last Update: 8-Jun-23 9:57 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.