forked from StanfordAI4HI/RepBM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqlearning_cartpole.py
More file actions
159 lines (134 loc) · 6.34 KB
/
Copy pathqlearning_cartpole.py
File metadata and controls
159 lines (134 loc) · 6.34 KB
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
import random
from collections import deque
import gym
import numpy as np
import torch
import torch.optim as optim
from src.memory import ReplayMemory, Transition
from src.config import cartpole_config
from src.models import QNet
from src.utils import save_qnet, select_maxq_action
import warnings
warnings.filterwarnings('ignore')
# if gpu is to be used
use_cuda = False #torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
ByteTensor = torch.cuda.ByteTensor if use_cuda else torch.ByteTensor
Tensor = FloatTensor
def epsilon_decay_per_ep(epsilon, config):
return max(config.dqn_epsilon_min, epsilon*config.dqn_epsilon_decay)
def preprocess_state(state, state_dim):
return Tensor(np.reshape(state, [1, state_dim]))
def select_action(state, qnet, epsilon, action_size):
sample = random.random()
if sample > epsilon:
return qnet(
state.type(FloatTensor)).data.max(1)[1].view(1,1)
else:
return LongTensor([[random.randrange(action_size)]])
def replay_and_optim(memory, qnet, optimizer, criterion, config):
# Adapted from pytorch tutorial example code
if config.dqn_batch_size > len(memory):
return
transitions = memory.sample(config.dqn_batch_size)
batch = Transition(*zip(*transitions))
# Compute a mask of non-final states and concatenate the batch elements
non_final_mask = ByteTensor(tuple(map(lambda d: d is False, batch.done)))
non_final_next_states = torch.cat([t.next_state for t in transitions if t.done is False])
state_batch = torch.cat(batch.state)
action_batch = torch.cat(batch.action)
reward_batch = torch.cat(batch.reward)
# Compute Q(s_t, a) - the model computes Q(s_t), then we select the
# columns of actions taken
state_action_values = qnet(state_batch.type(Tensor)).gather(1, action_batch).squeeze()
# Compute V(s_{t+1}) for all next states.
next_state_values = torch.zeros(config.dqn_batch_size).type(Tensor)
next_state_values[non_final_mask] = qnet(non_final_next_states.type(Tensor)).max(1)[0]
# Compute the expected Q values
expected_state_action_values = (next_state_values * config.gamma) + reward_batch
# Undo volatility (which was used to prevent unnecessary gradients)
expected_state_action_values = expected_state_action_values.detach()
# Compute Huber loss
loss = criterion(state_action_values, expected_state_action_values)
# Optimize the model
optimizer.zero_grad()
loss.backward()
#for param in qnet.parameters():
# param.grad.data.clamp_(-1, 1)
optimizer.step()
if __name__ == "__main__":
env = gym.make("CartPole-v0")
config = cartpole_config
noise_dim = config.noise_dim
state_dim = config.state_dim + noise_dim
qnet = QNet(state_dim,config.dqn_hidden_dims,config.action_size)
memory = ReplayMemory(config.buffer_capacity)
epsilon = config.dqn_epsilon
scores = deque(maxlen=100)
dp_scores = deque(maxlen=30)
optimizer = optim.Adam(qnet.parameters(), lr=config.dqn_alpha)
criterion = torch.nn.MSELoss()
for i_episode in range(config.dqn_num_episodes):
# Initialize the environment and state
state = np.append(env.reset(), np.random.normal(size = noise_dim))
state = preprocess_state(state,state_dim)
done = False
n_steps = 0
while not done:
# Select and perform an action
action = select_action(state,qnet,epsilon=epsilon,action_size=config.action_size)
next_state, reward, done, _ = env.step(action.item())
next_state = np.append(next_state, np.random.normal(size = noise_dim))
# print(next_state.shape, reward, done)
reward = Tensor([reward])
next_state = preprocess_state(next_state,state_dim)
#print(state.shape, next_state.shape, reward.shape, done)
# Store the transition in memory
memory.push(state, action, next_state, reward, done, None)
# Move to the next state
state = next_state
n_steps += 1
scores.append(n_steps)
state = np.append(env.reset(), np.random.normal(size = noise_dim))
state = preprocess_state(state, state_dim)
done = False
n_steps = 0
while not done:
# Select and perform an action
action = select_maxq_action(state, qnet)
next_state, reward, done, _ = env.step(action.item())
next_state = np.append(next_state, np.random.normal(size = noise_dim))
next_state = preprocess_state(next_state, state_dim)
state = next_state
n_steps += 1
dp_scores.append(n_steps)
mean_score = np.mean(dp_scores)
# Set the value we want to achieve
if mean_score >= 195 and i_episode >= 30:
print('Ran {} episodes. Solved after {} trials ✔'.format(i_episode, i_episode - 30))
break
if i_episode % 100 == 0:
print('[Episode {}] - Mean survival time over last 100 episodes was {} ticks. Epsilon is {}'
.format(i_episode, mean_score, epsilon))
# update
replay_and_optim(memory, qnet, optimizer, criterion, config)
epsilon = epsilon_decay_per_ep(epsilon,config)
save_qnet(state={'state_dict': qnet.state_dict()}, checkpoint='target_policies', filename='cp_s.pth.tar')
# save_qnet(state={'state_dict': qnet.state_dict()})
groundtruth = deque()
for i_episode in range(1000):
true_state = np.append(env.reset(), np.random.normal(size=noise_dim))
true_state = preprocess_state(true_state, state_dim)
true_done = False
true_steps = 0
while not true_done:
# action = select_action_random(action_size=config.action_size)
action = select_maxq_action(true_state, qnet)
true_next_state, true_reward, true_done, _ = env.step(action.item())
true_next_state = np.append(true_next_state, np.random.normal(size = noise_dim))
true_steps += 1
true_state = preprocess_state(true_next_state, state_dim)
groundtruth.append(true_steps)
print('True survival time is {} with std dev {:.3e}'.format(np.mean(groundtruth),
np.std(groundtruth) / len(groundtruth)))