pythonpython-3.xbioinformaticsgenetic-algorithmobjectinstantiation

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

I am trying to get the value of the object, but I receive the location of the object in memory:

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


Solution

  • the <__main__.Chromosome instance at 0x7f7c81a41050> is the instance of the class x. What I assume is you want the variable of the class. Get a dictionary of the variables of the class x by putting print(vars(x)) inside the for loop to see the available variables, then access the required variable you want.