C# how to create functions that are interpreted at runtime
Asked Answered
C

6

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?

Came answered 16/3, 2010 at 23:16 Comment(1)
this doesn't quite answer your question, but since you're using .net, you could use F# for that part of your app, as it can be interpreted at runtimeCosgrove
B
13

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.

Bellied answered 16/3, 2010 at 23:27 Comment(3)
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.Came
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.Bellied
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 :).Came
R
7

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

Rosenberg answered 16/3, 2010 at 23:23 Comment(3)
Expression trees are a much better route than a pure eval function.Tremann
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?Came
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.Came
L
2

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.

Luhe answered 16/3, 2010 at 23:34 Comment(0)
S
1

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.

Syndesis answered 16/3, 2010 at 23:25 Comment(0)
J
1

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.

Jenisejenkel answered 16/3, 2010 at 23:25 Comment(0)
B
1

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

Brutus answered 16/3, 2010 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.