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

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 SharpUtils;
using System.Collections;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace Neural_Net_Library
{
	public class AdalineWordNode : AdalineNode
	{
		private DebugLevel debugLevel;
		private Logger log;

		public AdalineWordNode( Logger log ) : base( log )
		{
			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;
		}

		public AdalineWordNode( Logger log, double dLearningRate ) : base( log, dLearningRate )
		{
			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;
		}

		/// <summary>
		/// Threshold transfer function
		/// </summary>
		/// <param name="dValue"></param>
		/// <returns></returns>
		protected override double TransferFunction( double dValue )
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Transfer function called with a value of " + dValue.ToString(), ClassName );
			}

			if( dValue < 0.5 )
				return -1.0;
			
			return 1.0;
		}

		protected override double TransferFunction( double dValue, double dBias )
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Transfer function with bias called with a value of " + dValue.ToString() + " and a bias of " + dBias.ToString(), ClassName );
			}

			dValue += dBias;

			if( dValue < 0.5 )
				return -1.0;

			return 1.0;
		}

		/// <summary>
		/// save the current node
		/// </summary>
		/// <param name="xmlWriter"></param>
		public override void Save( XmlWriter xmlWriter )
		{
			xmlWriter.WriteStartElement( "AdalineWordNode" );
			base.Save( xmlWriter );
			xmlWriter.WriteEndElement();
		}

		/// <summary>
		/// load the node
		/// </summary>
		/// <param name="xmlReader"></param>
		public override void Load( XmlReader xmlReader )
		{
			bool bBreak = false;
			for( ;; )
			{
				xmlReader.Read();
				switch( xmlReader.Name )
				{
					case "AdalineNode": base.Load( xmlReader ); bBreak = true; break;
				}

				if( bBreak == true )
					break;
			}
		}



	}

	/// <summary>
	/// Adaline Word Link
	/// </summary>
	public class AdalineWordLink : BasicLink
	{
		/// <summary>
		/// pass constructor call back to the default constructor
		/// </summary>
		/// <param name="log"></param>
		public AdalineWordLink( Logger log ) : base( log )
		{
			arrayLinkValues[ Values.Weight ] = Values.Random( 0, 1 );
		}


		/// <summary>
		/// save the current object
		/// </summary>
		/// <param name="xmlWriter"></param>
		public override void Save( XmlWriter xmlWriter )
		{
			xmlWriter.WriteStartElement( "AdalineWordLink" );
			base.Save( xmlWriter );
			xmlWriter.WriteEndElement();
		}

		/// <summary>
		/// reload the current object
		/// </summary>
		/// <param name="xmlReader"></param>
		public override void Load( XmlReader xmlReader )
		{
			bool bBreak = false;
			/// reader is on an adaline node 
			for( ;; )
			{
				xmlReader.Read();
				switch( xmlReader.Name )
				{
					case "AdalineLink": base.Load( xmlReader ); bBreak = true; break;
				}

				if( bBreak == true )
					break;
			}

		}

		/// <summary>
		/// get the class name
		/// </summary>
		public new string ClassName
		{
			get
			{
				return this.ToString();
			}
		}
	}


	/// <summary>
	/// Adaline word neuron Basically a rewrite of the Adaline neuron class
	/// </summary>
	public class AdalineWordNeuron : BasicNeuron
	{
		private AdalineNode adalineNode;
		protected ArrayList arrayLinks;

		private DebugLevel debugLevel;
		private Logger log;

		public AdalineNode Node
		{
			get
			{
				return adalineNode;
			}
			set
			{
				adalineNode = value;
			}
		}

		public ArrayList Links
		{
			get
			{
				return arrayLinks;
			}
		}

		/// <summary>
		/// constructor
		/// </summary>
		/// <param name="basicNodeInputNodeOne"></param>
		/// <param name="basicNodeInputNodeTwo"></param>
		/// <param name="biasNodeBias"></param>
		/// <param name="adalineNode"></param>
		public AdalineWordNeuron( Logger log, BasicNode basicNodeInputNodeOne, BasicNode basicNodeInputNodeTwo, AdalineWordNode adalineNode ) : base( log, basicNodeInputNodeOne, basicNodeInputNodeTwo )
		{
			Node = adalineNode;
			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;
			arrayLinks = new ArrayList();
			for( int i=0; i<2; i++ )
				arrayLinks.Add( new AdalineWordLink( log ) );

			BuildLinks();
		}


		/// <summary>
		/// build the adaline neuron links
		/// </summary>
		public override void BuildLinks()
		{

			/// create the connections 
			this.InputNodeOne.CreateLink( ( BasicNode )this.Node, ( BasicLink )this.Links[ 0 ] );
			this.InputNodeTwo.CreateLink( ( BasicNode )this.Node, ( BasicLink )this.Links[ 1 ] );

		}

		public new string ClassName
		{
			get
			{
				return this.ToString();
			}
		}

		/// <summary>
		/// return current information as a string
		/// </summary>
		public new string Data
		{
			get
			{
				StringBuilder strString = new StringBuilder();
				strString.Append( base.Data );
				strString.Append( " AdalineWordNode values: Node Values = " );
				for( int i=0; i<adalineNode.NodeValues.Count; i++ )
				{
					strString.Append( " value " + i.ToString() + " = " + adalineNode.NodeValues[ i ].ToString() );
				}
				strString.Append( " : Node Errors = "  );
				for( int i=0; i<adalineNode.NodeErrors.Count; i++ )
				{
					strString.Append( " value " + i.ToString() + " = " + adalineNode.NodeErrors[ i ].ToString() );
				}
				strString.Append( " : Node Input Links = " );
				for( int i=0; i<adalineNode.InputLinks.Count; i++ )
				{
					strString.Append( " value " + i.ToString() + " = " + adalineNode.InputLinks[ i ].ToString() );
				}
				strString.Append( " : Node Output Links = " );
				for( int i=0; i<adalineNode.OutputLinks.Count; i++ )
				{
					strString.Append( " value " + i.ToString() + " = " + adalineNode.OutputLinks[ i ].ToString() );
				}
				return strString.ToString();

			}
		}


		/// <summary>
		/// save the current neuron
		/// </summary>
		/// <param name="xmlWriter"></param>
		public override void Save( XmlWriter xmlWriter )
		{
			xmlWriter.WriteStartElement( "AdalineWordNeuron" );
			base.Save( xmlWriter );
			adalineNode.Save( xmlWriter );
			for( int i=0; i<arrayLinks.Count; i++ )
				( ( AdalineWordLink )arrayLinks[ i ] ).Save( xmlWriter );
			xmlWriter.WriteEndElement();
		}


		/// <summary>
		/// load a saved neuron
		/// </summary>
		public override void Load( XmlReader xmlReader )
		{
			bool bBreak = false;
			while( xmlReader.Read() == true )
			{
				switch( xmlReader.NodeType )
				{
					case XmlNodeType.Element:
					{
						switch( xmlReader.Name )
						{
							case "BasicNeuron" : base.Load( xmlReader ); break;
							case "AdalineWordNode" : adalineNode.Load( xmlReader ); break;
							case "AdalineWordLink" :
							{
								/// load the three adaline links
								for( int i=0; i<arrayLinks.Count; i++ )
								{
									bBreak = false;
									( ( AdalineWordLink )arrayLinks[ i ] ).Load( xmlReader );
									/// move the reader to the start of the next one
									for( ;; )
									{
										switch( xmlReader.NodeType )
										{
											case XmlNodeType.Element:
											{
												switch( xmlReader.Name )
												{
													case "AdalineLink" : bBreak = true; break;
												}
											} break;
										}
															/// escape after done final one
										if( bBreak == true || i+1 == arrayLinks.Count )
											break;

										xmlReader.Read();
									}
								} 
								
							}break;
						}
					}break;
				}
			}

			BuildLinks();
		}

	}

	/// <summary>
	/// adaline pattern class 
	/// </summary>
	public class AdalineWordPattern : Pattern
	{

		private DebugLevel debugLevel;
		private Logger log;


		/// <summary>
		/// constructor
		/// </summary>
		public AdalineWordPattern( Logger log ) : base( log )
		{
			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;

		}

		/// <summary>
		/// constructor taking the input and the output sizes
		/// </summary>
		/// <param name="nInSize"></param>
		/// <param name="nOutSize"></param>
		public AdalineWordPattern( Logger log, int nInSize, int nOutSize ) : base( log, nInSize, nOutSize )
		{
			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;

		}


		public AdalineWordPattern( Logger log, ArrayList arrayInSet, ArrayList arrayOutSet ) : base( log, arrayInSet, arrayOutSet )
		{
			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;
		}

		public new string ClassName
		{
			get
			{
				return this.ToString();
			}
		}



		public double GetInSetAt( int nID )
		{
			/// calculate the value for the string

			if( nID >= this.InSet.Count )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.WarningsAndErrors ) == true )
				{
					log.Log( DebugLevelSet.WarningsAndErrors, "Warning the value passed to adaline word GetInSetAt, access value is greater than array count, accessor = " + nID.ToString(), ClassName );  
				}

				return 0.0;
			}
			
			double dValue = 0;
			string strTemp = this.InSet[ nID ].ToString();
			for( int i=0; i<strTemp.Length; i++ )
			{
				dValue += strTemp[ i ] * ( i+1 );
			}

			StringBuilder strBuilder = new StringBuilder();
			
			/// move decimal place
			dValue = dValue / 10000;

			if( dValue < 0 )
			{
				StringBuilder strTempNum = new StringBuilder( dValue.ToString() );
				strTempNum.Remove( 0, 1 );
				strBuilder.Append( strTempNum );
				strBuilder.Insert( 0, '-' );
			}
			else
			{
				strBuilder.Append( dValue.ToString() );
			}

			return Double.Parse( strBuilder.ToString() );
		}

		public string Data()
		{
			StringBuilder strString = new StringBuilder();
			strString.Append( "Pattern ID = " + this.PatternID.ToString() );
			for( int n=0; n<this.InputSize(); n++ )
				strString.Append( " Input Value " + GetInSetAt( n ) + " = " + this.InSet[ n ].ToString() + " " );
			for( int n=0; n<this.OutputSize(); n++ )
			{
				strString.Append( " Output Value "  );
			    if( GetInSetAt( 0 ) > GetInSetAt( 1 ) )
					strString.Append( this.InputValue( 0 ) );
				else
					strString.Append( this.InputValue( 1 ) );
				strString.Append( " = " + this.OutSet[ n ].ToString() + " " );
			}

			return strString.ToString();
		}

		/// <summary>
		/// set the input value
		/// </summary>
		/// <param name="nIndex"></param>
		/// <param name="dValue"></param>
		public void SetInValue( int nIndex, string strValue )
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "setting the input value at " + nIndex.ToString() + " to " + strValue, ClassName );
			}

			try
			{
				this.InSet[ nIndex ] = strValue;
			}
			catch( ArgumentOutOfRangeException argorExp )
			{
				this.InSet.Add( strValue );
				if( debugLevel.TestDebugLevel( DebugLevelSet.Warning ) == true )
				{
					log.Log( DebugLevelSet.Warning, "The index the array in set in value is greater than the array count, adding it, reason " + argorExp.Message, ClassName );
				}
			}
		}


		/// <summary>
		/// get the input value at the given index
		/// </summary>
		/// <param name="nIndex"></param>
		/// <returns></returns>
		public new string InputValue( int nIndex )
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Getting the input value at " + nIndex.ToString(), ClassName );
			}

			if( nIndex < this.InSet.Count )
				return ( string )this.InSet[ nIndex ];
			else
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Warning ) == true )
				{
					log.Log( DebugLevelSet.Warning, "The index value to get the input value is greater than the array count", ClassName );
				}

				return "";
			}
		}

		public new int InputSize()
		{
			return this.InSet.Count;
		}

		public new int OutputSize()
		{
			return this.OutSet.Count;
		}

	}
	
}


 

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