Click here to Skip to main content
15,891,529 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 92.1K   1.6K   33  
An experiment to see if it possible to duplicate the behavior of the Adaline Network using Fuzzy Logic.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Fuzzy_Logic_Library;
using System.IO;
using System.Text;
using System.Threading;

namespace Fuzzy_Logic_Vs_Adaline_Neural_Network
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button generateTestFileButton;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage mainPage;
		private System.Windows.Forms.TabPage rulesPage;
		private ArrayList patterns;
		private System.Windows.Forms.Button startTestButton;
		private System.Windows.Forms.RichTextBox richTextBox;
		private System.Windows.Forms.CheckBox leftCheckBox;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.CheckBox rightCheckBox;

		/// <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
			//
			patterns = new ArrayList();

		}

		/// <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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.generateTestFileButton = new System.Windows.Forms.Button();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.mainPage = new System.Windows.Forms.TabPage();
			this.richTextBox = new System.Windows.Forms.RichTextBox();
			this.rulesPage = new System.Windows.Forms.TabPage();
			this.rightCheckBox = new System.Windows.Forms.CheckBox();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.leftCheckBox = new System.Windows.Forms.CheckBox();
			this.startTestButton = new System.Windows.Forms.Button();
			this.tabControl1.SuspendLayout();
			this.mainPage.SuspendLayout();
			this.rulesPage.SuspendLayout();
			this.SuspendLayout();
			// 
			// generateTestFileButton
			// 
			this.generateTestFileButton.Location = new System.Drawing.Point(24, 24);
			this.generateTestFileButton.Name = "generateTestFileButton";
			this.generateTestFileButton.Size = new System.Drawing.Size(112, 23);
			this.generateTestFileButton.TabIndex = 1;
			this.generateTestFileButton.Text = "Generate Test File";
			this.generateTestFileButton.Click += new System.EventHandler(this.OnGenerateTestFile);
			// 
			// tabControl1
			// 
			this.tabControl1.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.tabControl1.Controls.Add(this.mainPage);
			this.tabControl1.Controls.Add(this.rulesPage);
			this.tabControl1.Location = new System.Drawing.Point(0, 88);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(464, 456);
			this.tabControl1.TabIndex = 2;
			// 
			// mainPage
			// 
			this.mainPage.Controls.Add(this.richTextBox);
			this.mainPage.Location = new System.Drawing.Point(4, 22);
			this.mainPage.Name = "mainPage";
			this.mainPage.Size = new System.Drawing.Size(456, 430);
			this.mainPage.TabIndex = 0;
			this.mainPage.Text = "Main";
			// 
			// richTextBox
			// 
			this.richTextBox.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.richTextBox.Location = new System.Drawing.Point(0, 8);
			this.richTextBox.Name = "richTextBox";
			this.richTextBox.Size = new System.Drawing.Size(432, 400);
			this.richTextBox.TabIndex = 0;
			this.richTextBox.Text = "";
			// 
			// rulesPage
			// 
			this.rulesPage.Controls.Add(this.rightCheckBox);
			this.rulesPage.Controls.Add(this.label1);
			this.rulesPage.Controls.Add(this.textBox1);
			this.rulesPage.Controls.Add(this.leftCheckBox);
			this.rulesPage.Location = new System.Drawing.Point(4, 22);
			this.rulesPage.Name = "rulesPage";
			this.rulesPage.Size = new System.Drawing.Size(456, 430);
			this.rulesPage.TabIndex = 1;
			this.rulesPage.Text = "Rules";
			// 
			// rightCheckBox
			// 
			this.rightCheckBox.AutoCheck = false;
			this.rightCheckBox.Location = new System.Drawing.Point(24, 136);
			this.rightCheckBox.Name = "rightCheckBox";
			this.rightCheckBox.Size = new System.Drawing.Size(368, 24);
			this.rightCheckBox.TabIndex = 3;
			this.rightCheckBox.Text = "Item On The Right Is Greater Than Item On The left";
			this.rightCheckBox.Click += new System.EventHandler(this.OnRightItemCheckChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(376, 23);
			this.label1.TabIndex = 2;
			this.label1.Text = "Sample of The Training File";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(16, 32);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(376, 72);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "0 0.235057681442731 0.816407734908353 1\r\n1 0.799037052224873 0.380387105690496 -1" +
				"\r\n2 0.961737159156118 0.944366476472638 -1\r\n3 0.52571652993826 0.50834584725478 " +
				"-1\r\n4 0.0896959007204026 0.0723252180369223 -1";
			// 
			// leftCheckBox
			// 
			this.leftCheckBox.AutoCheck = false;
			this.leftCheckBox.Checked = true;
			this.leftCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
			this.leftCheckBox.Location = new System.Drawing.Point(24, 112);
			this.leftCheckBox.Name = "leftCheckBox";
			this.leftCheckBox.Size = new System.Drawing.Size(400, 24);
			this.leftCheckBox.TabIndex = 0;
			this.leftCheckBox.Text = "Item On The Left Is Greater Than Item On the Right ";
			this.leftCheckBox.Click += new System.EventHandler(this.OnLeftItemCheckChanged);
			// 
			// startTestButton
			// 
			this.startTestButton.Location = new System.Drawing.Point(208, 24);
			this.startTestButton.Name = "startTestButton";
			this.startTestButton.Size = new System.Drawing.Size(120, 23);
			this.startTestButton.TabIndex = 3;
			this.startTestButton.Text = "Start Test";
			this.startTestButton.Click += new System.EventHandler(this.OnStartTest);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(440, 526);
			this.Controls.Add(this.startTestButton);
			this.Controls.Add(this.tabControl1);
			this.Controls.Add(this.generateTestFileButton);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Form1";
			this.Text = "Form1";
			this.tabControl1.ResumeLayout(false);
			this.mainPage.ResumeLayout(false);
			this.rulesPage.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void OnLoadTestFile()
		{
			richTextBox.AppendText( "Loading The File lin2var.trn\n" );

			patterns.Clear();

			StreamReader stream = new StreamReader( "lin2var.trn" );
			StringBuilder strString = new StringBuilder( "" );
			StringBuilder strNumber = new StringBuilder( "" );

			/// load the data from the training file
			for( int i=0; i<250; i++ )
			{
				FuzzyDoublePattern aPattern = new FuzzyDoublePattern();
				strString.Remove( 0, strString.Length );
				strString.Append( stream.ReadLine() );

				/// Look Ma No pointers
				int n=0;
				char ch = strString[ n ];
				while( strString[ n ] != ' ' )
					n++;
				while( strString[ n ] == ' ' )
					n++;
				while( strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				aPattern.SetInValue( 0, new FuzzyDouble( Double.Parse( strNumber.ToString() ) ) );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				aPattern.SetInValue( 1, new FuzzyDouble( Double.Parse( strNumber.ToString() ) ) );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( n != strString.Length  && strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				aPattern.SetOutValue( 0, Double.Parse( strNumber.ToString() ) );
				strNumber.Remove( 0, strNumber.Length );

				patterns.Add( aPattern );
			}

			stream.Close();

			richTextBox.AppendText( "Test File Loaded\n" );
		}

		private void OnGenerateTestFile(object sender, System.EventArgs e)
		{
			richTextBox.AppendText( "Generating the values for a new test file\n" );

			Cursor.Current = Cursors.WaitCursor;

			double x = 0.0;
			double y = 0.0;
			double dOutput = 0.0;

			FileInfo info = new FileInfo( "Lin2Var.trn" );
			if( info.Exists == true )
				info.Delete();

			FileStream stream = new FileStream( "Lin2Var.trn", FileMode.Create, FileAccess.Write );
			StreamWriter writer = new StreamWriter( stream, Encoding.UTF8 );

			for( int i=0; i<250; i++ )
			{
				x = Random( -1, 1 );
				y = Random( -1, 1 );

				if( y<x )
					dOutput = -1.0;
				else
					dOutput = 1.0;

	
				writer.WriteLine( i.ToString() + " " + x.ToString() + " " + y.ToString() + " " + dOutput.ToString() );
	
			}

			writer.Close();

			Cursor.Current = Cursors.Default;

			richTextBox.AppendText( "New test file generated\n" );
		}

		private double Random( double dLowerLimit, double dUpperLimit )
		{
			Thread.Sleep( 100 );
			double dNumber = 0.0;
			Random temp = new Random();
			dNumber = temp.Next( ( int )dLowerLimit, ( int )dUpperLimit );
			if( dNumber>0 )
			{
				dNumber -= temp.NextDouble();
				if( dNumber < -1 )
					dNumber = -1;
			}
			else
			{
				dNumber = temp.NextDouble();
				if( dNumber > 1 )
					dNumber = 1;
			}

			return dNumber;
		}

		private void OnStartTest(object sender, System.EventArgs e)
		{
			richTextBox.AppendText( "Starting the test\n" );

			OnLoadTestFile();

			richTextBox.AppendText( "Processing the defined rules\n" );

			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" );
					}
				}
			}

			richTextBox.AppendText( "Test finished\n" );
		}

		private void OnLeftItemCheckChanged(object sender, System.EventArgs e)
		{
			if( this.leftCheckBox.Checked == true )
			{
				this.leftCheckBox.Checked = false;
				this.rightCheckBox.Checked = true;
			}
			else
			{
				this.leftCheckBox.Checked = true;
				this.rightCheckBox.Checked = false;
			}
		}

		private void OnRightItemCheckChanged(object sender, System.EventArgs e)
		{
			if( this.rightCheckBox.Checked == true )
			{
				this.rightCheckBox.Checked = false;
				this.leftCheckBox.Checked = true;
			}
			else
			{
				this.rightCheckBox.Checked = true;
				this.leftCheckBox.Checked = false;
			}
		}
	}
}

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