0

我一直致力于通过最大化道路的特定属性来生成道路。我将 DEAP 框架用于进化部分。我的道路表示为字典。我遇到了一个问题,而我正在交配两条道路。deap-tutorial 指出,在工具箱操作中更改了参数:“交叉运算符的一般规则是它们只与个体交配,这意味着如果原始个体有,则必须在与个体交配之前制作独立副本要保留或是对其他个人的引用。因此,当我尝试复制并粘贴教程中的解决方案并结合我的交叉操作时,我最终得到了一个列表元素,该元素没有 Fitness.value,或者工具箱操作根本不会改变道路。

creator.create("FitnessMax", base.Fitness, weights=(1.0, 1.0))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("getInd", getRandomTrack)
toolbox.register("individual", tools.initRepeat, creator.Individual,
             toolbox.getInd, n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalTest)
toolbox.register("mutate", mutateTestCase)
toolbox.register("mate", tools.cross)
toolbox.register("select", tools.selNSGA2)  

交叉操作现在只加载一个保存的字典,因为问题在于工具箱没有将其视为个体:

def cross(ind1, ind2):
    with open('c:/Users/****/.asfaultenv/output/final 
    /test_0004.json') as f                 
        ind2 = json.load(f)
    with open('c:/Users/****/.asfaultenv/output/final
    /test_0004.json') as f
        ind1 = json.load(f)
return (ind1), (ind2)

这是循环,它应该加载个体并将它们配对:

# Clone the selected individuals
offspring = [toolbox.clone(ind) for ind in pop]
# I've also used the approach beneath
# offspring = list(map(toolbox.clone, pop))

for child1, child2 in zip(offspring[::2], offspring[1::2]):
    toolbox.mate(child1, child2)
    if child1 is not None:
        del child1.fitness.values
    if child2 is not None:
        del child2.fitness.values

正如我上面提到的,这样,孩子们不会以任何方式被操纵,当我尝试这样的事情时:

child1, child2 = toolbox.mate(child1, child2)

然后我会得到错误:'list' object has no attribute 'fitness'

感谢您抽出宝贵时间。

4

1 回答 1

0

您的cross函数需要返回类型为 的对象individual。在您的示例中,您返回的对象类型为json.load.

于 2019-11-21T12:24:32.343 回答