-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththank you kind stranger.py
172 lines (131 loc) · 4.78 KB
/
thank you kind stranger.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
""" Monte-Carlo Policy Gradient """
from __future__ import print_function
import gym
import pybulletgym
import numpy as np
import random
import time
import math
import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
from torch.autograd import Variable
SEED = 464684
MAX_EPISODES = 15000
MAX_TIMESTEPS = 200
ALPHA = 3e-5
GAMMA = 0.99
ACTION_STEP = 0.1
# ENV_NAME = "InvertedDoublePendulumPyBulletEnv-v0"
# ENV_NAME = "CartPole-v0"
ENV_NAME = "InvertedPendulumPyBulletEnv-v0"
class reinforce(nn.Module):
def __init__(self):
super(reinforce, self).__init__()
self.list_action = np.arange(-1, 1 + ACTION_STEP, ACTION_STEP)
# self.list_action = [0, 1]
# self.list_action = np.array([-1, -0.5, 0, 0.5, 1])
# self.list_action = np.array([-1, 0, 1])
# self.list_action = np.array([-1, 1])
# policy network
self.fc1 = nn.Linear(5, 128)
self.relu = nn.ReLU(inplace=True)
self.tanh = nn.Tanh()
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, len(self.list_action))
self.softmax = nn.Softmax()
def forward(self, x):
x = self.fc1(x)
x = self.tanh(x)
x = self.fc2(x)
x = self.tanh(x)
x = self.fc3(x)
x = self.softmax(x)
return x
def get_action(self, state, epsilon=0.5, training=True):
r = random.random()
if r > epsilon or training == False:
state = Variable(torch.Tensor(state))
state = torch.unsqueeze(state, 0)
probs = self.forward(state)
probs = torch.squeeze(probs, 0)
action = probs.multinomial(1)
if training == False:
action = torch.argmax(probs)
# action = action.data
# action = action[0]
action = action.data.item()
return self.list_action[action]
else:
r = random.randint(0, len(self.list_action) - 1)
return self.list_action[r]
def pi(self, s, a):
s = Variable(torch.Tensor([s]))
probs = self.forward(s)
probs = torch.squeeze(probs, 0)
p = probs[np.where(self.list_action == a)]
return p
def update_weight(self, states, actions, rewards, optimizer):
G = Variable(torch.Tensor([0]))
# for each step of the episode t = T - 1, ..., 0
# r_tt represents r_{t+1}
for s_t, a_t, r_tt in zip(states[::-1], actions[::-1], rewards[::-1]):
G = Variable(torch.Tensor([r_tt])) + GAMMA * G
loss = (-1.0) * G * torch.log(self.pi(s_t, a_t))
# update policy parameter \theta
optimizer.zero_grad()
loss.backward()
optimizer.step()
def main():
env = gym.make(ENV_NAME)
agent = reinforce()
# agent.cuda()
# torch.set_default_tensor_type('torch.cuda.FloatTensor')
optimizer = optim.Adam(agent.parameters(), lr=ALPHA)
epsilon = 1
for i_episode in range(MAX_EPISODES):
state = env.reset()
states = []
actions = []
rewards = [0] # no reward at t = 0
for timesteps in range(MAX_TIMESTEPS):
if(i_episode != 0):
epsilon = 1/i_episode**-4
action = agent.get_action(state, epsilon=0.3)
states.append(state)
actions.append(action)
state, reward, done, _ = env.step([action])
# state, reward, done, _ = env.step(action)
rewards.append(reward)
if done:
print("Episode {} finished after {} timesteps".format(i_episode, timesteps+1))
break
agent.update_weight(states, actions, rewards, optimizer)
env.close()
# env = gym.make(ENV_NAME)
# state = env.reset()
# env.render(mode="human")
env = gym.make(ENV_NAME)
env.render(mode="human")
duration = []
for _ in range(50):
# print("###############################################")
# print("Reset Env")
# print("###############################################")
state = env.reset()
for i in range(MAX_TIMESTEPS+1):
action = agent.get_action(state, training=False)
state, reward, done, _ = env.step([action])
# print(action, state, i)
# time.sleep(0.05)
if done or i == MAX_TIMESTEPS:
print("Lasted {} timesteps".format(i))
duration.append(i)
# print("\n\n\n")
break
env.close()
print("Mean : {}".format(np.mean(duration)))
if __name__ == "__main__":
random.seed(SEED)
main()