From ab0c8b85212c59a9a97a09180eefcaa2b0b5d080 Mon Sep 17 00:00:00 2001 From: KOSMOGOR Date: Thu, 10 Apr 2025 14:15:16 +0300 Subject: [PATCH] update: changed NN and number of features --- genetic_alg.py | 17 +++++++++++------ main.py | 7 +++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/genetic_alg.py b/genetic_alg.py index 4492a41..1ee727f 100644 --- a/genetic_alg.py +++ b/genetic_alg.py @@ -5,11 +5,13 @@ import numpy as np class NeuralNetwork(nn.Module): def __init__(self, inputSize): super().__init__() - self.hidden = nn.Linear(inputSize, 32) - self.output = nn.Linear(32, 3) + self.hidden1 = nn.Linear(inputSize, 32) + self.hidden2 = nn.Linear(32, 16) + self.output = nn.Linear(16, 3) def forward(self, x): - x = torch.relu(self.hidden(x)) + x = torch.relu(self.hidden1(x)) + x = torch.relu(self.hidden2(x)) x = self.output(x) return torch.argmax(x) @@ -28,9 +30,12 @@ class GeneticAlgorithm: def crossover(self, parent1: NeuralNetwork, parent2: NeuralNetwork): child1 = NeuralNetwork(self.inputSize).to(self.device) child2 = NeuralNetwork(self.inputSize).to(self.device) - point = len(child1.hidden.weight.data) // 2 - child1.hidden.weight.data = torch.cat((parent1.hidden.weight.data[:point], parent2.hidden.weight.data[point:]), dim=0) - child2.hidden.weight.data = torch.cat((parent2.hidden.weight.data[:point], parent1.hidden.weight.data[point:]), dim=0) + point1 = len(child1.hidden1.weight.data) // 2 + point2 = len(child1.hidden2.weight.data) // 2 + child1.hidden1.weight.data = torch.cat((parent1.hidden1.weight.data[:point1], parent2.hidden1.weight.data[point1:]), dim=0) + child2.hidden1.weight.data = torch.cat((parent2.hidden1.weight.data[:point1], parent1.hidden1.weight.data[point1:]), dim=0) + child1.hidden2.weight.data = torch.cat((parent1.hidden2.weight.data[:point2], parent2.hidden2.weight.data[point2:]), dim=0) + child2.hidden2.weight.data = torch.cat((parent2.hidden2.weight.data[:point2], parent1.hidden2.weight.data[point2:]), dim=0) child1.output.weight.data = parent1.output.weight.data.clone().detach() child2.output.weight.data = parent2.output.weight.data.clone().detach() return child1, child2 diff --git a/main.py b/main.py index 61e0bb8..bb652f5 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ POPULATION_SIZE = 50 MUTATION_RATE = 0.5 POPULATION_BEST = 0.2 FRAME_RATE = 160 #TODO: FIX THE INCORRECT FRAME RATE CORRELATION +BARRIER_SPEED = 10 pygame.init() @@ -32,8 +33,6 @@ def init_game(): grass = Background("images/Grass.jpg", 0, 0) grass.set_size(WIDTH, HEIGHT) - BARRIER_SPEED = 1 - gameobjects = [] barriers = [] @@ -65,12 +64,12 @@ def get_features(horse): 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], - HEIGHT, BARRIER_SPEED] + 0, HEIGHT, BARRIER_SPEED] return features -genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, 10) horses = [Horse("images/Horse_1.png", 50, HEIGHT/2 + 0 * i) for i in range(POPULATION_SIZE)] init_game() +genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, len(get_features(horses[0]))) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: # Handle window close event