update: small genetic improvements

This commit is contained in:
KOSMOGOR
2025-04-11 11:13:19 +03:00
parent 766e6fb7e2
commit 90f8c08796
3 changed files with 6 additions and 6 deletions
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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):
+4 -4
View File
@@ -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