-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunBestFlappyNetwork.py
More file actions
148 lines (129 loc) · 4.17 KB
/
RunBestFlappyNetwork.py
File metadata and controls
148 lines (129 loc) · 4.17 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import random
import time
import decimal
os.system('mode con: cols=174 lines=48')
pipesFlownThrough = 0
playerHeight = 17
playerVelocity = 1
pipeWidth = 10
pipeHeight = 10
numPipes = 2
pipes = []
fitness = 0
layerValues = [[]]
def sigma(x):
if x < 0:
return 0
return x
class Neuron:
def __init__(self, strRep=""):
x = strRep.split(":")
self.bias = float(x[0])
self.weights = x[1::]
for i in range(len(self.weights)):
self.weights[i] = float(self.weights[i])
def calc(self, inputs):
sum = self.bias
for i in range(len(inputs)):
sum += inputs[i] * self.weights[i]
return sigma(sum)
def mutate(self):
for i in range(len(self.weights)):
self.weights[i] += ((random.random() / 20) - 0.025) + self.weights[i]
def child(self, other):
for i in range(len(self.weights)):
self.weights[i] = (self.weights[i] + other.weights[i]) / 2
def toString(self):
stri = str(self.bias)
for i in self.weights:
stri += ":" + str(i)
return stri
class Network:
def __init__(self, strRep):
self.layers = []
x = strRep.split("|")[1::]
for i in range(len(x)):
self.layers.append([])
for j in range(len(x[i].split(",")))[1::]:
self.layers[i].append(Neuron(x[i].split(",")[j]))
def toString(self):
str = ""
for j in self.layers:
str += "|"
for i in j:
str += ","
str += i.toString()
return str
def calc(self, inputs):
global layerValues
layerValues = [inputs]
for i in range(len(self.layers)):
layerValues.append([])
for j in range(len(self.layers[i])):
layerValues[i + 1].append(self.layers[i][j].calc(layerValues[i]))
return layerValues[len(layerValues) - 1]
f = open("BestFlappyNetwork.txt","r")
net = Network(f.read().split("\n")[1])
f.close
def display():
global layerValues
string = " |"
out = True
if playerHeight < 0 or playerHeight > 35:
return True
for i in range(36):
for j in range(150):
flag = True
for y in pipes:
if flag and j == 2 and i == round(playerHeight):
string += "v"
out = flag and j <= y[1] + (pipeWidth / 2) and j >= y[1] - (pipeWidth / 2) \
and (i > y[0] + (pipeHeight / 2) or i < y[0] - (pipeHeight / 2))
flag = False
if flag and j <= y[1] + (pipeWidth / 2) and j >= y[1] - (pipeWidth / 2) \
and (i > y[0] + (pipeHeight / 2) or i < y[0] - (pipeHeight / 2)):
string += "."
flag = False
if j == 2 and i == round(playerHeight):
out = True
if flag:
string += " "
string += "|\n |"
string = string[:-2:] + ("=" * 154) + "\n"
for i in layerValues:
string += "\n"
for x in i:
string += "{:.5E}".format(float(str(x))) + "\t"
print("\033[H\033[J" + ("=" * 154) + "\n" + string + "\n\npipes flown through: " + str(pipesFlownThrough) + "\n")
return out
def closestPipeHeight():
minX = 9999
y = 10000
for i in pipes:
if i[1] < minX:
y = i[0]
minX = i[1]
return (minX, y)
while True:
if display():
playerHeight = 10
playerVelocity = 1
pipes = []
pipesFlownThrough = 0
for i in range(numPipes):
pipes += [[random.randint(pipeHeight, 35 - pipeHeight), (i + 1) * (150 / numPipes)]]
fitness = 0
for y in pipes:
y[1] -= 1
if y[1] < -0.5 * pipeWidth:
y[0] = random.randint(round(pipeHeight / 2), round(35 - (pipeHeight / 2)))
y[1] = 149 + (pipeWidth / 2)
pipesFlownThrough += 1
playerHeight += playerVelocity
if net.calc([playerHeight, closestPipeHeight()[1], closestPipeHeight()[0] / 8])[0] > 0:
playerVelocity = -0.3
else:
playerVelocity = 0.3
time.sleep(0.005)
fitness += 1