-
Notifications
You must be signed in to change notification settings - Fork 2
/
deep_q_learning.py
155 lines (114 loc) · 4.71 KB
/
deep_q_learning.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
import math
import gym
import numpy as np
from matplotlib import pyplot as plt
"""
CartPole-v1 is considered "solved" when the agent obtains a reward of at
least 400 over 50 consecutive episodes.
"""
MAX_STEPS = 500
class DeepQAgent():
def __init__(self, episodes=2000, lr=0.01, epsilon=1.0, epsilon_min=0.01, gamma=0.9):
"""
Construct a new Deep Q-learning agent.
:param episodes: Number of training episodes
:param lr: Learning rate
:param epsilon: Exploration rate
:param epsilon: Minimum decayed exploration rate
:param gamma: Reward discount rate
:return: returns nothing
"""
self.episodes = episodes
self.epsilon = epsilon
self.epsilon_min = epsilon_min
self.lr = lr
self.gamma = gamma
# Load CartPole V1 environment
self.env = gym.make('CartPole-v1')
self.env._max_episode_steps = MAX_STEPS
# initialize network variables
self.input = self.env.observation_space.shape[0] + 5
self.w1 = np.random.uniform(0,1,(self.input, 1))
def take_action(self, propagations):
# # Follow an epsilon-greedy policy to choose the next action
if np.random.random() < self.epsilon:
return self.env.action_space.sample()
else:
return np.argmax(propagations)
def update_epsilon(self, t):
self.epsilon = max(self.epsilon_min, self.epsilon * 0.99)
def is_solved(self, episode, rewards):
# Check if game is solved
if len(rewards) > 50:
if all(r >= 400 for r in rewards[-50:]):
print("Solved at episode {}.".format(episode - 50))
return True
else:
return False
def plot_rewards_history(self, rewards):
# Plot rewards over episodes
episodes = list(range(1, len(rewards)+1))
plt.plot(episodes, rewards)
plt.xlabel("Episodes")
plt.ylabel("Rewards")
plt.show()
return
def compute_state_action_vector(self, observation, action):
f0_vector = np.append([1], observation)
f0_vector = np.append(f0_vector, [0, 0, 0, 0])
f1_vector = np.append([1], [0, 0, 0, 0])
f1_vector = np.append(f1_vector, observation)
# Return the corresponding vector based on action: left or right
if action == "predict":
return f0_vector, f1_vector
if action == 0:
return f0_vector
if action == 1:
return f1_vector
def forward_pass(self, f_vector):
# Outputs the Q value of the given vector
return np.dot(self.w1.T, f_vector)
def train_network(self, current_state, next_state, action, reward, terminate=False):
# Train the feedforward neural network
forward_vector = self.compute_state_action_vector(current_state, action)
q_value = self.forward_pass(forward_vector)
f0_vector, f1_vector = self.compute_state_action_vector(next_state, action="predict")
props = [self.forward_pass(f0_vector), self.forward_pass(f1_vector)]
q_max = max(props)
# Return reward if the episode is terminated
if terminate:
target = reward
else:
target = reward + self.gamma * q_max
error = q_value - target
error = np.clip(error, -30, 30)
self.w1 += self.lr * error * forward_vector.reshape((-1, 1))
self.w1 = np.clip(self.w1, -150, 150)
return error, props
def train_agent(self):
scores = []
error = []
for episode in range(self.episodes):
current_state = self.env.reset()
done = False
step = 0
f0_vector, f1_vector = self.compute_state_action_vector(current_state, action="predict")
predicted_q_values = [self.forward_pass(f0_vector), self.forward_pass(f1_vector)]
action = self.take_action(predicted_q_values)
while step < MAX_STEPS:
new_state, reward, done, info = self.env.step(action)
if done:
break
error_t, predicted_q_values = self.train_network(current_state, new_state, action, reward)
action = self.take_action(predicted_q_values)
current_state = new_state
step += 1
scores.append(step)
error.append(error_t)
if self.is_solved(episode, scores):
return scores, error_t
self.update_epsilon(episode)
return scores, error
solver = DeepQAgent()
rewards, error = solver.train_agent()
solver.plot_rewards_history(rewards)