# Fuzzy Logic Dot Net Sample Part Two

By , 13 Dec 2005
Votes of 3 or less require a comment

## Introduction

This is the third installment of the Fuzzy Dot Net series of articles and looks at the final version of the Fuzzy Dot Net Sample Application. You will remember from the previous article that the Fuzzy Dot Net Sample Application was designed as a simplification of a central heating model. This has been done in order to show how the techniques of Fuzzy Logic are used to come to decisions when the value of the numbers given are uncertain. This is done through the use of Fuzzy Numbers that represent a range of numbers and usually have an identifier or name to distinguish them from each other. These Fuzzy Numbers are then stored in a Fuzzy Set that I said previously allows us to process the information the set contains using an almost English way of thinking about it called rules. In this version of the Fuzzy Logic Sample Application we will take a more detailed look at how these sets and rules are used in practice.

## The Fuzzy Dot Net Sample Application Pt Two

From a visual perspective the most obvious change in the program is that the Heater Settings Tab has been removed. This is because the code now uses Fuzzy Logic to work out the incremental or decremented value that will be applied to the heater. The other change to note is that the Ideal Temperature on the Start Settings Tab is no longer read only this is because the changes to the code now allow the code to work out whether the heater value should be decremented or incremented. The code at present achieves an accuracy of give or take three units. This could be refined even more but I thought it would be slightly more realistic to leave it off by a small amount, as something as finicky as temperature is hardly ever going to be one hundred percent at the required temperature unless you have complete control of the environment which given this example of a room is not the case.

### Running The Sample Application

As with the previous version of the application it is possible to set the amount of heat lost through the separate Tabs. This time though you can change the ideal temperature that you want the room to be kept at although as stated above this will always contain a certain deliberate margin of error.

## Fuzzy Logic In The Sample Application

The first thing that has changed in the application is the addition of two new Fuzzy Sets. these are,

```    heaterControl.Name = "Heater Control";
FuzzyNumber temp36 = new FuzzyNumber( "PositionOne", 0, 2 );
FuzzyNumber temp37 = new FuzzyNumber( "PositionTwo", 1, 4 );
FuzzyNumber temp38 = new FuzzyNumber( "PositionThree", 3, 6 );
FuzzyNumber temp39 = new FuzzyNumber( "PositionFour", 5, 8 );
FuzzyNumber temp40 = new FuzzyNumber( "PositionFive", 7, 10 );
FuzzyNumber temp41 = new FuzzyNumber( "PositionSix", 9, 12 );
FuzzyNumber temp42 = new FuzzyNumber( "PositionSeven", 11, 14 );
heaterControl[ 0 ] = temp36;
heaterControl[ 1 ] = temp37;
heaterControl[ 2 ] = temp38;
heaterControl[ 3 ] = temp39;
heaterControl[ 4 ] = temp40;
heaterControl[ 5 ] = temp41;
heaterControl[ 6 ] = temp42;

heaterDifference.Name = "Heater Difference";
FuzzyNumber temp43 = new FuzzyNumber( "Light", 0, 8 );
FuzzyNumber temp44 = new FuzzyNumber( "MediumLight", 5, 13 );
FuzzyNumber temp45 = new FuzzyNumber( "Medium", 10, 18 );
FuzzyNumber temp46 = new FuzzyNumber( "MediumLarge", 15, 23 );
FuzzyNumber temp47 = new FuzzyNumber( "Large", 20, 28 );
FuzzyNumber temp48 = new FuzzyNumber( "LargeHeavy", 25, 33 );
FuzzyNumber temp49 = new FuzzyNumber( "Heavy", 28, 43 );
heaterDifference[ 0 ] = temp43;
heaterDifference[ 1 ] = temp44;
heaterDifference[ 2 ] = temp45;
heaterDifference[ 3 ] = temp46;
heaterDifference[ 4 ] = temp47;
heaterDifference[ 5 ] = temp48;
heaterDifference[ 6 ] = temp49;
```

The ideas behind these sets are that the `heaterControl` set is like the control knob on the heater. The naming of the members of the set reflects this in that each member of the set would represent one of the notches on the dial. The dial can obviously be turned one way to increase the output of the heater and the other way to blow cooler air into the environment/room.

The heater difference set is an internal set that is not meant to represent anything in the real world but is designed to help figure out the final result that is applied to the heater control. It is a bridge if you like between the values returned by the timers that fire setting the value for the amount of heat loss and the actual amount of change this reflects on the controller. This will probably be explained a little better by the code.

```    if( doorSet.IsTerm( "Warm" ) == true )
{
doorSet.FuzzySetValue = 0.0;
}

if( doorSet.IsTerm( "Cool" ) == true )
{
heaterDifference.FuzzySetValue +=
heaterDifference.ValueFromTerm( "Light" );
doorSet.FuzzySetValue = 0.0;
}

if( doorSet.IsTerm( "Cooler" ) == true )
{
heaterDifference.FuzzySetValue +=
heaterDifference.ValueFromTerm( "MediumLight" );
doorSet.FuzzySetValue = 0.0;
}

```

For each term or identifier that is detected the heater difference is updated by a certain amount. But and there's always a but, things are as usual more complicated than that. You may have noticed from the definition of the Heater Difference Set that the values overlap. This brings us to the classic Fuzzy Logic question of what happens when a value that is Cool is no longer quite so Cool but isn't yet quite warm enough to be Cooler. ie when it is possible for it to be both at the same timer which in this case would be a value of 7. Now at this point with a value of 7 the value for the `doorSet` would be both Cool and Cooler. There are a few ways that this situation can be dealt with, the first is the way that I have chosen and that is to increment heater difference variable once for each range that the value falls into. A more accurate way of doing it would be to have multiple if else statements that test to see if the value, in this case `doorSet` is valid within two ranges. e.g.

```    if( doorSet.IsTerm( "Cool" ) == true
&& doorSet.IsTerm( "Cooler" ) == true )
```

A third way of doing it would be to assign the values depending on the membership value in each range so a high membership value would assign most or all of the value assigned it the variable is in the Cool range but only a small amount of the value of the Cooler range.

The next section of the code is where we convert the values in the heater difference set to the correct control value for the heater control set. This is done through simple if statements that demonstrate a simple version of the idea of Fuzzy Logic rules.

```    /// if difference is Light then Control is position One
if( heaterDifference.IsTerm( "Light" ) == true )
{
heaterControl.SetToTerm( "PositionOne" );
}

/// if difference is medium light then control is Position Two
else if( heaterDifference.IsTerm( "MediumLight" ) == true )
{
heaterControl.SetToTerm( "PositionTwo" );
}
```

Note that in the tests this time an else if structure is used as the heater control is not designed to be cumulative.

Next we come to the final section and this as I stated at the end of the last article is the bit where we need to convert the Fuzzy numbers that we have been using into a variable that we can use to adjust the heat for the heater set. This is the only time in this code that we use a normal or to use a fuzzy logic term crisp variable,

```    if( ( double )this.idealTemperature.Value >=  heaterSet.FuzzySetValue )
{
heaterSet.FuzzySetValue += heaterControl.ValueFromTerm(
heaterControl.GetTerm() );
}
else
heaterSet.FuzzySetValue -= heaterControl.ValueFromTerm(
heaterControl.GetTerm() );
```

The idealTempaterature variable is used here to simply decide if the heater Control is top be incremented or decremented.

## Problems

There is one major problem with the code to download that is bugging the crap out of me but there seems to be nothing that I can do about it. Whenever the project is opened in Developer Studio 2003 the graphic at the top of the program gets lost by the GUI and if you try to add it the environment complains about an incorrect input value. This is pure garbage and highly annoying it means that whenever you open the project you have to remove the `FuzzyGraphic` from the user controls and then add the `FuzzyGraphic` to the user controls and reposition it on the form. You then have to open the form code and remove the added variable ( `FuzzyGraphic1` ) and do a find and replace for `FuzzyGraphic1` replacing it with `FuzzyGraphic2`. I hope this is just a problem on my system but so far I have no reason to believe it will be.

## Finally

That's it for this section which should serve as an introduction to Fuzzy Logic and how to use it. As for what comes next I've got to do some research and write some experimental code and see where it goes.

## History

• 08 August 2003 :- Initial release.

• 20 August 2003 :- Updated Link to next article

• 15 September 2004 :- Rebuilt demo code

## Note

The last article in the series contains the latest code for the library. No attempt at backward compatibility will be attempted and I will change the library as I see fit.

## References

• Tom Archer ( 2001 ) Inside C#, Microsoft Press
• Jeffery Richter ( 2002 ) Applied Microsoft .NET Framework Programming, Microsoft Press
• Charles Peltzold ( 2002 ) Programming Microsoft Windows With C#, Microsoft Press
• Robinson et al ( 2001 ) Professional C#, Wrox
• Bart Kosko ( 1994 ) Fuzzy Thinking, Flamingo
• Buckley & Eslami ( 2002 ) An Introduction To Fuzzy Logic And Fuzzy Sets, Physica-Verlag
• Earl Cox ( 1999 ) The Fuzzy Systems Handbook, AP Professional

A list of licenses authors might use can be found here

United Kingdom
No Biography provided

 First PrevNext
 My vote of 5 Kanasz Robert 6-Nov-12 0:00
 Fuzzy Logic Library for Microsoft .Net Dmitrie 29-Mar-09 10:32
 Fuzzy Logic code and sample program ahyeek 20-Sep-08 14:32
 cannot find 'SetToTerm', 'ValueFromTerm', 'GetTerm' CandyMe 11-Dec-05 22:53
 Re: cannot find 'SetToTerm', 'ValueFromTerm', 'GetTerm' pseudonym67 12-Dec-05 22:09
 I have yet again rebuilt and resent the code to the site. Sorry but I've no idea why this keeps happening nor why I have no access to edit my own articles like I'm supposed to. They should update it in the next few days.   pseudonym67   My Articles[^]   "So keep that smile on your face. Have a drink to help you sleep at night. They got what they desired. We're passive in their brave new world."   New Model Army   -- modified at 4:10 Tuesday 13th December, 2005
 example of c code Mafnis 5-Sep-05 2:23
 Timestamps westfale 14-Sep-04 2:17
 No Code Sample... Nic Oughton 8-Aug-03 1:13
 Last Visit: 31-Dec-99 18:00     Last Update: 7-Dec-13 21:54 Refresh 12 Next »