以python为例的单点子树交叉

2024-03-29 01:55:45 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我们正在使用遗传编程中python程序的ast表示,无法使用python-ast模块执行单点子树交叉。你知道吗

这是密码 要被替换的节点是一个元组,第0个元素包含从父节点随机选择的节点,第1个元素包含该列表中该节点的索引。你知道吗

collector_two是来自第二个父级的引用列表。你知道吗

def modify_node_using_parent(self, node_to_be_replaced, parent_two): size = len(self.collector_two.statements) if(self.get_the_node_type(node_to_be_replaced[0]) == "Assign") or (self.get_the_node_type(node_to_be_replaced[0]) == "AnnAssign") or (self.get_the_node_type(node_to_be_replaced[0]) == "AugAssign"): j = random.randint(0, size - 1) node_to_be_replaced_with = self.collector_two.statements[j] self.collector_one.all_changable_nodes[node_to_be_replaced[1]] = node_to_be_replaced_with self.collector_two.statements[j] = node_to_be_replaced[0]

Collector类是一个遍历树的类,它生成同一类型节点的引用列表,如语句和表达式等。 现在,我从一个父节点中选择一个节点,然后从另一个父节点中选择相同类型的节点,最后,我从列表中交换这些引用。 但当我将这些AST转换回源代码时,unparser类会引发AttributetypeError。你知道吗

任何帮助都将不胜感激


Tags: thetoselfnode元素列表get节点