我的 GA 个体是一个随机序列int
(例如:)[4, 5, 10, 11, 8, 12, 9, 13, 2, 6, 3, 7, 0, 14, 15, 1]
,它遵循在另一个自定义函数中处理的特定顺序约束。
如何将我的订单约束函数合并到DEAP个人初始化中?
我在https://groups.google.com/forum/#!msg/deap-users/KZYYHCGrFyY/x1nXcXpCyscJ的启发下实现了这种方式:
class MyContainer(list):
def __init__(self, attributes):
# Some initialisation with received values
# self.attr1 = attributes[0]
pass
def generate_individual(ind_class, size):
# ind_class will receive a class inheriting from MyContainer
# individual = ind_class(random.random() for _ in range(size))
individual = ind_class(random.sample(range(IND_SIZE), k=size))
individual = make_individual_valid(individual, other_parameters)
# make_individual_valid is the self-defined order constraint function
return individual
creator.create('FitnessMin', base.Fitness, weights=(-1.0,))
creator.create('Individual', MyContainer, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register('individual', generate_individual, creator.Individual, size=IND_SIZE)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
但我得到以下错误:
AttributeError: 'list' object has no attribute 'fitness'