-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartpole-agent-dqn.py
83 lines (70 loc) · 2.82 KB
/
cartpole-agent-dqn.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
import sys
import gym
import numpy as np
import random
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras import optimizers
from collections import deque
class DQNAgent:
def __init__(self, state_size, action_size):
self.state_size = state_size
self.action_size = action_size
self.memory = deque(maxlen=2000)
self.gamma = 0.95 # discount rate
self.epsilon = 1.0 # exploration rate
self.epsilon_min = 0.01
self.epsilon_decay = 0.995
self.learning_rate = 0.001
self.model = self._build_model()
def _build_model(self):
model = Sequential()
model.add(Dense(24, input_dim=self.state_size, activation='relu'))
model.add(Dense(24, activation='relu'))
model.add(Dense(self.action_size, activation='linear'))
model.compile(loss='mse', optimizer=optimizers.Adam(lr=self.learning_rate))
return model
def remember(self, state, action, reward, next_state, done):
self.memory.append((state.reshape(1,4), action, reward, next_state.reshape(1,4), done))
def act(self, state):
if np.random.rand() <= self.epsilon:
return random.randrange(self.action_size)
act_values = self.model.predict(state.reshape(1,4))
return np.argmax(act_values[0]) # returns action
def replay(self, batch_size):
for state, action, reward, next_state, done in random.sample(self.memory, batch_size):
target = reward
if not done:
target = reward + self.gamma * np.amax(self.model.predict(next_state)[0])
target_f = self.model.predict(state)
target_f[0][action] = target
self.model.fit(state, target_f, epochs=1, verbose=0)
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
def load(self, name):
self.model.load_weights(name)
def save(self, name):
self.model.save_weights(name)
EPISODES = 100
if __name__ == "__main__":
env = gym.make('CartPole-v0')
state_size = env.observation_space.shape[0]
action_size = env.action_space.n
agent = DQNAgent(state_size, action_size)
batch_size = 32
# agent.load("save/cartpole-dqn.h5")
for e in range(1, EPISODES):
state = env.reset()
for time_t in range(500):
action = agent.act(state)
next_state, reward, done, _ = env.step(action)
agent.remember(state, action, reward, next_state, done)
state = next_state
if e%10 == 0:
env.render()
agent.save("save/cartpole-dqn.h5")
if len(agent.memory)>batch_size:
agent.replay(batch_size)
if done:
print("Episode: {}/{}, Score: {}".format(e, EPISODES, time_t))
break