C# 中的遗传编程

发布于 2024-07-04 06:15:47 字数 1560 浏览 9 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

半步萧音过轻尘 2024-07-11 06:15:47

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

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.

羞稚 2024-07-11 06:15:47

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

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

心意如水 2024-07-11 06:15:47

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

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

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

这样做的好处是,您只需要使用 1 个表示(AST 表示),该表示最适合执行和遗传算法“再现”步骤。

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

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

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.

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.

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.

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.

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 DynamicMethod class. 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.

情绪少女 2024-07-11 06:15:47

您可以尝试 GeneticSharp

它具有所有经典的遗传算法操作,如选择、交叉、变异、重新插入和终止。

它具有很强的可扩展性,您也可以定义自己的染色体、适应度函数、种群生成策略以及上面引用的所有操作。

它可以在多种类型的应用程序中使用,例如 C# 库和 Unity 3D 游戏,有一些示例在 GTK# 应用Unity 3D 跳棋游戏

它也适用于 Win 和 OSX。

这是如何使用该库的基本示例:

var selection = new EliteSelection();
var crossover = new OrderedCrossover();
var mutation = new ReverseSequenceMutation();
var fitness = new YourFitnessFunction();
var chromosome = new YourChromosome();
var population = new Population (50, 70, chromosome);

var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);

ga.Start();

You can try GeneticSharp.

It has all classic GA operations, like selection, crossover, mutation, reinsertion and termination.

It's very extensible, you can define your own chromosomes, fitness function, population generation strategy and all cited operations above too.

It can be used in many kind of apps, like C# libraries and Unity 3D games, there is samples running it in a GTK# app and Unity 3D checkers game.

It also works in Win and OSX.

Here is a basic sample how to use the library:

var selection = new EliteSelection();
var crossover = new OrderedCrossover();
var mutation = new ReverseSequenceMutation();
var fitness = new YourFitnessFunction();
var chromosome = new YourChromosome();
var population = new Population (50, 70, chromosome);

var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);

ga.Start();
赤濁 2024-07-11 06:15:47

Manning 的书:“.NET 中的元编程”致力于通过表达式树介绍 GP 的很大一部分。

The Manning book: "Metaprogramming in .NET" dedicates a large section on GP via expression trees.

梦巷 2024-07-11 06:15:47

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

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

撑一把青伞 2024-07-11 06:15:47

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

I am reading A Field Guide to Genetic Programming right 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#.

诺曦 2024-07-11 06:15:47

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

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

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

http://branecloud.codeplex.com

我也在这个过程中将其他几个与“人工智能”研究相关的框架从 Java 转换为 .NET(当我有时间的时候)。

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.

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).

Anyway, you can download the initial conversion here:

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).

Ben

此生挚爱伱 2024-07-11 06:15:47

您指的是实际的遗传编程,而不是一般的遗传算法吗?

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

但是,如果必须的话,您可能会想要动态生成 CIL / MSIL。 您可以使用 System.Reflection.Emit 来执行此操作,但是我推荐 Mono.Cecil。 它缺乏好的文档(就好像反射发射有它们一样)。但它提供了更好的组件发射和反射。

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

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.

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.

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.

沫尐诺 2024-07-11 06:15:47

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

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

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.

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

多情癖 2024-07-11 06:15:47

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文