-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_level_setup.txt
More file actions
451 lines (411 loc) · 21.9 KB
/
diff_level_setup.txt
File metadata and controls
451 lines (411 loc) · 21.9 KB
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
diff --git a/level_setup.py b/level_setup.py
index 2b29c19..d4a0d9d 100644
--- a/level_setup.py
+++ b/level_setup.py
@@ -9,20 +9,32 @@ from ingame_objects.walls import Wall
from ingame_objects.drift_tiles import DriftTile
from ingame_objects.particles import Particle
from ingame_objects.lines import Line
+from displays import display_soc_question
from config import *
-from action_planner.action_planner import ActionPlanner
-from action_planner.CCL_action_selection import select_action_goal
-from action_planner.helper_functions import load_parameters_dict
-from helper_functions import degree_to_pixel
from draw_transparent_shapes import draw_rect_alpha, draw_polygon_alpha, draw_circle_alpha
+from actr import rpc_interface
+import asyncio
-display_keys = True
+display_keys = False
+question_soc = True
+#import threading
+
+import warnings
+warnings.filterwarnings(action="ignore", category=FutureWarning)
+
+"""
+def start_async_loop():
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+ loop.run_forever()
+asyncio_thread = threading.Thread(target=start_async_loop, daemon=True)
+"""
class Level:
def __init__(self, wall_list, obstacles_list, player_starting_position, drift_ranges, screen, scaling, code, FPS=30,
- n_run=0, trial=0, attempt=0, input_noise_magnitude=0,
+ n_run=0, tiny_vis=False, keyboard_input=False, trial=0, attempt=0, input_noise_magnitude=0,
input_noise_threshold=0, drift_enabled=False):
# experiment information
@@ -36,7 +48,8 @@ class Level:
self.attempt = attempt
self.display_surface = screen
self.level_size_y = 'undetermined'
- self.setup_level(wall_list, obstacles_list, player_starting_position, drift_ranges, drift_enabled, scaling)
+ self.setup_level(wall_list, obstacles_list, player_starting_position, drift_ranges, drift_enabled, scaling,
+ tiny_vis, keyboard_input)
# environment movement around agent
self.direction = pygame.math.Vector2(0, 0)
# environmentally imposed drift
@@ -51,7 +64,6 @@ class Level:
# listing visible obstacles in every instance, as well as adjacent wall tiles and last wall tile positions
self.visible_obstacles = []
- self.visible_drift_tiles = []
self.adjacent_wall_tiles_x_pos = []
self.last_wall_pos = []
@@ -80,17 +92,16 @@ class Level:
if 'training' in str(self.trial): # TRY CONTAIN
self.replay_threshold = 1000 # arbitrarily high threshold that is never reached
- self.action_goal_selected = False
-
# pandas Dataframe in which data of each frame will be stored
self.columns = ['trial', 'attempt', 'time_played', 'level_size_y', 'player_pos', 'collision', 'current_input',
'drift_enabled', 'current_drift', 'level_done', 'input_noise_magnitude', 'input_noise_on',
'visible_obstacles', 'last_walls_tile', 'adjacent_wall_tiles_x_pos', 'visible_drift_tiles',
- 'SoC', 'action_goal_selected', 'action_goal_x', 'action_goal_y']
+ 'SoC']
self.data = pd.DataFrame(columns=self.columns)
- def setup_level(self, wall_list, obstacles_list, player_starting_position, drift_ranges, drift_enabled, scaling):
+ def setup_level(self, wall_list, obstacles_list, player_starting_position, drift_ranges, drift_enabled, scaling,
+ tiny_vis, keyboard_input):
self.walls = pygame.sprite.Group()
self.comets = pygame.sprite.Group()
self.drift_tiles = pygame.sprite.Group()
@@ -120,15 +131,20 @@ class Level:
comet_sprite = Comet((key['x'], key['y']), key['size']) # arguments in Comet(): x-pos, y-pos, tile_size
self.comets.add(comet_sprite)
- player_appearance = [player_starting_position[0], (player_starting_position[1] - pre_trial_steps * scaling)]
- player_sprite = Player(player_appearance, agent_size_x, agent_size_y, scaling)
- self.player.add(player_sprite)
+ if keyboard_input:
+ player_appearance = [player_starting_position[0], (player_starting_position[1] - pre_trial_steps * scaling)]
+ player_sprite = Player(player_appearance, agent_size_x, agent_size_y, scaling, tiny_vis)
+ self.player.add(player_sprite)
+
+ else:
+ player_sprite = Player(player_starting_position, agent_size_x, agent_size_y, scaling, tiny_vis)
+ self.player.add(player_sprite)
if drift_enabled:
for i in range(len(drift_ranges)):
drift_info = drift_ranges[i]
# drift_info[0]: y_start, [1]: y_end, [2]: direction+magnitude, [3]: visibility
- drift_tile = DriftTile(drift_info[0], drift_info[1], drift_tile_x_size, observation_space_size_x, edge,
+ drift_tile = DriftTile(drift_info[0], drift_info[1], drift_tile_size_x, observation_space_size_x, edge,
drift_info[2], drift_info[3], scaling)
self.drift_tiles.add(drift_tile)
@@ -144,17 +160,9 @@ class Level:
self.bottom_edge.add(bottom_edge_tile)
finish_line_tile = Line(pos=[edge * scaling, last_wall_tile.rect.y],
- size=[level_size_x * scaling, scaling], col="black")
- # finish line has to be black because otherwise it will mess with select_action_goal()
+ size=[level_size_x * scaling, scaling], col="seagreen")
self.finish_line.add(finish_line_tile)
- # Initiate agent from class ActionPlanner for simulating dynamic decision-making
- parameters = load_parameters_dict("action_planner/Data/parameters.txt")
- self.agent = ActionPlanner(free_parameters=parameters,
- initial_position_x=self.player.sprite.rect.x + scaling,
- observation_space_in_pixel=[observation_space_size_x*scaling, observation_space_size_y*scaling])
- self.convolutionGranularity = int(self.agent.parameters['convolutionGranularity'])
-
def get_input(self):
# input noise
player = self.player.sprite
@@ -162,10 +170,6 @@ class Level:
# input noise magnitude can be any float which reflects the magnitude of actual displacement
# at the end of the left or right step. The magnitude directly translates to the sd of the normal distribution
# the displacement is sampled from.
-
- self.agent.apply_motor_control()
- self.current_input = self.agent.action
-
if player.rect.y > self.input_noise_threshold:
self.input_noise_on = True
mu = 0
@@ -178,61 +182,27 @@ class Level:
self.transparency_left = 90
self.transparency_right = 90
- if self.agent.action == 'Right':
+ keys = pygame.key.get_pressed()
+
+ if keys[pygame.K_m] and keys[pygame.K_y]: # pressing both keys
+ self.transparency_right = 150
+ self.transparency_left = 150
+ self.current_input = None # maybe we have to flag pressing both keys here
+ self.direction.x = 0
+ elif keys[pygame.K_m]: # K_m vs. K_RIGHT
+ self.current_input = 'Right'
self.direction.x = -1 + input_noise
self.transparency_right = 150
- elif self.agent.action == 'Left':
+ elif keys[pygame.K_y]: # K_y vs. K_LEFT
+ self.current_input = 'Left'
self.direction.x = 1 + input_noise
self.transparency_left = 150
- else: # self.agent.action is None
+ else:
+ self.current_input = None
self.direction.x = 0
def update(self):
- # create observation space of agent from subsection of display between boarders
- # get reference point
- reference_point = (self.walls.sprites()[-2].rect.x + scaling, 208)
- # 236 being player.sprite.rect.bottom after approach at the start of level
- # 208 is player.sprite.rect.top; player included in goal-driven visual environment subsection
-
- observation_space = reference_point[0], reference_point[1], 532, 394
- surface_subsection = self.display_surface.subsurface(observation_space)
- surface_array = np.transpose(pygame.surfarray.array_green(surface_subsection))
- # surface_array = np.transpose(pygame.surfarray.array2d(surface_subsection)) # to test
- surface_array[surface_array > 1] = 1
-
- # update agents predictions if horizontal movement present (due to action or drift)
- if self.horizontal_movement != 0:
- self.agent.perceived_step_size = abs(self.horizontal_movement)
- self.agent.prediction_error() #2D ARRAY
-
- # generate action_goal if none is applied OR assess action goal (vertically and horizontally) if one is applied
- self.action_goal_selected = False
- if self.agent.action_goal is None:
- self.agent.action_goal, self.agent.agent_goal_col, _, self.agent.HL_SoC = \
- select_action_goal(PAR=self.agent.parameters,
- HL_SoC=self.agent.HL_SoC,
- observation_in_pixel=surface_array,
- reference=reference_point,
- agent_pos_x=self.agent.agent_pos_x,
- min_percentage_for_rejection=self.agent.min_percentage_for_rejection)
- self.action_goal_selected = True
- else:
- # monitoring application of selected action goal
- self.agent.assess_action_goal(observation_in_pixel=surface_array, radius=scaling)
- if self.agent.action_goal is None:
- self.agent.action_goal, self.agent.action_goal_col, _, self.agent.HL_SoC = \
- select_action_goal(PAR=self.agent.parameters,
- HL_SoC=self.agent.HL_SoC,
- observation_in_pixel=surface_array,
- reference=reference_point,
- agent_pos_x=self.agent.agent_pos_x,
- min_percentage_for_rejection=self.agent.min_percentage_for_rejection)
- self.action_goal_selected = True
-
- # get input from agent
self.get_input()
-
- # apply horizontal movement of agent in environment (environment moves around agent)
self.horizontal_movement = self.direction.x + self.drift.x # compute horizontal movement with drift
def check_for_collision(self):
@@ -246,6 +216,18 @@ class Level:
else:
self.frames_with_collision = 0
+ # checking for individual collisions:
+ # # with obstacles
+ # for sprite in self.comets.sprites():
+ # if sprite.rect.colliderect(player.rect): # check for player-comet collision
+ # self.frames_with_collision += 1
+ #
+ # # with walls
+ # for sprite in self.walls.sprites():
+ # if sprite.rect.colliderect(player.rect): # check for player-wall collision
+ # self.currently_colliding = True
+ # self.frames_with_collision += 1
+
# check for collision threshold of consecutive frames with collision
if self.frames_with_collision > self.frames_collision_threshold:
player.crashed = True
@@ -260,6 +242,24 @@ class Level:
elif player.rect.bottom in range(sprite.rect.top, sprite.rect.bottom):
self.drift.x = -sprite.direction
+ def get_soc_response(self):
+ keys = pygame.key.get_pressed()
+ if keys[pygame.K_1]:
+ self.SoC = 1
+ if keys[pygame.K_2]:
+ self.SoC = 2
+ if keys[pygame.K_3]:
+ self.SoC = 3
+ if keys[pygame.K_4]:
+ self.SoC = 4
+ if keys[pygame.K_5]:
+ self.SoC = 5
+ if keys[pygame.K_6]:
+ self.SoC = 6
+ if keys[pygame.K_7]:
+ self.SoC = 7
+ return self.SoC
+
def get_data(self, scaling):
frame_data = pd.DataFrame(columns=self.columns)
@@ -279,26 +279,29 @@ class Level:
frame_data.attempt = self.attempt
frame_data.level_size_y = self.level_size_y
- # walls
- # There has to be a better alternative instead of simply inserting all wall tiles into a list.
- # Rather have one wall tile given and then distance to other wall? Or just distance from agent to wall left
- # and right? - brainstorming
+ """
+ Walls
+ There has to be a better alternative instead of simply inserting all wall tiles into a list.
+ Rather have one wall tile given and then distance to other wall? Or just distance from agent to wall left
+ and right? - brainstorming
- # wall narrowing start and wall narrowing complete + wall distant again?
- # These would be the only interesting y coordinates
+ wall narrowing start and wall narrowing complete + wall distant again?
+ These would be the only interesting y coordinates
# walls_narrow_start = ? # walls start getting narrow (first step) y coord
# walls_narrow_complete = ? # walls reached narrowest point y coord
# walls_wide_again = ? # based on current_wall_distance='narrow', when walls get wide again
# current_wall_distance = ['wide'] # on y coord of agent
- # stupidly inserting all visible wall tiles in a list into frame_data
+ stupidly inserting all visible wall tiles in a list into frame_data
# visible_walls = []
# for sprite in self.walls.sprites():
# # checking for visibility by checking for y of sprite being between 0 and size of observation window
# if 0 <= sprite.rect.y <= observation_space_size_y * scaling:
# visible_walls.append([sprite.rect.x, sprite.rect.y])
# frame_data.at[0, 'visible_walls'] = visible_walls
+ """
+ # inserting only last wall tile (bottom right of level) for later reconstruction of complete walls
last_wall_tile = self.walls.sprites()[-1]
frame_data.at[0, 'last_walls_tile'] = [last_wall_tile.rect.x, last_wall_tile.rect.y]
@@ -306,18 +309,17 @@ class Level:
frame_data.at[0, 'visible_obstacles'] = self.visible_obstacles
# drift
- frame_data.at[0, 'visible_drift_tiles'] = self.visible_drift_tiles
-
- # action goal
- frame_data.action_goal_selected = self.action_goal_selected
- frame_data.action_goal_x = self.agent.action_goal[0]
- frame_data.action_goal_y = self.agent.action_goal[1]
+ visible_drift_tiles = []
+ for sprite in self.drift_tiles.sprites():
+ if 0 <= sprite.rect.y <= (observation_space_size_y - bottom_edge) * scaling:
+ visible_drift_tiles.append([sprite.rect.x, sprite.rect.y])
+ frame_data.at[0, 'visible_drift_tiles'] = visible_drift_tiles
# append everything to pandas DataFrame
self.data = pd.concat([self.data, frame_data], ignore_index=True)
self.data.SoC = self.SoC
- def run(self, time_played, player_position, scaling):
+ def run(self, time_played, player_position, scaling, tiny_visualization=False, keyboard_input=False):
self.time_played = time_played
player = self.player.sprite
@@ -327,11 +329,6 @@ class Level:
for sprite in self.comets.sprites():
if 0 <= sprite.rect.y <= (observation_space_size_y - bottom_edge) * scaling:
self.visible_obstacles.append([sprite.rect.x, sprite.rect.y])
- # updating visible drift tiles
- self.visible_drift_tiles = []
- for sprite in self.drift_tiles.sprites():
- if 0 <= sprite.rect.y <= (observation_space_size_y - bottom_edge) * scaling:
- self.visible_drift_tiles.append([sprite.rect.x, sprite.rect.y])
# updating adjacent wall tiles y pos (left wall, right wall)
self.adjacent_wall_tiles_x_pos = [self.walls.sprites()[0].rect.x, self.walls.sprites()[1].rect.x]
@@ -339,29 +336,58 @@ class Level:
# check for level done: if player went over finish line => level_done
finish_line = self.finish_line.sprites()[-1]
if finish_line.rect.bottom < player.rect.top: # (observation_space_size_y - bottom_edge) * scaling:
- self.level_done = True
- self.quit = True
- # write data of all frames to csv
- self.get_data(scaling)
- self.data.to_csv(f'data/{self.code}_output_{self.convolutionGranularity}_{self.trial}_{self.n_run:0>2}.csv', sep=',', index=False)
+ if question_soc:
+ # ask for SoC:
+ display_soc_question(self.display_surface)
+ response = self.get_soc_response()
+ if response is not None:
+ self.level_done = True
+ self.quit = True
+ # write data of all frames to csv
+ self.get_data(scaling)
+ self.data.to_csv(f'data/{self.code}_output_{self.trial}_{self.n_run:0>2}.csv', sep=',', index=False)
+ else:
+ self.level_done = True
+ self.quit = True
+ # write data of all frames to csv
+ self.get_data(scaling)
+ self.data.to_csv(f'data/{self.code}_output_{self.trial}_{self.n_run:0>2}.csv', sep=',', index=False)
elif player.crashed:
- if self.time_played > self.replay_threshold:
- self.level_done = True
- self.quit = True
- # write data of all frames to csv
- self.get_data(scaling)
- self.data.to_csv(f'data/{self.code}_output_{self.convolutionGranularity}_{self.trial}_{self.n_run:0>2}.csv', sep=',', index=False)
+ if question_soc:
+ # ask for SoC:
+ display_soc_question(self.display_surface)
+ response = self.get_soc_response()
+ if response is not None:
+ if self.time_played > self.replay_threshold:
+ self.level_done = True
+ self.quit = True
+ # write data of all frames to csv
+ self.get_data(scaling)
+ self.data.to_csv(f'data/{self.code}_output_{self.trial}_{self.n_run:0>2}.csv', sep=',', index=False)
+ else:
+ if self.time_played > self.replay_threshold:
+ self.level_done = True
+ self.quit = True
+ # write data of all frames to csv
+ self.get_data(scaling)
+ self.data.to_csv(f'data/{self.code}_output_{self.trial}_{self.n_run:0>2}.csv', sep=',', index=False)
else:
self.level_done = False
- player.animate(self.current_input)
+ if not tiny_visualization and keyboard_input:
+ player.animate(self.current_input)
- if player.rect.y < player_position[1]:
+ if keyboard_input:
+ self.update()
+ else:
+ self.player.update(player_position, scaling, keyboard_input)
+
+ if keyboard_input and player.rect.y < player_position[1]:
player.approach(velocity, scaling)
pass
- if player.rect.y >= player_position[1]:
+ if not tiny_visualization and player.rect.y >= player_position[1]:
# update sprite positions
# update level tiles
self.comets.update(velocity, scaling, self.horizontal_movement)
@@ -370,16 +396,14 @@ class Level:
self.particles.update(velocity, scaling, self.horizontal_movement)
self.finish_line.update(velocity, scaling, self.horizontal_movement)
- # update action goal
- self.agent.update_action_goal(velocity, scaling, self.horizontal_movement)
-
# update input_noise threshold
self.input_noise_threshold -= 1 * scaling * velocity # same updating as for all in-game objects
- # check for collision
- self.check_for_collision()
- # check for drift
- self.check_for_drift()
+ if keyboard_input: # only needed if player is controlling spaceship
+ # check for collision
+ self.check_for_collision()
+ # check for drift
+ self.check_for_drift()
# draw sprites
# draw comets and tiles
@@ -389,24 +413,11 @@ class Level:
self.drift_tiles.draw(self.display_surface)
self.finish_line.draw(self.display_surface)
self.bottom_edge.draw(self.display_surface)
- # to display finish line when on screen but under bottom edge,
+ # to display finish line when on screen but under bottom edge,
# simply call draw method of buttom_edge.draw() AFTER finish_line.draw()
- self.update()
-
# draw agent
self.player.draw(self.display_surface)
- # draw transparent circle around action goal
- draw_circle_alpha(surface=self.display_surface, color=(255, 0, 0, 100), center=self.agent.action_goal, radius=degree_to_pixel(1))
- # draw fixated action goal
- pygame.draw.circle(self.display_surface, (255, 0, 0), self.agent.action_goal, 2)
- # draw SoC indicators
- # low-level
- pygame.draw.rect(self.display_surface, (50, 168, 82),
- (player.rect.x + 2*scaling, player.rect.y - self.agent.LL_SoC * 2*scaling, scaling, self.agent.LL_SoC * 2*scaling))
- # high-level
- pygame.draw.rect(self.display_surface, (232, 137, 12),
- (player.rect.x + 3.1*scaling, player.rect.y - self.agent.HL_SoC * 2*scaling, scaling, self.agent.HL_SoC * 2*scaling))
# draw keys
if display_keys: