D:\anaconda\python.exe C:\Users\lenovo\Desktop\MTGP\data1\MTGP.py D:\anaconda\lib\site-packages\deap\gp.py:257: RuntimeWarning: Ephemeral rand function cannot be pickled because its generating function is a lambda function. Use functools.partial instead. "instead.".format(name=name), RuntimeWarning) Traceback (most recent call last): File "C:\Users\lenovo\Desktop\MTGP\data1\MTGP.py", line 224, in <module> pop, log, hof = SRPMain(randomSeeds) File "C:\Users\lenovo\Desktop\MTGP\data1\MTGP.py", line 215, in SRPMain stats=mstats, psets=psets, halloffame=hof, verbose=True) File "C:\Users\lenovo\Desktop\MTGP\data1\evalGP_main.py", line 56, in eaSimple offspring = varAnd(offspring, toolbox, cxpb, mutpb) File "C:\Users\lenovo\Desktop\MTGP\data1\evalGP_main.py", line 17, in varAnd offspring[i - 1], offspring[i] = toolbox.mate(offspring[i - 1], offspring[i]) File "C:\Users\lenovo\Desktop\MTGP\data1\MTGP.py", line 174, in cxOnePointListOfTrees ind1[idx], ind2[idx] = tree1, tree2 File "D:\anaconda\lib\site-packages\deap\gp.py", line 86, in __setitem__ elif val.arity != self[key].arity: AttributeError: 'PrimitiveTree' object has no attribute 'arity' [Before Crossover] ind1[0] = <class 'deap.gp.PrimitiveTree'>, ind2[0] = <class 'deap.gp.PrimitiveTree'> [After Crossover] tree1 = <class 'deap.gp.PrimitiveTree'>, tree2 = <class 'deap.gp.PrimitiveTree'> [After Conversion] tree1 = <class 'deap.gp.PrimitiveTree'>, tree2 = <class 'deap.gp.PrimitiveTree'> Process finished with exit code 1
时间: 2025-03-10 21:07:30 浏览: 75
### 解决 DEAP 库中交叉操作时 `AttributeError` 的方法
当处理 DEAP 库中的交叉操作并遇到 `'PrimitiveTree' object has no attribute 'arity'` 错误时,这通常意味着尝试访问的对象不具有预期的属性。此问题可能源于对象类型的混淆或者初始化不当。
为了有效解决问题,可以考虑以下几个方面:
1. **确认数据结构一致性**
确认参与交叉运算的两个个体确实都是由相同工具箱创建而来,并且它们内部存储的数据类型一致。对于 GP (遗传编程),确保这些个体是由相同的原始集构建而成非常重要[^1]。
2. **检查工具箱配置**
工具箱设置决定了如何生成新个体以及定义其行为方式。如果在定义工具箱时遗漏了某些必要组件,则可能导致此类错误发生。特别是要验证是否正确设置了 `pset.addPrimitive()` 和其他相关函数来扩展可用的操作符集合。
3. **更新至最新版本**
如果正在使用的 DEAP 版本较旧,可能存在已知 bug 或者 API 变更影响到当前代码逻辑。建议升级到最新的稳定版以获得更好的兼容性和修复潜在缺陷。
4. **调试与日志记录**
添加更多详细的打印语句或利用 Python 内置的日志模块跟踪程序流,特别是在执行交叉之前的状态。这样可以帮助定位具体在哪一步出现了偏差,进而采取针对性措施加以修正。
```python
from deap import gp, creator, base, tools
# 定义适合特定应用需求的 PrimitiveSet
pset = gp.PrimitiveSet("MAIN", 1)
# 向 pset 中添加必要的原语(primitives)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
...
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("expr", gp.genFull, pset=pset, min_=1, max_=2)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def check_attributes(individual):
try:
_ = individual.root.arity # 尝试获取根节点的 arity 属性
return True
except AttributeError as e:
print(f"Error accessing attributes of the individual: {e}")
return False
# 注册交叉算子前先做一次合法性检测
toolbox.decorate("mate", tools.selTournament, k=2, tournsize=3)(check_attributes)
```
通过上述手段应该能够有效地排查并解决所提到的 `AttributeError` 问题,在实际开发过程中也可以借鉴类似的思路去应对不同场景下的异常情况。
阅读全文
相关推荐


















