-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
205 lines (171 loc) · 8.2 KB
/
agent.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
import gymnasium as gym
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import random
import torch
from torch import nn
import torch.nn.functional as F
import yaml
from collections import deque
from datetime import datetime, timedelta
import argparse
import itertools
import os
import flappy_bird_gymnasium
# For printing date and time
DATE_FORMAT = "%m-%d %H:%M:%S"
# Directory for saving run info
RUNS_DIR = "runs"
os.makedirs(RUNS_DIR, exist_ok=True)
# 'Agg': used to generate plots as images and save them to a file instead of rendering to screen
matplotlib.use('Agg')
device = 'cuda' if torch.cuda.is_available() else 'cpu'
device = 'cpu' # force cpu, sometimes GPU not always faster than CPU due to overhead of moving data to GPU
class ReplayMemory():
def __init__(self, maxlen, seed=None):
self.memory = deque([], maxlen=maxlen)
if seed is not None:
random.seed(seed)
def append(self, transition):
self.memory.append(transition)
def sample(self, sample_size):
return random.sample(self.memory, sample_size)
def __len__(self):
return len(self.memory)
class DQN(nn.Module):
def __init__(self, state_dim, action_dim, hidden_dim=256):
super(DQN, self).__init__()
self.fc1 = nn.Linear(state_dim, hidden_dim)
self.output = nn.Linear(hidden_dim, action_dim)
def forward(self, x):
x = F.relu(self.fc1(x))
return self.output(x)
# Deep Q-Learning Agent
class Agent():
def __init__(self, hyperparameter_set):
with open('hyperparameters.yml', 'r') as file:
all_hyperparameter_sets = yaml.safe_load(file)
hyperparameters = all_hyperparameter_sets[hyperparameter_set]
self.hyperparameter_set = hyperparameter_set
self.env_id = hyperparameters['env_id']
self.learning_rate_a = hyperparameters['learning_rate_a']
self.discount_factor_g = hyperparameters['discount_factor_g']
self.network_sync_rate = hyperparameters['network_sync_rate']
self.replay_memory_size = hyperparameters['replay_memory_size']
self.mini_batch_size = hyperparameters['mini_batch_size']
self.epsilon_init = hyperparameters['epsilon_init']
self.epsilon_decay = hyperparameters['epsilon_decay']
self.epsilon_min = hyperparameters['epsilon_min']
self.stop_on_reward = hyperparameters['stop_on_reward']
self.fc1_nodes = hyperparameters['fc1_nodes']
self.env_make_params = hyperparameters.get('env_make_params', {})
self.loss_fn = nn.MSELoss()
self.optimizer = None
self.LOG_FILE = os.path.join(RUNS_DIR, f'{self.hyperparameter_set}.log')
self.MODEL_FILE = os.path.join(RUNS_DIR, f'{self.hyperparameter_set}.pt')
self.GRAPH_FILE = os.path.join(RUNS_DIR, f'{self.hyperparameter_set}.png')
def run(self, is_training=True, render=False):
if is_training:
start_time = datetime.now()
last_graph_update_time = start_time
log_message = f"{start_time.strftime(DATE_FORMAT)}: Training starting..."
print(log_message)
with open(self.LOG_FILE, 'w') as file:
file.write(log_message + '\n')
env = gym.make(self.env_id, render_mode='human' if render else None, **self.env_make_params)
num_actions = env.action_space.n
num_states = env.observation_space.shape[0]
rewards_per_episode = []
policy_dqn = DQN(num_states, num_actions, self.fc1_nodes).to(device)
if is_training:
epsilon = self.epsilon_init
memory = ReplayMemory(self.replay_memory_size)
target_dqn = DQN(num_states, num_actions, self.fc1_nodes).to(device)
target_dqn.load_state_dict(policy_dqn.state_dict())
self.optimizer = torch.optim.Adam(policy_dqn.parameters(), lr=self.learning_rate_a)
epsilon_history = []
step_count = 0
best_reward = -9999999
else:
policy_dqn.load_state_dict(torch.load(self.MODEL_FILE))
policy_dqn.eval()
for episode in itertools.count():
state, _ = env.reset()
state = torch.tensor(state, dtype=torch.float, device=device)
terminated = False
episode_reward = 0.0
while not terminated and episode_reward < self.stop_on_reward:
if is_training and random.random() < epsilon:
action = env.action_space.sample()
action = torch.tensor(action, dtype=torch.int64, device=device)
else:
with torch.no_grad():
action = policy_dqn(state.unsqueeze(dim=0)).squeeze().argmax()
new_state, reward, terminated, truncated, info = env.step(action.item())
episode_reward += reward
new_state = torch.tensor(new_state, dtype=torch.float, device=device)
reward = torch.tensor(reward, dtype=torch.float, device=device)
if is_training:
memory.append((state, action, new_state, reward, terminated))
step_count += 1
state = new_state
rewards_per_episode.append(episode_reward)
if is_training:
if episode_reward > best_reward:
log_message = f"{datetime.now().strftime(DATE_FORMAT)}: New best reward {episode_reward:0.1f} ({(episode_reward-best_reward)/best_reward*100:+.1f}%) at episode {episode}, saving model..."
print(log_message)
with open(self.LOG_FILE, 'a') as file:
file.write(log_message + '\n')
torch.save(policy_dqn.state_dict(), self.MODEL_FILE)
best_reward = episode_reward
current_time = datetime.now()
if current_time - last_graph_update_time > timedelta(seconds=10):
self.save_graph(rewards_per_episode, epsilon_history)
last_graph_update_time = current_time
if len(memory) > self.mini_batch_size:
mini_batch = memory.sample(self.mini_batch_size)
self.optimize(mini_batch, policy_dqn, target_dqn)
epsilon = max(epsilon * self.epsilon_decay, self.epsilon_min)
epsilon_history.append(epsilon)
if step_count > self.network_sync_rate:
target_dqn.load_state_dict(policy_dqn.state_dict())
step_count = 0
def save_graph(self, rewards_per_episode, epsilon_history):
fig = plt.figure(1)
mean_rewards = np.zeros(len(rewards_per_episode))
for x in range(len(mean_rewards)):
mean_rewards[x] = np.mean(rewards_per_episode[max(0, x-99):(x+1)])
plt.subplot(121)
plt.ylabel('Mean Rewards')
plt.plot(mean_rewards)
plt.subplot(122)
plt.ylabel('Epsilon Decay')
plt.plot(epsilon_history)
plt.subplots_adjust(wspace=1.0, hspace=1.0)
fig.savefig(self.GRAPH_FILE)
plt.close(fig)
def optimize(self, mini_batch, policy_dqn, target_dqn):
states, actions, new_states, rewards, terminations = zip(*mini_batch)
states = torch.stack(states)
actions = torch.stack(actions)
new_states = torch.stack(new_states)
rewards = torch.stack(rewards)
terminations = torch.tensor(terminations).float().to(device)
with torch.no_grad():
target_q = rewards + (1-terminations) * self.discount_factor_g * target_dqn(new_states).max(dim=1)[0]
current_q = policy_dqn(states).gather(dim=1, index=actions.unsqueeze(dim=1)).squeeze()
loss = self.loss_fn(current_q, target_q)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Train or test model.')
parser.add_argument('hyperparameters', help='')
parser.add_argument('--train', help='Training mode', action='store_true')
args = parser.parse_args()
dql = Agent(hyperparameter_set=args.hyperparameters)
if args.train:
dql.run(is_training=True)
else:
dql.run(is_training=False, render=True)