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


 @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 #', 0, ' :', <__main__.Chromosome instance at 0x7f7c81a41050>, '| Fitness: ', 10)

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

相关内容

  • 没有找到相关文章

最新更新