【问题标题】:Deap python package: create individuals with different ranges and mix of integers and floatsDeap python 包:创建具有不同范围的个体以及整数和浮点数的混合
【发布时间】:2017-10-18 13:52:55
【问题描述】:

我正在尝试使用 DEAP 来最大化功能。

我了解如何通过基本示例来做到这一点:

toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, 
    toolbox.attr_bool, 100)

这会创建 100 个随机值或 0 或 1。然后您继续创建种群并变异 ...

当你有两个参数时,你如何建立种群:

parameter 1 integer with range [0,1] 
parameter 2 float with range [0,2]

然后创建一个结合两个随机采样参数的个体?或者对于具有任意步长值的参数 2 样本,例如 0.25。

【问题讨论】:

    标签: python deap


    【解决方案1】:

    您可以简单地执行以下操作来创建多种类型的染色体:

    toolbox.register("attr_int", random.randint, 0, 1)
    toolbox.register("attr_flt", random.uniform, 0, 2)
    toolbox.register("individual", tools.initCycle, creator.Individual,
                 (toolbox.attr_int, toolbox.attr_flt),
                 n=1)
    

    然后创建一个大小为 100 的种群:

    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    population = toolbox.population(n=100)
    

    【讨论】:

    • 你介意举一个完整的例子吗?即我如何在我的适应度函数中调用这种类型的人口并将 DEAP 应用于最小化问题?谢谢!
    • @Rock。您可以查看 DEAP 文档。这个link 包含可能对您有所帮助的示例。如果你没有找到答案,请告诉我。祝你好运:)
    • 谢谢。我将通过示例。
    • 感谢您的回答。鉴于我们处于这一点,对于这种情况应该使用什么突变机制? cxOrdered 似乎无法工作并给我一个错误。你有什么想法吗?
    猜你喜欢
    • 2017-08-08
    • 2016-05-13
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多