我正在学习python,并尝试通过模拟退火实现遗传算法。我创建了一个描述人口的类,其中包含个人列表以及其他一些属性和方法。当我尝试创建下一代种群时,我想从种群中删除父母的基因型,然后执行模拟退火以查看是否将父母或子女重新包含在种群中。尝试从人口中移走父母时会出现问题。
我检查了父母的基因型确实存在于人群中。我通过建立一个最小的示例并扩展它直到出现错误来创建一个最小的工作示例。
import random
from math import factorial
class Genotype(object):
def __init__(self, data_list):
self.__genotype = data_list
# fitness is a complicated long function depending on modules,
# replaced by random for simplicity
self.fitness = random.random()
def __eq__(self, other):
"""
Comparing if two genotypes are equal
:param other: Other Genotype object
:type other: Genotype
:return: True if they are equal, False otherwise
:rtype: Boolean
"""
if self.get_genotype() == other.get_genotype():
return True
else:
return False
def get_genotype(self):
"""
Return the genotype.
"""
return self.__genotype
class Population(object):
def __init__(self, genotype_list):
self.individuals = genotype_list
self.fitness = self.get_fitness()
def __getitem__(self, key):
return self.individuals[key]
def get_fitness(self):
return [x.fitness for x in self.individuals]
def next_generation(self):
parent_indices = list()
childs = list()
# define the number of parents
number_of_parents = 12
# Roulette-wheel selection
eff_fit = self.fitness
s = sum(eff_fit)
eff_fit_with_indices = list(enumerate(eff_fit))
eff_fit_sorted = sorted(eff_fit_with_indices, key=lambda i: i[1])
for rep in range(number_of_parents):
r = random.uniform(0, s)
partial_sum = 0
for idx, val in enumerate(eff_fit_sorted):
partial_sum += val[1]
if partial_sum >= r:
parent_indices.append(val)
break
parent_genotypes = [self[x[0]] for x in parent_indices]
for parent in parent_genotypes:
self.individuals.remove(parent)
return self
individuals = 120
rectangle_number = 79
max_permutations = factorial(rectangle_number)
master_genotype = range(0, rectangle_number)
# creating initial genotypes
initial_genotypes = list()
for idx in range(0, individuals):
unique = False
temp_seed = random.randint(1, max_permutations)
random.seed(temp_seed)
while not unique:
temp_genotype_data_list = list(master_genotype)
random.shuffle(temp_genotype_data_list)
temp_genotype = Genotype(temp_genotype_data_list)
if temp_genotype not in initial_genotypes:
initial_genotypes.append(temp_genotype)
unique = True
population = Population(initial_genotypes)
population.next_generation()
我希望自己的成员减少,因为父母被删除了,但是,出现以下错误:
Exception has occured: ValueError
list.remove(x): x not in list