C++遗传编程:调用链接器/编译器,执行编译后的程序和管道输入/输出

发布于 2024-10-30 21:45:35 字数 253 浏览 0 评论 0原文

这是一个通用问题,尽管我很确定其中一些部分已经得到解答,但我想要的是意见而不是广泛的讨论。我打算写一篇关于进化计算和遗传编程的硕士论文,我想请教Linux/C++专家的意见,是否可以从遗传程序创建源代码文件,调用gcc来编译它们,如果他们失败的话编译以捕获失败的原因,如果它们确实编译以执行已编译的程序(作为新进程),然后能够将输入发送到该程序并捕获输出和任何引发的异常(或崩溃)。我知道这个话题太宽泛,但我想知道是否有人认为这是不可撤销的,尝试愚蠢的,或者即使可以有更好的方法来做到这一点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮烟 2024-11-06 21:45:35

是的,可以做到这一点,而且实际上非常简单。您将源代码输出到临时文件 (mkstmp),fork/exec 编译过程(输出到临时文件),您fork/exec 生成的程序,然后使用 dup2pipe 插入输入和输出。这是基本的 Unix 编程,用 C 语言不需要做任何复杂的事情。

代码生成本身可能更难正确执行,但这在很大程度上取决于项目。

此外,几个月以来我们就拥有了现代化的工具:我相信 Clang 可能绝对适合看看这种东西。如果你打算做的代码生成很简单(或者不简单,而是结构化),你也可以直接输出 LLVM 代码。这并不难,并且使您能够生成高效的即时编译代码。

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.

情感失落者 2024-11-06 21:45:35

您想在这个基因程序中优化什么?除了寻找运行的程序之外,您还会寻找什么标准?我不太明白这一点......

需要明确的是,我问这个的原因是因为我只理解使用遗传算法来尝试解决某种优化问题。在这种情况下,您将拥有某种启发式方法,您可以评估该过程的所有子代,然后根据启发式和选择标准培育新的后代。我不明白生成此源的预期结果是什么,或者如何创建启发式方法来评估它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文