-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenetic_nn.py
197 lines (170 loc) · 5.53 KB
/
genetic_nn.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy
import random
import tflearn
import argparse
import logging
import pickle
import random
import sys
import cv2
from collections import defaultdict
from sklearn.preprocessing import normalize
import numpy as np
import better_exceptions
import gym
from gym import wrappers
import tensorflow as tf
class Model:
def __init__(self, net, layer):
self.net = net
self.layer = layer
@classmethod
def new(cls) -> tflearn.DNN:
net = tflearn.input_data(shape=[None, 84, 84, 1], name='space')
hidden = net = tflearn.fully_connected(net, 6, activation='relu')
net = tflearn.regression(
net, optimizer='adam', learning_rate=0.01, name='target')
m = tflearn.DNN(net)
print(m.get_weights(hidden.W).shape)
print(m.get_weights(hidden.b).shape)
return cls(m, hidden)
class Genome:
def __init__(self, model, weights, bias):
self.weights = weights
self.bias = bias
self.net = self.model = model
@classmethod
def new(cls, model) -> tflearn.DNN:
w = numpy.random.rand(7056, 6)
b = numpy.random.rand(6)
return cls(model, w, b)
def start(self):
self.model.net.set_weights(self.model.layer.W, self.weights)
self.model.net.set_weights(self.model.layer.b, self.bias)
def end(self):
self.weights = self.model.net.get_weights(self.model.layer.W)
self.bias = self.model.net.get_weights(self.model.layer.b)
def clone(self):
m = self.new(self.model)
m.weights = self.weights
m.bias = self.bias
m.mutate()
return m
def mutate(self) -> tflearn.DNN:
weights = self.weights
dim = int(weights.shape[0] * weights.shape[1] * 0.001)
for _ in range(dim):
col, row = random.randint(0, weights.shape[0] - 1), random.randint(
0, weights.shape[1] - 1)
new_val = weights[col][row] + random.uniform(-1, 1)
weights[col][row] = min(max(new_val, -1), 1)
self.bias[row] += random.uniform(-1, 1)
return weights
def cross_over(self, other):
m = self.new(self.model)
weights = self.weights
other_weights = other.weights
for i, r in enumerate(weights):
if i % 2 == 0:
continue
weights[i] = other_weights[i]
m.weights = weights
bias = self.bias
other_bias = other.bias
for i, r in enumerate(bias):
if i % 2 == 0:
continue
bias[i] = other_bias[i]
m.bias = bias
return m
def make_decision(self, state: np.array) -> int:
state = preprocess(state)
feed = numpy.array(state).reshape(1, 84, 84, 1)
actions = self.model.net.predict(feed)
choosen = numpy.argmax(actions[0])
return choosen
class Pool:
def __init__(self, size=24):
self.net = Model.new()
self.size = size
self.pool = [Genome.new(self.net) for _ in range(self.size)]
self.scores = []
self.gen = 0
def score(self, gen, score):
self.scores.append((score, gen))
def next_gen(self):
print('=' * 50)
self.gen += 1
print('Generation', self.gen)
fit_num = int(self.size * 0.2 + 0.5)
top3 = sorted(self.scores, key=lambda x: x[0])[-fit_num:]
print(top3)
new_pool = []
for j in range(int(self.size / fit_num)):
child = [
top3[-1 - j][1].cross_over(top3[-i][1]).clone()
for i in range(fit_num)
]
new_pool.extend(child)
self.pool = new_pool
self.scores = []
def preprocess(observation: np.array) -> np.array:
"""
Transform 265 x 160 x 3 -> 84 x 84 x 1 world
"""
observation = cv2.cvtColor(
cv2.resize(observation, (84, 110)), cv2.COLOR_BGR2GRAY)
observation = observation[26:110, :]
ret, observation = cv2.threshold(observation, 1, 255, cv2.THRESH_BINARY)
return np.reshape(observation, (84, 84, 1))
def train():
env = gym.make('SpaceInvaders-v0')
outdir = '/tmp/q-space-func'
# env = wrappers.Monitor(env, directory=outdir, force=True)
env.seed(0)
episode_count = 100
reward = 0
done = False
max_score = 0
pool = Pool()
all_time_max = 0
for i in range(episode_count):
for m in pool.pool:
m.start()
state = env.reset()
if max_score > all_time_max: # Enable recording
pool.net.net.save("savepoint/model.tfl".format(i, max_score))
all_time_max = max(all_time_max, max_score)
print('Gen', i)
print('#' * 50)
print("Current score", max_score)
print("Max score", all_time_max)
max_score = 0
while True:
action = m.make_decision(state)
state_ = state
state, reward, done, info = env.step(action)
max_score += reward
if done:
break
env.render()
m.end()
pool.score(m, max_score)
pool.next_gen()
env.close()
env = wrappers.Monitor(env, directory=outdir, force=True)
m = pool.pool[0]
m.start()
state = env.reset()
while True:
action = m.make_decision(state)
state_ = state
state, reward, done, info = env.step(action)
max_score += reward
if done:
break
env.render()
m.end()
env.close()
if __name__ == '__main__':
train()