Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C#

Fuzzy Logic Dot Net

Rate me:
Please Sign up or sign in to vote.
4.58/5 (27 votes)
7 Aug 200314 min read 154.5K   2.4K   77  
A Fuzzy Logic Library in C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Fuzzy_Logic_Library; 
using System.Xml.Serialization;
using System.IO;
 
namespace Fuzzy_Dot_Net
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.RichTextBox textBox;
		private System.Windows.Forms.Button fuzzyNumberTest;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox = new System.Windows.Forms.RichTextBox();
			this.fuzzyNumberTest = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox
			// 
			this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox.Location = new System.Drawing.Point(0, 128);
			this.textBox.Name = "textBox";
			this.textBox.Size = new System.Drawing.Size(576, 136);
			this.textBox.TabIndex = 0;
			this.textBox.Text = "";
			// 
			// fuzzyNumberTest
			// 
			this.fuzzyNumberTest.Location = new System.Drawing.Point(8, 16);
			this.fuzzyNumberTest.Name = "fuzzyNumberTest";
			this.fuzzyNumberTest.Size = new System.Drawing.Size(120, 23);
			this.fuzzyNumberTest.TabIndex = 1;
			this.fuzzyNumberTest.Text = "Test Fuzzy Numbers";
			this.fuzzyNumberTest.Click += new System.EventHandler(this.OnFuzzyNumberTest);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(144, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(144, 23);
			this.button1.TabIndex = 2;
			this.button1.Text = "Fuzzy Number Set Test";
			this.button1.Click += new System.EventHandler(this.OnFuzzyNUmberSetTest);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(576, 266);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.fuzzyNumberTest);
			this.Controls.Add(this.textBox);
			this.Name = "Form1";
			this.Text = "Fuzzy Dot Net";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void OnFuzzyNumberTest(object sender, System.EventArgs e)
		{
			textBox.AppendText( "\n Starting Fuzzy Number Test\n" );

			/// test the constructors
			FuzzyNumber one = new FuzzyNumber();

			textBox.AppendText( "\nFuzzy Number with empty constructor = " + one.ToString() + "\n" );

			FuzzyNumber two = new FuzzyNumber( 10, 20 );
			FuzzyNumber three = new FuzzyNumber( 16.3, 22.3344 );

			textBox.AppendText( "\nFuzzy Number with constructor( 10, 20 ) = " + two.ToString() + "\n"  );

			textBox.AppendText( "\nFuzzy Number with constructor( 16.2, 22.3344 ) = " + three.ToString() + "\n" );

			FuzzyNumber four = new FuzzyNumber( 33.8 );

			textBox.AppendText( "\nFuzzy Number with constructor( 33.8 ) = " + four.ToString() + "\n" );

			FuzzyNumber five = new FuzzyNumber( 22, 0, 165 );

			textBox.AppendText( "\nFuzzy Number with constructor( 22, 0, 165 ) = " + five.ToString() + "\n" );

			five += 22.3;

			textBox.AppendText( "\nFuzzy Number using overloaded operator + with value 22.3 = " + five.ToString() + "\n" );

			five += four;

			textBox.AppendText( "\nFuzzy number using overloaded operator + with Fuzzy Number value 33.8 = " + five.ToString() + "\n" );

			two -= 10;

			textBox.AppendText( "\nFuzzy Number using overloaded operator - with value 10 = " + two.ToString() + "\n" );

			five -= two;

			textBox.AppendText( "\nFuzzy Number using overloaded operator - with Fuzzy Number value " + two.ToString() + " = " + five.ToString() + "\n" );

			two *= 10;

			textBox.AppendText( "\nFuzzy Number using overloaded operator * with value 10 = " + two.ToString() + "\n" );
 
			five *= two;

			textBox.AppendText( "\nFuzzy Number using overloaded operator * with Fuzzy Number value " + two.ToString() + " = " + five.ToString()  + "\n" );

			four /= 5;

			textBox.AppendText( "\nFuzzy Number using overloaded operator / with value 5 = " + four.ToString() + "\n" );

			five /= two;

			textBox.AppendText( "\nFuzzy Number using overloaded operator / with Fuzzy Number value " + two.ToString() + " = " + five.ToString() + "\n" );

			four %= 3;

			textBox.AppendText( "\nFuzzy Number using overloaded operator % with value 3 = " + four.ToString() + "\n" );

			five %= two;

			textBox.AppendText( "\nFuzzy Number using overloaded operator % with Fuzzy Number value " + two.ToString() + " = " + five.ToString() + "\n" );
		
			textBox.AppendText( "\nTesting the membership stuff\n" );

			FuzzyNumber six = new FuzzyNumber( "Test membership", 0, 0, 10 );

			textBox.AppendText( "\nFuzzy Number created using constructor( \"Test Membership\", 0, 0, 10 ) " + six.Name + " " + six.ToString() + "\n" );

			for( int i=( int )six.Minimum; i<( int )six.Maximum; i++ )
			{
				six.Number++;

				textBox.AppendText( "\nFuzzy Number incremented = " + six.Name + " " + six.ToString() + "\n" );
			}

			textBox.AppendText( "\n\nRunning Fuzzy Number Demo\n" );
			textBox.AppendText( "\nImagine a person being born\n" );

			FuzzyNumber person = new FuzzyNumber( "person", 0, 0, 80 );

			textBox.AppendText( "\nThis person will live until a maximum age of eighty and they are currently new born\n" );

			textBox.AppendText( "\nAs a Fuzzy Number this is represented as " + person.Name + ", " + person.ToString() + "\n" );

			textBox.AppendText( "\nNow let the person get older\n" );

			for( int i=0; i<=person.Maximum; i++ )
			{
				if( person.Number % 5 == 0 )
				{
					textBox.AppendText( "\nCurrent age = " + person.Number.ToString() + ", Fuzzy Number Values = " + person.Name + ", " + person.ToString() + "\n" );
				}

				person.Number++;
			}

			textBox.AppendText( "\n O.K. They got old and wrinkly now send 'em back to the womb\n" );

			for( int i=( int )person.Maximum; i>=0; i-- )
			{
				if( person.Number % 5 == 0 )
				{
					textBox.AppendText( "\nCurrent age = " + person.Number.ToString() + ", Fuzzy Number Values = " + person.Name + ", " + person.ToString() + "\n" );
				}

				person.Number--;
			}

		}

		private void OnFuzzyNUmberSetTest(object sender, System.EventArgs e)
		{
			textBox.AppendText( "\nTesting the Fuzzy Number Set Class\n" );

			/// test the constructors
			FuzzyNumberSet fuzzySet = new FuzzyNumberSet();

			textBox.AppendText( "\nFuzzy Number Set with empty constructor created\n" );

			FuzzyNumberSet fuzzySet2 = new FuzzyNumberSet( 20 );

			textBox.AppendText( "\nFuzzy Number Set with constructor ( 20 ) called, Array contains " +
				 fuzzySet2.FuzzyArray.Count.ToString() + " ( empty ) sets \n" );

			FuzzyNumberSet fuzzySet3 = new FuzzyNumberSet( 20, 10, 30 );
			fuzzySet3.Name = "fuzzy set 3";

			textBox.AppendText( "\nFuzzy Number Set with constructor ( 20, 10, 30 ) called, Set Contains "
				+ fuzzySet3.FuzzyArray.Count.ToString() + " fuzzy numbers with values " 
				+ fuzzySet3[ 0 ].ToString() + "\n" );

			FuzzyNumber fuzzyNumber = new FuzzyNumber( "fuzzy number for fuzzy set 4", 50, 0, 75 );

			FuzzyNumberSet fuzzySet4 = new FuzzyNumberSet( fuzzyNumber );
			fuzzySet4.Name = "fuzzy set 4";

			textBox.AppendText( "\nFuzzy Number Set with constructor ( FuzzyNumber ) called, Set Contains "
				+ fuzzySet4.FuzzyArray.Count.ToString() + " fuzzy numbers with name "
				+ fuzzySet4.Name + " with values " 
				+ fuzzySet4[ 0 ].ToString() + "\n" );

			textBox.AppendText( "\nTesting the Fuzzy Number Set Membership functions\n" );

			if( fuzzySet3.IsCompleteMembership() == true )
			{
				textBox.AppendText( "\n IsCompleteMembership returned true for "
					+ fuzzySet3.Name + " with values "
					+ fuzzySet3[ 0 ].ToString() + "\n" );
			}
			else
			{
				textBox.AppendText( "\n IsCompleteMembership returned false for " 
					+ fuzzySet3.Name + " with values " 
					+ fuzzySet3[ 0 ].ToString() + "\n" );
			}

			if( fuzzySet4.IsCompleteMembership() == true )
			{
				textBox.AppendText( "\n IsCompleteMembership returned true for "
					+ fuzzySet4.Name + " with values "
					+ fuzzySet4[ 0 ].ToString() + "\n" );
			}
			else
			{
				textBox.AppendText( "\n IsCompleteMembership returned false for " 
					+ fuzzySet4.Name + " with values " 
					+ fuzzySet4[ 0 ].ToString() + "\n" );
			}


			textBox.AppendText( "\nAdding the created fuzzy number to fuzzy set 3\n" );
 
			/// to add a fuzzy number to the indexer add a number that is beyond the current count
			fuzzySet3[ fuzzySet3.FuzzyArray.Count + 1 ] = fuzzyNumber;


			FuzzyNumber temp1 = fuzzySet3.GetBestMembershipValue();
			if( temp1 == null )
				textBox.AppendText( "\nNo Fuzzy number value returned from GetBestMembershipValue()\n" );
			else
				textBox.AppendText( "\nGetBestMembershipValue returned " + temp1.ToString() );

			temp1 = fuzzySet3.GetCompleteMembership();

			if( temp1 == null )
				textBox.AppendText( "\nNo Fuzzy number value returned from GetCompleteMembership()\n" );
			else
				textBox.AppendText( "\nGetCompleteMembership returned " + temp1.ToString() );

			temp1 = fuzzySet3.GetHighestBestMembershipValue();

			if( temp1 == null )
				textBox.AppendText( "\nNo Fuzzy Number values returned from GetHighestMembershipValue()\n" );
			else
				textBox.AppendText( "\nGetHighestMembershipValue returned " + temp1.ToString() );


			/// Test the operations
			

			textBox.AppendText( "\n\nCreating 3 fuzzy sets to test the fuzzy operations\n" );

			FuzzyNumberSet fuzzyOpSet1 = new FuzzyNumberSet();
			fuzzyOpSet1.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 1", 20, 10, 30 ) );
			fuzzyOpSet1.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 2", 30, 20, 40 ) );
			fuzzyOpSet1.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 3", 40, 30, 50 ) );
			fuzzyOpSet1.Name = "Fuzzy Operations Set 1";


			FuzzyNumberSet fuzzyOpSet2 = new FuzzyNumberSet();
			fuzzyOpSet2.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 4", 30, 20, 40 ) );
			fuzzyOpSet2.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 5", 40, 30, 50 ) );
			fuzzyOpSet2.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 6", 50, 40, 60 ) );
			fuzzyOpSet2.Name = "Fuzzy Operations Set 2";

			FuzzyNumberSet fuzzyOpSet3 = new FuzzyNumberSet();
			fuzzyOpSet3.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 7", 40, 30, 50 ) );
			fuzzyOpSet3.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 8", 50, 40, 60 ) );
			fuzzyOpSet3.FuzzyArray.Add( new FuzzyNumber( "fuzzy number 9", 60, 50, 70 ) );
			fuzzyOpSet3.Name = "Fuzzy Operations Set 3";


			textBox.AppendText( "\n Fuzzy Sets to test the operations created \n" );
			
			textBox.AppendText( "\nFuzzy Set Name = " + fuzzyOpSet1.Name + fuzzyOpSet1.FuzzyPrintOut );
			textBox.AppendText( "\nFuzzy Set Name = " + fuzzyOpSet2.Name + fuzzyOpSet2.FuzzyPrintOut );
			textBox.AppendText( "\nFuzzy Set Name = " + fuzzyOpSet3.Name + fuzzyOpSet3.FuzzyPrintOut );

			
			textBox.AppendText( "\n Testing the Fuzzy Union \n" );

			FuzzyNumberSet union = fuzzyOpSet1.Union( fuzzyOpSet2 );

			textBox.AppendText( "\nResults of the union between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + " are\n" );
			textBox.AppendText( union.FuzzyPrintOut + "\n" );

			union = fuzzyOpSet1.Union( fuzzyOpSet3 );

			textBox.AppendText( "\nResults of the union between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + " are\n" );
			textBox.AppendText( union.FuzzyPrintOut + "\n" );

			textBox.AppendText( "\n Testing the Fuzzy Intersection \n" );

			FuzzyNumberSet intersection = fuzzyOpSet1.Intersection( fuzzyOpSet2 );

			textBox.AppendText( "\nResults of the intersection between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + " are\n" );
			textBox.AppendText( intersection.FuzzyPrintOut + "\n" );

			intersection = fuzzyOpSet1.Intersection( fuzzyOpSet3 );

			textBox.AppendText( "\nResults of the intersection between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet3.Name + " are\n" );
			textBox.AppendText( intersection.FuzzyPrintOut + "\n" );

			textBox.AppendText( "\n Testing the Fuzzy ExclusiveOR \n" );
			
			FuzzyNumberSet exclusiveOR = fuzzyOpSet1.ExclusiveOR( fuzzyOpSet2 );

			textBox.AppendText( "\nResults of the exclusiveOR between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + " are\n" );
			textBox.AppendText( exclusiveOR.FuzzyPrintOut + "\n" );

			exclusiveOR = fuzzyOpSet1.ExclusiveOR( fuzzyOpSet3 );

			textBox.AppendText( "\nResults of the exclusiveOR between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet3.Name + " are\n" );
			textBox.AppendText( exclusiveOR.FuzzyPrintOut + "\n" );



			textBox.AppendText( "\nTesting the fuzzy set operations with the fuzzy parameters\n" );
	
			
			/// create the fuzzy set parameters
			FuzzySetParameters setParams = new FuzzySetParameters();

			
			setParams.SetOneMinNumber = 30;
			setParams.SetOneMaxNumber = 50;
			setParams.SetTwoMinNumber = 30;
			setParams.SetTwoMaxNumber = 50;

			/// all other variables in set params are left alone thich means they will
			/// automatically be set to the FuzzyNumberSet values
			
			FuzzyNumberSet unionTest = fuzzyOpSet1.Union( fuzzyOpSet2, setParams );
			unionTest.Name = "Union Test";
 
			textBox.AppendText( "\nResults of the Union between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + "\n" ); 
			textBox.AppendText( unionTest.FuzzyPrintOut + "\n" );

			/// change the min number value to allow more through

			setParams.SetOneMinNumber = 0;
			setParams.SetOneMaxNumber = 50;

			unionTest = fuzzyOpSet1.Union( fuzzyOpSet2, setParams );

			textBox.AppendText( "\nResults of the Union between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + "\n" );
			textBox.AppendText( unionTest.FuzzyPrintOut + "\n" );

			textBox.AppendText( "\nTesting the Intersection with Fuzzy Parameters\n" );

			setParams.SetOneMinNumber = 30;
			setParams.SetOneMaxNumber = 50;
			setParams.SetTwoMinNumber = 30;
			setParams.SetTwoMaxNumber = 50;

			/// all other variables in set params are left alone thich means they will
			/// automatically be set to the FuzzyNumberSet values
			
			FuzzyNumberSet intersectionTest = fuzzyOpSet1.Intersection( fuzzyOpSet2, setParams );
			intersectionTest.Name = "Intersection Test";
 
			textBox.AppendText( "\nResults of the Intersection between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + "\n" ); 
			textBox.AppendText( intersectionTest.FuzzyPrintOut + "\n" );

			/// change the min number value to allow more through

			setParams.SetOneMinNumber = 0;
			setParams.SetOneMaxNumber = 50;

			intersectionTest = fuzzyOpSet1.Intersection( fuzzyOpSet3, setParams );

			textBox.AppendText( "\nResults of the Intersection between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet3.Name + "\n" );
			textBox.AppendText( intersectionTest.FuzzyPrintOut + "\n" );

			
			textBox.AppendText( "\nTesting the Exclusive OR with Fuzzy Parameters\n" );

			setParams.SetOneMinNumber = 30;
			setParams.SetOneMaxNumber = 50;
			setParams.SetTwoMinNumber = 30;
			setParams.SetTwoMaxNumber = 50;

			/// all other variables in set params are left alone thich means they will
			/// automatically be set to the FuzzyNumberSet values
			
			FuzzyNumberSet exclusiveorTest = fuzzyOpSet1.ExclusiveOR( fuzzyOpSet2, setParams );
			exclusiveorTest.Name = "Exclusive OR Test";
 
			textBox.AppendText( "\nResults of the ExclusiveOR between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet2.Name + "\n" ); 
			textBox.AppendText( exclusiveorTest.FuzzyPrintOut + "\n" );

			/// change the min number value to allow more through

			setParams.SetOneMinNumber = 0;
			setParams.SetOneMaxNumber = 50;

			exclusiveorTest = fuzzyOpSet1.ExclusiveOR( fuzzyOpSet3, setParams );

			textBox.AppendText( "\nResults of the ExclusiveOR between " + fuzzyOpSet1.Name + " and " + fuzzyOpSet3.Name + "\n" );
			textBox.AppendText( exclusiveorTest.FuzzyPrintOut + "\n" );


		}
	}
}

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