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

Neural Dot Net Pt 11 A Conclusion Of Sorts

, 9 Dec 2003
A neural network library in C#.
neural_dot_net_2002.zip
Neural Dot Net 2002
App.ico
bin
Debug
AdalineWordFile.dat
adalinewordWorkingFile.wrk
adword.trn
App.ico
BackpropagationWorkingFile.wrk
Lin2Var.trn
Neural Dot Net 2002.exe
Neural Net Library.dll
SelfOrganizingNetworkOne.trn
SelfOrganizingNetworkOne.wrk
SelfOrganizingNetworkOneTest.tst
SharpUtils.dll
equations.Bak
Neural Dot Net 2002.csproj.user
Neural Net Library
bin
Debug
Neural Net Library.csproj.user
obj
Debug
temp
TempPE
SharpUtils
bin
Debug
obj
Debug
temp
TempPE
SharpUtils.csproj.user

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using SharpUtils;
using Neural_Net_Library;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.Xml;

namespace Neural_Net_Test
{

	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private DebugLevel debugLevel;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem FileItem;
		private Logger log;

		/// threading stuff
		private ThreadStart threadStart;
		private Thread thread = null;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem8;

		/// <summary>
		/// Adaline variables ( Adaline 1 )
		/// </summary>
		private static int nNumberOfItemsInAdalineTrainingFile = 250;
		private int nNumberOfItemsInAdalineOneTrainingFile;
		private double dLearningRateOfAdalineOne;
		/// <summary>
		/// Adaline word variables ( Adaline 2 )
		/// </summary>
		private static int nNumberOfItemsInAdalineWordFile = 100;
		private double dLearningRateOfAdalineTwo;
		private int nNumberOfWordTestsToGenerate = 250;
		private bool bAdalineOneUseBias = false;
		private bool bAdalineTwoUseBias = false;
		/// <summary>
		/// BackPropagation variables ( BackPropagation 1 )
		/// </summary>
		private int nNumberOfBackPropagationTests = 250;
		private double dBackPropagationTolerance = 0.4;
		private double dBackPropagationLearningRate = 0.45;
		private double dBackPropagationMomentum = 0.9;
		private bool bBackPropagationOneUseBias = false;
		/// <summary>
		/// BackPropagationWord Variables ( Backpropagation 2 )
		/// </summary>
		private bool bBackPropagationTwoUseBias = false;
		private double dBackPropagationWordLearningRate = 0.45;
		private double dBackPropagationWordMomentum = 0.9;
		private double dBackPropagationWordTolerance = 0.4;
		private string strBackPropagationWordOne = "and";
		private string strBackPropagationWordTwo = "the";
		private string strBackPropagationWordRunFile = "origin-of-species.TXT";
		private string strBackPropagationWordTrainingFile = "originpart.txt";

		/// <summary>
		/// Self Organizing Network One variables
		/// </summary>
		private int nNumberOfItemsInSelfOrganizingNetworkTrainingFile = 300;
		private bool bGenerateNewSelfOrganizingTrainingFile = true;
		private bool bTrainingSelfOrganizingNetwork = true;
		private double dInitialSonOneLearningRate = 0.5;
		private double dFinalSonOneLearningRate = 0.01;
		private int nInitialSonOneNeighborhoodSize = 5;
		private int nFinalSonOneNeighborhoodSize = 5;
		private int nNeighborhoodDecrementInterval = 100;
		private int nNumberOfSonOneIterations = 500;

		/// <summary>
		/// Self Organizing Network two variables
		/// </summary>
		private double dInitialSonTwoLearningRate = 0.5;
		private double dFinalSonTwoLearningRate = 0.01;
		private int nInitialSonTwoNeighborhoodSize = 5;
		private int nFinalSonTwoNeighborhoodSize = 1;
		private int nSonTwoNeighborhoodDecrementInterval = 100;
		private int nNumberOfSonTwoIterations = 500;


		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem11;
		private System.Windows.Forms.MenuItem menuItem12;
		private System.Windows.Forms.MenuItem menuItem13;
		private System.Windows.Forms.MenuItem menuItem14;
		private System.Windows.Forms.MenuItem menuItem15;
		private System.Windows.Forms.MenuItem menuItem16;
		private System.Windows.Forms.MenuItem menuItem17;
		private System.Windows.Forms.MenuItem menuItem18;
		private System.Windows.Forms.MenuItem menuItem19;
		private System.Windows.Forms.MenuItem menuItem20;
		private System.Windows.Forms.MenuItem menuItem21;
		private System.Windows.Forms.MenuItem menuItem22;
		private System.Windows.Forms.MenuItem menuItem23;
		private System.Windows.Forms.MenuItem menuItem24;
		private System.Windows.Forms.MenuItem menuItem26;
		private System.Windows.Forms.MenuItem menuItem27;
		private System.Windows.Forms.MenuItem menuItem28;
		private System.Windows.Forms.MenuItem menuItem29;
		private System.Windows.Forms.MenuItem menuItem30;
		private System.Windows.Forms.MenuItem menuItem31;
		private System.Windows.Forms.MenuItem menuItem32;
		private System.Windows.Forms.MenuItem menuItem33;
		private System.Windows.Forms.MenuItem menuItem34;
		private System.Windows.Forms.MenuItem menuItem35;
		private System.Windows.Forms.MenuItem menuItem36;
		private System.Windows.Forms.MenuItem menuItem37;
		private System.Windows.Forms.MenuItem menuItem38;
		private System.Windows.Forms.MenuItem menuItem39;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.RichTextBox netWorkText;
		private System.Windows.Forms.TabControl tabControl1;
		private Icon icon;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			icon = new Icon( "App.ico" );
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.Icon = icon;

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

			nNumberOfItemsInAdalineOneTrainingFile = nNumberOfItemsInAdalineTrainingFile;
			dLearningRateOfAdalineOne = 0.45;
			dLearningRateOfAdalineTwo = 0.223333;

			debugLevel = new DebugLevel( DebugLevelSet.Errors );

		}

		/// <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.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.FileItem = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem21 = new System.Windows.Forms.MenuItem();
			this.menuItem22 = new System.Windows.Forms.MenuItem();
			this.menuItem29 = new System.Windows.Forms.MenuItem();
			this.menuItem30 = new System.Windows.Forms.MenuItem();
			this.menuItem31 = new System.Windows.Forms.MenuItem();
			this.menuItem32 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem19 = new System.Windows.Forms.MenuItem();
			this.menuItem20 = new System.Windows.Forms.MenuItem();
			this.menuItem24 = new System.Windows.Forms.MenuItem();
			this.menuItem33 = new System.Windows.Forms.MenuItem();
			this.menuItem34 = new System.Windows.Forms.MenuItem();
			this.menuItem38 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.menuItem11 = new System.Windows.Forms.MenuItem();
			this.menuItem18 = new System.Windows.Forms.MenuItem();
			this.menuItem12 = new System.Windows.Forms.MenuItem();
			this.menuItem23 = new System.Windows.Forms.MenuItem();
			this.menuItem27 = new System.Windows.Forms.MenuItem();
			this.menuItem28 = new System.Windows.Forms.MenuItem();
			this.menuItem37 = new System.Windows.Forms.MenuItem();
			this.menuItem13 = new System.Windows.Forms.MenuItem();
			this.menuItem14 = new System.Windows.Forms.MenuItem();
			this.menuItem15 = new System.Windows.Forms.MenuItem();
			this.menuItem16 = new System.Windows.Forms.MenuItem();
			this.menuItem17 = new System.Windows.Forms.MenuItem();
			this.menuItem26 = new System.Windows.Forms.MenuItem();
			this.menuItem35 = new System.Windows.Forms.MenuItem();
			this.menuItem36 = new System.Windows.Forms.MenuItem();
			this.menuItem39 = new System.Windows.Forms.MenuItem();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.netWorkText = new System.Windows.Forms.RichTextBox();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1.SuspendLayout();
			this.tabControl1.SuspendLayout();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.FileItem,
																					  this.menuItem2,
																					  this.menuItem4,
																					  this.menuItem9,
																					  this.menuItem13});
			// 
			// FileItem
			// 
			this.FileItem.Index = 0;
			this.FileItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItem7,
																					 this.menuItem8});
			this.FileItem.Text = "&File";
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 0;
			this.menuItem7.Text = "Stop";
			this.menuItem7.Click += new System.EventHandler(this.OnStop);
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 1;
			this.menuItem8.Text = "Exit";
			this.menuItem8.Click += new System.EventHandler(this.OnExit);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem3,
																					  this.menuItem1,
																					  this.menuItem21,
																					  this.menuItem22,
																					  this.menuItem29,
																					  this.menuItem30,
																					  this.menuItem31,
																					  this.menuItem32});
			this.menuItem2.Text = "Generate";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Text = "Generate Adaline One Training File";
			this.menuItem3.Click += new System.EventHandler(this.OnGenerateAdaline);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.Text = "Generate Adaline Two Working File";
			this.menuItem1.Click += new System.EventHandler(this.OnGenerateAdalineWord);
			// 
			// menuItem21
			// 
			this.menuItem21.Index = 2;
			this.menuItem21.Text = "-";
			// 
			// menuItem22
			// 
			this.menuItem22.Index = 3;
			this.menuItem22.Text = "Generate Backpropagation Working File";
			this.menuItem22.Click += new System.EventHandler(this.OnGenerateBackPropagationWorkingFile);
			// 
			// menuItem29
			// 
			this.menuItem29.Index = 4;
			this.menuItem29.Text = "-";
			// 
			// menuItem30
			// 
			this.menuItem30.Index = 5;
			this.menuItem30.Text = "Generate Self Organizing One Training File";
			this.menuItem30.Click += new System.EventHandler(this.OnGenerateSelfOrganizingTrainingFile);
			// 
			// menuItem31
			// 
			this.menuItem31.Index = 6;
			this.menuItem31.Text = "Generate Self Organizing One Working File";
			this.menuItem31.Click += new System.EventHandler(this.OnGenerateSelfOrganizingNetworkWorkingFile);
			// 
			// menuItem32
			// 
			this.menuItem32.Index = 7;
			this.menuItem32.Text = "Generate Self Organizing One Test File";
			this.menuItem32.Click += new System.EventHandler(this.OnGenerateSelfOrganizingTestFile);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 2;
			this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem5,
																					  this.menuItem6,
																					  this.menuItem19,
																					  this.menuItem20,
																					  this.menuItem24,
																					  this.menuItem33,
																					  this.menuItem34,
																					  this.menuItem38});
			this.menuItem4.Text = "Run";
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 0;
			this.menuItem5.Text = "Load and Run Adaline 1";
			this.menuItem5.Click += new System.EventHandler(this.OnLoadAndRunAdalineOne);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 1;
			this.menuItem6.Text = "Load and Run Adaline 2";
			this.menuItem6.Click += new System.EventHandler(this.OnLoadAndRunAdalineWord);
			// 
			// menuItem19
			// 
			this.menuItem19.Index = 2;
			this.menuItem19.Text = "-";
			// 
			// menuItem20
			// 
			this.menuItem20.Index = 3;
			this.menuItem20.Text = "Load And Run BackPropagation 1";
			this.menuItem20.Click += new System.EventHandler(this.OnLoadAdnRunBackpropagationOne);
			// 
			// menuItem24
			// 
			this.menuItem24.Index = 4;
			this.menuItem24.Text = "Load And Run BackPropagation 2";
			this.menuItem24.Click += new System.EventHandler(this.OnRunBackPropagtion2);
			// 
			// menuItem33
			// 
			this.menuItem33.Index = 5;
			this.menuItem33.Text = "-";
			// 
			// menuItem34
			// 
			this.menuItem34.Index = 6;
			this.menuItem34.Text = "Load And Run Self Organizing Network 1";
			this.menuItem34.Click += new System.EventHandler(this.OnLoadAndRunSelfOrganizingNetworkOneMenu);
			// 
			// menuItem38
			// 
			this.menuItem38.Index = 7;
			this.menuItem38.Text = "Load And Run Self Organizing Network 2";
			this.menuItem38.Click += new System.EventHandler(this.OnLoadAndRunSelfOrgainzingNetworktwoClick);
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 3;
			this.menuItem9.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem10,
																					  this.menuItem11,
																					  this.menuItem18,
																					  this.menuItem12,
																					  this.menuItem23,
																					  this.menuItem27,
																					  this.menuItem28,
																					  this.menuItem37});
			this.menuItem9.Text = "Train";
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 0;
			this.menuItem10.Text = "Adaline 1";
			this.menuItem10.Click += new System.EventHandler(this.AdalineOneClick);
			// 
			// menuItem11
			// 
			this.menuItem11.Index = 1;
			this.menuItem11.Text = "Adaline 2";
			this.menuItem11.Click += new System.EventHandler(this.OnTrainAdaline2);
			// 
			// menuItem18
			// 
			this.menuItem18.Index = 2;
			this.menuItem18.Text = "-";
			// 
			// menuItem12
			// 
			this.menuItem12.Index = 3;
			this.menuItem12.Text = "BackPropagtion 1";
			this.menuItem12.Click += new System.EventHandler(this.OnBackPropagation1);
			// 
			// menuItem23
			// 
			this.menuItem23.Index = 4;
			this.menuItem23.Text = "BackPropagation 2";
			this.menuItem23.Click += new System.EventHandler(this.OnBackPropagation2Click);
			// 
			// menuItem27
			// 
			this.menuItem27.Index = 5;
			this.menuItem27.Text = "-";
			// 
			// menuItem28
			// 
			this.menuItem28.Index = 6;
			this.menuItem28.Text = "Self Organizing Network 1";
			this.menuItem28.Click += new System.EventHandler(this.OnSelfOrganisingNetwork1);
			// 
			// menuItem37
			// 
			this.menuItem37.Index = 7;
			this.menuItem37.Text = "Self Organizing Network 2";
			this.menuItem37.Click += new System.EventHandler(this.OnSelfOrganizingNetworkTwoClick);
			// 
			// menuItem13
			// 
			this.menuItem13.Index = 4;
			this.menuItem13.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.menuItem14,
																					   this.menuItem15,
																					   this.menuItem16,
																					   this.menuItem17,
																					   this.menuItem26,
																					   this.menuItem35,
																					   this.menuItem36,
																					   this.menuItem39});
			this.menuItem13.Text = "Options";
			// 
			// menuItem14
			// 
			this.menuItem14.Index = 0;
			this.menuItem14.Text = "Adaline 1 Options";
			this.menuItem14.Click += new System.EventHandler(this.OnAdalineOneOptions);
			// 
			// menuItem15
			// 
			this.menuItem15.Index = 1;
			this.menuItem15.Text = "Adaline 2 Options";
			this.menuItem15.Click += new System.EventHandler(this.OnAdalineTwoOptions);
			// 
			// menuItem16
			// 
			this.menuItem16.Index = 2;
			this.menuItem16.Text = "-";
			// 
			// menuItem17
			// 
			this.menuItem17.Index = 3;
			this.menuItem17.Text = "BackPropagation 1 Options";
			this.menuItem17.Click += new System.EventHandler(this.OnBackPoropagationOneOptions);
			// 
			// menuItem26
			// 
			this.menuItem26.Index = 4;
			this.menuItem26.Text = "BackPropagation 2 Options";
			this.menuItem26.Click += new System.EventHandler(this.OnBackPropagationTwoOptions);
			// 
			// menuItem35
			// 
			this.menuItem35.Index = 5;
			this.menuItem35.Text = "-";
			// 
			// menuItem36
			// 
			this.menuItem36.Index = 6;
			this.menuItem36.Text = "Self Organizing Network 1 Options";
			this.menuItem36.Click += new System.EventHandler(this.OnSelfOrganizingNetworkOneOptions);
			// 
			// menuItem39
			// 
			this.menuItem39.Index = 7;
			this.menuItem39.Text = "Self Orgainizing Network 2 Options";
			this.menuItem39.Click += new System.EventHandler(this.OnSelfOrganizingNetworkTwoOptions);
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.netWorkText});
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(624, 390);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Net Work Output";
			// 
			// netWorkText
			// 
			this.netWorkText.Dock = System.Windows.Forms.DockStyle.Fill;
			this.netWorkText.Name = "netWorkText";
			this.netWorkText.Size = new System.Drawing.Size(624, 390);
			this.netWorkText.TabIndex = 0;
			this.netWorkText.Text = "";
			// 
			// tabControl1
			// 
			this.tabControl1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tabPage1});
			this.tabControl1.ItemSize = new System.Drawing.Size(93, 18);
			this.tabControl1.Location = new System.Drawing.Point(0, 24);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(632, 416);
			this.tabControl1.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(632, 441);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tabControl1});
			
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Neural Net Tester";
			this.tabPage1.ResumeLayout(false);
			this.tabControl1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

				/// catch exceptions thrown by explorer pages if not shown
			catch( System.NullReferenceException )
			{
			}
		}

		/// <summary>
		/// early test code
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnTestXMLStuff(object sender, System.EventArgs e)
		{
			/*		log.Log( DebugLevelSet.Note, "This is a note", ClassName );
					log.Log( DebugLevelSet.Important, "This is important", ClassName );
					log.Log( DebugLevelSet.HighLight, "This is a highlight", ClassName );
			*/	
		}


		/*****************************************************************************************************
		 *							ADALINE ONE STUFF
		 */
 
		/// <summary>
		/// start the adaline one thread
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AdalineOneClick(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( DoAdalineOne );
			thread = new Thread( threadStart );

			thread.Start();
		}


		/// <summary>
		/// load the adaline training file
		/// </summary>
		/// <returns></returns>
		private ArrayList LoadAdalineTrainingFile()
		{
			ArrayList patterns = new ArrayList();
			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<nNumberOfItemsInAdalineOneTrainingFile; i++ )
			{
				AdalinePattern aPattern = new AdalinePattern( log, 2, 1 );
				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, Double.Parse( strNumber.ToString() ) );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				aPattern.SetInValue( 1, 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 );

				netWorkText.AppendText( aPattern.Data() + "\n" );
				patterns.Add( aPattern );
			}

			stream.Close();

			return patterns;
		}


		/// <summary>
		/// run the adaline 1 program
		/// </summary>
		private void DoAdalineOne()
		{
			/// delete the old log first
			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Neural Network Tester.xml", "NeuralNetworkTester", true );
			
			ArrayList patterns = LoadAdalineTrainingFile();

			/// create the adaline network
			AdalineNeuron neuron = new AdalineNeuron( log, new BasicNode( log ), new BasicNode( log ), new AdalineNode( log, dLearningRateOfAdalineOne ) );
			
			/// train the adaline network
			int nIteration = 0;
			int nGood = 0;

			while( nGood < nNumberOfItemsInAdalineOneTrainingFile )
			{
				nGood = 0;
				for( int i=0; i<nNumberOfItemsInAdalineTrainingFile; i++ )
				{
					neuron.InputNodeOne.SetValue( Values.NodeValue, ( double )( ( Pattern )patterns[ i ] ).InSet[ 0 ] );
					neuron.InputNodeTwo.SetValue( Values.NodeValue, ( double )( ( Pattern )patterns[ i ] ).InSet[ 1 ] );

					if( bAdalineOneUseBias == false )
					{
						neuron.Node.Run( Values.NodeValue );
					}
					else
					{
						neuron.Node.Run( Values.NodeValue, neuron.Node.GetBias.GetOriginalBias() );
					}

					/// if the output value generated by run is not the same as the output value
					/// in the training file then it is an error
					if( ( ( Pattern )patterns[ i ] ).OutputValue( 0 ) != neuron.Node.GetValue( Values.NodeValue ) )
					{
						/// run the learn function
						log.Log( DebugLevelSet.Errors, "Learn called at number " + i.ToString() + " Pattern value = " +  ( ( Pattern )patterns[ i ] ).OutputValue( 0 ).ToString() + " Neuron value = " + neuron.Node.GetValue( Values.NodeValue ), "Form1" );
						netWorkText.AppendText( "Learn called at number " + i.ToString() + " Pattern value = " +  ( ( Pattern )patterns[ i ] ).OutputValue( 0 ).ToString() + " Neuron value = " + neuron.Node.GetValue( Values.NodeValue ) + "\n" );
						neuron.Node.Learn();
						break;
					}
					else
						nGood++;
				}

				log.Log( DebugLevelSet.Progress, "Iteration number " + nIteration.ToString() + " produced " + nGood.ToString() + " Good values out of 250 ", "Form1" );
				netWorkText.AppendText( "Iteration number " + nIteration.ToString() + " produced " + nGood.ToString() + " Good values out of 250 \n" );
				nIteration++;
			}


			/// Default xml serialization wont work here.
			/// I can't decide if this is because the classes are in a seperate dll or because they are returning
			/// arraylists. an I'm not in the mood to rewrite it just to find out it's because of something else
			/// so I'll write the xml serialisation for each class the old fashioned way.
	
			/* The following will work though so I'll look into it at a later date.  
			TextWriter textWriter = new StreamWriter( "Network.xml" );
			XmlSerializer xmlSerial = new XmlSerializer( typeof( TestSerializer ) );
			TestSerializer test = new TestSerializer();
			xmlSerial.Serialize( textWriter,  );
			*/


			FileStream xmlstream = new FileStream( "adalinenetworkone.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8, true );
			XmlWriter xmlWriter = new XmlTextWriter( xmlstream, System.Text.Encoding.UTF8 );
			xmlWriter.WriteStartDocument();

			neuron.Save( xmlWriter );

			xmlWriter.WriteEndDocument();
			xmlWriter.Close();


			/// now load the file
			FileStream readStream = new FileStream( "adalinenetworkone.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );
			
			/// create the adaline network
			AdalineNeuron neuron2 = new AdalineNeuron( log, new BasicNode( log ), new BasicNode( log ), new AdalineNode( log, dLearningRateOfAdalineOne ) );
			neuron2.Load( xmlReader );

			xmlReader.Close();


			/// rerun the adaline should get 250 out of 250
			
			/// could use the pattern count here but I'll be slack
			for( int i=0; i<nNumberOfItemsInAdalineOneTrainingFile; i++ )
			{
				neuron2.InputNodeOne.SetValue( Values.NodeValue, ( double )( ( Pattern )patterns[ i ] ).InSet[ 0 ] );
				neuron2.InputNodeTwo.SetValue( Values.NodeValue, ( double )( ( Pattern )patterns[ i ] ).InSet[ 1 ] );

				if( bAdalineOneUseBias == false )
				{
					neuron2.Node.Run( Values.NodeValue );
				}
				else
				{
					neuron2.Node.Run( Values.NodeValue, neuron2.Node.GetBias.GetOriginalBias() );
				}
				
				netWorkText.AppendText( "Pattern " + i.ToString() + " Input = ( " + ( ( Pattern )patterns[ i ] ).InSet[ 0 ].ToString() + "," + ( ( Pattern )patterns[ i ] ).InSet[ 1 ].ToString() +
					" ) Adaline = " + neuron2.Node.GetValue( Values.NodeValue ) + " Actual = "  + ( ( Pattern )patterns[ i ] ).OutputValue( Values.NodeValue ) + "\n" );
 

			}


			log.Close();
			patterns.Clear();

		}

		/// <summary>
		/// Generate the training file for the adaline one network
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnGenerateAdaline(object sender, System.EventArgs e)
		{
			netWorkText.AppendText( "Generating Adaline File... Please Wait\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<nNumberOfItemsInAdalineOneTrainingFile; i++ )
			{
				x = Values.Random( -1, 1 );
				y = Values.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;
			netWorkText.AppendText( "Adaline File Generated\n" );
		}


		/// <summary>
		/// stop the current running thread
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnStop(object sender, System.EventArgs e)
		{
			log.Close();
			thread.Suspend();
		}

		/// <summary>
		/// exit the application
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnExit(object sender, System.EventArgs e)
		{
			if( thread != null && thread.IsAlive == true )
				thread.Suspend();

			Application.Exit();
		}

		/// <summary>
		/// start the adaline one run code
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnLoadAndRunAdalineOne(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( LoadAndRunAdaline );
			thread = new Thread( threadStart );

			thread.Start();
		}

		/// <summary>
		/// Load a saved adaline network and run the code
		/// </summary>
		private void LoadAndRunAdaline()
		{
			log = new Logger( "Neural Network Tester Load And run Adaline One.xml", "NeuralNetworkTester", true );

			Cursor.Current = Cursors.WaitCursor;

			OnGenerateAdaline( this, EventArgs.Empty );
			
			Cursor.Current = Cursors.Default;

			FileStream readStream = new FileStream( "adalinenetworkone.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );

			ArrayList patterns = LoadAdalineTrainingFile();

			/// create the adaline network
			AdalineNeuron neuron = new AdalineNeuron( log, new BasicNode( log ), new BasicNode( log ), new AdalineNode( log, 0.45 ) );
			neuron.Load( xmlReader );
			
			/// could use the pattern count here but I'll be slack
			for( int i=0; i<nNumberOfItemsInAdalineTrainingFile; i++ )
			{
				neuron.InputNodeOne.SetValue( Values.NodeValue, ( double )( ( Pattern )patterns[ i ] ).InSet[ 0 ] );
				neuron.InputNodeTwo.SetValue( Values.NodeValue, ( double )( ( Pattern )patterns[ i ] ).InSet[ 1 ] );

				if( bAdalineOneUseBias == false )
				{
					neuron.Node.Run( Values.NodeValue );
				}
				else
				{
					neuron.Node.Run( Values.NodeValue, neuron.Node.GetBias.GetOriginalBias() );
				}
					
				netWorkText.AppendText( "Pattern " + i.ToString() + " Input = ( " + ( ( Pattern )patterns[ i ] ).InSet[ 0 ].ToString() + "," + ( ( Pattern )patterns[ i ] ).InSet[ 1 ].ToString() +
					" ) Adaline = " + neuron.Node.GetValue( Values.NodeValue ) + " Actual = "  + ( ( Pattern )patterns[ i ] ).OutputValue( Values.NodeValue ) + "\n" );
 

			}

			log.Close();
			patterns.Clear();

		}

		/// <summary>
		/// start the training adaline 2 thread
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnTrainAdaline2(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( OnDoAdaline2 );
			thread = new Thread( threadStart );

			thread.Start();
		}

		/// <summary>
		/// load the adaline 2 training file
		/// </summary>
		/// <returns></returns>
		private ArrayList LoadAdaline2TrainingFile()
		{
			ArrayList patterns = new ArrayList();
			StreamReader stream = new StreamReader( "adword.trn" );
			StringBuilder strString = new StringBuilder( "" );
			StringBuilder strNumber = new StringBuilder( "" );

			/// load the data from the training file
			for( int i=0; i<nNumberOfItemsInAdalineWordFile; i++ )
			{
				AdalineWordPattern aPattern = new AdalineWordPattern( log, 2, 1 );
				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, strNumber.ToString() );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				aPattern.SetInValue( 1, 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 );

				netWorkText.AppendText( aPattern.Data() + "\n" );
				patterns.Add( aPattern );
			}

			stream.Close();

			return patterns;
		}


		/// <summary>
		/// load the adaline 2 working file ( for the the run functionality )
		/// </summary>
		/// <returns></returns>
		private ArrayList LoadAdaline2WorkingFile()
		{
			ArrayList patterns = new ArrayList();
			StreamReader stream = new StreamReader( "adalinewordWorkingFile.wrk" );
			StringBuilder strString = new StringBuilder( "" );
			StringBuilder strNumber = new StringBuilder( "" );

			/// load the data from the training file
			for( int i=0; i<nNumberOfWordTestsToGenerate; i++ )
			{
				AdalineWordPattern aPattern = new AdalineWordPattern( log, 2, 1 );
				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, strNumber.ToString() );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				aPattern.SetInValue( 1, 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 );

				netWorkText.AppendText( aPattern.Data() + "\n" );
				patterns.Add( aPattern );
			}

			stream.Close();

			return patterns;
		}



		/// <summary>
		/// Run the adaline 2 code
		/// </summary>
		private void OnDoAdaline2()
		{
			/// delete the old log first
			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Neural Network Tester.xml", "NeuralNetworkTester", true );
			
			ArrayList patterns = LoadAdaline2TrainingFile();

			/// create the adaline network
			AdalineWordNeuron neuron = new AdalineWordNeuron( log, new BasicNode( log ), new BasicNode( log ), new AdalineWordNode( log, dLearningRateOfAdalineTwo ) );
			
			/// train the adaline network
			int nIteration = 0;
			int nGood = 0;

			while( nGood < nNumberOfItemsInAdalineWordFile )
			{
				nGood = 0;
				for( int i=0; i<nNumberOfItemsInAdalineWordFile; i++ )
				{
					netWorkText.AppendText( "Setting the Node Data to, Pattern " + i.ToString() + " word 1 = " + ( ( AdalineWordPattern )patterns[ i ] ).InputValue( 0 ) +
						" value = " + ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 0 ).ToString() + " word 2 = " + ( ( AdalineWordPattern )patterns[ i ] ).InputValue( 1 ) +
						" value = " + ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 1 ).ToString() + " output value = " + ( ( AdalineWordPattern )patterns[ i ] ).OutSet[ 0 ].ToString() + "\n" );
					neuron.InputNodeOne.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 0 ) );
					neuron.InputNodeTwo.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 1 ) );

					if( bAdalineTwoUseBias == false )
					{
						neuron.Node.Run( Values.NodeValue );
					}
					else
					{
						neuron.Node.Run( Values.NodeValue, neuron.Node.GetBias.GetOriginalBias() );
					}

					/// if the output value generated by run is not the same as the output value
					/// in the training file then it is an error
					if( ( ( Pattern )patterns[ i ] ).OutputValue( 0 ) != neuron.Node.GetValue( Values.NodeValue ) )
					{
						/// run the learn function
						log.Log( DebugLevelSet.Errors, "Learn called at number " + i.ToString() + " Pattern value = " +  ( ( Pattern )patterns[ i ] ).OutputValue( 0 ).ToString() + " Neuron value = " + neuron.Node.GetValue( Values.NodeValue ), "Form1" );
						netWorkText.AppendText( "Learn called at number " + i.ToString() + " Pattern value = " +  ( ( Pattern )patterns[ i ] ).OutputValue( 0 ).ToString() + " Neuron value = " + neuron.Node.GetValue( Values.NodeValue ) + "\n" );
						neuron.Node.Learn();
						break;
					}
					else
						nGood++;
				}

				log.Log( DebugLevelSet.Progress, "Iteration number " + nIteration.ToString() + " produced " + nGood.ToString() + " Good values out of " + nNumberOfItemsInAdalineWordFile.ToString(), "Form1" );
				netWorkText.AppendText( "Iteration number " + nIteration.ToString() + " produced " + nGood.ToString() + " Good values out of " + nNumberOfItemsInAdalineWordFile.ToString() + "\n" );
				nIteration++;
			}

			/// run the training sample
			for( int i=0; i<nNumberOfItemsInAdalineWordFile; i++ )
			{
				neuron.InputNodeOne.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 0 ) );
				neuron.InputNodeTwo.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 1 ) );

				if( bAdalineTwoUseBias == false )
				{
					neuron.Node.Run( Values.NodeValue );
				}
				else
				{
					neuron.Node.Run( Values.NodeValue, neuron.Node.GetBias.GetOriginalBias() );
				}
				
				netWorkText.AppendText( "Pattern " + i.ToString() + " Input = ( " + ( string )( ( AdalineWordPattern )patterns[ i ] ).InSet[ 0 ]  + "," + ( string )( ( AdalineWordPattern )patterns[ i ] ).InSet[ 1 ] +
					" ) Adaline = " + neuron.Node.GetValue( Values.NodeValue ) + " Actual = "  + ( ( AdalineWordPattern )patterns[ i ] ).OutSet[ 0 ].ToString() + "\n" );
 

			}
		


			FileStream xmlstream = new FileStream( "adalinewordnetwork.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8, true );
			XmlWriter xmlWriter = new XmlTextWriter( xmlstream, System.Text.Encoding.UTF8 );
			xmlWriter.WriteStartDocument();

			neuron.Save( xmlWriter );

			xmlWriter.WriteEndDocument();
			xmlWriter.Close();


			/// now load the file
			FileStream readStream = new FileStream( "adalinewordnetwork.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );
			
			/// create the adaline network
			AdalineWordNeuron neuron2 = new AdalineWordNeuron( log, new BasicNode( log ), new BasicNode( log ), new AdalineWordNode( log ) );
			neuron2.Load( xmlReader );

			xmlReader.Close();


			
			for( int i=0; i<nNumberOfItemsInAdalineWordFile; i++ )
			{
				neuron2.InputNodeOne.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 0 ) );
				neuron2.InputNodeTwo.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 1 ) );

				if( bAdalineTwoUseBias == false )
				{
					neuron2.Node.Run( Values.NodeValue );
				}
				else
				{
					neuron2.Node.Run( Values.NodeValue, neuron2.Node.GetBias.GetOriginalBias() );
				}
				
				netWorkText.AppendText( "Pattern " + i.ToString() + " Input = ( " + ( string )( ( AdalineWordPattern )patterns[ i ] ).InSet[ 0 ]  + "," + ( string )( ( AdalineWordPattern )patterns[ i ] ).InSet[ 1 ] +
					" ) Adaline = " + neuron2.Node.GetValue( Values.NodeValue ) + " Actual = "  + ( ( AdalineWordPattern )patterns[ i ] ).OutSet[ 0 ].ToString() + "\n" );
 

			}


			log.Close();	
			patterns.Clear();

		}

		/// <summary>
		/// generate an adaline word file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnGenerateAdalineWord(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( GenerateAdalineWord );
			thread = new Thread( threadStart );

			thread.Start();
		}

		/// <summary>
		/// Generate the working file for the adaline word network
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void GenerateAdalineWord()
		{
			netWorkText.AppendText( "Generating Adaline Word File... Please Wait\n" );
			Cursor.Current = Cursors.WaitCursor;

			ArrayList arrayWords = new ArrayList();

			FileInfo info = new FileInfo( "adalinewordWorkingFile.wrk" );
			if( info.Exists == true )
				info.Delete();

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

			FileInfo words = new FileInfo( "AdalineWordFile.dat" );
			if( words.Exists == false )
			{
				MessageBox.Show( "Error something has gone horribly wrong and the word file no longer exists" );
				return;
			}

			FileStream readerStream = new FileStream( words.FullName, FileMode.Open, FileAccess.Read );
			StreamReader reader = new StreamReader( readerStream );

			while( reader.Peek() > -1 )
			{
				arrayWords.Add( reader.ReadLine().TrimEnd( ' ', '\n', '\r' ) );
			}

			int nNumber = 0;
			double dTemp1Value = 0;
			double dTemp2Value = 0;
			string strTemp1;
			string strTemp2;
			int nDecisionValue = 0;
			for( int i=0; i<nNumberOfWordTestsToGenerate; i++ )
			{
				nNumber = ( int )Values.Random( 0, arrayWords.Count );
				strTemp1 = arrayWords[ nNumber ].ToString();
				nNumber = ( int )Values.Random( 0, arrayWords.Count );
				strTemp2 = arrayWords[ nNumber ].ToString();

				for( int n=0; n<strTemp1.Length; n++ )
				{
					dTemp1Value += strTemp1[ n ] *( n+1 );
				}

				for( int n=0; n<strTemp2.Length; n++ )
				{
					dTemp2Value += strTemp2[ n ] *( n+1 );
				}

				dTemp1Value = dTemp1Value / 10000;
				dTemp2Value = dTemp2Value / 10000;

				if( dTemp1Value > dTemp2Value )
					nDecisionValue = -1;
				else
					nDecisionValue = 1;

				dTemp1Value = 0;
				dTemp2Value = 0;

				writer.WriteLine( i.ToString() + " " + strTemp1 + " " + strTemp2 + " " + nDecisionValue.ToString() );

			}

			reader.Close();
			writer.Close();

			Cursor.Current = Cursors.Default;
			netWorkText.AppendText( "Adaline File Generated\n" );
		}

		/// <summary>
		/// start the thread for loading and running the adaline 2 code
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnLoadAndRunAdalineWord(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( LoadAndRunAdalineWord );
			thread = new Thread( threadStart );

			thread.Start();
		}

		/// <summary>
		/// run the adaline 2 code
		/// </summary>
		private void LoadAndRunAdalineWord()
		{
			log = new Logger( "Neural Network Tester Load And run Adaline Word Network.xml", "NeuralNetworkTester", true );

			Cursor.Current = Cursors.WaitCursor;

			GenerateAdalineWord();
			
			Cursor.Current = Cursors.Default;

			FileStream readStream = new FileStream( "adalinewordnetwork.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );

			ArrayList patterns = LoadAdaline2WorkingFile();

			/// create the adaline network
			AdalineWordNeuron neuron = new AdalineWordNeuron( log, new BasicNode( log ), new BasicNode( log ), new AdalineWordNode( log ) );
			neuron.Load( xmlReader );
			
			/// could use the pattern count here but I'll be slack
			for( int i=0; i<nNumberOfWordTestsToGenerate; i++ )
			{
				neuron.InputNodeOne.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 0 )  );
				neuron.InputNodeTwo.SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( 0 )  );

				if( bAdalineTwoUseBias == false )
				{
					neuron.Node.Run( Values.NodeValue );
				}
				else
				{
					neuron.Node.Run( Values.NodeValue, neuron.Node.GetBias.GetOriginalBias() );
				}
				
				netWorkText.AppendText( "Pattern " + i.ToString() + " Input = ( " + ( ( AdalineWordPattern )patterns[ i ] ).InputValue( 0 ) + "," + ( ( AdalineWordPattern )patterns[ i ] ).InputValue( 1 ) +
					" ) Adaline = " + neuron.Node.GetValue( Values.NodeValue ) + " Actual = "  + ( ( AdalineWordPattern )patterns[ i ] ).OutputValue( Values.NodeValue ) + "\n" );
 

			}

			log.Close();
			patterns.Clear();

		}


		/*******************************************************************************************************
		 *					BACKPROPAGATION ONE STUFF
		 */

		/// <summary>
		/// start the thread to run bp 1
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnBackPropagation1(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( OnBackPropagationOne );
			thread = new Thread( threadStart );

			thread.Start();
		
		}


		/// <summary>
		/// Train and run the first backpropagation example
		/// </summary>
		private void OnBackPropagationOne()
		{
			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Neural Network Tester.xml", "NeuralNetworkTester", true );

			/// create a trainign set to learn the xor problem
			ArrayList patterns = new ArrayList();

			patterns.Add( new Pattern( log, 0.0, 0.0, 0.0 ) );
			patterns.Add( new Pattern( log, 0.0, 1.0, 1.0 ) );
			patterns.Add( new Pattern( log, 1.0, 0.0, 1.0 ) );
			patterns.Add( new Pattern( log, 1.0, 1.0, 0.0 ) );

			/// create a backpropagation network with a learning rate of 0.45
			/// a momentum of 0.9 with 2 nodes in the input layer, 3 nodes in the middle layer
			/// and one output node.
			BackPropagationNetwork bpNetwork = new BackPropagationNetwork( log, dBackPropagationLearningRate, dBackPropagationMomentum, 2, 2, 1 ); 
			bpNetwork.UseBias = bBackPropagationOneUseBias;
			int nGood = 0;

			double dTotalError = 0.0;
			double dTest = 0.0;

			while( nGood < 4 )
			{
				nGood = 0;
				dTotalError = 0.0;

				for( int i=0; i<4; i++ )
				{
					/// set the input values
					bpNetwork.SetValue( ( ( Pattern )patterns[ i ] ) );
					
					/// run the network
					bpNetwork.Run();

					/// set the desired output
					bpNetwork.SetOutputError( ( ( Pattern )patterns[ i ] ) );

					/// run learn anyway
					bpNetwork.Learn();

					dTest = ( Math.Abs( bpNetwork.GetOutputValue( 0 ) ) - ( ( Pattern )patterns[ i ] ).OutputValue( 0 ) );
					netWorkText.AppendText( "Network out put value = " + ( double )bpNetwork.GetOutputValue( 0 ) + " Pattern out put value = " +  ( ( Pattern )patterns[ i ] ).OutputValue( 0 ) + "\n" );
		
					netWorkText.AppendText( "Absolute output value = " + dTest.ToString() + "\n" );
					if( dTest  < dBackPropagationTolerance )
					{
						if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
						{
							log.Log( DebugLevelSet.Progress, "Test falls within tolerance levels", ClassName );
						}

						netWorkText.AppendText( "Test = " + dTest.ToString() + " Tolerance = " + dBackPropagationTolerance.ToString() + " Test falls within tolerance levels\n" );

						nGood++;
					}


					dTotalError += Math.Abs( bpNetwork.OutputError( 0 ) );

					if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
					{
						log.Log( DebugLevelSet.Progress, "Back Propagation training run " + i.ToString() + " completed, total error = " + dTotalError.ToString(), ClassName );
					}

					netWorkText.AppendText( "Pattern " + ( ( Pattern )patterns[ i ] ).InputValue( 0 ).ToString() + "," + ( ( Pattern )patterns[ i ] ).InputValue( 1 ).ToString() +
						" run through the network " + " output error at 0 = " + bpNetwork.OutputError( 0 ).ToString() + 
						" total error = " + dTotalError.ToString() + "\n" );
 
				}
			}


			netWorkText.AppendText( "Saving the BackPropagation Network\n" );
			FileStream xmlstream = new FileStream( "backpropagationnetworkone.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8, true );
			XmlWriter xmlWriter = new XmlTextWriter( xmlstream, System.Text.Encoding.UTF8 );
			xmlWriter.WriteStartDocument();

			bpNetwork.Save( xmlWriter );

			xmlWriter.WriteEndDocument();
			xmlWriter.Close();

			netWorkText.AppendText( "Loading the BackPropagation Network\n" );

			/// now load the file
			FileStream readStream = new FileStream( "backpropagationnetworkone.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );
			
			/// create a backpropagation network with a learning rate of 0.45
			/// a momentum of 0.9 with 2 nodes in the input layer, 3 nodes in the middle layer
			/// and one output node.
			BackPropagationNetwork bpNetwork2 = new BackPropagationNetwork( log, dBackPropagationLearningRate, dBackPropagationMomentum, 2, 2, 1 );
			bpNetwork2.Load( xmlReader );
			bpNetwork2.UseBias = bBackPropagationOneUseBias;

			xmlReader.Close();


			double dTemp = 0.0;
			for( int i=0; i<4; i++ )
			{
				bpNetwork2.SetValue( ( Pattern )patterns[ i ] );

				bpNetwork2.Run();
			
				/// first output value is output value 0
				dTemp = bpNetwork2.GetOutputValue( 0 );

				netWorkText.AppendText( " Pattern Values = " + ( ( Pattern )patterns[ i ] ).InputValue( 0 ).ToString() + ", " + ( ( Pattern )patterns[ i ] ).InputValue( 1 ).ToString() +
					" Pattern Output Value = " + ( ( Pattern )patterns[ i ] ).OutputValue( 0 ).ToString() + " Net work Output Value = " + bpNetwork2.GetCalculatedOutputValue( 0, ( ( Pattern )patterns[ i ] ).OutputValue( 0 ) ).ToString() + "\n" );

			}

			log.Close();
			patterns.Clear();

		}

		/// <summary>
		/// start the thread to generate a new file for the back propagtion network
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnGenerateBackPropagationWorkingFile(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( GenerateBackPropagationWorkingFile );
			thread = new Thread( threadStart );

			thread.Start();
		
		}

		/// <summary>
		/// generate a new working file for the back propagtion network 
		/// </summary>
		private void GenerateBackPropagationWorkingFile()
		{
			netWorkText.AppendText( "Generating Backpropagation working File... Please Wait\n" );
			Cursor.Current = Cursors.WaitCursor;

			FileInfo info = new FileInfo( "BackpropagationWorkingFile.wrk" );
			if( info.Exists == true )
				info.Delete();

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


			double dTemp1Value = 0;
			double dTemp2Value = 0;
			StringBuilder strTemp1 = new StringBuilder();
			StringBuilder strTemp2 = new StringBuilder();
			bool bOne = false;

			for( int i=0; i<nNumberOfBackPropagationTests; i++ )
			{
				bOne = false;
				dTemp1Value = Values.Random( 0, 1 );
				dTemp2Value = Values.Random( 0, 1 );

				strTemp1.Remove( 0, strTemp1.Length );
				strTemp2.Remove( 0, strTemp2.Length );

				if( dTemp1Value <= 0.5 )
					strTemp1.Append( "0" );
				else
				{
					strTemp1.Append( "1" );
					bOne = true;
				}
				
				if( dTemp2Value <= 0.5 )
					strTemp2.Append( "0" );
				else
				{
					strTemp2.Append( "1" );
					if( bOne == false )
						bOne = true;
					else
						bOne = false;
				}

				if( bOne == true )
					writer.WriteLine( i.ToString() + " " + strTemp1.ToString() + " " + strTemp2.ToString() + " 1" );
				else
					writer.WriteLine( i.ToString() + " " + strTemp1.ToString() + " " + strTemp2.ToString() + " 0" );
			}

			writer.Close();

			Cursor.Current = Cursors.Default;
			netWorkText.AppendText( "Backpropagation Working File Generated\n" );

		}

		/// <summary>
		/// start the thread for running the back propagation one code
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnLoadAdnRunBackpropagationOne(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( LoadAndRunBackpropagationOne );
			thread = new Thread( threadStart );

			thread.Start();
		
		}

		/// <summary>
		/// load a saved back propagattion network and run it
		/// </summary>
		private void LoadAndRunBackpropagationOne()
		{
			log = new Logger( "Neural Network Tester Load And Run Backpropagation One Network.xml", "NeuralNetworkTester", true );

			Cursor.Current = Cursors.WaitCursor;

			GenerateBackPropagationWorkingFile();
			
			Cursor.Current = Cursors.Default;

			FileStream readStream = new FileStream( "backpropagationnetworkone.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );

			ArrayList patterns = LoadBackpropagationWorkingFile();

			/// create a backpropagation network with a learning rate of 0.45
			/// a momentum of 0.9 with 2 nodes in the input layer, 3 nodes in the middle layer
			/// and one output node.
			BackPropagationNetwork bpNetwork2 = new BackPropagationNetwork( log, dBackPropagationLearningRate, dBackPropagationMomentum, 2, 2, 1 );
			bpNetwork2.Load( xmlReader );
			bpNetwork2.UseBias = bBackPropagationOneUseBias;

			xmlReader.Close();


			double dTemp = 0.0;
			double dCalculatedTemp = 0.0;
			StringBuilder strTemp = new StringBuilder();
			for( int i=0; i<nNumberOfBackPropagationTests; i++ )
			{
				bpNetwork2.SetValue( ( Pattern )patterns[ i ] );

				bpNetwork2.Run();
			
				/// first output value is output value 0
				dTemp = bpNetwork2.GetOutputValue( 0 );
				dCalculatedTemp = bpNetwork2.GetCalculatedOutputValue( 0, ( ( Pattern )patterns[ i ] ).OutputValue( 0 ) );
				strTemp.Remove( 0, strTemp.Length );
				if( dCalculatedTemp < dBackPropagationTolerance )
				{
					strTemp.Append( "Output value is within tolerance level of " + dBackPropagationTolerance.ToString() + " of 0 " );
				}
				else if( dCalculatedTemp > ( 1 - dBackPropagationTolerance ) )
				{
					strTemp.Append( "Output value is within tolerance level of " + dBackPropagationTolerance.ToString() + " of 1 " );
				}
				else
				{
					strTemp.Append( "The Network is officially retarded " );
				}


				netWorkText.AppendText( " Pattern Values = " + ( ( Pattern )patterns[ i ] ).InputValue( 0 ).ToString() + ", " + ( ( Pattern )patterns[ i ] ).InputValue( 1 ).ToString() +
					" Pattern Output Value = " + ( ( Pattern )patterns[ i ] ).OutputValue( 0 ).ToString() + " Net work Output Value = " + dCalculatedTemp + " " + strTemp.ToString() + "\n" );

			}

			log.Close();
			patterns.Clear();
		}

		/// <summary>
		/// load the working or run file for the backpropagation network
		/// </summary>
		/// <returns></returns>
		private ArrayList LoadBackpropagationWorkingFile()
		{
			ArrayList patterns = new ArrayList();

			StreamReader stream = new StreamReader( "BackpropagationWorkingFile.wrk" );
			StringBuilder strString = new StringBuilder( "" );
			StringBuilder strNumber = new StringBuilder( "" );

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

				Pattern pattern = new Pattern( log, 2, 1 );
			
				/// 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++ ] );
				pattern.SetInValue( 0, Double.Parse( strNumber.ToString() ) );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				pattern.SetInValue( 1, Double.Parse( strNumber.ToString() ) );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( n != strString.Length  && strString[ n ] != ' ' )
					strNumber.Append( strString[ n++ ] );
				pattern.SetOutValue( 0, Double.Parse( strNumber.ToString() ) );
				strNumber.Remove( 0, strNumber.Length );

				netWorkText.AppendText( "Setting pattern " + i.ToString() + " to, Input One =  " + pattern.InputValue( 0 ).ToString() + " Input Two = " + pattern.InputValue( 1 ).ToString() + " Output Value = " + pattern.OutputValue( 0 ).ToString()  + "\n" );
				patterns.Add( pattern );
			}

			stream.Close();

			return patterns;
		}



		/***********************************************************************************************************
		 *					OPTIONS STUFF
		 */

		/// <summary>
		/// Set the Adaline one options
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnAdalineOneOptions(object sender, System.EventArgs e)
		{
			AdalineOneOptions options = new AdalineOneOptions( nNumberOfItemsInAdalineOneTrainingFile, dLearningRateOfAdalineOne, bAdalineOneUseBias );
			if( options.ShowDialog( this ) == DialogResult.OK )
			{
				nNumberOfItemsInAdalineOneTrainingFile = options.NumberOfItems;
				dLearningRateOfAdalineOne = options.LearningRate;
				bAdalineOneUseBias = options.UseBias;
			}
		}

		/// <summary>
		/// set the adaline two options
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnAdalineTwoOptions(object sender, System.EventArgs e)
		{
			AdalineTwoOptions options = new AdalineTwoOptions( nNumberOfItemsInAdalineWordFile, dLearningRateOfAdalineTwo, bAdalineTwoUseBias );
			if( options.ShowDialog( this ) == DialogResult.OK )
			{
				nNumberOfItemsInAdalineWordFile = options.NumberOfWords;
				dLearningRateOfAdalineTwo = options.LearningRate;
				bAdalineTwoUseBias = options.UseBias;
			}
		
		}

		/// <summary>
		/// set the back propagation one options 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnBackPoropagationOneOptions(object sender, System.EventArgs e)
		{
			BackPropagationOneOptions options = new BackPropagationOneOptions( nNumberOfBackPropagationTests, dBackPropagationTolerance, dBackPropagationMomentum, dBackPropagationLearningRate, bBackPropagationOneUseBias );
			if( options.ShowDialog( this ) == DialogResult.OK )
			{
				nNumberOfBackPropagationTests = options.NumberOfTests;
				dBackPropagationTolerance = options.Tolerance;
				dBackPropagationLearningRate = options.LearningRate;
				dBackPropagationMomentum = options.Momentum;
				bBackPropagationOneUseBias = options.UseBias;

			}
		}

		/// <summary>
		/// set the back propagation two options
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnBackPropagationTwoOptions(object sender, System.EventArgs e)
		{		
			BackPropagationTwoOptions options = new BackPropagationTwoOptions( bBackPropagationTwoUseBias, dBackPropagationWordLearningRate, dBackPropagationWordMomentum, dBackPropagationWordTolerance, strBackPropagationWordOne, strBackPropagationWordTwo, strBackPropagationWordRunFile, strBackPropagationWordTrainingFile );
			if( options.ShowDialog( this ) == DialogResult.OK )
			{
				bBackPropagationTwoUseBias = options.UseBias;
				dBackPropagationWordLearningRate = options.LearningRate;
				dBackPropagationWordMomentum = options.Momentum;
				dBackPropagationWordTolerance = options.Tolerance;
				strBackPropagationWordOne = options.WordOne;
				strBackPropagationWordTwo = options.WordTwo;
				strBackPropagationWordRunFile = options.RunFile;
				strBackPropagationWordTrainingFile = options.TrainingFile;
			}		
		}

		/// <summary>
		/// Set the self organizing network one options
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnSelfOrganizingNetworkOneOptions(object sender, System.EventArgs e)
		{

			SelfOrganizingNetworkOneOptions options = new SelfOrganizingNetworkOneOptions( nNumberOfItemsInSelfOrganizingNetworkTrainingFile, dInitialSonOneLearningRate, dFinalSonOneLearningRate,  nInitialSonOneNeighborhoodSize, nNeighborhoodDecrementInterval, nNumberOfSonOneIterations );
			if( options.ShowDialog( this ) == DialogResult.OK )
			{
				nNumberOfItemsInSelfOrganizingNetworkTrainingFile = options.NumberOfItems;
				dInitialSonOneLearningRate = options.InitialLearningRate;
				dFinalSonOneLearningRate = options.FinalLearningRate;
				nInitialSonOneNeighborhoodSize = options.InitialNeighborhoodSize;
				nNeighborhoodDecrementInterval = options.NeighborhoodDecrementInterval;
				nNumberOfSonOneIterations = options.NumberOfIterations;
			}

		
		}

		private void OnSelfOrganizingNetworkTwoOptions(object sender, System.EventArgs e)
		{
			SelfOrganizingNetworkTwoOptions options = new SelfOrganizingNetworkTwoOptions( dInitialSonTwoLearningRate, dFinalSonTwoLearningRate, nInitialSonTwoNeighborhoodSize, nFinalSonTwoNeighborhoodSize, nSonTwoNeighborhoodDecrementInterval, nNumberOfSonTwoIterations );
			if( options.ShowDialog( this ) == DialogResult.OK )
			{
				dInitialSonTwoLearningRate = options.InitialLearningRate;
				dFinalSonTwoLearningRate = options.FinalLearningRate;
				nInitialSonTwoNeighborhoodSize = options.InitialNeighborhoodSize;
				nFinalSonTwoNeighborhoodSize = options.FinalNeighborhoodSize;
				nSonTwoNeighborhoodDecrementInterval = options.NeighborhoodDecrementInterval;
				nNumberOfSonTwoIterations = options.NumberOfIterations;

			}
		
		}

		public string ClassName
		{
			get
			{
				return this.Name;
			}
		}


		/******************************************************************************************************
		 *					BACK PROPAGATION 2 STUFF
		 */

		/// <summary>
		/// start the thread for training the back propagation two network
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnBackPropagation2Click(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( OnBackPropagation2Train );
			thread = new Thread( threadStart );

			thread.Start();		
		}
	
		/// <summary>
		/// load the back propagation two training file
		/// </summary>
		/// <returns></returns>
		private ArrayList LoadBackPropagationWordTrainingFile()
		{
			netWorkText.AppendText( "Loading Back Propagation Training File please wait ... \n" );
			ArrayList patterns = new ArrayList();

			StreamReader stream = new StreamReader( strBackPropagationWordTrainingFile );
			StringBuilder strString = new StringBuilder( "" );
			StringBuilder strWord = new StringBuilder( "" );
			int nCount = 0;
			int n = 0;
			BackPropagationWordPattern pattern = new BackPropagationWordPattern( log );
			StringBuilder strTemp = new StringBuilder( " " );
			bool bEndOfSentence = false;

			/// load the data from the training file
			while( stream.Peek() > -1  )
			{
				
				strString.Remove( 0, strString.Length );
				strString.Append( stream.ReadLine() );

				n=0;
				while( n != strString.Length )
				{
					bEndOfSentence = false;
					strWord.Remove( 0, strWord.Length );

					while( n != strString.Length && strString[ n ] == ' ' )
						n++;

					while( n != strString.Length && strString[ n ] != ' ' )
					{
						/// end of sentence
						if( strString[ n ] == '.'  )
						{
							pattern.SetInValue( nCount, strWord.ToString() ); 
							if( strWord.ToString().CompareTo( strBackPropagationWordOne ) == 0 )
								pattern.SetOutValue( nCount, 1.0 );
							else if( strWord.ToString().CompareTo( strBackPropagationWordTwo ) == 0 )
								pattern.SetOutValue( nCount, 1.0 );
							else
								pattern.SetOutValue( nCount, 0.0 );


							patterns.Add( pattern );
							
							nCount = 0;
							n++;

							strTemp.Remove( 0, strTemp.Length );

							/// get the full string and calculate the output value as we go
							/// can't use string search functions such as indexof as this 
							/// will find part words
							int nWordCountOne = 0;
							int nWordCountTwo = 0;
							for( int i=0; i<pattern.InputSize(); i++ )
							{
								strTemp.Append( pattern.InputValue( i ) + " " );
								if( pattern.InputValue( i ).CompareTo( strBackPropagationWordOne ) == 0 )
									nWordCountOne++;
								if( pattern.InputValue( i ).CompareTo( strBackPropagationWordTwo ) == 0 )
									nWordCountTwo++;
							}

							if( nWordCountOne == 1 && nWordCountTwo == 0 )
								pattern.SetOutValue( 0, 1.0 );
							else if( nWordCountTwo == 1 && nWordCountOne == 0 )
								pattern.SetOutValue( 0, 1.0 );
							else
								pattern.SetOutValue( 0, 0.0 );


							netWorkText.AppendText( "Setting pattern to, " + strTemp.ToString() + "\n" );



							pattern = new BackPropagationWordPattern( log );

							bEndOfSentence = true;

						}
						else /// save the word
						{
							strWord.Append( strString[ n++ ] );
						}
					}

					if( bEndOfSentence == false )
					{
						pattern.SetInValue( nCount, strWord.ToString() ); 
					}

					nCount++;
				}

			}

			stream.Close();

			return patterns;
		}


		/// <summary>
		/// train the back propagation two network
		/// </summary>
		private void OnBackPropagation2Train()
		{

			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Neural Network Tester.xml", "NeuralNetworkTester", true );
		
			ArrayList patterns = LoadBackPropagationWordTrainingFile();


			/// create a backpropagation network with a learning rate of 0.45
			/// a momentum of 0.9 with 2 nodes in the input layer, 3 nodes in the middle layer
			/// and one output node.
			BackPropagationNetwork bpNetwork = new BackPropagationNetwork( log, dBackPropagationWordLearningRate, dBackPropagationWordMomentum, 20, 20, 1 );
			bpNetwork.UseBias = bBackPropagationTwoUseBias;


			

			double dTemp = 0.0;
			double dCalculatedTemp = 0.0;
			StringBuilder strTemp = new StringBuilder( " " );
			StringBuilder strSentence = new StringBuilder( " " );
			int nGood = 0;
			int nIteration = 1;

			while( nGood < patterns.Count )
			{
				nGood = 0;
				
				for( int i=0; i<patterns.Count; i++ )
				{
					for( int n=0; n<20; n++ )
					{
						( ( BasicNode )bpNetwork.Nodes[ n ] ).SetValue( Values.NodeValue, ( ( BackPropagationWordPattern )patterns[ i ] ).GetInSetAt( n ) );
					}
	
					bpNetwork.Run();

					/// set the desired output
					bpNetwork.SetOutputError( ( ( BackPropagationWordPattern )patterns[ i ] ) );

					/// run learn anyway
					bpNetwork.Learn();
			
					/// first output value is output value 0
					dTemp = bpNetwork.GetOutputValue( 0 );
					dCalculatedTemp = bpNetwork.GetCalculatedOutputValue( 0, ( ( BackPropagationWordPattern )patterns[ i ] ).OutputValue( 0 ) );
					strTemp.Remove( 0, strTemp.Length );

					netWorkText.AppendText( "Absolute output value = " + dCalculatedTemp.ToString() + "\n" );
					if( dCalculatedTemp < dBackPropagationWordTolerance )
					{
						strTemp.Append( "Output value is within tolerance level of " + dBackPropagationWordTolerance.ToString() + " of 0 " );
						nGood++;
					}
					else if( dCalculatedTemp > ( 1 - dBackPropagationWordTolerance ) )
					{
						strTemp.Append( "Output value is within tolerance level of " + dBackPropagationWordTolerance.ToString() + " of 1 " );
						nGood++;
					}	
					else
					{
						strTemp.Append( "Output value is outside tolerance levels\n" );
					}

					strTemp.Append( " " );

					for( int n=0; n<( ( BackPropagationWordPattern )patterns[ i ] ).InputSize(); n++ )
					{
						strTemp.Append( ( ( BackPropagationWordPattern )patterns[ i ] ).InputValue( n ) + " " );
					}

					strTemp.Append( "\n Output Value = " + ( ( BackPropagationWordPattern )patterns[ i ] ).OutputValue( Values.NodeValue ) );
					

					netWorkText.AppendText( "Test = " + dCalculatedTemp.ToString() + " Tolerance = " + dBackPropagationWordTolerance.ToString() + " " + strTemp.ToString() + "\n" );

				}
				
				netWorkText.AppendText( "\nNetwork iteration " + nIteration.ToString() + " produced " + nGood.ToString() + " good results out of " + patterns.Count.ToString() + "\n" );

				nIteration++;
				
			}

			netWorkText.AppendText( "Saving the BackPropagation Network\n" );
			FileStream xmlstream = new FileStream( "backpropagationnetworktwo.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8, true );
			XmlWriter xmlWriter = new XmlTextWriter( xmlstream, System.Text.Encoding.UTF8 );
			xmlWriter.WriteStartDocument();

			bpNetwork.Save( xmlWriter );

			xmlWriter.WriteEndDocument();
			xmlWriter.Close();

			netWorkText.AppendText( "\nTraining Done - Reloading Network and Testing against the training set\n" );

			/// now load the file
			FileStream readStream = new FileStream( "backpropagationnetworktwo.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );
			
			/// create a backpropagation network with a learning rate of 0.45
			/// a momentum of 0.9 with 2 nodes in the input layer, 3 nodes in the middle layer
			/// and one output node.
			netWorkText.AppendText( "Building Network\n" );
			BackPropagationNetwork bpNetwork2 = new BackPropagationNetwork( log, dBackPropagationWordLearningRate, dBackPropagationWordMomentum, 20, 20, 1 );
			netWorkText.AppendText( "Loading Network\n" );
			bpNetwork2.Load( xmlReader );
			bpNetwork2.UseBias = bBackPropagationTwoUseBias;

			xmlReader.Close();

			netWorkText.AppendText( "Testing Network\n" );
			ArrayList bpPatterns = LoadBackPropagationWordTrainingFile();

			for( int i=0; i<bpPatterns.Count; i++ )
			{
				for( int n=0; n<20; n++ )
				{
					( ( BasicNode )bpNetwork.Nodes[ n ] ).SetValue( Values.NodeValue, ( ( BackPropagationWordPattern )patterns[ i ] ).GetInSetAt( n ) );
				}
				

				bpNetwork2.Run();

		
				/// first output value is output value 0
				dTemp = bpNetwork2.GetOutputValue( 0 );
				dCalculatedTemp = bpNetwork2.GetCalculatedOutputValue( 0, ( ( BackPropagationWordPattern )bpPatterns[ i ] ).OutputValue( 0 ) );
				strTemp.Remove( 0, strTemp.Length );

				if( dCalculatedTemp > ( 1 - dBackPropagationWordTolerance ) )
				{
					netWorkText.AppendText( "Absolute output value = " + dCalculatedTemp.ToString() + "\n" );

					strTemp.Append( "Output value is within tolerance level of " + dBackPropagationWordTolerance.ToString() + " of 1 " );

					strTemp.Append( " " );

					for( int n=0; n<( ( BackPropagationWordPattern )bpPatterns[ i ] ).InputSize(); n++ )
					{
						strTemp.Append( ( ( BackPropagationWordPattern )bpPatterns[ i ] ).InputValue( n ) + " " );
					}

					strTemp.Append( "\n Output Value = " + ( ( BackPropagationWordPattern )bpPatterns[ i ] ).OutputValue( Values.NodeValue ) );
					

					netWorkText.AppendText( "Test = " + dCalculatedTemp.ToString() + " Tolerance = " + dBackPropagationWordTolerance.ToString() + " " + strTemp.ToString() + "\n" );

				}	
			}

			netWorkText.AppendText( "Test Finished\n" );

			log.Close();
			patterns.Clear();
			bpPatterns.Clear();

		}


		/// <summary>
		/// load the working or the run file for the back propagtion two network
		/// </summary>
		/// <returns>an array of patterns</returns>
		private ArrayList LoadBackPropagationWordWorkingFile()
		{
			netWorkText.AppendText( "Loading Back Propagation Working File please wait ... \n" );
			ArrayList patterns = new ArrayList();

			StreamReader stream = new StreamReader( strBackPropagationWordRunFile );
			StringBuilder strString = new StringBuilder( "" );
			StringBuilder strWord = new StringBuilder( "" );
			int nCount = 0;
			int n = 0;
			BackPropagationWordPattern pattern = new BackPropagationWordPattern( log );
			StringBuilder strTemp = new StringBuilder( " " );
			bool bEndOfSentence = false;

			/// load the data from the training file
			while( stream.Peek() > -1  )
			{
				
				strString.Remove( 0, strString.Length );
				strString.Append( stream.ReadLine() );

				n=0;
				while( n != strString.Length )
				{
					bEndOfSentence = false;
					strWord.Remove( 0, strWord.Length );

					while( n != strString.Length && strString[ n ] == ' ' )
						n++;

					while( n != strString.Length && strString[ n ] != ' ' )
					{
						/// end of sentence
						if( strString[ n ] == '.'  )
						{
							pattern.SetInValue( nCount, strWord.ToString() ); 
							if( strWord.ToString().CompareTo( strBackPropagationWordOne ) == 0 )
								pattern.SetOutValue( nCount, 1.0 );
							else if( strWord.ToString().CompareTo( strBackPropagationWordOne ) == 0 )
								pattern.SetOutValue( nCount, 1.0 );
							else
								pattern.SetOutValue( nCount, 0.0 );


							patterns.Add( pattern );
							
							nCount = 0;
							n++;

							strTemp.Remove( 0, strTemp.Length );

							/// get the full string and calculate the output value as we go
							/// can't use string search functions such as indexof as this 
							/// will find part words
							int nWordCountOne = 0;
							int nWordCountTwo = 0;
							for( int i=0; i<pattern.InputSize(); i++ )
							{
								strTemp.Append( pattern.InputValue( i ) + " " );
								if( pattern.InputValue( i ).CompareTo( strBackPropagationWordOne ) == 0 )
									nWordCountOne++;
								if( pattern.InputValue( i ).CompareTo( strBackPropagationWordTwo ) == 0 )
									nWordCountTwo++;
							}

							if( nWordCountOne == 1 && nWordCountTwo == 0 )
								pattern.SetOutValue( 0, 1.0 );
							else if( nWordCountTwo == 1 && nWordCountOne == 0 )
								pattern.SetOutValue( 0, 1.0 );
							else
								pattern.SetOutValue( 0, 0.0 );


							netWorkText.AppendText( "Setting pattern to, " + strTemp.ToString() + "\n" );



							pattern = new BackPropagationWordPattern( log );

							bEndOfSentence = true;

						}
						else /// save the word
						{
							strWord.Append( strString[ n++ ] );
						}
					}

					if( bEndOfSentence == false )
					{
						pattern.SetInValue( nCount, strWord.ToString() ); 
					}

					nCount++;
				}

			}

			stream.Close();

			return patterns;
		}

		/// <summary>
		/// start the thread to run the back propagation two network
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnRunBackPropagtion2( object sender, System.EventArgs e )
		{
			threadStart = new ThreadStart( OnBackPropagation2Run );
			thread = new Thread( threadStart );

			thread.Start();	
		}

		/// <summary>
		/// run the back propagation two network
		/// </summary>
		private void OnBackPropagation2Run()
		{
			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Load And Run BackPropagation 2.xml", "NeuralNetworkTester", true );

			double dTemp = 0.0;
			double dCalculatedTemp = 0.0;
			StringBuilder strTemp = new StringBuilder( " " );
			StringBuilder strSentence = new StringBuilder( " " );
		
			
			FileStream readStream = new FileStream( "backpropagationnetworktwo.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );
			
			/// create a backpropagation network with a learning rate of 0.45
			/// a momentum of 0.9 with 2 nodes in the input layer, 3 nodes in the middle layer
			/// and one output node.
			netWorkText.AppendText( "Building Network\n" );
			BackPropagationNetwork bpNetwork2 = new BackPropagationNetwork( log, dBackPropagationWordLearningRate, dBackPropagationWordMomentum, 20, 20, 1 );
			netWorkText.AppendText( "Loading Network\n" );
			bpNetwork2.Load( xmlReader );
			bpNetwork2.UseBias = bBackPropagationTwoUseBias;

			xmlReader.Close();

			netWorkText.AppendText( "Testing Network\n" );
			ArrayList bpPatterns = LoadBackPropagationWordWorkingFile();

			for( int i=0; i<bpPatterns.Count; i++ )
			{
				for( int n=0; n<20; n++ )
				{
					( ( BasicNode )bpNetwork2.Nodes[ n ] ).SetValue( Values.NodeValue, ( ( BackPropagationWordPattern )bpPatterns[ i ] ).GetInSetAt( n ) );
				}
	
				bpNetwork2.Run();

		
				/// first output value is output value 0
				dTemp = bpNetwork2.GetOutputValue( 0 );
				dCalculatedTemp = bpNetwork2.GetCalculatedOutputValue( 0, ( ( BackPropagationWordPattern )bpPatterns[ i ] ).OutputValue( 0 ) );
				strTemp.Remove( 0, strTemp.Length );

				if( dCalculatedTemp > ( 1 - dBackPropagationWordTolerance ) )
				{
					netWorkText.AppendText( "Absolute output value = " + dCalculatedTemp.ToString() + "\n" );

					strTemp.Append( "Output value is within tolerance level of " + dBackPropagationWordTolerance.ToString() + " of 1 " );

					strTemp.Append( " " );

					for( int n=0; n<( ( BackPropagationWordPattern )bpPatterns[ i ] ).InputSize(); n++ )
					{
						strTemp.Append( ( ( BackPropagationWordPattern )bpPatterns[ i ] ).InputValue( n ) + " " );
					}

					strTemp.Append( "\n Output Value = " + ( ( BackPropagationWordPattern )bpPatterns[ i ] ).OutputValue( Values.NodeValue ) );
					

					netWorkText.AppendText( "Test = " + dCalculatedTemp.ToString() + " Tolerance = " + dBackPropagationWordTolerance.ToString() + " " + strTemp.ToString() + "\n" );

				}	
			}

			netWorkText.AppendText( "Test Finished\n" );

			log.Close();
			bpPatterns.Clear();

		}

		private void OnAbout(object sender, System.EventArgs e)
		{
		}


		/*****************************************************************************************************
		 *					SELF ORGANISING NETWORK ONE STUFF
		 */

		private void OnSelfOrganisingNetwork1(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( OnTrainSelfOrganizingNetwork1 );
			thread = new Thread( threadStart );

			thread.Start();			
		}


		/// <summary>
		/// load the self organizing network training file
		/// </summary>
		/// <returns></returns>
		private ArrayList LoadSelfOrganizingNetworkFile( string strFileName )
		{
			ArrayList patterns = new ArrayList();
			StreamReader stream = new StreamReader( strFileName );
			StringBuilder strString = new StringBuilder( "" );
			StringBuilder strNumber = new StringBuilder( "" );

			/// load the data from the training file
			for( int i=0; i<nNumberOfItemsInSelfOrganizingNetworkTrainingFile; i++ )
			{
				AdalinePattern aPattern = new AdalinePattern( log, 2, 1 );
				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, Double.Parse( strNumber.ToString() ) );
				strNumber.Remove( 0, strNumber.Length );
				while( strString[ n ] == ' ' )
					n++;
				while( n != strString.Length && strString[ n ] != ' '  )
					strNumber.Append( strString[ n++ ] );
				aPattern.SetInValue( 1, Double.Parse( strNumber.ToString() ) );
				strNumber.Remove( 0, strNumber.Length );

				netWorkText.AppendText( aPattern.Data() + "\n" );
				patterns.Add( aPattern );
			}

			stream.Close();

			return patterns;
		}

		/// <summary>
		/// Generate the files for the self organizing network
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnGenerateSelfOrganizingNetworkFileFunction( bool bGenerateTestFile )
		{
			netWorkText.AppendText( "Generating Self Organizing Network File... Please Wait\n" );
			Cursor.Current = Cursors.WaitCursor;

			double x = 0.0;
			double y = 0.0;

			FileInfo info;
			if( bTrainingSelfOrganizingNetwork == true )
			{
				info = new FileInfo( "SelfOrganizingNetworkOne.trn" );
			}
			else if( bGenerateTestFile == true )
			{
				info = new FileInfo( "SelfOrganizingNetworkOneTest.tst" );
			}
			else
				info = new FileInfo( "SelfOrganizingNetworkOne.wrk" );

			if( info.Exists == true )
				info.Delete();

			FileStream stream;
			if( bTrainingSelfOrganizingNetwork == true )
			{
				stream = new FileStream( "SelfOrganizingNetworkOne.trn", FileMode.Create, FileAccess.Write );
			}
			else if( bGenerateTestFile == true )
			{
				stream = new FileStream( "SelfOrganizingNetworkOneTest.tst", FileMode.Create, FileAccess.Write );
			}
			else
				stream = new FileStream( "SelfOrganizingNetworkOne.wrk", FileMode.Create, FileAccess.Write );

			StreamWriter writer = new StreamWriter( stream, Encoding.UTF8 );

			if( bGenerateTestFile == true )
			{
				x = Values.Random( -5, 5 );
				y = Values.Random( -5, 5 );
			}

			for( int i=0; i<nNumberOfItemsInSelfOrganizingNetworkTrainingFile; i++ )
			{
				if( bGenerateTestFile == false )
				{
					x = Values.Random( -5, 5 );
					y = Values.Random( -5, 5 );
				}

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

			writer.Close();

			Cursor.Current = Cursors.Default;
			netWorkText.AppendText( "Self Organizing Network File Generated\n" );
		}



		/// <summary>
		/// Train the self organizing network
		/// </summary>
		public void OnTrainSelfOrganizingNetwork1()
		{
			/// delete the old log first
			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Neural Network Tester.xml", "NeuralNetworkTester", true );

			if( bGenerateNewSelfOrganizingTrainingFile == true )
				OnGenerateSelfOrganizingNetworkFileFunction( false );
			
			ArrayList patterns = LoadSelfOrganizingNetworkFile( "SelfOrganizingNetworkOne.trn" );

			netWorkText.AppendText( "Creating the network\n" );

			/// create the network
			SelfOrganizingNetwork son = new SelfOrganizingNetwork( log, 2, 10, 10, dInitialSonOneLearningRate, dFinalSonOneLearningRate, nInitialSonOneNeighborhoodSize, nFinalSonOneNeighborhoodSize, nNeighborhoodDecrementInterval, nNumberOfSonOneIterations );
			
			netWorkText.AppendText( "Network Created\n" );
			
			/// train the self organizing network
			int nIteration = 0;

			for( nIteration=0; nIteration<nNumberOfSonOneIterations; nIteration++ )
			{
				for( int i=0; i<nNumberOfItemsInSelfOrganizingNetworkTrainingFile; i++ )
				{
					son.SetValue( ( Pattern )patterns[ i ] );
					son.Run();
					netWorkText.AppendText( "." );
				}

				son.Learn();

				son.Epoch();

				log.Log( DebugLevelSet.Progress, "Iteration number " + nIteration.ToString() + " produced a winning node at  " + son.WinningHorizontalPos + " Horizontal and " + son.WinningVerticalPos + " vertical, winning node value = " + son.GetWinningNodeValue( son.WinningHorizontalPos, son.WinningVerticalPos ) + "\n", ClassName );
				netWorkText.AppendText( "\nIteration number " + nIteration.ToString() + " produced a winning node at  " + son.WinningHorizontalPos + " Horizontal and " + son.WinningVerticalPos + " vertical, winning node value = " + son.GetWinningNodeValue( son.WinningHorizontalPos, son.WinningVerticalPos ) + "\n" );

			}


			netWorkText.AppendText( "Saving the network" );

			FileStream xmlstream = new FileStream( "selforganizingnetworkone.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8, true );
			XmlWriter xmlWriter = new XmlTextWriter( xmlstream, System.Text.Encoding.UTF8 );
			xmlWriter.WriteStartDocument();

			son.Save( xmlWriter );

			xmlWriter.WriteEndDocument();
			xmlWriter.Close();


			/// now load the file
			FileStream readStream = new FileStream( "selforganizingnetworkone.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );
			

			SelfOrganizingNetwork sonTest = new SelfOrganizingNetwork( log );
			sonTest.Load( xmlReader );

			xmlReader.Close();

			netWorkText.AppendText( "Testing against the test file the following out put should be identical for the test\n" );

			ArrayList testPatterns = this.LoadSelfOrganizingNetworkFile( "SelfOrganizingNetworkOneTest.tst" );

			for( int i=0; i<nNumberOfItemsInSelfOrganizingNetworkTrainingFile; i++ )
			{
				sonTest.SetValue( ( Pattern )testPatterns[ i ] );
				sonTest.Run();

				netWorkText.AppendText( "Run called at " + i.ToString() + " Network Values are :- Composite Value = " + sonTest.GetPosition( Values.Composite ) + ", Horizontal Value = " + sonTest.GetPosition( Values.Row ) + ", Vertical Value = " + sonTest.GetPosition( Values.Column ) + ", Current Winning Horizontal Position = " + sonTest.WinningHorizontalPos + ", Current Winning Vertical Position " + sonTest.WinningVerticalPos + ", Inputs = " + ( ( BasicNode )sonTest.Nodes[ 0 ] ).NodeValues[ Values.NodeValue ].ToString() + "," + ( ( BasicNode )sonTest.Nodes[ 1 ] ).NodeValues[ Values.NodeValue ].ToString() + ", Winning Node Value = " + sonTest.GetWinningNodeValue( sonTest.WinningHorizontalPos, sonTest.WinningVerticalPos ) + "\n" );
			}

			testPatterns.Clear();
			StringBuilder strDataDisplay = new StringBuilder( "" );

			ArrayList arrayOutput = new ArrayList();
			SelfOrganizingNetworkData data;
			netWorkText.AppendText( "Completed the test ... now reprocessing the orginal data through the loaded network\n " ); 

			for( int i=0; i<nNumberOfItemsInSelfOrganizingNetworkTrainingFile; i++ )
			{
				sonTest.SetValue( ( Pattern )patterns[ i ] );
				sonTest.Run();

				strDataDisplay.Remove( 0, strDataDisplay.Length );
				strDataDisplay.Append( "Run Called at " + i.ToString() + " Network Values are :- Composite Value = " + sonTest.GetPosition( Values.Composite ) + ", Horizontal Value = " + sonTest.GetPosition( Values.Row ) + ", Vertical Value = " + sonTest.GetPosition( Values.Column ) + ", Current Winning Horizontal Position = " + sonTest.WinningHorizontalPos + ", Current Winning Vertical Position " + sonTest.WinningVerticalPos + ", Inputs = " + ( ( BasicNode )sonTest.Nodes[ 0 ] ).NodeValues[ Values.NodeValue ].ToString() + "," + ( ( BasicNode )sonTest.Nodes[ 1 ] ).NodeValues[ Values.NodeValue ].ToString() + ", Winning Node Value = " + sonTest.GetWinningNodeValue( sonTest.WinningHorizontalPos, sonTest.WinningVerticalPos ) + "\n" );

				netWorkText.AppendText( strDataDisplay.ToString() ); 

				data = new SelfOrganizingNetworkData();
				data.CompositeValue = ( int )sonTest.GetPosition( Values.Composite );
				data.Data = strDataDisplay.ToString();

				arrayOutput.Add( data );
			}

			netWorkText.AppendText( "\n Completed the run through the training data ... processing the answers\n" );

			int nItemCount = 0;
			
			SelfOrganizingNetworkData dataTest;
			bool bDataValid = false;
			for( int i=0; i<arrayOutput.Count; i++ )
			{
				/// first value is always valid
				if( i==0 )
				{
					nItemCount = 0;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					bDataValid = true;
				}
				else
				{
					bool bFound = false;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					for( int n=0; n<i; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							bFound = true;
							n=i;
						}
					}

					if( bFound == false )
					{
						nItemCount = 0;
						data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
						bDataValid = true;
					}
				}

				if( bDataValid == true )
				{
					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							nItemCount++;
						}
					}

					netWorkText.AppendText( "\n\nThere are " + nItemCount.ToString() + " items out of " + arrayOutput.Count.ToString() + " That have the Composite Value " + data.CompositeValue.ToString() + "\n" );

					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							netWorkText.AppendText( dataTest.Data + "\n" );
						}
					}

					bDataValid = false;
				}
			}

			netWorkText.AppendText( "Training for Self Organizing One network finished\n " );


			log.Close();
			patterns.Clear();

		}


		private void OnGenerateSelfOrganizingTrainingFile(object sender, System.EventArgs e)
		{
			bTrainingSelfOrganizingNetwork = true;
			threadStart = new ThreadStart( OnGenerateSelfOrganizingNetworkFile );
			thread = new Thread( threadStart );

			thread.Start();					
		}

		private void OnGenerateSelfOrganizingNetworkWorkingFile(object sender, System.EventArgs e)
		{
			bTrainingSelfOrganizingNetwork = false;
			threadStart = new ThreadStart( OnGenerateSelfOrganizingNetworkFile );
			thread = new Thread( threadStart );

			thread.Start();		
		}

		private void OnGenerateSelfOrganizingNetworkFile()
		{
			OnGenerateSelfOrganizingNetworkFileFunction( false );
		}

		private void OnGenerateSelfOrganizingTestFile(object sender, System.EventArgs e)
		{
			bTrainingSelfOrganizingNetwork = false;
			threadStart = new ThreadStart( OnGenerateSelfOrganizingTestNetworkFile );
			thread = new Thread( threadStart );

			thread.Start();	
		}

		private void OnGenerateSelfOrganizingTestNetworkFile()
		{
			OnGenerateSelfOrganizingNetworkFileFunction( true );
		}

		private void OnLoadAndRunSelfOrganizingNetworkOneMenu(object sender, System.EventArgs e)
		{
			bTrainingSelfOrganizingNetwork = false;
			threadStart = new ThreadStart( OnLoadAndRunSelfOrganizingNetworkOne );
			thread = new Thread( threadStart );

			thread.Start();		
		}

		private void OnLoadAndRunSelfOrganizingNetworkOne()
		{
			/// delete the old log first
			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Load And Run Self Organizing Network One.xml", "NeuralNetworkTester", true );

			if( bGenerateNewSelfOrganizingTrainingFile == true )
				OnGenerateSelfOrganizingNetworkFileFunction( false );
			
			ArrayList patterns = LoadSelfOrganizingNetworkFile( "SelfOrganizingNetworkOne.trn" );


			/// now load the file
			FileStream readStream = new FileStream( "selforganizingnetworkone.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );
			

			SelfOrganizingNetwork sonTest = new SelfOrganizingNetwork( log );
			sonTest.Load( xmlReader );

			xmlReader.Close();

			StringBuilder strDataDisplay = new StringBuilder( "" );

			ArrayList arrayOutput = new ArrayList();
			SelfOrganizingNetworkData data;
			netWorkText.AppendText( "Completed the test ... now reprocessing the orginal data through the loaded network\n " ); 

			for( int i=0; i<nNumberOfItemsInSelfOrganizingNetworkTrainingFile; i++ )
			{
				sonTest.SetValue( ( Pattern )patterns[ i ] );
				sonTest.Run();

				strDataDisplay.Remove( 0, strDataDisplay.Length );
				strDataDisplay.Append( "Run Called at " + i.ToString() + " Network Values are :- Composite Value = " + sonTest.GetPosition( Values.Composite ) + ", Horizontal Value = " + sonTest.GetPosition( Values.Row ) + ", Vertical Value = " + sonTest.GetPosition( Values.Column ) + ", Current Winning Horizontal Position = " + sonTest.WinningHorizontalPos + ", Current Winning Vertical Position " + sonTest.WinningVerticalPos + ", Inputs = " + ( ( BasicNode )sonTest.Nodes[ 0 ] ).NodeValues[ Values.NodeValue ].ToString() + "," + ( ( BasicNode )sonTest.Nodes[ 1 ] ).NodeValues[ Values.NodeValue ].ToString() + ", Winning Node Value = " + sonTest.GetWinningNodeValue( sonTest.WinningHorizontalPos, sonTest.WinningVerticalPos ) + "\n" );

				netWorkText.AppendText( strDataDisplay.ToString() ); 

				data = new SelfOrganizingNetworkData();
				data.CompositeValue = ( int )sonTest.GetPosition( Values.Composite );
				data.Data = strDataDisplay.ToString();

				arrayOutput.Add( data );
			}

			netWorkText.AppendText( "\n Completed the run through the data ... processing the answers\n" );

			int nItemCount = 0;
			
			SelfOrganizingNetworkData dataTest;
			bool bDataValid = false;
			for( int i=0; i<arrayOutput.Count; i++ )
			{
				/// first value is always valid
				if( i==0 )
				{
					nItemCount = 0;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					bDataValid = true;
				}
				else
				{
					bool bFound = false;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					for( int n=0; n<i; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							bFound = true;
							n=i;
						}
					}

					if( bFound == false )
					{
						nItemCount = 0;
						data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
						bDataValid = true;
					}
				}

				if( bDataValid == true )
				{
					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							nItemCount++;
						}
					}

					netWorkText.AppendText( "\n\nThere are " + nItemCount.ToString() + " items out of " + arrayOutput.Count.ToString() + " That have the Composite Value " + data.CompositeValue.ToString() + "\n" );

					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							netWorkText.AppendText( dataTest.Data + "\n" );
						}
					}

					bDataValid = false;
				}
			}

			netWorkText.AppendText( "Load And Run for Self Organizing One network finished\n " );


			log.Close();
			patterns.Clear();

		}


		/////////////////////////////////////////////////////////////////////////////////////////////////
		///			SELF ORGANIZING NETWORK TWO STUFF
		///			

		private void OnSelfOrganizingNetworkTwoClick(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( OnSelfOrganizingNetworkTwoTrain );
			thread = new Thread( threadStart );

			thread.Start();	
		
		}


		private void OnSelfOrganizingNetworkTwoTrain()
		{
			FileInfo info = new FileInfo( "Neural Network Tester.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Neural Network Tester.xml", "NeuralNetworkTester", true );
		
			/// rather than cut and paste the code and pretend its different just use earlier function
			ArrayList patterns = LoadBackPropagationWordTrainingFile();

			netWorkText.AppendText( "Creating Network\n" );

			/// note 20 input nodes ( there are gonna be a lot of links )
			SelfOrganizingNetworkWordNetwork soNetwork = new SelfOrganizingNetworkWordNetwork( log, 20, 10, 10, dInitialSonTwoLearningRate, dFinalSonTwoLearningRate, nInitialSonTwoNeighborhoodSize, nFinalSonTwoNeighborhoodSize, nSonTwoNeighborhoodDecrementInterval, nNumberOfSonTwoIterations );
					
			netWorkText.AppendText( "Network Created\n" );
			
			/// train the self organizing network
			int nIteration = 0;

			for( nIteration=0; nIteration<nNumberOfSonTwoIterations; nIteration++ )
			{
				for( int i=0; i<patterns.Count; i++ )
				{
					
					for( int n=0; n<20; n++ )
					{
						/// Note because I use the LoadBackPropagationWord training file function the pattern should be cast to an adaline word pattern which 
						/// contains the required function to get the values from the words.
						( ( BasicNode )soNetwork.Nodes[ n ] ).SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( n ));		
					}
					

					soNetwork.Run();
					netWorkText.AppendText( "." );
				}

				soNetwork.Learn();

				soNetwork.Epoch();

				log.Log( DebugLevelSet.Progress, "Iteration number " + nIteration.ToString() + " produced a winning node at  " + soNetwork.WinningHorizontalPos + " Horizontal and " + soNetwork.WinningVerticalPos + " vertical, winning node value = " + soNetwork.GetWinningNodeValue( soNetwork.WinningHorizontalPos, soNetwork.WinningVerticalPos ) + "\n", ClassName );
				netWorkText.AppendText( "\nIteration number " + nIteration.ToString() + " produced a winning node at  " + soNetwork.WinningHorizontalPos + " Horizontal and " + soNetwork.WinningVerticalPos + " vertical, winning node value = " + soNetwork.GetWinningNodeValue( soNetwork.WinningHorizontalPos, soNetwork.WinningVerticalPos ) + "\n" );

			}


			netWorkText.AppendText( "Saving the network\n" );

			FileStream xmlstream = new FileStream( "selforganizingnetworktwo.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8, true );
			XmlWriter xmlWriter = new XmlTextWriter( xmlstream, System.Text.Encoding.UTF8 );
			xmlWriter.WriteStartDocument();

			soNetwork.Save( xmlWriter );

			xmlWriter.WriteEndDocument();
			xmlWriter.Close();


			/// now load the file
			FileStream readStream = new FileStream( "selforganizingnetworktwo.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );


			netWorkText.AppendText( "Loading the network\n" );
			SelfOrganizingNetworkWordNetwork sonTest = new SelfOrganizingNetworkWordNetwork( log );
			sonTest.Load( xmlReader );

			xmlReader.Close();

			StringBuilder strDataDisplay = new StringBuilder( "" );

			ArrayList arrayOutput = new ArrayList();
			SelfOrganizingNetworkData data;
			netWorkText.AppendText( "Completed the test ... now reprocessing the orginal data through the loaded network\n " ); 

			for( int i=0; i<patterns.Count; i++ )
			{
				
				for( int n=0; n<20; n++ )
				{
					/// Note because I use the LoadBackPropagationWord training file function the pattern should be cast to an adaline word pattern which 
					/// contains the required function to get the values from the words.
					( ( BasicNode )sonTest.Nodes[ n ] ).SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( n ) );
				}

				sonTest.Run();

				strDataDisplay.Remove( 0, strDataDisplay.Length );
				strDataDisplay.Append( "Run Called at " + i.ToString() + " Network Values are :- Composite Value = " + sonTest.GetPosition( Values.Composite ) + ", Horizontal Value = " + sonTest.GetPosition( Values.Row ) + ", Vertical Value = " + sonTest.GetPosition( Values.Column ) + ", Current Winning Horizontal Position = " + sonTest.WinningHorizontalPos + ", Current Winning Vertical Position " + sonTest.WinningVerticalPos + ", Inputs = " + ( ( BasicNode )sonTest.Nodes[ 0 ] ).NodeValues[ Values.NodeValue ].ToString() + "," + ( ( BasicNode )sonTest.Nodes[ 1 ] ).NodeValues[ Values.NodeValue ].ToString() + ", Winning Node Value = " + sonTest.GetWinningNodeValue( sonTest.WinningHorizontalPos, sonTest.WinningVerticalPos ) + "\n" );

				strDataDisplay.Append( " String Data :- " );
				for( int n=0; n<( ( AdalineWordPattern )patterns[ i ] ).InputSize(); n++ )
				{
					strDataDisplay.Append( ( ( AdalineWordPattern )patterns[ i ] ).InputValue( n ) + " " );
				}

				netWorkText.AppendText( strDataDisplay.ToString() ); 

				data = new SelfOrganizingNetworkData();
				data.CompositeValue = ( int )sonTest.GetPosition( Values.Composite );
				data.Data = strDataDisplay.ToString();

				arrayOutput.Add( data );
			}

			netWorkText.AppendText( "\n Completed the run through the training data ... processing the answers\n" );

			int nItemCount = 0;
			
			SelfOrganizingNetworkData dataTest;
			bool bDataValid = false;
			for( int i=0; i<arrayOutput.Count; i++ )
			{
				/// first value is always valid
				if( i==0 )
				{
					nItemCount = 0;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					bDataValid = true;
				}
				else
				{
					bool bFound = false;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					for( int n=0; n<i; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							bFound = true;
							n=i;
						}
					}

					if( bFound == false )
					{
						nItemCount = 0;
						data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
						bDataValid = true;
					}
				}

				if( bDataValid == true )
				{
					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							nItemCount++;
						}
					}

					netWorkText.AppendText( "\n\nThere are " + nItemCount.ToString() + " items out of " + arrayOutput.Count.ToString() + " That have the Composite Value " + data.CompositeValue.ToString() + "\n" );

					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							netWorkText.AppendText( dataTest.Data + "\n" );
						}
					}

					bDataValid = false;
				}
			}

			netWorkText.AppendText( "Training for Self Organizing Two network finished\n " );

			
			netWorkText.AppendText( "Test Finished\n" );

			log.Close();
			patterns.Clear();

		}

		private void OnLoadAndRunSelfOrgainzingNetworktwoClick(object sender, System.EventArgs e)
		{
			threadStart = new ThreadStart( OnSelfOrganizingNetworkTwoRun );
			thread = new Thread( threadStart );

			thread.Start();			
		}


		private void OnSelfOrganizingNetworkTwoRun()
		{
			FileInfo info = new FileInfo( "Load And Run Self Organizing Network Two.xml" );
			if( info.Exists == true )
			{
				info.Delete();
			}

			log = new Logger( "Load And Run Self Organizing Network Two.xml", "NeuralNetworkTester", true );
		
			/// rather than cut and paste the code and pretend its different just use earlier function
			ArrayList patterns = LoadBackPropagationWordWorkingFile();


			/// now load the file
			FileStream readStream = new FileStream( "selforganizingnetworktwo.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
			XmlReader xmlReader = new XmlTextReader( readStream );

			SelfOrganizingNetworkWordNetwork sonTest = new SelfOrganizingNetworkWordNetwork( log );

			netWorkText.AppendText( "Loading the network\n" );
			sonTest.Load( xmlReader );

			xmlReader.Close();

			StringBuilder strDataDisplay = new StringBuilder( "" );

			ArrayList arrayOutput = new ArrayList();
			SelfOrganizingNetworkData data;
	
			for( int i=0; i<patterns.Count; i++ )
			{
				for( int n=0; n<20; n++ )
				{
					/// Note because I use the LoadBackPropagationWord training file function the pattern should be cast to an adaline word pattern which 
					/// contains the required function to get the values from the words.
					( ( BasicNode )sonTest.Nodes[ n ] ).SetValue( Values.NodeValue, ( ( AdalineWordPattern )patterns[ i ] ).GetInSetAt( n ) );				
				}

				sonTest.Run();

				strDataDisplay.Remove( 0, strDataDisplay.Length );
				strDataDisplay.Append( "Run Called at " + i.ToString() + " Network Values are :- Composite Value = " + sonTest.GetPosition( Values.Composite ) + ", Horizontal Value = " + sonTest.GetPosition( Values.Row ) + ", Vertical Value = " + sonTest.GetPosition( Values.Column ) + ", Current Winning Horizontal Position = " + sonTest.WinningHorizontalPos + ", Current Winning Vertical Position " + sonTest.WinningVerticalPos + ", Inputs = " + ( ( BasicNode )sonTest.Nodes[ 0 ] ).NodeValues[ Values.NodeValue ].ToString() + "," + ( ( BasicNode )sonTest.Nodes[ 1 ] ).NodeValues[ Values.NodeValue ].ToString() + ", Winning Node Value = " + sonTest.GetWinningNodeValue( sonTest.WinningHorizontalPos, sonTest.WinningVerticalPos ) + "\n" );

				strDataDisplay.Append( " String Data :- " );
				for( int n=0; n<( ( AdalineWordPattern )patterns[ i ] ).InputSize(); n++ )
				{
					strDataDisplay.Append( ( ( AdalineWordPattern )patterns[ i ] ).InputValue( n ) + " " );
				}

				netWorkText.AppendText( strDataDisplay.ToString() ); 

				data = new SelfOrganizingNetworkData();
				data.CompositeValue = ( int )sonTest.GetPosition( Values.Composite );
				data.Data = strDataDisplay.ToString();

				arrayOutput.Add( data );
			}

			netWorkText.AppendText( "\n Completed the run through the data ... processing the answers\n" );

			int nItemCount = 0;
			
			SelfOrganizingNetworkData dataTest;
			bool bDataValid = false;
			for( int i=0; i<arrayOutput.Count; i++ )
			{
				/// first value is always valid
				if( i==0 )
				{
					nItemCount = 0;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					bDataValid = true;
				}
				else
				{
					bool bFound = false;
					data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
					for( int n=0; n<i; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							bFound = true;
							n=i;
						}
					}

					if( bFound == false )
					{
						nItemCount = 0;
						data = ( SelfOrganizingNetworkData )arrayOutput[ i ];
						bDataValid = true;
					}
				}

				if( bDataValid == true )
				{
					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							nItemCount++;
						}
					}

					netWorkText.AppendText( "\n\nThere are " + nItemCount.ToString() + " items out of " + arrayOutput.Count.ToString() + " That have the Composite Value " + data.CompositeValue.ToString() + "\n" );

					for( int n=0; n<arrayOutput.Count; n++ )
					{
						dataTest = ( SelfOrganizingNetworkData )arrayOutput[ n ];
						if( dataTest.CompositeValue == data.CompositeValue )
						{
							netWorkText.AppendText( dataTest.Data + "\n" );
						}
					}

					bDataValid = false;
				}
			}

			netWorkText.AppendText( "Load And Run for Self Organizing Two network finished\n " );

			
			netWorkText.AppendText( "Test Finished\n" );

			log.Close();
			patterns.Clear();

		}

  	}


	/// <summary>
	/// structure to help storing and structuring the self organizing network output
	/// </summary>
	public struct SelfOrganizingNetworkData
	{
		private int nCompositeValue;
		private string strData;

		public int CompositeValue 
		{
			get
			{
				return nCompositeValue;
			}
			set
			{
				nCompositeValue = value;
			}
		}

		public string Data
		{
			get
			{
				return strData;
			}
			set
			{
				strData = value;
			}
		}

	}

	public class TestSerializer
	{
		public int nOne = 1;
	}
}

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 | Mobile
Web04 | 2.8.140922.1 | Last Updated 10 Dec 2003
Article Copyright 2003 by pseudonym67
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid