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

AForge.NET open source framework

, 16 May 2007 GPL3
The article describes an open source C# framework for researchers in the areas of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, etc.
aforge_demo.zip
aforge_demo
Docs
AForge.Controls.chm
AForge.Core.chm
AForge.Genetic.chm
AForge.Imaging.chm
AForge.Math.chm
AForge.Neuro.chm
Release
AForge.Controls.dll
AForge.dll
AForge.Genetic.dll
AForge.Imaging.dll
AForge.Math.dll
AForge.Neuro.dll
Samples
Genetic
Approximation
AForge.Controls.dll
AForge.dll
AForge.Genetic.dll
Approximation.exe
Data Samples
sample1.csv
sample2.csv
Optimization1D
AForge.Controls.dll
AForge.dll
AForge.Genetic.dll
Optimization1D.exe
TimeSeries
AForge.Controls.dll
AForge.dll
AForge.Genetic.dll
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
TimeSeries.exe
TSP
AForge.Controls.dll
AForge.dll
AForge.Genetic.dll
TSP.exe
Imaging
FiltersDemo
AForge.dll
AForge.Imaging.dll
AForge.Math.dll
FiltersDemo.exe
TexturesDemo
AForge.dll
AForge.Imaging.dll
AForge.Math.dll
TexturesDemo.exe
Neuro
Back Propagation
Approximation
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Approximation.exe
Data Samples
sample1.csv
sample2.csv
TimeSeries
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
TimeSeries.exe
XORProblem
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
XORProblem.exe
Simple
Delta Rule Learning
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
and.csv
cube.csv
or.csv
sample1.csv
sample2.csv
One-Layer Perceptron Classifier
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
sample1.csv
sample2.csv
Perceptron Classifier
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
and.csv
cube.csv
or.csv
SOM
2DOrganizing
2DOrganizing.exe
AForge.dll
AForge.Neuro.dll
Color
AForge.dll
AForge.Neuro.dll
Color.exe
TSP
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
TSP.exe
aforge_src.zip
aforge_src
Samples
Genetic
Approximation
App.ico
Data Samples
sample1.csv
sample2.csv
Optimization1D
App.ico
TimeSeries
App.ico
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
TSP
App.ico
Imaging
FiltersDemo
App.ico
TexturesDemo
Properties
Neuro
Back Propagation
Approximation
App.ico
Data Samples
sample1.csv
sample2.csv
TimeSeries
App.ico
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
XORProblem
App.ico
Simple
Delta Rule Learning
App.ico
Data Samples
and.csv
cube.csv
or.csv
sample1.csv
sample2.csv
One-Layer Perceptron Classifier
App.ico
Data Samples
sample1.csv
sample2.csv
Perceptron Classifier
App.ico
Data Samples
and.csv
cube.csv
or.csv
SOM
2DOrganizing
App.ico
Color
App.ico
TSP
App.ico
Sources
Controls
bin
Debug
obj
Debug
TempPE
Core
bin
Debug
obj
Debug
TempPE
Docs Projects
AForge.Controls.shfb
AForge.Core.shfb
AForge.Genetic.shfb
AForge.Imaging.shfb
AForge.Math.shfb
AForge.Neuro.shfb
Genetic
bin
Debug
Chromosomes
GP
Fitness Functions
obj
Debug
TempPE
Selection Algorithms
Imaging
bin
Debug
Filters
2 Source filters
Adaptive Binarization
Base classes
Binarization
Color Filters
Convolution
Edge Detectors
HSL Filters
Morphology
Other
Transform
YCbCr Filters
Images
sample1.jpg
sepia.jpg
obj
Debug
TempPE
Textures
Math
bin
Debug
obj
Debug
TempPE
Neuro
Activation Functions
bin
Debug
Images
sigmoid.bmp
sigmoid_bipolar.bmp
threshold.bmp
Layers
Learning
Networks
Neurons
obj
Debug
TempPE
// AForge Framework
// Approximation (Symbolic Regression) using Genetic Programming and Gene Expression Programming
//
// Copyright � Andrew Kirillov, 2006
// andrew.kirillov@gmail.com
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Threading;

using AForge;
using AForge.Genetic;
using AForge.Controls;

namespace Approximation
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.ListView dataList;
		private System.Windows.Forms.ColumnHeader xColumnHeader;
		private System.Windows.Forms.ColumnHeader yColumnHeader;
		private System.Windows.Forms.Button loadDataButton;
		private System.Windows.Forms.OpenFileDialog openFileDialog;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.GroupBox groupBox2;
		private AForge.Controls.Chart chart;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox populationSizeBox;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox selectionBox;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox iterationsBox;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button startButton;
		private System.Windows.Forms.Button stopButton;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox currentIterationBox;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox currentErrorBox;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox functionsSetBox;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.ComboBox geneticMethodBox;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.TextBox solutionBox;

		private double[,] data = null;

		private int populationSize = 40;
		private int iterations = 100;
		private int selectionMethod = 0;
		private int functionsSet = 0;
		private int geneticMethod = 0;

		private Thread	workerThread = null;
        private volatile bool needToStop = false;

		// Constructor
		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			chart.AddDataSeries( "data", Color.Red, Chart.SeriesType.Dots, 5 );
			chart.AddDataSeries( "solution", Color.Blue, Chart.SeriesType.Line, 1 );

			selectionBox.SelectedIndex		= selectionMethod;
			functionsSetBox.SelectedIndex	= functionsSet;
			geneticMethodBox.SelectedIndex	= geneticMethod;
			UpdateSettings( );
		}

		/// <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()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.dataList = new System.Windows.Forms.ListView();
			this.xColumnHeader = new System.Windows.Forms.ColumnHeader();
			this.yColumnHeader = new System.Windows.Forms.ColumnHeader();
			this.loadDataButton = new System.Windows.Forms.Button();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.chart = new AForge.Controls.Chart();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.geneticMethodBox = new System.Windows.Forms.ComboBox();
			this.label8 = new System.Windows.Forms.Label();
			this.functionsSetBox = new System.Windows.Forms.ComboBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.iterationsBox = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.selectionBox = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.populationSizeBox = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.startButton = new System.Windows.Forms.Button();
			this.stopButton = new System.Windows.Forms.Button();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.currentErrorBox = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.currentIterationBox = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.groupBox5 = new System.Windows.Forms.GroupBox();
			this.solutionBox = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.dataList);
			this.groupBox1.Controls.Add(this.loadDataButton);
			this.groupBox1.Location = new System.Drawing.Point(10, 10);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(180, 310);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Data";
			// 
			// dataList
			// 
			this.dataList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this.xColumnHeader,
																					   this.yColumnHeader});
			this.dataList.FullRowSelect = true;
			this.dataList.GridLines = true;
			this.dataList.Location = new System.Drawing.Point(10, 20);
			this.dataList.Name = "dataList";
			this.dataList.Size = new System.Drawing.Size(160, 255);
			this.dataList.TabIndex = 0;
			this.dataList.View = System.Windows.Forms.View.Details;
			// 
			// xColumnHeader
			// 
			this.xColumnHeader.Text = "X";
			// 
			// yColumnHeader
			// 
			this.yColumnHeader.Text = "Y";
			// 
			// loadDataButton
			// 
			this.loadDataButton.Location = new System.Drawing.Point(10, 280);
			this.loadDataButton.Name = "loadDataButton";
			this.loadDataButton.TabIndex = 1;
			this.loadDataButton.Text = "&Load";
			this.loadDataButton.Click += new System.EventHandler(this.loadDataButton_Click);
			// 
			// openFileDialog
			// 
			this.openFileDialog.Filter = "CSV (Comma delimited) (*.csv)|*.csv";
			this.openFileDialog.Title = "Select data file";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.chart);
			this.groupBox2.Location = new System.Drawing.Point(200, 10);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(300, 310);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Function";
			// 
			// chart
			// 
			this.chart.Location = new System.Drawing.Point(10, 20);
			this.chart.Name = "chart";
			this.chart.Size = new System.Drawing.Size(280, 280);
			this.chart.TabIndex = 0;
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.geneticMethodBox);
			this.groupBox3.Controls.Add(this.label8);
			this.groupBox3.Controls.Add(this.functionsSetBox);
			this.groupBox3.Controls.Add(this.label7);
			this.groupBox3.Controls.Add(this.label4);
			this.groupBox3.Controls.Add(this.iterationsBox);
			this.groupBox3.Controls.Add(this.label3);
			this.groupBox3.Controls.Add(this.selectionBox);
			this.groupBox3.Controls.Add(this.label2);
			this.groupBox3.Controls.Add(this.populationSizeBox);
			this.groupBox3.Controls.Add(this.label1);
			this.groupBox3.Location = new System.Drawing.Point(510, 10);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(185, 198);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Settings";
			// 
			// geneticMethodBox
			// 
			this.geneticMethodBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.geneticMethodBox.Items.AddRange(new object[] {
																  "GP",
																  "GEP"});
			this.geneticMethodBox.Location = new System.Drawing.Point(110, 95);
			this.geneticMethodBox.Name = "geneticMethodBox";
			this.geneticMethodBox.Size = new System.Drawing.Size(65, 21);
			this.geneticMethodBox.TabIndex = 7;
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(10, 97);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(88, 16);
			this.label8.TabIndex = 6;
			this.label8.Text = "Genetic method:";
			// 
			// functionsSetBox
			// 
			this.functionsSetBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.functionsSetBox.Items.AddRange(new object[] {
																 "Simple",
																 "Extended"});
			this.functionsSetBox.Location = new System.Drawing.Point(110, 70);
			this.functionsSetBox.Name = "functionsSetBox";
			this.functionsSetBox.Size = new System.Drawing.Size(65, 21);
			this.functionsSetBox.TabIndex = 5;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(10, 72);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(80, 16);
			this.label7.TabIndex = 4;
			this.label7.Text = "Functions set:";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.label4.Location = new System.Drawing.Point(125, 175);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 15);
			this.label4.TabIndex = 10;
			this.label4.Text = "( 0 - inifinity )";
			// 
			// iterationsBox
			// 
			this.iterationsBox.Location = new System.Drawing.Point(125, 155);
			this.iterationsBox.Name = "iterationsBox";
			this.iterationsBox.Size = new System.Drawing.Size(50, 20);
			this.iterationsBox.TabIndex = 9;
			this.iterationsBox.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(10, 157);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(60, 16);
			this.label3.TabIndex = 8;
			this.label3.Text = "Iterations:";
			// 
			// selectionBox
			// 
			this.selectionBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.selectionBox.Items.AddRange(new object[] {
															  "Elite",
															  "Rank",
															  "Roulette"});
			this.selectionBox.Location = new System.Drawing.Point(110, 45);
			this.selectionBox.Name = "selectionBox";
			this.selectionBox.Size = new System.Drawing.Size(65, 21);
			this.selectionBox.TabIndex = 3;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(10, 47);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Selection method:";
			// 
			// populationSizeBox
			// 
			this.populationSizeBox.Location = new System.Drawing.Point(125, 20);
			this.populationSizeBox.Name = "populationSizeBox";
			this.populationSizeBox.Size = new System.Drawing.Size(50, 20);
			this.populationSizeBox.TabIndex = 1;
			this.populationSizeBox.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(10, 22);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(85, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Population size:";
			// 
			// startButton
			// 
			this.startButton.Enabled = false;
			this.startButton.Location = new System.Drawing.Point(530, 297);
			this.startButton.Name = "startButton";
			this.startButton.TabIndex = 3;
			this.startButton.Text = "&Start";
			this.startButton.Click += new System.EventHandler(this.startButton_Click);
			// 
			// stopButton
			// 
			this.stopButton.Enabled = false;
			this.stopButton.Location = new System.Drawing.Point(620, 297);
			this.stopButton.Name = "stopButton";
			this.stopButton.TabIndex = 4;
			this.stopButton.Text = "S&top";
			this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.currentErrorBox);
			this.groupBox4.Controls.Add(this.label6);
			this.groupBox4.Controls.Add(this.currentIterationBox);
			this.groupBox4.Controls.Add(this.label5);
			this.groupBox4.Location = new System.Drawing.Point(510, 216);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(185, 75);
			this.groupBox4.TabIndex = 5;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "Current iteration";
			// 
			// currentErrorBox
			// 
			this.currentErrorBox.Location = new System.Drawing.Point(125, 45);
			this.currentErrorBox.Name = "currentErrorBox";
			this.currentErrorBox.ReadOnly = true;
			this.currentErrorBox.Size = new System.Drawing.Size(50, 20);
			this.currentErrorBox.TabIndex = 3;
			this.currentErrorBox.Text = "";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(10, 47);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(70, 16);
			this.label6.TabIndex = 2;
			this.label6.Text = "Error:";
			// 
			// currentIterationBox
			// 
			this.currentIterationBox.Location = new System.Drawing.Point(125, 20);
			this.currentIterationBox.Name = "currentIterationBox";
			this.currentIterationBox.ReadOnly = true;
			this.currentIterationBox.Size = new System.Drawing.Size(50, 20);
			this.currentIterationBox.TabIndex = 1;
			this.currentIterationBox.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(10, 22);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(70, 16);
			this.label5.TabIndex = 0;
			this.label5.Text = "Iteration:";
			// 
			// groupBox5
			// 
			this.groupBox5.Controls.Add(this.solutionBox);
			this.groupBox5.Location = new System.Drawing.Point(10, 330);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(685, 50);
			this.groupBox5.TabIndex = 6;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "Solution:";
			// 
			// solutionBox
			// 
			this.solutionBox.Location = new System.Drawing.Point(10, 20);
			this.solutionBox.Name = "solutionBox";
			this.solutionBox.ReadOnly = true;
			this.solutionBox.Size = new System.Drawing.Size(665, 20);
			this.solutionBox.TabIndex = 0;
			this.solutionBox.Text = "";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(704, 390);
			this.Controls.Add(this.groupBox5);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.stopButton);
			this.Controls.Add(this.startButton);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "MainForm";
			this.Text = "Approximation (Symbolic Regression) using Genetic Programming and Gene Expression" +
				" Programming";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox5.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

        // Delegates to enable async calls for setting controls properties
        private delegate void SetTextCallback( System.Windows.Forms.Control control, string text );

        // Thread safe updating of control's text property
        private void SetText( System.Windows.Forms.Control control, string text )
        {
            if ( control.InvokeRequired )
            {
                SetTextCallback d = new SetTextCallback( SetText );
                Invoke( d, new object[] { control, text } );
            }
            else
            {
                control.Text = text;
            }
        }

		// On main form closing
		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// check if worker thread is running
			if ( ( workerThread != null ) && ( workerThread.IsAlive ) )
			{
				needToStop = true;
                while ( !workerThread.Join( 100 ) )
                    Application.DoEvents( );
            }
		}

		// Update settings controls
		private void UpdateSettings( )
		{
			populationSizeBox.Text		= populationSize.ToString( );
			iterationsBox.Text			= iterations.ToString( );
		}

		// Load data
		private void loadDataButton_Click(object sender, System.EventArgs e)
		{
			// show file selection dialog
			if ( openFileDialog.ShowDialog( ) == DialogResult.OK )
			{
				StreamReader reader = null;
				// read maximum 50 points
				double[,] tempData = new double[50, 2];
				double minX = double.MaxValue;
				double maxX = double.MinValue;

				try
				{
					// open selected file
					reader = File.OpenText( openFileDialog.FileName );
					string	str = null;
					int		i = 0;

					// read the data
					while ( ( i < 50 ) && ( ( str = reader.ReadLine( ) ) != null ) )
					{
						string[] strs = str.Split( ';' );
						if ( strs.Length == 1 )
							strs = str.Split( ',' );
						// parse X
						tempData[i, 0] = double.Parse( strs[0] );
						tempData[i, 1] = double.Parse( strs[1] );

						// search for min value
						if ( tempData[i, 0] < minX )
							minX = tempData[i, 0];
						// search for max value
						if ( tempData[i, 0] > maxX )
							maxX = tempData[i, 0];

						i++;
					}

					// allocate and set data
					data = new double[i, 2];
					Array.Copy( tempData, 0, data, 0, i * 2 );
				}
				catch (Exception)
				{
					MessageBox.Show( "Failed reading the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
					return;
				}
				finally
				{
					// close file
					if ( reader != null )
						reader.Close( );
				}

				// update list and chart
				UpdateDataListView( );
				chart.RangeX = new DoubleRange( minX, maxX );
				chart.UpdateDataSeries( "data", data );
				chart.UpdateDataSeries( "solution", null );
				// enable "Start" button
				startButton.Enabled = true;
			}
		}

		// Update data in list view
		private void UpdateDataListView( )
		{
			// remove all current records
			dataList.Items.Clear( );
			// add new records
			for ( int i = 0, n = data.GetLength( 0 ); i < n; i++ )
			{
				dataList.Items.Add( data[i, 0].ToString( ) );
				dataList.Items[i].SubItems.Add( data[i, 1].ToString( ) );
			}
		}

        // Delegates to enable async calls for setting controls properties
        private delegate void EnableCallback( bool enable );

        // Enable/disale controls (safe for threading)
        private void EnableControls( bool enable )
		{
            if ( InvokeRequired )
            {
                EnableCallback d = new EnableCallback( EnableControls );
                Invoke( d, new object[] { enable } );
            }
            else
            {
                loadDataButton.Enabled      = enable;
                populationSizeBox.Enabled   = enable;
                iterationsBox.Enabled       = enable;
                selectionBox.Enabled        = enable;
                functionsSetBox.Enabled     = enable;
                geneticMethodBox.Enabled    = enable;

                startButton.Enabled         = enable;
                stopButton.Enabled          = !enable;
            }
 		}
		
		// On button "Start"
		private void startButton_Click(object sender, System.EventArgs e)
		{
			solutionBox.Text = string.Empty;

			// get population size
			try
			{
				populationSize = Math.Max( 10, Math.Min( 100, int.Parse( populationSizeBox.Text ) ) );
			}
			catch
			{
				populationSize = 40;
			}
			// iterations
			try
			{
				iterations = Math.Max( 0, int.Parse( iterationsBox.Text ) );
			}
			catch
			{
				iterations = 100;
			}
			// update settings controls
			UpdateSettings( );

			selectionMethod	= selectionBox.SelectedIndex;
			functionsSet	= functionsSetBox.SelectedIndex;
			geneticMethod	= geneticMethodBox.SelectedIndex;
		
			// disable all settings controls except "Stop" button
			EnableControls( false );

			// run worker thread
			needToStop = false;
			workerThread = new Thread( new ThreadStart( SearchSolution ) );
            
			workerThread.Start( );
		}

		// On button "Stop"
		private void stopButton_Click( object sender, System.EventArgs e )
		{
			// stop worker thread
			needToStop = true;
            while ( !workerThread.Join( 100 ) )
                Application.DoEvents( );
			workerThread = null;
		}

		// Worker thread
		void SearchSolution( )
		{
			// create fitness function
			SymbolicRegressionFitness fitness = new SymbolicRegressionFitness( data, new double[] { 1, 2, 3, 5, 7 } );
			// create gene function
			IGPGene gene = ( functionsSet == 0 ) ?
				(IGPGene) new SimpleGeneFunction( 6 ) :
				(IGPGene) new ExtendedGeneFunction( 6 );
			// create population
			Population population = new Population( populationSize,
				( geneticMethod == 0 ) ?
					(IChromosome) new GPTreeChromosome( gene ) :
					(IChromosome) new GEPChromosome( gene, 15 ),
				fitness,
				( selectionMethod == 0 ) ? (ISelectionMethod) new EliteSelection( ) :
				( selectionMethod == 1 ) ? (ISelectionMethod) new RankSelection( ) :
										   (ISelectionMethod) new RouletteWheelSelection( )
				);
			// iterations
			int i = 1;
			// solution array
			double[,]	solution = new double[50, 2];
			double[]	input = new double[6] { 0, 1, 2, 3, 5, 7 };

			// calculate X values to be used with solution function
			for ( int j = 0; j < 50; j++ )
			{
				solution[j, 0] = chart.RangeX.Min + (double) j * chart.RangeX.Length / 49;
			}

			// loop
			while ( !needToStop )
			{
				// run one epoch of genetic algorithm
				population.RunEpoch( );

				try
				{
					// get best solution
					string bestFunction = population.BestChromosome.ToString( );

					// calculate best function
					for ( int j = 0; j < 50; j++ )
					{
						input[0] = solution[j, 0];
						solution[j, 1] = PolishExpression.Evaluate( bestFunction, input );
					}
					chart.UpdateDataSeries( "solution", solution );
					// calculate error
					double error = 0.0;
					for ( int j = 0, k = data.GetLength( 0 ); j < k; j++ )
					{
						input[0] = data[j, 0];
						error += Math.Abs( data[j, 1] - PolishExpression.Evaluate( bestFunction, input ) );
					}

					// set current iteration's info
                    SetText( currentIterationBox, i.ToString( ) );
                    SetText( currentErrorBox, error.ToString( "F3" ) );
				}
				catch
				{
					// remove any solutions from chart in case of any errors
					chart.UpdateDataSeries( "solution", null );
				}

				// increase current iteration
				i++;

				//
				if ( ( iterations != 0 ) && ( i > iterations ) )
					break;
			}

			// show solution
            SetText( solutionBox, population.BestChromosome.ToString( ) );

			// enable settings controls
			EnableControls( true );
		}
	}
}

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)

Share

About the Author

Andrew Kirillov
Software Developer (Senior) Cisco Systems
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.
 
Going out of computers I am just a man loving his family, enjoying traveling, a bit of books, a bit of movies and a mixture of everything else. Always wanted to learn playing guitar, but it seems like 6 strings are much harder than few dozens of keyboard’s keys. Will keep progressing ...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 16 May 2007
Article Copyright 2006 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid