Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

Fuzzy Logic Vs Adaline Neural Network

Rate me:
Please Sign up or sign in to vote.
4.30/5 (8 votes)
29 Oct 20038 min read 92K   1.6K   33  
An experiment to see if it possible to duplicate the behavior of the Adaline Network using Fuzzy Logic.
using System;


namespace Fuzzy_Logic_Library
{
	/// <summary>
	/// Simple rule interface
	/// </summary>
	public interface IFuzzyBasicComparisonRules
	{
		bool IsGreaterThan
		{
			get;
			set;
		}

		bool IsLessThan
		{
			get;
			set;
		}

		bool IsEqual
		{
			get;
			set;
		}
	}

	public interface IFuzzyExtendedComparisonRules
	{
		bool IsNotEqual
		{
			get;
			set;
		}

		bool IsLessThanOrEqual
		{
			get;
			set;
		}

		bool IsGreaterThanOrEqual
		{
			get;
			set;
		}
	}



	/// <summary>
	/// reimplement the integer class ( grrrr )
	/// </summary>
	public class FuzzyInteger : IFuzzyBasicComparisonRules
	{
		private bool bGreaterThan;
		private bool bLessThan;
		private bool bEqual;

		private Int32 integer;

		public int Number
		{
			get
			{
				return integer;
			}
			set
			{
				integer = value;
			}
		}

		public FuzzyInteger()
		{
			bGreaterThan = false;
			bLessThan = false;
			bEqual = false;
		}

		public FuzzyInteger( int nValue  )
		{
			integer = nValue;

			bGreaterThan = false;
			bLessThan = false;
			bEqual = false;
		}

		public FuzzyInteger( bool greaterThan ) : this()
		{
			bGreaterThan = greaterThan;
		}

		public FuzzyInteger( int nValue, bool greaterThan ) : this( greaterThan )
		{
			integer = nValue;
		}

		public FuzzyInteger( bool greaterThan, bool lessThan ) : this()
		{
			bGreaterThan = greaterThan;
			bLessThan = lessThan;
		}

		public FuzzyInteger( int nValue, bool greaterThan, bool lessThan ) : this( greaterThan, lessThan )
		{
			integer = nValue;
		}

		public FuzzyInteger( bool greaterThan, bool lessThan, bool equal ) 
		{
			bGreaterThan = greaterThan;
			bLessThan = lessThan;
			bEqual = equal;	
		}

		public FuzzyInteger( int nValue, bool greaterThan, bool lessThan, bool equal ) : this( greaterThan, lessThan, equal )
		{
			integer = nValue;
		}

		#region IFuzzyBasicComparisonRules Members

		public bool IsGreaterThan
		{
			get
			{
				// TODO:  Add FuzzyBasicComparisonRules.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 FuzzyBasicComparisionRules.IsEqual setter implementation
				bEqual = value;
			}
		}

		#endregion


		public static FuzzyInteger operator +( FuzzyInteger num, int number )
		{
			return new FuzzyInteger( num.Number + number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator +( FuzzyInteger num, FuzzyInteger number )
		{
			return new FuzzyInteger( num.Number + number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator -( FuzzyInteger num, int number )
		{
			return new FuzzyInteger( num.Number - number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator -( FuzzyInteger num, FuzzyInteger number )
		{
			return new FuzzyInteger( num.Number - number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator *( FuzzyInteger num, int number )
		{
			return new FuzzyInteger( num.Number * number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator *( FuzzyInteger num, FuzzyInteger number )
		{
			return new FuzzyInteger( num.Number * number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator /( FuzzyInteger num, int number )
		{
			return new FuzzyInteger( num.Number / number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator /( FuzzyInteger num, FuzzyInteger number )
		{
			return new FuzzyInteger( num.Number / number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator %( FuzzyInteger num, int number )
		{
			return new FuzzyInteger( num.Number % number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator %( FuzzyInteger num, FuzzyInteger number )
		{
			return new FuzzyInteger( num.Number % number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator ++( FuzzyInteger num )
		{
			return new FuzzyInteger( num.Number++, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyInteger operator --( FuzzyInteger num )
		{
			return new FuzzyInteger( num.Number--, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static bool operator ==( FuzzyInteger num, int number )
		{
			if( num.Number == number )
				return true;
			else
				return false;
		}

		public static bool operator ==( FuzzyInteger num, FuzzyInteger number )
		{
			if( num.Number == number.Number )
				return true;
			else
				return false;
		}

		public static bool operator !=( FuzzyInteger num, int number )
		{
			if( num.Number != number )
				return true;
			else
				return false;
		}

		public static bool operator !=( FuzzyInteger num, FuzzyInteger number )
		{
			if( num.Number != number.Number )
				return true;
			else
				return false;
		}

		public static bool operator >( FuzzyInteger num, int number )
		{
			if( num.Number > number )
				return true;
			else
				return false;
		}

		public static bool operator >( FuzzyInteger num, FuzzyInteger number )
		{
			if( num.Number > number.Number )
				return true;
			else
				return false;
		}

		public static bool operator <( FuzzyInteger num, int number )
		{
			if( num.Number < number )
				return true;
			else
				return false;
		}

		public static bool operator <( FuzzyInteger num, FuzzyInteger number )
		{
			if( num.Number < number.Number )
				return true;
			else
				return false;
		}

		public static bool operator >=( FuzzyInteger num, int number )
		{
			if( num.Number >= number )
				return true;
			else
				return false;
		}

		public static bool operator >=( FuzzyInteger num, FuzzyInteger number )
		{
			if( num.Number >= number.Number )
				return true;
			else
				return false;
		}

		public static bool operator <=( FuzzyInteger num, int number )
		{
			if( num.Number <= number )
				return true;
			else
				return false;
		}

		public static bool operator <=( FuzzyInteger num, FuzzyInteger number )
		{
			if( num.Number <= number.Number )
				return true;
			else
				return false;
		}

		public override bool Equals(object obj)
		{
			return base.Equals (obj);
		}

		public override int GetHashCode()
		{
			return base.GetHashCode ();
		}

		public override string ToString()
		{
			return base.ToString () + ", IsGreaterThan :- " + this.IsGreaterThan.ToString() + ", IsLessThan :- " + this.IsLessThan.ToString() + ", IsEqual :- " + this.IsEqual.ToString();
		}

	}

	/// <summary>
	/// implement the fuzzy double class 
	/// </summary>
	public class FuzzyDouble : IFuzzyBasicComparisonRules
	{
		private bool bGreaterThan;
		private bool bLessThan;
		private bool bEqual;

		private double dNumber;

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

		public FuzzyDouble()
		{
			bGreaterThan = false;
			bLessThan = false;
			bEqual = false;
		}

		public FuzzyDouble( double dValue  )
		{
			dNumber = dValue;

			bGreaterThan = false;
			bLessThan = false;
			bEqual = false;
		}

		public FuzzyDouble( bool greaterThan ) : this()
		{
			bGreaterThan = greaterThan;
		}

		public FuzzyDouble( double dValue, bool greaterThan ) : this( greaterThan )
		{
			dNumber = dValue;
		}

		public FuzzyDouble( bool greaterThan, bool lessThan ) : this()
		{
			bGreaterThan = greaterThan;
			bLessThan = lessThan;
		}

		public FuzzyDouble( double dValue, bool greaterThan, bool lessThan ) : this( greaterThan, lessThan )
		{
			dNumber = dValue;
		}

		public FuzzyDouble( bool greaterThan, bool lessThan, bool equal ) 
		{
			bGreaterThan = greaterThan;
			bLessThan = lessThan;
			bEqual = equal;	
		}

		public FuzzyDouble( double dValue, bool greaterThan, bool lessThan, bool equal ) : this( greaterThan, lessThan, equal )
		{
			dNumber = dValue;
		}

		#region IFuzzyBasicComparisonRules Members

		public bool IsGreaterThan
		{
			get
			{
				// TODO:  Add FuzzyBasicComparisonRules.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 FuzzyBasicComparisionRules.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


		public static FuzzyDouble operator +( FuzzyDouble num, int number )
		{
			return new FuzzyDouble( num.Number + number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator +( FuzzyDouble num, FuzzyDouble number )
		{
			return new FuzzyDouble( num.Number + number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator -( FuzzyDouble num, int number )
		{
			return new FuzzyDouble( num.Number - number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator -( FuzzyDouble num, FuzzyInteger number )
		{
			return new FuzzyDouble( num.Number - number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator *( FuzzyDouble num, int number )
		{
			return new FuzzyDouble( num.Number * number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator *( FuzzyDouble num, FuzzyDouble number )
		{
			return new FuzzyDouble( num.Number * number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator /( FuzzyDouble num, int number )
		{
			return new FuzzyDouble( num.Number / number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator /( FuzzyDouble num, FuzzyDouble number )
		{
			return new FuzzyDouble( num.Number / number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator %( FuzzyDouble num, int number )
		{
			return new FuzzyDouble( num.Number % number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator %( FuzzyDouble num, FuzzyInteger number )
		{
			return new FuzzyDouble( num.Number % number.Number, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator ++( FuzzyDouble num )
		{
			return new FuzzyDouble( num.Number++, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static FuzzyDouble operator --( FuzzyDouble num )
		{
			return new FuzzyDouble( num.Number--, num.IsGreaterThan, num.IsLessThan, num.IsEqual );
		}

		public static bool operator ==( FuzzyDouble num, int number )
		{
			if( num.Number == number )
				return true;
			else
				return false;
		}

		public static bool operator ==( FuzzyDouble num, FuzzyDouble number )
		{
			if( num.Number == number.Number )
				return true;
			else
				return false;
		}

		public static bool operator !=( FuzzyDouble num, int number )
		{
			if( num.Number != number )
				return true;
			else
				return false;
		}

		public static bool operator !=( FuzzyDouble num, FuzzyDouble number )
		{
			if( num.Number != number.Number )
				return true;
			else
				return false;
		}

		public static bool operator >( FuzzyDouble num, int number )
		{
			if( num.Number > number )
				return true;
			else
				return false;
		}

		public static bool operator >( FuzzyDouble num, FuzzyDouble number )
		{
			if( num.Number > number.Number )
				return true;
			else
				return false;
		}

		public static bool operator <( FuzzyDouble num, int number )
		{
			if( num.Number < number )
				return true;
			else
				return false;
		}

		public static bool operator <( FuzzyDouble num, FuzzyDouble number )
		{
			if( num.Number < number.Number )
				return true;
			else
				return false;
		}

		public static bool operator >=( FuzzyDouble num, int number )
		{
			if( num.Number >= number )
				return true;
			else
				return false;
		}

		public static bool operator >=( FuzzyDouble num, FuzzyDouble number )
		{
			if( num.Number >= number.Number )
				return true;
			else
				return false;
		}

		public static bool operator <=( FuzzyDouble num, int number )
		{
			if( num.Number <= number )
				return true;
			else
				return false;
		}

		public static bool operator <=( FuzzyDouble num, FuzzyDouble number )
		{
			if( num.Number <= number.Number )
				return true;
			else
				return false;
		}

		public override bool Equals(object obj)
		{
			return base.Equals (obj);
		}

		public override int GetHashCode()
		{
			return base.GetHashCode ();
		}

		public override string ToString()
		{
			return base.ToString () + ", IsGreaterThan :- " + this.IsGreaterThan.ToString() + ", IsLessThan :- " + this.IsLessThan.ToString() + ", IsEqual :- " + this.IsEqual.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


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