-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (56 loc) · 1.87 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import time
from PIL import ImageGrab
import cv2
import pyautogui
# Tamanho da tela do jogo
SCREEN_REGION = (0, 40, 800, 540)
# Parâmetros genéticos
POPULATION_SIZE = 50
MUTATION_RATE = 0.1
def capture_screen():
screen = np.array(ImageGrab.grab(bbox=SCREEN_REGION))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
return screen
def jump():
pyautogui.press('up')
def run_generation(population):
for i in range(len(population)):
screen = capture_screen()
if np.mean(screen) < 200: # Se a tela estiver escura, o dinossauro pulou um obstáculo
population[i]['fitness'] += 1
jump()
time.sleep(0.1)
else:
population[i]['fitness'] -= 1
def select_parents(population):
fitness_scores = np.array([individual['fitness'] for individual in population])
probabilities = fitness_scores / fitness_scores.sum()
parents = np.random.choice(population, size=2, p=probabilities, replace=False)
return parents
def crossover(parent1, parent2):
child = {}
for key in parent1:
if np.random.rand() > 0.5:
child[key] = parent1[key]
else:
child[key] = parent2[key]
return child
def mutate(child):
for key in child:
if np.random.rand() < MUTATION_RATE:
child[key] = np.random.uniform(-1, 1)
return child
# Inicialização da população
population = [{'weights': np.random.uniform(-1, 1, 4), 'fitness': 0} for _ in range(POPULATION_SIZE)]
# Loop principal
generation = 0
while True:
print(f"Generation {generation}")
run_generation(population)
population.sort(key=lambda x: x['fitness'], reverse=True)
parents = select_parents(population)
child = crossover(parents[0]['weights'], parents[1]['weights'])
child = mutate(child)
population[-1]['weights'] = child
generation += 1