views:

202

answers:

6

I'm making a Genetic Program, but I'm hitting a limitation with C# where I want to present new functions to the algorithm but I can't do it without recompiling the program. In essence I want the user of the program to provide the allowed functions and the GP will automatically use them. It would be great if the user is required to know as little about programming as possible.

I want to plug in the new functions without compiling them into the program. In Python this is easy, since it's all interpreted, but I have no clue how to do it with C#. Does anybody know how to achieve this in C#? Are there any libraries, techniques, etc?

+7  A: 

You can try to create and manipulate Expression Trees. Use Linq to evaluate expression trees.

devio
Expression trees are a much better route than a pure eval function.
Paul Creasey
My GP has multiple individuals and each individual has a tree of functions and constants. A node in the tree can be a function or a constant... does that mean that I should replace the function at a given node with an expression tree? How can a user plug in their function?
Lirik
Note that the tree doesn't really contain a function, but an enum which identifies a function. When called the "executor" simply looks at the enum and calls the corresponding function.
Lirik
+1  A: 

You have access to the Compiler from within code, you can then create instances of the compiled code and use them without restarting the application. There are examples of it around

Here

and

Here

The second one is a javascript evaluator but could be adapted easily enough.

Leigh Shayler
+12  A: 

It depends on how you want the user of the program to "provide the allowed functions."

  • If the user is choosing functions that you've already implemented, you can pass these around as delegates or expression trees.
  • If the user is going to write their own methods in C# or another .NET language, and compile them into an assembly, you can load them using Reflection.
  • If you want the user to be able to type C# source code into your program, you can compile that using CodeDom, then call the resulting assembly using Reflection.
  • If you want to provide a custom expression language for the user, e.g. a simple mathematical language, then (assuming you can parse the language) you can use Reflection.Emit to generate a dynamic assembly and call that using -- you guessed it -- Reflection. Or you can construct an expression tree from the user code and compile that using LINQ -- depends on how much flexibility you need. (And if you can afford to wait, expression trees in .NET 4.0 remove many of the limitations that were in 3.5, so you may be able to avoid Reflection.Emit altogether.)
  • If you are happy for the user to enter expressions using Python, Ruby or another DLR language, you can host the Dynamic Language Runtime, which will interpret the user's code for you.

Hosting the DLR (and IronPython or IronRuby) could be a good choice here because you get a well tested environment and all the optimisations the DLR provides. Here's a how-to using IronPython.

Added in response to your performance question: The DLR is reasonably smart about optimisation. It doesn't blindly re-interpret the source code every time: once it has transformed the source code (or, specifically, a given function or class) to MSIL, it will keep reusing that compiled representation until the source code changes (e.g. the function is redefined). So if the user keeps using the same function but over different data sets, then as long as you can keep the same ScriptScope around, you should get decent perf; ditto if your concern is just that you're going to run the same function zillions of times during the genetic algorithm. Hosting the DLR is pretty easy to do, so it shouldn't be hard to do a proof of concept and measure to see if it's up to your needs.

itowlson
I like the reflection options you mentioned... they might be my best option. DLR might be a good alternative, is there a significant performance overhead when invoking a DLR method? The methods are going to be invoked VERY frequently, so I'd like to utilize an option that has as little overhead as possible.
Lirik
I've added some info about how the DLR improves performance with repetitively called code. I don't have quantitative figures though, but you may be able to find something on Google, or failing that it's pretty easy to host the DLR and give it a quick go.
itowlson
Yah, I don't plan to modify the source code during execution it's probably going to be the same method but with a different data instance, so DLR could be a good alternative. I'm also going to utilize memoization, so if the same parameters always produce the same results, then the method will not be needlessly executed. Thanks :).
Lirik
A: 

If the new algorithms are C#, or if your program can translate them to C#, then you could invoke the C# compiler (I may be mistaken but I think it comes with the .Net Framework) to compile the code.

Of you could design your own interpreted language (that's how I always thought I'd do it if I tried this).

Or you could use the Reflection Emit to emit IL.

Charles
+2  A: 

You can also use CodeDom to compile and run a function. For sure you can google to see some examples that might fit your needs. It seems that this article "How to dynamically compile C# code" and this article "Dynamically executing code in .Net" could help you.

Jenea
+1  A: 

It is in fact very easy to generate IL. See this tutorial: http://www.meta-alternative.net/calc.pdf

SK-logic