@@ -70,8 +70,7 @@ def __init__(self, playground, time_limit=None, replay=False, screen=False):
70
70
self .surface_sensors = pygame .Surface ((self .playground .width , self .playground .length ))
71
71
72
72
self .game_on = True
73
- self .episode_elapsed_time = 0
74
- self .total_elapsed_time = 0
73
+ self .elapsed_time = 0
75
74
76
75
def multiple_steps (self , actions , n_steps = 1 ):
77
76
"""
@@ -109,16 +108,32 @@ def multiple_steps(self, actions, n_steps=1):
109
108
for agent_name in actions :
110
109
cumulated_rewards [agent_name ] = 0
111
110
112
- for _ in range (n_steps - 1 ):
113
- self .step (hold_actions )
111
+ step = 0
112
+ continue_actions = True
113
+
114
+ while step < n_steps and continue_actions :
115
+
116
+ if step < n_steps - 1 :
117
+ action = hold_actions
118
+ else :
119
+ action = last_action
120
+
121
+ self ._engine_step (action )
114
122
115
123
for agent in self .agents :
116
124
cumulated_rewards [agent .name ] += agent .reward
117
125
118
- self .step (last_action )
126
+ step += 1
127
+
128
+ reset , terminate = self ._handle_terminations ()
129
+
130
+ if reset or terminate :
131
+ continue_actions = False
119
132
120
133
for agent in self .agents :
121
- agent .reward += cumulated_rewards [agent .name ]
134
+ agent .reward = cumulated_rewards [agent .name ]
135
+
136
+ return reset , terminate
122
137
123
138
def step (self , actions ):
124
139
"""
@@ -129,37 +144,67 @@ def step(self, actions):
129
144
130
145
"""
131
146
132
- for agent in self .agents :
133
- agent .apply_actions_to_body_parts (actions [agent .name ])
134
-
135
- self .playground .update (SIMULATION_STEPS )
147
+ self ._engine_step (actions )
136
148
137
149
# Termination
138
- game_reset , game_terminates = self .game_terminated ()
150
+ reset , terminate = self ._handle_terminations ()
151
+
152
+ return reset , terminate
153
+
154
+ def _handle_terminations (self ):
155
+
156
+ reset = False
157
+ terminate = False
158
+
159
+ playground_terminated = self .playground .done
160
+ reached_time_limit = self ._check_time ()
161
+ keyboard_reset , keyboard_quit = self ._check_keyboard ()
162
+
163
+ if keyboard_quit :
164
+ terminate = True
165
+
166
+ elif keyboard_reset :
167
+ reset = True
168
+
169
+ elif playground_terminated :
170
+
171
+ if self .replay_until_time_limit :
172
+ reset = True
173
+
174
+ else :
175
+ terminate = True
139
176
140
- if game_reset :
141
- self .reset ()
177
+ elif reached_time_limit :
142
178
143
- if game_terminates :
144
- self .game_on = False
145
- self .terminate ()
179
+ terminate = True
146
180
147
- self .total_elapsed_time += 1
148
- self .episode_elapsed_time += 1
181
+ return reset , terminate
182
+
183
+ def _engine_step (self , actions ):
184
+
185
+ for agent in self .agents :
186
+ agent .apply_actions_to_body_parts (actions [agent .name ])
187
+
188
+ self .playground .update (SIMULATION_STEPS )
189
+
190
+ self .elapsed_time += 1
149
191
150
192
def reset (self ):
151
193
"""
152
194
Resets the game to its initial state.
153
195
154
196
"""
155
- self .episode_elapsed_time = 0
156
-
157
197
self .playground .reset ()
158
-
159
198
self .game_on = True
160
199
200
+ def _check_time (self ):
201
+ if self .elapsed_time >= self .time_limit :
202
+ return True
203
+ else :
204
+ return False
205
+
161
206
162
- def game_terminated (self ):
207
+ def _check_keyboard (self ):
163
208
"""
164
209
Tests whether the game came to an end, because of time limit or termination of playground.
165
210
@@ -170,14 +215,6 @@ def game_terminated(self):
170
215
reset_game = False
171
216
terminate_game = False
172
217
173
- if self .total_elapsed_time == self .time_limit or self .playground .done :
174
-
175
- if self .replay_until_time_limit and self .total_elapsed_time < self .time_limit :
176
- reset_game = True
177
- else :
178
- terminate_game = True
179
-
180
-
181
218
if self .screen is not None :
182
219
183
220
pygame .event .get ()
@@ -198,10 +235,7 @@ def game_terminated(self):
198
235
elif pygame .key .get_pressed ()[K_r ] and self .reset_key_ready is True :
199
236
self .reset_key_ready = False
200
237
201
- if self .replay_until_time_limit :
202
- reset_game = True
203
- else :
204
- terminate_game = True
238
+ reset_game = True
205
239
206
240
return reset_game , terminate_game
207
241
@@ -349,7 +383,7 @@ def run(self, steps=None, with_screen = False, print_rewards = False):
349
383
for agent in self .agents :
350
384
actions [agent .name ] = agent .controller .generate_actions ()
351
385
352
- self .step (actions )
386
+ reset , terminate = self .step (actions )
353
387
self .update_observations ()
354
388
355
389
if with_screen and self .game_on :
@@ -366,12 +400,15 @@ def run(self, steps=None, with_screen = False, print_rewards = False):
366
400
if steps == 0 :
367
401
continue_for_n_steps = False
368
402
403
+ if reset :
404
+ self .reset ()
405
+
406
+ if terminate :
407
+ continue_for_n_steps = False
408
+ self .terminate ()
369
409
370
- # for agent in self.agents:
371
- # print(agent.position, agent.base_platform.pm_body.velocity, agent.base_platform.pm_body.kinetic_energy)
372
- # assert 0 < agent.position[0] < self.playground.size[0]
373
- # assert 0 < agent.position[1] < self.playground.size[1]
374
410
375
411
def terminate (self ):
376
412
413
+ self .game_on = False
377
414
pygame .quit ()
0 commit comments