Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Fuzzy Logic Vs Adaline Neural Network

, 29 Oct 2003
An experiment to see if it possible to duplicate the behavior of the Adaline Network using Fuzzy Logic.
fuzzylogicvadaline.zip
Fuzzy Logic Vs Adaline Neural Network
Fuzzy Logic Library
Fuzzy Logic Library.csproj.user
Fuzzy Logic Vs Adaline Neural Network
App.ico
bin
Debug
Fuzzy Logic Library.dll
Fuzzy Logic Vs Adaline Neural Network.exe
Lin2Var.trn
Fuzzy Logic Vs Adaline Neural Network.csproj.user
using System;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Collections;
using System.Text;


namespace Fuzzy_Logic_Library
{

	/// <summary>
	/// removed the basic stuff to a seperate class to avoid code duplication
	/// </summary>
	abstract public class FuzzyBasic
	{
		/// <summary>
		///  Does Fuzzy Number need an absoluteMaximum and an AbsoluteMinimum value
		///  that it cannot pass?
		/// </summary>
		private double dNumber;
		private double dMaximum;
		private double dMinimum;

		/// <summary>
		/// membership value goes between 0 and 2
		/// a value less than 1 is in the lower half of the membership and a value
		/// greater than 1 is in the higher part of the membership
		/// </summary>
		private double dMembership;

		/// <summary>
		/// arbritrary members
		/// </summary>
		private double dID;
		private string strName;

		public double Number
		{
			get
			{
				return dNumber;
			}
			set
			{
				dNumber = value;
				SetMembership();
			}
		}

		public double Maximum
		{
			get
			{
				return dMaximum;
			}
			set
			{
				dMaximum = value;
			}
		}

		/// <summary>
		/// is the value a full member of this set note it can be set by the outside
		/// this is because the code may not always treat the central point between the 
		/// maximum and minimum numbers as the location for full membership
		/// </summary>
		public double Membership
		{
			get
			{
				return dMembership;
			}
			set
			{
				dMembership = value;
			}
		}


		public double Minimum
		{
			get
			{
				return dMinimum;
			}
			set
			{
				dMinimum = value;
			}
		}

		public double ID
		{
			get
			{
				return dID;
			}
			set
			{
				dID = value;
			}
		}

		public string Name
		{
			get
			{
				return strName;
			}
			set
			{
				strName = value;
			}
		}

		public FuzzyBasic()
		{
			Minimum = 0;
			Maximum = 0;
			Number = 0;
			dMembership = 0.0;
			ID = 0;
			Name = "";
		}

		public FuzzyBasic( FuzzyBasic fuzzy ) 
		{
			this.ID = fuzzy.ID;
			this.Maximum = fuzzy.Maximum;
			this.Membership = fuzzy.Membership;
			this.Minimum = fuzzy.Minimum;
			this.Name = fuzzy.Name;
			this.Number = fuzzy.Number;
		}

		/// <summary>
		/// Set the membership value for the number
		/// Now virtual for overriding with the membership method of choice 
		/// </summary>
		public virtual void SetMembership()
		{
			if( Maximum == 0 || Maximum < Minimum )
				return;

			if( Number > Maximum || Number <= Minimum )
			{
				Membership = 0.0;
				return;
			}

			double dMiddle = Minimum + ( ( Maximum - Minimum ) / 2 );
			
			if( Number == dMiddle )
			{
				Membership = 1.0;
			} 
			else if( Number < dMiddle )
			{
				double dHalfMiddle = dMiddle - ( dMiddle / 2 );
				if( Number == dHalfMiddle )
				{
					Membership = 0.50;
				}
				else if( Number > dHalfMiddle )
				{
					double dHalfAgain = dHalfMiddle + ( dHalfMiddle / 2 );
					if( Number == dHalfAgain )
					{
						Membership = 0.75;
					}
					else if( Number > dHalfAgain )
					{
						Membership = 0.87;
					}
					else
						Membership = 0.63;

				}
				else
				{
					double dHalfAgain = dHalfMiddle - ( dHalfMiddle / 2 );
					if( Number == dHalfAgain )
					{
						Membership = 0.25;
					}
					else if( Number > dHalfAgain )
					{
						Membership = 0.37;
					}
					else
						Membership = 0.12;
				}

			}
			else
			{
				double dHalfMiddle = dMiddle + ( dMiddle / 2 );
				if( Number == dHalfMiddle )
				{
					Membership = 1.50;
				}
				else if( Number > dHalfMiddle )
				{
					double dHalfAgain = dHalfMiddle + ( ( Maximum - dHalfMiddle ) / 2 );
					if( Number == dHalfAgain )
					{
						Membership = 1.75;
					}
					else if( Number > dHalfAgain )
					{
						Membership = 1.87;
					}
					else
						Membership = 1.63;

				}
				else
				{
					double dHalfAgain = dHalfMiddle - ( ( dHalfMiddle - dMiddle ) / 2 );
					if( Number == dHalfAgain )
					{
						Membership = 1.25;
					}
					else if( Number > dHalfAgain )
					{
						Membership = 1.37;
					}
					else
						Membership = 1.12;
				}
			}

		}

		/// <summary>
		/// Check if the set values are equal name and id not checked as 
		/// these are arbitrary
		/// </summary>
		/// <param name="number"></param>
		/// <returns></returns>
		public bool IsEqual( FuzzyBasic fuzzy )
		{
			if( fuzzy.Maximum == this.Maximum &&
				fuzzy.Membership == this.Membership &&
				fuzzy.Minimum == this.Minimum && 
				fuzzy.Number == this.Number )
			{
				return true;
			}

			return false;
		}


	}


	/// <summary>
	/// Fuzzy SetParameters class is an aid class to help in setting up variables 
	/// for set operations between different types of sets
	/// </summary>
	public class FuzzySetParameters
	{
		private double dSetOneMinMinimum;
		private double dSetOneMaxMinimum;
		private double dSetOneMinMembership; 
		private double dSetOneMaxMembership;
		private double dSetOneMinMaximum;
		private double dSetOneMaxMaximum;
		private double dSetOneMinNumber;
		private double dSetOneMaxNumber;
		private double dSetTwoMinMinimum;
		private double dSetTwoMaxMinimum;
		private double dSetTwoMinMembership;
		private double dSetTwoMaxMembership;
		private double dSetTwoMinMaximum;
		private double dSetTwoMaxMaximum;
		private double dSetTwoMinNumber;
		private double dSetTwoMaxNumber;

		public FuzzySetParameters()
		{
			dSetOneMinMinimum = 0;
			dSetOneMaxMinimum = 0;
			dSetOneMinMembership = 0; 
			dSetOneMaxMembership = 0;
			dSetOneMinMaximum = 0;
			dSetOneMaxMaximum = 0;
			dSetOneMinNumber = 0;
			dSetOneMaxNumber = 0;
			dSetTwoMinMinimum = 0;
			dSetTwoMaxMinimum = 0;
			dSetTwoMinMembership = 0;
			dSetTwoMaxMembership = 0;
			dSetTwoMinMaximum = 0;
			dSetTwoMaxMaximum = 0;
			dSetTwoMinNumber = 0;
			dSetTwoMaxNumber = 0;
		}


		public double SetOneMinMinimum
		{
			get
			{
				return dSetOneMinMinimum;
			}
			set
			{
				dSetOneMinMinimum = value;
			}
		}

		public double SetOneMaxMinimum
		{
			get
			{
				return dSetOneMaxMinimum;
			}
			set
			{
				dSetOneMaxMinimum = value;
			}
		}

		public double SetOneMinMembership
		{
			get
			{
				return dSetOneMinMembership;
			}
			set
			{
				dSetOneMinMembership = value;
			}
		}

		public double SetOneMaxMembership
		{
			get
			{
				return dSetOneMaxMembership;
			}
			set
			{
				dSetOneMaxMembership = value;
			}
		}

		public double SetOneMinMaximum
		{
			get
			{
				return dSetOneMinMaximum;
			}
			set
			{
				dSetOneMinMaximum = value;
			}
		}

		public double SetOneMaxMaximum
		{
			get
			{
				return dSetOneMaxMaximum;
			}
			set
			{
				dSetOneMaxMaximum = value;
			}
		}

		public double SetOneMinNumber 
		{
			get
			{
				return dSetOneMinNumber;
			}
			set
			{
				dSetOneMinNumber = value;
			}
		}

		public double SetOneMaxNumber
		{
			get
			{
				return dSetOneMaxNumber;
			}
			set
			{
				dSetOneMaxNumber = value;
			}
		}

		public double SetTwoMinMinimum
		{
			get
			{
				return dSetTwoMinMinimum;
			}
			set
			{
				dSetTwoMinMinimum = value;
			}
		}

		public double SetTwoMaxMinimum
		{
			get
			{
				return dSetTwoMaxMinimum;
			}
			set
			{
				dSetTwoMaxMinimum = value;
			}
		}

		public double SetTwoMinMembership
		{
			get
			{
				return dSetTwoMinMembership;
			}
			set
			{
				dSetTwoMinMembership = value;
			}
		}

		public double SetTwoMaxMembership
		{
			get
			{
				return dSetTwoMaxMembership;
			}
			set
			{
				dSetTwoMaxMembership = value;
			}
		}

		public double SetTwoMinMaximum
		{
			get
			{
				return dSetTwoMinMaximum;
			}
			set
			{
				dSetTwoMinMaximum = value;
			}
		}

		public double SetTwoMaxMaximum
		{
			get
			{
				return dSetTwoMaxMaximum;
			}
			set
			{
				dSetTwoMaxMaximum = value;
			}
		}

		public double SetTwoMinNumber 
		{
			get
			{
				return dSetTwoMinNumber;
			}
			set
			{
				dSetTwoMinNumber = value;
			}
		}

		public double SetTwoMaxNumber
		{
			get
			{
				return dSetTwoMaxNumber;
			}
			set
			{
				dSetTwoMaxNumber = value;
			}
		}



	}

	/// <summary>
	/// The Fuzzy Set class holds the basic Fuzzy Set stuff
	/// </summary>
	abstract public class FuzzySet
	{
		private ArrayList arrayFuzzy;

		private string strName;

		public string Name
		{
			get
			{
				return strName;
			}
			set
			{
				strName = value;
			}
		}

		/// <summary>
		/// basic constructor
		/// </summary>
		public FuzzySet()
		{
			arrayFuzzy = new ArrayList();
		}


		/// <summary>
		/// allow direct access to the ArrayList
		/// </summary>
		public ArrayList FuzzyArray
		{
			get
			{
				return arrayFuzzy;
			}
		}

		public int Count
		{
			get
			{
				return arrayFuzzy.Count;
			}
		}

		/// <summary>
		/// is there a fuzzy number that has the membership value of 1
		/// </summary>
		/// <returns></returns>
		public bool IsCompleteMembership()
		{
			for( int i=0; i<arrayFuzzy.Count; i++ )
			{
				if( ( ( FuzzyBasic )arrayFuzzy[ i ] ).Membership == 1.0 )
					return true;
			}

			return false;
		}

		/// <summary>
		/// get the value that has complete membership
		/// </summary>
		/// <returns></returns>
		public FuzzyBasic GetCompleteMembership()
		{
			for( int i=0; i<arrayFuzzy.Count; i++ )
			{
				if( ( ( FuzzyBasic )arrayFuzzy[ i ] ).Membership == 1.0 )
					return ( FuzzyBasic )arrayFuzzy[ i ];
			}

			return null;
		}

		public bool IsInSet( FuzzyBasic fuzzy )
		{
			for( int i=0; i<this.Count; i++ )
			{
				if( ( ( FuzzyBasic )this.FuzzyArray[ i ] ).IsEqual( fuzzy ) == true )
					return true;
			}

			return false;
		}

		/// <summary>
		/// Get the value of the best member in the fuzzy set
		/// Note returns lowest value ie less than one
		/// </summary>
		/// <returns></returns>
		public FuzzyBasic GetBestMembershipValue()
		{
			int nBestMember = 0;
			double dBestValue = 0.0;
			double dTempValue = 0.0;

			for( int i=0; i<arrayFuzzy.Count; i++ )
			{
				dTempValue = ( ( FuzzyBasic )arrayFuzzy[ i ] ).Membership;
				if( dTempValue > dBestValue && dTempValue < 1.0 )
				{
					dBestValue = dTempValue;
					nBestMember = i;
				}
			}

			return ( FuzzyBasic )arrayFuzzy[ nBestMember ];
		}

		/// <summary>
		/// returns the best membership value greater than 1
		/// </summary>
		/// <returns></returns>
		public FuzzyBasic GetHighestBestMembershipValue()
		{
			int nBestMember = 0;
			double dBestValue = 0.0;
			double dTempValue = 0.0;

			for( int i=0; i<arrayFuzzy.Count; i++ )
			{
				dTempValue = ( ( FuzzyBasic )arrayFuzzy[ i ] ).Membership;
				if( dTempValue < dBestValue && dTempValue > 1.0 )
				{
					dBestValue = dTempValue;
					nBestMember = i;
				}
			}

			return ( FuzzyBasic )arrayFuzzy[ nBestMember ];
		}

		#region /* OVERRIDE THESE IN CHILD CLASSES IF NEEDED */
		/// The folowing functions are fairly essential ( so far )
		/// but require specific class function knowledge for implementation. 

		/// <summary>
		/// gets the default membership value for the term ie when membership == 1
		/// therefore gets the middle value
		/// </summary>
		/// <param name="strTerm"></param>
		/// <returns></returns>
		public virtual double ValueFromTerm( string strTerm )
		{
			return 0.0;
		}

		/// <summary>
		///  get the name of the current value of the set
		/// </summary>
		/// <returns></returns>
		public virtual string GetTerm()
		{
			return null;
		}

		/// <summary>
		///  Is the current value within the specified term ( name of the fuzzy item )
		///  
		/// </summary>
		/// <param name="strTerm">name of the fuzzy item that the value is supposed to be within</param>
		/// <returns>true or false</returns>
		public virtual bool IsTerm( string strTerm )
		{
			return false;
		}

		/// <summary>
		///  Set the value for the Set to the Name of a passed in fuzzy number
		/// </summary>
		/// <param name="strTerm"></param>
		public virtual void SetToTerm( string strTerm )
		{
		}

		#endregion


		/// <summary>
		/// use an indexer for the class
		/// </summary>
		public FuzzyBasic this[ int index ]
		{
			get
			{
				if( index <= arrayFuzzy.Count )
					return ( FuzzyBasic )arrayFuzzy[ index ];
				else
					return null;
			}
			set
			{
				if( index < arrayFuzzy.Count )
				{
					arrayFuzzy.RemoveAt( index );
					arrayFuzzy.Insert( index, value );
				}
				else
					arrayFuzzy.Add( value );
			}
		}


		/// get a string for logging and printing the contents of the current fuzzy set
		public string FuzzyPrintOut
		{
			get
			{
				StringBuilder strTemp = new StringBuilder();
				strTemp.Append( "\nContents Of Fuzzy Set " );
				if( this.Name != "" )
					strTemp.Append( "Name = " + this.Name + "\n" );
				else
					strTemp.Append( "\n" );

				for( int i=0; i<this.Count; i++ )
				{
					strTemp.Append( "\n" + ( ( FuzzyBasic )this.FuzzyArray[ i ] ).ToString() + "\n" );
				}

				return strTemp.ToString();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

pseudonym67

United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150128.1 | Last Updated 30 Oct 2003
Article Copyright 2003 by pseudonym67
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid