From 90f8c087968bd49bd833c36f05a6ea926f747836 Mon Sep 17 00:00:00 2001 From: KOSMOGOR Date: Fri, 11 Apr 2025 11:13:19 +0300 Subject: [PATCH 1/3] update: small genetic improvements --- gameobjects.py | 2 +- genetic_alg.py | 2 +- main.py | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gameobjects.py b/gameobjects.py index 9c24911..c1c003a 100644 --- a/gameobjects.py +++ b/gameobjects.py @@ -46,7 +46,7 @@ class GameObject: self.image.set_colorkey(None) # Explicitly disable colorkey class Horse(GameObject): - default_vacceleration = 0.3 # Default acceleration value + default_vacceleration = 0.2 # Default acceleration value vspeed = 0 # Vertical speed vacceleration = 0 # Vertical acceleration stopped = False # Whether the horse is stopped diff --git a/genetic_alg.py b/genetic_alg.py index 82bc4e0..4ed99f5 100644 --- a/genetic_alg.py +++ b/genetic_alg.py @@ -49,7 +49,7 @@ class GeneticAlgorithm: def mutate(self, model: NeuralNetwork): for param in model.parameters(): if torch.rand(1).item() < self.mutationRate: - param.data += torch.randn_like(param.data) * 0.1 * (1 if torch.rand(1).item() >= 0.5 else -1) + param.data += torch.randn_like(param.data) * 0.1 return model def learn(self, fitness: list): diff --git a/main.py b/main.py index 4163487..6a24934 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,10 @@ import sys from gameobjects import * from genetic_alg import GeneticAlgorithm -POPULATION_SIZE = 50 +POPULATION_SIZE = 100 MUTATION_RATE = 0.5 POPULATION_NEW = 0.1 -POPULATION_BEST = 0.2 +POPULATION_BEST = 0.3 FRAME_RATE = 160 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION BARRIER_SPEED = 10 BARRIER_DELAY = 100 @@ -60,12 +60,12 @@ def init_game(): UI.add_horses(horses) -def get_features(horse): +def get_features(horse: Horse): features = [last_barrier.rect.topleft[0], last_barrier.rect.topleft[1], last_barrier.rect.bottomright[0], last_barrier.rect.bottomright[1], horse.rect.topleft[0], horse.rect.topleft[1], horse.rect.bottomright[0], horse.rect.bottomright[1], - 0, HEIGHT, BARRIER_SPEED] + 0, HEIGHT, horse.vspeed, horse.vacceleration, BARRIER_SPEED] return features From ff2ddc98fbfd1ae186b1cbf722e97ad6e3b78dde Mon Sep 17 00:00:00 2001 From: KOSMOGOR Date: Mon, 14 Apr 2025 19:20:18 +0300 Subject: [PATCH 2/3] update: small twiks --- gameobjects.py | 3 +-- main.py | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gameobjects.py b/gameobjects.py index c1c003a..32d641c 100644 --- a/gameobjects.py +++ b/gameobjects.py @@ -106,8 +106,7 @@ class Horse(GameObject): def count_fitness(self): if self.stopped == False: self.fitness += 1 - if self.vacceleration == 0: - self.fitness -= 0.9 + if self.vspeed == 0: self.fitness -= 0.9 class Background(GameObject): def __init__(self, image_path, x, y): diff --git a/main.py b/main.py index 6a24934..718b328 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ POPULATION_SIZE = 100 MUTATION_RATE = 0.5 POPULATION_NEW = 0.1 POPULATION_BEST = 0.3 -FRAME_RATE = 160 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION +FRAME_RATE = 300 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION BARRIER_SPEED = 10 BARRIER_DELAY = 100 @@ -66,6 +66,7 @@ def get_features(horse: Horse): horse.rect.topleft[0], horse.rect.topleft[1], horse.rect.bottomright[0], horse.rect.bottomright[1], 0, HEIGHT, horse.vspeed, horse.vacceleration, BARRIER_SPEED] + features = [x / HEIGHT for x in features] return features From bd64901ebac92f3cc9e4737368b814916b05a741 Mon Sep 17 00:00:00 2001 From: KOSMOGOR Date: Mon, 14 Apr 2025 19:25:08 +0300 Subject: [PATCH 3/3] add: fitness best values --- genetic_alg.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/genetic_alg.py b/genetic_alg.py index 4ed99f5..c5f0bbd 100644 --- a/genetic_alg.py +++ b/genetic_alg.py @@ -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: