Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ genetic programming: Invoking a linker/compiler, executing the compiled program and piping input / output

This is a generic question, and although I'm pretty sure some of it's parts have been answered I want opinions rather than a broad discussion. I intend to do a master thesis on evolutionary computation and genetic programming, and I would like opinions from experts on Linux/C++ whether it would be possible to create source code files from the genetic program, invoke gcc to compile them, if they fail to compile to capture those reasons why they failed, if they do compile to execute the compiled program (as a new process) and then to be able to send input to that program and capture output and any raised exceptions (or crashes). I understand that the topic is too broad, but I would like to know if anyone thinks that this is undoable, silly to try, or even if there could be a better way on doing this.

like image 818
Ælex Avatar asked Apr 07 '11 18:04

Ælex


2 Answers

Yes, it is possible to do this, and actually quite simple. You output source code to a temporary file (mkstmp), you fork/exec a compilation process (that outputs to a temporary file), you fork/exec the resulting program, before that you dup2 and pipe to plug an input and an output. It is basic Unix programming, nothing really complicated to do in C here.

The code generation itself may be more difficult to get right, but this heavily depends on the project.

Also, we have modern tools since a couple of months : I believe that Clang may definitively be something to look at for this kind of stuff. If the code generation you plan to do is simple (or not simple, but structured), you can also directly output LLVM code. It is not hard, and enables you to generate efficient just in time compiled code.

like image 123
Alexandre C. Avatar answered Sep 30 '22 01:09

Alexandre C.


What are you trying to optimize in this genetic program? Other than just looking for programs that run, what criteria would you be looking for? I don't quite get the point...

To be clear, the reason I ask this is because I only understand using genetic algorithms in an attempt to solve some sort of optimization problem. In this case you would have some sort of heuristics where you can evaluate all children of the process and then breed new offspring based on the heuristic and a selection criteria. I don't understand what the desired outcome of generating this source would be or how you would create heuristics to evaluate it.

like image 27
jberg Avatar answered Sep 30 '22 00:09

jberg