我用Deep做遗传程序设计。我的个人是表情树。我添加了一些具有自定义1和2的自定义运算符。
pset = gp.PrimitiveSet("MAIN", 7)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
pset.addPrimitive(cop.delay10, 1)
pset.addPrimitive(cop.arg_max, 1)
pset.addPrimitive(cop.arg_min, 1)
pset.addPrimitive(cop.rank, 1)
pset.addPrimitive(cop.ma_n, 1)
pset.addPrimitive(cop.std_n, 1)
pset.addPrimitive(cop.max_diff, 1)
pset.addPrimitive(cop.min_diff, 1)
pset.addPrimitive(cop.factor_cov, 2)
pset.addPrimitive(cop.factor_corr, 2)
pset.renameArguments(ARG0="open")
pset.renameArguments(ARG1="high")
pset.renameArguments(ARG2="low")
pset.renameArguments(ARG3="close")
pset.renameArguments(ARG4="volume")
pset.renameArguments(ARG5="turn")
pset.renameArguments(ARG6="re_turn")
# Creators
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
# Create Individuals
toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=2, max_=5)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
# Create initial population
N_POP = 4
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
def evaluate_fit():
... # the part I customize my fitness score calculation function
toolbox.register("evaluate", evaluate_fit, stock_map=stock_dict, mkt=mkt_value, daily_return=wap_return)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
pop = toolbox.population(n=N_POP)
hof = tools.HallOfFame(2)
pop = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 2,
halloffame=hof, verbose=False)
我的大多数参数设置与官方示例非常相似:https://github.com/DEAP/deap/blob/454b4f65a9c944ea2c90b38a75d384cddf524220/examples/gp/symbreg.py
我使用相同的交叉方法:csOnePoint,程序的所有其他设置与此示例相同。
我认为程序与本示例之间的唯一区别是自定义运算符和评估方法。但是我不知道为什么在执行跨步步骤时总是有错误,我有这个错误:
ValueError:无效的分片分配:PrimitiveTree中不允许插入不完整的子树。考虑到原语的随意性,当某些节点无法映射到树中的任何位置时,将树定义为不完整。例如,如果sub的对数为2,则树[sub,4,5,6]是不完整的,因为它会产生一个孤立节点(6)。
我了解这可能意味着交叉后的树无法满足Arity要求,但是我不知道为什么会有这个问题。当我尝试symberg.py示例时,我没有遇到此问题。
最佳答案
遇到同样的问题时,可悲的是越来越困惑。
当我从here复制代码时,会引发错误:
for node in val[1:]:
total += node.arity - 1
if total != 0:
raise ValueError("Invalid slice assignation : insertion of"
" an incomplete subtree is not allowed in PrimitiveTree."
" A tree is defined as incomplete when some nodes cannot"
" be mapped to any position in the tree, considering the"
" primitives' arity. For instance, the tree [sub, 4, 5,"
" 6] is incomplete if the arity of sub is 2, because it"
" would produce an orphan node (the 6).")
只需在树上单独进行交叉操作,两棵树的arity得分均为0。
关于python - DEAP遗传程序-交叉失败(树不完整),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59184561/