-
Notifications
You must be signed in to change notification settings - Fork 0
/
trex.py
76 lines (65 loc) · 2.68 KB
/
trex.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
69
70
71
72
73
74
75
76
import copy
import random
from time import sleep
import numpy as np
import pyautogui
from camera import Camera
from neural_network import NeuralNetwork
class TRex:
def __init__(self):
self.__genomes = [NeuralNetwork() for i in range(12)]
self.__best_genomes = []
def execute(self):
camera = Camera()
camera.find_environment()
for genome in self.__genomes:
camera.reset()
pyautogui.click(200, 400)
pyautogui.press('F5')
sleep(1)
pyautogui.press('space')
while True:
try:
obs = camera.find_next_obstacle()
inputs = [obs['distance'] / 1000, obs['length'], obs['speed'] / 10]
outputs = genome.forward_propagate(np.array(inputs, dtype=float))
print outputs[0]
if outputs[0] > 0.55:
print "JUMP"
pyautogui.press('space')
except Exception as E:
print str(E)
break
genome.fitness = camera.get_fitness()
def keep_best_genomes(self):
self.__genomes.sort(key=lambda x: x.fitness, reverse=True)
self.__genomes = self.__genomes[:4]
self.__best_genomes = self.__genomes[:]
def mutations(self):
while len(self.__genomes) < 10:
genome1 = random.choice(self.__best_genomes)
genome2 = random.choice(self.__best_genomes)
self.__genomes.append(self.mutate(self.cross_over(genome1, genome2)))
while len(self.__genomes) < 12:
genome = random.choice(self.__best_genomes)
self.__genomes.append(self.mutate(genome))
def cross_over(self, genome1, genome2):
new_genome = copy.deepcopy(genome1)
other_genome = copy.deepcopy(genome2)
cut_location = int(len(new_genome.W1) * random.uniform(0, 1))
for i in range(cut_location):
new_genome.W1[i], other_genome.W1[i] = other_genome.W1[i], new_genome.W1[i]
cut_location = int(len(new_genome.W2) * random.uniform(0, 1))
for i in range(cut_location):
new_genome.W2[i], other_genome.W2[i] = other_genome.W2[i], new_genome.W2[i]
return new_genome
def __mutate_weights(self, weights):
if random.uniform(0, 1) < 0.2:
return weights * (random.uniform(0, 1) - 0.5) * 3 + (random.uniform(0, 1) - 0.5)
else:
return 0
def mutate(self, genome):
new_genome = copy.deepcopy(genome)
new_genome.W1 += self.__mutate_weights(new_genome.W1)
new_genome.W2 += self.__mutate_weights(new_genome.W2)
return new_genome