DEAP遗传程序-交叉失败(树不完整)

问题描述 投票:1回答:1

我使用深层工具进行基因编程。我的个人是表情树。我添加了一些具有自定义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示例时,我没有遇到此问题。

python genetic-algorithm genetic-programming deap
1个回答
0
投票

遇到相同的问题,但深入研究时会感到困惑。

当我从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。

© www.soinside.com 2019 - 2024. All rights reserved.