This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy path01-cartpole-dqn.py
109 lines (85 loc) · 3.02 KB
/
01-cartpole-dqn.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
# coding: utf-8
import gym
from gym import wrappers
import random
import math
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
from collections import deque
import numpy as np
env = gym.make('CartPole-v1')
# hyper parameters
EPISODES = 50 # number of episodes
EPS_START = 0.9 # e-greedy threshold start value
EPS_END = 0.05 # e-greedy threshold end value
EPS_DECAY = 200 # e-greedy threshold decay
GAMMA = 0.8 # Q-learning discount factor
LR = 0.001 # NN optimizer learning rate
HIDDEN_LAYER = 256 # NN hidden layer size
BATCH_SIZE = 64 # Q-learning batch size
class DQNAgent:
def __init__(self):
self.model = nn.Sequential(
nn.Linear(4, HIDDEN_LAYER),
nn.ReLU(),
nn.Linear(HIDDEN_LAYER, 2)
)
self.memory = deque(maxlen=10000)
self.optimizer = optim.Adam(self.model.parameters(), LR)
self.steps_done = 0
def act(self, state):
eps_threshold = EPS_END + (EPS_START - EPS_END) * math.exp(-1. * self.steps_done / EPS_DECAY)
self.steps_done += 1
if random.random() > eps_threshold:
return self.model(state).data.max(1)[1].view(1, 1)
else:
return torch.LongTensor([[random.randrange(2)]])
def memorize(self, state, action, reward, next_state):
self.memory.append((state,
action,
torch.FloatTensor([reward]),
torch.FloatTensor([next_state])))
def learn(self):
"""Experience Replay"""
if len(self.memory) < BATCH_SIZE:
return
batch = random.sample(self.memory, BATCH_SIZE)
states, actions, rewards, next_states = zip(*batch)
states = torch.cat(states)
actions = torch.cat(actions)
rewards = torch.cat(rewards)
next_states = torch.cat(next_states)
current_q = self.model(states).gather(1, actions)
max_next_q = self.model(next_states).detach().max(1)[0]
expected_q = rewards + (GAMMA * max_next_q)
loss = F.mse_loss(current_q.squeeze(), expected_q)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
agent = DQNAgent()
env = gym.make('CartPole-v0')
episode_durations = []
for e in range(1, EPISODES+1):
state = env.reset()
steps = 0
while True:
env.render()
state = torch.FloatTensor([state])
action = agent.act(state)
next_state, reward, done, _ = env.step(action.item())
# negative reward when attempt ends
if done:
reward = -1
agent.memorize(state, action, reward, next_state)
agent.learn()
state = next_state
steps += 1
if done:
print("{2} Episode {0} finished after {1} steps"
.format(e, steps, '\033[92m' if steps >= 195 else '\033[99m'))
episode_durations.append(steps)
break