diff --git a/gameobjects.py b/gameobjects.py index 9c24911..32d641c 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 @@ -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/genetic_alg.py b/genetic_alg.py index 82bc4e0..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() @@ -49,11 +52,14 @@ 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): - 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: diff --git a/main.py b/main.py index 4163487..718b328 100644 --- a/main.py +++ b/main.py @@ -3,11 +3,11 @@ 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 -FRAME_RATE = 160 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION +POPULATION_BEST = 0.3 +FRAME_RATE = 300 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION BARRIER_SPEED = 10 BARRIER_DELAY = 100 @@ -60,12 +60,13 @@ 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] + features = [x / HEIGHT for x in features] return features