Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#

Neural Dot Net Pt 11 A Conclusion Of Sorts

Rate me:
Please Sign up or sign in to vote.
3.38/5 (19 votes)
9 Dec 20033 min read 55.8K   5.4K   32  
A neural network library in C#.

using System;
using System.Collections;
using SharpUtils;
using System.Xml.Serialization;

namespace Neural_Net_Library
{
	/// <summary>
	/// Summary description for Pattern.
	/// </summary>
	public class Pattern
	{
		private ArrayList arrayInSet;
		private ArrayList arrayOutSet;
		private static int nPatternID = 0;
		private int nCurrentID;


		private DebugLevel debugLevel;
		private Logger log;

		/// <summary>
		/// get the in set array
		/// </summary>
		public ArrayList InSet
		{
			get
			{
				return arrayInSet;
			}
		}

		/// <summary>
		/// get the out set array
		/// </summary>
		public ArrayList OutSet
		{
			get
			{
				return arrayOutSet;
			}
		}

		/// <summary>
		/// get the pattern id
		/// </summary>
		public int PatternID
		{
			get
			{
				return nCurrentID;
			}
		}

		/// <summary>
		/// base constructor
		/// </summary>
		public Pattern( Logger log )
		{


			arrayInSet = new ArrayList();
			arrayOutSet = new ArrayList();
			nPatternID++;
			nCurrentID = nPatternID;

			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;
		}

		/// <summary>
		/// constructor taking the array sizes
		/// </summary>
		/// <param name="nInSize"></param>
		/// <param name="nOutSize"></param>
		public Pattern( Logger log, int nInSize, int nOutSize )
		{
			arrayInSet = new ArrayList( nInSize );
			for( int i=0; i<nInSize; i++ )
				arrayInSet.Add( 0.0 );
			arrayOutSet = new ArrayList( nOutSize );
			for( int i=0; i<nOutSize; i++ )
				arrayOutSet.Add( 0.0 );
			nPatternID++;
			nCurrentID = nPatternID;

			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;
		}

		/// <summary>
		/// constructor taking array lists
		/// </summary>
		/// <param name="arrayInSet"></param>
		/// <param name="arrayOutSet"></param>
		public Pattern( Logger log, ArrayList arrayInSet, ArrayList arrayOutSet )
		{
			arrayInSet = arrayInSet;
			arrayOutSet = arrayOutSet;
			nPatternID++;
			nCurrentID = nPatternID;

			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;
		}


		public Pattern( Logger log, double inputOne, double inputTwo, double output )
		{
			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			this.log = log;

			arrayInSet = new ArrayList();
			arrayInSet.Add( inputOne );
			arrayInSet.Add( inputTwo );
			arrayOutSet = new ArrayList();
			arrayOutSet.Add( output );

			nPatternID++;
			nCurrentID = nPatternID;

		}

		/// <summary>
		/// save the pattern
		/// </summary>
		public virtual void Save()
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Save called on Pattern", ClassName );
			}
		
		}

		/// <summary>
		/// load the pattern
		/// </summary>
		public virtual void Load()
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Load called on pattern", ClassName );
			}
		}

		/// <summary>
		/// make a copy of the pattern
		/// </summary>
		/// <param name="pattern"></param>
		public void Copy( Pattern pattern )
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Copying the pattern", ClassName );
			}

			arrayInSet = ( ArrayList )pattern.InSet.Clone();
			arrayOutSet = ( ArrayList )pattern.OutSet.Clone();
			nPatternID = pattern.PatternID;
		}


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

			try
			{
				arrayInSet[ nIndex ] = dValue;
			}
			catch( ArgumentOutOfRangeException argorExp )
			{
				arrayInSet.Add( dValue );
				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>
		/// set the output value
		/// </summary>
		/// <param name="nIndex"></param>
		/// <param name="dValue"></param>
		public void SetOutValue( int nIndex, double dValue )
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Setting the out value at " + nIndex.ToString() + " to " + dValue.ToString(), ClassName );
			}

			try
			{
				arrayOutSet[ nIndex ] = dValue;
			}
			catch( ArgumentOutOfRangeException argorExp )
			{
				arrayOutSet.Add( dValue );
				if( debugLevel.TestDebugLevel( DebugLevelSet.Warning ) == true )
				{
					log.Log( DebugLevelSet.Warning, "The index for the array set out 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 double InputValue( int nIndex )
		{
			if( debugLevel.TestDebugLevel( DebugLevelSet.Progress ) == true )
			{
				log.Log( DebugLevelSet.Progress, "Getting the input value at " + nIndex.ToString(), ClassName );
			}

			if( nIndex < arrayInSet.Count )
				return ( double )arrayInSet[ 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 0.0;
			}
		}

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

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

				return 0.0;
			}
		}

		public int InputSize()
		{
			return arrayInSet.Count;
		}

		public int OutputSize()
		{
			return arrayOutSet.Count;
		}


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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions