Python对象变量混淆了吗?
我正在尝试使用python(3.7)解决一个旅行推销员问题。我已为我的管线对象定义了管线类:Python对象变量混淆了吗?,python,class,object,genetic-algorithm,Python,Class,Object,Genetic Algorithm,我正在尝试使用python(3.7)解决一个旅行推销员问题。我已为我的管线对象定义了管线类: class Route: path = None distance = None fitness = None probability = None def __init__(self, path=None, distance=None): if distance is None: distance = 30000 if path is None: pa
class Route:
path = None
distance = None
fitness = None
probability = None
def __init__(self, path=None, distance=None):
if distance is None:
distance = 30000
if path is None:
path = []
self.path = path
self.distance = distance
我的遗传算法定义如下
def ga(initial_pop, distances, n_generations, pop_size, mutation_rate=1.00):
best_for_run = Route()
generation = initial_pop
for i in range(0, n_generations):
calculate_fitness(generation)
normalize_fitness(generation)
mating_pool = create_pool(generation, pop_size / 2)
new_generation = []
for j, individual in enumerate(mating_pool):
if j + 1 == len(mating_pool):
child1, child2 = crossover.cxPartialyMatched(individual.path, mating_pool[0].path)
else:
child1, child2 = crossover.cxPartialyMatched(individual.path, mating_pool[j + 1].path)
mutate = random.random()
if mutate < mutation_rate:
child1 = scramble_list(child1)
child1_distance = calculate_route_distance(child1, distances)
child2_distance = calculate_route_distance(child2, distances)
first_child = Route(child1, child1_distance)
second_child = Route(child2, child2_distance)
new_generation.append(first_child)
new_generation.append(second_child)
generation = new_generation
best_routes = get_best_routes(generation)
for ind in best_routes:
if ind.distance < best_for_run.distance:
best_for_run = ind
print('ind: ', ind.path, ind.distance)
print('best_for_run', best_for_run.path, best_for_run.distance)
print('Calc route distance', calculate_route_distance(ind.path, distances))
print('----------------------------------------')
return best_for_run
我得到这个输出:
ind: [3, 11, 13, 2, 8, 1, 15, 10, 0, 7, 6, 4, 5, 14, 9, 12] 4564
best_for_run [3, 11, 13, 2, 8, 1, 15, 10, 0, 7, 6, 4, 5, 14, 9, 12] 4564
Calc route distance 8439
----------------------------------------
ind: [10, 9, 4, 7, 2, 1, 3, 6, 8, 12, 11, 15, 13, 0, 5, 14] 4425
best_for_run [10, 9, 4, 7, 2, 1, 3, 6, 8, 12, 11, 15, 13, 0, 5, 14] 4425
Calc route distance 5811
----------------------------------------
ind: [2, 5, 3, 7, 12, 14, 9, 10, 13, 15, 11, 1, 0, 4, 6, 8] 4375
best_for_run [2, 5, 3, 7, 12, 14, 9, 10, 13, 15, 11, 1, 0, 4, 6, 8] 4375
Calc route distance 6132
----------------------------------------
ind: [3, 10, 12, 9, 15, 4, 13, 8, 14, 5, 7, 2, 0, 1, 6, 11] 3941
best_for_run [3, 10, 12, 9, 15, 4, 13, 8, 14, 5, 7, 2, 0, 1, 6, 11] 3941
Calc route distance 6068
----------------------------------------
ind: [6, 0, 3, 8, 10, 9, 2, 11, 1, 12, 7, 15, 4, 13, 5, 14] 3775
best_for_run [6, 0, 3, 8, 10, 9, 2, 11, 1, 12, 7, 15, 4, 13, 5, 14] 3775
Calc route distance 6586
----------------------------------------
ind: [2, 9, 8, 7, 4, 14, 11, 12, 10, 13, 15, 6, 5, 1, 0, 3] 3374
best_for_run [2, 9, 8, 7, 4, 14, 11, 12, 10, 13, 15, 6, 5, 1, 0, 3] 3374
Calc route distance 3374
----------------------------------------
Best for run [7, 1, 11, 2, 4, 12, 13, 5, 3, 0, 10, 6, 9, 15, 14, 8] 3374
提前感谢您的帮助。好吧,我似乎找到了一个解决方案,我创建了一个方法来计算整个一代人的距离,而不是计算“复制循环”中的所有路线距离,并在替换旧一代人时使用此方法:
def calculate_generation_distance(generation, distances):
for individual in generation:
individual.distance = calculate_route_distance(individual.path, distances)
return generation
还是不知道为什么事情一开始就被扔到哪里去了。我猜这是一个特性。可能没有关系,但是
路径
和距离
都是类和实例属性,不是最好的想法/实践。你说得对,我应该改变这一点。谢谢不幸的是这没有关系。
def calculate_generation_distance(generation, distances):
for individual in generation:
individual.distance = calculate_route_distance(individual.path, distances)
return generation
generation = calculate_generation_distance(new_generation, distances)