我正在尝试使用 DEAP 在 Python 中编写一个遗传程序,并有几个零参数函数作为终端。我希望能够使用例如 if_then_else 原语来组合它们,但这个原语一直试图调用我的其他 int 类型终端。我想强输入 pset 以避免这种情况,但 DEAP 不允许我在添加强类型基元时使用 None 类型参数,我该怎么办?
这是定义我到目前为止的 pset 的代码
def progn(*args):
for arg in args:
arg()
def prog2(out1, out2):
return partial(progn,out1,out2)
def prog3(out1, out2, out3):
return partial(progn,out1,out2,out3)
def if_then_else(condition, out1, out2):
out1() if condition() else out2()
pset = gp.PrimitiveSetTyped("main", [None, int, bool], None)
pset.addPrimitive(if_wall_ahead, [None, None], None)
pset.addPrimitive(prog2, [None, None], None)
pset.addPrimitive(prog3, [None, None, None], None)
pset.addPrimitive(compare, [int,int], bool)
pset.addPrimitive(operator.and_, [bool, bool], bool)
pset.addPrimitive(operator.or_, [bool, bool], bool)
pset.addPrimitive(operator.not_, [bool], bool)
pset.addTerminal(player.changeDirectionRight, None)
pset.addTerminal(player.changeDirectionUp, None)
pset.addTerminal(player.changeDirectionLeft, None)
pset.addTerminal(player.changeDirectionDown, None)
pset.addTerminal(player.h_target_distance, int)
pset.addTerminal(player.v_target_distance, int)
pset.addEphemeralConstant("rand", random.randint(0,20), int)