forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy2envs.py
184 lines (137 loc) · 4.21 KB
/
flappy2envs.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
# https://deeplearningcourses.com/c/cutting-edge-artificial-intelligence
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
from ple import PLE
from ple.games.flappybird import FlappyBird
import sys
from threading import Thread
HISTORY_LENGTH = 1
class Env:
def __init__(self):
self.game = FlappyBird(pipe_gap=125)
self.env = PLE(self.game, fps=30, display_screen=True)
self.env.init()
self.env.getGameState = self.game.getGameState # maybe not necessary
# by convention we want to use (0,1)
# but the game uses (None, 119)
self.action_map = self.env.getActionSet() #[None, 119]
def step(self, action):
action = self.action_map[action]
reward = self.env.act(action)
done = self.env.game_over()
obs = self.get_observation()
# don't bother returning an info dictionary like gym
return obs, reward, done
def reset(self):
self.env.reset_game()
return self.get_observation()
def get_observation(self):
# game state returns a dictionary which describes
# the meaning of each value
# we only want the values
obs = self.env.getGameState()
return np.array(list(obs.values()))
def set_display(self, boolean_value):
self.env.display_screen = boolean_value
# make a global environment to be used throughout the script
env = Env()
### neural network
# hyperparameters
D = len(env.reset())*HISTORY_LENGTH
M = 50
K = 2
def softmax(a):
c = np.max(a, axis=1, keepdims=True)
e = np.exp(a - c)
return e / e.sum(axis=-1, keepdims=True)
def relu(x):
return x * (x > 0)
class ANN:
def __init__(self, D, M, K, f=relu):
self.D = D
self.M = M
self.K = K
self.f = f
def init(self):
D, M, K = self.D, self.M, self.K
self.W1 = np.random.randn(D, M) / np.sqrt(D)
# self.W1 = np.zeros((D, M))
self.b1 = np.zeros(M)
self.W2 = np.random.randn(M, K) / np.sqrt(M)
# self.W2 = np.zeros((M, K))
self.b2 = np.zeros(K)
def forward(self, X):
Z = self.f(X.dot(self.W1) + self.b1)
return softmax(Z.dot(self.W2) + self.b2)
def sample_action(self, x):
# assume input is a single state of size (D,)
# first make it (N, D) to fit ML conventions
X = np.atleast_2d(x)
P = self.forward(X)
p = P[0] # the first row
# return np.random.choice(len(p), p=p)
return np.argmax(p)
def score(self, X, Y):
P = np.argmax(self.forward(X), axis=1)
return np.mean(Y == P)
def get_params(self):
# return a flat array of parameters
return np.concatenate([self.W1.flatten(), self.b1, self.W2.flatten(), self.b2])
def get_params_dict(self):
return {
'W1': self.W1,
'b1': self.b1,
'W2': self.W2,
'b2': self.b2,
}
def set_params(self, params):
# params is a flat list
# unflatten into individual weights
D, M, K = self.D, self.M, self.K
self.W1 = params[:D * M].reshape(D, M)
self.b1 = params[D * M:D * M + M]
self.W2 = params[D * M + M:D * M + M + M * K].reshape(M, K)
self.b2 = params[-K:]
env1, env2 = Env(), Env()
def reward_function(params, env):
model = ANN(D, M, K)
model.set_params(params)
# play one episode and return the total reward
episode_reward = 0
episode_length = 0 # not sure if it will be used
done = False
obs = env.reset()
obs_dim = len(obs)
if HISTORY_LENGTH > 1:
state = np.zeros(HISTORY_LENGTH*obs_dim) # current state
state[obs_dim:] = obs
else:
state = obs
while not done:
# get the action
action = model.sample_action(state)
# perform the action
obs, reward, done = env.step(action)
# update total reward
episode_reward += reward
episode_length += 1
# update state
if HISTORY_LENGTH > 1:
state = np.roll(state, -obs_dim)
state[-obs_dim:] = obs
else:
state = obs
print("Reward:", episode_reward)
if __name__ == '__main__':
j = np.load('es_flappy_results.npz')
best_params = np.concatenate([j['W1'].flatten(), j['b1'], j['W2'].flatten(), j['b2']])
# in case D isn't correct
D, M = j['W1'].shape
K = len(j['b2'])
t1 = Thread(target=reward_function, args=(best_params, env1))
t2 = Thread(target=reward_function, args=(best_params, env2))
t1.start()
t2.start()
t1.join()
t2.join()