1

I am curious about writing code in C# that "writes itself". I know that this is not possible in a broad sense, but I was thinking about setting up some kind of format for a dynamic assembly that defines everything except the body of some target function. Then an algorithm or maybe neural nets attempt to fill in the function body. After this the assembly is then executed, and the newly launched assembly then attempts to call the target function and after that creates another new assembly based of the same code, with hopefully a better implementation of that target function.

Given this kind of behavior would C# and dynamic assemblies be a suitable choice (I am concerned about the amount of time creating, and executing the assemblies would take). Is there some language that specializes in dynamically creating code to be executed, or is C# a good enough option?

Also any comments on the approach or setup of this whole assemblies creating assemblies idea is welcome, and appreciated! (I am very new if you couldn't tell)

Thanks!

17
  • 6
    Please don't write SkyNet...
    – PiousVenom
    Feb 7, 2013 at 22:07
  • 2
    @Twiltie We have had dynamic languages for many years. Most of the first languages were all dynamic, in the sense that they can trivially mutate the programs themselves or treat data as code, unlike programs such as C# which go to great lengths to prevent both. The prevalence of such dynamic languages has not made it easy to create useful AI. First off, it's not even a requirement, but even if it was, it's certainly nothing innovative or any sort of recent development (unless you consider the entirety of the field of computer science recent, which in history terms it is).
    – Servy
    Feb 7, 2013 at 22:19
  • 1
    @JerKimball: It is? How about: "function terminates in 1 second with the right answer for these 1000 vetted input-output pairs?" It is fair to say that passing such a test isn't proof the code is an algorithm; you'd need a theorem prover for that. But even that is well defined, if expensive (and only partial) to execute.
    – Ira Baxter
    Feb 7, 2013 at 23:41
  • 1
    @JerKimball: yes, it'd be hard to write a generic fitness function for any computation. But nature doesn't evolve any generic animal; it just needs a good fitness function for a cockroach, and voila, you get cockroaches.
    – Ira Baxter
    Feb 8, 2013 at 0:24
  • 1
    @Twiltie: So, "main () {}" is likely to be the best survivor. Goes to show writing fitness functions is hard.
    – Ira Baxter
    Aug 16, 2013 at 18:00

4 Answers 4

4

I may be wrong, but it sounds very much like you are interested in Genetic Programming. A good base would be some reading (I would recommend this book on machine learning, it's great).

Specifically for Genetic Programming you could try GPdotNET, but for broader Machine Learning I would definitely look at the Accord .NET Framework. The guy behind Accord writes a great blog that which could be useful too.

1
  • Thanks for the links! I am definitely interested in genetic programming and the blog looks great, I'll check out the book as well!
    – Twiltie
    Feb 7, 2013 at 22:19
2

If you want to "evolve" source code you have to be able to manipulate it. This is most easily done with Abstract Syntax Trees. Tools that make ASTs easy to manipulate are called program transformation systems, and these can encode source-to-source transformation rules that can act as genetic mutations.

One such rule code for our DMS Software Reengineering Toolkit would look like:

 swap_operators(x:product,y:term): sum-> sum
       "\x + \y " ->  "\x - \y" if  somecondition();

This replaces "+" by "-", if it is applied. You'd ideally have a bunch of these "crossover" operators (switching operators, changing expresssion precedence, change variables mentioned, changing control structures, etc.) and "somecondition"s that control whether the crossover operator is applied as part of an evolution step.

You need other means to compile/run/evaluate the fitness of the evolved program.

To do something like this with DMS, you'd have DMS read (parse to AST) a baseline program ("initial generation"), apply a set of evolution transforms, emit code for the modified ASTs, compile and run them (DMS can invoke subprocesses like "compile" and execute), evaluate the result pick the top N of this generation, apply evolution operators again, repeat until nirvana or your electric bill overflows.

4
  • I've always through AST manipulation would be the best way to do crossover for "code constructs"...actually have been meaning to give Roslyn a whirl in that area...
    – JerKimball
    Feb 7, 2013 at 23:24
  • We've had people ask about "mutation testing", too, which uses the same ideas.
    – Ira Baxter
    Feb 7, 2013 at 23:35
  • Ah yeah, I can see the crossover - mutation testing is a bit like "Fuzzing for Unit Tests", but instead of making sure your validation can handle any input, you are proving that if any of your invariants change, your unit test fails, yeah?
    – JerKimball
    Feb 7, 2013 at 23:48
  • @JerKimball: Right. Also, that your unit tests (which are a kind of invariant) actually detect that your code is wrong when mutated.
    – Ira Baxter
    Feb 8, 2013 at 0:22
0

C# is suitable for genetic programming, especially now that the dynamic language runtime is in the .NET framework and accessible with C#'s dynamic keyword.

GeneticProgramming .NET is a project that might get you off the ground. There was also an MSDN article on genetic programming with C# and Windows Forms a few years ago: Natural Selection with C#.

3
  • dynamic in C# just means you don't know what the type of that object is. The dynamic programming that the OP is talking about is basically an Eval command that executes a string as code in the context of the current program. C# can't really do that, certainly not in any practical sense anyway.
    – Servy
    Feb 7, 2013 at 22:14
  • 1
    Thanks, for the links! I just wanted to make sure I picked a suitable language before I invested too much time in it!
    – Twiltie
    Feb 7, 2013 at 22:14
  • @Servy Yep, I understand the difference between dynamic in C# and dynamic code emitting and execution. The DLR helps there, too. Feb 7, 2013 at 22:16
0

Mutating the code randomly doesnt make linear changes. Your small changes should mostly carry you to success. So it is not different from brute-force. Genetic algorithm will be draft into a state of chaos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.