forked from EnJiang/USV-Game
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_one_step.py
214 lines (173 loc) · 6.64 KB
/
main_one_step.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# this is for display currently!!!!
from environment import OnePlayerEnv
from policy import TestPolicy
from world import OneStepWorld
from time import sleep
from time import time as now
import random
import gym
import numpy as np
from collections import deque
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
from keras.optimizers import Adam
from dqn_one_step_test import model
class DQNAgent:
def __init__(self, state_size, action_size, mode, model=None):
self.state_size = state_size
self.action_size = action_size
self.memory = deque(maxlen=200000)
self.gamma = 0.95 # discount rate
self.epsilon = 1.0 # exploration rate
self.epsilon_min = 0.01
self.epsilon_decay = 0.99985
self.learning_rate = 0.001
assert mode in ["test", "train"]
if mode == "test":
self.epsilon = self.epsilon_min
if model:
self.model = model
else:
self.model = self._build_model()
def _build_model(self):
# Neural Net for Deep-Q learning Model
model = Sequential()
model.add(Conv2D(32, (2, 2),input_shape=(
1, 10, 10),
activation='selu',
kernel_initializer='lecun_normal',
bias_initializer='lecun_normal',
data_format="channels_first"))
model.add(Flatten())
model.add(Dense(512, activation='selu', kernel_initializer='lecun_normal', bias_initializer='lecun_normal'))
model.add(Dense(64, activation='selu', kernel_initializer='lecun_normal', bias_initializer='lecun_normal'))
model.add(Dense(32, activation='selu',
kernel_initializer='lecun_normal', bias_initializer='lecun_normal'))
model.add(Dense(self.action_size, activation='linear'))
model.compile(loss='mse',
optimizer=Adam(lr=self.learning_rate))
model.summary()
sleep(2)
return model
def remember(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def act(self, state, guide_action):
if np.random.rand() <= self.epsilon:
if np.random.rand() <= 0.1 or guide_action is None:
return random.randrange(self.action_size)
else:
return guide_action
state = np.array([state])
act_values = self.model.predict(state)
# exit()
return np.argmax(act_values[0]) # returns action
def replay(self, batch_size):
start = now()
x = []
y = []
minibatch = random.sample(self.memory, batch_size)
states = []
next_states = []
for state, action, reward, next_state, done in minibatch:
states.append(state)
next_states.append(next_state)
next_states = np.array(next_states)
predicted_q = self.model.predict(next_states)
states = np.array(states)
predicted_f = self.model.predict(states)
for i, r in enumerate(minibatch):
state, action, reward, next_state, done = r
target = reward
# print(self.model.predict(next_state))
# exit()
if not done:
target = (reward + self.gamma *
# np.amax(self.model.predict(next_state)))
np.amax(predicted_q[i]))
target_f = predicted_f[i]
# print(target_f)
# exit()
target_f[action] = target
x.append(state.reshape(1, 10, 10))
y.append(target_f.reshape(4))
x = np.array(x)
y = np.array(y)
self.model.fit(x, y, epochs=3, verbose=0)
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
print(now() - start)
def load(self, name):
self.model.load_weights(name)
def save(self, name):
self.model.save_weights(name)
EPISODES = 100000
warm_up_step = 18000
if __name__ == "__main__":
# env = gym.make('CartPole-v1')
w = OneStepWorld(TestPolicy)
env = OnePlayerEnv(w)
state_size = 100
action_size = 4
agent = DQNAgent(state_size, action_size, model=model, mode="test")
# agent.load("./save/cartpole-dqn.h5")
done = False
batch_size = 1024
print("warming up...")
while 0:
print(len(agent.memory))
state = env.reset()
# state = np.reshape(state, [1, state_size])
state = np.reshape(state, [1, 10, 10])
s = 0
t = 0
for time in range(500):
try:
a_star_action = env.world.policy_agents[0].finda()
a_star_action_i = env.world.action_space.index(a_star_action)
except:
a_star_action_i = None
action = agent.act(state, a_star_action_i)
# print(action)
next_state, reward, done, _ = env.step([action])
# reward = reward if not done else -100
s += reward
t += 1
# next_state = np.reshape(next_state, [1, state_size])
next_state = np.reshape(next_state, [1, 10, 10])
agent.remember(state, action, reward, next_state, done)
state = next_state
if done:
break
if len(agent.memory) > warm_up_step:
break
for e in range(EPISODES):
state = env.reset()
# state = np.reshape(state, [1, state_size])
state = np.reshape(state, [1, 10, 10])
s = 0
t = 0
for time in range(500):
try:
a_star_action = env.world.policy_agents[0].finda()
a_star_action_i = env.world.action_space.index(a_star_action)
except:
a_star_action_i = None
action = agent.act(state, a_star_action_i)
# print(action)
next_state, reward, done, _ = env.step([action])
# reward = reward if not done else -100
s += reward
t += 1
# next_state = np.reshape(next_state, [1, state_size])
next_state = np.reshape(next_state, [1, 10, 10])
agent.remember(state, action, reward, next_state, done)
state = next_state
if done:
print("episode: {}/{}, score: {}, e: {:.2}, end_r: {}, path_len: {}"
.format(e, EPISODES, s / t, agent.epsilon, reward, time + 1))
break
if len(agent.memory) > warm_up_step:
pass
# agent.replay(batch_size)
# if e % 3000 == 0 and e > 0:
# agent.save("%d.ml" % e)