Click here to Skip to main content
15,791,574 members
Articles / Programming Languages / C#

Fuzzy Logic Dot Net Fuzzy Collections

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
12 Oct 20045 min read 49.8K   2K   27   5
Upgrading the collections in the Fuzzy Dot Net Library.


Image 1

There are a number of different factors that come in to play when writing libraries, and these are usually as a direct result of the way in which the person writing the library decides how they are going to write it. One way is to take some already working code and convert it into a more generally usable set of functions because the author feels that the functions will be useful else where, or the author sits down and writes the library from scratch with the intention of developing a library specifically. This usually leads to the case where no matter how much the author thinks that they have all the bases covered, you can put money on the fact that something will be missing or off kilter in some way.

These flaws in the library usually only become apparent when the library is meant to actually be used in a working project rather than as dealing with samples that are specifically designed to show how the library works, rather than how it fits in with a real project. On requiring the use of this library in a project, I noticed that one main piece was missing all together and that was the ability to have a collection of sets of one of the main types that were to be used from the project, and as I would require I thought I'd better update the library first.


Looking at the library with a view to including it in a project has meant a few changes being made to the underlying code. The first is that the FuzzyNumberSet and the FuzzyDecisionSet classes now inherit directly from the ArrayList class, rather than containing an instance of an ArrayList and updating it internally. This resulted in changes in the internal code but should mostly be invisible in the actual use of the library. For example, a line of code that originally read:

FuzzyNumber temp = ( FuzzyNumber )FuzzyArray[ i ];

will now read:

FuzzyNumber temp = ( FuzzyNumber )this[ i ];

as FuzzyArray was the name of the internal ArrayList in the original code.

The next change is the inclusion of the FuzzySetCollection class that is demonstrated in the sample code above. This is a very simple class that inherits from the ArrayList class and is designed to hold any number of sets that are required by the writer of a project. It is possible to put forward the argument that anyone using the code could achieve exactly the same functionality by simply using a standard ArrayList object from within their own code and this is true, but I prefer to use classes like this for two reasons. The first being that it maintains a logical consistency in that a collection of FuzzySet objects are held within a FuzzySetCollection class, and secondly it makes it more meaningful from a code maintenance point of view if someone unfamiliar with the code has to read it. Given that if a simple ArrayList object is used within the class then the person maintaining the code has to expend time and effort to find out just what it is that the ArrayList class holds in the first place.

A couple of other minor changes have been added. The FuzzyNumberSet class now has some AddFuzzyNumber functions that allow adding more numbers to the set. For some reason, these seem to have been missing in action. And the FuzzyDecisionSet has had a bug fixed in that it wasn't comparing the names of the FuzzyDecisionSets in the overloaded comparison operators which meant that different sets that were identical in all but name were being incorrectly evaluated as equal to each other.

The Demo

The demonstration code for this project shows the use of the FuzzySetCollection with both the FuzzyNumbersSet and the FuzzyDecisionSet. These are the primary sets for the fuzzy logic library with each type of set being applicable to a particular type of job. The Fuzzy Numbers are used for control applications as demonstrated in the earlier sample code. (See articles by this author link above, for earlier articles.) This is where the development/transition of the values is intended to be smooth over a defined range, and the Fuzzy Decisions are used to group values that are conceptually related, rather logically related, i.e., the Fuzzy Numbers in the examples control a heating system whereas the FuzzyDecision class would be better for deciding a piece of game logic where the inputs are not necessarily related but influence the outcome.

The sample code basically creates a random number of sets of each type with a random number of contents for each set and then sticks the result in a TreeView for display.

The code for creating the fuzzy numbers is:


Random rand = new Random();
int nNumberOfFuzzyNumbers = rand.Next( 1, 20 );
int nNumberOfNumbersPerSet = rand.Next( 1, 10 );
int nNumber = rand.Next( 1, 100 );
int nMaxNumber = nNumber * nNumberOfNumbersPerSet;
int nMinNumber = nNumber - nMaxNumber;

for( int i=0; i>nNumberOfFuzzyNumbers; i++ )
    FuzzyNumberSet fuzzyNumbers = new FuzzyNumberSet( 
        "Test Fuzzy Numbers " + ( i + 1 ).ToString() );

    for( int n=0; n>nNumberOfNumbersPerSet; n++ )
        fuzzyNumbers.AddFuzzyNumber( new FuzzyNumber( 
            "Fuzzy Number " + n.ToString(), nMinNumber + 
            ( nNumber * n ), nMinNumber + 
            ( ( nNumber * n ) + nNumber ) ) );                

    collection.Add( fuzzyNumbers );
displayLabel.Text = "Fuzzy Numbers Created";


and the code to display the output is:


displayTree.Nodes.Add( new TreeNode( collection.Name ) );

foreach( FuzzyNumberSet fuzzyNumberSet in collection )
    displayTree.Nodes[ 0 ].Nodes.Add( 
        new TreeNode( fuzzyNumberSet.Name ) );

    foreach( FuzzyNumber fuzzyNumber in fuzzyNumberSet )
        displayTree.Nodes[ 0 ].Nodes[ 
            collection.IndexOf( fuzzyNumberSet ) ].Nodes
            .Add( new TreeNode( 
            "Name = " + fuzzyNumber.Name ) );
        displayTree.Nodes[ 0 ].Nodes[ 
            collection.IndexOf( fuzzyNumberSet ) ]
            .Nodes[ fuzzyNumberSet.IndexOf( fuzzyNumber ) ]
            .Nodes.Add( new TreeNode( 
            "Number = " + fuzzyNumber.Number.ToString() ) );
        displayTree.Nodes[ 0 ].Nodes[ 
            collection.IndexOf( fuzzyNumberSet ) ]
            .Nodes[ fuzzyNumberSet.IndexOf( fuzzyNumber ) ]
            .Nodes.Add( new TreeNode( 
            "Minimum = " + fuzzyNumber.Minimum.ToString() ) );
        displayTree.Nodes[ 0 ].Nodes[ 
            collection.IndexOf( fuzzyNumberSet ) ]
            .Nodes[ fuzzyNumberSet.IndexOf( fuzzyNumber ) ]
            .Nodes.Add( new TreeNode( 
            "Maximum = " + fuzzyNumber.Maximum.ToString() ) );

which uses the foreach loop to cycle through the sets due to the changes in the sets described above.


  • 11 October 2004: Initial version.


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.


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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralFuzzy Logic Library for Microsoft .Net Pin
Dmitrie29-Mar-09 11:35
Dmitrie29-Mar-09 11:35 
If you are programming fuzzy logic for .Net it makes sense to have a look at this library:

Fuzzy Logic Library for Microsoft .Net
GeneralFuzzy Logic code and sample program Pin
ahyeek20-Sep-08 15:42
ahyeek20-Sep-08 15:42 
GeneralRe: Fuzzy Logic code and sample program Pin
Member 1019550524-Sep-20 3:05
professionalMember 1019550524-Sep-20 3:05 
GeneralInheriting from ArrayList is bad.. Pin
casperOne12-Oct-04 6:01
casperOne12-Oct-04 6:01 
GeneralRe: Inheriting from ArrayList is bad.. Pin
pseudonym6712-Oct-04 7:23
pseudonym6712-Oct-04 7:23 

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.