gpt4 book ai didi

python - 获取遗传算法中使用的对象的值

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:01 24 4
gpt4 key购买 nike

 @staticmethod
def _select_tournament_population(pop):
tournament_pop = Population(0)
i = 0
while i < TOURNAMENT_SELECTION_SIZE:
tournament_pop.get_chromosomes().append(pop.get_chromosomes()[random.randrange(0, POPULATION_SIZE)])
i += 1
tournament_pop.get_chromosomes().sort(key=lambda x: x.get_fitness(), reverse=True)
return tournament_pop

def _print_population(self, pop, gen_number):
print("\n------------------------------------------------")
print("Generation #", gen_number, "| Fittest chromosome fitness:", pop.get_chromosomes()[0].get_fitness())
print("Target Chromosome:", TARGET_CHROMOSOME)
print("------------------------------------------------")
i = 0
for x in pop.get_chromosomes():
print("Chromosome #", i, " :", x, "| Fitness: ", x.get_fitness())
i += 1

chromosome fitness terminal output

我试图获取对象的值,但我收到了对象在内存中的位置:

('Chromosome #', 0, ' :', <__main__.Chromosome instance at 0x7f7c81a41050>, '| Fitness: ', 10)

最佳答案

<__main__.Chromosome instance at 0x7f7c81a41050>是类 x 的实例。我假设你想要类的变量。通过输入 print(vars(x)) 获取类 x 的变量的字典在 for 循环内查看可用变量,然后访问所需的变量。

关于python - 获取遗传算法中使用的对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com