add: genetic now working
This commit is contained in:
+7
-3
@@ -4,14 +4,14 @@ import numpy as np
|
|||||||
|
|
||||||
class NeuralNetwork(nn.Module):
|
class NeuralNetwork(nn.Module):
|
||||||
def __init__(self, inputSize):
|
def __init__(self, inputSize):
|
||||||
super(NeuralNetwork, self).__init__()
|
super().__init__()
|
||||||
self.hidden = nn.Linear(inputSize, 10)
|
self.hidden = nn.Linear(inputSize, 10)
|
||||||
self.output = nn.Linear(10, 3)
|
self.output = nn.Linear(10, 3)
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
x = torch.relu(self.hidden(x))
|
x = torch.relu(self.hidden(x))
|
||||||
x = self.output(x)
|
x = self.output(x)
|
||||||
return torch.log_softmax(x)
|
return torch.argmax(x)
|
||||||
|
|
||||||
class GeneticAlgorithm:
|
class GeneticAlgorithm:
|
||||||
def __init__(self, populationSize: int, mutationRate: float, percentageBest: float, inputSize: int = 5):
|
def __init__(self, populationSize: int, mutationRate: float, percentageBest: float, inputSize: int = 5):
|
||||||
@@ -54,7 +54,11 @@ class GeneticAlgorithm:
|
|||||||
self.population.extend([child1, child2])
|
self.population.extend([child1, child2])
|
||||||
while len(self.population) > self.populationSize: self.population.pop()
|
while len(self.population) > self.populationSize: self.population.pop()
|
||||||
|
|
||||||
def predict(self, data: list):
|
def predict(self, data: list, i):
|
||||||
|
data = torch.tensor(data, requires_grad=False).float()
|
||||||
|
return self.population[i](data)
|
||||||
|
|
||||||
|
def predict_all(self, data: list):
|
||||||
result = []
|
result = []
|
||||||
for i in range(self.populationSize):
|
for i in range(self.populationSize):
|
||||||
result.append(self.population[i](data[i]))
|
result.append(self.population[i](data[i]))
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ def get_features(horse):
|
|||||||
HEIGHT, BARRIER_SPEED]
|
HEIGHT, BARRIER_SPEED]
|
||||||
return features
|
return features
|
||||||
|
|
||||||
genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST)
|
genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST, 10)
|
||||||
|
|
||||||
UI.add_horses(horses) # Add horses to UI
|
UI.add_horses(horses) # Add horses to UI
|
||||||
while True:
|
while True:
|
||||||
@@ -56,14 +56,16 @@ while True:
|
|||||||
|
|
||||||
keys = pygame.key.get_pressed() # Get pressed keys
|
keys = pygame.key.get_pressed() # Get pressed keys
|
||||||
|
|
||||||
for horse in horses:
|
for i, horse in enumerate(horses):
|
||||||
if horse.stopped == False: # If the horse is not stopped
|
if horse.stopped == False: # If the horse is not stopped
|
||||||
if keys[pygame.K_UP]: # Move horse up if UP key is pressed
|
data = get_features(horse)
|
||||||
|
res = genecticAlg.predict(data, i)
|
||||||
|
if res == 0:
|
||||||
horse.up()
|
horse.up()
|
||||||
elif keys[pygame.K_DOWN]: # Move horse down if DOWN key is pressed
|
elif res == 1:
|
||||||
horse.down()
|
horse.down()
|
||||||
else:
|
else:
|
||||||
horse.stay() # Stop vertical movement if no key is pressed
|
horse.stay()
|
||||||
else:
|
else:
|
||||||
horse.move(-BARRIER_SPEED, 0) # Move stopped horse to the left
|
horse.move(-BARRIER_SPEED, 0) # Move stopped horse to the left
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user