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!

Twiltie
  • 572
  • 1
  • 6
  • 13
  • 6
    Please don't write SkyNet... – PiousVenom Feb 07 '13 at 22:07
  • Ha! I wouldn't dream of it, I'm thinking more along the lines of using this approach to stumble upon better algorithms for things like TSP, or Knights Tour, possibly factorization, primes etc... – Twiltie Feb 07 '13 at 22:09
  • @Twiltie People have been trying to create *useful* artificial intelligence for a *very* long time. While some useful stuff has come out of the research, nobody has really come anywhere *close* to actually succeeding. Just getting a dynamic language is insufficient, by a *lot*. – Servy Feb 07 '13 at 22:12
  • I have read a lot about the struggle to make AI "useful" and I realize that even in this decade we probably won't get much closer. But I think there is a definite future especially with more computing power, I am curious though about a dynamic language being insufficient, could you please elaborate? – Twiltie Feb 07 '13 at 22:17
  • 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 07 '13 at 22:19
  • Okay, I see what you're saying now. So this is way out of my depth but if you were striving to make a language useful for AI what kind of things would it include? – Twiltie Feb 07 '13 at 22:21
  • @Twiltie Since nobody has successfully created an effective AI with the level of complexity you're talking about, nobody in the world could accurately answer that question, since we haven't figured it out yet. Maybe after a few years of graduate school on the subject you'll be able to truly understand just how hard the problem is. (While still being nowhere near actually solving it.) – Servy Feb 07 '13 at 22:22
  • @Twiltie Now if you're talking AI in a more trivial sense, such as creating a bot for a game, then we can move out of the theoretical and more into an answerable question, but such an answer would be unlikely to help you answer this question. – Servy Feb 07 '13 at 22:25
  • @Servy thanks for the comments I really appreciate it! I'm sure it will be quite a few years before I can start to understand the depth and complexity of this problem (I'm still in high school). I guess for now I just have some reading to do! – Twiltie Feb 07 '13 at 22:26
  • The biggest problem with Genetic Algos is coming up with your "fitness function" - specifically, what does it mean for a genome to be "better"? This would be an *extremely* difficult question to answer in an abstract way. – JerKimball Feb 07 '13 at 23:23
  • 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 07 '13 at 23:41
  • @IraBaxter True, you can reason about "fitness" on a per-problem basis - I was referring to somehow algorithmically generating a fitness function that would somehow apply to *any* computation. – JerKimball Feb 07 '13 at 23:47
  • 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 08 '13 at 00:24
  • @IraBaxter Hah - I believe that is one of the best descriptions of the evolutionary process I've ever heard. Well put. – JerKimball Feb 08 '13 at 00:32
  • @IraBaxter actually as a general method to remove the weak code I was hoping that there would be someway to tell C# to let the assemblies with invalid code crash, that way each assembly that is invalid isn't able to write and then spawn a new assembly, and then to find good code it would probably have to be a problem by problem basis – Twiltie Feb 08 '13 at 01:42
  • 1
    @Twiltie: So, "main () {}" is likely to be the best survivor. Goes to show writing fitness functions is hard. – Ira Baxter Aug 16 '13 at 18:00
  • @IraBaxter actually I have taken a slightly different path since asking this question. I decided I had best go back to basics and try to learn a little more in a simpler environment. Currently I have some simple code that takes vectors that contain integers and tries to find the most fit using a simple genetic approach. More recently I rewrote this in CUDA to accelerate the process and run more tests/larger populations faster. I really can't thank you guy's enough for giving me some good information to think about and if you have further suggestions/advice I would love to discuss it! – Twiltie Aug 17 '13 at 01:14

4 Answers4

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.

Nashibukasan
  • 2,028
  • 23
  • 37
  • 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 07 '13 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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 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 07 '13 at 23:24
  • We've had people ask about "mutation testing", too, which uses the same ideas. – Ira Baxter Feb 07 '13 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 07 '13 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 08 '13 at 00: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#.

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • `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 07 '13 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 07 '13 at 22:14
  • @Servy Yep, I understand the difference between dynamic in C# and dynamic code emitting and execution. The DLR helps there, too. – Judah Gabriel Himango Feb 07 '13 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.

Cem Mutlu
  • 1,969
  • 1
  • 24
  • 24