-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
182 lines (145 loc) · 7.05 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
import torch
import gym
import torch.optim as optim
import torch.nn as nn
import matplotlib.pyplot as plt
import pandas as pd
from config import AgentConfig
from network import MlpPolicy
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Agent(AgentConfig):
def __init__(self):
self.env = gym.make('CartPole-v0')
self.action_size = self.env.action_space.n # 2 for cartpole
if self.train_cartpole:
self.policy_network = MlpPolicy(action_size=self.action_size).to(device)
self.optimizer = optim.Adam(self.policy_network.parameters(), lr=self.learning_rate)
self.scheduler = optim.lr_scheduler.StepLR(self.optimizer, step_size=self.k_epoch,
gamma=0.999)
self.loss = 0
self.criterion = nn.MSELoss()
self.memory = {
'state': [], 'action': [], 'reward': [], 'next_state': [], 'action_prob': [], 'terminal': [], 'count': 0,
'advantage': [], 'td_target': torch.FloatTensor([])
}
def new_random_game(self):
self.env.reset()
action = self.env.action_space.sample()
screen, reward, terminal, info = self.env.step(action)
return screen, reward, action, terminal
def train(self):
episode = 0
step = 0
reward_history = []
avg_reward = []
solved = False
# A new episode
while not solved:
start_step = step
episode += 1
episode_length = 0
# Get initial state
state, reward, action, terminal = self.new_random_game()
current_state = state
total_episode_reward = 1
# A step in an episode
while not solved:
step += 1
episode_length += 1
# Choose action
prob_a = self.policy_network.pi(torch.FloatTensor(current_state).to(device))
# print(prob_a)
action = torch.distributions.Categorical(prob_a).sample().item()
# Act
state, reward, terminal, _ = self.env.step(action)
new_state = state
reward = -1 if terminal else reward
self.add_memory(current_state, action, reward/10.0, new_state, terminal, prob_a[action].item())
current_state = new_state
total_episode_reward += reward
if terminal:
episode_length = step - start_step
reward_history.append(total_episode_reward)
avg_reward.append(sum(reward_history[-10:])/10.0)
self.finish_path(episode_length)
if len(reward_history) > 100 and sum(reward_history[-100:-1]) / 100 >= 195:
solved = True
print('episode: %.2f, total step: %.2f, last_episode length: %.2f, last_episode_reward: %.2f, '
'loss: %.4f, lr: %.4f' % (episode, step, episode_length, total_episode_reward, self.loss,
self.scheduler.get_lr()[0]))
self.env.reset()
break
if episode % self.update_freq == 0:
for _ in range(self.k_epoch):
self.update_network()
if episode % self.plot_every == 0:
plot_graph(reward_history, avg_reward)
self.env.close()
def update_network(self):
# get ratio
pi = self.policy_network.pi(torch.FloatTensor(self.memory['state']).to(device))
new_probs_a = torch.gather(pi, 1, torch.tensor(self.memory['action']))
old_probs_a = torch.FloatTensor(self.memory['action_prob'])
ratio = torch.exp(torch.log(new_probs_a) - torch.log(old_probs_a))
# surrogate loss
surr1 = ratio * torch.FloatTensor(self.memory['advantage'])
surr2 = torch.clamp(ratio, 1 - self.eps_clip, 1 + self.eps_clip) * torch.FloatTensor(self.memory['advantage'])
pred_v = self.policy_network.v(torch.FloatTensor(self.memory['state']).to(device))
v_loss = 0.5 * (pred_v - self.memory['td_target']).pow(2) # Huber loss
entropy = torch.distributions.Categorical(pi).entropy()
entropy = torch.tensor([[e] for e in entropy])
self.loss = (-torch.min(surr1, surr2) + self.v_coef * v_loss - self.entropy_coef * entropy).mean()
self.optimizer.zero_grad()
self.loss.backward()
self.optimizer.step()
self.scheduler.step()
def add_memory(self, s, a, r, next_s, t, prob):
if self.memory['count'] < self.memory_size:
self.memory['count'] += 1
else:
self.memory['state'] = self.memory['state'][1:]
self.memory['action'] = self.memory['action'][1:]
self.memory['reward'] = self.memory['reward'][1:]
self.memory['next_state'] = self.memory['next_state'][1:]
self.memory['terminal'] = self.memory['terminal'][1:]
self.memory['action_prob'] = self.memory['action_prob'][1:]
self.memory['advantage'] = self.memory['advantage'][1:]
self.memory['td_target'] = self.memory['td_target'][1:]
self.memory['state'].append(s)
self.memory['action'].append([a])
self.memory['reward'].append([r])
self.memory['next_state'].append(next_s)
self.memory['terminal'].append([1 - t])
self.memory['action_prob'].append(prob)
def finish_path(self, length):
state = self.memory['state'][-length:]
reward = self.memory['reward'][-length:]
next_state = self.memory['next_state'][-length:]
terminal = self.memory['terminal'][-length:]
td_target = torch.FloatTensor(reward) + \
self.gamma * self.policy_network.v(torch.FloatTensor(next_state)) * torch.FloatTensor(terminal)
delta = td_target - self.policy_network.v(torch.FloatTensor(state))
delta = delta.detach().numpy()
# get advantage
advantages = []
adv = 0.0
for d in delta[::-1]:
adv = self.gamma * self.lmbda * adv + d[0]
advantages.append([adv])
advantages.reverse()
if self.memory['td_target'].shape == torch.Size([1, 0]):
self.memory['td_target'] = td_target.data
else:
self.memory['td_target'] = torch.cat((self.memory['td_target'], td_target.data), dim=0)
self.memory['advantage'] += advantages
def plot_graph(reward_history, avg_reward):
df = pd.DataFrame({'x': range(len(reward_history)), 'Reward': reward_history, 'Average': avg_reward})
plt.style.use('seaborn-darkgrid')
palette = plt.get_cmap('Set1')
plt.plot(df['x'], df['Reward'], marker='', color=palette(1), linewidth=0.8, alpha=0.9, label='Reward')
# plt.plot(df['x'], df['Average'], marker='', color='tomato', linewidth=1, alpha=0.9, label='Average')
# plt.legend(loc='upper left')
plt.title("CartPole", fontsize=14)
plt.xlabel("episode", fontsize=12)
plt.ylabel("score", fontsize=12)
plt.savefig('score.png')