-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_rl.py
106 lines (77 loc) · 1.97 KB
/
play_rl.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
from gui.gui_info import GUIInfo
from gui.gui_manager import GUIManager
from logic.game_board import GameBoard
from rl.agent.epsilon import EpsilonInfo
from rl.agent.agent import Agent
from rl.state.utils.utils import state_to_numpy
from constants import *
import random
import cv2
import numpy as np
import matplotlib.pyplot as plt
rows = 7
cols = 7
guiInfo = GUIInfo(rows = rows,
cols = cols,
side = 35,
space = 3,
offset = (3, 3),
name = ':: Manga Snake ::')
def runStep(game_board, action):
if actions_map[action] == 'LEFT':
game_board.goLeft()
elif actions_map[action] == 'RIGHT':
game_board.goRight()
else: # action == 'S', straight
pass
game_state = game_board.update()
next_state = game_board.getState()
return (game_state, next_state)
name = '255_train.h5'
actions_map = {0:'LEFT',
1:'STRAIGHT',
2:'RIGHT'}
guiManager = GUIManager(guiInfo)
game_board = GameBoard(rows, cols)
episodes = 1000
steps = 200
eps_info = EpsilonInfo(0.00, 0.00, 0.00)
agent = Agent(rows + 2,
cols + 2,
actions_map, 0.0, 0.0, 0, eps_info)
agent.load('save/' + name)
# Main Loop
done = False
for w in range(episodes):
if done:
break
game_board.reset()
foodEaten = 0
wallHitten = 0
bodyHitten = 0
for v in range(steps):
if done:
break
curr_state = state_to_numpy(game_board.getState())
action = agent.act(curr_state)
result, next_state = runStep(game_board, action)
guiManager.update(next_state)
guiManager.show()
key = guiManager.waitKey(100)
if key == K_ESC:
done = True
if result.hitWall or result.hitSelf:
if result.hitWall:
wallHitten += 1
if result.hitSelf:
bodyHitten += 1
game_board.reset()
continue
if result.gotFood:
foodEaten += 1
print('episode', w + 1)
print('food:', foodEaten)
print('wall:', wallHitten)
print('body:', bodyHitten)
print()
guiManager.finish()