Click here to Skip to main content
Licence 
First Posted 27 Feb 2005
Views 56,770
Bookmarked 38 times

A simpler C# genetic algorithm

By | 27 Feb 2005 | Article
A simple demonstration of an algorithm making good use of C# 2.0

Sample Image - double f3 = (x * x * x + y * y * y + 3 * x * y);

Introduction

This article deals with enhancements for the Simple C# Genetic Algorithm by Barry Lapthorn. It makes an extensive use of "foreach" loop and .NET2.0 C# language improvements, so the .NET 2.0 framework or higher is needed.

The program does the same thing and gives the same results as Barry's one.

My Modifications :

  • use of generic List.
    Allows the use of type safe Collections.
  • use of the 'foreach' statement whenever possible.
    It exists since .NET 1.0
  • use of a deep copy of a 'Genome' object for 'elitism'
    Here I needed a copy of a Genome object to cope with elitism (a concept of Genetic Programming). I didn't find anything useful in the .NET 2.0 framework so I coded my own method of Genome.DeepCopy which performs a full-blown copy of a Genome object. i.e. it returns a new Genome object whose fields are the exact reproduction of the original, but the two Genome objects don't share any fields or value in common (in the sense that duplication of a Genome IS NOT a pointer to a former object).
  • use of delegate for the sort (no more need of a 'GenomeComparer' class) 
     m_thisGeneration.Sort(delegate(Genome x, Genome y) 
    
         { return Comparer<DOUBLE>.Default.Compare(x.Fitness, y.Fitness); });
    To my mind, this is the most powerfull enhancement. Here the code uses an anonymous function (with the delegate keyword) to perform the sort. This makes the code much shorter and clearer and avoids using a GenomeComparer helper class.

Improvements that can be done

The scaling of the GA parameters, ie allowing other mathematical functions to work with the GA (Genetic Algorithm) not only those with values between 0 and 1 (double floating point precision).

For example :

double f3 = (x * x * x + y * y * y + 3 * x * y); 

I encourage you to follow this link to Barry Lapthorn's site : http://www.lapthorn.net/

I made this to learn visual c# express 2005. I hope someone will find some elements interresting in my work. Nevertheless, I don't pretend to be a top-level OO Programmer :-)

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

About the Author

Lionel Monnier



France France

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA few Issues PinmemberMember 33176598:48 6 Feb '10  
GeneralIMPORTENT: Help me for Senoir Project on 14 JUN, 2008 PinmemberSalmaAlnajim8:25 7 Jan '08  
GeneralImage Enhancement PinmemberVPNampoothiri1:15 8 May '07  
Generalquestion Pinmemberjenif23:42 19 Jan '07  
AnswerRe: question PinmemberVPoke4:22 20 Jan '07  
Hi !
 
I'm sorry but I have a very little genetic programming background and other priorities at the time now.
 
Hope you find what you need elsewhere.
 
Regards
 
Lionel Monnier
GeneralRastrigin's Function PinmemberAustin Kalb6:29 22 Jun '06  
GeneralChange genome values Pinmembercreg douglaus4:58 15 May '05  
GeneralRe: Change genome values Pinmemberyann_lh3:49 14 Sep '05  
GeneralDeep copying objects PinmemberDaniel Cazzulino [MVP XML]20:07 1 Mar '05  
GeneralRe: Deep copying objects PinmemberLionel Monnier4:29 2 Mar '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 27 Feb 2005
Article Copyright 2005 by Lionel Monnier
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid