Genetic Programming in C# [closed]

2019-01-16 01:35发布

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?

13条回答
We Are One
2楼-- · 2019-01-16 02:05

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.

查看更多
相关推荐>>
3楼-- · 2019-01-16 02:05
4楼-- · 2019-01-16 02:09

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

查看更多
Evening l夕情丶
5楼-- · 2019-01-16 02:09

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

查看更多
Evening l夕情丶
6楼-- · 2019-01-16 02:11

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

查看更多
迷人小祖宗
7楼-- · 2019-01-16 02:12

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();
查看更多
登录 后发表回答