-
Notifications
You must be signed in to change notification settings - Fork 0
/
DQN_FrozenLake.py
123 lines (89 loc) · 3.26 KB
/
DQN_FrozenLake.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
import gym
import torch
from algorithms.DQN import DQN
from exploration.DiscreteExploration import DiscreteExploration
from algorithms.QLearning import QCritic
class Critic(QCritic):
def __init__(self, state_dim, action_dim):
super(Critic, self).__init__(state_dim, action_dim)
hidden = 64
limit = 0.001
self._hidden0 = torch.nn.Linear(state_dim, hidden)
self._hidden1 = torch.nn.Linear(hidden, int(hidden / 2))
self._output = torch.nn.Linear(int(hidden / 2), action_dim)
torch.nn.init.xavier_uniform_(self._hidden0.weight)
torch.nn.init.xavier_uniform_(self._hidden1.weight)
torch.nn.init.uniform_(self._output.weight, -limit, limit)
def forward(self, state):
x = torch.nn.functional.relu(self._hidden0(state))
x = torch.nn.functional.relu(self._hidden1(x))
value = torch.nn.functional.sigmoid(self._output(x))
return value
def test_policy(agent):
env = gym.make('FrozenLake-v0')
state0 = torch.zeros((env.observation_space.n), dtype=torch.float32)
policy = '\n'
for i in range(env.observation_space.n):
state0.fill_(0)
state0[i] = 1
action = agent.get_action(state0)
if action == 0:
policy += 'L'
if action == 1:
policy += 'D'
if action == 2:
policy += 'R'
if action == 3:
policy += 'U'
if (i + 1) % 4 == 0:
policy += '\n'
print(policy + '\n')
for i in range(env.observation_space.n):
state0.fill_(0)
state0[i] = 1
print(agent.activate(state0))
def test(agent):
env = gym.make('FrozenLake-v0')
state0 = torch.zeros((env.observation_space.n), dtype=torch.float32)
state0[env.reset()] = 1
done = False
while not done:
env.render()
action = agent.get_action(state0)
next_state, reward, done, info = env.step(action)
state0.fill_(0)
state0[next_state] = 1
env.render()
env.close()
def run():
epochs = 5000
env = gym.make('FrozenLake-v0')
agent = DQN(Critic, env.observation_space.n, env.action_space.n, 10000, 64, 1e-3, 0.99, 1e-4)
#agent = QLearning(Critic, env.observation_space.n, env.action_space.n, 1e-3, 0.99)
exploration = DiscreteExploration(0.9, 0.1, epochs / 2)
win = 0
lose = 0
for e in range(epochs):
state0 = torch.zeros((env.observation_space.n), dtype=torch.float32)
state0[env.reset()] = 1
done = False
while not done:
# env.render()
action = exploration.explore(agent.get_action(state0), env)
next_state, reward, done, info = env.step(action)
if done:
if reward == 1:
win += 1
else:
lose += 1
state1 = torch.zeros((env.observation_space.n), dtype=torch.float32)
state1[next_state] = 1
reward = torch.tensor([reward], dtype=torch.float32)
done = torch.tensor([done], dtype=torch.float32)
agent.train(state0, action, state1, reward, done)
state0 = state1
exploration.update(e)
print(str(win) + ' / ' + str(lose))
env.close()
test(agent)
test_policy(agent)