-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPyrace_RL.py
More file actions
229 lines (193 loc) · 7.4 KB
/
Pyrace_RL.py
File metadata and controls
229 lines (193 loc) · 7.4 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import sys
import numpy as np
import math
import random
import matplotlib.pyplot as plt
import gym
import gym_race
def simulate():
learning_rate = get_learning_rate(0)
explore_rate = get_explore_rate(0)
discount_factor = 0.99
total_reward = 0
total_rewards = []
training_done = False
threshold = 1000
env.set_view(True)
for episode in range(NUM_EPISODES):
total_rewards.append(total_reward)
if episode == 50000:
plt.plot(total_rewards)
plt.ylabel('rewards')
plt.show()
env.save_memory('50000')
break
obv = env.reset()
state_0 = state_to_bucket(obv)
total_reward = 0
if episode >= threshold:
explore_rate = 0.01
for t in range(MAX_T):
action = select_action(state_0, explore_rate)
obv, reward, done, _ = env.step(action)
state = state_to_bucket(obv)
env.remember(state_0, action, reward, state, done)
total_reward += reward
# Update the Q based on the result
best_q = np.amax(q_table[state])
q_table[state_0 + (action,)] += learning_rate * (reward + discount_factor * (best_q) - q_table[state_0 + (action,)])
# Setting up for the next iteration
state_0 = state
env.render()
if done or t >= MAX_T - 1:
print("Episode %d finished after %i time steps with total reward = %f."
% (episode, t, total_reward))
break
# Update parameters
explore_rate = get_explore_rate(episode)
learning_rate = get_learning_rate(episode)
def load_and_play():
print("Start loading history")
history_list = ['30000.npy']
# load data from history file
print("Start updating q_table")
discount_factor = 0.99
for list in history_list:
history = load_data(list)
learning_rate = get_learning_rate(0)
print(list)
file_size = len(history)
print("file size : " + str(file_size))
i = 0
for data in history:
state_0, action, reward, state, done = data
best_q = np.amax(q_table[state])
q_table[state_0 + (action,)] += learning_rate * (reward + discount_factor * (best_q) - q_table[state_0 + (action,)])
if done == True:
i += 1
learning_rate = get_learning_rate(i)
print("Updating q_table is complete")
# play game
env.set_view(True)
reward_count = 0
for episode in range(NUM_EPISODES):
obv = env.reset()
state_0 = state_to_bucket(obv)
total_reward = 0
for t in range(MAX_T):
action = select_action(state_0, 0.01)
obv, reward, done, _ = env.step(action)
state = state_to_bucket(obv)
total_reward += reward
best_q = np.amax(q_table[state])
q_table[state_0 + (action,)] += learning_rate * (reward + discount_factor * (best_q) - q_table[state_0 + (action,)])
state_0 = state
env.render()
if done or t >= MAX_T - 1:
print("Episode %d finished after %i time steps with total reward = %f."
% (episode, t, total_reward))
break
if total_reward >= 1000:
reward_count += 1
else:
reward_count = 0
if reward_count >= 10:
env.set_view(True)
learning_rate = get_learning_rate(i + episode)
def load_and_simulate():
print("Start loading history")
history_list = ['30000.npy']
# load data from history file
print("Start updating q_table")
discount_factor = 0.99
i = 0
for list in history_list:
history = load_data(list)
learning_rate = get_learning_rate(0)
print(list)
file_size = len(history)
print("file size : " + str(file_size))
for data in history:
state_0, action, reward, state, done = data
env.remember(state_0, action, reward, state, done)
best_q = np.amax(q_table[state])
q_table[state_0 + (action,)] += learning_rate * (reward + discount_factor * (best_q) - q_table[state_0 + (action,)])
if done == True:
i += 1
learning_rate = get_learning_rate(i)
print("Updating q_table is complete")
# simulate
env.set_view(False)
for episode in range(NUM_EPISODES):
obv = env.reset()
state_0 = state_to_bucket(obv)
total_reward = 0
if episode > 3000 and episode <= 3010:
if episode == 3001:
env.save_memory('3000_aft')
env.set_view(True)
elif episode > 5000 and episode <= 5010:
if episode == 5001:
env.save_memory('5000_aft')
env.set_view(True)
for t in range(MAX_T):
action = select_action(state_0, 0.01)
obv, reward, done, _ = env.step(action)
state = state_to_bucket(obv)
env.remember(state_0, action, reward, state, done)
state_0 = state
total_reward += reward
best_q = np.amax(q_table[state])
q_table[state_0 + (action,)] += learning_rate * (reward + discount_factor * (best_q) - q_table[state_0 + (action,)])
env.render()
if done or t >= MAX_T - 1:
print("Episode %d finished after %i time steps with total reward = %f."
% (episode, t, total_reward))
break
learning_rate = get_learning_rate(i + episode)
def select_action(state, explore_rate):
if random.random() < explore_rate:
action = env.action_space.sample()
else:
action = int(np.argmax(q_table[state]))
return action
def get_explore_rate(t):
return max(MIN_EXPLORE_RATE, min(0.8, 1.0 - math.log10((t+1)/DECAY_FACTOR)))
def get_learning_rate(t):
return max(MIN_LEARNING_RATE, min(0.8, 1.0 - math.log10((t+1)/DECAY_FACTOR)))
def state_to_bucket(state):
bucket_indice = []
for i in range(len(state)):
if state[i] <= STATE_BOUNDS[i][0]:
bucket_index = 0
elif state[i] >= STATE_BOUNDS[i][1]:
bucket_index = NUM_BUCKETS[i] - 1
else:
# Mapping the state bounds to the bucket array
bound_width = STATE_BOUNDS[i][1] - STATE_BOUNDS[i][0]
offset = (NUM_BUCKETS[i]-1)*STATE_BOUNDS[i][0]/bound_width
scaling = (NUM_BUCKETS[i]-1)/bound_width
bucket_index = int(round(scaling*state[i] - offset))
bucket_indice.append(bucket_index)
return tuple(bucket_indice)
def load_data(file):
np_load_old = np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
data = np.load(file)
np.load = np_load_old
return data
if __name__ == "__main__":
env = gym.make("Pyrace-v0")
NUM_BUCKETS = tuple((env.observation_space.high + np.ones(env.observation_space.shape)).astype(int))
NUM_ACTIONS = env.action_space.n
STATE_BOUNDS = list(zip(env.observation_space.low, env.observation_space.high))
MIN_EXPLORE_RATE = 0.001
MIN_LEARNING_RATE = 0.2
DECAY_FACTOR = np.prod(NUM_BUCKETS, dtype=float) / 10.0
print(DECAY_FACTOR)
NUM_EPISODES = 9999999
MAX_T = 2000
#MAX_T = np.prod(NUM_BUCKETS, dtype=int) * 100
q_table = np.zeros(NUM_BUCKETS + (NUM_ACTIONS,), dtype=float)
simulate()
#load_and_play()