Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Fuzzy Logic Vs Adaline Neural Network

0.00/5 (No votes)
29 Oct 2003 1  
An experiment to see if it possible to duplicate the behavior of the Adaline Network using Fuzzy Logic.

Introduction

Earlier this year, I wrote a series of articles about Neural Networks, complete with class library and to be honest they were hard work. They take time to develop, time to train and aren't especially flexible even when they do work correctly at the task assigned to them. Lately I've been playing around with Fuzzy Logic amongst other things and a constant nagging thought has been lurking at the back of my mind as to if Fuzzy Logic could do the stuff that a Neural Network could do and would it be easier to develop, faster, more flexible? I didn't know the answer to these questions, so I just sort of ignored it for a while.

Lately I've been working a lot on the PathFinder project which is just messing about with a few ideas and building a framework to begin implementing some of the things that I have been reading about. The PathFinder project is available on CodeProject here. The ideas being put into PathFinder stem from a branch of Complexity Theory called Emergence which many people may call the same thing really, but this is not about arguments over definitions. One of the central ideas behind Emergence and Complexity Theory is the way that simple rules lead to complex behavior patterns. But what effect does this have on programming? If any? And how would you go about programming it in the first place? This last question has probably been the driving question behind all the stuff that I've done on CodeProject and probably will continue to be in the future. I mean it's all well and good being able to put cool sounding titles to articles, an impressive list of references at the end of them, but I'm a programmer and this is a programming site and to be honest if I can't implement it and demonstrate it to be a workable idea, then it isn't worth that much to me.

So I found myself mulling over the simple rules idea and the idea of code containing the rules of its behavior at a low level rather than being imposed by a framework of logic over the top and I thought that I could make that work if I just used simple interface classes at a low level. So I wrote it and in writing it realized that while it works it may not exactly fit people's definition of what Fuzzy Logic is, so in being nice I could say that technically I'm bending the rules a little bit. In not being nice, I could say that I'm riding rough shod over them and ignoring them completely, but being more implementation minded, I think I've got an idea that is worth running with for a while to see where it goes.

The Adaline Neural Network

The complete article on the Adaline Network is available here. But I'll give a brief guide to what it does here, which will provide the information needed to understand what the rest of the article is talking about.

The Adaline Neural Network is a two input node network that has a single output node. Basically given a sample of the training file that looks like this:

0 0.0527202529146896 0.0353495702312093 -1
1 0.616699623696832 0.198049677162454 -1

These are the first two items in the training file. The first number is just the number of the item and can be ignored. There are then two numbers followed by either a -1 or a 1. What happens with the network is that the two numbers are placed in the nodes and the run function is called. The output of the run function is then placed in another node called the output node. This will be a value of -1 or 1. Now the output node value is compared to the number at the end of the training line. This number represents the desired output, with the meaning of the output dependant on how we set up the training file. For instance, the two values above are both -1 which we take to mean that the second number is less than the first number and if the first number is less than the second then the output value in the training file would be a 1.

At this point, we are going to ignore the specifics of how the network works this out and concentrate on what it does. Basically we give the Adaline Network a rule. This rule states that if the first number is greater than the second, then we want a -1 to be the output and if the first number is not greater than the second then we want a 1 to be the output. The fact that the network works out the rule mathematically is neither here nor there at the moment. The point is that if the person setting up the training file does not fully understand the rule, then they can completely mess up the file and ruin any chance of the network working.

For a Neural Network this time taken for the network to work out the rule that is being input is called the training phase and if we want to change the rule, then we have to retrain the network and make sure that the training file is correct manually.

The Fuzzy Way To Do It

We start simple with the idea that O.K. we have two numbers and at some point we want those numbers to be aware of the rules that are to be applied to them. How do we apply rules to the numbers so that in theory we could apply different rules to different numbers and even different class objects. Well we start with an interface,

public interface IFuzzyBasicComparisonRules
{
   bool IsGreaterThan
   {
      get;
      set;
   }

   bool IsLessThan
   {
     get;
     set;
   }

   bool IsEqual
   {
     get;
     set;
   }
}

The IFuzzyBasicComparisonRules interface works on the idea that it can be inherited by any object that we wish to inherit to. The reason to use an interface is because at this moment in time we don't know what class the interface is going to be used in nor if the rules for the comparisons is even going to be used between objects that are of the same type. I think I should make it specifically clear at this point that what the interface implements is not the rule itself, because at this very low level of dealing with a simple number, it would involve too much twisting logic into the class to make it work. The idea being that at some point a class would use the FuzzyDouble class and deal with the Fuzzy Double depending on the rules set within the object. So at this point, we are merely setting the rules that the type is supposed to adhere to.

For this project, the interface is implemented in the FuzzyDouble class which is not implemented in the same way as the FuzzyNumber class in that it doesn't have the standard Fuzzy Logic code for the number to be unknown and have a membership value. It is treated as a wrapper around the double type and can do anything a double can do with the added implementation of the IFuzzyBasicComaparisonRules interface.

The FuzzyDouble class is implemented as:

public class FuzzyDouble : IFuzzyBasicComparisonRules
{
    private bool bGreaterThan;
    private bool bLessThan;
    private bool bEqual;

    private double dNumber;

which has private members that are used for the implementation of the interface and a private double which contains the value that the class will hold.

The interface is implemented as:

#region IFuzzyBasicComparisonRules Members

public bool IsGreaterThan
{
    get
    {
        // TODO: Add FuzzyBasicComaparisonRules.IsGreaterThan

        // getter implementation

        return bGreaterThan;
    }
    set
    {
        // TODO: Add FuzzyBasicComparisonRules.IsGreaterThan

        // setter implementation

        bGreaterThan = value;
    }
}

public bool IsLessThan
{
    get
    {
        // TODO: Add FuzzyBasicComparisonRules.IsLessThan

        // getter implementation

        return bLessThan;
    }
    set
    {
        // TODO: Add FuzzyBasicComparisonRules.IsLessThan

        // setter implementation

        bLessThan = value;
    }
}

public bool IsEqual
{
    get
    {
        // TODO: Add FuzzyBasicComparisonRules.IsEqual

        // getter implementation

        return bEqual;
    }
    set
    {
        // TODO: Add FuzzyBasicComparisonRules.IsEqual

        // setter implementation

        bEqual = value;
    }
}

#endregion

which returns the rule for the implementation which is true or false depending on which rule is selected. If you look at the image above, it shows the rules page for the test application and that it has two rules: one which states that the number on the left is greater than the number on the right and one that states that the number on the right is greater than the number on the left. Depending on which option is checked, the results of the test change.

The controlling code for the form is:

for( int i=0; i<patterns.Count; i++ )
{
   if( this.leftCheckBox.Checked == true )
   {
      ( ( FuzzyDouble )( 
          ( FuzzyPattern )patterns[ i ] ).InSet[ 0 ] ).IsGreaterThan = true;
      ( ( FuzzyDouble )( 
          ( FuzzyPattern )patterns[ i ] ).InSet[ 1 ] ).IsLessThan = true;
   }
   else
   {
      ( ( FuzzyDouble )( 
          ( FuzzyPattern )patterns[ i ] ).InSet[ 0 ] ).IsLessThan = true;
      ( ( FuzzyDouble )( 
          ( FuzzyPattern )patterns[ i ] ).InSet[ 1 ] ).IsGreaterThan = true;
   }
}

richTextBox.AppendText( "Applying the rules\n" );

bool bLeftIsGreater = ( ( FuzzyDouble )( 
   ( FuzzyPattern )patterns[ 0 ] ).InSet[ 0 ] ).IsGreaterThan;
bool bRightIsGreater = ( ( FuzzyDouble )( 
   ( FuzzyPattern )patterns[ 0 ] ).InSet[ 1 ] ).IsGreaterThan;

FuzzyDouble leftValue = null;
FuzzyDouble rightValue = null;

for( int i=0; i<patterns.Count; i++ )
{
   leftValue = ( ( FuzzyDoublePattern )patterns[ i ] ).InputValue( 0 );
   rightValue = ( ( FuzzyDoublePattern )patterns[ i ] ).InputValue( 1 );

   if( bLeftIsGreater == true )
   {
      if( leftValue > rightValue )
      {
         richTextBox.AppendText( "The left value is " + 
           "greater than the right value, left :- " 
           + leftValue.Number.ToString() + ", right :- " 
           + rightValue.Number.ToString() + ", Output value = 1\n" );
      }
      else
      {
         richTextBox.AppendText( "The left value is not " +
           "greater than the right value, left :- " 
           + leftValue.Number.ToString() + ", right :- " 
           + rightValue.Number.ToString() + ", 
           Output value = -1\n" );
      }
   }
   else
   {
      if( rightValue > leftValue )
      {
          richTextBox.AppendText( "The right value is " + 
             "greater than the left value, right :- " 
             + rightValue.Number.ToString() + ", left :- " 
             + leftValue.Number.ToString() + ", 
             Output value = 1\n" );
      }
      else
      {
          richTextBox.AppendText( "The right value is " + 
            "not greater than the left value, right :- " 
            + rightValue.Number.ToString() + ", left :- " 
            + leftValue.Number.ToString() + ", 
            Output value = -1\n" );
      }
   }
}

This code is from the OnStartTest function and the first for loop is a concession to the test code in that in a real world example, the rules for the objects would probably not be set immediately before the code that implements them.

Before we can test the training file, it is loaded into a Fuzzy Double Pattern array which is copied from the way that the Adaline Network loads the data before processing it. Once we get into the second for loop, we extract the values from the array and test which rule we are using. Note that at this point we could test each individual value for its rule but as the only option is to set one rule for all, there is little point. The code then simply tests if the values follow the rules applied and outputs the result to the text box on the main tab page.

Finally

Using a kind of Fuzzy Logic, it is possible to duplicate the function of an Adaline Network without having to constantly train the program and to do it in a faster, more flexible way. It is possible to completely change the rules around on this program without any problem what so ever, something that is simply not possible when using a Neural Network.

Overall I feel this idea has some potential and is worth further investigation, primarily by continuing the investigation into seeing if it is possible to duplicate the behavior of other types of Neural Networks using this technique.

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
  • Robert Callan, (1999) The Essence Of Neural Networks, Prentice Hall
  • Timothy Masters (1993) Practical Neural Network Recipes In C++, Morgan Kaufmann (Academic Press)
  • Joey Rogers (1997) Object-Orientated Neural Networks in C++, Academic Press
  • Simon Haykin (1999) Neural Networks A Comprehensive Foundation, Prentice Hall
  • Bernd Oestereich (2002) Developing Software With UML Object-Orientated Analysis And Design In Practice Addison Wesley
  • R Beale & T Jackson (1990) Neural Computing An Introduction, Institute Of Physics Publishing
  • Bart Kosko (1994) Fuzzy Thinking, Flamingo
  • Buckley & Eslami (2002) An Introduction To Fuzzy Logic And Fuzzy Sets, Physica-Verlag

License

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