forked from nalsil/kimhun_rl_windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_2_dqn_2013_cartpole.py
132 lines (99 loc) · 3.89 KB
/
07_2_dqn_2013_cartpole.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
'''
This code is based on
https://github.com/hunkim/DeepRL-Agents
CF https://github.com/golbin/TensorFlow-Tutorials
https://github.com/dennybritz/reinforcement-learning/blob/master/DQN/dqn.py
'''
import numpy as np
import tensorflow as tf
import random
from collections import deque
from dqn import dqn
import gym
from gym import wrappers
env = gym.make('CartPole-v0')
# Constants defining our neural network
input_size = env.observation_space.shape[0]
output_size = env.action_space.n
dis = 0.9
REPLAY_MEMORY = 50000
def simple_replay_train(DQN, train_batch):
x_stack = np.empty(0).reshape(0, DQN.input_size)
y_stack = np.empty(0).reshape(0, DQN.output_size)
# Get stored information from the buffer
for state, action, reward, next_state, done in train_batch:
Q = DQN.predict(state)
# terminal?
if done:
Q[0, action] = reward
else:
# Obtain the Q' values by feeding the new state through our network
Q[0, action] = reward + dis * np.max(DQN.predict(next_state))
y_stack = np.vstack([y_stack, Q])
x_stack = np.vstack( [x_stack, state])
# Train our network using target and predicted Q values on each episode
return DQN.update(x_stack, y_stack)
def get_copy_var_ops(*, dest_scope_name="target", src_scope_name="main"):
# Copy variables src_scope to dest_scope
op_holder = []
src_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=src_scope_name)
dest_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=dest_scope_name)
for src_var, dest_var in zip(src_vars, dest_vars):
op_holder.append(dest_var.assign(src_var.value()))
return op_holder
def bot_play(mainDQN, env=env):
# See our trained network in action
state = env.reset()
reward_sum = 0
while True:
env.render()
action = np.argmax(mainDQN.predict(state))
state, reward, done, _ = env.step(action)
reward_sum += reward
if done:
print("Total score: {}".format(reward_sum))
break
def main():
max_episodes = 5000
# store the previous observations in replay memory
replay_buffer = deque()
with tf.Session() as sess:
mainDQN = dqn.DQN(sess, input_size, output_size)
tf.global_variables_initializer().run()
for episode in range(max_episodes):
e = 1. / ((episode / 10) + 1)
done = False
step_count = 0
state = env.reset()
while not done:
if np.random.rand(1) < e:
action = env.action_space.sample()
else:
# Choose an action by greedily from the Q-network
action = np.argmax(mainDQN.predict(state))
# Get new state and reward from environment
next_state, reward, done, _ = env.step(action)
if done: # Penalty
reward = -100
# Save the experience to our buffer
replay_buffer.append((state, action, reward, next_state, done))
if len(replay_buffer) > REPLAY_MEMORY:
replay_buffer.popleft()
state = next_state
step_count += 1
if step_count > 10000: # Good enough. Let's move on
break
print("Episode: {} steps: {}".format(episode, step_count))
if step_count > 10000:
pass
# break
if episode % 10 == 1: # train every 10 episode
# Get a random batch of experiences
for _ in range(50):
# Minibatch works better
minibatch = random.sample(replay_buffer, 10)
loss, _ = simple_replay_train(mainDQN, minibatch)
print("Loss: ", loss)
bot_play(mainDQN)
if __name__ == "__main__":
main()