add: fitness best values

This commit is contained in:
KOSMOGOR
2025-04-14 19:25:08 +03:00
parent ff2ddc98fb
commit bd64901eba
+7 -1
View File
@@ -22,6 +22,9 @@ class GeneticAlgorithm:
self.percentageBest = percentageBest
self.percentageNew = percentageNew
self.inputSize = inputSize
self.fitnessBest = []
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(self.device)
self.initialize_population()
@@ -53,7 +56,10 @@ class GeneticAlgorithm:
return model
def learn(self, fitness: list):
self.population = [self.population[x] for x in np.argsort(fitness)[::-1]]
sortedFitnessArg = np.argsort(fitness)[::-1]
self.fitnessBest.append(fitness[sortedFitnessArg[0]])
print(self.fitnessBest[-1])
self.population = [self.population[x] for x in sortedFitnessArg]
numBest = int(self.populationSize * self.percentageBest)
self.population = self.population[:numBest]
while len(self.population) < self.populationSize - self.populationSize * self.percentageNew: