update: small genetic improvements
This commit is contained in:
+1
-1
@@ -46,7 +46,7 @@ class GameObject:
|
|||||||
self.image.set_colorkey(None) # Explicitly disable colorkey
|
self.image.set_colorkey(None) # Explicitly disable colorkey
|
||||||
|
|
||||||
class Horse(GameObject):
|
class Horse(GameObject):
|
||||||
default_vacceleration = 0.3 # Default acceleration value
|
default_vacceleration = 0.2 # Default acceleration value
|
||||||
vspeed = 0 # Vertical speed
|
vspeed = 0 # Vertical speed
|
||||||
vacceleration = 0 # Vertical acceleration
|
vacceleration = 0 # Vertical acceleration
|
||||||
stopped = False # Whether the horse is stopped
|
stopped = False # Whether the horse is stopped
|
||||||
|
|||||||
+1
-1
@@ -49,7 +49,7 @@ class GeneticAlgorithm:
|
|||||||
def mutate(self, model: NeuralNetwork):
|
def mutate(self, model: NeuralNetwork):
|
||||||
for param in model.parameters():
|
for param in model.parameters():
|
||||||
if torch.rand(1).item() < self.mutationRate:
|
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
|
return model
|
||||||
|
|
||||||
def learn(self, fitness: list):
|
def learn(self, fitness: list):
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import sys
|
|||||||
from gameobjects import *
|
from gameobjects import *
|
||||||
from genetic_alg import GeneticAlgorithm
|
from genetic_alg import GeneticAlgorithm
|
||||||
|
|
||||||
POPULATION_SIZE = 50
|
POPULATION_SIZE = 100
|
||||||
MUTATION_RATE = 0.5
|
MUTATION_RATE = 0.5
|
||||||
POPULATION_NEW = 0.1
|
POPULATION_NEW = 0.1
|
||||||
POPULATION_BEST = 0.2
|
POPULATION_BEST = 0.3
|
||||||
FRAME_RATE = 160 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION
|
FRAME_RATE = 160 # TODO: FIX THE INCORRECT FRAME RATE CORRELATION
|
||||||
BARRIER_SPEED = 10
|
BARRIER_SPEED = 10
|
||||||
BARRIER_DELAY = 100
|
BARRIER_DELAY = 100
|
||||||
@@ -60,12 +60,12 @@ def init_game():
|
|||||||
UI.add_horses(horses)
|
UI.add_horses(horses)
|
||||||
|
|
||||||
|
|
||||||
def get_features(horse):
|
def get_features(horse: Horse):
|
||||||
features = [last_barrier.rect.topleft[0], last_barrier.rect.topleft[1],
|
features = [last_barrier.rect.topleft[0], last_barrier.rect.topleft[1],
|
||||||
last_barrier.rect.bottomright[0], last_barrier.rect.bottomright[1],
|
last_barrier.rect.bottomright[0], last_barrier.rect.bottomright[1],
|
||||||
horse.rect.topleft[0], horse.rect.topleft[1],
|
horse.rect.topleft[0], horse.rect.topleft[1],
|
||||||
horse.rect.bottomright[0], horse.rect.bottomright[1],
|
horse.rect.bottomright[0], horse.rect.bottomright[1],
|
||||||
0, HEIGHT, BARRIER_SPEED]
|
0, HEIGHT, horse.vspeed, horse.vacceleration, BARRIER_SPEED]
|
||||||
return features
|
return features
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user