C#中的遗传编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:47:05  来源:igfitidea点击:

Genetic Programming in C#

提问by Mac

I've been looking for some good genetic programming examples for C#. Anyone knows of good online/book resources? Wonder if there is a C# library out there for Evolutionary/Genetic programming?

我一直在为 C# 寻找一些好的遗传编程示例。任何人都知道好的在线/书籍资源?想知道是否有用于进化/遗传编程的 C# 库?

采纳答案by Jader Dias

After developing my own Genetic Programming didactic application, I found a complete Genetic Programming Framework called AForge.NET Genetics. It's a part of the Aforge.NET library. It's licensed under LGPL.

在开发了我自己的遗传编程教学应用程序之后,我找到了一个完整的遗传编程框架,称为AForge.NET Genetics。它是Aforge.NET 库的一部分。它是根据 LGPL 许可的。

回答by Judah Gabriel Himango

MSDN had an article last year about genetic programming: Genetic Algorithms: Survival of the Fittest with Windows Forms

MSDN 去年有一篇关于遗传编程的文章:遗传算法:Windows Forms 的适者生存

回答by mgsloan

Do you mean actual genetic programming, as opposed to genetic algorithms in general?

你的意思是实际的遗传编程,而不是一般的遗传算法?

If so, C#/.net isn't the best language for it. LISP, for example, has always been a mainstay of GP.

如果是这样,C#/.net 不是最好的语言。例如,LISP 一直是 GP 的支柱。

However, if you must, you're probably going to want to dynamically generate CIL / MSIL. You could do this using System.Reflection.Emit, however I'd recommend Mono.Cecil. It lacks good docs (as if reflection emit has them).. But it offers much better assembly emission and reflection.

但是,如果必须,您可能希望动态生成 CIL/MSIL。您可以使用System.Reflection.Emit来做到这一点,但我建议使用Mono.Cecil。它缺乏好的文档(好像反射发射有它们一样)。但它提供了更好的装配发射和反射。

Another issue is that it is less than trivial to load code, and later dispose of it, in the .net framework. At least, you cannot unload assemblies. You can unload appdomains, but the whole business of loading code into a seperate appdomain, and calling it externally can get pretty messy. .NET 3.5's System.Addin stuff should make this easier.

另一个问题是,在 .net 框架中加载代码并稍后处理它并非易事。至少,您不能卸载程序集。您可以卸载应用程序域,但是将代码加载到单独的应用程序域并在外部调用它的整个业务可能会变得非常混乱。.NET 3.5 的 System.Addin 内容应该使这更容易。

回答by Curt Hagenlocher

You might be able to implement genetic programming using LINQ expression trees -- it's more likely to generate something usable than random IL generation.

您可能能够使用 LINQ 表达式树来实现遗传编程——它比随机 IL 生成更有可能生成一些可用的东西。

回答by Scott Wisniewski

I would recommend against actually generating assemblies unless you absolutely need to, particularly if you are just getting started with implementing the genetic algorithm.

我建议不要实际生成程序集,除非您绝对需要,特别是如果您刚刚开始实施遗传算法。

The genetic algorithm is easiest to implement when the target language is functional and dynamically typed. That is generally why most genetic algorithm research is written in LISP. As a result, if you are going to implement it in C#, you are probably better off defining your own mini "tree language", having the algorithm generate trees, and just interpreting the trees when it comes time to run each iteration of the algorithm.

当目标语言具有功能性和动态类型时,遗传算法最容易实现。这就是为什么大多数遗传算法研究都是用 LISP 编写的。因此,如果你打算在 C# 中实现它,你可能最好定义自己的迷你“树语言”,让算法生成树,并在运行算法的每次迭代时解释树.

I did a project like this when I was in college (an implementation of the genetic algorithm in C#), and that was the approach I took.

我在大学时做过一个这样的项目(用 C# 实现遗传算法),这就是我采用的方法。

Doing it that way will give you the advantage of only having 1 representation to work with (the AST representation) that is optimally suited for both execution and the genetic algorithm "reproduction" steps.

这样做会给你带来只有 1 个表示(AST 表示)的优势,它最适合执行和遗传算法“复制”步骤。

Alternatively, if you try to generate assemblies you are probably going to end up adding a large amount of unneeded complexity to the app. Currently, the CLR does not allow an assembly to be unloaded from an App domain unless the entire app domain is destroyed. This would mean that you would need to spin up a separate app domain for each generated program in each iteration of the algorithm to avoid introducing a giant memory leak into your app. In general, the whole thing would just add a bunch of extra irritation.

或者,如果您尝试生成程序集,您可能最终会向应用程序添加大量不必要的复杂性。目前,除非整个应用程序域被销毁,否则 CLR 不允许从应用程序域卸载程序集。这意味着您需要在算法的每次迭代中为每个生成的程序启动一个单独的应用程序域,以避免在您的应用程序中引入巨大的内存泄漏。一般来说,整件事只会增加一堆额外的刺激。

Interpreted AST's, on the other hand, are garbage collectible just like any other object, and so you wouldn't need to monkey around with multiple app domains. If, for performance reasons you want to code-gen the final result you can add support for that later. However, I you would recommend that you do that using the DynamicMethodclass. It will allow you to convert an AST into a compiled delegate dynamically at runtime. That will enable you to deploy a single DLL while keeping the code generation stuff as simple as possible. Also, DynamicMethod instances are garbage collectible so you could end up employing them as part of the genetic algorithm to speed things up there as well.

另一方面,解释型 AST 就像任何其他对象一样是可垃圾收集的,因此您无需在多个应用程序域中四处游荡。如果出于性能原因,您想对最终结果进行代码生成,则可以稍后添加对此的支持。但是,我建议您使用DynamicMethod类来执行此操作。它将允许您在运行时动态地将 AST 转换为已编译的委托。这将使您能够部署单个 DLL,同时保持代码生成内容尽可能简单。此外,DynamicMethod 实例是可垃圾回收的,因此您最终可以将它们用作遗传算法的一部分,以加快速度。

回答by Jason Hymanson

I am reading A Field Guide to Genetic Programmingright now (free PDF download). It is also available as a paperback. It discuses the use of a library written in Java called TinyGP. You might get some mileage out of that. I have not started doing any actual programming but am hoping to applies some of the concepts in C#.

我现在正在阅读《遗传编程实地指南》(免费 PDF 下载)。它也可以作为平装本使用。它讨论了使用 Java 编写的名为TinyGP的库。你可能会从中获得一些里程。我还没有开始做任何实际的编程,但我希望在 C# 中应用一些概念。

回答by Jason Hymanson

I maintain a port of ECJ in C#. It's great.

我在 C# 中维护了一个 ECJ 端口。这很棒。

回答by bstabile

I've forked ECJ to C# .NET 4.0 if you are interested in a full-featured Evolutionary Computation framework. The package includes everything from the original ECJ Java project, including all of the working samples.

如果您对全功能的进化计算框架感兴趣,我已经将 ECJ 分叉到 C# .NET 4.0。该包包含来自原始 ECJ Java 项目的所有内容,包括所有工作示例。

I also wrote 500 unit tests to verify many aspects of the conversion. But many more tests are needed. In particular, the distributed computation aspects are not fully tested. That's because I plan on converting from ECJ's simple use of sockets to a more robust strategy using WCF and WF. I'll also be reworking the framework to utilize TPL (Task Parallel Library).

我还编写了 500 个单元测试来验证转换的许多方面。但是还需要更多的测试。特别是,分布式计算方面没有经过全面测试。那是因为我计划从 ECJ 对套接字的简单使用转换为使用 WCF 和 WF 的更强大的策略。我还将重新设计框架以利用 TPL(任务并行库)。

Anyway, you can download the initial conversion here:

无论如何,您可以在此处下载初始转换:

http://branecloud.codeplex.com

http://branecloud.codeplex.com

I am also in the process of converting several other frameworks from Java to .NET that relate to "synthetic intelligence" research (when I can find the time).

我也在将其他几个框架从 Java 转换为 .NET,这些框架与“综合智能”研究相关(如果我能找到时间)。

Ben

回答by Andreas

If you're interested in genetic algorithms or heuristic optimization in general you might want to take a look at HeuristicLab. It is developed for several years, 1.5 years since we released the new version. It is programmed in C# 4 and has a nice GUI. There are many algorithms already available like Genetic Algorithm, Genetic Programming, Evolution Strategy, Local Search, Tabu Search, Particle Swarm Optimization, Simulated Annealing and more. There are also several problems implemented like a vehicle routing problem, traveling salesman, real function optimization, knapsack, quadratic assignment problem, classification, regression, and many more. There are tutorials also and we have protocol buffers integrated so you can communicate with external programs for solution evaluation. It is licensed under GPL. In 2009 the software has received the Microsoft innovation award of Microsoft Austria.

如果你对遗传算法或启发式优化感兴趣,你可能想看看HeuristicLab. 它已经开发了几年,自我们发布新版本以来已有 1.5 年。它是用 C# 4 编写的,并有一个很好的 GUI。已有许多算法可用,如遗传算法、遗传编程、进化策略、局部搜索、禁忌搜索、粒子群优化、模拟退火等。还实现了几个问题,如车辆路径问题、旅行商、实函数优化、背包、二次分配问题、分类、回归等等。还有教程,我们集成了协议缓冲区,因此您可以与外部程序通信以进行解决方案评估。它是根据 GPL 许可的。2009年该软件获得了微软奥地利的微软创新奖。

We've also written a book on the subject: Genetic Algorithms and Genetic Programming.

我们还写了一本关于这个主题的书:遗传算法和遗传编程