Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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.

You can take a look at System.Reflection.Emit to generate code at the IL level.

Or generate C#, compile into a library and load that dynamically. Not nearly as flexible.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top