C # how to create functions that are interpreted at runtime

I am making a genetic program, but I will limit myself to restrictions in C #, where I want to introduce new functions for the algorithm, but I cannot do this without recompiling the program. In essence, I want the program user to provide the allowed functions, and the GP will automatically use them. It would be great if the user should have known as little as possible about programming.

I want to connect new functions without compiling them into a program. In Python, this is easy since all of this is interpreted, but I don't know how to do this with C #. Does anyone know how to achieve this in C #? Are there libraries, methods, etc.?

+6
c # genetic-algorithm genetic-programming
source share
6 answers

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

  • If the user selects functions that you have already implemented, you can pass them in the form of 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 enter the C # source code into your program, you can compile it using CodeDom, and then invoke the resulting assembly using Reflection.
  • If you want to provide a custom expression language for the user, for example. a simple mathematical language, then (provided that you can parse the language), you can use Reflection.Emit to create a dynamic assembly and call using - you guessed it - Reflection. Or you can build an expression tree from user code and compile it using LINQ - it depends on how much flexibility you need. (And if you can afford to wait, expression trees in .NET 4.0 remove many of the restrictions that were in 3.5, so you can avoid Reflection.Emit at all.)
  • If you are happy that the user enters expressions using Python, Ruby, or another DLR language, you can host a dynamic Runtime language that will interpret the user code for you.

DLR hosting (and IronPython or IronRuby) can be a good choice here because you get a well-tested environment and all the optimizations provided by DLR. Here's how to do it with IronPython.

Added to your question about performance: DLR intelligently understands optimization. It does not blindly repeat the interpretation of the source code each time: after it has converted the source code (or, in particular, a given function or class) to MSIL, it will continue to reuse this compiled view until the source code changes (for example, the function is overridden) . Therefore, if the user continues to use the same function, but for different data sets, then as long as you can support the same ScriptScope, you should get a decent perf; Similarly, if your concern is that you will perform the same function at once at times the genetic algorithm. DLR hosting is fairly easy to use, so it should not be difficult to make proof of concept and measure to ensure that it meets your needs.

+13
source share

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

+7
source share

You can also use CodeDom to compile and run the function. Of course, you can google see some examples that may suit your needs. It seems that this article “How to dynamically compile C # code” and this article “Dynamically executing code in .Net” could help you.

+2
source share

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

Here

and

Here

The second is a javascript evaluator, but it can be easily adapted.

+1
source share

You can look at System.Reflection.Emit for code generation at the IL level.

Or generate C #, compile to a library and load it dynamically. Not so flexible.

+1
source share

In fact, it is very easy to generate IL. See This Tutorial: http://www.meta-alternative.net/calc.pdf

+1
source share

All Articles