views:

455

answers:

3

I am familiar with the generalities of genetic programming but am wondering where i might find something that shows me details of implementing genetic programming. I use C# and .NET 3.5, and I would like to put to use genetic programming for things like pathfinding, and generally just want to see what it can do. EDIT: I should probably clarify what I'm looking for: I'm interested in what sort of data structures would be used to store the syntax trees, how a breeding operation might be performed, that sort of thing.

A: 

You could look at Survival of the Fittest: Natural Selection with Windows Forms.

EDIT: See this previous SO question, which I just found. It's pretty much a duplicate. Sorry you don't understand the link (it's good to mention such things in the question). Also, the other question is still open for more answers/edits, even though an answer has been accepted.

Matthew Flaschen
Thanks, but i've seen both of those. Neither really helped, since the MSDN article was written with CodeDOM which is a bit hard for me to understand and the previous SO post was basically a link to that same MSDN article. I'd prefer something a bit more modern; perhaps with lambdas? Again, thank you for answering though.
RCIX
+3  A: 

Roger Alsing's Mona Lisa project is a quite good example. http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/

EDIT: The reason I like the example is because it rather small and easy to understand. Its a quick and easy way to grasp the concept of genetic programming.

Carl Bergquist
Very interesting, and that could be one of the uses I apply my genetic programming implementation to, but I'm looking for specific details on how i might go about turning high-level theory into code that will perform the desired task.
RCIX
Thank you for answering, but i understand the concept already. I'm looking for a way to translate the high level concepts of genetic programming into specific structures in code that can be used to implement the various parts of genetic programming.
RCIX
The Mona Lisa program is impressive, but it's not really genetic programming.
Dan Dyer
+3  A: 

Here is a quick rewrite of one of C++ HelloWorld examples that helped me learn genetic programming:

using ga_vector = List<ga_struct>;

class ga_struct
{
    public ga_struct(string str, uint fitness)
    {
        Str = str;
        Fitness = fitness;
    }

    public string Str { get; set; }
    public uint Fitness { get; set; }
}

class Program
{

    private const int GA_POPSIZE = 2048;
    private const int GA_MAXITER = 16384;
    private const float GA_ELITRATE = 0.10f;
    private const float GA_MUTATIONRATE = 0.25f;
    private const float GA_MUTATION = 32767 * GA_MUTATIONRATE;
    private const string GA_TARGET = "Hello world!";

    private static readonly Random random = new Random((int)DateTime.Now.Ticks);

    static void Main(string[] args)
    {
        ga_vector popAlpha = new ga_vector();
        ga_vector popBeta = new ga_vector();

        InitPopulation(ref popAlpha, ref popBeta);
        ga_vector population = popAlpha;
        ga_vector buffer = popBeta;

        for (int i = 0; i < GA_MAXITER; i++)
        {
            CalcFitness(ref population);
            SortByFitness(ref population);
            PrintBest(ref population);

            if (population[0].Fitness == 0) break;

            Mate(ref population, ref buffer);
            Swap(ref population, ref buffer);
        }

        Console.ReadKey();
    }

    static void Swap(ref ga_vector population, ref ga_vector buffer)
    {
        var temp = population;
        population = buffer;
        buffer = temp;
    }

    static void InitPopulation(ref ga_vector population, ref ga_vector buffer)
    {
        int tsize = GA_TARGET.Length;
        for (int i = 0; i < GA_POPSIZE; i++)
        {
            var citizen = new ga_struct(string.Empty, 0);

            for (int j = 0; j < tsize; j++)
            {
                citizen.Str += Convert.ToChar(random.Next(90) + 32);
            }

            population.Add(citizen);
            buffer.Add(new ga_struct(string.Empty, 0));
        }
    }

    static void CalcFitness(ref ga_vector population)
    {
        const string target = GA_TARGET;
        int tsize = target.Length;

        for (int i = 0; i < GA_POPSIZE; i++)
        {
            uint fitness = 0;
            for (int j = 0; j < tsize; j++)
            {
                fitness += (uint) Math.Abs(population[i].Str[j] - target[j]);
            }

            population[i].Fitness = fitness;
        }
    }

    static int FitnessSort(ga_struct x, ga_struct y)
    {
        return x.Fitness.CompareTo(y.Fitness);
    }

    static void SortByFitness(ref ga_vector population)
    {
        population.Sort((x, y) => FitnessSort(x, y));
    }

    static void Elitism(ref ga_vector population, ref ga_vector buffer, int esize)
    {
        for (int i = 0; i < esize; i++)
        {
            buffer[i].Str = population[i].Str;
            buffer[i].Fitness = population[i].Fitness;
        }
    }

    static void Mutate(ref ga_struct member)
    {
        int tsize = GA_TARGET.Length;
        int ipos = random.Next(tsize);
        int delta = random.Next(90) + 32;

        var mutated = member.Str.ToCharArray();
        Convert.ToChar((member.Str[ipos] + delta)%123).ToString().CopyTo(0, mutated, ipos, 1);
        member.Str = mutated.ToString();
    }

    static void Mate(ref ga_vector population, ref ga_vector buffer)
    {
        const int esize = (int) (GA_POPSIZE*GA_ELITRATE);
        int tsize = GA_TARGET.Length, spos, i1, i2;

        Elitism(ref population, ref buffer, esize);

        for (int i = esize; i < GA_POPSIZE; i++)
        {
            i1 = random.Next(GA_POPSIZE/2);
            i2 = random.Next(GA_POPSIZE/2);
            spos = random.Next(tsize);

            buffer[i].Str = population[i1].Str.Substring(0, spos) + population[i2].Str.Substring(spos, tsize - spos);

            if (random.Next() < GA_MUTATION)
            {
                var mutated = buffer[i];
                Mutate(ref mutated);
                buffer[i] = mutated;
            }
        }
    }

    static void PrintBest(ref ga_vector gav)
    {
        Console.WriteLine("Best: " + gav[0].Str + " (" + gav[0].Fitness + ")");
    }

There might be some minor errors but otherwise it looks it's working ok. Also it could be written better in spirit of C# but those are just details. :)

msuvajac
Cool! thanks this ought to help a lot.
RCIX