-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patha2c_cartp.py
164 lines (123 loc) · 4.25 KB
/
a2c_cartp.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
import gym
import time
import numpy as np
import argparse
from collections import deque
from itertools import count
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.nn.init as init
import matplotlib.pyplot as plt
from drawnow import drawnow
last_score_plot = []
avg_score_plot = []
def draw_fig():
plt.ylabel('reward')
plt.xlabel('episode')
plt.plot(last_score_plot, '-')
plt.plot(avg_score_plot, 'r-')
parser = argparse.ArgumentParser(description='PyTorch A2C solution of CartPole-v0')
parser.add_argument('--gamma', type=float, default=0.99)
parser.add_argument('--actor_lr', type=float, default=1e-4)
parser.add_argument('--critic_lr', type=float, default=5e-4)
parser.add_argument('--batch_size', type=int, default=512)
parser.add_argument('--max_episode', type=int, default=500)
cfg = parser.parse_args()
env = gym.make('CartPole-v0')
class Memory(object):
def __init__(self, memory_size=10000):
self.memory = deque(maxlen=memory_size)
self.memory_size = memory_size
def __len__(self):
return len(self.memory)
def append(self, item):
self.memory.append(item)
def sample_batch(self, batch_size):
idx = np.random.permutation(len(self.memory))[:batch_size]
return [self.memory[i] for i in idx]
class Actor(nn.Module):
def __init__(self):
super(Actor, self).__init__()
self.fc1 = nn.Linear(4, 64)
self.fc2 = nn.Linear(64, 2)
init.xavier_normal_(self.fc1.weight)
init.xavier_normal_(self.fc2.weight)
def forward(self, x):
x = F.elu(self.fc1(x))
x = F.softmax(self.fc2(x), dim=1)
return x
class Critic(nn.Module):
def __init__(self):
super(Critic, self).__init__()
self.fc1 = nn.Linear(4, 64)
self.fc2 = nn.Linear(64, 1)
init.xavier_normal_(self.fc1.weight)
def forward(self, x):
x = F.elu(self.fc1(x))
value = self.fc2(x)
return value.squeeze()
def get_action(state):
action_probs = actor(state)
action_dist = torch.distributions.Categorical(action_probs)
action = action_dist.sample()
return action
def get_state_value(state):
state_value = critic(state)
return state_value
def update_actor(states, actions, advantages):
action_probs = actor(states)
action_dist = torch.distributions.Categorical(action_probs)
act_loss = -action_dist.log_prob(actions) * advantages
entropy = action_dist.entropy()
loss = torch.mean(act_loss - 1e-4 * entropy)
actor_optimizer.zero_grad()
loss.backward()
actor_optimizer.step()
return
def update_critic(states, targets):
state_value = critic(states)
loss = F.mse_loss(state_value, targets)
critic_optimizer.zero_grad()
loss.backward()
critic_optimizer.step()
return
actor = Actor()
critic = Critic()
actor_optimizer = optim.Adam(actor.parameters(), lr=cfg.actor_lr)
critic_optimizer = optim.Adam(critic.parameters(), lr=cfg.critic_lr)
memory = Memory(10000)
def main():
for episode in range(cfg.max_episode):
state = env.reset()
episode_score = 0
start_time = time.perf_counter()
for episode_steps in count():
action = get_action(torch.tensor(state).float()[None, :]).item()
next_state, reward, done, _ = env.step(action)
memory.append([state, action, next_state, reward, done])
state = next_state
episode_score += reward
if len(memory) > cfg.batch_size:
states, actions, next_states, rewards, dones = \
map(lambda x: torch.tensor(x).float(), zip(*memory.sample_batch(cfg.batch_size)))
# calculate estimated return
targets = rewards + cfg.gamma * get_state_value(next_states).detach() * (1 - dones)
td_errors = targets - get_state_value(states).detach()
update_actor(states=states, actions=actions, advantages=td_errors)
update_critic(states, targets)
if done:
print('episode: %d score %.5f, steps %d, (%.2f sec/eps)' %
(episode, episode_score, episode_steps, time.perf_counter() - start_time))
last_score_plot.append(episode_score)
if len(avg_score_plot) == 0:
avg_score_plot.append(episode_score)
else:
avg_score_plot.append(avg_score_plot[-1] * 0.99 + episode_score * 0.01)
drawnow(draw_fig)
break
env.close()
if __name__ == '__main__':
main()
plt.pause(0)