-
Notifications
You must be signed in to change notification settings - Fork 0
/
RLManager.py
129 lines (113 loc) · 3.86 KB
/
RLManager.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
class StartControl:
def __init__(self):
self.isStart = False
self.playerType = 'SARSA'
self.isReset = False
def start(self):
self.isStart = True
def stop(self):
self.isStart = False
def changePlayer(self, type):
self.playerType = type
def reset(self):
self.isReset = True
def Save(agent):
import pickle
output = open('RL-Agent.txt', 'wb')
pickle.dump(agent, output)
output.close()
def Load(agentList):
import pickle
input = open('RL-Agent.txt', 'rb')
agentList[0] = pickle.load(input)
def ChangeToSARSA(agentList, flowMgr):
agentList[0] = SARSA(0.1, 0.1, 0.9, (-1, 1))
flowMgr.changePlayer('SARSA')
flowMgr.reset()
def ChangeToHuman(agentList, flowMgr):
flowMgr.changePlayer('Human')
flowMgr.reset()
if __name__ == "__main__":
from SARSA import SARSA
from PongUI import PongUI
from Environment import Environment
from pgu import gui
import sys,pygame
app = gui.App()
form = gui.Form()
ui = PongUI()
frame = gui.Container(align=-1,valign=-1)
frame.add(ui,0,0)
app.init(frame)
gameScreenSize = 400, 400
uiSize = 400, 400
assert uiSize[1] == gameScreenSize[1]
screenSize = (gameScreenSize[0] + uiSize[0], gameScreenSize[1])
discrete_size = 20
delay = 100
interval = 50
action = 0
pygame.init()
pygame.key.set_repeat(delay, interval)
clock=pygame.time.Clock()
screen = pygame.display.set_mode(screenSize)
agentList = []
#agentList.append(SARSA(0.01, 0.1, 0.95, (-1, 1)))
agentList.append(SARSA(float(form['alphaLabel'].value) , float(form['epsilonLabel'].value), float(form['gammaLabel'].value), (-1, 1)))
start = StartControl()
ui.setStartListener(start.start)
ui.setStopListener(start.stop)
ui.setSaveHandler(lambda : Save(agentList[0]))
ui.setLoadHandler(lambda : Load(agentList))
ui.setPlayerHandler( dict(
SARSA= (lambda : ChangeToSARSA(agentList, start)),
Human= (lambda : ChangeToHuman(agentList, start)),
Q_Learning= (lambda : True)
))
episodeNum = 0
stepNum = 0
while 1:
agent = agentList[0]
env = Environment(gameScreenSize, discrete_size)
state = env.start()
if start.playerType == 'Human':
action = 0
else:
action = agent.start(state)
episodeNum += 1
start.isReset = False
while 1:
#remove me
agent = agentList[0]
#update game status here
form['stepLabel'].value = stepNum
form['episodeLabel'].value = episodeNum
#clock.tick(1000)
clock.tick(float(form['fpsLabel'].value))
for event in pygame.event.get():
if start.playerType == 'Human':
action = 0
if event.type == pygame.QUIT: sys.exit()
elif event.type==pygame.KEYDOWN and start.playerType == 'Human':
if event.key==pygame.K_LEFT:
action = -1
if event.key==pygame.K_RIGHT:
action = 1
else:
app.event(event)
screen.blit(env.getScreen(), (uiSize[0], 0))
app.paint(screen)
#print reward
#print state
#print isTerminal
pygame.display.flip()
if start.isReset:
break
if start.isStart:
(reward, state, isTerminal) = env.step(action)
if start.playerType != 'Human':
action = agent.step(reward, state)
stepNum += 1
if isTerminal:
agent.end(reward)
break