-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
196 lines (136 loc) · 4.93 KB
/
test.py
File metadata and controls
196 lines (136 loc) · 4.93 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: udemy
"""
import torch
import torch.nn as nn
import torch.optim as optim
import random
import math
import time
import matplotlib.pyplot as plt
# if gpu is to be used
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
Tensor = torch.Tensor
seed_value = 23
env.seed(seed_value)
torch.manual_seed(seed_value)
random.seed(seed_value)
###### PARAMS ######
learning_rate = 0.01
num_episodes = 500
gamma = 0.99
hidden_layer = 64
egreedy = 0.9
egreedy_final = 0.02
egreedy_decay = 500
report_interval = 10
score_to_solve = 195
####################
number_of_inputs = env.observation_space.shape[0]
number_of_outputs = env.action_space.n
def calculate_epsilon(steps_done):
epsilon = egreedy_final + (egreedy - egreedy_final) * \
math.exp(-1. * steps_done / egreedy_decay)
return epsilon
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.linear1 = nn.Linear(number_of_inputs, hidden_layer)
self.linear2 = nn.Linear(hidden_layer, number_of_outputs)
self.activation = nn.Tanh()
# self.activation = nn.ReLU()
def forward(self, x):
output1 = self.linear1(x)
output1 = self.activation(output1)
output2 = self.linear2(output1)
return output2
class QNet_Agent(object):
def __init__(self):
self.nn = NeuralNetwork().to(device)
self.loss_func = nn.MSELoss()
# self.loss_func = nn.SmoothL1Loss()
self.optimizer = optim.Adam(params=self.nn.parameters(), lr=learning_rate)
# self.optimizer = optim.RMSprop(params=mynn.parameters(), lr=learning_rate)
def select_action(self, state, epsilon):
random_for_egreedy = torch.rand(1)[0]
if random_for_egreedy > epsilon:
with torch.no_grad():
state = Tensor(state).to(device)
action_from_nn = self.nn(state)
action = torch.max(action_from_nn, 0)[1]
action = action.item()
else:
action = env.action_space.sample()
return action
def optimize(self, state, action, new_state, reward, done):
state = Tensor(state).to(device)
new_state = Tensor(new_state).to(device)
reward = Tensor([reward]).to(device)
if done:
target_value = reward
else:
new_state_values = self.nn(new_state).detach()
max_new_state_values = torch.max(new_state_values)
target_value = reward + gamma * max_new_state_values
predicted_value = self.nn(state)[action]
loss = self.loss_func(predicted_value, target_value)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# Q[state, action] = reward + gamma * torch.max(Q[new_state])
qnet_agent = QNet_Agent()
steps_total = []
frames_total = 0
solved_after = 0
solved = False
start_time = time.time()
for i_episode in range(num_episodes):
state = env.reset()
step = 0
# for step in range(100):
while True:
step += 1
frames_total += 1
epsilon = calculate_epsilon(frames_total)
# action = env.action_space.sample()
action = qnet_agent.select_action(state, epsilon)
new_state, reward, done, info = env.step(action)
qnet_agent.optimize(state, action, new_state, reward, done)
state = new_state
if done:
steps_total.append(step)
mean_reward_100 = sum(steps_total[-100:]) / 100
if (mean_reward_100 > score_to_solve and solved == False):
print("SOLVED! After %i episodes " % i_episode)
solved_after = i_episode
solved = True
if (i_episode % report_interval == 0):
print("\n*** Episode %i *** \
\nAv.reward: [last %i]: %.2f, [last 100]: %.2f, [all]: %.2f \
\nepsilon: %.2f, frames_total: %i"
%
(i_episode,
report_interval,
sum(steps_total[-report_interval:]) / report_interval,
mean_reward_100,
sum(steps_total) / len(steps_total),
epsilon,
frames_total
)
)
elapsed_time = time.time() - start_time
print("Elapsed time: ", time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
break
print("\n\n\n\nAverage reward: %.2f" % (sum(steps_total) / num_episodes))
print("Average reward (last 100 episodes): %.2f" % (sum(steps_total[-100:]) / 100))
if solved:
print("Solved after %i episodes" % solved_after)
plt.figure(figsize=(12, 5))
plt.title("Rewards")
plt.bar(torch.arange(len(steps_total)), steps_total, alpha=0.6, color='green', width=5)
plt.show()
env.close()
env.env.close()