-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
488 lines (383 loc) · 17.3 KB
/
environment.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import pygame
import random
import bisect
# import speech_recognition
class Simulation:
grid = []
agent_blocks = []
moving_obstacles = []
obstacle_percentages = []
agent_percentages = []
# slip_percentages[current_x][current_y][action](x_result, y_result, prob)
slip_percentages = []
log = {"agents": [], "moving_obstacles": []}
WINDOW_SIZE = [0, 0]
HEIGHT = 0
WIDTH = 0
MARGIN = 2
time_step = 0
screen = pygame.display.set_mode((0,0))
clock = pygame.time.Clock()
pygame.display.set_caption("Environment")
# recognizer = speech_recognition.Recognizer()
matrix_active = False
slip_active = False
def __init__(self, configFile):
#import config file
configFile = open(configFile, 'r')
for line in configFile:
line = line.split(" ")
if line[0] == "HEIGHT:":
self.HEIGHT = int(line[1])
elif line[0] == "WIDTH:":
self.WIDTH = int(line[1])
for column in range(self.WIDTH):
self.grid.append([])
for row in range(self.HEIGHT):
self.grid[column].append(Empty_block())
#elif line[0] == "MARGIN:":
# self.MARGIN = int(line[1])
elif line[0] == "BLOCK:":
if line[1] == "agent":
self.agent_blocks.append(Agent_block(int(line[2]),int(line[3])))
elif line[1] == "fixed_obstacle":
self.grid[int(line[2])][int(line[3])] = Obstacle_block()
elif line[1] == "moving_obstacle":
self.moving_obstacles.append(Moving_obstacle_block(int(line[2]), int(line[3])))
elif line[1] == "goal":
self.grid[int(line[2])][int(line[3])] = Goal_block()
else:
self.grid[int(line[2])][int(line[3])] = Empty_block()
self.WINDOW_SIZE[0] = int(30*self.WIDTH+2)
self.WINDOW_SIZE[1] = int(30*self.HEIGHT+2)
self.screen = pygame.display.set_mode(self.WINDOW_SIZE)
for i in self.agent_blocks:
self.agent_percentages.append({})
self.log["agents"].append([])
for i in self.moving_obstacles:
self.obstacle_percentages.append({})
self.log["moving_obstacles"].append([])
#setup slip percentages
for i in range(self.WIDTH):
self.slip_percentages.append([])
for k in range(self.HEIGHT):
self.slip_percentages[i].append([])
self.slip_percentages[i][k] = {'north': [], 'east': [], 'south': [], 'west': []}
#self.import_matrix_file(matrixFile)
pygame.init()
# self.recognizer.pause_threshold = 0.5
def clear(self):
self.screen.fill([0,0,0])
def out_of_bounds(self, x_pos, y_pos):
#return true if the position is outside of the playfield
return not (x_pos >= 0 and x_pos < self.WIDTH and y_pos >= 0 and y_pos < self.HEIGHT)
def move_agent(self, number, action):
x_pos = self.agent_blocks[number].column
y_pos = self.agent_blocks[number].row
self.log["agents"][number].append((self.time_step, (x_pos, y_pos), action))
if self.slip_active:
#then slip
#only execute of the action would keep the block within the grid
if action == "north" and y_pos > 0 or action == "south" and y_pos < self.HEIGHT - 1 or action == "west" and x_pos > 0 or action == "east" and x_pos < self.WIDTH - 1:
percents = []
percents_temp = self.slip_percentages[x_pos][y_pos][action] #list of tuples in form of ((x_result, y_result), probability of landing there)
for percent in percents_temp:
if not self.out_of_bounds(percent[0][0], percent[0][1]):
percents.append(percent)
result = self.weighted_choice(percents)
self.agent_blocks[number].move_to(result[0], result[1])
else:
if action == "north" and y_pos > 0:
self.agent_blocks[number].move_north()
elif action == "south" and y_pos < self.HEIGHT - 1:
self.agent_blocks[number].move_south()
elif action == "west" and x_pos > 0:
self.agent_blocks[number].move_west()
elif action == "east" and x_pos < self.WIDTH - 1:
self.agent_blocks[number].move_east()
def draw(self):
for column in range(self.WIDTH):
for row in range(self.HEIGHT):
self.grid[column][row].draw(self.screen, column, row, self.MARGIN)
for agent in self.agent_blocks:
agent.draw(self.screen, agent.column, agent.row, self.MARGIN)
for obstacle in self.moving_obstacles:
obstacle.draw(self.screen, obstacle.column, obstacle.row, self.MARGIN)
pygame.display.flip()
def get_state(self):
agent_list = []
fixed_obstacle_list = []
moving_obstacle_list = []
for agent in self.agent_blocks:
agent_list.append((agent.column, agent.row))
for column in range(self.WIDTH):
for row in range(self.HEIGHT):
if type(self.grid[column][row]) is Obstacle_block:
fixed_obstacle_list.append((column,row))
for obstacle in self.moving_obstacles:
moving_obstacle_list.append((obstacle.column, obstacle.row))
dict_blocks = {"agents": agent_list, "fixed_obstacles": fixed_obstacle_list, "moving_obstacles": moving_obstacle_list}
return dict_blocks
#action = weighted_choice([(1,0), (0,100)])
def weighted_choice(self, choices):
values, weights = zip(*choices)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
x = random.random() * total
i = bisect.bisect(cum_weights, x)
return values[i]
def move_obstacle(self, number, action):
x_pos = self.moving_obstacles[number].column
y_pos = self.moving_obstacles[number].row
self.log["moving_obstacles"][number].append((self.time_step, (x_pos, y_pos), action))
if action == "north" and y_pos > 0:
self.moving_obstacles[number].move_north()
elif action == "south" and y_pos < self.HEIGHT - 1:
self.moving_obstacles[number].move_south()
elif action == "west" and x_pos > 0:
self.moving_obstacles[number].move_west()
elif action == "east" and x_pos < self.WIDTH - 1:
self.moving_obstacles[number].move_east()
def load_matrix_file(self, matrixFile):
matrixFile = file(matrixFile, 'r')
for line in matrixFile:
if len(line) == 0:
continue
line = line.split(" ")
if len(line[0]) == 1:
if line[0] == 'a':
for agent_percentage in self.agent_percentages:
agent_percentage[(int(line[1]), int(line[2]))] = (int(line[3]), int(line[4]), int(line[5]), int(line[6]), int(line[6]))
elif line[0] == 'o':
for obstacle_percentage in self.obstacle_percentages:
obstacle_percentage[(int(line[1]), int(line[2]))] = (int(line[3]), int(line[4]), int(line[5]), int(line[6]), int(line[6]))
continue
index = int(line[0][1])
if line[0][0] == 'a' and index < len(self.agent_percentages):
self.agent_percentages[index][(int(line[1]), int(line[2]))] = (int(line[3]), int(line[4]), int(line[5]), int(line[6]), int(line[7]))
elif line[0][0] == 'o' and index < len(self.obstacle_percentages):
self.obstacle_percentages[index][(int(line[1]), int(line[2]))] = (int(line[3]), int(line[4]), int(line[5]), int(line[6]), int(line[7]))
self.matrix_active = True
matrixFile.close()
def load_slip_file(self, slipFile):
slipFile = open(slipFile, "r")
for line in slipFile:
line = line.split(" ")
print (line)
print (line[5])
# current_x current_y action (x_result, y_result, prob)
#self.slip_percentages[1][2]['north'].append(1, 1, .5)
self.slip_percentages[int(line[0])][int(line[1])][line[2]].append(((int(line[3]), int(line[4])), float(line[5])))
self.slip_active = True
slipFile.close()
def move_obstacles(self):
index = 0
for obstacle in self.moving_obstacles:
obstacle_percentages = self.obstacle_percentages[index][(obstacle.column, obstacle.row)]
action = self.weighted_choice((("north", obstacle_percentages[0]), ("east", obstacle_percentages[1]), ("south", obstacle_percentages[2]), ("west", obstacle_percentages[3]), ("stay", obstacle_percentages[4])))
self.move_obstacle(index, action)
index += 1
def move_agents_matrix(self):
index = 0
for agent in self.agent_blocks:
agent_percentages = self.agent_percentages[index][(agent.column, agent.row)]
action = self.weighted_choice((("north", agent_percentages[0]), ("east", agent_percentages[1]), ("south", agent_percentages[2]), ("west", agent_percentages[3]), ("stay", agent_percentages[4])))
self.move_agent(index, action)
index += 1
def move_agent_matrix(self, index):
agent = self.agent_blocks[index]
agent_percentages = self.agent_percentages[index][(agent.column, agent.row)]
action = self.weighted_choice((("north", agent_percentages[0]), ("east", agent_percentages[1]), ("south", agent_percentages[2]), ("west", agent_percentages[3]), ("stay", agent_percentages[4])))
self.move_agent(index, action)
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
for agent in self.agent_blocks:
for obstacle in self.moving_obstacles:
if agent.column == obstacle.column and agent.row == obstacle.row:
print ("Moving obstacle hit")
#return True
if isinstance(self.grid[agent.column][agent.row], Obstacle_block):
print ("Fixed obstacle hit")
#return True
if isinstance(self.grid[agent.column][agent.row], Goal_block):
print ("Goal block hit")
#return True
return False
def get_log(self):
return self.log
def get_history(self, steps_back):
if steps_back > self.time_step:
steps_back = self.time_step
out_log = {"agents": [], "moving_obstacles": []}
#self.log["agents"][number].append((self.time_step, (x_pos, y_pos), action))
for number in range(len(self.log["agents"])):
out_log["agents"].append(self.log["agents"][number][-steps_back:])
for number in range(len(self.log["moving_obstacles"])):
out_log["move_obstacles"][number].append(elf.log["moving_obstacles"][number][-steps_back:])
return out_log
# def get_voice(self):
# print "Listening..."
# with speech_recognition.Microphone() as source:
# self.recognizer.non_speaking_duration = 0.3
# self.recognizer.pause_threshold = 0.3
# self.recognizer.adjust_for_ambient_noise(source)
# audio = self.recognizer.listen(source)
# try:
# word = self.recognizer.recognize_sphinx(audio)
# except speech_recognition.UnknownValueError:
# print("Could not understand audio")
# except speech_recognition.RequestError as e:
# print("Recog Error; {0}".format(e))
# print "I heard you say: " + word
# print "Moving..."
# if len(word) == 0:
# return "stay"
# if word[0] < 'f':
# return "east"
# elif word[0] < 'l':
# return "stay"
# elif word[0] < 'q': # Uniform distribution across alphabet
# return "north" # to generate noise
# elif word[0] < 'v':
# return "south"
# else:
# return "west"
def get_key(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
return "north"
elif event.key == pygame.K_RIGHT:
return "east"
elif event.key == pygame.K_DOWN:
return "south"
elif event.key == pygame.K_LEFT:
return "west"
elif event.key == pygame.K_SPACE:
return "stay"
def update(self):
self.move_obstacles()
self.draw()
self.time_step += 1
#self.clock.tick(2)
return self.handle_events()
def step_forward(self):
self.time_step += 1
#self.clock.tick(2)
return self.handle_events()
def generate_agent_matrix(self, matrixFile):
log = self.get_log()["agents"]
f = file(matrixFile, 'w')
counter = 0
for agent in log:
event_count = []
for column in range(self.WIDTH):
event_count.append([])
for row in range(self.HEIGHT):
event_count[column].append([0, 0, 0, 0, 0]) #north, east, south, west, stay
for event in agent:
if event[2] == "north":
index = 0
elif event[2] == "east":
index = 1
elif event[2] == "south":
index = 2
elif event[2] == "west":
index = 3
else:
index = 4
event_count[event[1][0]][event[1][1]][index] += 1
for x in range(len(event_count)):
for y in range(len(event_count[x])):
prob_temp = event_count[x][y]
total_count = sum(prob_temp)
if total_count == 0:
f.write ("a{} {} {} 25 25 25 25 0\n".format(counter, x, y))
else:
f.write("a{} {} {} {} {} {} {} {}\n".format(counter, x, y, 100*prob_temp[0]/total_count, 100*prob_temp[1]/total_count, 100*prob_temp[2]/total_count, 100*prob_temp[3]/total_count, 100*prob_temp[4]/total_count))
f.write("\n")
counter += 1
def move(self, movement):
self.move_obstacles()
self.draw()
if (self.handle_events()):
return (True, 0)
if movement == "matrix":
#move according to matrix
self.move_agents_matrix()
elif movement == "keyboard":
#keyboard input
for agent in range(len(self.agent_blocks)):
self.move_agent(agent, self.get_key())
elif movement == "voic":
for agent in range(len(self.agent_blocks)):
self.move_agent(agent, self.get_voice())
elif movement in ['east','west','south','north']:
for agent in range(len(self.agent_blocks)):
self.move_agent(agent, movement)
else:
#move according to list input
for agent in range(len(movement)):
if movement[agent] == "keyboard":
self.move_agent(agent, self.get_key())
elif movement[agent] == "matrix":
self.move_agent_matrix(agent)
elif movement[agent] == "voice":
self.move_agent(agent, self.get_voice())
else:
self.move_agent(agent, movement[agent])
self.draw()
if (self.step_forward()):
return True, 0
return False, self.get_state()
class Block():
color = (0,0,0)
block_size = 30
def draw(self, surface, column, row, MARGIN):
pygame.draw.rect(surface, self.color, [self.block_size * column + MARGIN, self.block_size * row + MARGIN, self.block_size-MARGIN, self.block_size-MARGIN])
class Obstacle_block(Block):
color = (255,0,0) #red
class Moving_obstacle_block(Obstacle_block):
colomn = 0
row = 0
def __init__(self, column, row):
self.column = column
self.row = row
def move_north(self):
self.row += -1
def move_south(self):
self.row += 1
def move_east(self):
self.column += 1
def move_west(self):
self.column += -1
class Goal_block(Block):
color = (255,255,0) #yellow
class Agent_block(Block):
color = (0,128,0) #green
colomn = 0
row = 0
def __init__(self, column, row):
self.column = column
self.row = row
def move_to(self, column, row):
self.column = column
self.row = row
def move_north(self):
self.row += -1
def move_south(self):
self.row += 1
def move_east(self):
self.column += 1
def move_west(self):
self.column += -1
class Empty_block(Block):
color = (255,255,255)