搜索随机Python程序生成器

发布于 2024-10-19 18:21:02 字数 452 浏览 5 评论 0原文

我正在寻找一个可以生成随机但有效的 python 程序的程序,类似于

随机 C 程序生成器

我试图自己向 python tokenize.untokenize() 函数提供随机输入,但当然,大多数生成的源代码不是我可以用 eval( 解释的有效程序) )。所以我想知道,您是否知道如何生成随机但有效的 python 程序(也许使用 ast 模块?),或者这样的生成器是否已经存在。

编辑:我想使用随机Python源代码作为使用Python进行遗传编程的起点。所以我想要一个随机程序列表,然后将它们发展为返回“Hello World!”的程序。

I'm searching for a program that can generate random but valid python programs, similar to the

Random C program generator.

I was trying to do this myself giving random input to the python tokenize.untokenize() function, but of course most of the generated source code was not a valid program I could interpret with eval(). So I would like to know, if you either know a way how to generate random but valid python programs (maybe using the ast module?) or if such a generator already exists.

EDIT: I want to use the random python source code as a starting point for doing genetic programming with python. So I want to have a list of random programs, and then evolve them towards let's say a program that returns "Hello World!".

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

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

发布评论

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

评论(3

夕嗳→ 2024-10-26 18:21:03

谷歌搜索python“随机程序生成器”出现了随机Python 程序生成器

如果您想要可下载的脚本,请查看 pyfuzz

A google search for python "random program generator" turned up the Random Python Program Generator.

If you want a downloadable script, take a look at pyfuzz.

只等公子 2024-10-26 18:21:03

执行遗传编程来进化命令式程序并非完全微不足道。

您希望这些程序采用什么表示形式可能值得考虑,因为如果您计划对它们执行交叉/变异,那么字符串表示形式可能并不理想。相反,某种解析树抽象语法树可能更可取。这将使您的遗传算子能够轻松操纵子树。大部分困难在于在这些操作期间保持程序的有效性。

您可能需要考虑的一种方法是使用基于语法的进化技术,例如 语法进化 或 Whigham 的 < a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.26.2091&rep=rep1&type=pdf" rel="nofollow">CFG-GP。然后,您可以使用 BNF 语法 提供语言语法,并且生成的程序将符合这个语法。毫无疑问,您将能够在线找到您可以改编的 Python 语法。这些技术存在一些局限性,因为它们通常使用上下文无关语法,因此无法表示微妙的语义约束,但如果需要,可以采取一些方法来解决这个问题。

进一步考虑的是您是否真的希望整个 python 语言可用于进化过程。可用的功能越多,搜索空间就越大。在传统的 GP 中,函数和终端集是根据要解决的问题来指定的,挑战之一是确定一种具有足够表达性而又不过分的语法。使用单独的语法,您将能够针对不同的问题使用不同的语法。

Performing genetic programming to evolve imperative programs is not completely trivial.

It is probably worth some consideration as to what representation you want these programs in, because if you plan to perform crossover/mutation on them then a string representation is probably not ideal. Rather, some sort of parse tree or abstract syntax tree is probably preferable. This will allow your genetic operators to easily manipulate subtrees. Much of the difficulty is then in maintaining the validity of the programs during these operations.

One approach you may like to consider is using a grammar based evolutionary technique such as Grammatical Evolution or Whigham's CFG-GP. You can then provide the language syntax using a BNF grammar, and the programs will be generated to conform to this grammar. You will no doubt be able to find a grammar for Python online which you could adapt. There are some limitations with these techniques, since they generally use context-free grammars, and so cannot represent subtle semantic constraints but there are ways around that if necessary.

A further consideration is whether you really want the whole python language to be available to the evolutionary process. The more features you make available, the larger the search space. In traditional GP the function and terminal sets are specified according to the problem being tackled, and one of the challenges is deciding upon a syntax that is sufficiently expressive without being excessive. Using a separate grammar you will be able to use different grammars for different problems.

心凉怎暖 2024-10-26 18:21:03

我推荐 PushGP这里是一个链接一个Python类。

I recommend PushGP. Here is a link to a python class.

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