-
Notifications
You must be signed in to change notification settings - Fork 3
/
QAgent.py
188 lines (147 loc) · 5.51 KB
/
QAgent.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import torch
import random
import numpy as np
from collections import deque
from game2 import CarGameAI, Direction, Point
from model import Linear_QNet, QTrainer
from helper import plot
MAX_MEMORY = 100_000
BATCH_SIZE = 1000
LR = 0.001
class Agent:
def __init__(self):
self.n_games = 0
def get_state(self, game):
head = game.car[0]
# head = game.head
point_l = Point(head.x - 20, head.y)
point_r = Point(head.x + 20, head.y)
point_u = Point(head.x, head.y - 20)
point_d = Point(head.x, head.y + 20)
dir_l = game.direction == Direction.LEFT
dir_r = game.direction == Direction.RIGHT
dir_u = game.direction == Direction.UP
dir_d = game.direction == Direction.DOWN
state = [
# Danger straight
(dir_r and game.is_collision(point_r)) or
(dir_l and game.is_collision(point_l)) or
(dir_u and game.is_collision(point_u)) or
(dir_d and game.is_collision(point_d)),
# Danger right
(dir_u and game.is_collision(point_r)) or
(dir_d and game.is_collision(point_l)) or
(dir_l and game.is_collision(point_u)) or
(dir_r and game.is_collision(point_d)),
# Danger left
(dir_d and game.is_collision(point_r)) or
(dir_u and game.is_collision(point_l)) or
(dir_r and game.is_collision(point_u)) or
(dir_l and game.is_collision(point_d)),
# food straight
(dir_r and game.food.x > game.head.x) or
(dir_l and game.food.x < game.head.x) or
(dir_u and game.food.y < game.head.y) or
(dir_d and game.food.y > game.head.y),
# food right
(dir_u and game.food.x > game.head.x) or
(dir_d and game.food.x < game.head.x) or
(dir_l and game.food.y < game.head.y) or
(dir_r and game.food.y > game.head.y),
# food left
(dir_d and game.food.x > game.head.x) or
(dir_u and game.food.x < game.head.x) or
(dir_r and game.food.y < game.head.y) or
(dir_l and game.food.y > game.head.y),
# food back
(dir_d and game.food.y < game.head.y) or
(dir_u and game.food.y > game.head.y) or
(dir_r and game.food.x < game.head.x) or
(dir_l and game.food.x > game.head.x)
]
stateInt = np.array(state, dtype=int)
stateToReturn = (stateInt[0],stateInt[1],stateInt[2],stateInt[3],stateInt[4]
,stateInt[5],stateInt[6])
#print(stateToReturn)
return stateToReturn
def max_action(Q, state, actions=[0,1,2]):
values = np.array([Q[state,a] for a in actions])
#print(values)
action = np.argmax(values)
return action
if __name__ == '__main__':
plot_scores = []
plot_mean_scores = []
total_score = 0
record = 0
n_games = 50000
alpha = 0.1
gamma = 0.99
eps = 1.0
action_space = [0,1,2]# [[0,1,0],[1,0,0],[0,0,1]]
# dangerStraight
# dangerRight
# dangerLeft
# foodLocationL
# foodLocationR
# foodLocationB
# foodLocationF
states = []
for dangerStraight in range(2):
for dangerRight in range(2):
for dangerLeft in range(2):
for foodLocationL in range(2):
for foodLocationR in range(2):
for foodLocationB in range(2):
for foodLocationF in range(2):
states.append((dangerStraight , dangerRight , dangerLeft , foodLocationL ,
foodLocationR , foodLocationB , foodLocationF))
Q = {}
for state in states:
for action in action_space:
Q[state, action] = 0
score = 0
#print(states)
total_rewards = np.zeros(n_games)
for i in range(n_games):
done = False
game = CarGameAI()
agent = Agent()
state = agent.get_state(game)
score = 0
#if i % 50 == 0 and i > 0:
#print('episode ', i, 'score ', score, 'epsilon %.3f' % eps)
values = np.array([Q[state, a] for a in [0,1,2]])
while not done:
action = np.random.choice([0,1,2]) if np.random.random() < eps \
else max_action(Q, state)
reward, done, score = game.play_step_helper(action)
obs_ = agent.get_state(game)
score = game.score
state_ = agent.get_state(game)
score += reward
action_ = max_action(Q, state_)
Q[state, action] = Q[state, action] + alpha*(reward + gamma*Q[state_, action_] - Q[state, action])
state = state_
total_rewards[i] = score
eps = eps - 2/n_games if eps > 0.01 else 0.01
mean_rewards = np.zeros(n_games)
for t in range(n_games):
mean_rewards[t] = np.mean(total_rewards[max(0, t-50):(t+1)])
#plt.plot(mean_rewards)
#plt.savefig('mountaincar.png')
#f = open("mountaincar.pkl","wb")
#pickle.dump(Q,f)
#f.close()
if done:
# train long memory, plot result
game.reset()
if score > record:
record = score
agent.model.save()
print('Game', agent.n_games, 'Score', score, 'Record:', record)
plot_scores.append(score)
total_score += score
mean_score = total_score / agent.n_games
plot_mean_scores.append(mean_score)
plot(plot_scores, plot_mean_scores)