1
 @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)

chb
  • 1,727
  • 7
  • 25
  • 47
  • In `pop..get_chromosomes()[0].get_fitness()` you're taking the element at index zero and calling `get_fitness()`. Why don't you index into the list when you call the same method later on to get the "value of the object"? – chb Jan 18 '18 at 13:00

1 Answers1

1

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.

Pratik Kumar
  • 2,211
  • 1
  • 17
  • 41
  • It gave me the followind dictionary `{'_genes': [1, 1, 0, 1, 0, 0, 1, 1, 1, 0], '_fitness': 10}` ,the list `_genes` was what I wanted. Thank you – Bipin Mishra Jan 18 '18 at 13:10