-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.py
executable file
·415 lines (322 loc) · 11.7 KB
/
main.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python3
import sys
import os
directory=os.getcwd()
directory=directory+'/model'
if not directory in sys.path:
sys.path.insert(0,directory)
print("adding directory path")
import time
import sys
import threading
import copy
import random
from optparse import OptionParser
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QInputDialog
from PyQt5.QtWidgets import QLabel, QTextEdit, QFrame
from PyQt5.QtWidgets import QPushButton, QSlider, QHBoxLayout, QVBoxLayout
from PyQt5.QtGui import QImage, QPixmap, QPainter, QColor
<<<<<<< HEAD
=======
# Gym environment used by the Baby AI Game
>>>>>>> 80b5ea0f5f67cca97c6a5770250b2b54a9e86dc7
import gym
import gym_minigrid
from gym_minigrid import minigrid
import levels
import agents
class ImgWidget(QLabel):
"""
Widget to intercept clicks on the full image view
"""
def __init__(self, window):
super().__init__()
self.window = window
def mousePressEvent(self, event):
self.window.imageClick(event.x(), event.y())
class AIGameWindow(QMainWindow):
"""Application window for the baby AI game"""
def __init__(self, env):
super().__init__()
self.initUI()
# By default, manual stepping only
self.fpsLimit = 0
self.env = env
self.lastObs = None
self.resetEnv()
self.stepTimer = QTimer()
self.stepTimer.setInterval(0)
self.stepTimer.setSingleShot(False)
self.stepTimer.timeout.connect(self.stepClicked)
self.stepIdx=1
# Pointing and naming data
self.pointingData = []
def initUI(self):
"""Create and connect the UI elements"""
self.resize(512, 512)
self.setWindowTitle('Baby AI Game')
# Full render view (large view)
self.imgLabel = ImgWidget(self)
self.imgLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)
leftBox = QVBoxLayout()
leftBox.addStretch(1)
leftBox.addWidget(self.imgLabel)
leftBox.addStretch(1)
# Area on the right of the large view
rightBox = self.createRightArea()
# Arrange widgets horizontally
hbox = QHBoxLayout()
hbox.addLayout(leftBox)
hbox.addLayout(rightBox)
# Create a main widget for the window
mainWidget = QWidget(self)
self.setCentralWidget(mainWidget)
mainWidget.setLayout(hbox)
# Show the application window
self.show()
self.setFocus()
def createRightArea(self):
# Agent render view (partially observable)
self.obsImgLabel = QLabel()
self.obsImgLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)
miniViewBox = QHBoxLayout()
miniViewBox.addStretch(1)
miniViewBox.addWidget(self.obsImgLabel)
miniViewBox.addStretch(1)
self.missionBox = QTextEdit()
self.missionBox.setMinimumSize(500, 100)
self.missionBox.textChanged.connect(self.missionEdit)
buttonBox = self.createButtons()
self.stepsLabel = QLabel()
self.stepsLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)
self.stepsLabel.setAlignment(Qt.AlignCenter)
self.stepsLabel.setMinimumSize(60, 10)
resetBtn = QPushButton("Reset")
resetBtn.clicked.connect(self.resetEnv)
stepsBox = QHBoxLayout()
stepsBox.addStretch(1)
stepsBox.addWidget(QLabel("Steps remaining"))
stepsBox.addWidget(self.stepsLabel)
stepsBox.addWidget(resetBtn)
stepsBox.addStretch(1)
hline2 = QFrame()
hline2.setFrameShape(QFrame.HLine)
hline2.setFrameShadow(QFrame.Sunken)
# Stack everything up in a vetical layout
vbox = QVBoxLayout()
vbox.addLayout(miniViewBox)
vbox.addLayout(stepsBox)
vbox.addWidget(hline2)
vbox.addWidget(QLabel("Mission"))
vbox.addWidget(self.missionBox)
vbox.addLayout(buttonBox)
return vbox
def createButtons(self):
"""Create the row of UI buttons"""
stepButton = QPushButton("Step")
stepButton.clicked.connect(self.stepClicked)
minusButton = QPushButton("- Reward")
minusButton.clicked.connect(self.minusReward)
plusButton = QPushButton("+ Reward")
plusButton.clicked.connect(self.plusReward)
slider = QSlider(Qt.Horizontal, self)
slider.setFocusPolicy(Qt.NoFocus)
slider.setMinimum(0)
slider.setMaximum(100)
slider.setValue(0)
slider.valueChanged.connect(self.setFrameRate)
self.fpsLabel = QLabel("Manual")
self.fpsLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)
self.fpsLabel.setAlignment(Qt.AlignCenter)
self.fpsLabel.setMinimumSize(80, 10)
# Assemble the buttons into a horizontal layout
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(stepButton)
hbox.addWidget(slider)
hbox.addWidget(self.fpsLabel)
hbox.addStretch(1)
hbox.addWidget(minusButton)
hbox.addWidget(plusButton)
hbox.addStretch(1)
return hbox
def keyPressEvent(self, e):
# Manual agent control
actions = self.env.unwrapped.actions
if e.key() == Qt.Key_Left:
self.stepEnv(actions.left)
elif e.key() == Qt.Key_Right:
self.stepEnv(actions.right)
elif e.key() == Qt.Key_Up:
self.stepEnv(actions.forward)
elif e.key() == Qt.Key_PageUp:
self.stepEnv(actions.pickup)
elif e.key() == Qt.Key_PageDown:
self.stepEnv(actions.drop)
elif e.key() == Qt.Key_Space:
self.stepEnv(actions.toggle)
elif e.key() == Qt.Key_Backspace:
self.resetEnv()
elif e.key() == Qt.Key_Escape:
self.close()
def mousePressEvent(self, event):
"""
Clear the focus of the text boxes and buttons if somewhere
else on the window is clicked
"""
# Set the focus on the full render image
self.imgLabel.setFocus()
QMainWindow.mousePressEvent(self, event)
def imageClick(self, x, y):
"""
Pointing and naming logic
"""
# Set the focus on the full render image
self.imgLabel.setFocus()
env = self.env.unwrapped
imgW = self.imgLabel.size().width()
imgH = self.imgLabel.size().height()
i = (env.grid.width * x) // imgW
j = (env.grid.height * y) // imgH
assert i < env.grid.width
assert j < env.grid.height
print('grid clicked: i=%d, j=%d' % (i, j))
desc, ok = QInputDialog.getText(self, 'Pointing & Naming', 'Enter Description:')
desc = str(desc)
if not ok or len(desc) == 0:
return
pointObj = env.grid.get(i, j)
if pointObj is None:
return
print('description: "%s"' % desc)
print('object: %s %s' % (pointObj.color, pointObj.type))
viewSz = minigrid.AGENT_VIEW_SIZE
NUM_TARGET = 50
numItrs = 0
numPos = 0
numNeg = 0
while (numPos < NUM_TARGET or numNeg < NUM_TARGET) and numItrs < 300:
env2 = copy.deepcopy(env)
# Randomly place the agent around the selected point
x, y = i, j
x += random.randint(-viewSz, viewSz)
y += random.randint(-viewSz, viewSz)
x = max(0, min(x, env2.grid.width - 1))
y = max(0, min(y, env2.grid.height - 1))
env2.agent_pos = (x, y)
env2.agent_dir = random.randint(0, 3)
# Don't want to place the agent on top of something
if env2.grid.get(*env2.agent_pos) != None:
continue
agent_sees = env2.agent_sees(i, j)
obs = env2.gen_obs()
img = obs['image'] if isinstance(obs, dict) else obs
obsGrid = minigrid.Grid.decode(img)
datum = {
'desc': desc,
'img': img,
'pos': (i, j),
'present': agent_sees
}
if agent_sees and numPos < NUM_TARGET:
self.pointingData.append(datum)
numPos += 1
if not agent_sees and numNeg < NUM_TARGET:
# Don't want identical object in mismatch examples
if (pointObj.color, pointObj.type) not in obsGrid:
self.pointingData.append(datum)
numNeg += 1
numItrs += 1
print('positive examples: %d' % numPos)
print('negative examples: %d' % numNeg)
print('total examples: %d' % len(self.pointingData))
def missionEdit(self):
# The agent will get the mission as an observation
# before performing the next action
text = self.missionBox.toPlainText()
self.lastObs['mission'] = text
def plusReward(self):
print('+reward')
self.env.setReward(1)
def minusReward(self):
print('-reward')
self.env.setReward(-1)
def stepClicked(self):
self.stepEnv(action=None)
def setFrameRate(self, value):
"""Set the frame rate limit. Zero for manual stepping."""
print('Set frame rate: %s' % value)
self.fpsLimit = int(value)
if value == 0:
self.fpsLabel.setText("Manual")
self.stepTimer.stop()
elif value == 100:
self.fpsLabel.setText("Fastest")
self.stepTimer.setInterval(0)
self.stepTimer.start()
else:
self.fpsLabel.setText("%s FPS" % value)
self.stepTimer.setInterval(int(1000 / self.fpsLimit))
self.stepTimer.start()
def resetEnv(self):
obs = self.env.reset()
self.lastObs = obs
self.showEnv(obs)
def showEnv(self, obs):
unwrapped = self.env.unwrapped
# Render and display the environment
pixmap = self.env.render(mode='pixmap')
self.imgLabel.setPixmap(pixmap)
# Render and display the agent's view
image = obs['image']
obsPixmap = unwrapped.get_obs_render(image)
self.obsImgLabel.setPixmap(obsPixmap)
# Update the mission text
mission = obs['mission']
self.missionBox.setPlainText(mission)
# Set the steps remaining
stepsRem = unwrapped.steps_remaining
self.stepsLabel.setText(str(stepsRem))
def stepEnv(self, action=None):
<<<<<<< HEAD
#print('stepEnv : ',self.stepIdx)
#print('action=%s' % action)
# If the environment doesn't supply a mission, get the
# mission from the input text box
if not hasattr(self.lastObs, 'mission'):
text = self.missionBox.toPlainText()
self.lastObs['mission'] = text
=======
>>>>>>> 80b5ea0f5f67cca97c6a5770250b2b54a9e86dc7
# If no manual action was specified by the user
if action == None:
action = random.randint(0, self.env.action_space.n - 1)
obs, reward, done, info = self.env.step(action)
self.showEnv(obs)
self.lastObs = obs
self.stepIdx+=1
if done:
self.resetEnv()
def main(argv):
parser = OptionParser()
parser.add_option(
"--env-name",
help="gym environment to load",
default='MiniGrid-MultiRoom-N6-v0'
)
(options, args) = parser.parse_args()
# Load the gym environment
<<<<<<< HEAD
env = gym.make(options.env)
env = Teacher(env)
=======
env = gym.make(options.env_name)
>>>>>>> 80b5ea0f5f67cca97c6a5770250b2b54a9e86dc7
# Create the application window
app = QApplication(sys.argv)
window = AIGameWindow(env)
# Run the application
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv)