Repository navigation
Expand file tree
/
Copy pathagent.py
More file actions
281 lines (226 loc) · 10.9 KB
/
Copy pathagent.py
File metadata and controls
281 lines (226 loc) · 10.9 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import torch
import os
import random
import numpy as np
from collections import deque
from gamePyglet import SnakeGame, Direction, Point, BLOCK_SIZE
from model import Linear_QNet, QTrainer
from helper import plot
MAX_MEM = 100_000
BATCH_SIZE = 1000
LEARN_RATE = 0.001
LR_DECAY = 0.0
RANDOMNESS = 100
RAND_DECAY = 0.5
class Agent:
def __init__(self, load_model:bool, model_filename) -> None:
"""Initializes the agent's default parameters
Args:
loaded_model (str, optional): File name for a model to load from the model/ dir. Defaults to None.
"""
self.num_games = 0
self.model_path = model_filename
self.epsilon = 0 # randomness
self.gamma = 0.9 # discount rate (<1)
self.memory = deque(maxlen=MAX_MEM) # popLeft() when MAX_MEM exceeded
self.randmove = 0 # num of rand moves (testing remove later)
# Check if model should be loaded
if load_model:
self.model_path = './model/' + self.model_path
self.model = torch.load(self.model_path)
self.loaded_model = True
else:
self.model = Linear_QNet(input_size=11, hidden_size=256, output_size=3, model_filename=self.model_path)
self.loaded_model = False
# Create trainer
self.trainer = QTrainer(self.model, LEARN_RATE, self.gamma)
def getState(self, game):
"""Gets the game's current state
Args:
game (Game): The instance of the snake game that will be analyzed
Returns:
[bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool]: state array conveying game state data.
[
Danger Straight, Danger Right, Danger Left,
Moving West, Moving East, Moving North, Moving South,
Fruit West, Fruit East, Fruit North, Fruit South
]
"""
snake_head = game.snakeBody[0]
head_w = Point(snake_head.x - BLOCK_SIZE, snake_head.y)
head_e = Point(snake_head.x + BLOCK_SIZE, snake_head.y)
head_n = Point(snake_head.x, snake_head.y + BLOCK_SIZE)
head_s = Point(snake_head.x, snake_head.y - BLOCK_SIZE)
direction_w = game.direction == Direction.WEST
direction_e = game.direction == Direction.EAST
direction_n = game.direction == Direction.NORTH
direction_s = game.direction == Direction.SOUTH
state = [
# Danger Ahead (Straight)
(direction_w and game.find_collision(head_w)) or
(direction_e and game.find_collision(head_e)) or
(direction_n and game.find_collision(head_n)) or
(direction_s and game.find_collision(head_s)),
# Danger Right
(direction_s and game.find_collision(head_w)) or
(direction_n and game.find_collision(head_e)) or
(direction_w and game.find_collision(head_n)) or
(direction_e and game.find_collision(head_s)),
# Danger Left
(direction_n and game.find_collision(head_w)) or
(direction_s and game.find_collision(head_e)) or
(direction_e and game.find_collision(head_n)) or
(direction_w and game.find_collision(head_s)),
# Direction of movement
direction_w,
direction_e,
direction_n,
direction_s,
# Food relative location
game.fruit.x < game.snakeHead.x, # food west
game.fruit.x > game.snakeHead.x, # food east
game.fruit.y > game.snakeHead.y, # food north
game.fruit.y < game.snakeHead.y # food south
]
# TODO: DELETE TEST PRINT BELOW
# os.system('clear')
# print(f"Danger S {state[0]} Danger Right {state[1]} Danger Left {state[2]}",
# f"Dir W {state[3]} Dir E {state[4]} Dir N {state[5]} Dir S {state[6]}",
# f"Fruit W {state[7]} Fruit E {state[8]} Fruit N {state[9]} Fruit S {state[10]}")
return np.array(state, dtype=int)
def remember(self, state, action, reward, next_state, game_over):
"""Stores the current state, action(move), reward, the next state, and game over into memory
Args:
state (state array): the current state of the game
action ([int, int, int]): the action took this step
reward (int): the reward for the action of this step
next_state (state array): the next state of the game
game_over (bool): whether or not the game ended
"""
# Store all parameters as one tuple in the memory
self.memory.append((state, action, reward, next_state, game_over)) # pops left if max mem is exceeded
def trainLongMem(self):
"""Trains the model after game over. Uses a batch of memory rather than just one step
"""
if len(self.memory) > BATCH_SIZE:
# Generate a random batch of memory samples
miniSample = random.sample(self.memory, BATCH_SIZE)
else:
# Use whole memory if not enough samples for full batch
miniSample = self.memory
# Unpack all the states, actions, rewards, etc. from miniSample, and repack
# in groups using built in zip function
states, actions, rewards, next_states, game_overs = zip(*miniSample)
# Pass the new packages to the trainer
self.trainer.trainStep(states, actions, rewards, next_states, game_overs)
def trainShortMem(self, state, action, reward, next_state, game_over):
"""Trains the model after every step.
Args:
state (state array): the current state of the game
action ([int, int, int]): the action took this step
reward (int): the reward for the action of this step
next_state (state array): the next state of the game
game_over (bool): whether or not the game ended
"""
self.trainer.trainStep(state, action, reward, next_state, game_over)
def getAction(self, state):
"""Determines the move for the current step in the game. Either random, or predicted
based on the number of games played
Args:
state (state array): the current state of the game
Returns:
[int, int, int]: The action to be performed this step
"""
# random moves: tradeoff exploration / exploitation
# Random moves when still exploring/learning. (exploration)
# Less random moves as the model gets better and better. (exploitation)
self.epsilon = RANDOMNESS - self.num_games * RAND_DECAY
nextMove = [0, 0, 0]
# As num games increases, if statement will be True less
# If true, does random move
if random.randint(0, 200) < self.epsilon:
self.randmove += 1
move = random.randint(0, 2)
nextMove[move] = 1
# If False, does predicted move from model
else:
state0 = torch.tensor(state, dtype=torch.float) # converts state array to torch tensor object
prediction = self.model(state0) # calls the model's forward function (gets model's prediction)
move = torch.argmax(prediction).item()
nextMove[move] = 1
return nextMove
# POSSIBLY REMOVE
def _calcualteEpsilon(self) -> int:
epsilon = RANDOMNESS
# Calculate any decreases for epsilon (randomness)
decrease_epsilon = self.num_games
if self.loaded_model:
decrease_epsilon += RANDOMNESS
# Increases randomness as model stops improving
increase_epsilon = self.numGamesUnimproved
# Limit the dynamic randomness
if increase_epsilon > RANDOMNESS * 0.01:
increase_epsilon = RANDOMNESS * 0.01
if decrease_epsilon < epsilon:
epsilon -= decrease_epsilon
epsilon += increase_epsilon
else:
epsilon = 0 + increase_epsilon
return epsilon
class AgentTrainer():
def __init__(self, agent:Agent, game:SnakeGame, path:str = None) -> None:
"""Initializes an agent trainer, which takes an agent and trains its model.
Args:
agent (Agent): Agent in which the model will be trained
game (SnakeGame): Game where the agent is learning to play
"""
# Init Variables for the Trainer
self.agent = agent
self.game = game
self.model_path = path # Specifies a model to save to from the model/ directory
self.plotScores = []
self.plotMeanScores = []
self.totalScore = 0
self.record = 0
def train(self):
""" Trains the agent and model for the current frame
"""
# get old state
oldState = self.agent.getState(self.game)
# get the next move based on the state
nextMove = self.agent.getAction(oldState)
# perform the move and get the new game state
reward, gameOver, score = self.game.playStep(nextMove)
newState = self.agent.getState(self.game)
# train short mem (1 step)
self.agent.trainShortMem(oldState, nextMove, reward, newState, gameOver)
# remember
self.agent.remember(oldState, nextMove, reward, newState, gameOver)
if gameOver:
# train long memory and plot results of game
self.game.reset()
self.agent.num_games += 1
self.agent.trainLongMem()
# automaticall save the model as it gets better scores
# also manages the dynamic randomness
if score > self.record:
record = score
if self.model_path:
self.agent.model.save(self.model_path)
else:
self.agent.model.save()
# plot results in pyplot (only working n pycharm)
self.plotScores.append(score)
self.totalScore += score
meanScore = self.totalScore / self.agent.num_games
self.plotMeanScores.append(meanScore)
print('Game', self.agent.num_games, 'Score', score, 'Record:', self.record, 'Average:', meanScore, 'Epsi:', self.agent.epsilon, "Rands:", self.agent.randmove)\
#reset the rand move
self.agent.randmove = 0
# plot(plotScores, plotMeanScores)
# Print out model data-----------------------REMOVE LATER
# for param_tensor in agent.model.state_dict():
# print(param_tensor, "\t", agent.model.state_dict()[param_tensor].size())
# for var_name in agent.trainer.optimizer.state_dict():
# print(var_name, "\t", agent.trainer.optimizer.state_dict()[var_name])
# Run the program from "python agent.py" command