Click here to Skip to main content
15,891,841 members
Articles / Artificial Intelligence

Evolution computations on C#

Rate me:
Please Sign up or sign in to vote.
4.93/5 (73 votes)
15 Oct 2006GPL313 min read 271.6K   8.3K   194  
The articles describes a C# library for evolution computations and their application for several problems solving.
// AForge Genetic Library
//
// Copyright � Andrew Kirillov, 2006
// andrew.kirillov@gmail.com
//

namespace AForge.Genetic
{
	using System;

	/// <summary>
	/// Genetic programming gene, which represents simple arithmetic function
	/// </summary>
	public class SimpleGeneFunction : IGPGene
	{
		// supported functions list
		protected enum Functions
		{
			Add,
			Subtract,
			Multiply,
			Divide,
		}

		protected const int FunctionsCount = 4;

		// gene type
		private GPGeneType	type;
		// total amount of variables in the task which is supposed to be solved
		private int			variablesCount;
		//
		private int			val;
		
		// random number generator for chromosoms generation
		protected static Random	rand = new Random( (int) DateTime.Now.Ticks );

		
		/// <summary>
		/// Gene type
		/// </summary>
		public GPGeneType GeneType
		{
			get { return type; }
		}

		/// <summary>
		/// Arguments count
		/// </summary>
		public int ArgumentsCount
		{
			get { return ( type == GPGeneType.Argument ) ? 0 : 2; }
		}

		/// <summary>
		/// Maximum arguments count
		/// </summary>
		public int MaxArgumentsCount
		{
			get { return 2; }
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public SimpleGeneFunction( int variablesCount ) : this( variablesCount, true ) { }

		/// <summary>
		/// Constructor
		/// </summary>
		public SimpleGeneFunction( int variablesCount, GPGeneType type )
		{
			this.variablesCount = variablesCount;
			// generate the gene value
			Generate( type );
		}

		/// <summary>
		/// Constructor
		/// </summary>
		protected SimpleGeneFunction( int variablesCount, bool random )
		{
			this.variablesCount = variablesCount;
			// generate the gene value
			if ( random )
				Generate( );
		}

		/// <summary>
		/// Get string representation of the chromosome
		/// </summary>
		public override string ToString( )
		{
			if ( type == GPGeneType.Function )
			{
				// get function string representation
				switch ( (Functions) val )
				{
					case Functions.Add:			// addition
						return "+";

					case Functions.Subtract:	// subtraction
						return "-";

					case Functions.Multiply:	// multiplication
						return "*";

					case Functions.Divide:		// division
						return "/";
				}
			}

			// get argument string representation
			return string.Format( "${0}", val );
		}

		/// <summary>
		/// Clone the gene
		/// </summary>
		public IGPGene Clone( )
		{
			// create new gene ...
			SimpleGeneFunction clone = new SimpleGeneFunction( variablesCount, false );
			// ... with the same type and value
			clone.type	= type;
			clone.val	= val;

			return clone;
		}

		/// <summary>
		/// Generate random gene with random type
		/// </summary>
		public void Generate( )
		{
			// give more chance to function
			Generate( ( rand.Next( 4 ) == 3 ) ? GPGeneType.Argument : GPGeneType.Function );
		}

		/// <summary>
		/// Generate random gene with certain type
		/// </summary>
		public void Generate( GPGeneType type )
		{
			// gene type
			this.type = type;
			// gene value
			val = rand.Next( ( type == GPGeneType.Function ) ? FunctionsCount : variablesCount );
				
		}

		/// <summary>
		/// Creates new gene with random type
		/// </summary>
		public IGPGene CreateNew( )
		{
			return new SimpleGeneFunction( variablesCount );
		}

		/// <summary>
		/// Creates new gene with certain type
		/// </summary>
		public IGPGene CreateNew( GPGeneType type )
		{
			return new SimpleGeneFunction( variablesCount, type );
		}
	}
}

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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET, Computer Vision Sandbox, cam2web, ANNT, etc.

Comments and Discussions