Click here to Skip to main content
13,055,408 members (54,410 online)
Click here to Skip to main content

Stats

81.3K views
32 bookmarked
Posted 29 Oct 2003

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

namespace Fuzzy_Logic_Library
{
	/// <summary>
	/// Fuzzy number class deals with fuzzy numbers
	/// </summary>
	public class FuzzyNumber : FuzzyBasic
	{

		public FuzzyNumber() : base()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public FuzzyNumber( double number ) : base()
		{
			Number = number;
		}

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

		public FuzzyNumber( double rangeLow, double rangeHigh ) : base()
		{
			Minimum = rangeLow;
			Maximum = rangeHigh;
			Number = rangeLow + ( ( rangeHigh - rangeLow ) /2 );
			Membership = 1.0;
		}

		public FuzzyNumber( double number, double rangeLow, double rangeHigh ) : base()
		{
			Minimum = rangeLow;
			Maximum = rangeHigh;
			Number = number;
			Membership = 0.0;
			SetMembership();
		}

		public FuzzyNumber( string name, double number )
		{
			Minimum = number - 10;
			Maximum = number + 10;
			Number = number;
			SetMembership();
			Name = name;
		}

		public FuzzyNumber( string name, double number, double rangeLow, double rangeHigh ) : base()
		{
			Minimum = rangeLow;
			Maximum = rangeHigh;
			Number = number;
			SetMembership();
			Name = name;
		}

		public FuzzyNumber( string name, double rangeLow, double rangeHigh ) : base()
		{
			Minimum = rangeLow;
			Maximum = rangeHigh;
			Number = rangeLow + ( ( rangeHigh - rangeLow ) / 2 );
			Membership = 1.0;
			Name = name;
		}

		public FuzzyNumber( string name, double number, double rangeLow, double rangeHigh, bool bUseMaxAsComplete ) : base()
		{
			Minimum = rangeLow;
			if( bUseMaxAsComplete == true )
				Maximum = rangeHigh + rangeHigh;
			else
				Maximum = rangeHigh;

			Number = number;
			Membership = 0.0;
			Name = name;
			SetMembership();
		}


		public override string ToString()
		{
			return base.ToString() + ", Number = " + Number.ToString() + ", Minimum = " + Minimum.ToString() + ", Maximum = " + Maximum.ToString() + ", Membership = " + Membership.ToString();
		}


		/// <summary>
		/// Overloaded operators Note no attempt is made to check for minimum and maximum
		/// values this is up to the code using the class 
		/// </summary>

		public static FuzzyNumber operator +( FuzzyNumber num, double number )
		{
			FuzzyNumber fuzzTemp = new FuzzyNumber( num.Number + number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator +( FuzzyNumber num, FuzzyNumber number )
		{
			FuzzyNumber fuzzTemp = new FuzzyNumber( num.Number + number.Number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator -( FuzzyNumber num, double number )
		{
			FuzzyNumber fuzzTemp = new FuzzyNumber( num.Number - number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator -( FuzzyNumber num, FuzzyNumber number )
		{
			FuzzyNumber fuzzTemp = new FuzzyNumber( num.Number - number.Number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator *( FuzzyNumber num, double number )
		{
			FuzzyNumber fuzzTemp;
			if( num.Number == 0.0 || number == 0.0 )
			{
				fuzzTemp = new FuzzyNumber( num.Number, num.Minimum, num.Maximum );
				fuzzTemp.SetMembership();
				return fuzzTemp;
			}

			fuzzTemp = new FuzzyNumber( num.Number * number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator *( FuzzyNumber num, FuzzyNumber number )
		{
			FuzzyNumber fuzzTemp;
			if( num.Number == 0.0 || number.Number == 0.0 )
			{
				fuzzTemp = new FuzzyNumber( num.Number, num.Minimum, num.Maximum );
				fuzzTemp.SetMembership();
				return fuzzTemp;
			}

			fuzzTemp = new FuzzyNumber( num.Number * number.Number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator /( FuzzyNumber num, double number )
		{
			FuzzyNumber fuzzTemp;
			if( num.Number == 0.0 || number == 0.0 )
			{
				fuzzTemp = new FuzzyNumber( num.Number, num.Minimum, num.Maximum );
				fuzzTemp.SetMembership();
				return fuzzTemp;
			}

			fuzzTemp = new FuzzyNumber( num.Number / number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator /( FuzzyNumber num, FuzzyNumber number )
		{
			FuzzyNumber fuzzTemp;
			if( num.Number == 0.0 || number.Number == 0.0 )
			{
				fuzzTemp = new FuzzyNumber( num.Number, num.Minimum, num.Maximum );
				fuzzTemp.SetMembership();
				return fuzzTemp;
			}

			fuzzTemp = new FuzzyNumber( num.Number / number.Number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator %( FuzzyNumber num, double number )
		{
			FuzzyNumber fuzzTemp;
			if( num.Number == 0.0 || number == 0.0 )
			{
				fuzzTemp = new FuzzyNumber( num.Number, num.Minimum, num.Maximum );
				fuzzTemp.SetMembership();
				return fuzzTemp;
			}

			fuzzTemp = new FuzzyNumber( num.Number % number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}

		public static FuzzyNumber operator %( FuzzyNumber num, FuzzyNumber number )
		{
			FuzzyNumber fuzzTemp;
			if( num.Number == 0.0 || number.Number == 0.0 )
			{
				fuzzTemp = new FuzzyNumber( num.Number, num.Minimum, num.Maximum );
				fuzzTemp.SetMembership();
				return fuzzTemp;
			}

			fuzzTemp = new FuzzyNumber( num.Number % number.Number, num.Minimum, num.Maximum );
			fuzzTemp.SetMembership();
			return fuzzTemp;
		}
	}

	/// <summary>
	/// The fuzzy set class for holding and manipulating fuzzy numbers
	/// </summary>
	public class FuzzyNumberSet : FuzzySet
	{
		private double setValue;

		public double FuzzySetValue
		{
			get
			{
				return setValue;
			}
			set
			{
				setValue = value;
				UpdateSetValue();
			}
		}
		
		/// <summary>
		/// basic constructor
		/// </summary>
		public FuzzyNumberSet() : base()
		{
		}

		/// <summary>
		///  conbstructor that creates a number of empty fuzzy numbers
		/// </summary>
		/// <param name="nNumberCount"></param>
		public FuzzyNumberSet( int nNumberCount ) : base()
		{

			for( int i=0; i<nNumberCount; i++ )
			{
				FuzzyNumber temp = new FuzzyNumber();
				FuzzyArray.Add( temp );
			}
		}

		/// <summary>
		/// constructor that takes the first fuzzy number
		/// </summary>
		/// <param name="fuzzyNum"></param>
		public FuzzyNumberSet( FuzzyNumber fuzzyNum ) : base()
		{
			FuzzyArray.Add( fuzzyNum );
		}

		/// <summary>
		/// constructor that takes the values for the first fuzzy number
		/// </summary>
		/// <param name="number"></param>
		/// <param name="minimum"></param>
		/// <param name="maximum"></param>
		public FuzzyNumberSet( double number, double minimum, double maximum ) : base()
		{
			FuzzyNumber temp = new FuzzyNumber( number, minimum, maximum );
			FuzzyArray.Add( temp );
		}

		

		/// <summary>
		///  get the name of the current value of the set
		/// </summary>
		/// <returns></returns>
		public override string GetTerm()
		{
			for( int i=0; i<this.Count; i++ )
			{
				FuzzyNumber temp = ( FuzzyNumber )FuzzyArray[ i ];
				if( temp.Number == this.FuzzySetValue )
				{
					return temp.Name;
				}
			}

			return null;
		}




		/// <summary>
		///  Is the current value within the specified term ( name of the fuzzy number )
		///  
		/// </summary>
		/// <param name="strTerm">name of the fuzzy number that the value is supposed to be within</param>
		/// <returns>true or false</returns>
		public override bool IsTerm( string strTerm )
		{
			for( int i=0; i<this.Count; i++ )
			{
				if( ( ( FuzzyBasic )FuzzyArray[ i ] ).Name == strTerm )
				{
					FuzzyBasic temp = ( FuzzyBasic )FuzzyArray[ i ];
					if( temp.Maximum >= this.setValue && temp.Minimum <= this.setValue )
					{
						return true;
					} 
					else
						return false;
				}
			}

			return false;
		}

		/// <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 override double ValueFromTerm( string strTerm )
		{
			for( int i=0; i<this.Count; i++ )
			{
				FuzzyNumber temp = ( FuzzyNumber )FuzzyArray[ i ];
				if( temp.Name == strTerm )
				{
					return ( temp.Maximum - ( ( temp.Maximum - temp.Minimum ) / 2 ) );
				}
			}

			return 0.0;
		}



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


		/// <summary>
		/// update the individual fuzzy numbers when the set value changes
		/// </summary>
		private void UpdateSetValue()
		{
			for( int i=0; i<this.Count; i++ )
			{
				if( this.FuzzySetValue <= ( ( FuzzyNumber )FuzzyArray[ i ] ).Maximum  
					&& this.FuzzySetValue >= ( ( FuzzyNumber )FuzzyArray[ i ] ).Minimum )
				{
					( ( FuzzyNumber )FuzzyArray[ i ] ).Number = this.FuzzySetValue;
				}
				else
					( ( FuzzyNumber )FuzzyArray[ i ] ).Number = 0.0;
			}
		}


		/// Set operations
		/// Set Operations need to go here as you can't create a base class set ( FuzzySet ) and 
		/// then down cast it in CSharp
		/// ( Still thinking in C++ )
		
		/// <summary>
		/// return a union of the two passed sets
		/// </summary>
		/// <param name="setOne"></param>
		/// <param name="setTwo"></param>
		/// <returns></returns>
		public FuzzyNumberSet Union( FuzzyNumberSet fuzzySet )
		{
			FuzzyNumberSet returnSet = new FuzzyNumberSet();
			bool bFound = false;

			for( int i=0; i<this.FuzzyArray.Count; i++ )
			{
				returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )this.FuzzyArray[ i ] );
			}

			/// Get the ones in set one but not in set two
			for( int i=0; i<fuzzySet.Count; i++ )
			{
				bFound = false;

				for( int n=0; n<this.Count; n++ )
				{
					if( ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number )
					{
						bFound = true;
						n=fuzzySet.Count;
					}
				}

				if( bFound == false )
				{
					returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] );					
				}
			}

			return returnSet;
		}


		/// <summary>
		/// Union override that allows the setting of the membership parameters
		/// </summary>
		/// <param name="setOne"></param>
		/// <param name="setTwo"></param>
		/// <param name="setParams"></param>
		/// <returns></returns>
		public FuzzyNumberSet Union( FuzzyNumberSet fuzzySet, FuzzySetParameters setParams )
		{
			FuzzyNumberSet returnSet = new FuzzyNumberSet();

			double dSetOneMinMinimum;
			double dSetOneMaxMinimum;
			double dSetOneMinMembership; 
			double dSetOneMaxMembership;
			double dSetOneMinMaximum;
			double dSetOneMaxMaximum;
			double dSetOneMinNumber;
			double dSetOneMaxNumber;
			double dSetTwoMinMinimum;
			double dSetTwoMaxMinimum;
			double dSetTwoMinMembership;
			double dSetTwoMaxMembership;
			double dSetTwoMinMaximum;
			double dSetTwoMaxMaximum;
			double dSetTwoMinNumber;
			double dSetTwoMaxNumber;


			/// get all the items from set one that fit the required ( passed in ) parameters.
			for( int i=0; i<this.Count; i++ )
			{
				if( setParams.SetOneMaxMembership == 0 )
					dSetOneMaxMembership = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership;
				else
					dSetOneMaxMembership = setParams.SetOneMaxMembership;

				if( setParams.SetOneMaxMinimum == 0 )
					dSetOneMaxMinimum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum;
				else
					dSetOneMaxMinimum = setParams.SetOneMaxMinimum;

				if( setParams.SetOneMaxNumber == 0 )
					dSetOneMaxNumber = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number;
				else
					dSetOneMaxNumber = setParams.SetOneMaxNumber;

				if( setParams.SetOneMinMaximum == 0 )
					dSetOneMinMaximum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum;
				else
					dSetOneMinMaximum = setParams.SetOneMinMaximum;

				if( setParams.SetOneMaxMaximum == 0 )
					dSetOneMaxMaximum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum;
				else
					dSetOneMaxMaximum = setParams.SetOneMaxMaximum;

				if( setParams.SetOneMinMembership == 0 )
					dSetOneMinMembership = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership;
				else
					dSetOneMinMembership = setParams.SetOneMinMembership;

				if( setParams.SetOneMinMinimum == 0 )
					dSetOneMinMinimum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum;
				else
					dSetOneMinMinimum = setParams.SetOneMinMinimum;

				if( setParams.SetOneMinNumber == 0 )
					dSetOneMinNumber = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number;
				else
					dSetOneMinNumber = setParams.SetOneMinNumber;

				if( ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership <= dSetOneMaxMembership &&
					( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum <= dSetOneMaxMaximum &&
					( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum <= dSetOneMaxMinimum &&
					( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number  <= dSetOneMaxNumber && 
					( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership >= dSetOneMinMembership &&
					( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum >= dSetOneMinMaximum &&
					( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum >= dSetOneMinMinimum && 
					( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number >= dSetOneMinNumber )
				{
					returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )this.FuzzyArray[ i ] );
				}
			}

			/// Get the ones in set two, not in set one ( though none should be when comparing different sets )
			for( int i=0; i<fuzzySet.Count; i++ )
			{
				if( setParams.SetTwoMaxMembership == 0 )
					dSetTwoMaxMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership;
				else
					dSetTwoMaxMembership = setParams.SetTwoMaxMembership;

				if( setParams.SetTwoMaxMinimum == 0 )
					dSetTwoMaxMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum;
				else
					dSetTwoMaxMinimum = setParams.SetTwoMaxMinimum;

				if( setParams.SetTwoMaxNumber == 0 )
					dSetTwoMaxNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number;
				else
					dSetTwoMaxNumber = setParams.SetTwoMaxNumber;

				if( setParams.SetTwoMinMaximum == 0 )
					dSetTwoMinMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum;
				else
					dSetTwoMinMaximum = setParams.SetTwoMinMaximum;

				if( setParams.SetTwoMaxMaximum == 0 )
					dSetTwoMaxMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum;
				else
					dSetTwoMaxMaximum = setParams.SetTwoMaxMaximum;

				if( setParams.SetTwoMinMembership == 0 )
					dSetTwoMinMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership;
				else
					dSetTwoMinMembership = setParams.SetTwoMinMembership;

				if( setParams.SetTwoMinMinimum == 0 ) 
					dSetTwoMinMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum;
				else
					dSetTwoMinMinimum = setParams.SetTwoMinMinimum;

				if( setParams.SetTwoMinNumber == 0 )
					dSetTwoMinNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number;
				else
					dSetTwoMinNumber = setParams.SetTwoMinNumber;

				for( int n=0; n<fuzzySet.Count; n++ )
				{

					if( setParams.SetOneMaxMembership == 0 )
						dSetOneMaxMembership = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership;
					else
						dSetOneMaxMembership = setParams.SetOneMaxMembership;

					if( setParams.SetOneMaxMinimum == 0 )
						dSetOneMaxMinimum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum;
					else
						dSetOneMaxMinimum = setParams.SetOneMaxMinimum;

					if( setParams.SetOneMaxNumber == 0 )
						dSetOneMaxNumber = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number;
					else
						dSetOneMaxNumber = setParams.SetOneMaxNumber;

					if( setParams.SetOneMinMaximum == 0 )
						dSetOneMinMaximum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum;
					else
						dSetOneMinMaximum = setParams.SetOneMinMaximum;

					if( setParams.SetOneMaxMaximum == 0 )
						dSetOneMaxMaximum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum;
					else
						dSetOneMaxMaximum = setParams.SetOneMaxMaximum;

					if( setParams.SetOneMinMembership == 0 )
						dSetOneMinMembership = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership;
					else
						dSetOneMinMembership = setParams.SetOneMinMembership;

					if( setParams.SetOneMinMinimum == 0 )
						dSetOneMinMinimum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum;
					else
						dSetOneMinMinimum = setParams.SetOneMinMinimum;

					if( setParams.SetOneMinNumber == 0 )
						dSetOneMinNumber = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number;
					else
						dSetOneMinNumber = setParams.SetOneMinNumber;


					if( ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum >= dSetOneMinMaximum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum >= dSetTwoMinMaximum &&
						( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum <= dSetOneMaxMaximum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum <= dSetTwoMaxMaximum &&
						( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership >= dSetOneMinMembership &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership >= dSetTwoMinMembership &&
						( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership <= dSetOneMaxMembership &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership <= dSetTwoMaxMembership &&
						( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum >= dSetOneMinMinimum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum >= dSetTwoMinMinimum &&
						( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum <= dSetOneMaxMinimum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum <= dSetTwoMaxMinimum &&
						( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number >= dSetOneMinNumber &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number >= dSetTwoMinNumber &&
						( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number <= dSetOneMaxNumber &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number <= dSetTwoMaxNumber )
					{
						if( returnSet.IsInSet( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ]  ) == false )
						{
							returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] );
						}
					}
				}
			}

			return returnSet;

		}


		/// <summary>
		/// return an intersection of the two sets
		/// </summary>
		/// <param name="setOne"></param>
		/// <param name="?"></param>
		/// <returns></returns>
		public FuzzyNumberSet Intersection( FuzzyNumberSet fuzzySet )
		{
			FuzzyNumberSet returnSet = new FuzzyNumberSet();

			for( int i=0; i<this.Count; i++ )
			{
				for( int n=0; n<fuzzySet.Count; n++ )
				{
					/// can't decide if name and id should be in here or not
					/// leave them out for now as they are meant to be optional
					if( ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum && 
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number )
					{
						returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )this.FuzzyArray[ i ] );
					}
				}
			}

			return returnSet;
		}


		/// <summary>
		/// return an intersection of the two sets and allow the passing in of the required membership values
		/// </summary>
		/// <param name="setOne"></param>
		/// <param name="setTwo"></param>
		/// <param name="setOneMinMembership"></param>
		/// <param name="setOneMaxMembership"></param>
		/// <param name="setTwoMinMembership"></param>
		/// <param name="setTwoMaxMembership"></param>
		/// <returns></returns>
		public FuzzyNumberSet Intersection( FuzzyNumberSet fuzzySet, FuzzySetParameters setParams )
		{
			FuzzyNumberSet returnSet = new FuzzyNumberSet();

			double dSetOneMinMinimum;
			double dSetOneMaxMinimum;
			double dSetOneMinMembership; 
			double dSetOneMaxMembership;
			double dSetOneMinMaximum;
			double dSetOneMaxMaximum;
			double dSetOneMinNumber;
			double dSetOneMaxNumber;
			double dSetTwoMinMinimum;
			double dSetTwoMaxMinimum;
			double dSetTwoMinMembership;
			double dSetTwoMaxMembership;
			double dSetTwoMinMaximum;
			double dSetTwoMaxMaximum;
			double dSetTwoMinNumber;
			double dSetTwoMaxNumber;

			for( int i=0; i<this.Count; i++ )
			{

				if( setParams.SetOneMaxMembership == 0 )
					dSetOneMaxMembership = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership;
				else
					dSetOneMaxMembership = setParams.SetOneMaxMembership;

				if( setParams.SetOneMaxMinimum == 0 )
					dSetOneMaxMinimum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum;
				else
					dSetOneMaxMinimum = setParams.SetOneMaxMinimum;

				if( setParams.SetOneMaxNumber == 0 )
					dSetOneMaxNumber = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number;
				else
					dSetOneMaxNumber = setParams.SetOneMaxNumber;

				if( setParams.SetOneMinMaximum == 0 )
					dSetOneMinMaximum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum;
				else
					dSetOneMinMaximum = setParams.SetOneMinMaximum;

				if( setParams.SetOneMaxMaximum == 0 )
					dSetOneMaxMaximum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum;
				else
					dSetOneMaxMaximum = setParams.SetOneMaxMaximum;

				if( setParams.SetOneMinMembership == 0 )
					dSetOneMinMembership = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership;
				else
					dSetOneMinMembership = setParams.SetOneMinMembership;

				if( setParams.SetOneMinMinimum == 0 )
					dSetOneMinMinimum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum;
				else
					dSetOneMinMinimum = setParams.SetOneMinMinimum;

				if( setParams.SetOneMinNumber == 0 )
					dSetOneMinNumber = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number;
				else
					dSetOneMinNumber = setParams.SetOneMinNumber;

				for( int n=0; n<fuzzySet.Count; n++ )
				{

					if( setParams.SetTwoMaxMembership == 0 )
						dSetTwoMaxMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership;
					else
						dSetTwoMaxMembership = setParams.SetTwoMaxMembership;

					if( setParams.SetTwoMaxMinimum == 0 )
						dSetTwoMaxMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum;
					else
						dSetTwoMaxMinimum = setParams.SetTwoMaxMinimum;

					if( setParams.SetTwoMaxNumber == 0 )
						dSetTwoMaxNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number;
					else
						dSetTwoMaxNumber = setParams.SetTwoMaxNumber;

					if( setParams.SetTwoMinMaximum == 0 )
						dSetTwoMinMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum;
					else
						dSetTwoMinMaximum = setParams.SetTwoMinMaximum;

					if( setParams.SetTwoMaxMaximum == 0 )
						dSetTwoMaxMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum;
					else
						dSetTwoMaxMaximum = setParams.SetTwoMaxMaximum;

					if( setParams.SetTwoMinMembership == 0 )
						dSetTwoMinMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership;
					else
						dSetTwoMinMembership = setParams.SetTwoMinMembership;

					if( setParams.SetTwoMinMinimum == 0 ) 
						dSetTwoMinMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum;
					else
						dSetTwoMinMinimum = setParams.SetTwoMinMinimum;

					if( setParams.SetTwoMinNumber == 0 )
						dSetTwoMinNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number;
					else
						dSetTwoMinNumber = setParams.SetTwoMinNumber;

					if( ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum >= dSetOneMinMaximum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum >= dSetTwoMinMaximum &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum <= dSetOneMaxMaximum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum <= dSetTwoMaxMaximum &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership >= dSetOneMinMembership &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership >= dSetTwoMinMembership &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership <= dSetOneMaxMembership &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership <= dSetTwoMaxMembership &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum >= dSetOneMinMinimum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum >= dSetTwoMinMinimum &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum <= dSetOneMaxMinimum &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum <= dSetTwoMaxMinimum &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number >= dSetOneMinNumber &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number >= dSetTwoMinNumber &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number <= dSetOneMaxNumber &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number <= dSetTwoMaxNumber )
					{

						if( dSetOneMinMaximum >= dSetTwoMinMaximum &&
							dSetOneMaxMaximum <= dSetTwoMaxMaximum &&
							dSetOneMinMembership >= dSetTwoMinMembership &&
							dSetOneMaxMembership <= dSetTwoMaxMembership &&
							dSetOneMinMinimum >= dSetTwoMinMinimum &&
							dSetOneMaxMinimum <= dSetTwoMaxMinimum &&
							dSetOneMinNumber >= dSetTwoMinNumber &&
							dSetOneMaxNumber <= dSetTwoMaxNumber )					
						{
							returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )this.FuzzyArray[ i ] );
						}
					}
				}
			}

			return returnSet;

		}

		/// <summary>
		/// return an exclusive or set
		/// </summary>
		/// <param name="setOne"></param>
		/// <param name="setTwo"></param>
		/// <returns></returns>
		public FuzzyNumberSet ExclusiveOR( FuzzyNumberSet fuzzySet )
		{
			FuzzyNumberSet returnSet = new FuzzyNumberSet();
			bool bFound = false;


			/// Get the ones in set one but not in set two
			for( int i=0; i<this.Count; i++ )
			{
				bFound = false;

				for( int n=0; n<fuzzySet.Count; n++ )
				{
					if( ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum &&
						( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number == ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number )
					{
						bFound = true;
						n=fuzzySet.Count;
					}
				}

				if( bFound == false )
				{
					returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )this.FuzzyArray[ i ] );
				}
			}

			/// get the ones in set two but not in set one
			for( int i=0; i<fuzzySet.Count; i++ )
			{
				bFound = false;

				for( int n=0; n<this.Count; n++ )
				{
					if( ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum && 
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership &&
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum && 
						( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number == ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number )
					{
						bFound = true;
						n=this.Count;
					}
				}

				if( bFound == false )
				{
					returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] );
				}
			}

			return returnSet;
		}


		/// <summary>
		/// exclusive or taking the fuzzy parameter set.
		/// </summary>
		/// <param name="setOne"></param>
		/// <param name="setTwo"></param>
		/// <param name="setParams"></param>
		/// <returns></returns>
		public FuzzyNumberSet ExclusiveOR( FuzzyNumberSet fuzzySet, FuzzySetParameters setParams )
		{
			FuzzyNumberSet returnSet = new FuzzyNumberSet();
			bool bFound = false;

			double dSetOneMinMinimum;
			double dSetOneMaxMinimum;
			double dSetOneMinMembership; 
			double dSetOneMaxMembership;
			double dSetOneMinMaximum;
			double dSetOneMaxMaximum;
			double dSetOneMinNumber;
			double dSetOneMaxNumber;
			double dSetTwoMinMinimum;
			double dSetTwoMaxMinimum;
			double dSetTwoMinMembership;
			double dSetTwoMaxMembership;
			double dSetTwoMinMaximum;
			double dSetTwoMaxMaximum;
			double dSetTwoMinNumber;
			double dSetTwoMaxNumber;


			/// Get the ones in set one but not in set two
			for( int i=0; i<this.Count; i++ )
			{
				bFound = false;

				if( setParams.SetOneMaxMembership == 0 )
					dSetOneMaxMembership = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership;
				else
					dSetOneMaxMembership = setParams.SetOneMaxMembership;

				if( setParams.SetOneMaxMinimum == 0 )
					dSetOneMaxMinimum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum;
				else
					dSetOneMaxMinimum = setParams.SetOneMaxMinimum;

				if( setParams.SetOneMaxNumber == 0 )
					dSetOneMaxNumber = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number;
				else
					dSetOneMaxNumber = setParams.SetOneMaxNumber;

				if( setParams.SetOneMinMaximum == 0 )
					dSetOneMinMaximum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum;
				else
					dSetOneMinMaximum = setParams.SetOneMinMaximum;

				if( setParams.SetOneMaxMaximum == 0 )
					dSetOneMaxMaximum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Maximum;
				else
					dSetOneMaxMaximum = setParams.SetOneMaxMaximum;

				if( setParams.SetOneMinMembership == 0 )
					dSetOneMinMembership = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Membership;
				else
					dSetOneMinMembership = setParams.SetOneMinMembership;

				if( setParams.SetOneMinMinimum == 0 )
					dSetOneMinMinimum = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Minimum;
				else
					dSetOneMinMinimum = setParams.SetOneMinMinimum;

				if( setParams.SetOneMinNumber == 0 )
					dSetOneMinNumber = ( ( FuzzyNumber )this.FuzzyArray[ i ] ).Number;
				else
					dSetOneMinNumber = setParams.SetOneMinNumber;


				for( int n=0; n<fuzzySet.Count; n++ )
				{
					if( setParams.SetTwoMaxMembership == 0 )
						dSetTwoMaxMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership;
					else
						dSetTwoMaxMembership = setParams.SetTwoMaxMembership;

					if( setParams.SetTwoMaxMinimum == 0 )
						dSetTwoMaxMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum;
					else
						dSetTwoMaxMinimum = setParams.SetTwoMaxMinimum;

					if( setParams.SetTwoMaxNumber == 0 )
						dSetTwoMaxNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number;
					else
						dSetTwoMaxNumber = setParams.SetTwoMaxNumber;

					if( setParams.SetTwoMinMaximum == 0 )
						dSetTwoMinMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum;
					else
						dSetTwoMinMaximum = setParams.SetTwoMinMaximum;

					if( setParams.SetTwoMaxMaximum == 0 )
						dSetTwoMaxMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Maximum;
					else
						dSetTwoMaxMaximum = setParams.SetTwoMaxMaximum;

					if( setParams.SetTwoMinMembership == 0 )
						dSetTwoMinMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Membership;
					else
						dSetTwoMinMembership = setParams.SetTwoMinMembership;

					if( setParams.SetTwoMinMinimum == 0 ) 
						dSetTwoMinMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Minimum;
					else
						dSetTwoMinMinimum = setParams.SetTwoMinMinimum;

					if( setParams.SetTwoMinNumber == 0 )
						dSetTwoMinNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ n ] ).Number;
					else
						dSetTwoMinNumber = setParams.SetTwoMinNumber;


					if( dSetOneMinMaximum >= dSetTwoMinMaximum &&
						dSetOneMaxMaximum <= dSetTwoMaxMaximum &&
						dSetOneMinMembership >= dSetTwoMinMembership &&
						dSetOneMaxMembership <= dSetTwoMaxMembership &&
						dSetOneMinMinimum >= dSetTwoMinMinimum &&
						dSetOneMaxMinimum <= dSetTwoMaxMinimum &&
						dSetOneMinNumber >= dSetTwoMinNumber &&
						dSetOneMaxNumber <= dSetTwoMaxNumber )						
					{
						bFound = true;
						n=fuzzySet.Count;
					}
				}

				if( bFound == false )
				{
					returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )this.FuzzyArray[ i ] );
				}
			}

			/// get the ones in set two but not in set one
			for( int i=0; i<fuzzySet.Count; i++ )
			{
				bFound = false;

				if( setParams.SetTwoMaxMembership == 0 )
					dSetTwoMaxMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership;
				else
					dSetTwoMaxMembership = setParams.SetTwoMaxMembership;

				if( setParams.SetTwoMaxMinimum == 0 )
					dSetTwoMaxMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum;
				else
					dSetTwoMaxMinimum = setParams.SetTwoMaxMinimum;

				if( setParams.SetTwoMaxNumber == 0 )
					dSetTwoMaxNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number;
				else
					dSetTwoMaxNumber = setParams.SetTwoMaxNumber;

				if( setParams.SetTwoMinMaximum == 0 )
					dSetTwoMinMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum;
				else
					dSetTwoMinMaximum = setParams.SetTwoMinMaximum;

				if( setParams.SetTwoMaxMaximum == 0 )
					dSetTwoMaxMaximum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Maximum;
				else
					dSetTwoMaxMaximum = setParams.SetTwoMaxMaximum;

				if( setParams.SetTwoMinMembership == 0 )
					dSetTwoMinMembership = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Membership;
				else
					dSetTwoMinMembership = setParams.SetTwoMinMembership;

				if( setParams.SetTwoMinMinimum == 0 ) 
					dSetTwoMinMinimum = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Minimum;
				else
					dSetTwoMinMinimum = setParams.SetTwoMinMinimum;

				if( setParams.SetTwoMinNumber == 0 )
					dSetTwoMinNumber = ( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] ).Number;
				else
					dSetTwoMinNumber = setParams.SetTwoMinNumber;


				for( int n=0; n<this.Count; n++ )
				{
					if( setParams.SetOneMaxMembership == 0 )
						dSetOneMaxMembership = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership;
					else
						dSetOneMaxMembership = setParams.SetOneMaxMembership;

					if( setParams.SetOneMaxMinimum == 0 )
						dSetOneMaxMinimum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum;
					else
						dSetOneMaxMinimum = setParams.SetOneMaxMinimum;

					if( setParams.SetOneMaxNumber == 0 )
						dSetOneMaxNumber = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number;
					else
						dSetOneMaxNumber = setParams.SetOneMaxNumber;

					if( setParams.SetOneMinMaximum == 0 )
						dSetOneMinMaximum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum;
					else
						dSetOneMinMaximum = setParams.SetOneMinMaximum;

					if( setParams.SetOneMaxMaximum == 0 )
						dSetOneMaxMaximum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Maximum;
					else
						dSetOneMaxMaximum = setParams.SetOneMaxMaximum;

					if( setParams.SetOneMinMembership == 0 )
						dSetOneMinMembership = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Membership;
					else
						dSetOneMinMembership = setParams.SetOneMinMembership;

					if( setParams.SetOneMinMinimum == 0 )
						dSetOneMinMinimum = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Minimum;
					else
						dSetOneMinMinimum = setParams.SetOneMinMinimum;

					if( setParams.SetOneMinNumber == 0 )
						dSetOneMinNumber = ( ( FuzzyNumber )this.FuzzyArray[ n ] ).Number;
					else
						dSetOneMinNumber = setParams.SetOneMinNumber;


					if( dSetOneMinMaximum >= dSetTwoMinMaximum &&
						dSetOneMaxMaximum <= dSetTwoMaxMaximum &&
						dSetOneMinMembership >= dSetTwoMinMembership &&
						dSetOneMaxMembership <= dSetTwoMaxMembership &&
						dSetOneMinMinimum >= dSetTwoMinMinimum &&
						dSetOneMaxMinimum <= dSetTwoMaxMinimum &&
						dSetOneMinNumber >= dSetTwoMinNumber &&
						dSetOneMaxNumber <= dSetTwoMaxNumber )						
					{
						bFound = true;
						n=this.Count;
					}
				}

				if( bFound == false )
				{
					returnSet[ returnSet.Count + 1 ] = new FuzzyNumber( ( FuzzyNumber )fuzzySet.FuzzyArray[ i ] );
				}
			}

			return returnSet;

		}
	}
}

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

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Oct 2003
Article Copyright 2003 by pseudonym67
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid