Genetic programming in C ++: calling the linker / compiler, executing the compiled program and piping I / O

This is a general question, and although I am sure that some of its parts have received answers, I want opinions rather than a broad discussion. I intend to do a master's thesis in evolutionary computing and genetic programming, and I would like the opinions of Linux / C ++ experts to create source code files from a genetic program, to call gcc to compile them if they cannot compile, to fix those the reasons why they failed if they are compiled to run the compiled program (as a new process), and then to send input to this program and receive output and any exceptions thrown (or failures). I understand that the topic is too broad, but I would like to know if anyone thinks that this cannot be undone, it is stupid to try, or even if there may be a better way to do it.

+6
source share
2 answers

Yes, it can be done and is actually quite simple. You output the source code to a temporary file ( mkstmp ), you fork / exec the compilation process (which is output to a temporary file), you fork/exec resulting program, before which you dup2 and pipe , to connect the input and output. This is basic Unix programming, in C there is nothing complicated.

Generating the code itself can be harder to get, but it depends a lot on the project.

In addition, we have modern tools since two months: I believe that Clang can be something like what you can look at things. If the generation of the code you plan to do is simple (or not simple, but structured), you can also directly output LLVM code. It is not complicated and allows you to generate efficient only compiled time code.

+1
source

What are you trying to optimize in this genetic program? Besides the fact that you are only looking for those programs, what criteria would you be looking for? I don’t quite understand ...

To be clear, the reason I'm asking about this is because I only understand the use of genetic algorithms in an attempt to solve some kind of optimization problem. In this case, you will have some kind of heuristic, where you can evaluate all the children in the process, and then breed new offspring based on heuristics and selection criteria. I do not understand what the desired result of generating this source would be, or how you would create a heuristic to evaluate it.

+1
source

Source: https://habr.com/ru/post/885446/


All Articles