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

Get value of object used in genetic algorithm

 @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> 是 class x 的实例。我假设您想要 class 的变量。通过将 print(vars(x)) 放入 for 循环以查看可用变量,获取 class x 变量的字典,然后访问所需的变量。