Skip to content
Open

Init #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# Teach AI To Play Snake! Reinforcement Learning With PyTorch and Pygame
# How to Start

In this Python Reinforcement Learning Tutorial series we teach an AI to play Snake! We build everything from scratch using Pygame and PyTorch. The tutorial consists of 4 parts:

You can find all tutorials on my channel: [Playlist](https://www.youtube.com/playlist?list=PLqnslRFeH2UrDh7vUmJ60YrmWd64mTTKV)

- Part 1: I'll show you the project and teach you some basics about Reinforcement Learning and Deep Q Learning.
- Part 2: Learn how to setup the environment and implement the Snake game.
- Part 3: Implement the agent that controls the game.
- Part 4: Implement the neural network to predict the moves and train it.
agent.py is the instructon for the computer.
Binary file added __pycache__/game.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/helper.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/model.cpython-310.pyc
Binary file not shown.
8 changes: 3 additions & 5 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Agent:

def __init__(self):
self.n_games = 0
self.epsilon = 0 # randomness
self.gamma = 0.9 # discount rate
self.memory = deque(maxlen=MAX_MEMORY) # popleft()
self.epsilon = 0
self.gamma = 0.9
self.memory = deque(maxlen=MAX_MEMORY)
self.model = Linear_QNet(11, 256, 3)
self.trainer = QTrainer(self.model, lr=LR, gamma=self.gamma)

Expand Down Expand Up @@ -78,8 +78,6 @@ def train_long_memory(self):

states, actions, rewards, next_states, dones = zip(*mini_sample)
self.trainer.train_step(states, actions, rewards, next_states, dones)
#for state, action, reward, nexrt_state, done in mini_sample:
# self.trainer.train_step(state, action, reward, next_state, done)

def train_short_memory(self, state, action, reward, next_state, done):
self.trainer.train_step(state, action, reward, next_state, done)
Expand Down
Binary file removed arial.ttf
Binary file not shown.
9 changes: 5 additions & 4 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import numpy as np

pygame.init()
font = pygame.font.Font('arial.ttf', 25)
#font = pygame.font.SysFont('arial', 25)
font = pygame.font.SysFont('arial', 25)

class Direction(Enum):
RIGHT = 1
Expand Down Expand Up @@ -78,13 +77,15 @@ def play_step(self, action):
game_over = False
if self.is_collision() or self.frame_iteration > 100*len(self.snake):
game_over = True
reward = -10
reward = -5
return reward, game_over, self.score

# 4. place new food or just move
if self.head == self.food:
self.score += 1
reward = 10
reward = 5


self._place_food()
else:
self.snake.pop()
Expand Down
64 changes: 64 additions & 0 deletions grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import pygame as pg

TITLE = "Grid"
TILES_HORIZONTAL = 10
TILES_VERTICAL = 10
TILE_SIZE = 80
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 800


class Player:
def __init__(self, surface):
self.surface = surface
self.pos = (40, 40)

def draw(self):
pg.draw.circle(self.surface, (255, 255, 255), self.pos, 40)

def move(self, target):
x = (80 * (target[0] // 80)) + 40
y = (80 * (target[1] // 80)) + 40

self.pos = (x, y)


class Game:
def __init__(self):
pg.init()
self.clock = pg.time.Clock()
pg.display.set_caption(TITLE)
self.surface = pg.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
self.loop = True
self.player = Player(self.surface)

def main(self):
while self.loop:
self.grid_loop()
pg.quit()

def grid_loop(self):
self.surface.fill((0, 0, 0))
for row in range(TILES_HORIZONTAL):
for col in range(row % 2, TILES_HORIZONTAL, 2):
pg.draw.rect(
self.surface,
(40, 40, 40),
(row * TILE_SIZE, col * TILE_SIZE, TILE_SIZE, TILE_SIZE),
)
self.player.draw()
for event in pg.event.get():
if event.type == pg.QUIT:
self.loop = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
self.loop = False
elif event.type == pg.MOUSEBUTTONUP:
pos = pg.mouse.get_pos()
self.player.move(pos)
pg.display.update()


if __name__ == "__main__":
mygame = Game()
mygame.main()
5 changes: 1 addition & 4 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,4 @@ def train_step(self, state, action, reward, next_state, done):
loss = self.criterion(target, pred)
loss.backward()

self.optimizer.step()



self.optimizer.step()
Binary file added model/model.pth
Binary file not shown.