3434
3535logger = logging .getLogger ("scenario-runner" )
3636
37+ infractions_dict = {
38+ "OutsideRouteLanesTest" : 0.3 ,
39+ "CollisionTest" : 1.0 ,
40+ "RunningRedLightTest" : 0.4 ,
41+ "RunningStopTest" : 0.25 ,
42+ }
43+
44+ terminations_dict = {"AgentBlockedTest" : 0.0 }
45+
3746
3847class AWScenarioRunner (object ):
3948 # flags
@@ -50,6 +59,7 @@ class AWScenarioRunner(object):
5059 definition_manager = None
5160
5261 aw_agent = None
62+ host_volume = os .environ ["SR_HOST_VOLUME" ]
5363
5464 def __init__ (self , config : dict ) -> None :
5565 """
@@ -178,12 +188,14 @@ def run_scenario(
178188 # allow the agent to localise and set the route
179189 budget = int (self ._scenario_config ["initialisation_budget" ])
180190 status = False # completion status
181- for tick in range (0 , budget ):
191+ for tick in range (1 , budget + 1 ):
182192 self .carla_world .tick ()
183193 status = route_config .agent .run_step_init () # type: ignore
184194
185- if status :
186- logger .info (f"Successfully initialised agent in { tick } ticks" )
195+ if not status :
196+ logger .info ("Agent failed to initialise route." )
197+ else :
198+ logger .info ("Successfully initialised agent; route set." )
187199
188200 if self ._tm_config ["active" ]:
189201 logger .info ("Loading Traffic Manager..." )
@@ -194,7 +206,7 @@ def run_scenario(
194206 tm .set_random_device_seed (int (self ._tm_config ["seed" ])) # ADD TO CONFIG
195207 tm .set_synchronous_mode (self ._tm_config ["sync" ])
196208
197- try : # the route gets sent to the agent here
209+ try :
198210 scenario = RouteScenario (
199211 world = self .carla_world ,
200212 config = route_config ,
@@ -208,14 +220,12 @@ def run_scenario(
208220
209221 logger .info ("Starting scenario..." )
210222 try :
211- # recorder_name = f"{self.results_manager.last_scenario}/recording.log"
212- # self.carla_client.start_recorder(recorder_name, True)
223+ self .carla_client .start_recorder ("/home/carla/recording.log" , True )
213224 self .scenario_manager .load_scenario (
214225 scenario , self .aw_agent , follow_ego = self ._scenario_config ["follow_ego" ]
215226 )
216227 self .scenario_manager .run_scenario ()
217-
218- # self.carla_client.stop_recorder()
228+ self .carla_client .stop_recorder ()
219229 result = True
220230 except Exception :
221231 traceback .print_exc ()
@@ -269,7 +279,7 @@ def run(self) -> None:
269279 for iteration in range (self .iterations ):
270280 logger .info ("Starting CARLA container...." )
271281 CARLAManager .restart_carla ()
272- time .sleep (5 )
282+ time .sleep (5 ) # allow CARLA to load
273283
274284 self .curr_iteration = iteration
275285 logger .info (f"Starting algorithm iteration number { self .curr_iteration } " )
@@ -297,7 +307,7 @@ def run(self) -> None:
297307 scenario_result .put (result_dict )
298308
299309 scenario_process = multiprocessing .Process (
300- target = self .run_scenario ,
310+ target = self .run_scenario , # need to catch connection exception
301311 args = (
302312 route_config ,
303313 env_config ,
@@ -316,7 +326,18 @@ def run(self) -> None:
316326 if scenario_process .is_alive ():
317327 scenario_process .kill ()
318328
329+ # copy over the recording from CARLA container if env variable is setup
330+ if self .host_volume is not None :
331+ CARLAManager .fetch_file (
332+ "/home/carla/recording.log" ,
333+ f"{ self .host_volume } /{ self .results_manager .last_scenario } /recording.log" ,
334+ )
335+
336+ logger .info ("Calculating driving score..." )
319337 driving_score = self ._calculate_driving_score (result ["criteria" ])
338+ logger .info (
339+ f"Scenario iteration { iteration } achieved a score of { driving_score } "
340+ )
320341
321342 # read the scenario definition
322343 if not self .DEV_MODE :
@@ -352,8 +373,31 @@ def _output_criteria(
352373 return criteria_dict
353374
354375 def _calculate_driving_score (self , criteria : dict ) -> float :
355- # to be implemented
356- return 0.0
376+ driving_score = 0.0
377+
378+ for key in terminations_dict .keys ():
379+ if not criteria [key ]["success_value" ] == criteria [key ]["actual_value" ]:
380+ logger .info (f"Found terminal condition { key } ." )
381+ return 0.0 # hit a termination condition, driving score of 0.0
382+
383+ completed_route = float (criteria ["RouteCompletionTest" ]["actual_value" ]) / 100
384+ logger .info (f"Agent route completion: { completed_route * 100 } %" )
385+
386+ logger .info ("Checking penality conditions..." )
387+ penalties = 1
388+ for infraction , penalty in infractions_dict .items ():
389+ delta_penalty = float (criteria [infraction ]["actual_value" ] * penalty )
390+
391+ if delta_penalty :
392+ logger .info (
393+ f"Condition { infraction } : Breached { criteria [infraction ]['actual_value' ]} times"
394+ )
395+ logger .info (f"Applying penalty of { delta_penalty } " )
396+ else :
397+ logger .info (f"Condition { infraction } : Found zero breaches" )
398+
399+ driving_score = completed_route * (1 / penalties )
400+ return driving_score
357401
358402 def destroy (self ) -> None :
359403 """Deletes instances of all classes related to CARLA"""
0 commit comments