Python 对象变量混淆了?
Python objects variables getting mixed up?
我正在尝试使用 python(3.7) 解决旅行商问题。我为我的路线对象定义了路线 class:
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
据我所知,这应该是 return 距离最短的路线对象。但在某些时候,每个对象(或路径?)的距离变得混乱,因此如果您计算 returned 对象距离,它将与打印的不同。
例如,如果我 运行 这样的代码:
distances = get_cities('cities.xlsx')
pop = initial_population(100, distances)
best = ga(pop, distances, 1000, 100, mutation_rate=0.4)
print('Best for run', best.path, best.distance)
我得到这个输出:
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
提前感谢您的帮助。
好吧,我似乎找到了一个解决方案,而不是计算 "reproduction loop" 中的所有路由距离,我创建了一种计算整代距离的方法,并在我替换老一代的地方使用它:
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)
仍然不确定为什么事情首先会被扔掉。我猜这是一个功能。
我正在尝试使用 python(3.7) 解决旅行商问题。我为我的路线对象定义了路线 class:
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
据我所知,这应该是 return 距离最短的路线对象。但在某些时候,每个对象(或路径?)的距离变得混乱,因此如果您计算 returned 对象距离,它将与打印的不同。
例如,如果我 运行 这样的代码:
distances = get_cities('cities.xlsx')
pop = initial_population(100, distances)
best = ga(pop, distances, 1000, 100, mutation_rate=0.4)
print('Best for run', best.path, best.distance)
我得到这个输出:
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
提前感谢您的帮助。
好吧,我似乎找到了一个解决方案,而不是计算 "reproduction loop" 中的所有路由距离,我创建了一种计算整代距离的方法,并在我替换老一代的地方使用它:
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)
仍然不确定为什么事情首先会被扔掉。我猜这是一个功能。