我很自豪地宣布,新版本的Pyevolve将拥有遗传编程支持;经过一段时间与这些邪恶的语法树进行战斗,我认为我在Python中非常简单而灵活地实现GP。亚洲金博宝我厌倦了看到人们放弃并试图学习如何使用C / C ++和Java的密封库来实现一个简单的GP(不幸的是我是Java Web Developer Hehe)。
实施仍在一些测试和优化下,但它的工作很好,这里有一些关于它的细节:
实施已经完成纯Python.,所以我们仍然有很多奖金,但不幸的是,我们失去了一些表现。
GP核心非常灵活,因为它亚洲金博宝在Python字节码中编译GP树加快函数的执行。因此,您可以使用甚至使用Python对象作为终端,或任何可能的Python表达式。也可以使用任何Python函数,您可以使用Python的所有电源来创建这些功能,该功能将由框架使用名称前缀=)自动检测到
正如您在源代码中看到的,您无需在调用个人的语法树时绑定变量,简单使用“GetCompileDcode.“返回要执行的Python编译函数的方法。
以下是源代码示例:
来自Pyevolve导入*导入数学error_accum = util.ErrorAccumulator()#这是GP核心使用的功能,#Pyevolve将自动检测它们#和它们的参数def gp_add(a,b):返回a + b defgp_sub(a, b): return a-b def gp_mul(a, b): return a*b def gp_sqrt(a): return math.sqrt(abs(a)) def eval_func(chromosome): global error_accum error_accum.reset() code_comp = chromosome.getCompiledCode() for a in xrange(0, 5): for b in xrange(0, 5): # The eval will execute a pre-compiled syntax tree # as a Python expression, and will automatically use # the "a" and "b" variables (the terminals defined) evaluated = eval(code_comp) target = math.sqrt((a*a)+(b*b)) error_accum += (target, evaluated) return error_accum.getRMSE() def main_run(): genome = GTree.GTreeGP() genome.setParams(max_depth=5, method="ramped") genome.evaluator.set(eval_func) ga = GSimpleGA.GSimpleGA(genome) # This method will catch and use every function that # begins with "gp", but you can also add them manually. # The terminals are Python variables, you can use the # ephemeral random consts too, using ephemeral:random.randint(0,2) # for example. ga.setParams(gp_terminals = ['a', 'b'], gp_function_prefix = "gp") # You can even use a function call as terminal, like "func()" # and Pyevolve will use the result of the call as terminal ga.setMinimax(Consts.minimaxType["minimize"]) ga.setGenerations(1000) ga.setMutationRate(0.08) ga.setCrossoverRate(1.0) ga.setPopulationSize(2000) ga.evolve(freq_stats=5) print ga.bestIndividual() if __name__ == "__main__": main_run()
我很开心亚洲金博宝,并测试Python中这个GP实现的可能性。
当然,Pyovolve中的一切都亚洲金博宝可以随时可视化(点击放大):


可视化也非常灵活,如果您使用Python装亚洲金博宝饰器设置函数的方式是图形的,您可以有许多有趣的可视化模式。如果我将函数“gp_add”更改为:
@ gtree.gpdec(表示=“+”,color =“红色”)def gp_add(a,b):返回a + b
我们将获得触手可及(点击放大):

我希望你喜欢它,我目前正在修复一些错误,实现新功能,文档和准备下一个Pyevolve的发布,这将需要一些时间=)