【问题标题】:Genetic Programming Implementation遗传编程实现
【发布时间】:2010-10-31 05:25:32
【问题描述】:

我熟悉遗传编程的一般性,但我想知道在哪里可以找到一些可以向我展示实现遗传编程的细节的东西。我使用 C# 和 .NET 3.5,我想将遗传编程用于寻路之类的事情,通常只是想看看它能做什么。 编辑:我可能应该澄清我在寻找什么:我对什么样的数据结构将用于存储语法树、如何执行繁殖操作等感兴趣。

【问题讨论】:

    标签: c# implementation genetic-programming


    【解决方案1】:

    以下是对帮助我学习基因编程的C++ HelloWorld 示例之一的快速重写:

    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 + ")");
        }
    

    可能有一些小错误,但其他方面看起来一切正常。它也可以用 C# 的精神写得更好,但这些只是细节。 :)

    【讨论】:

      【解决方案2】:

      Roger Alsing 的蒙娜丽莎项目就是一个很好的例子。 http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/

      编辑:我喜欢这个例子的原因是因为它相当小而且容易理解。它是掌握遗传编程概念的一种快速简便的方法。

      【讨论】:

      • 非常有趣,这可能是我应用遗传编程实现的用途之一,但我正在寻找有关如何将高级理论转化为代码的具体细节执行所需的任务。
      • 感谢您的回答,但我已经理解了这个概念。我正在寻找一种将遗传编程的高级概念转化为代码中特定结构的方法,这些结构可用于实现遗传编程的各个部分。
      • 蒙娜丽莎计划令人印象深刻,但它并不是真正的基因编程。
      【解决方案3】:

      你可以看看Survival of the Fittest: Natural Selection with Windows Forms

      编辑:请参阅我刚刚找到的 previous SO question。这几乎是重复的。抱歉,您不理解该链接(最好在问题中提及此类内容)。此外,即使答案已被接受,其他问题仍可供更多答案/编辑。

      【讨论】:

      • 谢谢,但这两个我都看过。两者都没有真正帮助,因为 MSDN 文章是用 CodeDOM 编写的,这对我来说有点难以理解,而之前的 SO 帖子基本上是指向同一篇 MSDN 文章的链接。我更喜欢现代一点的东西;也许与 lambdas?再次感谢您的回答。
      【解决方案4】:

      您可以尝试 Sean Luke 的 ECJ(Java 中的进化计算)的 C# .NET 4.0 端口:

      http://branecloud.codeplex.com

      它是非常灵活和强大的软件!但它也相对容易上手,因为它包含许多开箱即用的工作控制台示例(以及在转换过程中开发的许多有用的单元测试)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-08
        • 2011-07-09
        • 1970-01-01
        • 1970-01-01
        • 2014-11-25
        • 2015-10-24
        • 2011-09-13
        相关资源
        最近更新 更多