-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathagent.py
140 lines (122 loc) · 4.09 KB
/
agent.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
#!/usr/bin/env python
"""Environment for Microsoft AirSim Unity Quadrotor
- Author: Subin Yang
- Contact: [email protected]
"""
import math
import random
from collections import deque
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import gym
from gym import wrappers
from env import DroneEnv
env = DroneEnv()
class DQN(nn.Module):
def __init__(self, in_channels=84, num_actions=7):
super(DQN, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 84, kernel_size=8, stride=4)
self.conv2 = nn.Conv2d(84, 42, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(42, 21, kernel_size=3, stride=1)
self.fc4 = nn.Linear(336, 168)
self.fc5 = nn.Linear(168, num_actions)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = F.relu(self.fc4(x.view(x.size(0), -1)))
return self.fc5(x)
class Agent:
def __init__(
self,
eps_start=0.9,
eps_end=0.05,
eps_decay=200,
gamma=0.8,
learning_rate=0.001,
batch_size=1,
):
self.eps_start = eps_start
self.eps_end = eps_end
self.eps_decay = eps_decay
self.gamma = gamma
self.learning_rate = learning_rate
self.batch_size = batch_size
dqn = DQN()
self.model = dqn.forward()
self.memory = deque(maxlen=10000)
self.optimizer = optim.Adam(self.model.parameters(), self.learning_rate)
self.steps_done = 0
def act(self, state):
eps_threshold = self.eps_end + (self.eps_start - self.eps_end) * math.exp(
-1.0 * self.steps_done / self.eps_decay
)
self.steps_done += 1
if random.random() > eps_threshold:
action = self.model(state).data.max(1)[1]
action = [action.max(1)[1]]
return torch.LongTensor([action])
else:
action = [random.randrange(0, 7)]
return torch.LongTensor([action])
def memorize(self, state, action, reward, next_state):
self.memory.append(
(
state,
action,
torch.FloatTensor([reward]),
torch.FloatTensor([next_state]),
)
)
def learn(self):
if len(self.memory) < self.batch_size:
return
batch = random.sample(self.memory, self.batch_size)
states, actions, rewards, next_states = zip(*batch)
print(actions)
states = torch.cat(states)
actions = torch.cat(actions)
rewards = torch.cat(rewards)
next_states = torch.cat(next_states)
print(actions)
current_q = self.model(states)
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()
def train(self):
score_history = []
reward_history = []
score = 0
for e in range(1, EPISODES + 1):
state = env.reset()
steps = 0
while True:
state = torch.FloatTensor([state])
action = act(state)
print(action)
next_state, reward, done = env.step(action)
memorize(state, action, reward, next_state)
learn()
state = next_state
steps += 1
score += reward
if done:
print("episode:{0}, reward: {1}, score: {2}".format(e, reward, score))
print("----------------------------------------------------")
score_history.append(steps)
reward_history.append(reward)
f = open("reward.txt", "a")
f.write(str(reward))
f.close()
f2 = open("score.txt", "a")
f2.write(str(score))
f2.close()
break