Разбираюсь в коде, но не могу понять как трактовать.
Как я понял:
1) Создали класс методы
2) Создали экземпляр класса ga
А вот далее непонятно.
Что значит такая запись? " for _ in range(N_GENERATIONS): " ?
И вот эта " DNA_prod, pred = ga.evolve(5)" ?
class MGA(object):
......
.....
.....
def evolve(self, n): # nature selection wrt pop's fitness
for _ in range(n): # random pick and compare n times
sub_pop_idx = np.random.choice(np.arange(0, self.pop_size), size=2, replace=False)
sub_pop = self.pop[sub_pop_idx] # pick 2 from pop
product = F(self.translateDNA(sub_pop))
fitness = self.get_fitness(product) #Write in txt
loser_winner_idx = np.argsort(fitness)
loser_winner = sub_pop[loser_winner_idx] # the first is loser and second is winner
loser_winner = self.crossover(loser_winner)
loser_winner = self.mutate(loser_winner)
self.pop[sub_pop_idx] = loser_winner
DNA_prod = self.translateDNA(self.pop)
pred = F(DNA_prod)
return DNA_prod, pred
ga = MGA(DNA_size=DNA_SIZE, DNA_bound=[0, 1], cross_rate=CROSS_RATE, mutation_rate=MUTATION_RATE, pop_size=POP_SIZE)
# 100 generations
for _ in range(N_GENERATIONS):
DNA_prod, pred = ga.evolve(5) # natural selection, crossover and mutation, DNA_prod -ось x, pred-ось y
print(pred)