update: horses now probably learn

This commit is contained in:
KOSMOGOR
2025-04-10 23:34:47 +03:00
parent 0128f33f44
commit e6735af700
3 changed files with 29 additions and 19 deletions
+18 -11
View File
@@ -5,9 +5,9 @@ import numpy as np
class NeuralNetwork(nn.Module):
def __init__(self, inputSize):
super().__init__()
self.hidden1 = nn.Linear(inputSize, 32)
self.hidden2 = nn.Linear(32, 16)
self.output = nn.Linear(16, 3)
self.hidden1 = nn.Linear(inputSize, 128)
self.hidden2 = nn.Linear(128, 128)
self.output = nn.Linear(128, 3)
def forward(self, x):
x = torch.relu(self.hidden1(x))
@@ -16,20 +16,25 @@ class NeuralNetwork(nn.Module):
return torch.argmax(x)
class GeneticAlgorithm:
def __init__(self, populationSize: int, mutationRate: float, percentageBest: float, inputSize: int = 5):
def __init__(self, populationSize: int, mutationRate: float, percentageBest: float, percentageNew: float, inputSize: int = 5):
self.populationSize = populationSize
self.mutationRate = mutationRate
self.percentageBest = percentageBest
self.percentageNew = percentageNew
self.inputSize = inputSize
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(self.device)
self.initialize_population()
def create_ind(self):
return NeuralNetwork(self.inputSize).to(self.device)
def initialize_population(self):
self.population = [NeuralNetwork(self.inputSize).to(self.device) for _ in range(self.populationSize)]
self.population = [self.create_ind() for _ in range(self.populationSize)]
def crossover(self, parent1: NeuralNetwork, parent2: NeuralNetwork):
child1 = NeuralNetwork(self.inputSize).to(self.device)
child2 = NeuralNetwork(self.inputSize).to(self.device)
child1 = self.create_ind()
child2 = self.create_ind()
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)
@@ -44,21 +49,23 @@ 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
param.data += torch.randn_like(param.data) * 0.1 * (1 if torch.rand(1).item() >= 0.5 else -1)
return model
def learn(self, fitness: list):
self.population = [self.population[x] for x in np.argsort(fitness)]
self.population = [self.population[x] for x in np.argsort(fitness)[::-1]]
numBest = int(self.populationSize * self.percentageBest)
self.population = self.population[:numBest]
while len(self.population) < self.populationSize:
while len(self.population) < self.populationSize - self.populationSize * self.percentageNew:
parent1, parent2 = np.random.choice(self.population), np.random.choice(self.population)
child1, child2 = self.crossover(parent1, parent2)
child1 = self.mutate(child1)
child2 = self.mutate(child2)
self.population.extend([child1, child2])
while len(self.population) < self.populationSize: self.population.append(self.create_ind())
while len(self.population) > self.populationSize: self.population.pop()
def predict(self, data: list, i):
data = torch.tensor(data, requires_grad=False).float().to(self.device)
return self.population[i](data)
return self.population[i](data)