DEAP-通过工具箱操作丢失的适用性值

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

我一直在努力通过最大化道路的特定属性来生成道路。我将DEAP框架用于演化部分。我的道路以字典表示。我在两条道路交配时遇到问题。深入的研究表明,在工具箱操作中更改了参数:“跨界经营者的一般规则是,他们只能交配个体,这意味着如果必须保留原始个体或者是指其他个体,则必须在交配个体之前制作独立的副本。”因此,当我尝试将解决方案与交叉操作结合并复制并粘贴到教程中时,我最终得到的是list元素,它没有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)

然后我会得到错误:'列表'对象没有属性'健身'

感谢您抽出宝贵的时间。

evolutionary-algorithm deap
1个回答
0
投票

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

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