如何使用DEAP库中访问遗传算法的所有可行解决方案?
我正在尝试应用GA来生成一组满足约束列表的解决方案。我想找到满足营养约束的饮食解决方案(个体),我认为GA可能是一种很好的方法。我在这里找到了一个类似的例子: >但是,不使用约束的示例,它只找到最佳解决方案。因此,我的问题是:
- 如何删除所有不可行的解决方案,而只评估不违反约束的个人?
- 是否有一种方法可以存储所有满足我约束(也不最佳)的生成的个人?
从DEAP库的网页中,我发现我可以添加一个可行性函数,在这里我会像在此处那样对不可行的解决方案进行惩罚:
def feasible(individual):
individual = individual[0]
if (7*1999. < sum(x*y for x,y in zip(kcal_data,individual)) < 7*2001. and
7*60.5 < sum(x*y for x,y in zip(prot_data,individual)) and
... and so on....):
return True
return False
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 9999))
但是,尽管它们违反了约束,但仍保留了解决方案。 是否可以直接在评估功能中插入约束,以确保我仅评估Feaisble解决方案? 删除解决方案吗?
还是我可以为我的第二个问题 但是,当使用GA时,更糟糕的解决方案将从人群中删除,从而导致最终人群最好。 是否可以从算法(包括中间解决方案)中存储所有
?
世代
I am trying to apply GA for generating a set of solutions that fulfil a list of constraints. I would like to find solutions (individuals) of diets that fulfill nutritional constraints, for which I think GA could be a good way to do so. I have found a similar example here: A Genetic Algorithm to Optimize Your Diet, however, the example to not use constraints and it only finds the optimal solutions. Thus my questions are:
- How can I remove all infeasible solutions and only evalute individuals that do not violate the constraints?
- Is there a way to store/acess all the generated individuals that fulfil my constraints (also non-optimal)?
From the webpage of the DEAP library, I found that I can add a feasibility function, where I penalise the infeasible solutions, like in the example here:
def feasible(individual):
individual = individual[0]
if (7*1999. < sum(x*y for x,y in zip(kcal_data,individual)) < 7*2001. and
7*60.5 < sum(x*y for x,y in zip(prot_data,individual)) and
... and so on....):
return True
return False
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 9999))
However, the solutions are still kept, although they violate the constraints. Would it be possible to insert the constraints directly in the evaluate function, to ensure that I only evaluate feaisble solutions? Or could I, instead of penalising, delete the solutions?
For my second question, I am interested in obtaining a diverse set of individuals, not just the optimal ones. But, when using the GA, the worse solutions are remove from the population resulting in a final population of the best individuals. Is it instead possible to store all generations from the algorithm (incl. intermediate solutions)?
Any help will be appreciated,
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论